From f98110458b69a461d3cf23a9cf73d334ab658b32 Mon Sep 17 00:00:00 2001 From: Nadar Date: Sat, 29 Oct 2016 15:53:40 +0200 Subject: [PATCH] CMS Page properties with overriden default implementation returns wrong administration api value. closes #1061 --- CHANGELOG.md | 1 + docs/guide1.0/app-cmsproperties.md | 6 +- modules/admin/src/base/Property.php | 137 ++++++++++++++++++- modules/cms/src/admin/apis/NavController.php | 2 +- 4 files changed, 140 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b785c0f2f..3bacdbd7d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,6 +34,7 @@ The changelog contains informations about bug fixes, new features or bc breaking - [#1011](https://github.com/luyadev/luya/issues/1011) The ViewContext implementation for cmslayout rendering allows you now to render other templates inside a cmslayout. - [#1044](https://github.com/luyadev/luya/issues/1044) Changing the cms permission force menu reload in order to fix bug with old menu permissions. - [#1013](https://github.com/luyadev/luya/issues/1013) Delete a cms page displays blank page and reloads menu, fixed bug where page was still visible. +- [#1061](https://github.com/luyadev/luya/issues/1061) CMS Page properties with overriden default implementation returns wrong administration api value. 1.0.0-RC1 (04.10.2016) ----------------------- diff --git a/docs/guide1.0/app-cmsproperties.md b/docs/guide1.0/app-cmsproperties.md index 05238fe56..3eba20a34 100644 --- a/docs/guide1.0/app-cmsproperties.md +++ b/docs/guide1.0/app-cmsproperties.md @@ -73,7 +73,9 @@ class MyImage extends \luya\admin\base\ImageProperty } ``` -In order to get use the above MyImage property just run: ``. +In order to get use the above MyImage property just run: ``. + +> As the `$item->getProperty` method returns the Property, as the all propertys implement the `__toString()` they will return the `getValue()` method by default. ## Get the Proprety value @@ -95,7 +97,7 @@ A very common scenario is to add properties to an existing menu item like an ima getProperty('navImage')): ?> - + // equals to: diff --git a/modules/admin/src/base/Property.php b/modules/admin/src/base/Property.php index ec0b4626e..72c4ada48 100644 --- a/modules/admin/src/base/Property.php +++ b/modules/admin/src/base/Property.php @@ -8,36 +8,167 @@ /** * Abstract Page Property Class. * - * @todo remove defaultValue change to initvalue like in blocks! - * - * @author nadar + * Each property must implement this method. + * + * @author Basil Suter */ abstract class Property extends Component implements TypesInterface { + /** + * @var string The name of the event will be triggered before rendering + */ const EVENT_BEFORE_RENDER = 'EVENT_BEFORE_RENDER'; + /** + * @var string The module where the property is located. + */ public $moduleName = null; + /** + * @var mixed The value from the database assigned into the property object. + */ public $value = null; + /** + * @var boolean Whether the property is used for an i18n use case or not, this will + * serialize the input as json into the database and the getValue/getAdminValue methods will + * automatically unserialize the correctly value. + */ public $i18n = false; + /** + * The internal variable name for this property. + * + * This is like a variable name identifer, this should be unique value across all properties. Allowed + * chars are `a-zA-Z0-9-_`. The defined variable named will be use when retrieving data from a property + * in the frontend. For example `Yii::$app->menu->current->getProperty('varName`)` where varName is the + * varaiable name you choosen as return value of this method. + * + * Example: + * + * ```php + * public function varName() + * { + * return 'myVariable'; + * } + * ``` + * + * @return string + */ abstract public function varName(); + /** + * The label which is displayed in the administration area. + * + * Example: + * + * ```php + * public function label() + * { + * return 'My Variable'; + * } + * ``` + * + * @return string + */ abstract public function label(); + /** + * The specifation of what type this property is. + * + * There are different types of variables/propertys to create. Sometimes its + * just a plain text field, textarea or and image or multip image upload. Therefore + * the method `type()` defines what should be created. All types are available als + * constants inside the {{\luya\admin\base\TypesInterface}}. + * + * Example: + * + * ```php + * public function type() + * { + * return self::TYPE_SELECT; + * } + * ``` + * + * @see \luya\admin\base\TypesInterface + * @return string + */ abstract public function type(); + /** + * When the object is force to return as string the `getValue()` method is returned. + * + * @return mixed + */ + public function __toString() + { + return $this->getValue(); + } + + /** + * Options you may have to pass to the selected type. + * + * Sometimes the type of property requires more informations and optional data + * those datas needs to be returned. Example of options to return when using + * the TYPE_SELECT property type: + * + * ```php + * public function options() + * { + * return [ + * ['value' => 'ul', 'label' => 'Pointed List'], + * ['value' => 'ol', 'label' => 'Nummeic List'], + * ]; + * } + * ``` + * + * @return mixed + */ public function options() { return []; } + /** + * If the property is requested in the frontend or admin context and there is no value + * the `default()` value response will be used. + * + * For example a preselecting item from a list select dropdown: + * + * ```php + * public function defaultValue() + * { + * return 'default'; + * } + * ``` + * + * @return mixed + */ public function defaultValue() { return false; } + /** + * The value is passed from the administration area side to the angular view. + * + * @return mixed + */ + public function getAdminValue() + { + if ($this->i18n) { + $this->value = I18n::decode($this->value); + } + return $this->value; + } + + /** + * This is what will be returned when the property is requested in the frontend. + * + * You can override this function in order to provide your own output logic. + * + * @return mixed + */ public function getValue() { if ($this->i18n) { diff --git a/modules/cms/src/admin/apis/NavController.php b/modules/cms/src/admin/apis/NavController.php index 2d5a27da9..9bfe56a2e 100644 --- a/modules/cms/src/admin/apis/NavController.php +++ b/modules/cms/src/admin/apis/NavController.php @@ -85,7 +85,7 @@ public function actionGetProperties($navId) $object = \luya\admin\models\Property::findOne($row['admin_prop_id']); $blockObject = $object->createObject($row['value']); - $value = $blockObject->getValue(); + $value = $blockObject->getAdminValue(); $row['value'] = (is_numeric($value)) ? (int) $value : $value;