/**
  * 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);
 }
 /**
  * checks whether a category exists or not
  *
  * @return boolean
  * @param int $value the current category id
  */
 public function isValid($value)
 {
     // uncategorized
     if ($value == 0) {
         return true;
     }
     $categories = new application_models_categories();
     if ($categories->fetchAll($categories->select()->where("id=?", $value))->count() == 0) {
         $this->_error(application_validate_categoryid::NOT_EXISTS);
         return false;
     }
     return true;
 }
 /**
  * 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));
     }
 }
 /**
  * test max position
  * feeds has wrong positions by setUp
  */
 public function testSort()
 {
     $categoryModel = new application_models_categories();
     $this->model->sort($categoryModel->find('2')->current(), array('2', '1'));
     $this->assertEquals(1, $this->model->find('1')->count());
     $this->assertEquals(1, $this->model->find('2')->count());
     $this->assertEquals(1, $this->model->find('3')->count());
     $this->assertEquals('0', $this->model->find('2')->current()->position);
     // correct position
     $this->assertEquals('2', $this->model->find('2')->current()->category);
     // corrent category
     $this->assertEquals('1', $this->model->find('1')->current()->position);
     // correct position
     $this->assertEquals('2', $this->model->find('1')->current()->category);
     // corrent category
     $this->assertEquals('0', $this->model->find('3')->current()->position);
     // correct position
     $this->assertEquals('1', $this->model->find('3')->current()->category);
     // corrent category
 }
 /**
  * set feedlist data for the current view
  *
  * @return void
  */
 protected function feedlistData()
 {
     // check whether uncategorized exists or not (add it if not)
     $categoriesModel = new application_models_categories();
     $categoriesModel->checkUncategorized();
     // categories and feeds (target template param)
     $this->view->categories = array();
     // load unread items
     $itemCounter = Zend_Controller_Action_HelperBroker::getStaticHelper('itemcounter');
     $unreadCategory = $itemCounter->unreadItemsCategories();
     $unreadFeed = $itemCounter->unreadItemsFeeds();
     // get open categories
     if (Zend_Registry::get('session')->saveOpenCategories == 1) {
         $open = explode(',', Zend_Registry::get('session')->openCategories);
     } else {
         $open = array();
     }
     // load categories
     $categoriesDb = $categoriesModel->fetchAll($categoriesModel->select()->order('position ASC'));
     $feedPositions = Zend_Controller_Action_HelperBroker::getStaticHelper('icon')->getFeedsIconPosition();
     // read feeds and count items of the loaded categories
     $feedsModel = new application_models_feeds();
     foreach ($categoriesDb as $cat) {
         $newcat = $cat->toArray();
         // get feeds of cat
         $feedRowset = $cat->findDependentRowset('application_models_feeds', null, $feedsModel->select()->order('position ASC'));
         $newcat['feeds'] = array();
         foreach ($feedRowset as $feed) {
             $newfeed = $feed->toArray();
             $newfeed['unread'] = isset($unreadFeed[$feed->id]) ? $unreadFeed[$feed->id] : 0;
             $newfeed['iconposition'] = $feedPositions[$feed->id];
             $newcat['feeds'][] = $newfeed;
         }
         // get unread items of cat
         $newcat['unread'] = isset($unreadCategory[$cat->id]) ? $unreadCategory[$cat->id] : 0;
         // set open (feeds visible) or not
         if (in_array($cat->id, $open)) {
             $newcat['open'] = true;
         } else {
             $newcat['open'] = false;
         }
         // append category
         $this->view->categories[] = $newcat;
     }
     // count starred items
     $this->view->starred = $itemCounter->starredItems();
     // count unread items
     $this->view->unread = $unreadCategory[0];
     // count all items
     $this->view->all = $itemCounter->allItems();
     // count read items
     $this->view->read = $this->view->all - $this->view->unread;
     // count feeds
     $this->view->amountfeeds = $feedsModel->count(Zend_Registry::get('session')->currentPriorityStart, Zend_Registry::get('session')->currentPriorityEnd, Zend_Registry::get('session')->view);
 }
 /**
  * removes a feed
  *
  * @return array|bool true or error messages
  * @param int id of the feed
  */
 public function remove($id)
 {
     // get feed and category
     $feed = $this->find($id);
     if ($feed->count() == 0) {
         return Zend_Registry::get('language')->translate("feed doesn't exists");
     }
     $feed = $feed->current();
     $category = $feed->category;
     // delete all items
     $itemsModel = new application_models_items();
     $itemsModel->delete('feed=' . $feed->id);
     // delete icon
     $this->deleteIcon($feed);
     // delete messages
     $messagesModel = new application_models_messages();
     $messagesModel->delete('feed=' . $feed->id);
     // delete feed
     $this->delete('id=' . $feed->id);
     // reorder feeds in parent category
     if ($category != 0) {
         $categoryModel = new application_models_categories();
         $categoryModel->fixPositions($categoryModel->find($category)->current());
     }
     // success
     return true;
 }
 /**
  * insert given feeds
  * 
  * @return void
  * @param int $category id
  * @param array $feeds as array
  */
 protected function insertFeeds($category, $feeds)
 {
     $feedsModel = new application_models_feeds();
     // insert unkown feeds
     foreach ($feeds as $feed) {
         // search existing feed
         $moreFeeds = $feedsModel->fetchAll($feedsModel->select()->where($feedsModel->getAdapter()->quoteInto('url=?', trim($feed['link']))));
         // if feed doesn't exists
         if ($moreFeeds->count() == 0) {
             $feedsModel->add(array('name' => trim($feed['title']), 'url' => trim($feed['link']), 'category' => $category, 'priority' => 1, 'source' => 'plugins_rss_feed'));
         }
     }
     // fix positions
     $categoriesModel = new application_models_categories();
     $categoriesModel->fixPositions($categoriesModel->find($category)->current());
 }
 /**
  * set view template params for showing all available sources
  * in add/edit dialog. prepares categories
  *
  * @return void
  */
 protected function prepare()
 {
     // load available plugins
     $plugins = Zend_Controller_Action_HelperBroker::getStaticHelper('pluginloader')->getPlugins();
     // order plugins by category
     $cat = array();
     foreach ($plugins as $plugin => $obj) {
         $cat[$obj->category][$plugin] = $obj;
     }
     ksort($cat);
     $this->view->sources = $cat;
     // load categories
     $categories = new application_models_categories();
     $cats = $categories->fetchAll($categories->select()->order('position ASC'));
     $this->view->categories = array();
     foreach ($cats as $cat) {
         $this->view->categories[$cat->id] = $cat->name;
     }
 }