use Cubist\Backpack\app\Magic\Controllers\CubistMagicController;
use Cubist\Backpack\app\Template\TemplateAbstract;
+use Doctrine\DBAL\Schema\Schema;
+use Doctrine\DBAL\Schema\Table;
class CMSPage extends CubistMagicNestedModel
{
* @param $controller CubistMagicController
* @throws \Exception
*/
- protected function useTemplate($template, $controller)
+ protected function useTemplate($template, $controller = null)
{
if (is_string($template)) {
$template = TemplateAbstract::getTemplateIntanceByName($template);
$attr = array_merge($this->defaultFieldAttributes, $field);
$this->addFakeField($attr);
}
- $controller->updateFieldsFromModel();
+ if (null !== $controller) {
+ $controller->updateFieldsFromModel();
+ }
+ }
+
+ /**
+ * @param $schema Schema
+ * @return Table
+ */
+ public function setSchema($schema)
+ {
+ // We need to know all the columns that will be needed by all the templates
+ $this->useAllTemplates();
+ parent::setSchema($schema);
+ }
+
+ public function useAllTemplates()
+ {
+ foreach (TemplateAbstract::getTemplates() as $template) {
+ $this->useTemplate($template);
+ }
}
public function update(array $attributes = [], array $options = [])
return Media::where('model_id', $this->getKey())->where('model_type', get_class($this))->get();
}
- public function update(array $attributes = [], array $options = [])
+ protected function _prepareData($attributes)
{
+ print_r($attributes);
$attributes = Json::decodeRecursive($attributes, Json::TYPE_ARRAY);
- return $this->updateTranslations($attributes, $options);
+ $res = [];
+ 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 = [])
+ {
+
+ return $this->updateTranslations($this->_prepareData($attributes), $options);
}
public function create(array $attributes = [])
{
- $attributes = Json::decodeRecursive($attributes, Json::TYPE_ARRAY);
- return $this->createTranslations($attributes);
+ return $this->createTranslations($this->_prepareData($attributes));
}
- public function getSlugAttribute($value){
- if(!$value){
+ public function getSlugAttribute($value)
+ {
+ if (!$value) {
return Str::slug($this->title);
}
return $value;