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;
}
 /**
  * Generates a page with user tracking information.
  *
  * @param Request $request
  * @return string Rendered page content
  */
 public function userTrackAction(Request $request)
 {
     if (Settings::get('enabletracking') == '0') {
         throw new BadRequestException('Tracking is disabled.');
     }
     if ($request->query->has('thread')) {
         $thread_id = $request->query->get('thread');
         if (!preg_match("/^\\d{1,8}\$/", $thread_id)) {
             throw new BadRequestException('Wrong thread ID.');
         }
         $visitor = track_get_visitor_by_thread_id($thread_id);
         if (!$visitor) {
             throw new BadRequestException('Wrong thread.');
         }
     } else {
         $visitor_id = $request->query->get('visitor');
         if (!preg_match("/^\\d{1,8}\$/", $visitor_id)) {
             throw new BadRequestException('Wrong visitor ID.');
         }
         $visitor = track_get_visitor_by_id($visitor_id);
         if (!$visitor) {
             throw new BadRequestException('Wrong visitor.');
         }
     }
     $path = track_get_path($visitor);
     $page['entry'] = htmlspecialchars($visitor['entry']);
     $page['history'] = array();
     ksort($path);
     foreach ($path as $k => $v) {
         $page['history'][] = array('date' => date_to_text($k), 'link' => htmlspecialchars($v));
     }
     $page['title'] = getlocal('Tracked path of visitor');
     $page['show_small_login'] = false;
     return $this->render('tracked', $page);
 }