public function output_message_page($type = 'inline') { $message_id = $this->get('shub_message_id'); if ($message_id && $this->get('shub_account_id')) { if ('popup' == $type) { $this->mark_as_read(); } // icon for this account. $icons = SupportHub::getInstance()->message_managers[$this->network]->get_friendly_icon('shub_message_account_icon'); // icon for the linked product $shub_product_id = $this->get_product_id(); $product_data = array(); $item_data = array(); $bbpress_item = $this->get('item'); if (!$shub_product_id && $bbpress_item) { $shub_product_id = $bbpress_item->get('shub_product_id'); $item_data = $bbpress_item->get('item_data'); if (!is_array($item_data)) { $item_data = array(); } } if ($shub_product_id) { $shub_product = new SupportHubProduct(); $shub_product->load($shub_product_id); $product_data = $shub_product->get('product_data'); } if ($shub_product_id && !empty($product_data['image'])) { if (!empty($product_data['url'])) { $icons .= '<a href="' . esc_attr($product_data['url']) . '" target="_blank">'; } $icons .= '<img src="' . esc_attr($product_data['image']) . '" class="shub_message_account_icon">'; if (!empty($product_data['url'])) { $icons .= '</a>'; } } ?> <div class="message_edit_form" data-network="<?php echo $this->network; ?> "> <section class="message_sidebar"> <nav> <?php if ($this->get('shub_status') == _shub_MESSAGE_STATUS_ANSWERED) { ?> <a href="#" class="shub_message_action btn btn-default btn-xs button shub_button_loading" data-action="set-unanswered" data-post="<?php echo esc_attr(json_encode(array('network' => $this->network, 'shub_message_id' => $message_id, 'last_activity' => $this->get('last_active')))); ?> "><?php _e('Inbox'); ?> </a> <?php } else { ?> <a href="#" class="shub_message_action btn btn-default btn-xs button shub_button_loading" data-action="set-answered" data-post="<?php echo esc_attr(json_encode(array('network' => $this->network, 'shub_message_id' => $message_id, 'last_activity' => $this->get('last_active')))); ?> "><?php _e('Archive'); ?> </a> <?php } ?> <span class="responsive_sidebar_summary"> <?php echo $icons; // todo - what other details need to show in mobile view? ?> </span> <a href="#" class="shub_view_full_message_sidebar btn btn-default btn-xs button alignright"><?php _e('More Details'); ?> </a> </nav> <header> <a href="<?php echo $this->get_link(); ?> " class="social_view_external btn btn-default btn-xs button" target="_blank"><?php _e('View Comment'); ?> </a> <?php if ($this->get('shub_status') == _shub_MESSAGE_STATUS_ANSWERED) { ?> <a href="#" class="shub_message_action btn btn-default btn-xs button shub_button_loading" data-action="set-unanswered" data-post="<?php echo esc_attr(json_encode(array('network' => $this->network, 'shub_message_id' => $message_id, 'last_activity' => $this->get('last_active')))); ?> "><?php _e('Inbox'); ?> </a> <?php } else { ?> <a href="#" class="shub_message_action btn btn-default btn-xs button shub_button_loading" data-action="set-answered" data-post="<?php echo esc_attr(json_encode(array('network' => $this->network, 'shub_message_id' => $message_id, 'last_activity' => $this->get('last_active')))); ?> "><?php _e('Archive'); ?> </a> <?php } ?> </header> <aside class="message_sidebar"> <?php echo $icons; echo '<br/>'; // this method does all the magic of getting the linked user ids and other messages $data = $this->get_message_sidebar_data($product_data, $item_data); if (!empty($data['message_details'])) { ?> <div class="message_sidebar_details"> <?php foreach ($data['message_details'] as $message_details) { ?> <div> <strong><?php echo htmlspecialchars($message_details[0]); ?> :</strong> <?php echo $message_details[1]; ?> </div> <?php } ?> </div> <?php } if (!empty($data['extra_datas'])) { ?> <div class="message_sidebar_extra_data"> <?php foreach ($data['extra_datas'] as $extra_data) { if (isset($extras[$extra_data->get('shub_extra_id')])) { ?> <div> <strong><?php echo htmlspecialchars($extras[$extra_data->get('shub_extra_id')]->get('extra_name')); ?> :</strong> <?php switch ($extras[$extra_data->get('shub_extra_id')]->get('field_type')) { case 'encrypted': echo '(encrypted)'; break; default: echo shub_forum_text($extra_data->get('extra_value'), false); } ?> </div> <?php } } ?> </div> <?php } if (!empty($data['user_bits'])) { ?> <ul class="linked_user_details"> <?php foreach ($data['user_bits'] as $user_bit) { ?> <li><strong><?php echo $user_bit[0]; ?> :</strong> <?php echo $user_bit[1]; ?> </li> <?php } ?> </ul> <?php } if (!empty($data['other_messages'])) { ?> <div class="shub_other_messages"> <strong><?php echo sprintf(_n('%d Other Message:', '%d Other Messages:', count($data['other_messages']), 'support_hub'), count($data['other_messages'])); ?> </strong><br/> <ul> <?php foreach ($data['other_messages'] as $other_message) { ?> <li> <span class="other_message_time"><?php echo shub_pretty_date($other_message['time']); ?> </span> <span class="other_message_status"><?php if (isset($other_message['message_status'])) { switch ($other_message['message_status']) { case _shub_MESSAGE_STATUS_ANSWERED: echo '<span class="message_status_archived">Archived</span>'; break; case _shub_MESSAGE_STATUS_UNANSWERED: echo '<span class="message_status_inbox">Inbox</span>'; break; case _shub_MESSAGE_STATUS_HIDDEN: echo '<span class="message_status_hidden">Hidden</span>'; break; default: echo 'UNKNOWN?'; } } ?> </span> <span class="other_message_network"> <?php echo $other_message['icon']; ?> </span> <br/> <a href="<?php echo esc_attr($other_message['link']); ?> " target="_blank" class="shub_modal" data-network="<?php echo esc_attr($other_message['network']); ?> " data-message_id="<?php echo (int) $other_message['message_id']; ?> " data-message_comment_id="<?php echo isset($other_message['message_comment_id']) ? (int) $other_message['message_comment_id'] : ''; ?> " data-modaltitle="<?php echo esc_attr($other_message['summary']); ?> "><?php echo esc_html($other_message['summary']); ?> </a> </li> <?php } ?> </ul> </div> <?php } do_action('supporthub_message_header', $this->network, $this); ?> </aside> </section> <section class="message_content"> <?php // we display the first "primary" message (from the ucm_message table) followed by comments from the ucm_message_comment table. //$this->full_message_output(true); $this->output_message_list(); ?> </section> <section class="message_request_extra"> <?php SupportHubExtra::form_request_extra(array('network' => $this->network, 'account-id' => $this->get('shub_account_id'), 'message-id' => $message_id)); ?> </section> </div> <?php } }
public function get_message_user_summary($user_hints, $current_extension, $message_object) { // here we hunt down other messages from this user and bring them back to the UI $return = array('extra_datas' => array(), 'user_details' => array()); $user_details = array(); $other_messages = array(); if (!empty($user_hints['shub_user_id']) && !is_array($user_hints['shub_user_id'])) { $user_hints['shub_user_id'] = array($user_hints['shub_user_id']); } $possible_new_user_ids = array(); do { $this_user_hints = $user_hints; if (count($possible_new_user_ids)) { $this_user_hints['shub_user_id'] = array(); foreach ($possible_new_user_ids as $possible_new_user_id) { if (!in_array($possible_new_user_id, $user_hints['shub_user_id'])) { $user_hints['shub_user_id'][] = $possible_new_user_id; $this_user_hints['shub_user_id'][] = $possible_new_user_id; } } $possible_new_user_ids = array(); } foreach ($this->message_managers as $name => $message_manager) { $details = $message_manager->find_other_user_details($this_user_hints, $current_extension, $message_object); if ($details && isset($details['messages']) && is_array($details['messages'])) { $other_messages = array_merge_recursive($other_messages, $details['messages']); } if ($details && isset($details['user']) && is_array($details['user'])) { $user_details = array_merge_recursive($user_details, $details['user']); } if ($details && isset($details['user_ids']) && is_array($details['user_ids'])) { foreach ($details['user_ids'] as $possible_new_user_id) { if ((int) $possible_new_user_id > 0 && !in_array($possible_new_user_id, $user_hints['shub_user_id'])) { //echo "GOT A NEW USER ID BACK $possible_new_user_id <br><br>"; // re-run the search loop with this new user id t oget even more data. $possible_new_user_ids[] = $possible_new_user_id; } } } } } while (count($possible_new_user_ids)); // pull out the 'extra data' linked to this ticket $extras = SupportHubExtra::get_all_extras(); $extra_datas = array(); foreach ($extras as $extra_id => $extra) { $shub_user_ids = !empty($user_hints['shub_user_id']) ? $user_hints['shub_user_id'] : array(0); // stop duplicate values (if a user submits two support tickets with different email addresses and the same data, the extra data will be inserted into the database under his new user account, but it will show up here as a linked data info account, so we prevent duplicate data showing in this step) // todo: highlight data related to this support message, fade out data related to other support messages $this_data = array(); foreach ($shub_user_ids as $shub_user_id) { $this_extras = array(); foreach ($extra->get_data($current_extension, $message_object->get('shub_account_id'), $message_object->get('shub_message_id'), $shub_user_id) as $this_extra) { if (!in_array($this_extra->get('extra_value'), $this_data)) { $this_extras[] = $this_extra; $this_data[] = $this_extra->get('extra_value'); } // build up the list of linked user accounts based on user data. // example: if two users submit $extra_shub_user_id = $this_extra->get('shub_user_id'); if (!empty($extra_shub_user_id) && !in_array($extra_shub_user_id, $user_hints['shub_user_id'])) { echo " <br><br><Br>Error: Displaying extra data for the user '{$extra_shub_user_id}' when that user ID isn't already in the list. Please report this bug to dtbaker.<br><br><br> "; // moved the user_hints loop to the top to build up the user id list first. $user_hints['shub_user_id'][] = $extra_shub_user_id; } } $return['extra_datas'] = $return['extra_datas'] + $this_extras; } } $user_bits = array(); //$user_bits[] = array('Support Pack','UNKNOWN'); if (!empty($user_hints['shub_user_id'])) { foreach ($user_hints['shub_user_id'] as $shub_user_id) { $user = new SupportHubUser($shub_user_id); if ($user->get('shub_user_id')) { // find messages and comments from this user /*$messages = shub_get_multiple('shub_message', array( 'shub_user_id' => $shub_user_id ), 'shub_message_id');*/ global $wpdb; $sql = "SELECT sm.*, sa.shub_extension FROM `" . _support_hub_DB_PREFIX . "shub_message` sm "; $sql .= " LEFT JOIN `" . _support_hub_DB_PREFIX . "shub_account` sa USING (shub_account_id) WHERE 1 "; $sql .= " AND sm.`shub_user_id` = " . (int) $user->get('shub_user_id'); $sql .= " AND sm.`shub_message_id` != " . (int) $message_object->get('shub_message_id'); $sql .= ' ORDER BY shub_message_id'; $messages = $wpdb->get_results($sql, ARRAY_A); if (is_array($messages)) { foreach ($messages as $message) { if (!isset($other_messages[$message['shub_message_id']])) { $other_messages[$message['shub_message_id']] = array('link' => '?page=support_hub_main&network=' . $message['shub_extension'] . '&message_id=' . $message['shub_message_id'], 'summary' => $message['title'], 'time' => $message['last_active'], 'network' => $message['shub_extension'], 'icon' => $this->message_managers[$message['shub_extension']]->get_friendly_icon(), 'message_id' => $message['shub_message_id'], 'message_comment_id' => 0, 'message_status' => $message['shub_status']); } } } /*$comments = shub_get_multiple('shub_message_comment', array( 'shub_user_id' => $shub_user_id ), 'shub_message_comment_id');*/ $sql = "SELECT smc.*, sa.shub_extension, sm.shub_status FROM `" . _support_hub_DB_PREFIX . "shub_message_comment` smc "; $sql .= " LEFT JOIN `" . _support_hub_DB_PREFIX . "shub_message` sm USING (shub_message_id) "; $sql .= " LEFT JOIN `" . _support_hub_DB_PREFIX . "shub_account` sa USING (shub_account_id) WHERE 1 "; $sql .= " AND smc.`shub_user_id` = " . (int) $user->get('shub_user_id'); $sql .= " AND sm.`shub_message_id` != " . (int) $message_object->get('shub_message_id'); $sql .= ' ORDER BY shub_message_comment_id'; $comments = $wpdb->get_results($sql, ARRAY_A); if (is_array($comments)) { foreach ($comments as $comment) { if (!isset($other_messages[$comment['shub_message_id']])) { $other_messages[$comment['shub_message_id']] = array('link' => '?page=support_hub_main&network=' . $comment['shub_extension'] . '&message_id=' . $comment['shub_message_id'] . '&message_comment_id=' . $comment['shub_message_comment_id'], 'summary' => $comment['message_text'], 'time' => $comment['time'], 'network' => $comment['shub_extension'], 'icon' => $this->message_managers[$comment['shub_extension']]->get_friendly_icon(), 'message_id' => $comment['shub_message_id'], 'message_comment_id' => $comment['shub_message_comment_id'], 'message_status' => $comment['shub_status']); } } } // for debugging: $user_bits[] = array('shub_user_id', $user->get('shub_user_id')); if (!empty($user->details['user_fname'])) { $user_bits[] = array('FName', esc_html($user->details['user_fname'])); } if (!empty($user->details['user_lname'])) { $user_bits[] = array('LName', esc_html($user->details['user_lname'])); } if (!empty($user->details['user_username'])) { // add this code in here ( as well as below ) so we don't duplicate up on the 'username' field display if (isset($user_details['url']) && isset($user_details['username']) && $user->details['user_username'] == $user_details['username']) { $user_bits[] = array('Username', '<a href="' . esc_url($user_details['url']) . '" target="_blank">' . esc_html($user_details['username']) . '</a>'); unset($user_details['username']); // stop it displaying again below } else { if (isset($user_details['username']) && $user->details['user_username'] == $user_details['username']) { $user_bits[] = array('Username', esc_html($user_details['username'])); unset($user_details['username']); // stop it displaying again below } else { $user_bits[] = array('Username', esc_html($user->details['user_username'])); } } } if (!empty($user->details['user_email'])) { $user_bits[] = array('Email', '<a href="mailto:' . esc_html($user->details['user_email']) . '">' . esc_html($user->details['user_email']) . '</a>'); } } } } //var_export($this->message_managers['envato']->pull_purchase_code(false,'61782ac6-1f67-4302-b34a-17c9e4d4f123',array(),3452)); echo 'done';exit; if (isset($user_details['url']) && isset($user_details['username'])) { if (!is_array($user_details['url'])) { $user_details['url'] = array($user_details['url']); } if (!is_array($user_details['username'])) { $user_details['username'] = array($user_details['username']); } foreach ($user_details['url'] as $key => $url) { $user_bits[] = array('Username', '<a href="' . esc_url($url) . '" target="_blank">' . esc_html(isset($user_details['username'][$key]) ? $user_details['username'][$key] : 'N/A') . '</a>'); } } else { if (isset($user_details['username'])) { if (!is_array($user_details['username'])) { $user_details['username'] = array($user_details['username']); } foreach ($user_details['username'] as $key => $username) { $user_bits[] = array('Username', esc_html($username)); } } } /* if(isset($user_details['codes'])){ // todo: pull in information about this purchase code via ajax after the page has loaded. // cache and re-validate the purchase code from time to time. $purchase_codes = (is_array($user_details['codes']) ? implode(', ',$user_details['codes']) : $user_details['codes']); if(!empty($purchase_codes)){ $user_bits[] = array('Purchase Codes',$purchase_codes); } } */ $user_bits = apply_filters('supporthub_message_user_sidebar', $user_bits, $user_hints['shub_user_id']); // todo - group user output together nicely (e.g. Name <email>) so it looks better when there are multiple linked user accounts foreach ($user_bits as $key => $val) { // check if this one exists yet? foreach ($user_bits as $key_check => $val_check) { if ($key != $key_check && $val[0] == $val_check[0]) { // we're matching something like 'Username' with 'Username' if (trim(strip_tags($val[1])) == trim(strip_tags($val_check[1]))) { // same same! keep one, the longer one (which might contian a link) if (strlen($val[1]) > strlen($val_check[1])) { unset($user_bits[$key_check]); } else { unset($user_bits[$key]); } } } } } $return['user_bits'] = $user_bits; $return['user_details'] = $user_details; $return['other_messages'] = $other_messages; return $return; }
public function load_by_network_key($network_key, $ticket, $type, $debug = false) { switch ($type) { case 'ticket': $existing = shub_get_single('shub_message', 'network_key', $network_key); if ($existing) { // load it up. $this->load($existing['shub_message_id']); } if ($ticket && isset($ticket['ticket_id']) && $ticket['ticket_id'] == $network_key) { // get the messages from the API $api = $this->account->get_api(); $api_result = $api->api('ticket', 'message', array('ticket_ids' => $network_key)); if ($api_result && isset($api_result['tickets'][$network_key]) && count($api_result['tickets'][$network_key])) { //print_r($api_result); if (isset($ticket['staff']) && !empty($ticket['staff']['email'])) { $ticket['reply_from_shub_user_id'] = $this->account->get_api_user_to_id($ticket['staff']); } $all_comments = $api_result['tickets'][$network_key]; $comments = array(); foreach ($all_comments as $comment_id => $comment) { if (isset($comment['cache']) && $comment['cache'] == 'autoreply' || isset($comment['message_type_id']) && $comment['message_type_id'] == 3) { // this is an auto reply, don't bother importing it into the system here } else { $comment['id'] = $comment['ticket_message_id']; $comment['shub_user_id'] = $this->account->get_api_user_to_id($comment['user']); $comment['timestamp'] = $comment['message_time']; $comments[] = $comment; } } if (!$existing) { $this->create_new(); } $this->update('shub_account_id', $this->account->get('shub_account_id')); $this->update('shub_item_id', $this->item->get('shub_item_id')); // create/update a user entry for this comments. $shub_user_id = $this->account->get_api_user_to_id($ticket['user']); $this->update('shub_user_id', $shub_user_id); $this->update('title', $ticket['subject']); // latest comment goes in summary $this->update('summary', $comments[count($comments) - 1]['content']); $this->update('last_active', $ticket['last_message_timestamp']); $this->update('shub_type', $type); $this->update('shub_data', $ticket); $this->update('shub_link', $ticket['url']); $this->update('network_key', $network_key); if ($this->get('shub_status') != _shub_MESSAGE_STATUS_HIDDEN) { // we have to decide if we're updating the message status from answered to unanswered. // if this message status is already answered and the existing comment count matches the new comment count then we don't update the status // this is because we insert a placeholder "comment" into the db while the API does the push, and when we read again a few minutes later it overwrites this placeholder comment, so really it's not a new comment coming in just the one we posted through the API that takes a while to come back through. if ($this->get('shub_status') == _shub_MESSAGE_STATUS_ANSWERED && count($comments) == count($this->get_comments())) { // don't do anything } else { // comment count is different $this->update('shub_status', _shub_MESSAGE_STATUS_UNANSWERED); } } $this->update('comments', $comments); // add the extra fields from UCM into the ticket. if (!empty($ticket['extra']) && is_array($ticket['extra'])) { foreach ($ticket['extra'] as $extra_key => $extra_val) { $extra_val = trim($extra_val); if (strlen($extra_val)) { // add this one into the system. $ExtraField = new SupportHubExtra(); if (!$ExtraField->load_by('extra_name', $extra_key)) { $ExtraField->create_new(); $ExtraField->update('extra_name', $extra_key); } $ExtraField->save_and_link(array('extra_value' => $extra_val), 'ucm', $this->account->get('shub_account_id'), $this->get('shub_message_id'), $shub_user_id); } } } } return $this->get('shub_message_id'); } break; } return false; }
<?php $current_extra = isset($_REQUEST['shub_extra_id']) ? (int) $_REQUEST['shub_extra_id'] : false; $shub_extra = new SupportHubExtra(); if ($current_extra !== false) { $shub_extra->load($current_extra); ?> <div class="wrap"> <h2> <?php _e('Extra Details', 'support_hub'); ?> </h2> <form action="" method="post"> <input type="hidden" name="_process" value="save_extra_details"> <input type="hidden" name="shub_extra_id" value="<?php echo (int) $shub_extra->get('shub_extra_id'); ?> "> <?php wp_nonce_field('save-extra' . (int) $shub_extra->get('shub_extra_id')); ?> <table class="form-table"> <tbody> <tr> <th class="width1"> <?php _e('Extra Name', 'support_hub');