Example #1
0
 /** 
  * Creates a new item or saves the modified one.
  */
 public function save_item()
 {
     $this->check_authorization();
     $item = new Item();
     $item->id = $this->get_arg('id', 0);
     $item->name = $this->get_arg('name', '');
     $item->category_id = $this->get_arg('category_id', 0);
     $params = $this->get_arg('params', array());
     $category = new Category($item->category_id);
     $item->params = $category->get_params();
     $data['breadcrumbs'] = $this->generate_breadcrumbs($item->category_id, array('/show_item?id=' . $item->id => $item->name));
     foreach ($item->params as &$param) {
         if (isset($params[$param->id])) {
             $param->value = $params[$param->id];
         }
     }
     try {
         $item->save();
         return new Redirect('/show_item?id=' . $item->id);
     } catch (Exception $e) {
         $data['error'] = $e->getMessage();
         $data['item'] = $item;
         return new View('item_form', $data);
     }
 }