Example #1
0
 /**
  * One-purpose method for switching statuses
  *
  * @param AppModel $Model
  * @param string $status
  * @param string $id
  * @return boolean
  */
 public function setStatus($Model, $status, $id = null)
 {
     if ($id) {
         $Model->id = $id;
     }
     return $Model->saveField('status', $status, true);
 }
Example #2
0
 /**
  * Remove the current node from the tree, and reparent all children up one level.
  *
  * If the parameter delete is false, the node will become a new top level node. Otherwise the node will be deleted
  * after the children are reparented.
  *
  * @param AppModel $Model Model instance
  * @param mixed $id The ID of the record to remove
  * @param boolean $delete whether to delete the node after reparenting children (if any)
  * @return boolean true on success, false on failure
  * @access public
  * @link http://book.cakephp.org/view/1354/removeFromTree
  */
 function removefromtree(&$Model, $id = null, $delete = false)
 {
     if (is_array($id)) {
         extract(array_merge(array('id' => null), $id));
     }
     extract($this->settings[$Model->alias]);
     list($node) = array_values($Model->find('first', array('conditions' => array($scope, $Model->escapeField() => $id), 'fields' => array($Model->primaryKey, $left, $right, $parent), 'recursive' => $recursive)));
     if ($node[$right] == $node[$left] + 1) {
         if ($delete) {
             return $Model->delete($id);
         } else {
             $Model->id = $id;
             return $Model->saveField($parent, null);
         }
     } elseif ($node[$parent]) {
         list($parentNode) = array_values($Model->find('first', array('conditions' => array($scope, $Model->escapeField() => $node[$parent]), 'fields' => array($Model->primaryKey, $left, $right), 'recursive' => $recursive)));
     } else {
         $parentNode[$right] = $node[$right] + 1;
     }
     $db =& ConnectionManager::getDataSource($Model->useDbConfig);
     $Model->updateAll(array($parent => $db->value($node[$parent], $parent)), array($parent => $node[$Model->primaryKey]));
     $this->__sync($Model, 1, '-', 'BETWEEN ' . ($node[$left] + 1) . ' AND ' . ($node[$right] - 1));
     $this->__sync($Model, 2, '-', '> ' . $node[$right]);
     $Model->id = $id;
     if ($delete) {
         $Model->updateAll(array($Model->escapeField($left) => 0, $Model->escapeField($right) => 0, $Model->escapeField($parent) => null), array($Model->escapeField() => $id));
         return $Model->delete($id);
     } else {
         $edge = $this->__getMax($Model, $scope, $right, $recursive);
         if ($node[$right] == $edge) {
             $edge = $edge - 2;
         }
         $Model->id = $id;
         return $Model->save(array($left => $edge + 1, $right => $edge + 2, $parent => null), array('callbacks' => false));
     }
 }
Example #3
0
 /**
  * Remove the current node from the tree, and reparent all children up one level.
  *
  * If the parameter delete is false, the node will become a new top level node. Otherwise the node will be deleted
  * after the children are reparented.
  *
  * @param AppModel $Model Model instance
  * @param mixed $id The ID of the record to remove
  * @param boolean $delete whether to delete the node after reparenting children (if any)
  * @return boolean true on success, false on failure
  */
 function removefromtree(&$Model, $id = null, $delete = false)
 {
     if (is_array($id)) {
         extract(array_merge(array('id' => null), $id));
     }
     extract($this->settings[$Model->alias]);
     list($node) = array_values($Model->find('first', array('conditions' => array($scope, $Model->escapeField() => $id), 'fields' => array($Model->primaryKey, $left, $right, $parent), 'recursive' => $recursive)));
     if ($node[$right] == $node[$left] + 1) {
         if ($delete) {
             return $Model->delete($id);
         } else {
             $Model->id = $id;
             return $Model->saveField($parent, null);
         }
     } elseif ($node[$parent]) {
         list($parentNode) = array_values($Model->find('first', array('conditions' => array($scope, $Model->escapeField() => $node[$parent]), 'fields' => array($Model->primaryKey, $left, $right), 'recursive' => $recursive)));
     } else {
         $parentNode[$right] = $node[$right] + 1;
     }
     $db =& ConnectionManager::getDataSource($Model->useDbConfig);
     $Model->updateAll(array($parent => $db->value($node[$parent], $parent)), array($parent => $node[$Model->primaryKey]));
     $Model->id = $id;
     $this->__sync($Model, 1, '-', 'BETWEEN ' . ($node[$left] + 1) . ' AND ' . ($node[$right] - 1));
     $this->__sync($Model, 2, '-', '> ' . $node[$right]);
     if ($delete) {
         $sub_tree = $Model->find('list', array('conditions' => array($left . ' >= ' . $node[$left], $right . ' <= ' . $node[$right]), 'fields' => array('id', 'parent_id')));
         foreach ($sub_tree as $sChild => $sParent) {
             $Model->del($sChild);
         }
         return true;
     } else {
         /*
          * this could feasibly create a lot of new trees.. worst case
          * performance slightly worse than original
          */
         $edge = $this->__getPartition($Model, $scope, $recursive);
         if ($node[$right] == $edge) {
             $edge = $edge - 2;
         }
         $Model->id = $id;
         return $Model->save(array($left => $edge, $right => $edge + 1, $parent => null), array('callbacks' => false));
     }
 }
Example #4
0
 /**
  * Backward compatible method
  *
  * Returns true if the change is successful.
  *
  * @param AppModel $model
  * @param mixed $parentId The ID to set as the parent of the current node.
  * @return true on success
  * @access public
  */
 function setparent(&$model, $parentId = null, $created = null)
 {
     extract($this->settings[$model->alias]);
     if ($created === false && $parentId == $model->field($parent)) {
         return true;
     }
     return $model->saveField($parent, $parentId);
 }
Example #5
0
 /**
  * Backward compatible method
  *
  * Returns true if the change is successful.
  *
  * @param AppModel $model
  * @param mixed $parentId The ID to set as the parent of the current node.
  * @return true on success
  * @access public
  * @deprecated
  */
 function setparent(&$model, $parentId = null, $created = null)
 {
     trigger_error(__('(TreeBehavior::setParent) Deprecated - save the record with a parent ID instead', true), E_USER_ERROR);
     extract($this->settings[$model->alias]);
     if ($created === false && $parentId == $model->field($parent)) {
         return true;
     }
     return $model->saveField($parent, $parentId, array('callbacks' => false));
 }
Example #6
0
 /**
  * Move up element
  *
  * @param AppModel $Model Model instance
  * @param mixed $id The ID of the record to move
  * @param mixed $number how many places to move the node, or true to move to first position
  * @return boolean true on success, false on failure
  */
 public function moveup(&$Model, $id = null, $number = 1)
 {
     if (is_array($id)) {
         extract(array_merge(array('id' => null), $id));
     }
     if (!$number) {
         return false;
     }
     if (empty($id)) {
         $id = $Model->id;
     }
     $data = $Model->read(am($this->settings[$Model->alias]['order_field'], $this->settings[$Model->alias]['group_fields']), $id);
     $order = $data[$Model->alias][$this->settings[$Model->alias]['order_field']];
     if (is_int($number)) {
         $order -= abs($number);
     } elseif ($number === true) {
         $order = $this->settings[$Model->alias]['start_at'];
     } else {
         return false;
     }
     if ($order < $this->settings[$Model->alias]['start_at']) {
         $order = $this->settings[$Model->alias]['start_at'];
     }
     $Model->id = $id;
     return $Model->saveField($this->settings[$Model->alias]['order_field'], $order);
 }