Contacts > List * @copyright Tamaranga */ class ContactsPage extends AccountPage { /** @var int Current User Id */ public $userId = 0; /** @var int Company Id */ public $companyId; /** @var array Folders list */ public $folders = []; /** @var int Active folder id */ public $folderId = 0; /** @var string Search query */ public $query = ''; /** @var int Total contacts found */ public $total = 0; /** @var array Contacts list */ public $contacts = []; /** @var string List pagination */ public $pages = ''; /** @var int Page number */ public $pageId; /** @var int Page size active option */ public $pageSize; /** @var array Page size options */ public $pageSizeOptions; public function init() { parent::init(); $this->setTitle(_t('users', 'Messages')); $this->setKey('internalmail.contacts.page'); $this->setTemplate('account/contacts/page', 'internalmail'); } public function handle() { if ($guest = $this->guestWarning()) { return $guest; } $this->userId = $this->request->userId(); $this->companyId = (bff::businessEnabled() ? $this->request->user()->companyID() : 0); $this->loadFilter(); } public function blocks() { $this->addTemplateBlock('filter', 'account/contacts/filter', 'internalmail'); $this->addTemplateBlock('list', 'account/contacts/list', 'internalmail'); } public function onSubmitAction() { $this->loadContacts(); $this->with('contacts', $this->contacts); # pass contacts => list return $this->getActionResponse([ 'list' => $this->getBlock('list'), 'pages' => $this->pages, 'total' => $this->total, ]); } public function data() { $data = parent::data(); $this->loadContacts(); return $data; } public function loadFilter() { $this->query = $this->input->postget('qq', TYPE_NOTAGS); if (! empty($this->query)) { $this->query = $this->input->cleanSearchString($this->query, 200/* max chars */); } $this->pageId = $this->input->postget('page', TYPE_UINT); $this->pageSize = $this->input->postget('pp', TYPE_INT); $this->pageSizeOptions = [ -1 => ['t' => _t('pgn', 'show all'), 'c' => 100], 15 => ['t' => _t('pgn', '[cnt] per page', ['cnt' => 15]), 'c' => 15], 25 => ['t' => _t('pgn', '[cnt] per page', ['cnt' => 25]), 'c' => 25], 50 => ['t' => _t('pgn', '[cnt] per page', ['cnt' => 50]), 'c' => 50], ]; if (! array_key_exists($this->pageSize, $this->pageSizeOptions)) { $this->pageSize = 15; } $this->folderId = $this->input->postget('f', TYPE_UINT); $this->foldersList(); } public function foldersList() { $this->folders = [ InternalMail::FOLDER_ALL => ['title' => _t('internalmail', 'All Messages')], ]; # Publisher filter if ($this->companyId && Listings::publisher([Listings::PUBLISHER_USER_OR_COMPANY, Listings::PUBLISHER_USER_TO_COMPANY])) { $this->folders[InternalMail::FOLDER_FOR_COMPANY] = ['title' => _t('internalmail', 'For company')]; $this->folders[InternalMail::FOLDER_FOR_USER] = ['title' => _t('internalmail', 'For individual')]; } if (InternalMail::foldersEnabled()) { $this->folders += InternalMail::getFolders(); } # Active if (! array_key_exists($this->folderId, $this->folders)) { $this->folderId = InternalMail::FOLDER_ALL; } $i = 0; foreach ($this->folders as $key => &$v) { $v['i'] = $i++; $v['id'] = $key; $v['active'] = ($this->folderId === $key); } unset($v); } public function loadContacts($countOnly = false) { # Disable folder filter $folderId = $this->folderId; if (! InternalMail::foldersEnabled()) { $folderId = -1; } $load = function ($count, $limit = '') use ($folderId) { return InternalMail::model()->getContactsListingFront( $this->userId, $this->companyId, $folderId, $this->query, $count, $limit ); }; $this->total = $load(true); if (! $this->total || $countOnly) { return $this->total; } if ($this->total > 0) { $pageSize = $this->pageSizeOptions[$this->pageSize]['c']; $pages = new Pagination($this->total, $pageSize, '?' . Pagination::PAGE_PARAM); $this->contacts = $load(false, $pages->getLimitOffset()); if (! empty($this->contacts)) { $conversation_url_company = InternalMail::url('account.chat', ['company' => '']); $conversation_url_user = InternalMail::url('account.chat', ['user' => '']); foreach ($this->contacts as &$v) { $v['company_id_my'] = ($v['company_id'] && $v['company_id'] == $this->companyId); if ($v['company_id'] && ! $v['company_id_my'] && ! empty($v['company'])) { $v['c_url'] = $conversation_url_company . $v['company']['keyword'] . '-' . $v['company']['id']; $v['c_name'] = $v['company']['title']; $v['c_logo'] = $v['company']['logo']; } else { $v['c_url'] = $conversation_url_user . $v['login'] . ($v['company_id_my'] ? '&company=1' : ''); $v['c_name'] = ( ! empty($v['name']) ? $v['name'] : $v['login'] ); $v['c_logo'] = UsersAvatar::url($v['user_id'], $v['avatar'], null, $v['sex']); $v['c_logoblock'] = UsersAvatar::block($v['user_id'], $v); } } unset($v); } $this->pages = $pages->view([], tpl::PGN_COMPACT); } return $this->total; } public function onMoveToFolderAction() { do { $interlocutorID = $this->request->post('user', TYPE_UINT); $companyID = $this->request->post('company', TYPE_UINT); $folderID = $this->request->post('folder', TYPE_UINT); if ( ! InternalMail::foldersEnabled() || ! $interlocutorID || ! $folderID || ! $this->userId || ! $this->isRequestValid() ) { $this->errors->reloadPage(); break; } if (! array_key_exists($folderID, InternalMail::getFolders())) { $this->errors->reloadPage(); break; } $interlocutorData = Users::model()->userData($interlocutorID, [ 'user_id','company_id','admin', ]); if ( empty($interlocutorData) || ($folderID == InternalMail::FOLDER_IGNORE && $interlocutorData['admin']) ) { $this->respond('added', 0); } else { $this->respond('added', InternalMail::model()->interlocutorToFolder( $this->userId, $interlocutorID, $companyID, $folderID )); } } while (false); return $this->getActionResponse(); } }