Example #1
0
 private function inbox($group = false)
 {
     $max = $this->input->get_post('max');
     $offset = $this->input->get_post('offset');
     $do_transcriptions = $this->vbx_settings->get('transcriptions', $this->tenant->id);
     if (!$max) {
         $max = self::PAGE_SIZE;
     }
     $this->template->add_css(asset_url('assets/c/messages.css'), 'link');
     $data = $this->init_view_data();
     $inbox_counts = $data['counts'];
     if ($group && !array_key_exists($group, $inbox_counts)) {
         redirect('messages/inbox');
         return;
     }
     $this->template->add_js('messages/scripts' . ($group ? '/' . $group : '') . '?' . http_build_query(compact('max', 'offset')), 'dynamic');
     $data['group'] = $group;
     $total_items = 0;
     $folders = array();
     if (!$group) {
         $data['group_name'] = 'Inbox';
         $groups = array_keys($inbox_counts);
     } else {
         $data['group_name'] = $inbox_counts[$group]->name;
         $groups = array($group);
         $folders[$group] = $inbox_counts[$group];
     }
     $users = $group == 0 ? array($this->user_id) : array();
     $message_options = array('group' => $groups, 'user' => $users);
     $messages = $this->vbx_message->get_messages($message_options, $offset, $max);
     $total_items = $messages['total'];
     $items = array();
     $this->load->library('pagination');
     $group_name = '';
     $data['active_users'] = $this->vbx_user->get_active_users();
     if ($messages['total'] < 1) {
         $group_name = $inbox_counts[$group]->name;
     } else {
         uasort($messages['messages'], 'sort_by_date');
         foreach ($messages['messages'] as $item) {
             $group_name = '';
             $group_id = 0;
             if ($item->owner_type == 'group' && isset($inbox_counts[$item->owner_id])) {
                 $group_name = $inbox_counts[$item->owner_id]->name;
                 $group_id = $item->owner_id;
             }
             if ($item->owner_type == 'user' && isset($inbox_counts[$item->owner_id])) {
                 $group_name = 'Inbox';
                 $group_id = $item->owner_id;
             }
             $this->session->set_flashdata('selected-folder', $group_name);
             $this->session->set_flashdata('selected-folder-id', $group_id);
             $short_summary = null;
             if (is_null($item->content_text)) {
                 $short_summary = '&nbsp;';
                 if ($do_transcriptions) {
                     $short_summary = "(no transcription)";
                 }
             } else {
                 $short_summary = substr($item->content_text, 0, 125) . (strlen($item->content_text) > 125 ? '...' : '');
             }
             $date_recorded = date('c', strtotime($item->created));
             $date_updated = date('c', strtotime($item->updated));
             $assigned_user = null;
             foreach ($data['active_users'] as $u) {
                 if ($u->id == $item->assigned_to) {
                     $assigned_user = clone $u;
                 }
             }
             $items[] = array('id' => $item->id, 'folder' => $group_name, 'folder_id' => $group_id, 'short_summary' => $short_summary, 'assigned' => $item->assigned_to, 'type' => $item->type, 'assigned_user' => $assigned_user, 'ticket_status' => $item->ticket_status, 'archived' => $item->status == 'archived' ? true : false, 'unread' => $item->status == 'new' ? true : false, 'recording_url' => $item->content_url, 'recording_length' => format_player_time($item->size), 'received_time' => $date_recorded, 'last_updated' => $date_updated, 'called' => format_phone($item->called), 'caller' => format_phone($item->caller), 'original_called' => $item->called, 'original_caller' => $item->caller, 'owner_type' => $item->owner_type);
         }
         $group_name = 'Inbox';
     }
     // set up pagination
     $group_id = $group === false ? 0 : $group;
     $page_config = array('base_url' => site_url('messages/inbox/' . $group_id), 'total_rows' => $total_items, 'per_page' => $max, 'uri_segment' => 4);
     $this->pagination->initialize($page_config);
     $data['items'] = $json['messages']['items'] = $items;
     // render to output array
     $data['pagination'] = CI_Template::literal($this->pagination->create_links());
     $data['transcribe'] = $do_transcriptions;
     /* Return current group */
     if ($group !== false && $group >= 0) {
         // $json = $messages;
         $json['id'] = $group;
         $json['name'] = $inbox_counts[$group]->name;
         $json['read'] = $inbox_counts[$group]->read;
         $json['new'] = $inbox_counts[$group]->new;
         $json['messages']['total'] = $total_items;
         $json['messages']['offset'] = $offset;
         $json['messages']['max'] = $max;
     } else {
         /* Return folder summary */
         foreach ($inbox_counts as $folder_count) {
             $folders[] = $folder_count;
         }
         $json = array('max' => $max, 'offset' => $offset, 'total' => $total_items, 'folders' => $folders);
     }
     $data['json'] = $json;
     $this->respond(' - ' . $group_name, 'messages/inbox', $data);
 }
Example #2
0
 private function get_details($message_id)
 {
     $max_annotations = $this->input->get('max_annotations');
     if (empty($max_annotations)) {
         $max_annotations = 20;
     }
     $data = $this->init_view_data();
     try {
         $message = $this->vbx_message->get_message($message_id);
         if (!$message) {
             throw new MessageException("Unable to retrieve message: {$message_id}");
         }
     } catch (VBX_MessageException $e) {
         $this->session->set_flashdata('error', $e->getMessage());
         redirect('messages/inbox');
     } catch (MessageException $e) {
         $this->session->set_flashdata('error', $e->getMessage());
         redirect('messages/inbox');
     }
     $this->vbx_message->mark_read($message->id, $this->user_id);
     if ($message->owner_type == 'user' && $message->owner_id != $this->session->userdata('user_id') && !in_array($message->owner_id, array_keys($data['counts']))) {
         $this->session->set_flashdata('You are not allowed to view that message');
         redirect('messages');
     }
     $data['group'] = '';
     if ($message->owner_type == 'group') {
         if (isset($data['counts'][$message->owner_id])) {
             $data['group'] = $data['counts'][$message->owner_id]->name;
         } else {
             $data['group'] = 'Inbox';
         }
     }
     $message->pretty_called = format_phone($message->called);
     $message->pretty_caller = format_phone($message->caller);
     $data['message'] = $message;
     $summary = $message->content_text;
     $this->load->model('vbx_user');
     $annotations = array();
     // $users = $this->vbx_user->get_active_users();
     $users = VBX_User::search(array('is_active' => 1));
     $active_users = array();
     foreach ($users as $active_user) {
         $active_users[] = array('id' => $active_user->id, 'first_name' => $active_user->first_name, 'last_name' => $active_user->last_name, 'email' => $active_user->email);
     }
     $folder_id = $message->owner_type == 'group' ? $message->owner_id : 0;
     $details = array('id' => $message_id, 'selected_folder' => $this->session->flashdata('selected-folder'), 'selected_folder_id' => $this->session->flashdata('selected-folder-id'), 'status' => $message->status, 'type' => $message->type, 'ticket_status' => $message->ticket_status, 'summary' => $summary, 'assigned' => $message->assigned_to, 'archived' => $message->status == 'archived' ? true : false, 'unread' => $message->status == 'new' ? true : false, 'recording_url' => preg_replace('/http:\\/\\//', 'https://', $message->content_url), 'recording_length' => format_player_time($message->size), 'received_time' => date('Y-M-d\\TH:i:s+00:00', strtotime($message->created)), 'last_updated' => date('Y-M-d\\TH:i:s+00:00', strtotime($message->updated)), 'called' => format_phone($message->called), 'caller' => format_phone($message->caller), 'original_called' => $message->called, 'original_caller' => $message->caller, 'folder' => $data['group'], 'folder_id' => $folder_id, 'message_type' => $message->type, 'active_users' => $active_users, 'owner_type' => $message->owner_type);
     $data = array_merge($data, $details);
     $data['json'] = $details;
     if ($max_annotations) {
         $annotations = $this->vbx_message->get_annotations($message_id);
         $items = array_slice($annotations, 0, $max_annotations);
         foreach ($items as $item_id => $item) {
             $items[$item_id]->created = date('c', strtotime($item->created));
         }
         $max_annotations = count($annotations) > $max_annotations ? $max_annotations : count($annotations);
         $annotation_details = array('items' => $items, 'max' => $max_annotations, 'total' => count($annotations));
         $data['annotations'] = $data['json']['annotations'] = $annotation_details;
     }
     $data['gravatars'] = $this->vbx_settings->get('gravatars', $this->tenant->id);
     $data['default_gravatar'] = asset_url('assets/i/user-icon.png');
     $date = date('M j, Y h:i:s', strtotime($message->created));
     $this->respond(' - ' . $data['group'] . " voicemail from  {$message->pretty_caller} at {$date} ", 'messages/details', $data);
 }