/** * Generates a page with awaiting visitors. * * @param Request $request * @return string Rendered page content */ public function indexAction(Request $request) { $operator = $this->getOperator(); // Operator becomes online as soon as he open "operator/users" page notify_operator_alive($operator['operatorid'], 0); $operator['istatus'] = 0; $this->getAuthenticationManager()->setOperator($operator); $_SESSION[SESSION_PREFIX . "operatorgroups"] = get_operator_groups_list($operator['operatorid']); $page = array(); $page['havemenu'] = !$request->query->has('nomenu'); $page['showonline'] = (Settings::get('showonlineoperators') == '1'); $page['showvisitors'] = (Settings::get('enabletracking') == '1'); $page['title'] = getlocal("List of visitors waiting"); $page['menuid'] = "users"; $page = array_merge($page, prepare_menu($operator)); // Attach files of the client side application and start it $this->getAssetManager()->attachJs('js/compiled/users_app.js'); $this->getAssetManager()->attachJs( $this->startJsApplication($request, $operator), \Mibew\Asset\AssetManagerInterface::INLINE, 1000 ); return $this->render('users', $page); }
/** * Return updated threads list. API function * * Triggers * {@link \Mibew\EventDispatcher\Events::USERS_UPDATE_THREADS_ALTER} event. * * @param array $args Associative array of arguments. It must contains the * following keys: * - 'agentId': Id of the agent related to users window * - 'revision': last revision number at client side * @return array Array of results. It contains the following keys: * - 'threads': array of threads changes */ protected function apiUpdateThreads($args) { $operator = $this->checkOperator($args['agentId']); $since = $args['revision']; // Get operator groups if (!isset($_SESSION[SESSION_PREFIX . "operatorgroups"])) { $_SESSION[SESSION_PREFIX . "operatorgroups"] = get_operator_groups_list($operator['operatorid']); } $group_ids = $_SESSION[SESSION_PREFIX . "operatorgroups"]; $db = Database::getInstance(); $query = "SELECT t.*, " . " g.vclocalname AS group_localname, " . " g.vccommonname AS group_commonname " . " FROM {thread} t LEFT OUTER JOIN {opgroup} g ON " . " t.groupid = g.groupid " . " WHERE t.lrevision > :since " . " AND t.istate <> " . Thread::STATE_INVITED . ($since == 0 ? " AND t.istate <> " . Thread::STATE_CLOSED . " AND t.istate <> " . Thread::STATE_LEFT : "") . (Settings::get('enablegroups') == '1' ? " AND (g.groupid is NULL" . ($group_ids ? " OR g.groupid IN ({$group_ids}) OR g.groupid IN " . "(SELECT parent FROM {opgroup} " . "WHERE groupid IN ({$group_ids})) " : "") . ") " : "") . " ORDER BY t.threadid"; $rows = $db->query($query, array(':since' => $since), array('return_rows' => Database::RETURN_ALL_ROWS)); $revision = $since; $threads = array(); foreach ($rows as $row) { // Create thread instance $thread = Thread::createFromDbInfo($row); // Calculate agent permissions $can_open = !($thread->state == Thread::STATE_CHATTING && $thread->agentId != $operator['operatorid'] && !is_capable(CAN_TAKEOVER, $operator)); $can_view = $thread->agentId != $operator['operatorid'] && $thread->nextAgent != $operator['operatorid'] && is_capable(CAN_VIEWTHREADS, $operator); $can_ban = Settings::get('enableban') == "1"; // Get ban info $ban = Settings::get('enableban') == "1" ? Ban::loadByAddress($thread->remote) : false; if ($ban !== false && !$ban->isExpired()) { $ban_info = array('id' => $ban->id, 'reason' => $ban->comment); } else { $ban_info = false; } // Get user name $user_name = get_user_name($thread->userName, $thread->remote, $thread->userId); // Get user ip if (preg_match("/(\\d+\\.\\d+\\.\\d+\\.\\d+)/", $thread->remote, $matches) != 0) { $user_ip = $matches[1]; } else { $user_ip = false; } // Get thread operartor name $next_agent = $thread->nextAgent != 0 ? operator_by_id($thread->nextAgent) : false; if ($next_agent) { $agent_name = get_operator_name($next_agent); } else { if ($thread->agentName) { $agent_name = $thread->agentName; } else { $group_name = get_group_name(array('vccommonname' => $row['group_commonname'], 'vclocalname' => $row['group_localname'])); if ($group_name) { $agent_name = '-' . $group_name . '-'; } else { $agent_name = '-'; } } } // Get first message $first_message = null; if ($thread->shownMessageId != 0) { $line = $db->query("SELECT tmessage FROM {message} WHERE messageid = ? LIMIT 1", array($thread->shownMessageId), array('return_rows' => Database::RETURN_ONE_ROW)); if ($line) { $first_message = preg_replace("/[\r\n\t]+/", " ", $line["tmessage"]); } } $threads[] = array('id' => $thread->id, 'token' => $thread->lastToken, 'userId' => $thread->userId, 'userName' => $user_name, 'userIp' => $user_ip, 'remote' => $thread->remote, 'userAgent' => get_user_agent_version($thread->userAgent), 'agentId' => $thread->agentId, 'agentName' => $agent_name, 'canOpen' => $can_open, 'canView' => $can_view, 'canBan' => $can_ban, 'ban' => $ban_info, 'state' => $thread->state, 'totalTime' => $thread->created, 'waitingTime' => $thread->modified, 'firstMessage' => $first_message); // Get max revision if ($thread->lastRevision > $revision) { $revision = $thread->lastRevision; } // Clean up unset($thread); } // Provide an ability to alter threads list $arguments = array('threads' => $threads); $dispatcher = EventDispatcher::getInstance(); $dispatcher->triggerEvent(Events::USERS_UPDATE_THREADS_ALTER, $arguments); // Send results back to the client. "array_values" function should be // used to avoid problems with JSON conversion. If there will be gaps in // keys (the keys are not serial) JSON Object will be produced instead // of an Array. return array('threads' => array_values($arguments['threads']), 'lastRevision' => $revision); }