Plugin Directory

Changeset 959428


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

Better attributes default and validation, 'post' keyword for to_date attribute, dump attribute

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

Legend:

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

    r957730 r959428  
    6262    function gen_script($attr, $data, $options = Null)
    6363    {
    64         $vardata = $attr['js_data'];
    65         $varopt = '{}';
     64        $vadata = 'null';
     65        $varopt = 'null';
     66        if (key_exists('js_data', $attr)) {
     67            $vardata = $attr['js_data'];
     68        }
     69        if (key_exists('js_options', $attr)) {
     70            $varopt = $attr['js_options'];
     71        }
    6672        $varobj = $this->sid . 'Object';
    6773        $out = "<script>\n";
    6874        $out .= 'jQuery(window).load(function() {' . "\n";
    6975        $out .= $this->gen_data($attr, $data);
    70         if (key_exists('js_options', $attr)) {
    71             // out .= $this->gen_options($attr, $options);
    72             $varopt = $attr['js_options'];
    73         }
     76
    7477        $out .= "var ctx = document.getElementById(\"" .
    7578            $this->sid . "\").getContext(\"2d\");\n";
  • wp-custom-field-chart/trunk/Field.php

    r957730 r959428  
    55namespace WpCustomFieldChart;
    66
    7 class ErrorMissingAttribute extends \ErrorException
    8 {
     7class ErrorMissingAttribute extends \ErrorException {};
     8class ErrorInvalidValue extends \ErrorException {};
     9class ErrorAddingSameField extends \ErrorException {};
     10class ErrorNonExistingField extends \ErrorException {};
     11
     12class FieldArray {
     13    private $pool = array();
     14
     15    function __construct($fields=Null) {
     16           $this->populate($fields);
     17    }
     18
     19    function add(Field $field) {
     20        if (key_exists($field->name, $this->pool)) {
     21            throw new ErrorAddingSameField($field->name);
     22        }
     23        $this->pool[$field->name] = $field;
     24    }
     25
     26    function get($name) {
     27        if (!key_exists($name, $this->pool)) {
     28            throw new ErrorNonExistingField($name);
     29        }
     30        return $this->pool[$name];
     31    }
     32
     33    function populate($fields) {
     34        foreach($fields as $field) {
     35            $this->add($field);
     36        }
     37    }
     38
     39    function getPool() {
     40        return $this->pool;
     41    }
    942}
    10 ;
    1143
    1244class Field
     
    1951    public $info = Null;
    2052    public $callback = Null;
     53    public $value = Null;
     54    public $is_valid = False;
    2155
    2256    function __construct($name, $required, $default = Null, $match = Null,
     
    3367    function validate($value)
    3468    {
     69        $this->is_valid = False;
    3570        if ($value == '') {
    3671            $value = Null;
     72        }
     73        if (is_null($value) && !$this->required) {
     74            $this->value = Null;
     75            return Null;
    3776        }
    3877        if (is_null($value) && $this->required) {
     
    4079                throw new ErrorMissingAttribute($this->name);
    4180            }
    42             return $this->default;
     81            $value = $this->default;
    4382        }
    4483        $cb = $this->callback;
     
    4685            $value = $cb($value);
    4786        }
     87        $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        }
     93        $this->is_valid = True;
    4894        return $value;
    4995    }
    5096
    51     function make_error_message()
     97    function make_error_message($e)
    5298    {
     99        $msg = "";
     100        if ($e instanceof ErrorMissingAttribute) {
     101            $msg .= '<b>Missing attribute:</b>&nbsp;' . $this->name . '<br>';
     102            $msg .= '<b>Attribute information:</b>&nbsp;' . $this->info . '';
     103        } elseif ($e instanceof ErrorInvalidValue) {
     104            $msg .= '<b>Invalid value for '. $this->name . ':</b>&nbsp;' .
     105                $this->value . ' (regex: ' . $this->match . ')<br>';
     106            $msg .= '<b>Attribute information:</b>&nbsp;' . $this->info . '';
     107        } else {
     108            $msg .= "<b>Unknown error for attribute:</b>&nbsp;" . $this->name
     109                . '<br>';
     110            $msg .= '<b>Attribute information:</b>&nbsp;' . $this->info . '';
     111        }
    53112        $out = '<div class="cfc-error" style="background-color: black; ' .
    54113            'color:white; padding: 0.5em 1em; font-family: Arial; ' .
    55114            'border-style: solid; border-width:2px; border-color: red">';
    56115        $out .= '<h2>Wordpress Extension Error / Custom Field Chart</h2>';
    57         $out .= '<b>Missing attribute:</b>&nbsp;' . $this->name . '<br>';
    58         $out .= '<b>Attribute information:</b>&nbsp;' . $this->info . '';
     116        $out .= $msg;
    59117        $out .= '</div>';
    60118        return $out;
  • wp-custom-field-chart/trunk/readme.txt

    r958396 r959428  
    55Requires at least: 3.9.1
    66Tested up to: 3.9.1
    7 Stable tag: 0.0.2
     7Stable tag: 0.0.3
    88License: GPLv2 or later
    99License URI: http://www.gnu.org/licenses/gpl-2.0.html
    1010
    11 Make chart from *custom field* using *Chart.js* library
     11Make chart from custom field attached to your post/page using Chart.js
     12javascript library.
    1213
    1314== Description ==
    14 
    15 This plugin collect data attached to post/article via *custom field* and make
     15This plugin collect data attached to post/article via **custom field** and make
    1616chart of it.
    17 This plugin use *Chart.js* for chart drawing [ChartJs](http://www.chartjs.org/)
     17This plugin use **Chart.js** for chart drawing [ChartJs](http://www.chartjs.org/)
    1818
    1919Data are collected by looking for specific *custom field* attached to your
    20 post/page. You can change aggregation method and intervall, chart multiple
    21 field in one chart...
     20post/page. You can change aggregation method, intervall...
     21
     22See [usage](http://wordpress.org/plugins/wp-custom-field-chart/other_notes/)
    2223
    2324== Installation ==
    24 
    25251. Upload the entire `wp-custom-field-chart` folder to the `/wp-content/plugins/` directory
    26262. Activate the plugin through the 'Plugins' menu in WordPress
    27 3. include [custom_field_chart] tag in your post/page (See Other Notes)
     273. Include [custom_field_chart] tag in your post/page (See Other Notes)
    2828
    2929== Frequently Asked Questions ==
     
    3131
    3232== Screenshots ==
    33 1. One field for each chart
    34 2. Combined field in one chart
    35 3. Bar char with different period
     331. One field for each chart (Two tags)
     342. Combined field in one chart (One Tag)
     353. Bar chart with different period
    3636
    3737== Changelog ==
    38 
     38= 0.0.3 =
     39* More attribute validation and default
     40* Now as to_date default, introducing post to specify post data as to_date
     41* Better readme, well more informations...
    3942= 0.0.2 =
    4043* Uploading some screenshots
     
    4750Edit your post/page in text mode and put some Javascript and a Wordpress tag
    4851
     52= Minimum =
     53`
     54<script>
     55var mydata = { datasets: [{}]};
     56</script>
     57[custom_field_chart fields="humidity" js_data="mydata"]
     58`
     59For each field you need to put empty {} into datasets.
     60
     61For two fields:
     62`
     63<script>
     64var mydata = {datasets: [{},{}]}
     65</script>
     66[custom_field_chart fields="humidity,temperature" js_data="mydata"]
     67`
     68But it's pretty useless to put more than one field without different colors :)
     69
     70= More =
    4971`
    5072<script>
     
    1001223. You don't need to supply labels and data (like in chartjs.org example) :)
    101123
    102 = Tag options =
     124= Tag options (required)=
    1031251. fields: Custom field separate by comma
    104 1. method: Aggregate method (track, delta, cumulative)
    1051261. js_data: Name of javascript variable holding chart datasets
     127= Tag options (optional)
     1281. width: Chart width (default: 400)
     1291. height: Chart Height (default: 200)
     1301. method: Aggregate method track, delta or cumulative (defaul: track)
    1061311. js_options: Name of javascript variable holding chart options
    107 1. kind: Chart type (line or bar)
    108 1. width: Chart width
    109 1. height: Chart Height
    110 
     1321. kind: Chart type line or bar (default: line)
     1331. to_date: Current date by default, post date if 'post' specified else value supplied
     1341. dump: Dumping attributes for debug (default: False)
    111135== Note ==
    112136Beta software... Interface may change.
  • wp-custom-field-chart/trunk/wp-custom-field-chart.php

    r957731 r959428  
    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.1
     6Version: 0.0.2
    77Author: Joachim Basmaison
    88Note: Based on Dylan Kuhn Tally wordpress plugin (GPLv2)
     
    3838
    3939use WpCustomFieldChart\Field as Field;
    40 
    41 $CFC_FIELDS = array(
     40use WpCustomFieldChart\FieldArray;
     41
     42
     43$CFC_FIELDS = new FieldArray(array(
    4244    new Field('width', true, 400, '^\d+$', 'Chart width'),
    4345    new Field('height', true, 200, '^\d+$', 'Chart height'),
     
    4648    new Field('js_data', true, Null, '^\w[\w\d_]+$',
    4749        'Javascript variable holding our chart datasets'),
    48     new Field('js_option', false, Null, '^\w[\w\d_]+$',
     50    new Field('js_options', false, Null, '^\w[\w\d_]+$',
    4951        'Javascript variable holding our chart options'),
    50 //  new Field('period', true, 'month', '^(year|month|day)$',
    51 //      'Period: year, month, day'),
    52     new Field('fields', true, Null, '^\w[\w\d_-]+$',
     52    new Field('fields', true, Null, '^\w[\w\d,_-]+$',
    5353        'Comma separated wordpress custom field that we want to plot'),
    5454    new Field('method', true, 'track', '^(track|cumulative|delta)',
    5555        'Aggregation method: track, cumulative, delta'),
    56     new Field('interval', true, 'month', '^(year|month|day)$',
     56    new Field('interval', true, 'day', '^(year|month|day)$',
    5757        'Period: year, month, day'),
    58     new Field('interval_count', true, 6, '^\d+$', 'Interval count'),
     58    new Field('interval_count', true, 31, '^\d+$', 'Interval count'),
    5959    new Field('class', true, 'cfc-chart', '^[\w\d-_]+$',
    6060        'Class used for our HTML div element'),
    61     new Field('to_date', false)
    62 );
     61    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    ),
     71));
    6372
    6473/************************
     
    7382        cfc_validate_attributes($atts);
    7483    } catch (WpCustomFieldChart\ErrorMissingAttribute $e) {
    75         return cfc_get_field_error($e->getMessage());
     84        return cfc_get_field_error($e);
     85    } catch (WpCustomFieldChart\ErrorInvalidValue $e) {
     86        return cfc_get_field_error($e);
    7687    }
    7788    $data =  cfc_collect_data($atts);
     
    7990}
    8091
    81 function cfc_get_field_error($name) {
     92function cfc_get_field_error($e) {
    8293    global $CFC_FIELDS;
    83     foreach($CFC_FIELDS as $field) {
     94    $name = $e->getMessage();
     95    foreach($CFC_FIELDS->getPool() as $key => $field) {
    8496        if ($field->name != $name) {
    8597            continue;
    8698        }
    87         return $field->make_error_message();
     99        return $field->make_error_message($e);
    88100    }
    89101    return "Unknow field $name";
     
    93105{
    94106    global $CFC_FIELDS;
    95     foreach ($CFC_FIELDS as $field) {
    96         $atts[$field->name] = $field->validate($atts[$field->name]);
     107    $dump = False;
     108    if (key_exists('dump', $atts)) {
     109        $dump = $CFC_FIELDS->get('dump')->validate($atts['dump']);
     110    }
     111    foreach ($CFC_FIELDS->getPool() as $key => $field) {
     112        $value = $field->validate($atts[$field->name]);
     113        if (is_null($value)) {
     114            unset($atts[$field->name]);
     115        } else {
     116            $atts[$field->name] = $value;
     117        }
     118        if ($dump) {
     119            echo "$key: $value<br>\n";
     120        }
    97121    }
    98122}
     
    109133{
    110134    global $wp_query;
    111 
    112     // $atts = wp_parse_args($atts);
    113 //     $defaults = array(
    114 //         'interval_count' => '6',
    115 //         'chs' => '200x200',
    116 //         'cht' => 'bvs'
    117 //     );
    118 //     $atts = array_merge($defaults, $atts);
    119 //     if (! isset($atts['fields']))
    120 //         return 'Tally Graph: required parameter "key" is missing.';
    121 
    122         // Extract non-chart variables from attributes
    123135    $keys = split(',', $atts['fields']);
    124     // unset($atts['fields']);
    125     // $use_cache = true;
    126     // if (isset($atts['no_cache'])) {
    127     // $use_cache = false;
    128     // unset($atts['no_cache']);
    129     // }
     136    $end_time = time();
    130137    if (isset($atts['to_date'])) {
    131         $end_time = strtotime($atts['to_date']);
    132         if (! $end_time) {
    133             return 'Tally Graph: couldn\'t read the to_date ' .
    134                 $atts['to_date'];
    135         }
    136         // unset($atts['to_date']);
    137     } else
    138         if ($wp_query->post_count > 0) {
     138        if ($atts['to_date'] == 'post') {
    139139            $end_time = strtotime($wp_query->posts[0]->post_date);
    140140        } else {
    141             $end_time = time();
    142         }
     141            $end_time = strtotime($atts['to_date']);
     142        }
     143        if (! $end_time) {
     144            return 'Tally Graph: couldn\'t read the to_date ' . $atts['to_date'];
     145        }
     146    }
    143147    if (isset($atts['interval'])) {
    144148        $cfc_interval = $atts['interval'];
    145         // unset($atts['interval']);
    146149    } else {
    147150        $cfc_interval = 'month';
     
    149152    if (isset($atts['label_interval'])) {
    150153        $label_interval = $atts['label_interval'];
    151         // unset( $atts['label_interval'] );
    152154    } else {
    153155        $label_interval = $cfc_interval;
     
    163165            return 'Tally Graph: Unknown method "' . $method . '"';
    164166        }
    165         // unset($atts['method']);
    166167    }
    167168    $interval_count = $atts['interval_count'];
    168     // unset($atts['interval_count']);
    169169
    170170    list ($index_gnu_format, $index_mysql_format, $first_day_suffix) =
     
    197197    }
    198198
    199     // Build the chart parameters
    200199    $first_index = null;
    201200    $label_array = Array();
Note: See TracChangeset for help on using the changeset viewer.