Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Scoped Tree CakePHP Behavior

By: a guest on Nov 5th, 2010  |  syntax: PHP  |  size: 2.23 KB  |  views: 455  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2. /**
  3.  * Scoped Tree behavior - Created by Arno Esterhuizen from code by Jamie Nay to add better scope support.
  4.  *
  5.  * Scoped Tree behavior class.
  6.  *
  7.  * Enables a model object to act as a node-based tree, with subtrees allowed.
  8.  */
  9. App::import('Behavior', 'Tree');
  10.  
  11. class ScopedTreeBehavior extends TreeBehavior {
  12. /**
  13.  * Initiate Tree behavior
  14.  *
  15.  * @param object $Model instance of model
  16.  * @param array $config array of configuration settings.
  17.  * @return void
  18.  * @access public
  19.  */
  20.         function setup(&$Model, $config = array()) {
  21.                 parent::setup($Model, $config = array());
  22.                
  23.                 if (!is_array($config)) {
  24.                         $config = array('type' => $config);
  25.                 }
  26.                 $settings = array_merge($this->_defaults, $config);
  27.  
  28.                 if (in_array($settings['scope'], $Model->getAssociated('belongsTo'))) {
  29.                         $data = $Model->getAssociated($settings['scope']);
  30.                         $this->settings[$Model->alias]['scopeForeignKey'] = $data['foreignKey'];
  31.                 }
  32.         }
  33. /**
  34.  * Before save method. Called before all saves
  35.  *
  36.  * Overriden to transparently manage setting the lft and rght fields if and only if the parent field is included in the
  37.  * parameters to be saved. For newly created nodes with NO parent the left and right field values are set directly by
  38.  * this method bypassing the setParent logic.
  39.  *
  40.  * @since         1.2
  41.  * @param AppModel $Model Model instance
  42.  * @return boolean true to continue, false to abort the save
  43.  * @access public
  44.  */
  45.         function beforeSave(&$Model) {
  46.                 // Correct the scope of the behavior settings if required.
  47.                 $this->_addScopeForeignKey($Model);
  48.  
  49.                 parent::beforeSave($Model);
  50.         }
  51.  
  52. /**
  53.  * add a foreign ID scope to the model settings.
  54.  *
  55.  * @param AppModel $Model Model instance
  56.  * @return true on success, false on failure
  57.  * @access protected
  58.  */
  59.         function _addScopeForeignKey(&$Model) {
  60.                 if (!isset($this->settings[$Model->alias]['scopeForeignKey'])) {
  61.                         return false;
  62.                 }
  63.  
  64.                 if (!isset($Model->data[$Model->alias][$this->settings[$Model->alias]['scopeForeignKey']])) {
  65.                         return false;
  66.                 }
  67.  
  68.                 $this->settings[$Model->alias]['scope'] = array($Model->alias . '.' . $this->settings[$Model->alias]['scopeForeignKey'] => $Model->data[$Model->alias][$this->settings[$Model->alias]['scopeForeignKey']]);
  69.  
  70.                 return true;
  71.         }
  72. }
  73. ?>