_role = $role; return $this; } /** * @return string * @throws Exception */ public function getRole() { if (null === $this->_role) { throw new Exception('Role is empty.'); } return $this->_role; } public function getListSelect() { if (null === $this->_select) { parent::getListSelect(); $this->_joinPerson($this->_select); $this->_jointLastActivity($this->_select); } return $this->_select; } protected function _getFromColumns() { $columns = parent::_getFromColumns(); $columns['duration'] = $this->_getDurationExpr(); return $columns; } protected function _getDurationExpr() { $tableAlias = $this->_tableShortAlias; $exprClosed = 'UNIX_TIMESTAMP(`' . $tableAlias . '`.`changed`) - UNIX_TIMESTAMP(`' . $tableAlias . '`.`added`)'; $exprOpened = 'UNIX_TIMESTAMP() - UNIX_TIMESTAMP(`' . $tableAlias . '`.`added`)'; $expr = 'IF (`' . $tableAlias . '`.`closed` = "y", (' . $exprClosed . '), (' . $exprOpened . '))'; return new Zend_Db_Expr($expr); } protected function _filter(Zend_Db_Select $select) { if (!$this->hasFilter()) { return $this; } parent::_filter($select); if (($controller = Qs_Array::get($this->_filter, 'controller'))) { $this->_joinActions($select); $select->where('`vca`.`controller` = ?', $controller); $select->group($this->_tableShortAlias . '.id'); } return $this; } protected function _joinPerson(Zend_Db_Select $select) { $role = $this->getRole(); switch ($role) { case Entity::ROLE_USER: $this->_joinUser($select); break; case Entity::ROLE_ADMIN; $this->_joinAdmin($select); break; case Entity::ROLE_STAFF; throw new Exception('Role "' . $role . '" not implemented'); break; default: throw new Exception('Unknown role "' . $role . '".'); } return $this; } protected function _joinUser(Zend_Db_Select $select) { $select->join( $this->_getPair('User', 'u'), '`u`.`id` = `' . $this->_tableShortAlias . '`.`roleId`', ['roleName' => 'CONCAT_WS(" ", `u`.`firstName`, `u`.`lastName`)'] ); return $this; } protected function _joinAdmin(Zend_Db_Select $select) { $select->join( $this->_getPair('Admin', 'a'), '`a`.`id` = `' . $this->_tableShortAlias . '`.`roleId`', ['roleName' => 'CONCAT_WS(" ", `a`.`firstName`, `a`.`lastName`)'] ); return $this; } protected function _jointLastActivity(Zend_Db_Select $select) { $select->joinLeft( $this->_getPair('ViewControllerLog', 'vcla'), '`vcla`.`id` = `' . $this->_tableShortAlias . '`.`lastActivityId`', ['lastActivity' => 'added', 'lastActivityIp' => 'ip'] ); return $this; } protected function _joinActions(Zend_Db_Select $select) { $select->join( $this->_getPair('ViewControllerLog', 'vca'), '`vca`.`sessionId` = `' . $this->_tableShortAlias . '`.`id`', [] ); return $this; } protected function _prepareRow(array &$row) { $row['durationString'] = Qs_Time::durationToString($row['duration']); return parent::_prepareRow($row); } public function getRoleNameById($role, $roleId) { switch ($role) { case Entity::ROLE_USER: return $this->getUserNameById($roleId); break; case Entity::ROLE_ADMIN: return $this->getAdminNameById($roleId); break; case Entity::ROLE_STAFF: throw new Exception('Role "' . $role . '" not implemented'); break; default: throw new Exception('Unknown role "' . $role . '".'); } } public function getUserNameById($userId) { return $this->_getPersonNameById($userId, 'User'); } public function getAdminNameById($adminId) { return $this->_getPersonNameById($adminId, 'Admin'); } protected function _getPersonNameById($personId, $tableAlias) { if (!$personId || !$tableAlias) { return false; } $select = $this->_db->select(); $select->from($this->_getPair($tableAlias, 'p'), ['CONCAT_WS(" ", `p`.`firstName`, `p`.`lastName`)']); $select->where('`p`.`id` = ?', $personId, Qs_Db::INT_TYPE); return $this->_db->fetchOne($select); } public function getAutocomplete($term) { $role = $this->getRole(); switch ($role) { case Entity::ROLE_USER: $data = $this->_getAutocompletePerson($term, 'User'); break; case Entity::ROLE_ADMIN; $data = $this->_getAutocompletePerson($term, 'Admin'); break; case Entity::ROLE_STAFF; throw new Exception('Role "' . $role . '" not implemented'); break; default: throw new Exception('Unknown role "' . $role . '".'); } return $data; } protected function _getAutocompletePerson($term, $tableAlias) { $select = $this->_db->select(); $select->from( $this->_getPair($tableAlias, 'p'), [ 'value' => 'id', 'title' => 'CONCAT_WS(" ", `p`.`firstName`, `p`.`lastName`)' ] ); if ('User' == $tableAlias) { $select->where('`p`.`bought` = "y"'); } $select->where('`p`.`firstName` LIKE ? OR `p`.`lastName` LIKE ?', '%' . $term . '%'); $select->limit(Entity::AUTOCOMPLETE_LIMIT); return $this->_db->fetchAll($select); } public function getObjectInfo() { $info = parent::getObjectInfo(); $info['itemsName'] = Entity::$roleTitles[$this->getRole()] . ' ' . $this->getConfig('itemsName'); return $info; } }