+++ /dev/null
-<?php
-
-namespace Cubist\Backpack\app\Http\Controllers;
-
-use Backpack\PageManager\app\Http\Controllers\Admin\PageCrudController;
-use Cubist\Backpack\app\Template\TemplateAbstract;
-use Illuminate\Support\Str;
-
-class CubistPageCrudController extends PageCrudController
-{
- protected static $_templates = null;
-
- /**
- * Get all defined templates.
- * @return TemplateAbstract[]
- */
- public function getTemplates($template_name = false)
- {
- if (null === self::$_templates) {
- $templates_root = app_path() . '/Templates';
- $dr = opendir($templates_root);
- while ($file = readdir($dr)) {
- if ($file == '.' || $file == '..' || is_dir($templates_root . '/' . $file)) {
- continue;
- }
- $e = explode('.', $file);
- $classname = '\\App\\Templates\\' . $e[0];
- self::$_templates[] = new $classname();
- }
-
- if (!count(self::$_templates)) {
- abort(503, trans('backpack::pagemanager.template_not_found'));
- }
- }
- return self::$_templates;
- }
-
- /**
- * Add the fields defined for a specific template.
- *
- * @param string $template_name The name of the template that should be used in the current form.
- */
- public function useTemplate($template_name = false)
- {
- $templates = $this->getTemplates();
-
- // set the default template
- if ($template_name == false) {
- $template_name = $templates[0]->getSlug();
- }
- // actually use the template
- if ($template_name) {
- foreach ($templates as $template) {
- if ($template->getSlug() == $template_name) {
- $template->use($this->crud);
- }
- }
- }
- }
-
- // -----------------------------------------------
- // Methods that are particular to the PageManager.
- // -----------------------------------------------
-
- /**
- * Populate the create/update forms with basic fields, that all pages need.
- *
- * @param string $template The name of the template that should be used in the current form.
- */
- public function addDefaultPageFields($template = false)
- {
- $this->crud->addField([
- 'name' => 'template',
- 'label' => trans('backpack::pagemanager.template'),
- 'type' => 'select_page_template',
- 'view_namespace' => 'pagemanager::fields',
- 'options' => $this->getTemplatesArray(),
- 'value' => $template,
- 'allows_null' => false,
- 'wrapperAttributes' => [
- 'class' => 'form-group col-md-6',
- ],
- 'tab' => 'General',
- ]);
- }
-
-
- /**
- * Get all defined template as an array.
- *
- * Used to populate the template dropdown in the create/update forms.
- */
- public function getTemplatesArray()
- {
- $templates_array = [];
-
- $templates = $this->getTemplates();
- foreach ($templates as $template) {
- if ($template->showInDropDown()) {
- $slug = $template->getSlug();
- $name = $template->getName();
- $templates_array[$slug] = str_replace('_', ' ', Str::title($name));
- }
- }
-
- return $templates_array;
- }
-}
$template = request('template');
// if the template in the GET parameter is missing, figure it out from the db
if ($template == false) {
- $entry = self::findOrFail($id);
- $template = $entry->template;
+ $template = self::getTemplatesById()[$id];
}
$this->useTemplateIfNotSet($template);
}
}
+
+ public static function getTemplatesById()
+ {
+ if (null === self::$_templatesById) {
+ self::$_templatesById = DB::table(self::$_table)->get()->pluck('template', 'id');
+ }
+ return self::$_templatesById;
+ }
+
/**
* @param $schema Schema
* @return Table
return parent::update($attributes, $options);
}
-
public function setRawAttributes(array $attributes, $sync = false)
{
if (isset($attributes['template']) && null === $this->_usedTemplate) {
}
return static::$_pagesList;
}
+
+ public function addFakes($columns = ['extras'])
+ {
+ return parent::addFakes($columns);
+ }
}
if (!in_array($name, $this->fillable)) {
$this->fillable[] = $name;
}
- $this->casts[$store_in] = 'array';
- if ($field->getAttribute('translatable') && !in_array($store_in, $this->translatable)) {
- $this->translatable[] = $store_in;
+ // do not enable it again !!
+ // $this->casts[$store_in] = 'array';
+ if ($field->getAttribute('translatable')) {
+ if (!in_array($store_in, $this->translatable)) {
+ $this->translatable[] = $store_in;
+ }
+ if (!in_array($name, $this->translatable)) {
+ $this->translatable[] = $name;
+ }
}
+
if (!in_array($store_in, $this->fakeColumns)) {
$this->fakeColumns[] = $store_in;
}
$table->addColumn('deleted_at', 'datetime', $options);
}
- public function __call($method, $parameters)
- {
-
- // Set mutators
- if (preg_match('/^set([a-zA-Z0-9]+)Attribute$/', $method, $matches)) {
- $attr = Str::snake($matches[1]);
-
- if (isset($this->_fields[$attr])) {
- $callback = [$this->_fields[$attr], 'setMutator'];
- if (is_callable($callback)) {
- return call_user_func_array($callback, $parameters);
- }
- }
- }
-
- // magic call of relationships
- foreach ($this->_relationships as $relationship) {
- /** @var $relationship Field */
- if ($method == $relationship->getAttribute('entity')) {
- return $this->relationship($relationship);
- }
- }
-
- return parent::__call($method, $parameters);
- }
-
- /**
- * @param $field Field|string
- * @return \Illuminate\Database\Eloquent\Relations\BelongsTo|\Illuminate\Database\Eloquent\Relations\BelongsToMany
- */
- public function relationship($field)
- {
- if (is_string($field)) {
- foreach ($this->_fields as $f) {
- if ($f->getAttribute('entity') == $field) {
- $field = $f;
- break;
- }
- }
- }
- switch ($field->getRelationship()) {
- case 'belongsTo':
- return $this->belongsTo($field->getAttribute('model'), $field->getAttribute('name'));
- case 'belongsToMany':
- return $this->belongsToMany($field->getAttribute('model'), $this->getRelationShipTable($field));
- }
- }
-
- /**
- * @param $field Field
- * @return string
- */
- public function getRelationShipTable($field)
- {
- /** @var Model $foreignEntity */
- $foreignEntity = self::_toModel($field->getAttribute('model'));
-
- if ($field->getRelationship() == 'belongsToMany') {
- return $this->getTable() . '_' . $field->getAttribute('name') . '_rel_btm';
- }
- }
+// public function __call($method, $parameters)
+// {
+//
+// // Set mutators
+// if (preg_match('/^set([a-zA-Z0-9]+)Attribute$/', $method, $matches)) {
+// $attr = Str::snake($matches[1]);
+//
+// if (isset($this->_fields[$attr])) {
+// $callback = [$this->_fields[$attr], 'setMutator'];
+// if (is_callable($callback)) {
+// return call_user_func_array($callback, $parameters);
+// }
+// }
+// }
+//
+// // magic call of relationships
+// foreach ($this->_relationships as $relationship) {
+// /** @var $relationship Field */
+// if ($method == $relationship->getAttribute('entity')) {
+// return $this->relationship($relationship);
+// }
+// }
+//
+// return parent::__call($method, $parameters);
+// }
+//
+// /**
+// * @param $field Field|string
+// * @return \Illuminate\Database\Eloquent\Relations\BelongsTo|\Illuminate\Database\Eloquent\Relations\BelongsToMany
+// */
+// public function relationship($field)
+// {
+// if (is_string($field)) {
+// foreach ($this->_fields as $f) {
+// if ($f->getAttribute('entity') == $field) {
+// $field = $f;
+// break;
+// }
+// }
+// }
+// switch ($field->getRelationship()) {
+// case 'belongsTo':
+// return $this->belongsTo($field->getAttribute('model'), $field->getAttribute('name'));
+// case 'belongsToMany':
+// return $this->belongsToMany($field->getAttribute('model'), $this->getRelationShipTable($field));
+// }
+// }
+//
+// /**
+// * @param $field Field
+// * @return string
+// */
+// public function getRelationShipTable($field)
+// {
+// /** @var Model $foreignEntity */
+// $foreignEntity = self::_toModel($field->getAttribute('model'));
+//
+// if ($field->getRelationship() == 'belongsToMany') {
+// return $this->getTable() . '_' . $field->getAttribute('name') . '_rel_btm';
+// }
+// }
/**
* @param $class Model|string