Exemple #1
0
    public function handle_ajax($action, $simple_social_inbox_wp)
    {
        switch ($action) {
            case 'send-message-reply':
                if (!headers_sent()) {
                    header('Content-type: text/javascript');
                }
                if (isset($_REQUEST['social_twitter_id']) && !empty($_REQUEST['social_twitter_id']) && isset($_REQUEST['id']) && (int) $_REQUEST['id'] > 0) {
                    $ucm_twitter = new ucm_twitter_account($_REQUEST['social_twitter_id']);
                    if ($ucm_twitter->get('social_twitter_id') == $_REQUEST['social_twitter_id']) {
                        $ucm_twitter_message = new ucm_twitter_message($ucm_twitter, $_REQUEST['id']);
                        if ($ucm_twitter_message->get('social_twitter_message_id') == $_REQUEST['id']) {
                            $return = array();
                            $message = isset($_POST['message']) && $_POST['message'] ? $_POST['message'] : '';
                            $debug = isset($_POST['debug']) && $_POST['debug'] ? $_POST['debug'] : false;
                            if ($message) {
                                ob_start();
                                //$twitter_message->send_reply( $message, $debug );
                                $new_twitter_message = new ucm_twitter_message($ucm_twitter, false);
                                $new_twitter_message->create_new();
                                $new_twitter_message->update('reply_to_id', $ucm_twitter_message->get('social_twitter_message_id'));
                                $new_twitter_message->update('social_twitter_id', $ucm_twitter->get('social_twitter_id'));
                                $new_twitter_message->update('summary', $message);
                                //$new_twitter_message->update('type','pending');
                                $new_twitter_message->update('data', json_encode($_POST));
                                $new_twitter_message->update('user_id', get_current_user_id());
                                // do we send this one now? or schedule it later.
                                $new_twitter_message->update('status', _SOCIAL_MESSAGE_STATUS_PENDINGSEND);
                                if (isset($_FILES['picture']['tmp_name']) && is_uploaded_file($_FILES['picture']['tmp_name'])) {
                                    $new_twitter_message->add_attachment($_FILES['picture']['tmp_name']);
                                }
                                $worked = $new_twitter_message->send_queued(isset($_POST['debug']) && $_POST['debug']);
                                $return['message'] = ob_get_clean();
                                if ($debug) {
                                    // just return message
                                } else {
                                    if ($worked) {
                                        // success, redicet!
                                        //set_message( _l( 'Message sent and conversation archived.' ) );
                                        //$return['redirect'] = module_social_twitter::link_open_message_view( $social_twitter_id );
                                        $return['redirect'] = 'admin.php?page=simple_social_inbox_main';
                                        //$return['success'] = 1;
                                    } else {
                                        // failed, no debug, force debug and show error.
                                    }
                                }
                            }
                            echo json_encode($return);
                        }
                    }
                }
                break;
            case 'modal':
                if (isset($_REQUEST['socialtwittermessageid']) && (int) $_REQUEST['socialtwittermessageid'] > 0) {
                    $ucm_twitter_message = new ucm_twitter_message(false, $_REQUEST['socialtwittermessageid']);
                    if ($ucm_twitter_message->get('social_twitter_message_id') == $_REQUEST['socialtwittermessageid']) {
                        $social_twitter_id = $ucm_twitter_message->get('twitter_account')->get('social_twitter_id');
                        $social_twitter_message_id = $ucm_twitter_message->get('social_twitter_message_id');
                        include trailingslashit($simple_social_inbox_wp->dir) . 'pages/twitter_message.php';
                    }
                }
                break;
            case 'set-answered':
                if (!headers_sent()) {
                    header('Content-type: text/javascript');
                }
                if (isset($_REQUEST['social_twitter_message_id']) && (int) $_REQUEST['social_twitter_message_id'] > 0) {
                    $ucm_twitter_message = new ucm_twitter_message(false, $_REQUEST['social_twitter_message_id']);
                    if ($ucm_twitter_message->get('social_twitter_message_id') == $_REQUEST['social_twitter_message_id']) {
                        $ucm_twitter_message->update('status', _SOCIAL_MESSAGE_STATUS_ANSWERED);
                        ?>
						jQuery('.socialtwitter_message_action[data-id=<?php 
                        echo (int) $ucm_twitter_message->get('social_twitter_message_id');
                        ?>
]').parents('tr').first().hide();
						<?php 
                        // if this is a direct message, we also archive all other messages in it.
                        if ($ucm_twitter_message->get('type') == _TWITTER_MESSAGE_TYPE_DIRECT) {
                            $from = preg_replace('#[^0-9]#', '', $ucm_twitter_message->get('twitter_from_id'));
                            $to = preg_replace('#[^0-9]#', '', $ucm_twitter_message->get('twitter_to_id'));
                            if ($from && $to) {
                                $sql = "SELECT * FROM `" . _SIMPLE_SOCIAL_DB_PREFIX . "social_twitter_message` WHERE `type` = " . _TWITTER_MESSAGE_TYPE_DIRECT . " AND `status` = " . (int) _SOCIAL_MESSAGE_STATUS_UNANSWERED . " AND social_twitter_id = " . (int) $ucm_twitter_message->get('twitter_account')->get('social_twitter_id') . " AND ( (`twitter_from_id` = '{$from}' AND `twitter_to_id` = '{$to}') OR (`twitter_from_id` = '{$to}' AND `twitter_to_id` = '{$from}') ) ";
                                global $wpdb;
                                $others = $wpdb->get_results($sql, ARRAY_A);
                                if (count($others)) {
                                    foreach ($others as $other_message) {
                                        $ucm_twitter_message = new ucm_twitter_message(false, $other_message['social_twitter_message_id']);
                                        if ($ucm_twitter_message->get('social_twitter_message_id') == $other_message['social_twitter_message_id']) {
                                            $ucm_twitter_message->update('status', _SOCIAL_MESSAGE_STATUS_ANSWERED);
                                            ?>
											jQuery('.socialtwitter_message_action[data-id=<?php 
                                            echo (int) $ucm_twitter_message->get('social_twitter_message_id');
                                            ?>
]').parents('tr').first().hide();
										<?php 
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                break;
            case 'set-unanswered':
                if (!headers_sent()) {
                    header('Content-type: text/javascript');
                }
                if (isset($_REQUEST['social_twitter_message_id']) && (int) $_REQUEST['social_twitter_message_id'] > 0) {
                    $ucm_twitter_message = new ucm_twitter_message(false, $_REQUEST['social_twitter_message_id']);
                    if ($ucm_twitter_message->get('social_twitter_message_id') == $_REQUEST['social_twitter_message_id']) {
                        $ucm_twitter_message->update('status', _SOCIAL_MESSAGE_STATUS_UNANSWERED);
                        ?>
						jQuery('.socialtwitter_message_action[data-id=<?php 
                        echo (int) $ucm_twitter_message->get('social_twitter_message_id');
                        ?>
]').parents('tr').first().hide();
						<?php 
                    }
                }
                break;
        }
        return false;
    }
    public function process()
    {
        if ("save_twitter" == $_REQUEST['_process']) {
            $social_twitter_id = isset($_REQUEST['social_twitter_id']) ? (int) $_REQUEST['social_twitter_id'] : 0;
            $twitter = new ucm_twitter_account($social_twitter_id);
            if (isset($_POST['butt_del']) && module_social::can_i('delete', 'Twitter', 'Social', 'social')) {
                if (module_form::confirm_delete('social_twitter_id', "Really delete this Twitter account from the system? All messages will be lost.", self::link_open($_REQUEST['social_twitter_id']))) {
                    $twitter->delete();
                    set_message("Twitter account deleted successfully");
                    redirect_browser(self::link_open(false));
                }
            }
            $twitter->save_data($_POST);
            $social_twitter_id = $twitter->get('social_twitter_id');
            if (isset($_POST['butt_save_connect'])) {
                $redirect = $this->link_open($social_twitter_id, false, false, 'twitter_account_connect');
            } else {
                set_message('Twitter account saved successfully');
                $redirect = $this->link_open($social_twitter_id);
            }
            redirect_browser($redirect);
            exit;
        } else {
            if ("send_twitter_message" == $_REQUEST['_process']) {
                if (module_form::check_secure_key()) {
                    // queue the message into the twitter_message table
                    // if there's a scheduled date in the past we send it in the past, no date we send straight away, date in the future we leave it in the db table for the cron job to pick up.
                    //print_r($_POST);exit;
                    $send_time = false;
                    // default: now
                    if (isset($_POST['schedule_date']) && isset($_POST['schedule_time']) && !empty($_POST['schedule_date']) && !empty($_POST['schedule_time'])) {
                        $date = $_POST['schedule_date'];
                        $time_hack = $_POST['schedule_time'];
                        $time_hack = str_ireplace('am', '', $time_hack);
                        $time_hack = str_ireplace('pm', '', $time_hack);
                        $bits = explode(':', $time_hack);
                        if (strpos($_POST['schedule_time'], 'pm')) {
                            $bits[0] += 12;
                        }
                        // add the time if it exists
                        $date .= ' ' . implode(':', $bits) . ':00';
                        $send_time = strtotime(input_date($date, true));
                    } else {
                        if (isset($_POST['schedule_date']) && !empty($_POST['schedule_date'])) {
                            $send_time = strtotime(input_date($_POST['schedule_date'], true));
                        }
                    }
                    //echo print_date($send_time,true);
                    //echo '<br>';
                    //echo date('c',$send_time);
                    //exit;
                    $send_accounts = isset($_POST['compose_account_id']) && is_array($_POST['compose_account_id']) ? $_POST['compose_account_id'] : array();
                    $page_count = 0;
                    $last_twitter_account_id = false;
                    if ($send_accounts) {
                        foreach ($send_accounts as $twitter_account_id => $tf) {
                            if (!$tf) {
                                continue;
                            }
                            // see if this is an available account.
                            $twitter_account = new ucm_twitter_account($twitter_account_id);
                            //todo: check permissiont o access thi saccount
                            if ($twitter_account->get('social_twitter_id') == $twitter_account_id) {
                                // push to db! then send.
                                $last_twitter_account_id = $twitter_account_id;
                                $twitter_message = new ucm_twitter_message($twitter_account, false);
                                $twitter_message->create_new();
                                $twitter_message->update('social_twitter_id', $twitter_account->get('social_twitter_id'));
                                $twitter_message->update('summary', isset($_POST['message']) ? $_POST['message'] : '');
                                $twitter_message->update('type', 'pending');
                                $twitter_message->update('data', json_encode($_POST));
                                $twitter_message->update('user_id', module_security::get_loggedin_id());
                                // do we send this one now? or schedule it later.
                                $twitter_message->update('status', _SOCIAL_MESSAGE_STATUS_PENDINGSEND);
                                if ($send_time) {
                                    // schedule for sending at a different time (now or in the past)
                                    $twitter_message->update('message_time', $send_time);
                                } else {
                                    // send it now.
                                    $twitter_message->update('message_time', 0);
                                }
                                if (isset($_FILES['picture']['tmp_name']) && is_uploaded_file($_FILES['picture']['tmp_name'])) {
                                    $twitter_message->add_attachment($_FILES['picture']['tmp_name']);
                                }
                                $twitter_message->send_queued(isset($_POST['debug']) && $_POST['debug']);
                                $page_count++;
                            } else {
                                // log error?
                            }
                        }
                    }
                    set_message(_l('Message delivered successfully to %s Twitter accounts', $page_count));
                    $redirect = $this->link_open_message_view($last_twitter_account_id);
                    redirect_browser($redirect);
                }
                exit;
            } else {
                if ("ajax_social_twitter" == $_REQUEST['_process']) {
                    // ajax functions from wdsocial. copied from the datafeed.php sample files.
                    header('Content-type: text/javascript');
                    if (module_form::check_secure_key()) {
                        $social_twitter_id = isset($_REQUEST['social_twitter_id']) ? (int) $_REQUEST['social_twitter_id'] : 0;
                        $twitter = new ucm_twitter_account($social_twitter_id);
                        if ($social_twitter_id && $twitter->get('social_twitter_id') == $social_twitter_id) {
                            $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : false;
                            $message_id = isset($_REQUEST['social_twitter_message_id']) ? (int) $_REQUEST['social_twitter_message_id'] : 0;
                            $twitter_message = new ucm_twitter_message();
                            $twitter_message->load($message_id);
                            if ($twitter_message->get('social_twitter_id') == $social_twitter_id && $twitter_message->get('social_twitter_message_id') == $message_id) {
                                switch ($action) {
                                    case "send-message-reply":
                                        if (module_social::can_i('create', 'Twitter Comments', 'Social', 'social')) {
                                            $return = array();
                                            $message = isset($_POST['message']) && $_POST['message'] ? $_POST['message'] : '';
                                            $debug = isset($_POST['debug']) && $_POST['debug'] ? $_POST['debug'] : false;
                                            if ($message) {
                                                ob_start();
                                                //$twitter_message->send_reply( $message, $debug );
                                                $new_twitter_message = new ucm_twitter_message($twitter, false);
                                                $new_twitter_message->create_new();
                                                $new_twitter_message->update('reply_to_id', $twitter_message->get('social_twitter_message_id'));
                                                $new_twitter_message->update('social_twitter_id', $twitter->get('social_twitter_id'));
                                                $new_twitter_message->update('summary', $message);
                                                //$new_twitter_message->update('type','pending');
                                                $new_twitter_message->update('data', json_encode($_POST));
                                                $new_twitter_message->update('user_id', module_security::get_loggedin_id());
                                                // do we send this one now? or schedule it later.
                                                $new_twitter_message->update('status', _SOCIAL_MESSAGE_STATUS_PENDINGSEND);
                                                if (isset($_FILES['picture']['tmp_name']) && is_uploaded_file($_FILES['picture']['tmp_name'])) {
                                                    $new_twitter_message->add_attachment($_FILES['picture']['tmp_name']);
                                                }
                                                $worked = $new_twitter_message->send_queued(isset($_POST['debug']) && $_POST['debug']);
                                                $return['message'] = ob_get_clean();
                                                if ($debug) {
                                                    // just return message
                                                } else {
                                                    if ($worked) {
                                                        // success, redicet!
                                                        set_message(_l('Message sent and conversation archived.'));
                                                        $return['redirect'] = module_social_twitter::link_open_message_view($social_twitter_id);
                                                    } else {
                                                        // failed, no debug, force debug and show error.
                                                    }
                                                }
                                            }
                                            echo json_encode($return);
                                        }
                                        break;
                                    case "set-answered":
                                        if (module_social::can_i('edit', 'Twitter Comments', 'Social', 'social')) {
                                            $twitter_message->update('status', _SOCIAL_MESSAGE_STATUS_ANSWERED);
                                            ?>

									$('.twitter_message_row[data-id=<?php 
                                            echo $message_id;
                                            ?>
]').hide();
									<?php 
                                            // if this is a direct message, we also archive all other messages in it.
                                            if ($twitter_message->get('type') == _TWITTER_MESSAGE_TYPE_DIRECT) {
                                                $from = preg_replace('#[^0-9]#', '', $twitter_message->get('twitter_from_id'));
                                                $to = preg_replace('#[^0-9]#', '', $twitter_message->get('twitter_to_id'));
                                                if ($from && $to) {
                                                    $sql = "SELECT * FROM `" . _DB_PREFIX . "social_twitter_message` WHERE `type` = " . _TWITTER_MESSAGE_TYPE_DIRECT . " AND `status` = " . (int) _SOCIAL_MESSAGE_STATUS_UNANSWERED . " AND social_twitter_id = " . (int) $twitter_message->get('twitter_account')->get('social_twitter_id') . " AND ( (`twitter_from_id` = '{$from}' AND `twitter_to_id` = '{$to}') OR (`twitter_from_id` = '{$to}' AND `twitter_to_id` = '{$from}') ) ";
                                                    $others = qa($sql);
                                                    if (count($others)) {
                                                        foreach ($others as $other_message) {
                                                            $ucm_twitter_message = new ucm_twitter_message(false, $other_message['social_twitter_message_id']);
                                                            if ($ucm_twitter_message->get('social_twitter_message_id') == $other_message['social_twitter_message_id']) {
                                                                $ucm_twitter_message->update('status', _SOCIAL_MESSAGE_STATUS_ANSWERED);
                                                                ?>

														$('.twitter_message_row[data-id=<?php 
                                                                echo $ucm_twitter_message->get('social_twitter_message_id');
                                                                ?>
]').hide();
													<?php 
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                        break;
                                    case "set-unanswered":
                                        if (module_social::can_i('edit', 'Twitter Comments', 'Social', 'social')) {
                                            $twitter_message->update('status', _SOCIAL_MESSAGE_STATUS_UNANSWERED);
                                            ?>

					                $('.twitter_message_row[data-id=<?php 
                                            echo $message_id;
                                            ?>
]').hide();
					                <?php 
                                        }
                                        break;
                                }
                                //echo 'The status is '.$twitter_message->get('status');
                            }
                        }
                    }
                    exit;
                }
            }
        }
    }