304 lines
8.2 KiB
PHP
304 lines
8.2 KiB
PHP
<?php
|
|
namespace App\Models\Content\ModelTraits;
|
|
|
|
use App\CMS\Contracts\Repositories\Content as IContent;
|
|
use App\Http\Requests\Request;
|
|
use App\Models\Content\ContentHasFile;
|
|
use App\Models\Content\ContentHistory;
|
|
use App\Models\FileManager\Directory;
|
|
use App\Models\FileManager\File;
|
|
use Illuminate\Support\Facades\App;
|
|
|
|
trait Content
|
|
{
|
|
/**
|
|
* @var File
|
|
*/
|
|
private $file;
|
|
protected $foundSubpages = [];
|
|
protected $subpagesInit = false;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->file = App::make(File::class);
|
|
}
|
|
|
|
/**
|
|
* Is current content draft?
|
|
*
|
|
* @return bool
|
|
*/
|
|
public function isDraft()
|
|
{
|
|
return $this->status == IContent::STATUS_DRAFT;
|
|
}
|
|
|
|
/**
|
|
* Is current content visible for all visitors?
|
|
*
|
|
* @return bool
|
|
*/
|
|
public function isPublic()
|
|
{
|
|
return $this->status == IContent::STATUS_PUBLIC;
|
|
}
|
|
|
|
/**
|
|
* Is current content visible only for logged users?
|
|
*
|
|
* @return bool
|
|
*/
|
|
public function isProtected()
|
|
{
|
|
return $this->status == IContent::STATUS_PROTECTED;
|
|
}
|
|
|
|
/**
|
|
* Is current content visible for logged user OR for everyone?
|
|
*
|
|
* @return bool
|
|
*/
|
|
public function isVisible()
|
|
{
|
|
return $this->isPublic() || $this->isProtected();
|
|
}
|
|
|
|
/**
|
|
* Get list of all subpages
|
|
*
|
|
* @param bool $orderBy
|
|
* @param bool $orderDirection
|
|
*
|
|
* @return array
|
|
*/
|
|
public function subpages($orderBy = false, $orderDirection = false)
|
|
{
|
|
if (!$this->subpagesInit) {
|
|
$subpages = $this->getPageQuery()
|
|
->where('contents.parent_id', $this->id);
|
|
|
|
if ($orderBy) {
|
|
$subpages->orderBy($orderBy, $orderDirection ? $orderDirection : 'ASC');
|
|
}
|
|
|
|
$subpages = $subpages->get();
|
|
|
|
// convert JSON settings into stdClass
|
|
foreach ($subpages as $i => $subpage) {
|
|
$subpages[$i]->module_settings = json_decode($subpage->module_settings);
|
|
}
|
|
|
|
$this->subpagesInit = true;
|
|
$this->foundSubpages = $subpages;
|
|
}
|
|
|
|
return $this->foundSubpages;
|
|
}
|
|
|
|
/**
|
|
* Returns list of all root pages - those pages, which parent_id is null
|
|
*
|
|
* @return mixed
|
|
*/
|
|
public function rootPages()
|
|
{
|
|
$rootPages = $this->getPageQuery()
|
|
->where('parent_id', null);
|
|
|
|
if ($this->id) {
|
|
$rootPages = $rootPages->where('id', '!=', $this->id);
|
|
}
|
|
|
|
return $rootPages->get();
|
|
}
|
|
|
|
/**
|
|
* Updates current model with information about changed columns
|
|
*
|
|
* @param array $fields
|
|
* @param array $request
|
|
* @param string $revisionId
|
|
*
|
|
* @return array revisionID and number of changed columns
|
|
*/
|
|
public function updateWithHistory(array $fields, array $request, $revisionId = null)
|
|
{
|
|
if (!$revisionId) {
|
|
$revisionId = uniqid();
|
|
}
|
|
|
|
$changed = 0;
|
|
|
|
foreach ($fields as $fieldDB => $fieldRequest) {
|
|
// ignore non-existing fields
|
|
if (!isset($request[$fieldRequest]) && !is_null($request[$fieldRequest])) {
|
|
continue;
|
|
}
|
|
|
|
// if module settings is still JSON, decode it to normal stdClass
|
|
if (is_string($this->module_settings)) {
|
|
$this->module_settings = json_decode($this->module_settings);
|
|
}
|
|
|
|
// are we editing common settings, or module-specific settings?
|
|
if (!in_array($fieldDB, $this->fillable)) {
|
|
// this field is not part of "core" content - probably it will be module settings
|
|
$commonField = false;
|
|
$fieldValue = isset($this->module_settings->$fieldDB) ? $this->module_settings->$fieldDB : null;
|
|
} else {
|
|
$commonField = true;
|
|
$fieldValue = $this->$fieldDB;
|
|
}
|
|
|
|
// remove any whitespaces around real content
|
|
if (!is_null($request[$fieldRequest])) {
|
|
$request[$fieldRequest] = trim($request[$fieldRequest]);
|
|
}
|
|
|
|
if (trim($fieldValue) != $request[$fieldRequest]) {
|
|
|
|
$this->saveHistory($revisionId, $fieldRequest, $fieldValue, $request);
|
|
|
|
// save change into database
|
|
if ($commonField) {
|
|
$this->$fieldDB = $request[$fieldRequest];
|
|
} else {
|
|
$this->module_settings->$fieldDB = $request[$fieldRequest];
|
|
}
|
|
|
|
$changed++;
|
|
}
|
|
}
|
|
|
|
// make JSON from stdClass
|
|
if (!is_string($this->module_settings)) {
|
|
$this->module_settings = json_encode($this->module_settings);
|
|
}
|
|
|
|
$this->save();
|
|
|
|
return [$revisionId, $changed];
|
|
}
|
|
|
|
/**
|
|
* Upload and set some image as title photo
|
|
*
|
|
* @param Request $request
|
|
*/
|
|
public function uploadTitlePhoto(Request $request)
|
|
{
|
|
if ($request->hasFile('titlePhoto')) {
|
|
return $this->file->upload($request->file('titlePhoto'), $request);
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Upload and assign files to newly created content page
|
|
*
|
|
* @param Request $request
|
|
* @param null $revisionId
|
|
*
|
|
* @return int
|
|
*/
|
|
public function uploadRelatedFiles(Request $request, $revisionId = null)
|
|
{
|
|
$files = $request->all()['files'];
|
|
$changed = 0;
|
|
|
|
// check if any file was removed
|
|
if ($revisionId) {
|
|
$changed += $this->removeRelatedFiles($revisionId, $request);
|
|
}
|
|
|
|
// allow uploading only on created pages
|
|
if (!$this->id || !$files[0]) {
|
|
return $changed;
|
|
}
|
|
|
|
// find or create new directory for files assigned to content
|
|
$directory = Directory::firstOrCreate([
|
|
'name' => '.content_files',
|
|
'hidden' => true
|
|
]);
|
|
$request->directoryId = $directory->id;
|
|
|
|
foreach ($files as $file) {
|
|
if (!$file) {
|
|
continue;
|
|
}
|
|
$uploaded = $this->file->upload($file, $request);
|
|
|
|
// add assignement
|
|
ContentHasFile::create([
|
|
'file_id' => $uploaded['id'],
|
|
'content_id' => $this->id
|
|
]);
|
|
|
|
if ($revisionId) {
|
|
$this->saveHistory($revisionId, 'file', null, ['file' => $uploaded['file']->getClientOriginalName()]);
|
|
}
|
|
|
|
$changed++;
|
|
}
|
|
|
|
return $changed;
|
|
}
|
|
|
|
/**
|
|
* Delete files from association
|
|
*
|
|
* @param $revisionId
|
|
* @param $request
|
|
*
|
|
* @return int
|
|
*/
|
|
private function removeRelatedFiles($revisionId, $request)
|
|
{
|
|
$files = array_keys((array)$request->keepAssigned);
|
|
|
|
$allFiles = $this->files()
|
|
->whereNotIn('id', $files)
|
|
->get();
|
|
|
|
foreach ($allFiles as $file) {
|
|
$this->saveHistory($revisionId, 'file', $file->original_filename, ['file' => null]);
|
|
|
|
ContentHasFile::where('content_id', $this->id)
|
|
->where('file_id', $file->id)
|
|
->delete();
|
|
}
|
|
|
|
return count($allFiles);
|
|
}
|
|
|
|
/**
|
|
* Save history entry into database
|
|
*
|
|
* @param $revisionId
|
|
* @param $fieldRequest
|
|
* @param $fieldValue
|
|
* @param $request
|
|
*/
|
|
private function saveHistory($revisionId, $fieldRequest, $fieldValue, $request)
|
|
{ // setup history object
|
|
$history = [
|
|
'user_id' => \Auth::user()->id,
|
|
'content_id' => $this->id,
|
|
'edit_batch' => $revisionId,
|
|
'column' => $fieldRequest,
|
|
'old_value' => $fieldValue,
|
|
'new_value' => $request[$fieldRequest]
|
|
];
|
|
|
|
// save history
|
|
ContentHistory::create($history);
|
|
}
|
|
|
|
public function view()
|
|
{
|
|
$this->viewed++;
|
|
$this->save(['timestamps' => false]);
|
|
}
|
|
} |