use Cubist\Backpack\app\Template\TemplateAbstract;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\Table;
+use Illuminate\Support\Facades\DB;
class CMSPage extends CubistMagicNestedModel
{
protected static $_templates = [];
+ protected $_usedTemplate = null;
+ protected static $_templatesById = null;
+ protected static $_pagesList = null;
+
+ protected static $_table = 'cubist_cms_pages';
protected $table = 'cubist_cms_pages';
protected $_options = ['name' => 'page',
protected $defaultFieldAttributes = ['translatable' => true];
- public static function boot()
- {
- parent::boot();
- }
-
public function setFields()
{
parent::setFields();
*/
public function onBeforeCreate($controller)
{
- $this->useTemplate(request('template'), $controller);
+ $this->useTemplateIfNotSet(request('template'), $controller);
parent::onBeforeCreate($controller);
}
$template = request('template');
// if the template in the GET parameter is missing, figure it out from the db
if ($template == false) {
- /** @var self $entry */
- $controller->data['entry'] = self::findOrFail($id);
- $template = $controller->data['entry']->template;
+ $entry = self::findOrFail($id);
+ $template = $entry->template;
}
- $this->useTemplate($template, $controller);
+ $this->useTemplateIfNotSet($template);
+ $controller->updateFieldsFromModel($this);
parent::onBeforeEdit($controller, $id);
}
public function onBeforeStore($controller, $request)
{
- $this->useTemplate(\Request::input('template'), $controller);
+ $this->useTemplateIfNotSet(\Request::input('template'));
+ $controller->updateFieldsFromModel($this);
parent::onBeforeStore($controller, $request);
}
public function onBeforeUpdate($controller, $request)
{
- $this->useTemplate(\Request::input('template'), $controller);
+ $this->useTemplateIfNotSet(\Request::input('template'));
+ $controller->updateFieldsFromModel($this);
parent::onBeforeUpdate($controller, $request);
}
* @param $controller CubistMagicController
* @throws \Exception
*/
- protected function useTemplate($template, $controller = null)
+ public function useTemplate($template)
{
if (is_string($template)) {
$template = TemplateAbstract::getTemplateIntanceByName($template);
}
+
if (null === $template) {
return;
}
+ $this->_usedTemplate = $template;
+
$fields = $template->getFields();
if (!count($fields)) {
return;
$attr = array_merge($this->defaultFieldAttributes, $field);
$this->addFakeField($attr);
}
- if (null !== $controller) {
- $controller->updateFieldsFromModel();
+ }
+
+ public function useTemplateIfNotSet($template)
+ {
+ if ($this->_usedTemplate === null) {
+ $this->useTemplate($template);
}
}
return parent::update($attributes, $options);
}
- public static function getMenu(){
- }
+ public function setRawAttributes(array $attributes, $sync = false)
+ {
+ if (isset($attributes['template']) && null === $this->_usedTemplate) {
+ $this->useTemplateIfNotSet($attributes['template']);
+ }
+ $res = parent::setRawAttributes($attributes, $sync);
+ return $res;
+ }
+ public static function getPagesList()
+ {
+ if (null === static::$_pagesList) {
+ static::$_pagesList = DB::table(self::$_table)->orderBy('lft')->get()->pluck('name', 'id');
+ }
+ return static::$_pagesList;
+ }
}
use Doctrine\DBAL\Schema\Table;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
+use mysql_xdevapi\Exception;
use Spatie\MediaLibrary\HasMedia\HasMedia;
use Spatie\MediaLibrary\HasMedia\HasMediaTrait;
use Spatie\MediaLibrary\Models\Media;
if (!in_array($name, $this->fillable)) {
$this->fillable[] = $name;
}
- //$this->casts[$store_in] = 'array';
- if ($field->getAttribute('translatable')) {
+ $this->casts[$store_in] = 'array';
+ if ($field->getAttribute('translatable') && !in_array($store_in, $this->translatable)) {
$this->translatable[] = $store_in;
}
- $this->fakeColumns[] = $store_in;
+ if (!in_array($store_in, $this->fakeColumns)) {
+ $this->fakeColumns[] = $store_in;
+ }
} else {
if ($field->getAttribute('fillable')) {
$this->fillable[] = $name;
protected function _prepareData($attributes)
{
return Json::decodeRecursive($attributes, Json::TYPE_ARRAY);
-
- foreach ($attributes as $key => $attribute) {
- if (is_array($attribute) || is_object($attribute)) {
- $res[$key] = json_encode($attribute);
- } else {
- $res[$key] = $attribute;
- }
- }
- return $res;
}
public function update(array $attributes = [], array $options = [])
$link_types = ['none' => 'Désactiver', 'page_link' => trans('backpack::crud.page_link'), 'internal_link' => trans('backpack::crud.internal_link'), 'external_link' => trans('backpack::crud.external_link')];
$field['allows_null'] = false;
$page_model = $field['page_model'];
-$active_pages = $page_model::all();
+$active_pages = \Cubist\Backpack\app\Magic\Models\CMSPage::getPagesList();
$empty = ['label' => '', 'type' => 'page_link', 'link' => '', 'external_link' => '', 'page_id' => ''];
$value = old(square_brackets_to_dots($field['name'])) ?? $field['value'] ?? $field['default'] ?? [];
@if (!count($active_pages))
<option value="">-</option>
@else
- @foreach ($active_pages as $key => $page)
- <option value="{{ $page->id }}"
- @if (isset($value['page_id']) && $page->id==$value['page_id'])
+ @foreach ($active_pages as $id => $page)
+ <option value="{{ $id }}"
+ @if (isset($value['page_id']) && $id==$value['page_id'])
selected
@endif
- >{{ $page->name }}</option>
+ >{{ $page }}</option>
@endforeach
@endif