1 <?php 2 /* Copyright (c) 2013, 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: PageDefinitionTab.php 866 2013-08-25 16:22:35Z geert $ 30 */ 31 32 /** 33 * Implementation of the \Scrivo\PageDefinitionTab class. 34 */ 35 36 namespace Scrivo; 37 38 /** 39 * The class PageDefinitionTab is used to create tabs in the Scrivo user 40 * interface. 41 * 42 * A page can have can a number of configurable properities, which are defined 43 * by the page definition. The PageDefinitionTab class is used as a means to 44 * distribute these properties over different tabs in the Scrivo user interface. 45 * 46 * With respect to the content this class has no function at all, it is used 47 * for display purposes. 48 * 49 * TODO: The page_definition_tab table is still used to hold property data for 50 * 'html_text' and 'application' tabs. Access to this data is handled bij the 51 * PageProperty class now. In a later version the data model should be 52 * changed also. 53 * 54 * @property-read int $id The page definition tab id (DB key). 55 * @property int $pageDefinitionId The page definition id of the page 56 * definition where this tab belongs to. 57 * @property \Scrivo\String $title A descriptive title for the tab. 58 * @property int $type The type of the tab: one out of the 59 * PageDefinitionTab::TYPE_* constant values. 60 */ 61 class PageDefinitionTab { 62 63 /** 64 * Constant to mark a tab as a property tab. 65 */ 66 const TYPE_PROPERTY_TAB = 1; 67 68 /** 69 * Constant to mark a tab as an html text tab. 70 */ 71 const TYPE_HTML_TEXT_TAB = 2; 72 73 /** 74 * Constant to mark a tab as an application tab. 75 */ 76 const TYPE_APPLICATION_TAB = 3; 77 78 /** 79 * The page definition tab id (DB key). 80 * @var int 81 */ 82 private $id = 0; 83 84 85 /** 86 * The page definition id of the page definition where this tab belongs to. 87 * @var int 88 */ 89 private $pageDefinitionId = 0; 90 91 /** 92 * A descriptive title for the tab. 93 * @var \Scrivo\String 94 */ 95 private $title = null; 96 97 /** 98 * The type of the tab: one out of the PageDefinitionTab::TYPE_* constant 99 * values. 100 * @var int 101 */ 102 private $type = self::TYPE_PROPERTY_TAB; 103 104 /** 105 * A Scrivo context. 106 * @var \Scrivo\Context 107 */ 108 private $context = null; 109 110 /** 111 * Create an empty page defintion tab object. 112 * 113 * @param \Scrivo\Context $context A Scrivo context. 114 */ 115 public function __construct(\Scrivo\Context $context=null) { 116 \Scrivo\ArgumentCheck::assertArgs(func_get_args(), array(null), 0); 117 118 if ($context) { 119 $this->title = new \Scrivo\String(); 120 121 $this->context = $context; 122 } 123 } 124 125 /** 126 * Implementation of the readable properties using the PHP magic 127 * method __get(). 128 * 129 * @param string $name The name of the property to get. 130 * 131 * @return mixed The value of the requested property. 132 */ 133 public function __get($name) { 134 switch($name) { 135 case "id": return $this->id; 136 case "pageDefinitionId": return $this->pageDefinitionId; 137 case "title": return $this->title; 138 case "type": return $this->type; 139 } 140 throw new \Scrivo\SystemException("No such get-property '$name'."); 141 } 142 143 /** 144 * Implementation of the writable properties using the PHP magic 145 * method __set(). 146 * 147 * @param string $name The name of the property to set. 148 * @param mixed $value The value of the property to set. 149 */ 150 public function __set($name, $value) { 151 switch($name) { 152 case "pageDefinitionId": $this->setPageDefinitionId($value); return; 153 case "title": $this->setTitle($value); return; 154 case "type": $this->setType($value); return; 155 } 156 throw new \Scrivo\SystemException("No such set-property '$name'."); 157 } 158 159 /** 160 * Convenience method to set the fields of a page defintion tab object from 161 * an array (result set row). 162 * 163 * @param \Scrivo\Context $context A Scrivo context. 164 * @param array $rd An array containing the field data using the database 165 * field names as keys. 166 */ 167 private function setFields(\Scrivo\Context $context, array $rd) { 168 169 $this->id = intval($rd["page_definition_tab_id"]); 170 $this->pageDefinitionId = intval($rd["page_definition_id"]); 171 $this->title = new \Scrivo\String($rd["title"]); 172 $this->type = $this->toType($rd); 173 174 $this->context = $context; 175 } 176 177 178 /** 179 * Set The page definition id of the page definition where this tab 180 * belongs to. 181 * 182 * @param int $pageDefinitionId The page definition id of the page 183 * definition where this tab belongs to. 184 */ 185 private function setPageDefinitionId($pageDefinitionId) { 186 \Scrivo\ArgumentCheck::assertArgs(func_get_args(), array( 187 array(\Scrivo\ArgumentCheck::TYPE_INTEGER) 188 )); 189 190 $this->pageDefinitionId = $pageDefinitionId; 191 } 192 193 /** 194 * Set A descriptive title for the tab. 195 * 196 * @param \Scrivo\String $title A descriptive title for the tab. 197 */ 198 private function setTitle(\Scrivo\String $title) { 199 $this->title = $title; 200 } 201 202 /** 203 * Set The type of the tab: one out of the PageDefinitionTab::TYPE_* 204 * constant values. 205 * 206 * @param array $rd Array (result set row) that contains the 207 * application_definition_id field. 208 */ 209 private function toType(array $rd) { 210 $val = intval($rd["application_definition_id"]); 211 if ($val === -1) { 212 return self::TYPE_PROPERTY_TAB; 213 } else if ($val === 0) { 214 return self::TYPE_HTML_TEXT_TAB; 215 } 216 return self::TYPE_APPLICATION_TAB; 217 } 218 219 /** 220 * Check if this page defintion tab object can be inserted into the 221 * database. 222 * 223 * @throws \Scrivo\ApplicationException If the data is not accessible or 224 * one or more of the fields contain invalid data. 225 */ 226 private function validateInsert() { 227 $this->context->checkPermission(\Scrivo\AccessController::WRITE_ACCESS); 228 } 229 230 /** 231 * Insert new page defintion tab object data into the database. 232 * 233 * First it is checked if the data of this page defintion tab object can be 234 * inserted into the database, then the data is inserted into the database. 235 * If no id was set a new object id is generated. 236 * 237 * @throws \Scrivo\ApplicationException If the data is not accessible or 238 * one or more of the fields contain invalid data. 239 */ 240 public function insert() { 241 try { 242 $this->validateInsert(); 243 244 if (!$this->id) { 245 $this->id = $this->context->connection->generateId(); 246 } 247 248 $sth = $this->context->connection->prepare( 249 "INSERT INTO page_definition_tab ( 250 instance_id, page_definition_tab_id, page_definition_id, sequence_no, 251 title, application_definition_id 252 ) VALUES ( 253 :instId, :id, :pageDefinitionId, 0, 254 :title, -1 255 )"); 256 257 $this->context->connection->bindInstance($sth); 258 $sth->bindValue(":id", $this->id, \PDO::PARAM_INT); 259 $sth->bindValue( 260 ":pageDefinitionId", $this->pageDefinitionId, \PDO::PARAM_INT); 261 $sth->bindValue(":title", $this->title, \PDO::PARAM_STR); 262 263 $sth->execute(); 264 265 \Scrivo\SequenceNo::position($this->context, "page_definition_tab", 266 "page_definition_id", $this->id, \Scrivo\SequenceNo::MOVE_LAST); 267 268 } catch(\PDOException $e) { 269 throw new \Scrivo\ResourceException($e); 270 } 271 } 272 273 /** 274 * Check if this page defintion tab object can be updated in the database. 275 * 276 * @throws \Scrivo\ApplicationException If the data is not accessible or 277 * one or more of the fields contain invalid data. 278 */ 279 private function validateUpdate() { 280 $this->context->checkPermission(\Scrivo\AccessController::WRITE_ACCESS); 281 } 282 283 /** 284 * Update existing page defintion tab object data in the database. 285 * 286 * First it is checked if the data of this page defintion tab object can be 287 * updated in the database, then the data is updated in the database. 288 * 289 * @throws \Scrivo\ApplicationException If the data is not accessible or 290 * one or more of the fields contain invalid data. 291 */ 292 public function update() { 293 try { 294 $this->validateUpdate(); 295 296 $sth = $this->context->connection->prepare( 297 "UPDATE page_definition_tab SET 298 page_definition_id = :pageDefinitionId, 299 title = :title 300 WHERE instance_id = :instId AND page_definition_tab_id = :id"); 301 302 $this->context->connection->bindInstance($sth); 303 $sth->bindValue(":id", $this->id, \PDO::PARAM_INT); 304 305 $sth->bindValue( 306 ":pageDefinitionId", $this->pageDefinitionId, \PDO::PARAM_INT); 307 $sth->bindValue(":title", $this->title, \PDO::PARAM_STR); 308 309 $sth->execute(); 310 311 unset($this->context->cache[$this->id]); 312 313 } catch(\PDOException $e) { 314 throw new \Scrivo\ResourceException($e); 315 } 316 } 317 318 /** 319 * Check if deletion of page defintion tab object data does not violate any 320 * business rules. 321 * 322 * @param \Scrivo\Context $context A Scrivo context. 323 * @param int $id The object id of the page defintion tab to select. 324 * 325 * @throws \Scrivo\ApplicationException If the data is not accessible or 326 * if it is not possible to delete the language data. 327 */ 328 private static function validateDelete(\Scrivo\Context $context, $id) { 329 $context->checkPermission(\Scrivo\AccessController::WRITE_ACCESS); 330 } 331 332 /** 333 * Delete existing page defintion tab data from the database. 334 * 335 * First it is is checked if it's possible to delete page defintion tab 336 * data, then the page defintion tab data including its dependencies is 337 * deleted from the database. 338 * 339 * @param \Scrivo\Context $context A Scrivo context. 340 * @param int $id The object id of the page defintion tab to select. 341 * 342 * @throws \Scrivo\ApplicationException If the data is not accessible or 343 * if it is not possible to delete the page defintion tab data. 344 */ 345 public static function delete(\Scrivo\Context $context, $id) { 346 \Scrivo\ArgumentCheck::assertArgs(func_get_args(), array( 347 null, 348 array(\Scrivo\ArgumentCheck::TYPE_INTEGER) 349 )); 350 try { 351 self::validateDelete($context, $id); 352 353 $sth = $context->connection->prepare( 354 "DELETE FROM page_definition_tab 355 WHERE instance_id = :instId AND page_definition_tab_id = :id"); 356 357 $context->connection->bindInstance($sth); 358 $sth->bindValue(":id", $id, \PDO::PARAM_INT); 359 360 $sth->execute(); 361 362 unset($context->cache[$id]); 363 364 } catch(\PDOException $e) { 365 throw new \Scrivo\ResourceException($e); 366 } 367 } 368 369 /** 370 * Move an tab one position up (left) or down (right). 371 * 372 * @param int $dir Direction of the move, see \Scrivo\SequenceNo:::MOVE_* 373 */ 374 public function move($dir=\Scrivo\SequenceNo::MOVE_DOWN) { 375 \Scrivo\ArgumentCheck::assertArgs(func_get_args(), array( 376 array(\Scrivo\ArgumentCheck::TYPE_INTEGER) 377 ), 0); 378 379 $this->context->checkPermission(\Scrivo\AccessController::WRITE_ACCESS); 380 381 \Scrivo\SequenceNo::position($this->context, "page_definition_tab", 382 "page_definition_id", $this->id, $dir); 383 } 384 385 /** 386 * Fetch a page defintion tab object from the database using its object id. 387 * 388 * @param \Scrivo\Context $context A Scrivo context. 389 * @param int $id The object id of the page defintion tab to select. 390 * 391 * @return \Scrivo\PageDefinitionTab The requested page defintion 392 * tab object. 393 */ 394 public static function fetch(\Scrivo\Context $context, $id) { 395 \Scrivo\ArgumentCheck::assertArgs(func_get_args(), array( 396 null, 397 array(\Scrivo\ArgumentCheck::TYPE_INTEGER) 398 )); 399 try { 400 // Try to retieve the page defintion tab from the cache ... 401 if (isset($context->cache["TAB_".$id])) { 402 // ... get it from the cache and set the context. 403 $pageDefinitionTab = $context->cache["TAB_".$id]; 404 $pageDefinitionTab->context = $context; 405 } else { 406 // ... else retrieve it and set it in the cache. 407 $sth = $context->connection->prepare( 408 "SELECT page_definition_tab_id, page_definition_id, sequence_no, 409 title, application_definition_id 410 FROM page_definition_tab 411 WHERE instance_id = :instId AND page_definition_tab_id = :id"); 412 413 $context->connection->bindInstance($sth); 414 $sth->bindValue(":id", $id, \PDO::PARAM_INT); 415 416 $sth->execute(); 417 418 if ($sth->rowCount() != 1) { 419 throw new \Scrivo\SystemException( 420 "Failed to load page defintion tab"); 421 } 422 423 $pageDefinitionTab = new \Scrivo\PageDefinitionTab($context); 424 $pageDefinitionTab->setFields( 425 $context, $sth->fetch(\PDO::FETCH_ASSOC)); 426 427 $context->cache["TAB_".$id] = $pageDefinitionTab; 428 } 429 430 return $pageDefinitionTab; 431 432 } catch(\PDOException $e) { 433 throw new \Scrivo\ResourceException($e); 434 } 435 } 436 437 /** 438 * Select page defintion tabs from the database. 439 * 440 * @param \Scrivo\Context $context A Scrivo context. 441 * @param int $pageDefinitionId The id of the pageDefinition for which to 442 * retrieve the tabs. 443 * 444 * @return \Scrivo\PageDefinitionTab[id] An array containing the selected 445 * page defintion tabs. 446 */ 447 public static function select(\Scrivo\Context $context, $pageDefinitionId) { 448 \Scrivo\ArgumentCheck::assertArgs(func_get_args(), array( 449 null, 450 array(\Scrivo\ArgumentCheck::TYPE_INTEGER) 451 )); 452 try { 453 $sth = $context->connection->prepare( 454 "SELECT page_definition_tab_id, page_definition_id, sequence_no, 455 title, application_definition_id 456 FROM page_definition_tab 457 WHERE instance_id = :instId AND page_definition_id = :pageDefinitionId 458 ORDER BY sequence_no"); 459 460 $context->connection->bindInstance($sth); 461 $sth->bindValue( 462 ":pageDefinitionId", $pageDefinitionId, \PDO::PARAM_INT); 463 464 $sth->execute(); 465 466 $res = array(); 467 468 while ($rd = $sth->fetch(\PDO::FETCH_ASSOC)) { 469 470 $li = new PageDefinitionTab(); 471 $li->setFields($context, $rd); 472 473 $res[$li->id] = $li; 474 } 475 476 return $res; 477 478 } catch(\PDOException $e) { 479 throw new \Scrivo\ResourceException($e); 480 } 481 } 482 483 } 484 485 ?>
Documentation generated by phpDocumentor 2.0.0a12 and ScrivoDocumentor on August 29, 2013