public function listAction() { // handle data change requests if ($this->_getParam('data') && $this->_getParam('xaction')) { $refresh = false; $data = Zend_Json::decode($this->_getParam('data')); switch ($this->_getParam('xaction')) { case 'update': $question = Poll_Question::getById($data['id']); unset($data['id']); $question->setValues($data); $question->save(); if (isset($data['isActive'])) { $refresh = true; } break; case 'destroy': $question = Poll_Question::getById($data); $question->delete(); break; } $this->_helper->json(array('success' => true, 'refresh' => $refresh)); } $list = new Poll_Question_List(); $list->setOffset($this->_getParam("start")); $list->setLimit($this->_getParam("limit")); $list->setOrderKey("id"); $list->setOrder("DESC"); if ($this->_getParam("filter")) { $list->setCondition("`title` LIKE ?", array("%{$this->_getParam("filter")}%")); } $list->load(); $questions = array(); foreach ($list as $question) { // @todo - optimization - single query via IN() $question->answers = iterator_to_array($question->getAnswers()); $question->current = $question->isCurrent(); $question->responses = $question->sumResponses(); $questions[] = $question; } $this->_helper->json(array('success' => true, 'total' => $list->getTotalCount(), 'data' => $questions)); }
/** * @return Poll_Question * @todo cache with expiration == endDate */ public static function getCurrent() { if (null === self::$_current) { $now = date('Y-m-d H:i:s'); $list = new Poll_Question_List(); $list->setCondition('isActive = 1 AND (' . '(startDate <= ? AND endDate >= ?) OR ' . '(startDate IS NULL AND endDate >= ?) OR ' . '(startDate <= ? AND endDate IS NULL) OR ' . '(startDate IS NULL AND endDate IS NULL)' . ')', array($now, $now, $now, $now)); $list->setLimit(1); $list->setOrderKey('id'); $list->setOrder('ASC'); self::$_current = $list->current(); } return self::$_current; }