/** * test validation */ public function testValidate() { $newdata = array(array('id' => 44, 'name' => 'cat 1', 'position' => 1), array('id' => 2, 'name' => ' ', 'position' => 0), array('id' => 0, 'name' => 'new cat', 'position' => 'aa'), array('id' => 'abc', 'name' => 'cat', 'position' => 'aa'), array('id' => 'abc', 'position' => 'aa'), array('id' => 'abc', 'name' => null, 'position' => 'aa'), array('id' => null, 'name' => 'bbb', 'position' => 'aa'), array('id' => 0, 'name' => 'bbb', 'position' => null)); foreach ($this->model->setCategories($newdata) as $errors) { $this->assertGreaterThan(0, count($errors)); } }
/** * saves the given categories * * @return void */ public function saveAction() { // get new categories $newcategories = $this->getRequest()->getParam('categories'); // validate parameter if (!is_array($newcategories)) { $this->_helper->json(array('error' => Zend_Registry::get('language')->translate('no data given'))); } // prepare categories for insertion and update $newcategories = array_chunk($newcategories, 2); $categorieList = array(); $position = 0; foreach ($newcategories as $cat) { $newCat = array('name' => $cat[1], 'position' => $position++); // parse id (z.B. cat_3 => 3) if (strlen(trim($cat[0])) >= 4) { $newCat['id'] = substr(trim($cat[0]), 4); } $categorieList[] = $newCat; } // insert and update $categories = new application_models_categories(); $result = $categories->setCategories($categorieList); $this->_helper->json($result); }