Example #1
0
/**
 * Invite visitor by operator
 *
 * Triggers {@link \Mibew\EventDispatcher\Events::INVITATION_CREATE} event.
 *
 * @param int $visitor_id ID of the visitor, who must be invited.
 * @param array $operator Info for operator  who invite the visitor
 * @return Thread|boolean Thread object related with invitation or boolean
 *   false on failure
 */
function invitation_invite($visitor_id, $operator)
{
    // Check if visitor already invited
    $invitation_state = invitation_state($visitor_id);
    if ($invitation_state['invited']) {
        return false;
    }
    // Get visitor info
    $visitor = track_get_visitor_by_id($visitor_id);
    // Get last page visited by the visitor
    $visitor_path = track_get_path($visitor);
    ksort($visitor_path);
    $last_visited_page = array_pop($visitor_path);
    // Get visitor details
    $visitor_details = track_retrieve_details($visitor);
    // Get some operator's info
    $operator_name = get_operator_name($operator);
    // Create thread for invitation
    $thread = new Thread();
    // Populate thread and save it
    $thread->agentId = $operator['operatorid'];
    $thread->agentName = $operator_name;
    $thread->userName = $visitor['username'];
    $thread->remote = $visitor_details['remote_host'];
    $thread->referer = $last_visited_page;
    // User's locale is unknown, set operator locale to the thread
    $thread->locale = get_current_locale();
    $thread->userId = $visitor['userid'];
    $thread->userAgent = $visitor_details['user_agent'];
    $thread->state = Thread::STATE_INVITED;
    $thread->invitationState = Thread::INVITATION_WAIT;
    $thread->save();
    $db = Database::getInstance();
    $db->query("UPDATE {sitevisitor} set " . "invitations = invitations + 1, " . "threadid = :thread_id " . "WHERE visitorid = :visitor_id", array(':thread_id' => $thread->id, ':visitor_id' => $visitor_id));
    // Send some messages
    $thread->postMessage(Thread::KIND_FOR_AGENT, getlocal('Operator {0} invites visitor at {1} page', array($operator_name, $last_visited_page), get_current_locale(), true));
    $thread->postMessage(Thread::KIND_AGENT, getlocal('Hello, how can I help you?', null, get_current_locale(), true), array('name' => $operator_name, 'operator_id' => $operator['operatorid']));
    // Let plugins know about the invitation.
    $args = array('invitation' => $thread);
    EventDispatcher::getInstance()->triggerEvent(Events::INVITATION_CREATE, $args);
    return $thread;
}
Example #2
0
 /**
  * Return updated visitors list. API function.
  *
  * Triggers
  * {@link \Mibew\EventDispatcher\Events::USERS_UPDATE_VISITORS_LOAD} and
  * {@link \Mibew\EventDispatcher\Events::USERS_UPDATE_VISITORS_ALTER}
  * events.
  *
  * @param array $args Associative array of arguments. It must contains the
  *   following keys:
  *    - 'agentId': Id of the agent related to users window
  *
  * @return array Array of results. It contains the following keys:
  *  - 'visitors': array of visitors on the site
  */
 protected function apiUpdateVisitors($args)
 {
     // Check access
     $this->checkOperator($args['agentId']);
     // Close old invitations
     invitation_close_old();
     // Remove old visitors
     track_remove_old_visitors();
     // Get instance of event dispatcher
     $dispatcher = EventDispatcher::getInstance();
     // Trigger load event
     $arguments = array('visitors' => false);
     $dispatcher->triggerEvent(Events::USERS_UPDATE_VISITORS_LOAD, $arguments);
     // Check if visiors list loaded by plugins
     if (!is_array($arguments['visitors'])) {
         // Load visitors list
         $db = Database::getInstance();
         // Load visitors
         $query = "SELECT v.visitorid, " . "v.userid, " . "v.username, " . "v.firsttime, " . "v.lasttime, " . "v.entry, " . "v.details, " . "t.invitationstate, " . "t.dtmcreated AS invitationtime, " . "t.agentId AS invitedby, " . "v.invitations, " . "v.chats " . "FROM {sitevisitor} v " . "LEFT OUTER JOIN {thread} t " . "ON t.threadid = v.threadid " . "WHERE v.threadid IS NULL " . "OR (t.istate = :state_invited " . "AND t.invitationstate = :invitation_wait)" . "ORDER BY t.invitationstate, v.lasttime DESC, v.invitations";
         $query .= Settings::get('visitors_limit') == '0' ? "" : " LIMIT " . Settings::get('visitors_limit');
         $rows = $db->query($query, array(':state_invited' => Thread::STATE_INVITED, ':invitation_wait' => Thread::INVITATION_WAIT), array('return_rows' => Database::RETURN_ALL_ROWS));
         $visitors = array();
         foreach ($rows as $row) {
             // Get visitor details
             $details = track_retrieve_details($row);
             // Get user agent
             $user_agent = get_user_agent_version($details['user_agent']);
             // Get user ip
             if (preg_match("/(\\d+\\.\\d+\\.\\d+\\.\\d+)/", $details['remote_host'], $matches) != 0) {
                 $user_ip = $matches[1];
             } else {
                 $user_ip = false;
             }
             // Get invitation info
             $row['invited'] = $row['invitationstate'] == Thread::INVITATION_WAIT;
             if ($row['invited']) {
                 $agent_name = get_operator_name(operator_by_id($row['invitedby']));
                 $invitation_info = array('time' => $row['invitationtime'], 'agentName' => $agent_name);
             } else {
                 $invitation_info = false;
             }
             // Create resulting visitor structure
             $visitors[] = array('id' => (int) $row['visitorid'], 'userId' => $row['userid'], 'userName' => $row['username'], 'userAgent' => $user_agent, 'userIp' => $user_ip, 'remote' => $details['remote_host'], 'firstTime' => $row['firsttime'], 'lastTime' => $row['lasttime'], 'invitations' => (int) $row['invitations'], 'chats' => (int) $row['chats'], 'invitationInfo' => $invitation_info);
         }
     } else {
         $visitors = $arguments['visitors'];
     }
     // Provide ability to alter visitors list
     $arguments = array('visitors' => $visitors);
     $dispatcher->triggerEvent(Events::USERS_UPDATE_VISITORS_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('visitors' => array_values($arguments['visitors']));
 }