Plugin Directory

Changeset 959466


Ignore:
Timestamp:
08/03/2014 08:55:03 AM (12 years ago)
Author:
showi
Message:

New attributes (round, dump), Using Field class everywhere

Location:
wp-custom-field-chart/trunk
Files:
1 deleted
4 edited

Legend:

Unmodified
Added
Removed
  • wp-custom-field-chart/trunk/ChartJs.php

    r959428 r959466  
    4040    }
    4141
    42     function gen_html($attr, $data, $options)
     42    function gen_html($fields, $data, $options)
    4343    {
    44         return '<div class="' . $attr['class'] . '">' .
    45             $this->gen_canvas($attr) .
    46             $this->gen_script($attr, $data, $options) . '</div>';
     44        return '<div class="' . $fields->getValue('class') . '">' .
     45            $this->gen_canvas($fields) .
     46            $this->gen_script($fields, $data, $options) . '</div>';
    4747    }
    4848
    49     function gen_canvas($attr)
     49    function gen_canvas($fields)
    5050    {
    5151        $out = '<canvas id="' . $this->sid . '" ';
     
    5454            'height'
    5555        ) as $key) {
    56             $out .= $key . '="' . $attr[$key] . '" ';
     56            $out .= $key . '="' . $fields->getValue($key) . '" ';
    5757        }
    5858        $out .= "/>\n";
     
    6060    }
    6161
    62     function gen_script($attr, $data, $options = Null)
     62    function gen_script($fields, $data, $options = Null)
    6363    {
    6464        $vadata = 'null';
    6565        $varopt = 'null';
    66         if (key_exists('js_data', $attr)) {
    67             $vardata = $attr['js_data'];
     66        if (!$fields->isNull('js_data')) {
     67            $vardata = $fields->getValue('js_data');
    6868        }
    69         if (key_exists('js_options', $attr)) {
    70             $varopt = $attr['js_options'];
     69        if (!$fields->isNull('js_options')) {
     70            $varopt = $fields->getValue('js_options');
    7171        }
    7272        $varobj = $this->sid . 'Object';
    7373        $out = "<script>\n";
    7474        $out .= 'jQuery(window).load(function() {' . "\n";
    75         $out .= $this->gen_data($attr, $data);
     75        $out .= $this->gen_data($fields, $data);
    7676
    7777        $out .= "var ctx = document.getElementById(\"" .
    7878            $this->sid . "\").getContext(\"2d\");\n";
    79         $out .= "var $varobj = new Chart(ctx)." . $attr['kind'] .
     79        $out .= "var $varobj = new Chart(ctx)." . $fields->getValue('kind') .
    8080            "($vardata, $varopt);\n";
    81         if (key_exists('js_hook', $attr)) {
    82             $out .= $attr['js_hook'] . "($varobj);\n";
    83         }
    8481        $out .= "});</script>\n";
    8582        return $out;
    8683    }
    8784
    88     function gen_data($attr, $data)
     85    function gen_data($fields, $data)
    8986    {
    90         $vardata = $attr['js_data'];
    91         $fields = split(',', $attr['fields']);
     87        $vardata = $fields->getValue('js_data');
     88        $keys = $fields->getValue('fields');
    9289        $out = $vardata . ".labels=[" .
    9390            join(',',
     
    9592                $data['labels'])) .
    9693            "];\n";
    97         foreach ($fields as $idx => $name) {
     94        foreach ($keys as $idx => $name) {
    9895            $out .= $vardata . ".datasets[$idx].data=[";
    9996            foreach ($data['datasets'][$idx] as $key => $value) {
     
    105102    }
    106103
    107     function gen_options($attr, $options = Null)
     104    function gen_options($fields, $options = Null)
    108105    {
    109         if (is_null($options) || $options == '') {
     106        if ($fields->isNull('js_options')) {
    110107            return '';
    111108        }
    112         $vardata = $attr['js_options'];
     109        $vardata = $fields->getValue('js_options');
    113110        return "var $vardata=$options;\n";
    114111    }
  • wp-custom-field-chart/trunk/Field.php

    r959428 r959466  
    3737    }
    3838
     39    function getValue($name) {
     40        return $this->get($name)->getValue();
     41    }
     42
     43    function isNull($name) {
     44        return $this->get($name)->isNull();
     45    }
     46
    3947    function getPool() {
    4048        return $this->pool;
     
    6573    }
    6674
     75    function getValue() {
     76        return $this->value;
     77    }
     78
     79    function isNull() {
     80        return is_null($this->value);
     81    }
     82
    6783    function validate($value)
    6884    {
     
    8197            $value = $this->default;
    8298        }
     99        if (!is_null($this->match)) {
     100            if (!preg_match('/' . $this->match . '/i', $value)) {
     101                throw new ErrorInvalidValue($this->name);
     102            }
     103        }
    83104        $cb = $this->callback;
    84105        if (!is_null($cb)) {
     
    86107        }
    87108        $this->value = $value;
    88         if (!is_null($this->match)) {
    89             if (!preg_match('/' . $this->match . '/i', $value)) {
    90                 throw new ErrorInvalidValue($this->name);
    91             }
    92         }
    93109        $this->is_valid = True;
    94110        return $value;
  • wp-custom-field-chart/trunk/readme.txt

    r959434 r959466  
    55Requires at least: 3.9.1
    66Tested up to: 3.9.1
    7 Stable tag: 0.0.3
     7Stable tag: 0.0.4
    88License: GPLv2 or later
    99License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    3636
    3737== Changelog ==
     38= 0.0.4 =
     39* Add *round* and *dump* attribute
     40* Using *Field* class everywhere
    3841= 0.0.3 =
    3942* More attribute validation and default
     
    118121= Notes =
    1191221. js_data and js_options must reflect name given to your javascript variable.
    120 2. Look as http://chartjs.org/ for documentation
     1232. Look at http://chartjs.org/ for documentation
    1211243. You don't need to supply labels and data (like in chartjs.org example) :)
    122125
    123126
    124 == Tag attribute ==
     127== Tag attributes ==
    125128= Required =
    1261291. *fields*: Custom field separate by comma
     
    1371401. *to_date*: Current date by default, post date if 'post' specified else value supplied
    1381411. *dump*: Dumping attributes for debug (default: False)
     1421. *round*: Rounding data with specified precision
    139143
    140144== Note ==
  • wp-custom-field-chart/trunk/wp-custom-field-chart.php

    r959430 r959466  
    44Plugin URI: http://wordpress.org/extend/plugins/wp-custom-field-chart
    55Description: Add Chart.js and graphs to your WordPress site based on tallies of any numeric custom field over time. Visualize progress toward any goal by day, week, month, or year.
    6 Version: 0.0.3
     6Version: 0.0.4
    77Author: Joachim Basmaison
    88Note: Based on Dylan Kuhn Tally wordpress plugin (GPLv2)
     
    5151        'Javascript variable holding our chart options'),
    5252    new Field('fields', true, Null, '^\w[\w\d,_-]+$',
    53         'Comma separated wordpress custom field that we want to plot'),
     53        'Comma separated wordpress custom field that we want to plot', function($v) {
     54        return array_map(trim, explode(',', $v));
     55    }),
    5456    new Field('method', true, 'track', '^(track|cumulative|delta)',
    5557        'Aggregation method: track, cumulative, delta'),
     
    6062        'Class used for our HTML div element'),
    6163    new Field('to_date', false),
    62     new Field('dump', false, false, null, 'Dumping attributes',
    63         function($e) {
    64             $e = strtolower($e);
    65             if ($e == 'true' || $e == '1') {
    66                 return True;
    67             }
    68             return False;
    69         }
    70     ),
     64    new Field('dump', false, false, null, 'Dumping attributes', function($e) {
     65        $e = strtolower($e);
     66        if ($e == 'true' || $e == '1') {
     67            return True;
     68        }
     69        return False;
     70    }),
     71    new Field('round', false, null, '^\d+$',
     72        'Rounding data with specified precision', function($e) {
     73        return (int)$e;
     74    })
    7175));
    7276
     
    7680function custom_field_chart($atts) {
    7781    global $CFC_FIELDS;
    78     $atts = wp_parse_args($atts);
    7982    $chartJs = new WpCustomFieldChart\ChartJs();
    8083    add_action('wp_enqueue_script', $chartJs->enqueue_script());
    8184    try {
    82         cfc_validate_attributes($atts);
     85        cfc_validate_attributes(wp_parse_args($atts));
    8386    } catch (WpCustomFieldChart\ErrorMissingAttribute $e) {
    8487        return cfc_get_field_error($e);
     
    8689        return cfc_get_field_error($e);
    8790    }
    88     $data =  cfc_collect_data($atts);
    89     return $chartJs->gen_html($atts, $data, $options);
     91    $data =  cfc_collect_data($CFC_FIELDS);
     92    return $chartJs->gen_html($CFC_FIELDS, $data, $options);
    9093}
    9194
     
    130133}
    131134
    132 function cfc_collect_data($atts)
     135function cfc_collect_data($fields)
    133136{
    134137    global $wp_query;
    135     $keys = split(',', $atts['fields']);
     138
    136139    $end_time = time();
    137     if (isset($atts['to_date'])) {
    138         if ($atts['to_date'] == 'post') {
     140    if (!$fields->isNull('to_date')) {
     141        $to_date = $fields->getValue('to_date');
     142        if ($to_date == 'post') {
    139143            $end_time = strtotime($wp_query->posts[0]->post_date);
    140144        } else {
    141145            $end_time = strtotime($atts['to_date']);
    142146        }
    143         if (! $end_time) {
    144             return 'Tally Graph: couldn\'t read the to_date ' . $atts['to_date'];
    145         }
    146     }
    147     if (isset($atts['interval'])) {
    148         $cfc_interval = $atts['interval'];
    149     } else {
    150         $cfc_interval = 'month';
    151     }
    152     if (isset($atts['label_interval'])) {
    153         $label_interval = $atts['label_interval'];
    154     } else {
    155         $label_interval = $cfc_interval;
    156     }
    157     $method = CFC_GRAPH_CUMULATIVE_METHOD;
    158     if (isset($atts['method'])) {
    159         $method = $atts['method'];
    160         if (! in_array($method, array(
    161             CFC_GRAPH_CUMULATIVE_METHOD,
    162             CFC_GRAPH_DELTA_METHOD,
    163             CFC_GRAPH_TRACK_METHOD
    164         ))) {
    165             return 'Tally Graph: Unknown method "' . $method . '"';
    166         }
    167     }
    168     $interval_count = $atts['interval_count'];
    169 
     147        if (!$end_time) {
     148            return 'WpCustomFieldChart: couldn\'t read the to_date: $to_date';
     149        }
     150    }
     151    $cfc_interval = $fields->getValue('interval');
     152    $label_interval = $cfc_interval;
     153    $method = $fields->getValue('method');
     154    $interval_count = $fields->getValue('interval_count');
     155    $keys = $fields->getValue('fields');
    170156    list ($index_gnu_format, $index_mysql_format, $first_day_suffix) =
    171157        cfc_graph_interval_settings($cfc_interval);
    172     // Always start on the first day of the starting interval
    173     // Always end on the first day after the ending interval
     158    /*
     159     * Always start on the first day of the starting interval
     160     * Always end on the first day after the ending interval
     161     */
    174162    $start_time = strtotime('-' . $interval_count . ' ' . $cfc_interval,
    175163        $end_time);
     
    188176        $end_time = strtotime($next_date_prefix . $first_day_suffix);
    189177    }
    190 
    191     // Tally ho
    192178    $key_counts = array();
    193179    $key_labels = array();
     
    195181        $key_counts[$index] = cfc_graph_get_counts($key, $start_time,
    196182            $end_time, $cfc_interval, $method, $key_labels);
    197     }
    198 
     183            if (!$fields->isNull('round')) {
     184                $round = $fields->getValue('round');
     185                foreach($key_counts[$index] as $idx => $value) {
     186                    $key_counts[$index][$idx] = round($value, $round);
     187                }
     188            }
     189    }
    199190    $first_index = null;
    200191    $label_array = Array();
Note: See TracChangeset for help on using the changeset viewer.