Example #1
0
 /**
  * Edit category information
  * 
  * @return ViewModel
  */
 public function editAction()
 {
     $module = $this->getModule();
     $configs = Pi::config('', $module);
     $configs['max_media_size'] = Pi::service('file')->transformSize($configs['max_media_size']);
     $form = $this->getCategoryForm('edit');
     $this->view()->assign(array('title' => _a('Edit Category Info'), 'configs' => $configs));
     if ($this->request->isPost()) {
         $post = $this->request->getPost();
         $form->setData($post);
         $options = array('id' => $post['id']);
         $form->setInputFilter(new CategoryEditFilter($options));
         $form->setValidationGroup(Category::getAvailableFields());
         if (!$form->isValid()) {
             $this->renderForm($form, _a('Can not update data!'));
             return;
         }
         $data = $form->getData();
         $id = $this->saveCategory($data);
         if (empty($id)) {
             return;
         }
         // Clear cache
         Pi::registry('category', $module)->clear($module);
         return $this->redirect()->toRoute('', array('action' => 'list'));
     }
     $id = $this->params('id', 0);
     if (empty($id)) {
         $this->jumpto404(_a('Invalid category ID!'));
     }
     $model = $this->getModel('category');
     $row = $model->find($id);
     if (!$row->id) {
         return $this->jumpTo404(_a('Can not find category!'));
     }
     $form->setData($row->toArray());
     $parent = $model->getParentNode($row->id);
     if ($parent) {
         $form->get('parent')->setAttribute('value', $parent['id']);
     }
     $this->view()->assign('form', $form);
 }