示例#1
0
    private function render_xref()
    {
        $name_id = \helpers\html\html::generate_id($this->name);
        // create if the header exists
        if (!empty($this->headers)) {
            // generate the header
            $html = <<<HEADER
\t\t\t<tr class="header_row">
\t\t\t\t<th id="{$name_id}_{$name_id}">{$this->name}</th>
HEADER;
            foreach ($this->headers as $header) {
                $header_id = \helpers\html\html::generate_id($header);
                $html .= <<<HEADER
\t\t\t\t<th id="{$name_id}_{$header_id}">{$header_id}</th>
HEADER;
            }
            $html .= '</tr>';
            // generate the body of the table
            // this bit checks that only a single dimension array is used for the data of this type of table
            $values = array_values($this->data);
            if (!is_array(reset($values))) {
                foreach ($this->data as $heading => $value) {
                    $html .= <<<DATA
\t\t\t\t\t<tr>
\t\t\t\t\t\t<td headers="{$name_id}_{$name_id}">{$heading}</td>
DATA;
                    // run through each header and output the right bits for each one
                    foreach ($this->headers as $header) {
                        if (strcasecmp($header, $value) === 0) {
                            $header_id = \helpers\html\html::generate_id($header);
                            $html .= <<<DATA
\t\t\t\t\t\t\t<td headers="{$name_id}_{$header_id}">{$this->xref_x}</td>
DATA;
                        } else {
                            $html .= '<td></td>';
                        }
                    }
                    $html .= '</tr>';
                }
            }
            return $html;
        }
        return '';
    }
示例#2
0
 /**
  * a static method to build a list of select list options using a template and return that list as an html string
  * @param array $options a list of values for the select list
  * @param string $element_name the name of the select list element - used to determine if this should be marked as selected in the rendered html or not (e.g. for a form posted with errors)
  * @return string
  */
 public static function build_select_options($options, $element_name, $snippets_dir)
 {
     $html = '';
     foreach ($options as $option) {
         $selected = isset($_REQUEST[$element_name]) && $_REQUEST[$element_name] == $option ? 'selected="selected"' : '';
         if ($snippets_dir && file_exists("{$snippets_dir}/input_option.php")) {
             $snippet = "{$snippets_dir}/input_option.php";
         } else {
             $snippet = __DIR__ . "/snippets/input_option.php";
         }
         $html .= \helpers\html\html::load_snippet($snippet, array('value' => $option, 'display_value' => $option, 'selected' => $selected));
     }
     return $html;
 }
示例#3
0
 /**
  * use object buffering to build up the views into a single string and either return or echo it
  * optionally output any headers that have been added to the view instance
  * @param bool $echo whether to echo the view or return it as a string
  * @param bool $with_headers if set to true and $echo is also set to true, then send headers to the browser, otherwise do nothing
  * @return string
  */
 public static function render($echo = true, $with_headers = false)
 {
     $v = view::getInstance();
     $app = \maverick\maverick::getInstance();
     $view_file_path = MAVERICK_VIEWSDIR . "/{$v->view}.php";
     if (!file_exists($view_file_path)) {
         error::show("View '{$v->view}' does not exist");
     } else {
         ob_start();
         include $view_file_path;
         $view = ob_get_contents();
         ob_end_clean();
         if ($app->get_config('config.view_parsing') !== false) {
             $view = $v->parse_view($view);
         }
         // this just stores the view if the config value is set to cache it
         if ($app->get_config('cache.on') !== false) {
             $hash = $app->get_request_route_hash();
             \maverick\cache::store($hash, $view);
             if ($with_headers) {
                 \maverick\cache::store("{$hash}_headers", json_encode($v->headers));
             }
         }
         if ($with_headers) {
             foreach ($v->headers as $header) {
                 header($header);
             }
             // teapot
             if (isset($v->original_headers['status']) && $v->original_headers['status'] == 418 && $app->get_config('config.teapot') !== false) {
                 $teapot = \helpers\html\html::load_snippet(\MAVERICK_BASEDIR . 'vendor/maverick/teapot.html', array());
                 $view = preg_replace('/<body/', "<body>{$teapot}", $view, 1);
             }
         }
         if ($echo) {
             echo $view;
         } else {
             return $view;
         }
     }
 }
    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;
        }
    }
示例#5
0
 /**
  * deals with the creation of action buttons (links) used throughout the CMS to do something
  * @param string $section the section, as all links will contain this in their URL
  * @param int $id the ID of the object being worked on
  * @param array $actions a basic single dimensional array of single-word actions, that go into the URL and the text of the link
  * @param string $extra_classes a string of extra classes that should be added to each button
  * @param string $type the type of element to use, either a button or a link
  * @return string
  */
 static function generate_actions($section, $id, $actions = array(), $extra_classes = '', $type = 'link')
 {
     if (empty($actions) || empty($section)) {
         return '';
     }
     $app = \maverick\maverick::getInstance();
     $type = in_array($type, array('link', 'button')) ? $type : 'link';
     $actions_html = '';
     foreach ($actions as $action) {
         $replacements = array('href' => str_replace(' ', '_', "/{$app->get_config('tweed.admin_path')}/{$section}/{$action}/{$id}"), 'action' => $action, 'id' => $id, 'section' => $section, 'class' => str_replace(' ', '_', $action) . " {$extra_classes}");
         $actions_html .= \helpers\html\html::load_snippet(MAVERICK_VIEWSDIR . "includes/snippets/action_{$type}.php", $replacements);
     }
     return $actions_html;
 }