/** * Fetch the amount of comments for the items related to the $alias with keys * * @param mixed $key * @param string $alias * @param array $items * @param string $fieldName * @return integer * @throws Zend_Controller_Action_Exception */ public function commentsCounter($key, $alias, $items, $fieldName = 'id') { $aliasManager = new Comments_Model_CommentAlias_Manager(); $aliasRow = $aliasManager->getByAlias($alias); // check if alias registered if (!Zend_Registry::isRegistered($alias . '.' . $fieldName)) { $userId = $this->view->user() ? $this->view->user()->id : 0; $commentsManager = new Comments_Model_Comment_Manager(); $values = array(); // collect items foreach ($items as $item) { $value = (int) $item[$fieldName]; if ($value > 0) { array_push($values, $value); } } // fetch gropped comments amount $commentsAmount = $commentsManager->getGrouppedCommentsAmount($aliasRow, $fieldName, $values, $userId); Zend_Registry::set($alias . '.' . $fieldName, $commentsAmount); } // fetch the data from registry $commentsAmount = Zend_Registry::get($alias . '.' . $fieldName); if (isset($commentsAmount[$key])) { return $commentsAmount[$key]; } else { return 0; } }
/** * Load the comments by the unique alias. * * Options: * - template * - key * * @param string $aliasKey * @param array $options * @return string * @throws Zend_Controller_Action_Exception */ public function getComments($aliasKey, $options = array()) { $user = Zend_Auth::getInstance()->getIdentity(); $request = Zend_Controller_Front::getInstance()->getRequest(); $aliasManager = new Comments_Model_CommentAlias_Manager(); $manager = new Comments_Model_Comment_Manager(); $alias = $aliasManager->getByAlias($aliasKey); $page = $request->getParam('page'); $userId = $user ? $user->id : 0; $this->_checkOptions($options); // throws Exception when aliasKey is missed or wrong if (!$aliasKey || !$alias) { throw new Zend_Controller_Action_Exception('Page not found'); } // throw Exception when key required and missed if (!$this->_key && $alias->isKeyRequired()) { throw new Zend_Controller_Action_Exception('Missed key parameter'); } $paginator = Zend_Paginator::factory($manager->getSelect($alias, $userId, $this->_key)); // set paginator options if ($alias->isPaginatorEnabled()) { $paginator->setItemCountPerPage($alias->countPerPage); $paginator->setCurrentPageNumber($page); } else { // Is there a way to disable pagination? $paginator->setItemCountPerPage(9999); } // init form $form = new Comments_Model_Comment_Form_Create(); $form->setAlias($aliasKey); $form->setKey($this->_key); $form->setReturnUrl($this->view->url()); if (!$alias->isTitleDisplayed()) { $form->removeTitleElement(); } return $this->view->partial('list.phtml', 'comments', array('paginator' => $paginator, 'form' => $form, 'user' => $user, 'template' => $this->_template)); }