示例#1
0
 /**
  * CRUD controller: UPDATE
  */
 public function action_update()
 {
     $this->template->title = __('Update') . ' ' . __($this->_orm_model) . ' ' . $this->request->param('id');
     $this->template->styles = array('css/sortable.css' => 'screen');
     $this->template->scripts['footer'][] = 'js/oc-panel/category_edit.js';
     $form = new FormOrm($this->_orm_model, $this->request->param('id'));
     $category = new Model_Category($this->request->param('id'));
     $fields = Model_Field::get_all();
     $category_fields = array();
     $selectable_fields = array();
     // get selectable fields
     foreach ($fields as $field => $values) {
         if (!(is_array($values['categories']) and in_array($category->id_category, $values['categories']))) {
             $selectable_fields[$field] = $values;
         } else {
             $category_fields[$field] = $values;
         }
     }
     if ($this->request->post()) {
         if ($success = $form->submit()) {
             //category is different than himself, cant be his own father!!!
             if ($form->object->id_category == $form->object->id_category_parent) {
                 Alert::set(Alert::INFO, __('You can not set as parent the same category'));
                 $this->redirect(Route::get($this->_route_name)->uri(array('controller' => Request::current()->controller(), 'action' => 'update', 'id' => $form->object->id_category)));
             }
             //check if the parent is loaded/exists avoiding errors
             $parent_cat = new Model_Category($form->object->id_category_parent);
             if (!$parent_cat->loaded()) {
                 Alert::set(Alert::INFO, __('You are assigning a parent category that does not exist'));
                 $this->redirect(Route::get($this->_route_name)->uri(array('controller' => Request::current()->controller(), 'action' => 'update', 'id' => $form->object->id_category)));
             }
             $form->object->description = Kohana::$_POST_ORIG['formorm']['description'];
             try {
                 $form->object->save();
             } catch (Exception $e) {
                 throw HTTP_Exception::factory(500, $e->getMessage());
             }
             $form->object->parent_deep = $form->object->get_deep();
             try {
                 $form->object->save();
             } catch (Exception $e) {
                 throw HTTP_Exception::factory(500, $e->getMessage());
             }
             $this->action_deep();
             //rename icon name
             if ($category->has_image and $category->seoname != $form->object->seoname) {
                 $category->rename_icon($form->object->seoname);
             }
             Core::delete_cache();
             Alert::set(Alert::SUCCESS, __('Item updated'));
             $this->redirect(Route::get($this->_route_name)->uri(array('controller' => Request::current()->controller())));
         } else {
             Alert::set(Alert::ERROR, __('Check form for errors'));
         }
     }
     return $this->render('oc-panel/pages/categories/update', compact('form', 'category', 'category_fields', 'selectable_fields'));
 }