From d616d3ae8b7e61551895961277eb005c8a86656a Mon Sep 17 00:00:00 2001 From: Sam Georges Date: Fri, 23 May 2014 19:35:56 +1000 Subject: [PATCH] Class `CmsPropertyHelper` has been deprecated Cms Objects now support basic queries that return a collection --- CHANGELOG.md | 4 + modules/cms/classes/CmsObject.php | 48 ++++++++++- modules/cms/classes/CmsObjectCollection.php | 14 ++++ modules/cms/classes/CmsObjectQuery.php | 92 +++++++++++++++++++++ modules/cms/classes/CmsPropertyHelper.php | 27 +++--- modules/cms/classes/Page.php | 2 +- 6 files changed, 168 insertions(+), 19 deletions(-) create mode 100644 modules/cms/classes/CmsObjectCollection.php create mode 100644 modules/cms/classes/CmsObjectQuery.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 1d8a786bd..bc4b899ab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +* **Build 90** (2014-05-23) + - Class `CmsPropertyHelper` has been deprecated, will be removed year > 2014. + - Cms Objects now support basic queries that return a collection. Eg: `Page::sortBy('title')->lists('title', 'baseFileName')` + * **Build 89** (2014-05-22) - Components have a new override method `onRender()` called before a component is rendered. - The `{% component %}` tag now supports passing parameters that override the component properties when they are rendered. diff --git a/modules/cms/classes/CmsObject.php b/modules/cms/classes/CmsObject.php index 7e9dd4aba..9d10d0f3b 100644 --- a/modules/cms/classes/CmsObject.php +++ b/modules/cms/classes/CmsObject.php @@ -5,7 +5,6 @@ use Lang; use Cache; use Config; use Validator; -use Cms\Classes\FileHelper; use System\Classes\SystemException; use System\Classes\ApplicationException; use October\Rain\Support\ValidationException; @@ -63,7 +62,7 @@ class CmsObject * Creates an instance of the object and associates it with a CMS theme. * @param \Cms\Classes\Theme $theme Specifies the theme the object belongs to. */ - public function __construct(Theme $theme) + public function __construct(Theme $theme = null) { $this->theme = $theme; } @@ -370,6 +369,9 @@ class CmsObject */ public static function listInTheme($theme, $skipCache = false) { + if (!$theme) + throw new ApplicationException(Lang::get('cms::lang.theme.active.not_set')); + $dirPath = $theme->getPath().'/'.static::getObjectTypeDirName(); $result = []; @@ -433,6 +435,48 @@ class CmsObject return false; } + // + // Queries + // + + /** + * Get a new query builder for the object + * @return CmsObjectQuery + */ + public function newQuery() + { + $query = new CmsObjectQuery($this, $this->theme); + return $query; + } + + /** + * Handle dynamic method calls into the method. + * @param string $method + * @param array $parameters + * @return mixed + */ + public function __call($method, $parameters) + { + $query = $this->newQuery(); + return call_user_func_array(array($query, $method), $parameters); + } + + /** + * Handle dynamic static method calls into the method. + * @param string $method + * @param array $parameters + * @return mixed + */ + public static function __callStatic($method, $parameters) + { + $instance = new static; + return call_user_func_array([$instance, $method], $parameters); + } + + // + // Overrides + // + /** * Initializes the object properties from the cached data. * @param array $cached The cached data array. diff --git a/modules/cms/classes/CmsObjectCollection.php b/modules/cms/classes/CmsObjectCollection.php new file mode 100644 index 000000000..7a358ffc2 --- /dev/null +++ b/modules/cms/classes/CmsObjectCollection.php @@ -0,0 +1,14 @@ +cmsObject = $cmsObject; + + if ($theme) + $this->theme = $theme; + else + $this->inEditTheme(); + } + + /** + * Set the theme to a specific one. + * @return self + */ + public function inTheme($theme) + { + $this->theme = $theme; + return $this; + } + + /** + * Set the theme to the editing one. + * @return self + */ + public function inEditTheme() + { + return $this->inTheme(Theme::getEditTheme()); + } + + /** + * Set the theme to the activated one. + * @return self + */ + public function inActiveTheme() + { + return $this->inTheme(Theme::getActiveTheme()); + } + + /** + * Enable or disable cache + * @param boolean $value + * @return self + */ + public function useCache($value = true) + { + $this->useCache = $value; + return $this; + } + + /** + * Returns all CMS objects for the set theme + * @return CmsObjectCollection + */ + public function all() + { + $collection = forward_static_call([$this->cmsObject, 'listInTheme'], $this->theme, !$this->useCache); + $collection = new CmsObjectCollection($collection); + return $collection; + } + + /** + * Handle dynamic method calls into the method. + * @param string $method + * @param array $parameters + * @return mixed + */ + public function __call($method, $parameters) + { + $collection = $this->all(); + return call_user_func_array(array($collection, $method), $parameters); + } + +} \ No newline at end of file diff --git a/modules/cms/classes/CmsPropertyHelper.php b/modules/cms/classes/CmsPropertyHelper.php index 0c396c277..ca74252f2 100644 --- a/modules/cms/classes/CmsPropertyHelper.php +++ b/modules/cms/classes/CmsPropertyHelper.php @@ -1,12 +1,19 @@ getBaseFileName(); - $result[$fileName] = $fileName; - } - - ksort($result); - - return $result; + Flash::warning("CmsPropertyHelper::listPages() is deprecated, use Page::sortBy('baseFileName')->lists('baseFileName', 'baseFileName') instead."); + return Page::sortBy('baseFileName')->lists('baseFileName', 'baseFileName'); } } \ No newline at end of file diff --git a/modules/cms/classes/Page.php b/modules/cms/classes/Page.php index cb86fc11a..8e6775a0e 100644 --- a/modules/cms/classes/Page.php +++ b/modules/cms/classes/Page.php @@ -22,7 +22,7 @@ class Page extends CmsCompoundObject * Creates an instance of the object and associates it with a CMS theme. * @param \Cms\Classes\Theme $theme Specifies the theme the object belongs to. */ - public function __construct(Theme $theme) + public function __construct(Theme $theme = null) { parent::__construct($theme);