示例#1
0
 protected function _save(\Menus $menu, &$error)
 {
     if ($menu->isNew() && ($parent_id = $this->request()->get('parent_id', 'INT', 0))) {
     } else {
         $parent_id = $this->request()->post('parent_id', 'INT', 0);
     }
     if ($parent_id) {
         $parent = \Menus::retrieveById($parent_id);
     } else {
         $parent = \Menus::retrieveRoot();
     }
     if (!$parent) {
         $error['parent'] = t('Not found menu parent with id:' . $parent_id);
         return false;
     }
     $isNew = $menu->isNew();
     $menu->hydrate($this->request()->post('menus', 'ARRAY', array()));
     if ($menu->type == \Menus::INTERNAL) {
         if (!$menu->route) {
             $error['menus.route'] = t("Menu's route is required.");
         }
     } else {
         if ($menu->type == \Menus::EXTERNAL) {
             if (!$menu->link || !filter_var($menu->link, FILTER_VALIDATE_URL)) {
                 $error['menus.link'] = t("Menu's link is invalid.");
             }
         }
     }
     if (!empty($error)) {
         return false;
     }
     if ($isNew) {
         $menu->insertAsLastChildOf($parent);
     } else {
         if ($parent->getId() == $menu->getId()) {
             $error['parent'] = t('Could not make child of itself');
             return false;
         }
         $currentParent = $menu->getParent();
         if ($currentParent->id != $parent->id) {
             //change parent
             $menu->moveToLastChildOf($parent);
         } else {
             //save normal
             if ($menu->save()) {
                 // only save information
                 return true;
             } else {
                 if (!$menu->isValid()) {
                     $failures = $menu->getValidationFailures();
                     foreach ($failures as $failure) {
                         if (!isset($error[$failure->getColumn()])) {
                             $error[$failure->getColumn()] = array();
                         }
                         $error[$failure->getColumn()][] = $failure->getMessage();
                     }
                     return empty($error);
                 }
                 return false;
             }
         }
         //end save normal
     }
     if (!$menu->isValid()) {
         $failures = $menu->getValidationFailures();
         foreach ($failures as $failure) {
             $error[$failure->getColumn()] = $failure->getMessage();
         }
     }
     return empty($error);
 }