_user, $field, $default); } public function setUser($user) { $this->_user = $user; return $this; } public function findUser($userId) { $select = $this->_db->select(); $select->from($this->_getPair('User'), [ 'id', 'fullName' => Model::getFullNameDbExpr(), 'email', 'alias', 'pmNotificationEnabled', ]); $select->where('id = ?', $userId, Qs_Db::INT_TYPE); $select->limit(1); return $this->_db->fetchRow($select); } public function getSummary() { $list = []; foreach ($this->getConfig('labels') as $id => $title) { $row = [ 'id' => $id, 'title' => $title, 'count' => $this->getMessagesCount($id), 'lastMessage' => $this->getLastMessage($id), ]; $list[] = $row; } return $list; } protected function getMessagesCount($labelId) { $select = $this->_db->select(); $select->from($this->_getPair(), 'COUNT(*)'); switch ($labelId) { case Entity::LABEL_INBOX: $select->where('`toUserId` = ?', $this->getUser('id'), Qs_Db::INT_TYPE); $select->where('`read` = ?', 'n'); break; case Entity::LABEL_SENT: $select->where('`fromUserId` = ?', $this->getUser('id'), Qs_Db::INT_TYPE); break; default: throw new Exception('Unsupported Mailbox'); } return $this->_db->fetchOne($select); } protected function getLastMessage($labelId) { $select = $this->_db->select(); $select->from($this->_getPair(), ['id', 'subject', 'added']); switch ($labelId) { case Entity::LABEL_INBOX: $this->joinSender($select); $select->where('`toUserId` = ?', $this->getUser('id'), Qs_Db::INT_TYPE); break; case Entity::LABEL_SENT: $this->joinReceiver($select); $select->where('`fromUserId` = ?', $this->getUser('id'), Qs_Db::INT_TYPE); break; default: throw new Exception('Unsupported Mailbox'); } $select->order('added DESC'); $select->limit(1); return $this->_db->fetchRow($select); } protected function joinReceiver(Zend_Db_Select $select) { $select->join($this->_getPair('User', 'ur'), "`ur`.`id` = `uwm`.`toUserId`", [ 'receiverFullName' => Model::getFullNameDbExpr('ur'), 'receiverAlias' => 'alias', ]); return $this; } protected function joinSender(Zend_Db_Select $select) { $select->join($this->_getPair('User', 'us'), "`us`.`id` = `uwm`.`fromUserId`", [ 'senderFullName' => Model::getFullNameDbExpr('us'), 'senderAlias' => 'alias', ]); return $this; } public function getListSelect() { if (null !== $this->_select) { return $this->_select; } $select = parent::getListSelect(); $this->joinSender($select); $this->joinReceiver($select); return $select; } }