Ejemplo n.º 1
0
    function admin($params)
    {
        $params = $this->clean_params($params);
        if (!$this->check_login_status($params)) {
            view::redirect('/' . $this->app->get_config('tweed.admin_path') . '/login');
        }
        if (!count($params)) {
            $params[0] = 'dash';
        }
        switch ($params[0]) {
            case 'dash':
                $campaigns = content::get_all_campaigns();
                $headers = '["ID #","Name","Start","End","Last Used","Search Params","Created By","Modified By","Status","Actions"]';
                $data = array();
                foreach ($campaigns as $campaign) {
                    $campaign_actions = array('edit');
                    if ($campaign['force_deactivated'] == 'no' && strtotime($campaign['end']) > time()) {
                        $campaign_actions[] = 'deactivate';
                    }
                    if ($campaign['force_deactivated'] == 'yes' && strtotime($campaign['end']) > time()) {
                        $campaign_actions[] = 'reactivate';
                    }
                    $data[] = array($campaign['id'], $campaign['name'], $campaign['start'], $campaign['end'], $campaign['last_used'], $campaign['query_params'], $campaign['created_by'], $campaign['modified_by'], $campaign['force_deactivated'] == 'yes' ? '<span class="deactivated">deactivated</span>' : (strtotime($campaign['end']) < time() ? '<span class="ended">ended</span>' : '<span class="active">active</span>'), content::generate_actions('campaign', $campaign['id'], $campaign_actions));
                }
                $campaigns_table = new \helpers\html\tables('forms', 'layout', $data, $headers);
                $campaigns_table->class = 'item_table';
                $view_params = array('campaigns' => $campaigns_table->render(), 'campaigns_buttons' => content::generate_actions('campaign', '', array('new campaign'), 'full', 'a'), 'scripts' => array('/js/cms/forms.js' => 10));
                $this->load_view($params[0], $view_params);
                break;
            case 'campaign':
                if (!isset($params[1])) {
                    return false;
                }
                switch ($params[1]) {
                    case 'edit':
                        $errors = false;
                        if (isset($params[2]) && intval($params[2])) {
                            if (count($_REQUEST)) {
                                // validate what we can
                                $rules = array('name' => array('required'), 'url' => 'url', 'start' => array('required', 'regex:/\\d{4}-\\d\\d-\\d\\d/'), 'end' => array('required', 'regex:/\\d{4}-\\d\\d-\\d\\d/'), 'force_deactivated' => array('required', 'in:yes:no'));
                                validator::make($rules);
                                if (validator::run()) {
                                    // update a campaign
                                    content::update_campaign($params[2]);
                                } else {
                                    $errors = $this->get_all_errors_as_string(null, array('<span class="error">', '</span>'));
                                }
                            }
                            $campaign_buttons = content::generate_actions($params[1], $params[2], array('save', 'add query'), 'full', 'button');
                            $campaign = content::get_campaign($params[2]);
                            $campaign_html = '';
                            foreach ($campaign as $q) {
                                $campaign_html .= $q['html'];
                            }
                            // build up the extra elements specifically for the campaign details - not the queries
                            $campaign_details = \helpers\html\html::load_snippet(MAVERICK_BASEDIR . 'vendor/helpers/html/snippets/label_wrap.php', array('label' => 'Campaign Name', 'element' => \helpers\html\html::load_snippet(MAVERICK_BASEDIR . 'vendor/helpers/html/snippets/input_text.php', array('value' => "value=\"{$campaign[0]['name']}\"", 'placeholder' => "placeholder=\"campaign name\"", 'name' => 'name'))));
                            $campaign_details .= \helpers\html\html::load_snippet(MAVERICK_BASEDIR . 'vendor/helpers/html/snippets/label_wrap.php', array('label' => 'URL', 'element' => \helpers\html\html::load_snippet(MAVERICK_BASEDIR . 'vendor/helpers/html/snippets/input_text.php', array('value' => "value=\"{$campaign[0]['url']}\"", 'placeholder' => "placeholder=\"campaign url\"", 'name' => 'url'))));
                            $campaign_details .= \helpers\html\html::load_snippet(MAVERICK_BASEDIR . 'vendor/helpers/html/snippets/label_wrap.php', array('label' => 'Start', 'element' => \helpers\html\html::load_snippet(MAVERICK_BASEDIR . 'vendor/helpers/html/snippets/input_date.php', array('value' => "value=\"{$campaign[0]['start']}\"", 'placeholder' => "placeholder=\"start date\"", 'name' => 'start'))));
                            $campaign_details .= \helpers\html\html::load_snippet(MAVERICK_BASEDIR . 'vendor/helpers/html/snippets/label_wrap.php', array('label' => 'End', 'element' => \helpers\html\html::load_snippet(MAVERICK_BASEDIR . 'vendor/helpers/html/snippets/input_date.php', array('value' => "value=\"{$campaign[0]['end']}\"", 'placeholder' => "placeholder=\"end date\"", 'name' => 'end'))));
                            $deactivated = array();
                            foreach (array('yes', 'no') as $d) {
                                $deactivated[] = \helpers\html\html::load_snippet(MAVERICK_BASEDIR . 'vendor/helpers/html/snippets/input_option.php', array('selected' => $d == $campaign[0]['force_deactivated'] ? 'selected="selected"' : '', 'value' => $d, 'display_value' => $d));
                            }
                            $campaign_details .= \helpers\html\html::load_snippet(MAVERICK_BASEDIR . 'vendor/helpers/html/snippets/label_wrap.php', array('label' => 'Force Deactivated?', 'element' => \helpers\html\html::load_snippet(MAVERICK_BASEDIR . 'vendor/helpers/html/snippets/input_select.php', array('values' => implode('', $deactivated), 'name' => 'force_deactivated'))));
                            $view_params = array('campaign_fields' => $campaign_html, 'campaign_buttons' => $campaign_buttons, 'campaign_details' => $campaign_details, 'scripts' => array('/js/campaigns.js' => 10, 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js' => 5));
                            if ($errors) {
                                $view_params['errors'] = $errors;
                            }
                            $this->load_view('campaign_edit', $view_params);
                        } else {
                            return false;
                        }
                        // just bomb out if people are f*****g with the URL
                        break;
                    case 'new_campaign':
                        $campaign_id = content::create_new_campaign();
                        view::redirect('/' . $this->app->get_config('tweed.admin_path') . "/campaign/edit/{$campaign_id}");
                        break;
                    case 'deactivate':
                    case 'reactivate':
                        // just bomb out if people are f*****g with the URL
                        if (empty($params[2]) || !intval($params[2])) {
                            return false;
                        }
                        content::set_campaign_status($params[2], $params[1]);
                        view::redirect('/' . $this->app->get_config('tweed.admin_path'));
                        break;
                }
                break;
            case 'tweets':
                $errors = false;
                // todo: generate fetch parameters based on the passed in filter options
                $pagination_filter = array();
                foreach (array('campaign', 'lang', 'user', 'after') as $filter) {
                    if (!empty($_REQUEST[$filter])) {
                        ${$filter} = $_REQUEST[$filter];
                        $pagination_filter[] = "{$filter}=" . filter_var($_REQUEST[$filter], FILTER_SANITIZE_SPECIAL_CHARS);
                    } else {
                        ${$filter} = null;
                    }
                }
                $current_page = !empty($_REQUEST['page']) && intval($_REQUEST['page']) ? intval($_REQUEST['page']) : 1;
                // get the tweets (with any specified filter parameters if available)
                $tweets = content::get_tweets(null, $this->app->get_config('twitter.total'), $current_page, $lang, $user, null, null, null, null, null);
                $tweets_count = content::get_tweets_count(null, $lang, $user, null, null, null, null, null);
                // get the pagination string
                $pagination_links = $this->get_pagination('/' . $this->app->get_config('tweed.admin_path') . '/tweets', $current_page, $tweets_count, $pagination_filter);
                // create the table
                $headers = '["ID #","Campaign","Lang","User","Sent At","Retweet?","Reply?","Approved?","Content","Actions"]';
                $data = array();
                if (is_array($tweets)) {
                    foreach ($tweets as $tweet) {
                        $tweet_actions = array($tweet['approved'] == 'no' ? 'approve' : 'unapprove');
                        $data[] = array($tweet['id'], $tweet['campaign_name'], $tweet['iso_lang'], $tweet['user_screen_name'], date("l, jS M, Y", strtotime($tweet['created_at'])), intval($tweet['retweet_count']) ? 'yes' : 'no', intval($tweet['in_reply_to_id']) ? 'yes' : 'no', $tweet['approved'], $tweet['content'], content::generate_actions('tweets', $tweet['id'], $tweet_actions));
                    }
                } else {
                    $errors = "<span class=\"error\">{$tweets}</span>";
                }
                $tweets_table = new \helpers\html\tables('forms', 'layout', $data, $headers);
                $tweets_table->class = 'item_table tweets';
                // create the filter form
                // campaign list
                $campaign_list = content::get_all_campaigns();
                foreach ($campaign_list as &$c) {
                    $c = "\"{$c['name']}\"";
                }
                // language list
                $languages = content::get_languages();
                foreach ($languages as &$l) {
                    $l = "\"{$l['iso_lang']}\"";
                }
                $filter_form_elements = '{
					"campaign":{"type":"select","label":"Campaign","values":["",' . implode(',', $campaign_list) . ']},
					"lang":{"type":"select","label":"Language","values":["",' . implode(',', $languages) . ']},
					"user":{"type":"text","label":"User"},
					"after":{"type":"date","placeholder":"' . date("Y-m-d H:i:s") . '","label":"Sent after"},
					"submit":{"type":"submit","value":"Filter","class":"action filter full"}
				}';
                $filter_form = new \helpers\html\form('filter_form', $filter_form_elements);
                // set up the view
                $view_params = array('tweets_table' => $tweets_table->render(), 'filter_form' => $filter_form->render(), 'pagination' => $pagination_links, 'scripts' => array('/js/tweets.js' => 10));
                // add in any errors that might have been generated which should be shown to the user
                if ($errors) {
                    $view_params['errors'] = $errors;
                }
                $this->load_view('tweets', $view_params);
                break;
        }
    }