richard
/
cyca
Archived
1
0
Fork 0
This repository has been archived on 2024-05-04. You can view files and clone it, but cannot push or open issues or pull requests.
cyca/app/Services/Exporter.php

171 lines
3.8 KiB
PHP
Executable File

<?php
namespace App\Services;
use App\Models\Folder;
use App\Models\User;
/**
* Exports data from Cyca.
*/
class Exporter
{
/**
* Should we include highlights in the export ?
*
* @var bool
*/
protected $withHighlights = true;
/**
* Should we include bookmarks in the export ?
*
* @var bool
*/
protected $withBookmarks = true;
/**
* User to import data for.
*
* @var \App\Models\User
*/
protected $forUser;
/**
* Folder to export bookmarks and feeds from.
*
* @var \App\Models\Folder
*/
protected $fromFolder;
/**
* Should we ignore highlights during export ? By default, highlights will
* be exported as well.
*
* @return self
*/
public function withoutHighlights()
{
$this->withHighlights = false;
return $this;
}
/**
* Should we ignore bookmarks during export ? By default, bookmarks will
* be exported as well.
*
* @return self
*/
public function withoutBookmarks()
{
$this->withoutBookmarks = false;
return $this;
}
/**
* Defines which user to export data for. If not defined, user will be
* extracted from request.
*
* @return self
*/
public function forUser(User $user)
{
$this->forUser = $user;
return $this;
}
/**
* Defines from which folder data will be exported. If not defined, root
* folder attached to specified user will be used.
*
* @return self
*/
public function fromFolder(Folder $folder)
{
$this->fromFolder = $folder;
return $this;
}
/**
* Export specified user's data into a PHP array.
*
* @return array
*/
public function export()
{
if (empty($this->fromFolder)) {
$this->fromFolder = $this->forUser->groups()->active()->first()->folders()->ofType('root')->first();
}
$rootArray = [
'documents' => [],
'folders' => $this->exportTree($this->fromFolder->children()->get()),
];
foreach ($this->fromFolder->documents()->get() as $document) {
$documentArray = [
'url' => $document->url,
'feeds' => [],
];
foreach ($document->feeds()->get() as $feed) {
$documentArray['feeds'] = [
'url' => $feed->url,
'is_ignored' => $feed->is_ignored,
];
}
$rootArray['documents'][] = $documentArray;
}
return [
'highlights' => $this->forUser->highlights()->select(['expression', 'color'])->get(),
'bookmarks' => $rootArray,
];
}
/**
* Export a single tree branch.
*
* @param mixed $folders
*/
protected function exportTree($folders)
{
$array = [];
foreach ($folders as $folder) {
$folderArray = [
'title' => $folder->title,
'documents' => [],
'folders' => [],
];
foreach ($folder->documents()->get() as $document) {
$documentArray = [
'url' => $document->url,
'feeds' => [],
];
foreach ($document->feeds()->get() as $feed) {
$documentArray['feeds'][] = [
'url' => $feed->url,
'is_ignored' => $feed->is_ignored,
];
}
$folderArray['documents'][] = $documentArray;
}
$folderArray['folders'] = $this->exportTree(($folder->children()->get()));
$array[] = $folderArray;
}
return $array;
}
}