Esempio n. 1
0
/**
 * Prepare all info that needed to build paginated items
 *
 * @param array $items Items which are separated by pages.
 * @param int $default_items_per_page Count of items per page.
 * @return array Associative array of with the following keys:
 *   - info: array, pagination info. See description of the result of
 *     pagination_info function for details.
 *   - items: slice of items to display.
 */
function setup_pagination($items, $default_items_per_page = 15)
{
    if (count($items) > 0) {
        $info = pagination_info(count($items), $default_items_per_page);
        if ($info) {
            $items_slice = array_slice($items, $info['start'], $info['end'] - $info['start']);
            return array('info' => $info, 'items' => $items_slice);
        }
    }
    return array('info' => false, 'items' => false);
}
Esempio n. 2
0
function setup_redirect_links(UrlGeneratorInterface $url_generator, $threadid, $operator, $token)
{
    $result = array();

    $operator_in_isolation = in_isolation($operator);

    $list_options = $operator_in_isolation
        ? array('isolated_operator_id' => $operator['operatorid'])
        : array();
    $operators = get_operators_list($list_options);
    $operators_count = count($operators);

    $groups_count = 0;
    $groups = array();
    if (Settings::get('enablegroups') == "1") {
        $groupslist = $operator_in_isolation
            ? get_groups_for_operator($operator, true)
            : get_groups(true);
        foreach ($groupslist as $group) {
            if ($group['inumofagents'] == 0) {
                continue;
            }
            $groups[] = $group;
        }
        $groups_count = count($groups);
    }

    $p = pagination_info(max($operators_count, $groups_count), 8);
    $result['pagination'] = $p;

    $operators = array_slice($operators, $p['start'], $p['end'] - $p['start']);
    $groups = array_slice($groups, $p['start'], $p['end'] - $p['start']);

    $agent_list = "";
    $params = array('thread_id' => $threadid, 'token' => $token);
    foreach ($operators as $agent) {
        $params['nextAgent'] = $agent['operatorid'];
        $status = $agent['time'] < Settings::get('online_timeout')
            ? ($agent['istatus'] == 0
                ? getlocal("(online)")
                : getlocal("(away)"))
            : "";
        $agent_list .= "<li><a href=\"" . $url_generator->generate('chat_operator_redirect', $params)
            . "\" title=\"" . get_operator_name($agent) . "\">"
            . get_operator_name($agent)
            . "</a> $status</li>";
    }
    $result['redirectToAgent'] = $agent_list;

    $group_list = "";
    if (Settings::get('enablegroups') == "1") {
        $params = array('thread_id' => $threadid, 'token' => $token);
        foreach ($groups as $group) {
            $params['nextGroup'] = $group['groupid'];
            $status = group_is_online($group)
                ? getlocal("(online)")
                : (group_is_away($group) ? getlocal("(away)") : "");
            $group_list .= "<li><a href=\"" . $url_generator->generate('chat_operator_redirect', $params)
                . "\" title=\"" . get_group_name($group) . "\">"
                . get_group_name($group)
                . "</a> $status</li>";
        }
    }
    $result['redirectToGroup'] = $group_list;

    return $result;
}
 /**
  * Generates the main history page with search and results.
  *
  * @param Request $request
  * @return string Rendered page content
  */
 public function indexAction(Request $request)
 {
     $page = array();
     $operator = $this->getOperator();
     $query = $request->query->get('q', false);
     $search_type = $request->query->get('type');
     if (!in_array($search_type, array('all', 'message', 'operator', 'visitor'))) {
         $search_type = 'all';
     }
     $search_in_system_messages = $request->query->get('insystemmessages') == 'on' || !$query;
     if ($query !== false) {
         // Escape MySQL LIKE wildcards in the query
         $escaped_query = str_replace(array('%', '_'), array('\\%', '\\_'), $query);
         // Replace commonly used "?" and "*" wildcards with MySQL ones.
         $escaped_query = str_replace(array('*', '?'), array('%', '_'), $escaped_query);
         $db = Database::getInstance();
         $groups = $db->query("SELECT {opgroup}.groupid AS groupid, vclocalname " . "FROM {opgroup} " . "ORDER BY vclocalname", null, array('return_rows' => Database::RETURN_ALL_ROWS));
         $group_name = array();
         foreach ($groups as $group) {
             $group_name[$group['groupid']] = $group['vclocalname'];
         }
         $values = array(':query' => "%{$escaped_query}%", ':invitation_accepted' => Thread::INVITATION_ACCEPTED, ':invitation_not_invited' => Thread::INVITATION_NOT_INVITED);
         $search_conditions = array();
         if ($search_type == 'message' || $search_type == 'all') {
             $search_conditions[] = "({message}.tmessage LIKE :query" . ($search_in_system_messages ? '' : " AND ({message}.ikind = :kind_user OR {message}.ikind = :kind_agent)") . ")";
             if (!$search_in_system_messages) {
                 $values[':kind_user'] = Thread::KIND_USER;
                 $values[':kind_agent'] = Thread::KIND_AGENT;
             }
         }
         if ($search_type == 'operator' || $search_type == 'all') {
             $search_conditions[] = "({thread}.agentname LIKE :query)";
         }
         if ($search_type == 'visitor' || $search_type == 'all') {
             $search_conditions[] = "({thread}.username LIKE :query)";
             $search_conditions[] = "({thread}.remote LIKE :query)";
         }
         // Build access condition
         $operator = $this->getOperator();
         $access = $this->buildAccessCondition($operator);
         $access_condition = $access['condition'];
         $values += $access['values'];
         // Load threads
         list($threads_count) = $db->query("SELECT COUNT(DISTINCT {thread}.dtmcreated) " . "FROM {thread}, {message} " . "WHERE {message}.threadid = {thread}.threadid " . "AND ({thread}.invitationstate = :invitation_accepted " . "OR {thread}.invitationstate = :invitation_not_invited) " . "AND (" . implode(' OR ', $search_conditions) . ") " . $access_condition, $values, array('return_rows' => Database::RETURN_ONE_ROW, 'fetch_type' => Database::FETCH_NUM));
         $pagination_info = pagination_info($threads_count);
         if ($threads_count && $pagination_info) {
             $page['pagination'] = $pagination_info;
             $limit_start = intval($pagination_info['start']);
             $limit_end = intval($pagination_info['end'] - $pagination_info['start']);
             $threads_list = $db->query("SELECT DISTINCT {thread}.* " . "FROM {thread}, {message} " . "WHERE {message}.threadid = {thread}.threadid " . "AND ({thread}.invitationstate = :invitation_accepted " . "OR {thread}.invitationstate = :invitation_not_invited) " . "AND (" . implode(' OR ', $search_conditions) . ") " . $access_condition . "ORDER BY {thread}.dtmcreated DESC " . "LIMIT " . $limit_start . ", " . $limit_end, $values, array('return_rows' => Database::RETURN_ALL_ROWS));
             foreach ($threads_list as $item) {
                 $thread = Thread::createFromDbInfo($item);
                 $group_name_set = $thread->groupId && $thread->groupId != 0 && isset($group_name[$thread->groupId]);
                 $page['pagination.items'][] = array('threadId' => $thread->id, 'userName' => $thread->userName, 'userAddress' => get_user_addr($thread->remote), 'agentName' => $thread->agentName, 'messageCount' => $thread->messageCount, 'groupName' => $group_name_set ? $group_name[$thread->groupId] : false, 'chatTime' => $thread->modified - $thread->created, 'chatCreated' => $thread->created);
             }
         } else {
             $page['pagination'] = false;
             $page['pagination.items'] = false;
         }
         $page['formq'] = $query;
     } else {
         $page['pagination'] = false;
         $page['pagination.items'] = false;
     }
     $page['formtype'] = $search_type;
     $page['forminsystemmessages'] = $search_in_system_messages;
     $page['title'] = getlocal("Chat history");
     $page['menuid'] = "history";
     $page['canSearchInSystemMessages'] = $search_type != 'all' && $search_type != 'message';
     $page = array_merge($page, prepare_menu($operator));
     return $this->render('history', $page);
 }