Example #1
0
 /**
  * If the menu is deleted, the menu items should also be deleted. As its a
  * mptt tree deleting the root node will cause cake to delete everything
  * within the tree and the whole thing will be gone
  * 
  * @access public
  * 
  * @return mixed what ever the parent returns
  */
 public function afterDelete()
 {
     $menuItem = $this->MenuItem->find('first', array('conditions' => array('menu_id' => $this->id, 'parent_id' => null)));
     if (!empty($menuItem['MenuItem']['id'])) {
         $this->MenuItem->Behaviors->disable('Trashable');
         $this->MenuItem->delete($menuItem['MenuItem']['id']);
         $this->MenuItem->Behaviors->enable('Trashable');
     }
     return parent::afterDelete();
 }
Example #2
0
 /**
  * Set the parent_id for the menu item before saving so that the menu will
  * be within the correct menu item group. This only applies to the root
  * level menu items and not the sub items.
  *
  * @param bool $cascade not used
  * @access public
  *
  * @return the parent method
  */
 public function beforeValidate($options = array())
 {
     $foreignKey = $this->belongsTo[$this->Menu->alias]['foreignKey'];
     if (!empty($this->data[$this->alias][$foreignKey]) && empty($this->data[$this->alias]['parent_id'])) {
         $menuItem = $this->find('first', array('fields' => array($this->alias . '.' . $this->primaryKey), 'conditions' => array($this->alias . '.parent_id' => null, $this->alias . '.' . $foreignKey => $this->data[$this->alias][$foreignKey])));
         if (empty($menuItem[$this->alias]['id'])) {
             return false;
         }
         $this->data[$this->alias]['parent_id'] = $menuItem[$this->alias]['id'];
     }
     return parent::beforeValidate($options);
 }