public function handle_process($process, $options = array())
 {
     switch ($process) {
         case 'send_shub_message':
             $message_count = 0;
             if (check_admin_referer('shub_send-message') && isset($options['shub_message_id']) && (int) $options['shub_message_id'] > 0 && isset($_POST['ucm_message']) && !empty($_POST['ucm_message'])) {
                 // we have a social message id, ready to send!
                 // which ucm accounts are we sending too?
                 $ucm_accounts = isset($_POST['compose_ucm_id']) && is_array($_POST['compose_ucm_id']) ? $_POST['compose_ucm_id'] : array();
                 foreach ($ucm_accounts as $ucm_account_id => $send_products) {
                     $ucm_account = new shub_ucm_account($ucm_account_id);
                     if ($ucm_account->get('shub_ucm_id') == $ucm_account_id) {
                         /* @var $available_products shub_ucm_product[] */
                         $available_products = $ucm_account->get('products');
                         if ($send_products) {
                             foreach ($send_products as $ucm_product_id => $tf) {
                                 if (!$tf) {
                                     continue;
                                 }
                                 // shouldnt happen
                                 switch ($ucm_product_id) {
                                     case 'share':
                                         // doing a status update to this ucm account
                                         $ucm_message = new shub_ucm_message($ucm_account, false, false);
                                         $ucm_message->create_new();
                                         $ucm_message->update('shub_ucm_product_id', 0);
                                         $ucm_message->update('shub_message_id', $options['shub_message_id']);
                                         $ucm_message->update('shub_ucm_id', $ucm_account->get('shub_ucm_id'));
                                         $ucm_message->update('summary', isset($_POST['ucm_message']) ? $_POST['ucm_message'] : '');
                                         $ucm_message->update('title', isset($_POST['ucm_title']) ? $_POST['ucm_title'] : '');
                                         $ucm_message->update('link', isset($_POST['ucm_link']) ? $_POST['ucm_link'] : '');
                                         if (isset($_POST['track_links']) && $_POST['track_links']) {
                                             $ucm_message->parse_links();
                                         }
                                         $ucm_message->update('type', 'share');
                                         $ucm_message->update('data', json_encode($_POST));
                                         $ucm_message->update('user_id', get_current_user_id());
                                         // do we send this one now? or schedule it later.
                                         $ucm_message->update('shub_status', _shub_MESSAGE_STATUS_PENDINGSEND);
                                         if (isset($options['send_time']) && !empty($options['send_time'])) {
                                             // schedule for sending at a different time (now or in the past)
                                             $ucm_message->update('last_active', $options['send_time']);
                                         } else {
                                             // send it now.
                                             $ucm_message->update('last_active', 0);
                                         }
                                         if (isset($_FILES['ucm_picture']['tmp_name']) && is_uploaded_file($_FILES['ucm_picture']['tmp_name'])) {
                                             $ucm_message->add_attachment($_FILES['ucm_picture']['tmp_name']);
                                         }
                                         $now = time();
                                         if (!$ucm_message->get('last_active') || $ucm_message->get('last_active') <= $now) {
                                             // send now! otherwise we wait for cron job..
                                             if ($ucm_message->send_queued(isset($_POST['debug']) && $_POST['debug'])) {
                                                 $message_count++;
                                             }
                                         } else {
                                             $message_count++;
                                             if (isset($_POST['debug']) && $_POST['debug']) {
                                                 echo "message will be sent in cron job after " . shub_print_date($ucm_message->get('last_active'), true);
                                             }
                                         }
                                         break;
                                     case 'blog':
                                         // doing a blog post to this ucm account
                                         // not possible through api
                                         break;
                                     default:
                                         // posting to one of our available products:
                                         // see if this is an available product.
                                         if (isset($available_products[$ucm_product_id])) {
                                             // push to db! then send.
                                             $ucm_message = new shub_ucm_message($ucm_account, $available_products[$ucm_product_id], false);
                                             $ucm_message->create_new();
                                             $ucm_message->update('shub_ucm_product_id', $available_products[$ucm_product_id]->get('shub_ucm_product_id'));
                                             $ucm_message->update('shub_message_id', $options['shub_message_id']);
                                             $ucm_message->update('shub_ucm_id', $ucm_account->get('shub_ucm_id'));
                                             $ucm_message->update('summary', isset($_POST['ucm_message']) ? $_POST['ucm_message'] : '');
                                             $ucm_message->update('title', isset($_POST['ucm_title']) ? $_POST['ucm_title'] : '');
                                             if (isset($_POST['track_links']) && $_POST['track_links']) {
                                                 $ucm_message->parse_links();
                                             }
                                             $ucm_message->update('type', 'product_post');
                                             $ucm_message->update('link', isset($_POST['link']) ? $_POST['link'] : '');
                                             $ucm_message->update('data', json_encode($_POST));
                                             $ucm_message->update('user_id', get_current_user_id());
                                             // do we send this one now? or schedule it later.
                                             $ucm_message->update('shub_status', _shub_MESSAGE_STATUS_PENDINGSEND);
                                             if (isset($options['send_time']) && !empty($options['send_time'])) {
                                                 // schedule for sending at a different time (now or in the past)
                                                 $ucm_message->update('last_active', $options['send_time']);
                                             } else {
                                                 // send it now.
                                                 $ucm_message->update('last_active', 0);
                                             }
                                             if (isset($_FILES['ucm_picture']['tmp_name']) && is_uploaded_file($_FILES['ucm_picture']['tmp_name'])) {
                                                 $ucm_message->add_attachment($_FILES['ucm_picture']['tmp_name']);
                                             }
                                             $now = time();
                                             if (!$ucm_message->get('last_active') || $ucm_message->get('last_active') <= $now) {
                                                 // send now! otherwise we wait for cron job..
                                                 if ($ucm_message->send_queued(isset($_POST['debug']) && $_POST['debug'])) {
                                                     $message_count++;
                                                 }
                                             } else {
                                                 $message_count++;
                                                 if (isset($_POST['debug']) && $_POST['debug']) {
                                                     echo "message will be sent in cron job after " . shub_print_date($ucm_message->get('last_active'), true);
                                                 }
                                             }
                                         } else {
                                             // log error?
                                         }
                                 }
                             }
                         }
                     }
                 }
             }
             return $message_count;
             break;
     }
     parent::handle_process($process, $options);
 }
    public function load_latest_item_data($debug = false)
    {
        // serialise this result into ucm_data.
        if (!$this->account) {
            echo 'No ucm account linked, please try again';
            return;
        }
        $api = $this->account->get_api();
        $ucm_product_id = $this->get('network_key');
        if (!$ucm_product_id) {
            echo 'No ucm product id found';
            return;
        }
        // we keep a record of the last message received so we know where to stop checking the feed
        $last_message_received = (int) $this->get('last_message');
        // dont want to import ALL tickets, so we pick a 20 day limit if we haven't done this yet
        if (!$last_message_received) {
            $last_message_received = strtotime('-20 days');
        }
        //        $last_message_received = false;
        SupportHub::getInstance()->log_data(_SUPPORT_HUB_LOG_INFO, 'ucm', 'Loading latest tickets for product (' . $ucm_product_id . ') "' . $this->get('product_name') . '" modified since ' . shub_print_date($last_message_received, true));
        // find any messages from this particular UCM product that have been updated since our last scrape time.
        $tickets = $api->api('ticket', 'list', array('search' => array('faq_product_id' => $ucm_product_id, 'time_from' => $last_message_received, 'status_id' => 0)));
        if ($debug) {
            echo "Getting the latest tickets for product: " . $ucm_product_id . " (last message in database is from " . shub_print_date($last_message_received, true) . ")<br>\n";
        }
        $newest_message_received = 0;
        $count = 0;
        if (isset($tickets['reply_options'])) {
            $this->account->save_account_data(array('reply_options' => $tickets['reply_options']));
        }
        if (isset($tickets['tickets'])) {
            foreach ($tickets['tickets'] as $ticket) {
                $message_time = $ticket['last_message_timestamp'];
                $newest_message_received = max($newest_message_received, $message_time);
                //if($message_time <= $last_message_received)break; // all done here.
                $ucm_message = new shub_ucm_message($this->account, $this, false);
                $ucm_message->load_by_network_key($ticket['ticket_id'], $ticket, 'ticket', $debug);
                $count++;
                if ($debug) {
                    ?>
                    <div>
                        <pre> Imported Ticket ID: <?php 
                    echo $ucm_message->get('network_key');
                    ?>
                            with <?php 
                    echo $ticket['message_count'];
                    ?>
 message. </pre>
                    </div>
                    <?php 
                }
            }
        } else {
            SupportHub::getInstance()->log_data(_SUPPORT_HUB_LOG_ERROR, 'ucm', 'Failed to get a reply from the API for product ' . $ucm_product_id . ' "', $tickets);
        }
        // get user, return envato_codes in meta
        SupportHub::getInstance()->log_data(_SUPPORT_HUB_LOG_INFO, 'ucm', 'Imported  ' . $count . ' product tickets into database (from a total of ' . count($tickets['tickets']) . ' returned by the api)');
        if ($debug) {
            echo " imported {$count} new product tickets <br>";
        }
        $this->update('last_message', $newest_message_received);
        $this->update('last_checked', time());
    }