Use Page objects for page condition
authorMatthias Schmidt <gravatronics@live.com>
Sat, 23 Apr 2016 16:18:49 +0000 (18:18 +0200)
committerMatthias Schmidt <gravatronics@live.com>
Sat, 23 Apr 2016 16:18:49 +0000 (18:18 +0200)
com.woltlab.wcf/objectType.xml
wcfsetup/install/files/lib/system/condition/AbstractMultiSelectCondition.class.php
wcfsetup/install/files/lib/system/condition/MultiPageControllerCondition.class.php
wcfsetup/install/files/lib/system/condition/page/MultiPageCondition.class.php [new file with mode: 0644]
wcfsetup/install/files/lib/system/request/Request.class.php

index 91e6426c8ee6641aa5ea0549676d20e5667c47a8..08badc908f08ea86cb4b2ef9c3ab772827a1f255 100644 (file)
                
                <!-- notice conditions -->
                <type>
-                       <name>com.woltlab.wcf.page.controller</name>
+                       <name>com.woltlab.wcf.page</name>
                        <definitionname>com.woltlab.wcf.condition.notice</definitionname>
-                       <classname><![CDATA[wcf\system\condition\MultiPageControllerCondition]]></classname>
+                       <classname><![CDATA[wcf\system\condition\page\MultiPageCondition]]></classname>
                        <conditionobject>com.woltlab.wcf.page</conditionobject>
                </type>
                
                
                <!-- ad conditions -->
                <type>
-                       <name>com.woltlab.wcf.page.controller</name>
+                       <name>com.woltlab.wcf.page</name>
                        <definitionname>com.woltlab.wcf.condition.ad</definitionname>
-                       <classname><![CDATA[wcf\system\condition\MultiPageControllerCondition]]></classname>
+                       <classname><![CDATA[wcf\system\condition\page\MultiPageCondition]]></classname>
                        <conditionobject>com.woltlab.wcf.page</conditionobject>
                </type>
                
                        <conditiongroup>userOptions</conditiongroup>
                </type>
                <!-- /user search conditions -->
+               
+               <!-- deprecated -->
+               <type>
+                       <name>com.woltlab.wcf.page.controller</name>
+                       <definitionname>com.woltlab.wcf.condition.notice</definitionname>
+                       <classname><![CDATA[wcf\system\condition\MultiPageControllerCondition]]></classname>
+                       <conditionobject>com.woltlab.wcf.page</conditionobject>
+               </type>
+               <type>
+                       <name>com.woltlab.wcf.page.controller</name>
+                       <definitionname>com.woltlab.wcf.condition.ad</definitionname>
+                       <classname><![CDATA[wcf\system\condition\MultiPageControllerCondition]]></classname>
+                       <conditionobject>com.woltlab.wcf.page</conditionobject>
+               </type>
+               <!-- /deprecated -->
        </import>
 </data>
index 5b996b706d21d462706bae05ef120fa3492b9b11..75ed722c08d3e960031e058f84dc33ae4a0b5d5d 100644 (file)
@@ -16,25 +16,24 @@ use wcf\util\ArrayUtil;
  */
 abstract class AbstractMultiSelectCondition extends AbstractSelectCondition {
        /**
-        * @see \wcf\system\condition\AbstractSelectCondition::$fieldValue
+        * selected values
+        * @var mixed[]
         */
-       protected $fieldValue = array();
+       protected $fieldValue = [];
        
        /**
-        * @see \wcf\system\condition\ICondition::getData()
+        * @inheritDoc
         */
        public function getData() {
                if (!empty($this->fieldValue)) {
-                       return array(
-                               $this->fieldName => $this->fieldValue
-                       );
+                       return [$this->fieldName => $this->fieldValue];
                }
                
                return null;
        }
        
        /**
-        * @see \wcf\system\condition\AbstractSingleFieldCondition::getFieldElement()
+        * @inheritDoc
         */
        protected function getFieldElement() {
                $options = $this->getOptions();
@@ -54,28 +53,28 @@ abstract class AbstractMultiSelectCondition extends AbstractSelectCondition {
        }
        
        /**
-        * @see \wcf\system\condition\AbstractSelectCondition::getOptionCode()
+        * @inheritDoc
         */
        protected function getOptionCode($value, $label) {
                return '<option value="'.$value.'"'.(in_array($value, $this->fieldValue) ? ' selected="selected"' : '').'>'.WCF::getLanguage()->get($label).'</option>';
        }
        
        /**
-        * @see \wcf\system\condition\ICondition::readFormParameters()
+        * @inheritDoc
         */
        public function readFormParameters() {
                if (isset($_POST[$this->fieldName]) && is_array($_POST[$this->fieldName])) $this->fieldValue = ArrayUtil::toIntegerArray($_POST[$this->fieldName]);
        }
        
        /**
-        * @see \wcf\system\condition\ICondition::validate()
+        * @inheritDoc
         */
        public function reset() {
                $this->fieldValue = array();
        }
        
        /**
-        * @see \wcf\system\condition\ICondition::validate()
+        * @inheritDoc
         */
        public function validate() {
                $options = $this->getOptions();
index 7940eb14f0ef773be72f025262a9fc86fabe5cf5..391331fb0b04b8c6eb51d6ac6fb285f1970a8c64 100644 (file)
@@ -10,58 +10,32 @@ use wcf\system\WCF;
  * Condition implementation for selecting multiple page controllers.
  * 
  * @author     Matthias Schmidt
- * @copyright  2001-2015 WoltLab GmbH
+ * @copyright  2001-2016 WoltLab GmbH
  * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
  * @package    com.woltlab.wcf
  * @subpackage system.condition
  * @category   Community Framework
+ * @deprecated since 2.2
  */
 class MultiPageControllerCondition extends AbstractMultiSelectCondition implements IContentCondition {
        /**
-        * @see \wcf\system\condition\AbstractSingleFieldCondition::$label
+        * @inheritDoc
         */
-       protected $description = 'wcf.global.multiSelect';
-       
-       /**
-        * @see \wcf\system\condition\AbstractSelectCondition::$fieldName
-        */
-       protected $fieldName = 'pageControllers';
-       
-       /**
-        * @see \wcf\system\condition\AbstractSingleFieldCondition::$label
-        */
-       protected $label = 'wcf.page.requestedPage';
-       
-       /**
-        * @see \wcf\system\condition\AbstractSelectCondition::getOptionCode()
-        */
-       protected function getOptionCode($value, $label) {
-               return '<option value="'.$value.'" data-object-type="'.ObjectTypeCache::getInstance()->getObjectType($value)->objectType.'"'.(in_array($value, $this->fieldValue) ? ' selected="selected"' : '').'>'.WCF::getLanguage()->get($label).'</option>';
+       protected function getFieldElement() {
+               return '';
        }
        
        /**
-        * @see \wcf\system\condition\AbstractSelectCondition::getOptions()
+        * @inheritDoc
         */
        protected function getOptions() {
-               return PageManager::getInstance()->getSelection();
+               return [];
        }
        
        /**
-        * @see \wcf\system\condition\IContentCondition::showContent()
+        * @inheritDoc
         */
        public function showContent(Condition $condition) {
-               $requestClassName = RequestHandler::getInstance()->getActiveRequest()->getClassName();
-               $requestClassName = ltrim($requestClassName, '\\'); // remove leading backslash
-               $pageControllers = $condition->pageControllers;
-               foreach ($pageControllers as $objectTypeID) {
-                       $objectType = ObjectTypeCache::getInstance()->getObjectType($objectTypeID);
-                       if ($objectType === null) return false;
-                       
-                       if ($requestClassName == $objectType->className) {
-                               return true;
-                       }
-               }
-               
                return false;
        }
 }
diff --git a/wcfsetup/install/files/lib/system/condition/page/MultiPageCondition.class.php b/wcfsetup/install/files/lib/system/condition/page/MultiPageCondition.class.php
new file mode 100644 (file)
index 0000000..f47d1db
--- /dev/null
@@ -0,0 +1,86 @@
+<?php
+namespace wcf\system\condition\page;
+use wcf\data\condition\Condition;
+use wcf\data\page\PageCache;
+use wcf\data\page\PageNode;
+use wcf\data\page\PageNodeTree;
+use wcf\system\condition\AbstractMultiSelectCondition;
+use wcf\system\condition\IContentCondition;
+use wcf\system\exception\UserInputException;
+use wcf\system\request\RequestHandler;
+
+/**
+ * Condition implementation for selecting multiple pages.
+ * 
+ * @author     Matthias Schmidt
+ * @copyright  2001-2016 WoltLab GmbH
+ * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package    com.woltlab.wcf
+ * @subpackage system.condition.page
+ * @category   Community Framework
+ * @deprecated since 2.2
+ */
+class MultiPageCondition extends AbstractMultiSelectCondition implements IContentCondition {
+       /**
+        * @inheritDoc
+        */
+       protected $description = 'wcf.global.multiSelect';
+       
+       /**
+        * @inheritDoc
+        */
+       protected $fieldName = 'pageIDs';
+       
+       /**
+        * @inheritDoc
+        */
+       protected $label = 'wcf.page.requestedPage';
+       
+       /**
+        * @inheritDoc
+        */
+       protected function getFieldElement() {
+               $pageNodes = (new PageNodeTree())->getNodeList();
+               
+               $fieldElement = '<select name="'.$this->fieldName.'[]" id="'.$this->fieldName.'" multiple="multiple" size="10">';
+               /** @var PageNode $pageNode */
+               foreach ($pageNodes as $pageNode) {
+                       $fieldElement .= '<option value="'.$pageNode->getPage()->pageID.'">'.($pageNode->getDepth() > 1 ? str_repeat("&nbsp;&nbsp;&nbsp;&nbsp;", $pageNode->getDepth() - 1) : '').$pageNode->getPage()->name.'</option>';
+               }
+               $fieldElement .= "</select>";
+               
+               return $fieldElement;
+       }
+       
+       /**
+        * @inheritDoc
+        */
+       protected function getOptions() {
+               return [];
+       }
+       
+       /**
+        * @inheritDoc
+        */
+       public function showContent(Condition $condition) {
+               $pageID = RequestHandler::getInstance()->getActiveRequest()->getPageID();
+               if ($pageID && $condition->pageIDs && is_array($condition->pageIDs)) {
+                       return in_array($pageID, $condition->pageIDs);
+               }
+               
+               return false;
+       }
+       
+       /**
+        * @inheritDoc
+        */
+       public function validate() {
+               foreach ($this->fieldValue as $value) {
+                       if (PageCache::getInstance()->getPage($value) === null) {
+                               $this->errorMessage = 'wcf.global.form.error.noValidSelection';
+                               
+                               throw new UserInputException($this->fieldName, 'noValidSelection');
+                       }
+               }
+       }
+}
\ No newline at end of file
index 31436fd3f603cdc953a680f38e1a13519bba060d..3b3cbb38255e287f27d80bb5ae3fe276d1b7bb90 100644 (file)
@@ -166,7 +166,7 @@ class Request {
        /**
         * Returns the current page id.
         * 
-        * @return      integer         current page id or `0` if unknown
+        * @return      integer         current page id or `0` if unknown
         */
        public function getPageID() {
                if ($this->pageID === null) {