/**
  * test set categories
  */
 public function testSetCategories()
 {
     $newdata = array(array('id' => 1, 'name' => 'cat 1', 'position' => 1), array('id' => 2, 'name' => 'category 2', 'position' => 0), array('id' => 0, 'name' => 'new cat', 'position' => 3));
     // set new without delete an filled category
     $this->model->setCategories($newdata);
     $this->assertEquals($newdata[0], $this->model->find(1)->current()->toArray());
     // update
     $this->assertEquals($newdata[1], $this->model->find(2)->current()->toArray());
     // update
     $this->assertEquals(1, $this->model->fetchAll($this->model->select()->where('name=?', 'new cat'))->count());
     $this->assertEquals(3, $this->model->fetchAll()->count());
     // old deleted
     // set with delete filled category
     $newdata = array(array('id' => 0, 'name' => 'newcat', 'position' => 1));
     $this->model->setCategories($newdata);
     $this->assertEquals(1, $this->model->fetchAll()->count());
     $this->assertEquals(0, $this->model->feeds(1));
     // no more feeds in cat 1
     $this->assertEquals(2, $this->model->feeds(-1));
     // now all feeds in cat -1 (uncategorized)
 }
 /**
  * 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;
 }
 /**
  * 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);
 }
 /**
  * Show edit dialog
  *
  * @return void
  */
 public function indexAction()
 {
     // load categories
     $categories = new application_models_categories();
     $this->view->categories = $categories->fetchAll($categories->select()->order('position ASC'));
 }
 /**
  * check whether a category exists and returns the id
  * else insert new category
  *
  * @return int category id
  * @param string $category the category name
  */
 protected function insertCategory($category)
 {
     // no category handling for uncategorized
     if ($category == -1) {
         return -1;
     }
     // search existing category
     $categoriesModel = new application_models_categories();
     $categories = $categoriesModel->fetchAll($categoriesModel->select()->where($categoriesModel->getAdapter()->quoteInto('name=?', trim($category))));
     // use existing category
     if ($categories->count() > 0) {
         return $categories->current()->id;
     } else {
         return $categoriesModel->insert(array('name' => trim($category), 'position' => $categoriesModel->maxPosition() + 1));
     }
 }
 /**
  * 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;
     }
 }