Skip to content

Commit

Permalink
Added Model for database source. Added Template::getOriginal method f…
Browse files Browse the repository at this point in the history
…or nova-flexible-content (HasMediaLibrary issue) compatibility. References #42
  • Loading branch information
toonvandenbos committed Aug 23, 2019
1 parent c7af390 commit 7d0d15d
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 24 deletions.
10 changes: 10 additions & 0 deletions src/Pages/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
77 changes: 59 additions & 18 deletions src/Sources/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -33,47 +47,74 @@ 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(),
'attributes' => json_encode($template->getAttributes()),
'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;
}

/**
Expand Down Expand Up @@ -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 . '".';
}
}
20 changes: 15 additions & 5 deletions src/Sources/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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
*
Expand Down
1 change: 1 addition & 0 deletions src/Sources/SourceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
35 changes: 35 additions & 0 deletions src/Sources/StaticPage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Whitecube\NovaPage\Sources;

use Illuminate\Database\Eloquent\Model;

class StaticPage extends Model
{
/**
* The table associated with the model.
*
* @var string
*/
protected $table;

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['type', 'name', 'title', 'attributes'];

/**
* Create a new Eloquent model instance.
*
* @param array $attributes
* @return void
*/
public function __construct(array $attributes = [])
{
$this->table = config('novapage.sources.database.table_name');

parent::__construct($attributes);
}
}
3 changes: 2 additions & 1 deletion src/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
],
]

Expand Down

0 comments on commit 7d0d15d

Please sign in to comment.