From 7d0d15deaac8565a0d9c53a758fc7cf20f6a58b8 Mon Sep 17 00:00:00 2001 From: Toon Van den Bos Date: Fri, 23 Aug 2019 17:40:24 +0200 Subject: [PATCH] Added Model for database source. Added Template::getOriginal method for nova-flexible-content (HasMediaLibrary issue) compatibility. References #42 --- src/Pages/Template.php | 10 +++++ src/Sources/Database.php | 77 +++++++++++++++++++++++++-------- src/Sources/Filesystem.php | 20 ++++++--- src/Sources/SourceInterface.php | 1 + src/Sources/StaticPage.php | 35 +++++++++++++++ src/config.php | 3 +- 6 files changed, 122 insertions(+), 24 deletions(-) create mode 100644 src/Sources/StaticPage.php diff --git a/src/Pages/Template.php b/src/Pages/Template.php index ab55797..56887ae 100644 --- a/src/Pages/Template.php +++ b/src/Pages/Template.php @@ -107,6 +107,16 @@ public function getSource() : SourceInterface return $this->source; } + /** + * Get underlying source object + * + * @return mixed + */ + public function getOriginal() + { + return $this->getSource()->getOriginal($this); + } + /** * Load the page's static content if needed * diff --git a/src/Sources/Database.php b/src/Sources/Database.php index 261d270..67c1837 100644 --- a/src/Sources/Database.php +++ b/src/Sources/Database.php @@ -14,7 +14,21 @@ class Database implements SourceInterface { * * @var string */ - protected $tableName; + protected $table; + + /** + * The model used to store static pages content + * + * @var string + */ + protected $model; + + /** + * The fetched model instance + * + * @var \Illuminate\Database\Eloquent\Model + */ + protected $original; /** * Retrieve the source's name @@ -33,40 +47,48 @@ public function getName() */ public function setConfig(array $config) { - $this->tableName = $config['table_name']; + $this->table = $config['table_name']; + $this->model = $config['model']; } /** - * Retrieve data from the filesystem + * Retrieve data from the database * * @param \Whitecube\NovaPage\Pages\Template $template * @return object */ public function fetch(Template $template) { - $staticPage = DB::table($this->tableName)->where('name', $template->getName())->first(); - if ($staticPage) { - return [ - 'title' => $staticPage->title, - 'created_at' => $staticPage->created_at, - 'updated_at' => $staticPage->updated_at, - 'attributes' => $this->getParsedAttributes($template, json_decode($staticPage->attributes, true)) ?? [] - ]; + $model = $this->getOriginal($template); + + if(!$model->id) { + return; } - return; + + $attributes = $this->getParsedAttributes( + $template, + $model->attributes ? json_decode($model->attributes, true) : [] + ); + + return [ + 'title' => $model->title, + 'created_at' => $model->created_at, + 'updated_at' => $model->updated_at, + 'attributes' => $attributes + ]; } /** - * Save template in the filesystem + * Save template in the database * * @param \Whitecube\NovaPage\Pages\Template $template - * @return bool + * @return void */ public function store(Template $template) { - DB::table($this->tableName)->updateOrInsert([ - 'name' => $template->getName() - ], [ + $original = $this->getOriginal($template); + + $original->fill([ 'name' => $template->getName(), 'title' => $template->getTitle(), 'type' => $template->getType(), @@ -74,6 +96,25 @@ public function store(Template $template) 'created_at' => $template->getDate('created_at'), 'updated_at' => Carbon::now() ]); + + $original->save(); + } + + /** + * Retrieve original StaticPage model + * + * @param \Whitecube\NovaPage\Pages\Template $template + * @return null|\Illuminate\Database\Eloquent\Model + */ + public function getOriginal(Template $template) + { + if(!$this->original) { + $instance = call_user_func($this->model . '::where', 'name', $template->getName())->first(); + + $this->original = $instance ?? (new $this->model); + } + + return $this->original; } /** @@ -101,6 +142,6 @@ protected function getParsedAttributes(Template $template, $attributes) */ public function getErrorLocation($type, $name) { - return $this->getName() . ' table "' . $this->tableName . '". Page "' . $name . '".'; + return $this->getName() . ' table "' . $this->table . '". Page "' . $name . '".'; } } diff --git a/src/Sources/Filesystem.php b/src/Sources/Filesystem.php index 7192595..5335895 100644 --- a/src/Sources/Filesystem.php +++ b/src/Sources/Filesystem.php @@ -46,13 +46,11 @@ public function setConfig(array $config) */ public function fetch(Template $template) { - $file = $this->getFilePath($template->getType(), $template->getName()); - - if(!($file = realpath($file))) { + if(!($path = realpath($this->getOriginal($template)))) { return; } - return $this->parse($template, $file); + return $this->parse($template, $path); } /** @@ -69,12 +67,24 @@ public function store(Template $template) $data['updated_at'] = Carbon::now()->toDateTimeString(); $data['attributes'] = $template->getAttributes(); - $path = $this->getFilePath($template->getType(), $template->getName()); + $path = $this->getOriginal($template); + $this->makeDirectory($path); return file_put_contents($path, json_encode($data, JSON_PRETTY_PRINT, 512)); } + /** + * Retrieve original storage path + * + * @param \Whitecube\NovaPage\Pages\Template $template + * @return string + */ + public function getOriginal(Template $template) + { + return $this->getFilePath($template->getType(), $template->getName()); + } + /** * Build the path to the file using its identifier * diff --git a/src/Sources/SourceInterface.php b/src/Sources/SourceInterface.php index 9119b64..16e0151 100644 --- a/src/Sources/SourceInterface.php +++ b/src/Sources/SourceInterface.php @@ -10,5 +10,6 @@ public function getName(); public function setConfig(array $config); public function fetch(Template $template); public function store(Template $template); + public function getOriginal(Template $template); public function getErrorLocation($type, $name); } diff --git a/src/Sources/StaticPage.php b/src/Sources/StaticPage.php new file mode 100644 index 0000000..4956f3a --- /dev/null +++ b/src/Sources/StaticPage.php @@ -0,0 +1,35 @@ +table = config('novapage.sources.database.table_name'); + + parent::__construct($attributes); + } +} diff --git a/src/config.php b/src/config.php index 7f61728..8ac4347 100644 --- a/src/config.php +++ b/src/config.php @@ -78,7 +78,8 @@ 'path' => resource_path('lang/{type}/{key}.json') ], 'database' => [ - 'table_name' => 'static_pages' + 'table_name' => 'static_pages', + 'model' => \Whitecube\NovaPage\Sources\StaticPage::class ], ]