1 <?php 2 /* Copyright (c) 2012, Geert Bergman (geert@scrivo.nl) 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, 9 * this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright notice, 11 * this list of conditions and the following disclaimer in the documentation 12 * and/or other materials provided with the distribution. 13 * 3. Neither the name of "Scrivo" nor the names of its contributors may be 14 * used to endorse or promote products derived from this software without 15 * specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 * POSSIBILITY OF SUCH DAMAGE. 28 * 29 * $Id: PageProperty.php 866 2013-08-25 16:22:35Z geert $ 30 */ 31 32 /** 33 * Implementation of the \Scrivo\PageProperty class. 34 */ 35 36 namespace Scrivo; 37 38 /** 39 * The PageProperty class is the base class for all page properties. Page 40 * properties are a part of the page. 41 * 42 * Note that PageProperties can't be created nor deleted using this class. 43 * PageProperties are created when the page itself is created, using the 44 * PageDefintion rules. 45 * 46 * This is an abstract class that implements the methods for creating (factory 47 * method "create"), listing and updating page properties. 48 * 49 * Legacy information: Page properties (including applications) are stored 50 * in serveral Scrivo data tables (page_property_html, page_property) as 51 * their definitions (page_definition_tab, page_property_definition, application_definition). 52 * This distiction is dropped, all these are accessed though a the 53 * single page property "properties". 54 */ 55 class PageProperty { 56 57 /** 58 * The property type: one out of the TemplateProperty::TYPE_* constants. 59 * @var \Scrivo\String 60 */ 61 private $type; 62 63 /** 64 * An textual identification/key for this property. 65 * @var \Scrivo\String 66 */ 67 private $phpSelector; 68 69 /** 70 * The property data. 71 * @var \Scrivo\String 72 */ 73 private $data; 74 75 /** 76 * A derived data of the property data. 77 * @var \Scrivo\String 78 */ 79 private $extData; 80 81 /** 82 * The property defintion id. 83 * @var int 84 */ 85 private $definitionId; 86 87 /** 88 * The page where this property belongs to. 89 * @var \Scrivo\Page 90 */ 91 private $page; 92 93 /** 94 * Create an empty page property object. 95 * 96 * @param \Scrivo\Page $page This poperty's page. 97 * @param array $rd An array containing the initial data (result set row). 98 */ 99 protected function __construct(\Scrivo\Page $page, array $rd) { 100 101 $this->type = new \Scrivo\String($rd["type"]); 102 $this->phpSelector = new \Scrivo\String($rd["php_key"]); 103 $this->setData(new \Scrivo\String($rd["value"])); 104 $this->setExtData(new \Scrivo\String($rd["VALUE2"])); 105 $this->definitionId = intval($rd["ID_DEF"]); 106 107 $this->page = $page; 108 } 109 110 /** 111 * Factory method to create page properties. The method will nog return 112 * a base PageProperty object but a sepacialized descendant based upon 113 * the given data. 114 * 115 * @param \Scrivo\Page $page The page for which to create the page property. 116 * @param array $rd Array (result set row data) with the page property 117 * data. 118 119 * @return \Scrivo\PageProperty\Image|\Scrivo\PageProperty\SelectList| 120 * \Scrivo\PageProperty\ColorList|\Scrivo\PageProperty\Color| 121 * \Scrivo\PageProperty\Url|\Scrivo\PageProperty\CheckBox| 122 * \Scrivo\PageProperty\Input|\Scrivo\PageProperty\Text| 123 * \Scrivo\PageProperty\HtmlText|\Scrivo\PageProperty\HtmlContent| 124 * \Scrivo\PageProperty\Application 125 */ 126 public static function create(\Scrivo\Page $page, $rd) { 127 \Scrivo\ArgumentCheck::assertArgs(func_get_args(), array(null, null)); 128 129 switch ($rd["type"]) { 130 case \Scrivo\PagePropertyDefinition::TYPE_IMAGE: 131 case "imgalttit": 132 return new \Scrivo\PageProperty\Image($page, $rd); 133 134 case \Scrivo\PagePropertyDefinition::TYPE_SELECT: 135 return new \Scrivo\PageProperty\SelectList($page, $rd); 136 137 case \Scrivo\PagePropertyDefinition::TYPE_COLOR_LIST: 138 return new \Scrivo\PageProperty\ColorList($page, $rd); 139 140 case \Scrivo\PagePropertyDefinition::TYPE_COLOR: 141 return new \Scrivo\PageProperty\Color($page, $rd); 142 143 case \Scrivo\PagePropertyDefinition::TYPE_URL: 144 return new \Scrivo\PageProperty\Url($page, $rd); 145 146 case \Scrivo\PagePropertyDefinition::TYPE_CHECKBOX: 147 return new \Scrivo\PageProperty\CheckBox($page, $rd); 148 149 case \Scrivo\PagePropertyDefinition::TYPE_DATE_TIME: 150 return new \Scrivo\PageProperty\DateTime($page, $rd); 151 152 case \Scrivo\PagePropertyDefinition::TYPE_INPUT: 153 return new \Scrivo\PageProperty\Input($page, $rd); 154 155 case \Scrivo\PagePropertyDefinition::TYPE_TEXT: 156 return new \Scrivo\PageProperty\Text($page, $rd); 157 158 case \Scrivo\PagePropertyDefinition::TYPE_HTML_TEXT: 159 return new \Scrivo\PageProperty\HtmlText($page, $rd); 160 161 case \Scrivo\PagePropertyDefinition::TYPE_HTML_TEXT_TAB: 162 return new \Scrivo\PageProperty\HtmlContent($page, $rd); 163 164 case \Scrivo\PagePropertyDefinition::TYPE_APPLICATION_TAB: 165 return new \Scrivo\PageProperty\Application($page, $rd); 166 } 167 throw new \Scrivo\SystemException( 168 "Invalid property type '{$rd["type"]}'"); 169 } 170 171 /** 172 * Implementation of the readable properties using the PHP magic 173 * method __get(). 174 * 175 * @param string $name The name of the property to get. 176 * 177 * @return mixed The value of the requested property. 178 */ 179 public function __get($name) { 180 switch($name) { 181 case "type": return $this->type; 182 case "phpSelector": return $this->phpSelector; 183 case "data": return $this->data; 184 case "extData": return $this->extData; 185 //case "definitionId": return $this->definitionId; 186 case "definition": return $this->definitionId ? 187 \Scrivo\PagePropertyDefinition::fetch( 188 $this->page->context, $this->definitionId) 189 : new \Scrivo\PageDefinition($this->page->context); 190 case "page": return $this->page; 191 } 192 throw new \Scrivo\SystemException("No such get-property '$name'."); 193 } 194 195 /** 196 * Implementation of the writable properties using the PHP magic 197 * method __set(). 198 * 199 * @param string $name The name of the property to set. 200 * @param mixed $value The value of the property to set. 201 */ 202 public function __set($name, $value) { 203 switch($name) { 204 case "data": $this->setData($value); return; 205 case "extData": $this->setExtData($value); return; 206 } 207 throw new \Scrivo\SystemException("No such set-property '$name'."); 208 } 209 210 /** 211 * Set the property data. 212 * 213 * @param \Scrivo\String $data The property data. 214 */ 215 protected function setData(\Scrivo\String $data) { 216 $this->data = $data; 217 } 218 219 /** 220 * Set the property extended data. 221 * 222 * @param \Scrivo\String $extData A derived data of the property data. 223 */ 224 protected function setExtData(\Scrivo\String $extData) { 225 $this->extData = $extData; 226 } 227 228 /** 229 * Check if this page property object can be updated in the database. 230 * 231 * @throws \Scrivo\ApplicationException If the data is not accessible or 232 * one or more of the fields contain invalid data. 233 */ 234 private function validateUpdate() { 235 $this->page->context->checkPermission( 236 \Scrivo\AccessController::WRITE_ACCESS); 237 } 238 239 /** 240 * Update page property object data in the database. 241 * 242 * First it is checked if the data of this page property object can be 243 * updated in the database, then the data is updated in the database. 244 * 245 * @throws \Scrivo\ApplicationException If the data is not accessible or 246 * one or more of the fields contain invalid data. 247 */ 248 function update() { 249 try { 250 //$dummy = $this->getData(); 251 252 $this->validateUpdate(); 253 254 if ((string)$this->type === 255 \Scrivo\PagePropertyDefinition::TYPE_HTML_TEXT_TAB) { 256 257 $this->updateContent(); 258 259 } else if ((string)$this->type !== 260 \Scrivo\PagePropertyDefinition::TYPE_APPLICATION_TAB) { 261 262 $this->updateProperty(); 263 } 264 265 266 unset($this->page->context->cache[$this->page->id]); 267 268 } catch(\PDOException $e) { 269 throw new \Scrivo\ResourceException($e); 270 } 271 } 272 273 /** 274 * Update property data in the page_property table. 275 */ 276 private function updateProperty() { 277 278 $sth = $this->page->context->connection->prepare( 279 "DELETE FROM page_property WHERE instance_id = :instId AND 280 page_id = :pageId AND page_property_definition_id = :idDef AND 281 version = (SELECT version FROM page WHERE 282 instance_id = :instId AND page_id = :pageId 283 AND (has_staging+version) = 0)"); 284 285 $this->page->context->connection->bindInstance($sth); 286 $sth->bindValue(":pageId", $this->page->id, \PDO::PARAM_INT); 287 $sth->bindValue(":idDef", $this->definitionId, \PDO::PARAM_STR); 288 289 $sth->execute(); 290 291 $sth = $this->page->context->connection->prepare( 292 "INSERT INTO page_property ( 293 instance_id, page_id, version, page_property_definition_id, 294 value 295 ) VALUES (:instId, :pageId, (SELECT version FROM page WHERE 296 instance_id = :instId AND page_id = :pageId 297 AND (has_staging+version) = 0), :idDef, :data)"); 298 299 $this->page->context->connection->bindInstance($sth); 300 $sth->bindValue(":pageId", $this->page->id, \PDO::PARAM_INT); 301 $sth->bindValue(":idDef", $this->definitionId, \PDO::PARAM_STR); 302 $sth->bindValue(":data", $this->data, \PDO::PARAM_STR); 303 304 $sth->execute(); 305 306 } 307 308 /** 309 * Update property data in the page_property_html table. 310 */ 311 private function updateContent() { 312 313 $sth = $this->page->context->connection->prepare( 314 "DELETE FROM page_property_html WHERE instance_id = :instId AND 315 page_id = :pageId AND page_definition_tab_id = :defId AND 316 version = (SELECT version FROM page WHERE 317 instance_id = :instId AND page_id = :pageId 318 AND (has_staging+version) = 0)"); 319 320 $this->page->context->connection->bindInstance($sth); 321 $sth->bindValue(":pageId", $this->page->id, \PDO::PARAM_INT); 322 $sth->bindValue(":defId", $this->definitionId, \PDO::PARAM_STR); 323 324 $sth->execute(); 325 326 $sth = $this->page->context->connection->prepare( 327 "INSERT INTO page_property_html ( 328 instance_id, page_id, version, page_definition_tab_id, 329 raw_html, html 330 ) VALUES (:instId, :pageId, (SELECT version FROM page WHERE 331 instance_id = :instId AND page_id = :pageId 332 AND (has_staging+version) = 0), :defId, :data2, :data)"); 333 334 $this->page->context->connection->bindInstance($sth); 335 $sth->bindValue(":pageId", $this->page->id, \PDO::PARAM_INT); 336 $sth->bindValue(":defId", $this->definitionId, \PDO::PARAM_STR); 337 $sth->bindValue(":data", $this->data, \PDO::PARAM_STR); 338 $sth->bindValue(":data2", $this->extData, \PDO::PARAM_STR); 339 340 $sth->execute(); 341 342 } 343 344 345 } 346 347 ?>
Documentation generated by phpDocumentor 2.0.0a12 and ScrivoDocumentor on August 29, 2013