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: ListItemDefinition.php 866 2013-08-25 16:22:35Z geert $ 30 */ 31 32 /** 33 * Implementation of the \Scrivo\ListItemDefinition class. 34 */ 35 36 namespace Scrivo; 37 38 /** 39 * Class to hold the definition of a list item. Scrivo lists contain list 40 * items and each list item can have a number of configurable fields. List 41 * item definitions are used to store this configuration data. 42 * 43 * Note that a Scrivo list is not limited to items using one list item 44 * definition only, a Scrivo list can contain items that use different list 45 * item definitions. 46 * 47 * Also note that Scrivo lists are actually defined by these list item 48 * defintions: a list is a collection of items using a particular set of 49 * list item definitions. This set is determined by the application definition. 50 * 51 * Lists can be hierachically structured (like a threaded forum for instance). 52 * Therefore list item definitions can structured accordingly: ie. list items 53 * using the "reply" definition can only occur underneath list items using 54 * the "topic" ore "reply" item. 55 * 56 * @property-read int $id The id of the list item definition (DB key). 57 * @property int $applicationDefinitionId The id of the list/application 58 * where this list item definition belongs to. 59 * @property \Scrivo\String $icon The location of an icon to identify the 60 * item definition in the user interface. 61 * @property int $pageDefinitionId The id of the template to use for linked 62 * pages (optional). 63 * @property \Scrivo\String $phpSelector A textual identification/key for 64 * this list item definition. 65 * @property \Scrivo\String $title A descriptive name for the list item 66 * definition. 67 * @property \Scrivo\String $titleLabel An alternative label for the title 68 * property of a list item in the user interface. 69 * @property int $titleWidth The width of the title property in the user 70 * interface for a list item (column width in list view mode). 71 * @property array $childListItemDefinitionIds List of possible list item 72 * definitions for child items. 73 * @property array $parentListItemDefinitionIds List of possible list item 74 * definitions for parent items. 75 */ 76 class ListItemDefinition { 77 78 /** 79 * The id of the list item definition (DB key). 80 * @var int 81 */ 82 private $id = 0; 83 84 /** 85 * The id of the list/application where this list item definition belongs 86 * to. 87 * @var int 88 */ 89 private $applicationDefinitionId = 0; 90 91 /** 92 * The id of the template to use for linked pages (optional). 93 * @var int 94 */ 95 private $pageDefinitionId = 0; 96 97 /** 98 * A descriptive name for the list item definition. 99 * @var \Scrivo\String 100 */ 101 private $title = null; 102 103 /** 104 * The location of an icon to identify the item definition in the user 105 * interface. 106 * @var \Scrivo\String 107 */ 108 private $icon = null; 109 110 /** 111 * A textual identification/key for this list item definition. 112 * @var \Scrivo\String 113 */ 114 private $phpSelector = null; 115 116 /** 117 * The width of the title property in the user interface for a list item 118 * (column width in list view mode). 119 * @var int 120 */ 121 private $titleWidth = 0; 122 123 /** 124 * An alternative label for the title property of a list item in the user 125 * interface. 126 * @var \Scrivo\String 127 */ 128 private $titleLabel = null; 129 130 /** 131 * When working with hierarchical type definitions this member contains an 132 * array with the child list item definitions of this list item definition. 133 * @var int[] 134 */ 135 private $childListItemDefinitionIds = null; 136 137 /** 138 * When working with hierarchical type definitions this member contains an 139 * array with the parent list item definitions of this list item definition. 140 * @var int[] 141 */ 142 private $parentListItemDefinitionIds = null; 143 144 /** 145 * A Scrivo context. 146 * @var \Scrivo\Context 147 */ 148 private $context = null; 149 150 /** 151 * Create an empty list item definition object. 152 * 153 * @param \Scrivo\Context $context A Scrivo context. 154 */ 155 public function __construct(\Scrivo\Context $context=null) { 156 \Scrivo\ArgumentCheck::assertArgs(func_get_args(), array(null)); 157 158 if ($context) { 159 $this->title = new \Scrivo\String(); 160 $this->icon = new \Scrivo\String(); 161 $this->phpSelector = new \Scrivo\String(); 162 $this->titleLabel = new \Scrivo\String(); 163 164 $this->context = $context; 165 } 166 } 167 168 /** 169 * Implementation of the readable properties using the PHP magic 170 * method __get(). 171 * 172 * @param string $name The name of the property to get. 173 * 174 * @return mixed The value of the requested property. 175 */ 176 public function __get($name) { 177 switch($name) { 178 case "id": return $this->id; 179 case "applicationDefinitionId": 180 return $this->applicationDefinitionId; 181 case "pageDefinitionId": return $this->pageDefinitionId; 182 case "title": return $this->title; 183 case "icon": return $this->icon; 184 case "phpSelector": return $this->phpSelector; 185 case "titleWidth": return $this->titleWidth; 186 case "titleLabel": return $this->titleLabel; 187 case "childListItemDefinitionIds": 188 return $this->getChildListItemDefinitionIds(); 189 case "parentListItemDefinitionIds": 190 return $this->getParentListItemDefinitionIds(); 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 "applicationDefinitionId": 205 $this->setApplicationDefinitionId($value); return; 206 case "pageDefinitionId": $this->setPageDefinitionId($value); return; 207 case "title": $this->setTitle($value); return; 208 case "icon": $this->setIcon($value); return; 209 case "phpSelector": $this->setPhpSelector($value); return; 210 case "titleWidth": $this->setTitleWidth($value); return; 211 case "titleLabel": $this->setTitleLabel($value); return; 212 } 213 throw new \Scrivo\SystemException("No such set-property '$name'."); 214 } 215 216 /** 217 * Convenience method to set the fields of a list item definition object 218 * from an array (result set row). 219 * 220 * @param \Scrivo\Context $context A Scrivo context. 221 * @param array $rd An array containing the field data using the database 222 * field names as keys. 223 */ 224 private function setFields(\Scrivo\Context $context, array $rd) { 225 226 $this->id = intval($rd["list_item_definition_id"]); 227 $this->applicationDefinitionId = intval($rd["application_definition_id"]); 228 $this->pageDefinitionId = intval($rd["page_definition_id"]); 229 $this->title = new \Scrivo\String($rd["title"]); 230 $this->icon = new \Scrivo\String($rd["icon"]); 231 $this->phpSelector = new \Scrivo\String($rd["php_key"]); 232 $this->titleWidth = intval($rd["title_width"]); 233 $this->titleLabel = new \Scrivo\String($rd["title_label"]); 234 235 $this->context = $context; 236 } 237 238 /** 239 * Get an array with the child list item definitions of this list item 240 * definition when working with hierarchical list type definitions. 241 * 242 * @return array An array with the child list item definitions for this 243 * list item definition 244 */ 245 private function getChildListItemDefinitionIds() { 246 if ($this->childListItemDefinitionIds === null) { 247 $this->childListItemDefinitionIds = 248 $this->selectChildListItemDefinitionIds(); 249 } 250 return $this->childListItemDefinitionIds; 251 } 252 253 /** 254 * Get an array with the parent list item definitions of this list item 255 * definition when working with hierarchical list type definitions. 256 * 257 * @return array An array with the parent list item definitions for this 258 * list item definition 259 */ 260 private function getParentListItemDefinitionIds() { 261 if ($this->parentListItemDefinitionIds === null) { 262 $this->parentListItemDefinitionIds = 263 $this->selectParentListItemDefinitionIds(); 264 } 265 return $this->parentListItemDefinitionIds; 266 } 267 268 /** 269 * Set the id of the list/application where this list item definition 270 * belongs to. It is only possible to set the application id for 271 * uninitialized list item definitions. 272 * 273 * @param int $appId The id of the list/application where this list item 274 * definition belongs to. 275 */ 276 private function setApplicationDefinitionId($appId) { 277 \Scrivo\ArgumentCheck::assertArgs(func_get_args(), array( 278 array(\Scrivo\ArgumentCheck::TYPE_INTEGER) 279 )); 280 281 if ($this->applicationDefinitionId) { 282 throw new \Scrivo\SystemException( 283 "Can't reset the the application id"); 284 } 285 return $this->applicationDefinitionId = $appId; 286 } 287 288 /** 289 * Set The id of the template to use for linked pages (optional). 290 * 291 * @param int $pageDefinitionId The id of the template to use for linked 292 * pages (optional). 293 */ 294 private function setPageDefinitionId($pageDefinitionId) { 295 \Scrivo\ArgumentCheck::assertArgs(func_get_args(), array( 296 array(\Scrivo\ArgumentCheck::TYPE_INTEGER) 297 )); 298 299 $this->pageDefinitionId = $pageDefinitionId; 300 } 301 302 /** 303 * Set A descriptive name for the list item definition. 304 * 305 * @param \Scrivo\String $title A descriptive name for the list item 306 * definition. 307 */ 308 private function setTitle(\Scrivo\String $title) { 309 $this->title = $title; 310 } 311 312 /** 313 * Set The location of an icon to identify the item definition in the user 314 * interface. 315 * 316 * @param \Scrivo\String $icon The location of an icon to identify the 317 * item definition in the user interface. 318 */ 319 private function setIcon(\Scrivo\String $icon) { 320 $this->icon = $icon; 321 } 322 323 /** 324 * Set A textual identification/key for this list item definition. 325 * 326 * @param \Scrivo\String $phpSelector A textual identification/key for 327 * this list item definition. 328 */ 329 private function setPhpSelector(\Scrivo\String $phpSelector) { 330 $this->phpSelector = $phpSelector; 331 } 332 333 /** 334 * Set The width of the title property in the user interface for a list 335 * item (column width in list view mode). 336 * 337 * @param int $titleWidth The width of the title property in the user 338 * interface for a list item (column width in list view mode). 339 */ 340 private function setTitleWidth($titleWidth) { 341 \Scrivo\ArgumentCheck::assertArgs(func_get_args(), array( 342 array(\Scrivo\ArgumentCheck::TYPE_INTEGER) 343 )); 344 345 $this->titleWidth = $titleWidth; 346 } 347 348 /** 349 * Set An alternative label for the title property of a list item in the 350 * user interface. 351 * 352 * @param \Scrivo\String $titleLabel An alternative label for the title 353 * property of a list item in the user interface. 354 */ 355 private function setTitleLabel(\Scrivo\String $titleLabel) { 356 $this->titleLabel = $titleLabel; 357 } 358 359 /** 360 * Retrieve the ids of the child list item definitions. List items 361 * definitions can have a parent child relation to enable you to create 362 * nested (hierarchical) lists. 363 * 364 * @return object[id] An array of objects containing the id field. 365 */ 366 private function selectChildListItemDefinitionIds() { 367 try { 368 $sth = $this->context->connection->prepare( 369 "SELECT list_item_definition_id 370 FROM parent_list_item_definitions 371 WHERE instance_id = :instId AND parent_list_item_definition_id = :id"); 372 373 $this->context->connection->bindInstance($sth); 374 $sth->bindValue(":id", $this->id, \PDO::PARAM_INT); 375 376 $sth->execute(); 377 378 $res = array(); 379 380 while ($rd = $sth->fetch(\PDO::FETCH_ASSOC)) { 381 $id = intval($rd["list_item_definition_id"]); 382 $res[$id] = (object)array("id" => $id); 383 } 384 385 return $res; 386 387 } catch(\PDOException $e) { 388 throw new \Scrivo\ResourceException($e); 389 } 390 } 391 392 /** 393 * Retrieve the ids of the parent list item definitions. List items 394 * definitions can have a parent child relation to enable you to create 395 * nested (hierarchical) lists. 396 * 397 * @return object[id] An array of objects containing the id field. 398 */ 399 private function selectParentListItemDefinitionIds() { 400 try { 401 $sth = $this->context->connection->prepare( 402 "SELECT parent_list_item_definition_id 403 FROM parent_list_item_definitions 404 WHERE instance_id = :instId AND list_item_definition_id = :id"); 405 406 $this->context->connection->bindInstance($sth); 407 $sth->bindValue(":id", $this->id, \PDO::PARAM_INT); 408 409 $sth->execute(); 410 411 $res = array(); 412 413 while ($rd = $sth->fetch(\PDO::FETCH_ASSOC)) { 414 $id = intval($rd["parent_list_item_definition_id"]); 415 $res[$id] = (object)array("id" => $id); 416 } 417 418 return $res; 419 420 } catch(\PDOException $e) { 421 throw new \Scrivo\ResourceException($e); 422 } 423 } 424 425 /** 426 * Check if this list item definition object can be inserted into the 427 * database. 428 * 429 * @throws \Scrivo\ApplicationException If the data is not accessible or 430 * one or more of the fields contain invalid data. 431 */ 432 private function validateInsert() { 433 $this->context->checkPermission(\Scrivo\AccessController::WRITE_ACCESS); 434 // application id not 0 435 } 436 437 /** 438 * Insert new list item definition object data into the database. 439 * 440 * First it is checked if the data of this list item definition object can 441 * be inserted into the database, then the data is inserted into the 442 * database. If no id was set a new object id is generated. 443 * 444 * @throws \Scrivo\ApplicationException If the data is not accessible or 445 * one or more of the fields contain invalid data. 446 */ 447 public function insert() { 448 try { 449 $this->validateInsert(); 450 451 if (!$this->id) { 452 $this->id = $this->context->connection->generateId(); 453 } 454 455 $sth = $this->context->connection->prepare( 456 "INSERT INTO list_item_definition ( 457 instance_id, list_item_definition_id, sequence_no, application_definition_id, 458 page_definition_id, title, icon, php_key, title_width, 459 title_label 460 ) VALUES ( 461 :instId, :id, 0, :appDefId, 462 :pageDefId, :title, :icon, :phpSelector, :titleWidth, 463 :titleLabel 464 )"); 465 466 $this->context->connection->bindInstance($sth); 467 $sth->bindValue(":id", $this->id, \PDO::PARAM_INT); 468 $sth->bindValue( 469 ":appDefId", $this->applicationDefinitionId, \PDO::PARAM_INT); 470 $sth->bindValue( 471 ":pageDefId", $this->pageDefinitionId, \PDO::PARAM_INT); 472 $sth->bindValue(":title", $this->title, \PDO::PARAM_STR); 473 $sth->bindValue(":icon", $this->icon, \PDO::PARAM_STR); 474 $sth->bindValue( 475 ":phpSelector", $this->phpSelector, \PDO::PARAM_STR); 476 $sth->bindValue(":titleWidth", $this->titleWidth, \PDO::PARAM_INT); 477 $sth->bindValue(":titleLabel", $this->titleLabel, \PDO::PARAM_STR); 478 479 $sth->execute(); 480 481 \Scrivo\SequenceNo::position($this->context, "list_item_definition", 482 "application_definition_id", $this->id, \Scrivo\SequenceNo::MOVE_LAST); 483 484 } catch(\PDOException $e) { 485 throw new \Scrivo\ResourceException($e); 486 } 487 } 488 489 /** 490 * Set the parent list item definitions for this list item definition in 491 * the database. List items definitions can have a parent child relation 492 * to enable you to create nested (hierarchical) lists. 493 * 494 * @param array $pids An array with ids of parent list item definitions. 495 */ 496 public function updateParentListItemDefinitionIds(array $pids) { 497 \Scrivo\ArgumentCheck::assertArgs(func_get_args(), array(null)); 498 try { 499 $this->context->checkPermission(\Scrivo\AccessController::WRITE_ACCESS); 500 501 $sth = $this->context->connection->prepare( 502 "DELETE FROM parent_list_item_definitions 503 WHERE instance_id = :instId AND list_item_definition_id = :id"); 504 505 $this->context->connection->bindInstance($sth); 506 $sth->bindValue(":id", $this->id, \PDO::PARAM_INT); 507 508 $sth->execute(); 509 510 if ($pids) { 511 512 $sth = $this->context->connection->prepare( 513 "INSERT INTO parent_list_item_definitions 514 (instance_id, list_item_definition_id, parent_list_item_definition_id) 515 VALUES (:instId, :id, :pid)"); 516 517 $this->context->connection->bindInstance($sth); 518 $sth->bindValue(":id", $this->id, \PDO::PARAM_INT); 519 520 foreach ($pids as $pid) { 521 $sth->bindValue(":pid", $pid, \PDO::PARAM_INT); 522 $sth->execute(); 523 524 unset($this->context->cache[$pid]); 525 } 526 unset($this->context->cache[$this->id]); 527 } 528 529 $this->childListItemDefinitionIds = null; 530 $this->parentListItemDefinitionIds = null; 531 532 } catch(\PDOException $e) { 533 throw new \Scrivo\ResourceException($e); 534 } 535 } 536 537 /** 538 * Check if this list item definition object can be updated in the database. 539 * 540 * @throws \Scrivo\ApplicationException If the data is not accessible or 541 * one or more of the fields contain invalid data. 542 */ 543 private function validateUpdate() { 544 $this->context->checkPermission(\Scrivo\AccessController::WRITE_ACCESS); 545 } 546 547 /** 548 * Update existing list item definition object data in the database. 549 * 550 * First it is checked if the data of this list item definition object can 551 * be updated in the database, then the data is updated in the database. 552 * 553 * @throws \Scrivo\ApplicationException If the data is not accessible or 554 * one or more of the fields contain invalid data. 555 */ 556 public function update() { 557 try { 558 $this->validateUpdate(); 559 560 $sth = $this->context->connection->prepare( 561 "UPDATE list_item_definition SET 562 page_definition_id = :pageDefId, title = :title, icon = :icon, 563 php_key = :phpSelector, title_width = :titleWidth, 564 title_label = :titleLabel 565 WHERE instance_id = :instId AND list_item_definition_id = :id"); 566 567 $this->context->connection->bindInstance($sth); 568 $sth->bindValue(":id", $this->id, \PDO::PARAM_INT); 569 570 $sth->bindValue( 571 ":pageDefId", $this->pageDefinitionId, \PDO::PARAM_INT); 572 $sth->bindValue(":title", $this->title, \PDO::PARAM_STR); 573 $sth->bindValue(":icon", $this->icon, \PDO::PARAM_STR); 574 $sth->bindValue( 575 ":phpSelector", $this->phpSelector, \PDO::PARAM_STR); 576 $sth->bindValue(":titleWidth", $this->titleWidth, \PDO::PARAM_INT); 577 $sth->bindValue(":titleLabel", $this->titleLabel, \PDO::PARAM_STR); 578 579 $sth->execute(); 580 581 unset($this->context->cache[$this->id]); 582 583 } catch(\PDOException $e) { 584 throw new \Scrivo\ResourceException($e); 585 } 586 } 587 588 /** 589 * Check if deletion of list item definition object data does not violate 590 * any business rules. 591 * 592 * @param \Scrivo\Context $context A Scrivo context. 593 * @param int $id The object id of the list item definition to select. 594 * 595 * @throws \Scrivo\ApplicationException If the data is not accessible or 596 * if it is not possible to delete the language data. 597 */ 598 private static function validateDelete(\Scrivo\Context $context, $id) { 599 $context->checkPermission(\Scrivo\AccessController::WRITE_ACCESS); 600 } 601 602 /** 603 * Delete existing list item definition data from the database. 604 * 605 * First it is is checked if it's possible to delete list item definition 606 * data, then the list item definition data including its dependencies is 607 * deleted from the database. 608 * 609 * @param \Scrivo\Context $context A Scrivo context. 610 * @param int $id The object id of the list item definition to select. 611 * 612 * @throws \Scrivo\ApplicationException If the data is not accessible or 613 * if it is not possible to delete the list item definition data. 614 */ 615 public static function delete(\Scrivo\Context $context, $id) { 616 \Scrivo\ArgumentCheck::assertArgs(func_get_args(), array( 617 null, 618 array(\Scrivo\ArgumentCheck::TYPE_INTEGER) 619 )); 620 try { 621 self::validateDelete($context, $id); 622 623 $tmp = self::fetch($context, $id); 624 625 $sth = $context->connection->prepare( 626 "DELETE FROM parent_list_item_definitions 627 WHERE instance_id = :instId AND 628 (list_item_definition_id = :id OR parent_list_item_definition_id = :id)"); 629 630 $context->connection->bindInstance($sth); 631 $sth->bindValue(":id", $id, \PDO::PARAM_INT); 632 633 $sth->execute(); 634 635 $sth = $context->connection->prepare( 636 "DELETE FROM list_item_definition 637 WHERE instance_id = :instId AND list_item_definition_id = :id"); 638 639 $context->connection->bindInstance($sth); 640 $sth->bindValue(":id", $id, \PDO::PARAM_INT); 641 642 $sth->execute(); 643 644 unset($context->cache[$id]); 645 646 } catch(\PDOException $e) { 647 throw new \Scrivo\ResourceException($e); 648 } 649 } 650 651 /** 652 * Move a list item definition one position up or down. 653 * 654 * @param int $dir Direction of the move, see \Scrivo\SequenceNo:::MOVE_* 655 */ 656 function move($dir=\Scrivo\SequenceNo::MOVE_DOWN) { 657 658 $this->context->checkPermission(\Scrivo\AccessController::WRITE_ACCESS); 659 660 \Scrivo\SequenceNo::position($this->context, "list_item_definition", 661 "application_definition_id", $this->id, $dir); 662 } 663 664 /** 665 * Fetch a list item definition object from the database using its 666 * object id. 667 * 668 * @param \Scrivo\Context $context A Scrivo context. 669 * @param int $id The object id of the list item definition to select. 670 * 671 * @return \Scrivo\ListItemDefinition The requested list item definition 672 * object. 673 */ 674 public static function fetch(\Scrivo\Context $context, $id) { 675 \Scrivo\ArgumentCheck::assertArgs(func_get_args(), array( 676 null, 677 array(\Scrivo\ArgumentCheck::TYPE_INTEGER) 678 )); 679 try { 680 // Try to retieve the list item definition from the cache ... 681 if (isset($context->cache[$id])) { 682 // ... get it from the cache and set the context. 683 $listItemDefinition = $context->cache[$id]; 684 $listItemDefinition->context = $context; 685 } else { 686 // ... else retrieve it and set it in the cache. 687 $sth = $context->connection->prepare( 688 "SELECT list_item_definition_id, sequence_no, application_definition_id, 689 page_definition_id, title, icon, php_key, title_width, 690 title_label 691 FROM list_item_definition 692 WHERE instance_id = :instId AND list_item_definition_id = :id"); 693 694 $context->connection->bindInstance($sth); 695 $sth->bindValue(":id", $id, \PDO::PARAM_INT); 696 697 $sth->execute(); 698 699 if ($sth->rowCount() != 1) { 700 throw new \Scrivo\SystemException( 701 "Failed to load list item definition"); 702 } 703 704 $listItemDefinition = new \Scrivo\ListItemDefinition($context); 705 $listItemDefinition->setFields( 706 $context, $sth->fetch(\PDO::FETCH_ASSOC)); 707 708 $context->cache[$id] = $listItemDefinition; 709 } 710 711 return $listItemDefinition; 712 713 } catch(\PDOException $e) { 714 throw new \Scrivo\ResourceException($e); 715 } 716 } 717 718 /** 719 * Select list item definitions from the database. 720 * 721 * @param \Scrivo\Context $context A Scrivo context. 722 * @param int $applicationDefinitionId The id of the application for which 723 * to retrieve the list item definitions. 724 * @param int $parentId An optional parent list item definition id. 725 * 726 * @return \Scrivo\ListItemDefinition[id] An array containing the selected 727 * list item definitions. 728 */ 729 public static function select( 730 \Scrivo\Context $context, $applicationDefinitionId, $parentId=-1) { 731 \Scrivo\ArgumentCheck::assertArgs(func_get_args(), array( 732 null, 733 array(\Scrivo\ArgumentCheck::TYPE_INTEGER), 734 array(\Scrivo\ArgumentCheck::TYPE_INTEGER) 735 )); 736 try { 737 $sth = $context->connection->prepare( 738 "SELECT I.list_item_definition_id, I.sequence_no, I.application_definition_id, 739 I.page_definition_id, I.title, I.icon, I.php_key, 740 I.title_width, I.title_label 741 FROM list_item_definition I ". 742 ($parentId != -1 ? 743 "LEFT JOIN parent_list_item_definitions P ON (P.instance_id = :instId 744 AND I.list_item_definition_id = P.list_item_definition_id) " : ""). 745 "WHERE I.instance_id = :instId AND I.application_definition_id = :appId ". 746 ($parentId != -1 ? "AND 747 IFNULL(P.parent_list_item_definition_id, 0) = :parentId " : ""). 748 "ORDER BY sequence_no"); 749 750 $context->connection->bindInstance($sth); 751 $sth->bindValue( 752 ":appId", $applicationDefinitionId, \PDO::PARAM_INT); 753 if ($parentId != -1) { 754 $sth->bindValue( 755 ":parentId", $parentId, \PDO::PARAM_INT); 756 757 } 758 759 $sth->execute(); 760 761 $res = array(); 762 763 while ($rd = $sth->fetch(\PDO::FETCH_ASSOC)) { 764 765 $li = new ListItemDefinition(); 766 $li->setFields($context, $rd); 767 768 $res[$li->id] = $li; 769 } 770 771 return $res; 772 773 } catch(\PDOException $e) { 774 throw new \Scrivo\ResourceException($e); 775 } 776 } 777 778 /** 779 * Select child list item definitions for a given parent list item 780 * (not a list item definition!) from the database. 781 * 782 * TODO: 1) This method refereces content table list_item, should it be 783 * defined in this class? 2) is this obselete? 784 * 785 * @param \Scrivo\Context $context A Scrivo context. 786 * @param int $applicationDefinitionId The id of the application for which 787 * to retrieve the list item definitions. 788 * @param int $listItemId The id of the list item 789 * 790 * @return ListItemDefinition[id] An array containing the selected list 791 * item definitions. 792 public static function selectChildListItemDefintions( 793 \Scrivo\Context $context, $applicationDefinitionId, $listItemId) { 794 try { 795 $sth = $context->connection->prepare( 796 "SELECT T.list_item_definition_id, T.sequence_no, T.application_definition_id, 797 T.page_definition_id, T.title, T.icon, T.php_key, 798 T.title_width, T.title_label 799 FROM list_item_definition T 800 LEFT JOIN parent_list_item_definitions P ON (P.instance_id = :instId 801 AND T.list_item_definition_id = P.list_item_definition_id) 802 LEFT JOIN list_item I ON (I.instance_id = :instId 803 AND I.list_item_definition = P.parent_list_item_definition_id) 804 WHERE instance_id = :instId AND application_definition_id = :appId AND 805 IFNULL(I.list_item_id, 0) = :pid 806 ORDER BY T.sequence_no"); 807 808 $context->connection->bindInstance($sth); 809 $sth->bindValue( 810 ":appId", $applicationDefinitionId, \PDO::PARAM_INT); 811 $sth->bindValue(":pid", $listItemId, \PDO::PARAM_INT); 812 813 $sth->execute(); 814 815 $res = array(); 816 817 while ($rd = $sth->fetch(\PDO::FETCH_ASSOC)) { 818 819 $li = new ListItemDefinition(); 820 $li->setFields($context, $rd); 821 822 $res[$li->id] = $li; 823 } 824 825 return $res; 826 827 } catch(\PDOException $e) { 828 throw new \Scrivo\ResourceException($e); 829 } 830 } 831 */ 832 } 833 834 ?>
Documentation generated by phpDocumentor 2.0.0a12 and ScrivoDocumentor on August 29, 2013