/** * 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; } }
/** * Initialize * * @return void */ public function init() { parent::init(); $aliasManager = new Comments_Model_CommentAlias_Manager(); // get the Alias by the requested param $this->_alias = $aliasManager->getDbTable()->find($this->getRequest()->getParam('alias'))->current(); }
public function indexAction() { $aliasManager = new Comments_Model_CommentAlias_Manager(); $commentsTable = new Comments_Model_Comment_Table(); $alias = $aliasManager->getByAlias($this->getRequest()->getParam('alias')); $key = $this->getRequest()->getParam('key'); if (!$alias) { throw new Zend_Controller_Action_Exception('Page not found'); } // user should have an identity if (Zend_Auth::getInstance()->hasIdentity()) { $form = new Comments_Model_Comment_Form_Create(); // check alias item options // check that required status for key if ($alias->isKeyRequired()) { $form->setKey($key); } // check need and visibility of title if (!$alias->isTitleDisplayed()) { $form->removeTitleElement(); } // validate form by the POST request pararm if ($this->getRequest()->isPost() && $form->isValid($this->getRequest()->getParams())) { // creating new comments row $row = $commentsTable->createRow($form->getValues()); $row->aliasId = $alias->id; $row->status = Comments_Model_Comment::STATUS_ACTIVE; // change status to "review" when pre-moderation required if ($alias->isPreModerationRequired()) { $row->status = Comments_Model_Comment::STATUS_REVIEW; } $row->save(); // display the flash message according to the comment status switch ($row->status) { case Comments_Model_Comment::STATUS_REVIEW: $this->_helper->flashMessenger->addMessage('Comment added successfully and awaiting pre-moderation.'); break; default: $this->_helper->flashMessenger->addMessage('Comment added successfully'); break; } // redirect to the URL that was setted to the form element $this->_redirect($form->getValue('returnUrl')); } // set the view variables $this->view->user = $this->view->user(); $this->view->form = $form; } }
/** * Decrement comments amount * * @param integer $count * @throws Zend_Db_Exception * @return Comments_Model_CommentAlias */ protected function decComments($count = 1) { $aliasManager = new Comments_Model_CommentAlias_Manager(); $alias = $aliasManager->getDbTable()->find($this->aliasId)->current(); if ($alias->isKeyRequired() && $alias->isRelatedTableDefined()) { $table = new Zend_Db_Table($alias->relatedTable); $row = $table->find($this->key)->current(); if ($row) { $row->comments -= $count; $row->save(); } else { throw new Zend_Db_Exception('Row not found'); } } return $this; }
/** * 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)); }