* @license PHP License * @package WB * @subpackage content */ /** * Load classes */ WBClass::load('WBContent'); /** * Content component: User Mailbox * * @version 1.0.0 * @package WB * @subpackage content */ class WBContent_User_Mailbox extends WBContent { const TABLE_USER = 'user'; const TABLE_USERGROUPMSG = 'usergroupmessage'; const TABLE_USERGROUPMSGUSER = 'usergroupmessageuser'; /** * Table access * @var WBDatasource_Table */ private $table; /** * my parameter list * @var array */ protected $config = array( 'action' => 'list', 'status' => 'new', 'id' => 0, 'goto' => 0, 'limit' => 10, 'requiredgroup' => '', 'mode' => 'normal' ); /** * Skeleton of clause * @var */ private $clause; /** * 2nd constructor * * Called after configuration was done */ protected function init() { $this->table = WBClass::create('WBDatasource_Table'); $this->clause = array(); } /** * Run * * Select either admin or normals user mode * * @return array parameter list */ public function run() { // user required, group optionaly required if (!$this->user->isInGroup($this->config['requiredgroup'])) { $this->loadTemplates('anon'); $this->config['action'] = 'display'; return $this->config; } if ('admin' == $this->config['mode']) { return $this->runAdmin(); } return $this->runNormal(); } /** * Run component in normal user mode * * @return array parameter list */ private function runNormal() { // Only select user's owned messages $this->setUserIdInClause($this->user->getId()); $id = null; switch (strtolower($this->config['action'])) { case 'confirm': $this->confirmMsg(); $this->loadTemplates('list'); break; default: case 'list'; if (!empty($this->config['id'])) { $id = $this->config['id']; } $this->loadTemplates('list'); break; } // add total number of messages per user $clause = $this->clause; $total = $this->table->count(self::TABLE_USERGROUPMSGUSER, null, null, $clause); $this->tmpl->addGlobalVar('total', $total); $this->loadList($id); return $this->config; } /** * Run component in admin user mode * */ private function runAdmin() { $id = null; switch (strtolower($this->config['action'])) { case 'show': $id = $this->config['id']; $this->loadTemplates('adminlist'); break; case 'rm': $this->removeMsg($this->req->get('uid')); // fall through default: case 'list'; $this->loadTemplates('adminlist'); break; } $this->loadList($id); return $this->config; } /** * Mark message as read * * Select user's message and save confirmed date */ private function confirmMsg() { if (empty($this->config['id'])) { return; } $clause = $this->clause; $clause[] = array( 'field' => $this->table->getIdentifier(self::TABLE_USERGROUPMSG), 'value' => $this->config['id'] ); $save = array( 'confirmed' => gmdate('Y-m-d H:i:s') ); $this->table->save(self::TABLE_USERGROUPMSGUSER, null, $save, $clause); } /** * Delete user's message * * @param string $uid */ private function removeMsg($uid) { if (empty($this->config['id'])) { return; } if (empty($uid)) { return; } $this->setUserIdInClause($uid); $clause = $this->clause; $this->setUserIdInClause(null); $clause[] = array( 'field' => $this->table->getIdentifier(self::TABLE_USERGROUPMSG), 'value' => $this->config['id'] ); $this->table->delete(self::TABLE_USERGROUPMSGUSER, null, null, $clause); } /** * Load list of messages * * Select records from database and add to templates * * @param string $id message id, if any */ private function loadList($id = null) { $clause = $this->clause; if (!empty($id)) { $clause[] = array( 'table' => self::TABLE_USERGROUPMSG, 'field' => $this->table->getIdentifier(self::TABLE_USERGROUPMSG), 'value' => $id ); } // Respect deleted users $userInfo = $this->table->getTableInfo('user'); if (isset($userInfo['delete']) && !empty($userInfo['delete'])) { $clause[] = array( 'table' => self::TABLE_USER, 'field' => $userInfo['delete'], 'value' => 0 ); } // filter by status $relation = 'le'; switch ($this->config['status']) { case 'any': break; case 'old': $relation = 'gt'; // fall through default: case 'new': $clause[] = array( 'table' => self::TABLE_USERGROUPMSGUSER, 'field' => 'confirmed', 'relation' => $relation, 'valuetype' => 'foreign', 'foreign' => self::TABLE_USERGROUPMSGUSER, 'value' => 'created' ); break; } $options = array( 'column' => array( array( 'table' => self::TABLE_USERGROUPMSG, 'field' => '*' ), array( 'table' => self::TABLE_USERGROUPMSGUSER, 'field' => 'confirmed' ), array( 'table' => self::TABLE_USERGROUPMSGUSER, 'field' => $this->table->getIdentifier(self::TABLE_USER) ), array( 'table' => self::TABLE_USERGROUPMSGUSER, 'field' => 'created', 'as' => 'sent' ) ), 'limit' => $this->config['limit'], 'order' => array( array( 'table' => self::TABLE_USERGROUPMSGUSER, 'field' => 'created', 'asc' => 0 ), array( 'table' => self::TABLE_USER, 'field' => 'surname', 'asc' => 1 ) ) ); $options['join'] = array(); $options['join'][] = array( 'table' => self::TABLE_USERGROUPMSGUSER, 'using' => $this->table->getIdentifier(self::TABLE_USERGROUPMSG) ); $options['join'][] = array( 'table' => self::TABLE_USER, 'onleft' => $this->table->getIdentifier(self::TABLE_USER), 'lefttable' => self::TABLE_USERGROUPMSGUSER, 'onright' => $this->table->getIdentifier(self::TABLE_USER) ); $pager = $this->table->getPager(self::TABLE_USERGROUPMSG . ':' . $this->user->getID(), self::TABLE_USERGROUPMSG, null, $clause, $options); $this->tmpl->addGlobalVars($this->config, 'config_'); $this->tmpl->addGlobalVars($pager->browse($this->config['goto']), 'pager_'); $list = $pager->get(); $this->tmpl->addRows('list_entry', $list); if (!empty($id)) { $this->tmpl->addGlobalVars($list[0], 'MSG_'); } } /** * Set user id in global clause * * @param string $uid */ private function setUserIdInClause($uid) { if (empty($uid)) { $this->clause = array(); return; } if (empty($this->clause)) { $this->clause[] = array( 'table' => self::TABLE_USERGROUPMSGUSER, 'field' => $this->table->getIdentifier(self::TABLE_USER), 'value' => $uid ); return; } $this->clause[0]['value'] = $uid; } /** * receive output * * fetch output of this content component * * @return string */ public function getString() { return $this->tmpl->getParsedTemplate('snippet'); } }