function &getItem($id = null) { if (!isset($this->_item)) { if ($this->_item === null) { if (empty($id)) { $id = $this->getState('improvemycity.id'); } $db = $this->getDbo(); $query = $db->getQuery(true); $query->select('a.*'); $query->from('#__improvemycity as a'); $query->where('a.id = ' . (int) $id); // Join on user table. $query->select('u.name AS fullname'); $query->join('LEFT', '#__users AS u on u.id = a.userid'); // Join on catid table. $query->select('c.title AS catname'); $query->join('LEFT', '#__categories AS c on c.id = a.catid'); $db->setQuery((string) $query); if (!$db->query()) { JError::raiseError(500, $db->getErrorMsg()); } $this->_item = $db->loadObject(); } } if ($this->_item != null) { //also get the discussion for that record as well $model_discussions = JModel::getInstance('Discussions', 'ImprovemycityModel'); $this->_item->discussion = $model_discussions->getItems($this->_item->id); $this->_item->reported_rel = ImprovemycityHelper::getRelativeTime($this->_item->reported); $this->_item->acknowledged_rel = ImprovemycityHelper::getRelativeTime($this->_item->acknowledged); $this->_item->closed_rel = ImprovemycityHelper::getRelativeTime($this->_item->closed); } return $this->_item; }
function getItems() { // Invoke the parent getItems method to get the main list $items = parent::getItems(); //$this->_total = count($items); //I need the discussions model to get discussions for every item... //so I get model (discussions) from within another model (issues) ... //JModel::addIncludePath(JPATH_SITE.'/components/com_improvemycity/models', 'Discussions'); //don't need this we are already inside a model ;) // Convert the params field into an object, saving original in _params $model_discussions = JModel::getInstance('Discussions', 'ImprovemycityModel'); for ($i = 0, $n = count($items); $i < $n; $i++) { $item =& $items[$i]; //calculate relative dates here $item->reported_rel = ImprovemycityHelper::getRelativeTime($item->reported); $item->acknowledged_rel = ImprovemycityHelper::getRelativeTime($item->acknowledged); $item->closed_rel = ImprovemycityHelper::getRelativeTime($item->closed); //TODO: Important: Get this outside for loop and set it to main query. It causes lots of queries (overhead) $item->discussion = $model_discussions->getItems($item->id); if (!isset($this->_params)) { $params = new JRegistry(); $params->loadJSON($item->params); $item->params = $params; } } return $items; }
public function comment($pk = 0, $userid = 0, $description = '') { $pk = !empty($pk) ? $pk : (int) ($id = $this->getState('improvemycity.id')); $db = $this->getDbo(); $db->setQuery('INSERT INTO #__improvemycity_comments ( improvemycityid, userid, description)' . ' VALUES ( ' . (int) $pk . ', ' . (int) $userid . ', "' . $description . '")'); if (!$db->query()) { $this->setError($db->getErrorMsg()); return false; } //return the latest comment so as to be displayed with ajax in the frontend $query = $db->getQuery(true); $query->select('a.*'); $query->from('#__improvemycity_comments as a'); $query->where('a.improvemycityid = ' . (int) $pk); $query->where('a.state = 1'); // Join on user table. $query->select('u.name AS username'); $query->join('LEFT', '#__users AS u on u.id = a.userid'); $query->order('created DESC'); $db->setQuery((string) $query); if (!$db->query()) { $this->setError($db->getErrorMsg()); return false; } //$comments = $db->loadResult(); //return first field of first row //$comments = $db->loadAssocList(); //return all rows $comments = $db->loadAssoc(); //return first row $comments['textual_descr'] = JText::_('COMMENT_REPORTED') . ' ' . ImprovemycityHelper::getRelativeTime($comments['created']) . ' ' . JText::_('BY') . ' ' . $comments['username']; return $comments; }