Exemplo n.º 1
0
 public function editAction()
 {
     $id = $this->_request->getParam('id');
     $mdlMenu = new Model_Menu();
     $frmMenu = new Form_Menu();
     // if this is a postback, then process the form if valid
     if ($this->getRequest()->isPost()) {
         if ($frmMenu->isValid($_POST)) {
             $menuName = $frmMenu->getValue('name');
             $mdlMenu = new Model_Menu();
             $result = $mdlMenu->updateMenu($id, $menuName);
             if ($result) {
                 // redirect to the index action
                 return $this->_forward('index');
             }
         }
     } else {
         // fetch the current menu from the db
         $currentMenu = $mdlMenu->find($id)->current();
         // populate the form
         $frmMenu->getElement('id')->setValue($currentMenu->id);
         $frmMenu->getElement('name')->setValue($currentMenu->name);
     }
     $frmMenu->setAction('/menu/edit');
     // pass the form to the view to render
     $this->view->form = $frmMenu;
 }
Exemplo n.º 2
0
 public function updateAction()
 {
     $id = $this->_request->getParam('id');
     // fetch the current item
     $mdlMenuItem = new Model_MenuItem();
     $currentMenuItem = $mdlMenuItem->find($id)->current();
     // fetch its menu
     $mdlMenu = new Model_Menu();
     $this->view->menu = $mdlMenu->find($currentMenuItem->menu_id)->current();
     // create and populate the form instance
     $frmMenuItem = new Form_MenuItem();
     $frmMenuItem->setAction('/menuitem/update');
     // process the postback
     if ($this->_request->isPost()) {
         if ($frmMenuItem->isValid($_POST)) {
             $data = $frmMenuItem->getValues();
             $mdlMenuItem->updateItem($data['id'], $data['label'], $data['page_id'], $data['link']);
             $this->_request->setParam('menu', $data['menu_id']);
             return $this->_forward('index');
         }
     } else {
         $frmMenuItem->populate($currentMenuItem->toArray());
     }
     $this->view->form = $frmMenuItem;
 }
Exemplo n.º 3
0
 public function update(array $menu, $root = null)
 {
     // No root passed, so get the tree root node
     if (is_null($root)) {
         $root = $this->data;
     }
     // Previous menu item id
     $previous = null;
     // Processed menu items
     // Declared static so recursion can set it's value
     static $processed = array();
     foreach ($menu as $item) {
         // Get id from input and find the model
         $id = \Arr::get($item, 'id');
         $model = Model_Menu::find($id);
         // If model is not found then forge it
         is_null($model) and $model = Model_Menu::forge();
         $properties = $this->prep_props($item);
         $model->set($properties);
         // If this is the first child then set it to parent
         // Otherwise set it next to the previous node
         if (is_null($previous)) {
             $model->first_child($root)->save();
         } else {
             $model->next_sibling($previous)->save();
         }
         // Is this a new menu item? Get it's id
         is_null($id) and $id = $model->id;
         // Set the previous node
         $previous = $model;
         // This menu item is processed
         $processed[] = $id;
         // If there are children then call the function again
         empty($item['children']) or $this->update($item['children'], $model);
     }
     // We are back from recusion: the current root is the tree root node
     if ($root->is_root()) {
         // Remove processed nodes from all nodes, so we can delete unused ones
         $nodes = $root->descendants()->get();
         \Arr::delete($nodes, $processed);
         foreach ($nodes as $id => $model) {
             $model->delete();
         }
     }
     return true;
 }