[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/cms/src/model/ -> lmbCmsNode.class.php (source)

   1  <?php
   2  /*

   3   * Limb PHP Framework

   4   *

   5   * @link http://limb-project.com

   6   * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)

   7   * @license    LGPL http://www.gnu.org/copyleft/lesser.html

   8   */
   9  lmb_require('limb/active_record/src/lmbActiveRecord.class.php');
  10  lmb_require('limb/cms/src/model/lmbCmsClassName.class.php');
  11  lmb_require('limb/cms/src/model/lmbCmsRootNode.class.php');
  12  
  13  /**

  14   * class lmbCmsNode.

  15   *

  16   * @package cms

  17   * @version $Id: lmbCmsNode.class.php 5998 2007-06-18 12:28:49Z pachanga $

  18   */
  19  class lmbCmsNode extends lmbActiveRecord
  20  {
  21    protected static $_gateway_path = '';
  22    protected $_db_table_name = 'node';
  23    protected $_default_sort_params = array('priority' => 'ASC');
  24    protected $_is_being_destroyed = false;
  25  
  26    protected $object;
  27    protected $url_path;
  28    protected $_tree;
  29    protected $controller_name;
  30  
  31    protected $_has_one = array('parent' => array('field' => 'parent_id',
  32                                                  'class' => 'lmbCmsNode',
  33                                                  'can_be_null' => true,
  34                                                  'cascade_delete' => false));
  35  
  36    protected $_has_many = array('kids' => array('field' => 'parent_id',
  37                                                 'class' => 'lmbCmsNode'));
  38  
  39    function __construct($magic_params = null)
  40    {
  41      $this->_tree = lmbToolkit :: instance()->getCmsTree();
  42  
  43      parent :: __construct($magic_params);
  44    }
  45  
  46    static function getGatewayPath()
  47    {
  48      return self :: $_gateway_path;
  49    }
  50  
  51    static function setGatewayPath($gateway_path)
  52    {
  53      lmbCmsNode :: $_gateway_path = $gateway_path;
  54    }
  55  
  56    protected function _createValidator()
  57    {
  58      $validator = new lmbValidator();
  59      $validator->addRequiredRule('title');
  60      $validator->addRequiredRule('identifier');
  61      return $validator;
  62    }
  63  
  64    protected function _onAfterSave()
  65    {
  66      if(is_object($this->object))
  67      {
  68        $this->object->registerOnAfterSaveCallback($this, 'updateNodeToObjectLink');
  69        $this->object->save($this->_error_list);
  70      }
  71    }
  72  
  73    protected function _onAfterUpdate()
  74    {
  75      if($this->isDirtyProperty('parent'))
  76      {
  77        $this->_tree->moveNode($this->getId(), $this->getParent()->getId());
  78      }
  79    }
  80  
  81    function updateNodeToObjectLink($object)
  82    {
  83      $this->_setRaw('object_id', $object_id = $object->getId());
  84      $this->_setRaw('object_class_id', $object_class_id = lmbCmsClassName :: generateIdFor($object));
  85      $this->_updateDbRecord(array('object_id' => $object_id,
  86                                   'object_class_id' => $object_class_id));
  87    }
  88  
  89    protected function _insertDbRecord($values)
  90    {
  91      if($this->getParent() && $parent_id = $this->getParent()->getId())
  92        return $this->_tree->createNode($parent_id, $values);
  93      else
  94      {
  95        if(!$root = $this->_tree->getRootNode())
  96        {
  97          $cms_root = new lmbCmsRootNode();
  98          $root = $cms_root->save();
  99        }
 100        return $this->_tree->createNode($root, $values);
 101      }
 102    }
 103  
 104    protected function _updateDbRecord($values)
 105    {
 106      return $this->_tree->updateNode($this->getId(), $values);
 107    }
 108  
 109    protected function _onBeforeDestroy()
 110    {
 111      if($object = $this->getObject())
 112      {
 113        $object->node = $this;
 114        $object->destroy();
 115      }
 116    }
 117  
 118    protected function _deleteDbRecord()
 119    {
 120      $this->_tree->deleteNode($this->getId());
 121    }
 122  
 123    function loadByPath($path)
 124    {
 125      if(!$node = $this->_tree->getNodeByPath($path))
 126        return false;
 127  
 128      $this->import($node);
 129      return true;
 130    }
 131  
 132    function getObject()
 133    {
 134      if(!isset($this->object_id) || !$this->object_id)
 135        return null;
 136  
 137      $class_name = lmbActiveRecord :: findById('lmbCmsClassName', $this->object_class_id, true, $this->_db_conn);
 138      return lmbActiveRecord :: findById($class_name->title, $this->object_id, true, $this->_db_conn);
 139    }
 140  
 141    function getControllerName()
 142    {
 143      if(!$this->controller_id)
 144        return '';
 145  
 146      if(!$this->controller_name)
 147      {
 148        $class_name = lmbActiveRecord :: findById('lmbCmsClassName', $this->controller_id, true, $this->_db_conn);
 149        $this->controller_name = $class_name->title;
 150      }
 151  
 152      return $this->controller_name;
 153    }
 154  
 155    function setControllerName($controller_name)
 156    {
 157      $this->controller_name = $controller_name;
 158      $this->_setRaw('controller_id', $controler_id = lmbCmsClassName :: generateIdFor($this->controller_name));
 159    }
 160  
 161    function getAbsoluteUrlPath()
 162    {
 163      return '/' . $this->getRelativeUrlPath();
 164    }
 165  
 166    function getRelativeUrlPath()
 167    {
 168      if(isset($this->url_path))
 169        return $this->url_path;
 170  
 171      if(!($parent_path = $this->_tree->getPathToNode($this->getId())))
 172        $this->url_path = $this->getIdentifier();
 173      else
 174        $this->url_path = ltrim($parent_path, '/');
 175  
 176      return $this->url_path;
 177    }
 178  
 179    function getUrlPath()
 180    {
 181      return self :: getGatewayPath() . $this->getRelativeUrlPath();
 182    }
 183  
 184    static function findByPath($path, $conn = null)
 185    {
 186      $tree = lmbToolkit :: instance()->getCmsTree();
 187      $node = $tree->getNodeByPath($path);
 188      if($node)
 189        return lmbActiveRecord :: findById('lmbCmsNode', $node['id'], $conn);
 190    }
 191  
 192    static function findById($node_id, $conn = null)
 193    {
 194      $tree = lmbToolkit :: instance()->getCmsTree();
 195      if($node_id && $node = $tree->getNode($node_id))
 196        return lmbActiveRecord :: findById('lmbCmsNode', $node['id'], true, $conn);
 197    }
 198  
 199    static function findByIdOrPath($node_id, $path, $conn = null)
 200    {
 201      if($node_ar = lmbCmsNode :: findById($node_id, $conn))
 202       return $node_ar;
 203  
 204      $tree = lmbToolkit :: instance()->getCmsTree();
 205  
 206      if($node = $tree->getNodeByPath($path))
 207        return lmbActiveRecord :: findById('lmbCmsNode', $node['id'], true, $conn);
 208    }
 209  
 210    static function findRequested()
 211    {
 212      if($path = lmbToolkit :: instance()->getRequest()->getUriPath())
 213        return lmbCmsNode :: findByPath($path);
 214    }
 215  
 216    static function findChildren($node_id, $depth = 1, $conn = null)
 217    {
 218      if($node_id)
 219      {
 220        $tree = lmbToolkit :: instance()->getCmsTree();
 221        return lmbActiveRecord :: decorateRecordSet($tree->getChildren($node_id, $depth),
 222                                                    'lmbCmsNode',
 223                                                    $conn);
 224      }
 225    }
 226  
 227    static function findChildrenByPath($path, $depth = 1, $conn = null)
 228    {
 229      $tree = lmbToolkit :: instance()->getCmsTree();
 230      if($path && $parent = $tree->getNodeByPath($path))
 231        return lmbActiveRecord :: decorateRecordSet($tree->getChildren($parent['id'], $depth),
 232                                                    'lmbCmsNode',
 233                                                    $conn);
 234    }
 235  
 236    static function findImmediateChildren($parent_id, $controller = '', $conn = null)
 237    {
 238      $criteria = new lmbSQLRawCriteria("parent_id = " . (int)$this->parent_id);
 239      if($controller)
 240      {
 241        $controller_id = lmbCmsClassName :: generateIdFor($controller);
 242        $criteria->addAnd(new lmbSQLRawCriteria('controller_id ='. $controller_id));
 243      }
 244      return lmbActiveRecord :: find('lmbCmsNode', array('criteria' => $criteria), $conn);
 245    }
 246  
 247    function getChildren($depth = 1)
 248    {
 249      return lmbActiveRecord :: decorateRecordSet($this->_tree->getChildren($this->getId(), $depth),
 250                                                  'lmbCmsNode',
 251                                                  $this->_db_conn);
 252    }
 253  
 254    function getParents()
 255    {
 256      return $this->_decorateRecordSet($this->_tree->getParents($this->getId()));
 257    }
 258  
 259    function getRoots()
 260    {
 261      return lmbActiveRecord :: decorateRecordSet($this->_tree->getChildren('/'),
 262                                                  'lmbCmsNode',
 263                                                  $this->_db_conn);
 264    }
 265  
 266    function getRootNodes()
 267    {
 268      return $this->getRoots();
 269    }
 270  
 271    function generateIdentifier($parent_id)
 272    {
 273      $identifier = lmbCmsNode :: getMaxChildIdentifier($parent_id);
 274  
 275      if($identifier === false)
 276        return 1;
 277  
 278      if(preg_match('/(.*?)(\d+)$/', $identifier, $matches))
 279        $new_identifier = $matches[1] . ($matches[2] + 1);
 280      else
 281        $new_identifier = $identifier . '1';
 282  
 283      return $new_identifier;
 284    }
 285  
 286    static function getMaxChildIdentifier($node)
 287    {
 288      if(!$parent = lmbCmsNode :: findById($node))
 289        return false;
 290  
 291      $children = lmbCmsNode :: findChildren($parent['id']);
 292      $identifiers = array();
 293      foreach($children as $child)
 294        $identifiers[] = $child['identifier'];
 295  
 296      if(count($identifiers))
 297      {
 298        uasort($identifiers, 'strnatcmp');
 299        return end($identifiers);
 300      }
 301      else
 302        return 0;
 303    }
 304  
 305  }
 306  
 307  ?>


Generated: Tue Oct 7 05:02:03 2008 Cross-referenced by PHPXref 0.7