TYPO3 API  SVNRelease
class.t3lib_utility_dependency_element.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003  * Copyright notice
00004  *
00005  * (c) 2010-2011 Oliver Hader <oliver@typo3.org>
00006  * All rights reserved
00007  *
00008  * This script is part of the TYPO3 project. The TYPO3 project is
00009  * free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * The GNU General Public License can be found at
00015  * http://www.gnu.org/copyleft/gpl.html.
00016  * A copy is found in the textfile GPL.txt and important notices to the license
00017  * from the author is found in LICENSE.txt distributed with these scripts.
00018  *
00019  *
00020  * This script is distributed in the hope that it will be useful,
00021  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00022  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023  * GNU General Public License for more details.
00024  *
00025  * This copyright notice MUST APPEAR in all copies of the script!
00026  ***************************************************************/
00027 
00028 /**
00029  * Object to hold information on a dependent database element in abstract.
00030  */
00031 class t3lib_utility_Dependency_Element {
00032     const REFERENCES_ChildOf = 'childOf';
00033     const REFERENCES_ParentOf = 'parentOf';
00034     const EVENT_Construct = 't3lib_utility_Dependency_Element::construct';
00035     const EVENT_CreateChildReference = 't3lib_utility_Dependency_Element::createChildReference';
00036     const EVENT_CreateParentReference = 't3lib_utility_Dependency_Element::createParentReference';
00037     const RESPONSE_Skip = 't3lib_utility_Dependency_Element->skip';
00038 
00039     /**
00040      * @var string
00041      */
00042     protected $table;
00043 
00044     /**
00045      * @var integer
00046      */
00047     protected $id;
00048 
00049     /**
00050      * @var array
00051      */
00052     protected $data;
00053 
00054     /**
00055      * @var t3lib_utility_Dependency
00056      */
00057     protected $dependency;
00058 
00059     /**
00060      * @var array
00061      */
00062     protected $children;
00063 
00064     /**
00065      * @var array
00066      */
00067     protected $parents;
00068 
00069     /**
00070      * @var boolean
00071      */
00072     protected $traversingParents = FALSE;
00073 
00074     /**
00075      * @var t3lib_utility_Dependency_Element
00076      */
00077     protected $outerMostParent;
00078 
00079     /**
00080      * @var array
00081      */
00082     protected $nestedChildren;
00083 
00084     /**
00085      * Creates this object.
00086      *
00087      * @param string $table
00088      * @param integer $id
00089      * @param array $data (optional)
00090      * @param t3lib_utility_Dependency $dependency
00091      */
00092     public function __construct($table, $id, array $data = array(), t3lib_utility_Dependency $dependency) {
00093         $this->table = $table;
00094         $this->id = intval($id);
00095         $this->data = $data;
00096         $this->dependency = $dependency;
00097 
00098         $this->dependency->executeEventCallback(self::EVENT_Construct, $this);
00099     }
00100 
00101     /**
00102      * Gets the table.
00103      *
00104      * @return string
00105      */
00106     public function getTable() {
00107         return $this->table;
00108     }
00109 
00110     /**
00111      * Gets the id.
00112      *
00113      * @return integer
00114      */
00115     public function getId() {
00116         return $this->id;
00117     }
00118 
00119     /**
00120      * Gets the data.
00121      *
00122      * @return array
00123      */
00124     public function getData() {
00125         return $this->data;
00126     }
00127 
00128     /**
00129      * Gets a value for a particular key from the data.
00130      *
00131      * @param string $key
00132      * @return mixed
00133      */
00134     public function getDataValue($key) {
00135         $result = NULL;
00136 
00137         if ($this->hasDataValue($key)) {
00138             $result = $this->data[$key];
00139         }
00140 
00141         return $result;
00142     }
00143 
00144     /**
00145      * Sets a value for a particular key in the data.
00146      *
00147      * @param string $key
00148      * @param mixed $value
00149      * @return void
00150      */
00151     public function setDataValue($key, $value) {
00152         $this->data[$key] = $value;
00153     }
00154 
00155     /**
00156      * Determines whether a particular key holds data.
00157      *
00158      * @param string $key
00159      * @return
00160      */
00161     public function hasDataValue($key) {
00162         return (isset($this->data[$key]));
00163     }
00164 
00165     /**
00166      * Converts this object for string representation.
00167      *
00168      * @return string
00169      */
00170     public function __toString() {
00171         return self::getIdentifier($this->table, $this->id);
00172     }
00173 
00174     /**
00175      * Gets the parent dependency object.
00176      *
00177      * @return t3lib_utility_Dependency
00178      */
00179     public function getDependency() {
00180         return $this->dependency;
00181     }
00182 
00183     /**
00184      * Gets all child references.
00185      *
00186      * @return array
00187      */
00188     public function getChildren() {
00189         if (!isset($this->children)) {
00190             $this->children = array();
00191             $rows = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
00192                 '*',
00193                 'sys_refindex',
00194                 'tablename=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($this->table, 'sys_refindex') .
00195                 ' AND recuid=' . $this->id
00196             );
00197             if (is_array($rows)) {
00198                 foreach ($rows as $row) {
00199                     $reference = $this->getDependency()->getFactory()->getReferencedElement(
00200                         $row['ref_table'], $row['ref_uid'], $row['field'], array(), $this->getDependency()
00201                     );
00202                     $callbackResponse = $this->dependency->executeEventCallback(
00203                         self::EVENT_CreateChildReference,
00204                         $this, array('reference' => $reference)
00205                     );
00206                     if ($callbackResponse !== self::RESPONSE_Skip) {
00207                         $this->children[] = $reference;
00208                     }
00209                 }
00210             }
00211         }
00212         return $this->children;
00213     }
00214 
00215     /**
00216      * Gets all parent references.
00217      *
00218      * @return array
00219      */
00220     public function getParents() {
00221         if (!isset($this->parents)) {
00222             $this->parents = array();
00223             $rows = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
00224                 '*',
00225                 'sys_refindex',
00226                 'ref_table=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($this->table, 'sys_refindex') .
00227                 ' AND deleted=0 AND ref_uid=' . $this->id
00228             );
00229             if (is_array($rows)) {
00230                 foreach ($rows as $row) {
00231                     $reference = $this->getDependency()->getFactory()->getReferencedElement(
00232                         $row['tablename'], $row['recuid'], $row['field'], array(), $this->getDependency()
00233                     );
00234                     $callbackResponse = $this->dependency->executeEventCallback(
00235                         self::EVENT_CreateParentReference,
00236                         $this, array('reference' => $reference)
00237                     );
00238                     if ($callbackResponse !== self::RESPONSE_Skip) {
00239                         $this->parents[] = $reference;
00240                     }
00241                 }
00242             }
00243         }
00244         return $this->parents;
00245     }
00246 
00247     /**
00248      * Determines whether there are child or parent references.
00249      *
00250      * @return boolean
00251      */
00252     public function hasReferences() {
00253         return (count($this->getChildren()) > 0 || count($this->getParents()) > 0);
00254     }
00255 
00256     /**
00257      * Gets the outermost parent element.
00258      *
00259      * @return t3lib_utility_Dependency_Element
00260      */
00261     public function getOuterMostParent() {
00262         if (!isset($this->outerMostParent)) {
00263             $parents = $this->getParents();
00264             if (count($parents) === 0) {
00265                 $this->outerMostParent = $this;
00266             } else {
00267                 $this->outerMostParent = FALSE;
00268                 /** @var $parent t3lib_utility_Dependency_Reference */
00269                 foreach ($parents as $parent) {
00270                     $outerMostParent = $parent->getElement()->getOuterMostParent();
00271                     if ($outerMostParent instanceof t3lib_utility_Dependency_Element) {
00272                         $this->outerMostParent = $outerMostParent;
00273                         break;
00274                     } elseif ($outerMostParent === FALSE) {
00275                         break;
00276                     }
00277                 }
00278             }
00279         }
00280 
00281         return $this->outerMostParent;
00282     }
00283 
00284     /**
00285      * Gets nested children accumulated.
00286      *
00287      * @return array
00288      */
00289     public function getNestedChildren() {
00290         if (!isset($this->nestedChildren)) {
00291             $this->nestedChildren = array();
00292             $children = $this->getChildren();
00293             /** @var $child t3lib_utility_Dependency_Reference */
00294             foreach ($children as $child) {
00295                 $this->nestedChildren = array_merge(
00296                     $this->nestedChildren,
00297                     array($child->getElement()->__toString() => $child->getElement()),
00298                     $child->getElement()->getNestedChildren()
00299                 );
00300             }
00301         }
00302 
00303         return $this->nestedChildren;
00304     }
00305 
00306     /**
00307      * Converts the object for string representation.
00308      *
00309      * @param string $table
00310      * @param integer $id
00311      * @return string
00312      */
00313     public static function getIdentifier($table, $id) {
00314         return $table . ':' . $id;
00315     }
00316 }