Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Scoped Tree behavior - Created by Arno Esterhuizen from code by Jamie Nay to add better scope support.
- *
- * Scoped Tree behavior class.
- *
- * Enables a model object to act as a node-based tree, with subtrees allowed.
- */
- App::import('Behavior', 'Tree');
- class ScopedTreeBehavior extends TreeBehavior {
- /**
- * Initiate Tree behavior
- *
- * @param object $Model instance of model
- * @param array $config array of configuration settings.
- * @return void
- * @access public
- */
- function setup(&$Model, $config = array()) {
- parent::setup($Model, $config = array());
- if (!is_array($config)) {
- $config = array('type' => $config);
- }
- $settings = array_merge($this->_defaults, $config);
- if (in_array($settings['scope'], $Model->getAssociated('belongsTo'))) {
- $data = $Model->getAssociated($settings['scope']);
- $this->settings[$Model->alias]['scopeForeignKey'] = $data['foreignKey'];
- }
- }
- /**
- * Before save method. Called before all saves
- *
- * Overriden to transparently manage setting the lft and rght fields if and only if the parent field is included in the
- * parameters to be saved. For newly created nodes with NO parent the left and right field values are set directly by
- * this method bypassing the setParent logic.
- *
- * @since 1.2
- * @param AppModel $Model Model instance
- * @return boolean true to continue, false to abort the save
- * @access public
- */
- function beforeSave(&$Model) {
- // Correct the scope of the behavior settings if required.
- $this->_addScopeForeignKey($Model);
- parent::beforeSave($Model);
- }
- /**
- * add a foreign ID scope to the model settings.
- *
- * @param AppModel $Model Model instance
- * @return true on success, false on failure
- * @access protected
- */
- function _addScopeForeignKey(&$Model) {
- if (!isset($this->settings[$Model->alias]['scopeForeignKey'])) {
- return false;
- }
- if (!isset($Model->data[$Model->alias][$this->settings[$Model->alias]['scopeForeignKey']])) {
- return false;
- }
- $this->settings[$Model->alias]['scope'] = array($Model->alias . '.' . $this->settings[$Model->alias]['scopeForeignKey'] => $Model->data[$Model->alias][$this->settings[$Model->alias]['scopeForeignKey']]);
- return true;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement