9 ) $number = 9; for ($i = 1; $i <= 9; $i++) { $name = array('Simple Graph %s', 'widgets', $i); register_sidebar_widget($name, $i <= $number ? 'widget_pjm_graph_widget' : /* unregister */ '', $i); register_widget_control($name, $i <= $number ? 'widget_pjm_graph_control' : /* unregister */ '', 400, 620, $i); } add_action('sidebar_admin_setup', 'widget_pjm_graph_setup'); add_action('sidebar_admin_page', 'widget_pjm_graph_page'); add_filter('the_content', 'simple_graph_filter'); } /** * * * @param unknown $args * @param integer $number (optional) Graph ID number */ function widget_pjm_graph_widget($args, $number = 1) { global $wpdb; extract($args); $options = get_option('pjm_graph_options'); if (!is_array($options[$number])) $options[$number] = array( 'title' => '', 'text' => '', 'width' => 160, 'height' => 120, 'bg_col' => 'FFFFFF', 'fg_col' => '000000', 'line_col' => '0000FF', 'bg_line_col' => 'CCCCFF', 'trend_line_col' => '88FF88', 'target_line_col' => 'FF0000', 'date_fmt' => 'y/m/d', 'show_text' => TRUE, 'show_title' => TRUE, 'show_trend' => FALSE, 'show_target' => FALSE, 'show_hl_graph' => TRUE, 'user_id' => 1, 'table_id' => 1, 'gchart' => FALSE ); if (!isset($options[$number]['gchart'])) $options[$number]['gchart'] = FALSE; $title = $options[$number]['title']; if ((!$options[$number]['show_title'])|| ($title=="")) $title = null; $tags = null; if (($options[$number]['show_title'] && (strpos($options[$number]['title'], "%") !==FALSE) ) || ($options[$number]['show_text']) && (strpos($options[$number]['text'], "%") !==FALSE)) { $tags = pjm_graph_get_tags($options[$number]['user_id'], $options[$number]['table_id']); } if (is_array($tags)) { $tags['target'] = $options[$number]['target']; $tags['first_date'] = date($options[$number]['date_fmt'], $tags['first_date']); $tags['last_date'] = date($options[$number]['date_fmt'], $tags['last_date']); } echo $before_widget; if ($title!=null) { echo $before_title; echo pjm_graph_tags($title, $tags); echo $after_title; } echo "

"; pjm_graph( $number, $options[$number]['width'], $options[$number]['height'], $options[$number]['show_trend'], $options[$number]['show_target'], FALSE, FALSE, FALSE, $options[$number]['user_id'], $options[$number]['table_id'], FALSE, $options[$number]['gchart'] ); echo "

"; if ($options[$number]['show_text']) echo pjm_graph_tags($options[$number]['text'], $tags); echo $after_widget; } /** * Get the various tags for substitution into the Widget's text from * the dataset (%CURRENT, %HIGH, %LOW, etc) * * @param Integer $uid User ID * @param Integer $tid Graph ID * @return Array */ function pjm_graph_get_tags($uid, $tid) { global $wpdb; $tags = array(); $table = $wpdb->prefix . 'simple_graph'; $sql ="SELECT MAX(stamp) AS highdate, MIN(stamp) AS lowdate, " . "MAX(value) AS highvalue, MIN(value) AS lowvalue " . "FROM $table WHERE user_id=$uid AND table_id=$tid;"; if ( $valueset = $wpdb->get_results($sql) ) { foreach ($valueset as $values) { $tags['high'] = $values->highvalue; $tags['low'] = $values->lowvalue; $tags['last_date'] = $values->highdate; $tags['first_date'] = $values->lowdate; } } $sql = "SELECT value FROM $table WHERE stamp = {$tags['last_date']};"; if ( $valueset = $wpdb->get_results($sql) ) { $tags['current'] = $valueset[0]->value; } $sql = "SELECT value FROM $table WHERE stamp = {$tags['first_date']};"; if ( $valueset = $wpdb->get_results($sql) ) { $tags['start'] = $valueset[0]->value; } return $tags; } /** * Substitute values for tags in the widget text (%CURRENT, %HIGH, * %LOW, etc...) * * @param String $string Widget text before substitution * @param Array $tag_values Values to substitute for tags in an array * @return String The string with the values substituted for the placeholder tags */ function pjm_graph_tags($string, $tag_values) { if (!is_array($tag_values)) return $string; $string = str_replace("%CURRENT", $tag_values['current'], $string); $string = str_replace("%HIGH", $tag_values['high'], $string); $string = str_replace("%LOW", $tag_values['low'], $string); $string = str_replace("%START", $tag_values['start'], $string); $string = str_replace("%TARGET", $tag_values['target'], $string); $string = str_replace("%FIRST_DATE", $tag_values['first_date'], $string); $string = str_replace("%LAST_DATE", $tag_values['last_date'], $string); return $string; } /** * Util function to format a colour string read from the user * * @param String $col User input of a colour string * @return String Sanitised version of input */ function format_color($col) { $col = strip_tags(stripslashes($col)); if ($col[0] == '#') $col = substr($col, 1); return $col; } /** * * * @param unknown $number */ function widget_pjm_graph_control($number) { global $wpdb; $options = get_option('pjm_graph_options'); if (!is_array($options[$number])) $options[$number] = array('title' => '', 'text' => '', 'width' => 160, 'height' => 120, 'bg_col' => 'FFFFFF', 'fg_col' => '000000', 'line_col' => '0000FF', 'bg_line_col' => 'CCCCFF', 'trend_line_col' => '88FF88', 'target_line_col' => 'FF0000', 'date_fmt' => 'y/m/d', 'show_text' => TRUE, 'show_title' => TRUE, 'show_trend' => FALSE, 'show_target' => FALSE, 'show_hl_graph' => TRUE, 'user_id' => 1, 'table_id' => 1, 'gchart' => FALSE ); if (isset($options[$number]['gchart'])) $options[$number]['gchart'] = FALSE; $newoptions = $options; if ($_POST['pjm_graph_submit-'.$number]) { $newoptions[$number]['title'] = strip_tags(stripslashes($_POST['pjm_graph_title-'.$number])); $newoptions[$number]['text'] = stripslashes($_POST['pjm_graph_text-'.$number]); if ( !current_user_can('unfiltered_html') ) $newoptions[$number]['text'] = stripslashes(wp_filter_post_kses($newoptions[$number]['text'])); $newoptions[$number]['target'] = strip_tags(stripslashes($_POST['pjm_graph_target-'.$number])); $newoptions[$number]['width'] = strip_tags(stripslashes($_POST['pjm_graph_width-'.$number])); $newoptions[$number]['height'] = strip_tags(stripslashes($_POST['pjm_graph_height-'.$number])); $newoptions[$number]['bg_col'] = format_color($_POST['pjm_graph_bg_col-'.$number]); $newoptions[$number]['fg_col'] = format_color($_POST['pjm_graph_fg_col-'.$number]); $newoptions[$number]['line_col'] = format_color($_POST['pjm_graph_line_col-'.$number]); $newoptions[$number]['bg_line_col'] = format_color($_POST['pjm_graph_bg_line_col-'.$number]); $newoptions[$number]['trend_line_col'] = format_color($_POST['pjm_graph_trend_line_col-'.$number]); $newoptions[$number]['target_line_col'] = format_color($_POST['pjm_graph_target_line_col-'.$number]); $newoptions[$number]['date_fmt'] = stripslashes($_POST['pjm_graph_date_fmt-'.$number]); list ($newoptions[$number]['user_id'], $newoptions[$number]['table_id']) = explode(":", $_POST['pjm_graph_user_table_id-'.$number]); $newoptions[$number]['show_title'] = isset($_POST['pjm_graph_show_title-'.$number]) ? TRUE : FALSE; $newoptions[$number]['show_text'] = isset($_POST['pjm_graph_show_text-'.$number]) ? TRUE : FALSE; $newoptions[$number]['show_target'] = isset($_POST['pjm_graph_show_target-'.$number]) ? TRUE : FALSE; $newoptions[$number]['show_trend'] = isset($_POST['pjm_graph_show_trend-'.$number]) ? TRUE : FALSE; $newoptions[$number]['show_hl_graph'] = isset($_POST['pjm_graph_show_hl_graph-'.$number]) ? TRUE : FALSE; $newoptions[$number]['gchart'] = isset($_POST['pjm_graph_use_gchart-'.$number]) ? TRUE : FALSE; } if ($newoptions != $options) { $options = $newoptions; update_option('pjm_graph_options', $options); } $options[$number]['title'] = htmlspecialchars($options[$number]['title'], ENT_QUOTES); $options[$number]['text'] = htmlspecialchars($options[$number]['text'], ENT_QUOTES); echo '

'; echo '

'; echo ''; ?>

Compatibility check:
PHP Version:
GD Loaded:
GD Version:
Image format:

9 ) $number = 9; if ( $number < 1 ) $number = 1; $newoptions['number'] = $number; } if ( $options != $newoptions ) { $options = $newoptions; update_option('pjm_graph_options', $options); widget_pjm_graph_register($options['number']); } } /** * */ function widget_pjm_graph_page() { $options = $newoptions = get_option('pjm_graph_options'); ?>

1, 'x' => 0, 'y' => 0, 'trend' => false, 'target' => false, 'ytd' => false, 'lm' => false, 'wkly' => false, 'uid' => false, 'gid' => false ); foreach ($params as $param) { list ($name, $value) = explode("=", $param); $options[$name] = $value; } $img_tag = pjm_graph($options['n'], $options['x'], $options['y'], $options['trend'], $options['target'], $options['ytd'], $options['lm'], $options['wkly'], $options['uid'], $options['gid'], true); $content = str_replace($simplegraph, $img_tag, $content); } return $content; } /** * * * @param unknown $number (optional) * @param unknown $x (optional) * @param unknown $y (optional) * @param unknown $trend (optional) * @param unknown $target (optional) * @param unknown $ytd (optional) * @param unknown $lm (optional) * @param unknown $wkly (optional) * @param unknown $user_id (optional) * @param unknown $table_id (optional) * @param unknown $only_return_tag (optional) * @param unknown $use_gchart (optional) * @return unknown */ function pjm_graph($number=1, $x=0, $y=0, $trend=FALSE, $target=FALSE, $ytd=FALSE, $lm=FALSE, $wkly=FALSE, $user_id=0, $table_id=0, $only_return_tag=FALSE, $use_gchart=FALSE) { $options = get_option('pjm_graph_options'); if (!is_array($options[$number])) $options[$number] = array('title' => '', 'text' => '', 'width' => 160, 'height' => 120, 'bg_col' => 'FFFFFF', 'fg_col' => '000000', 'line_col' => '0000FF', 'bg_line_col' => 'CCCCFF', 'trend_line_col' => '88FF88', 'target_line_col' => 'FF0000', 'date_fmt' => 'y/m/d', 'show_text' => TRUE, 'show_title' => TRUE, 'show_trend' => FALSE, 'show_target' => FALSE, 'show_hl_graph' => TRUE, 'user_id' => 1, 'table_id' => 1, 'gchart' => FALSE ); if (!isset($options[$number]['gchart'])) $options[$number]['gchart'] = FALSE; $width = $options[$number]['width']; $height = $options[$number]['height']; $uid = $options[$number]['user_id']; $tid = $options[$number]['table_id']; $gchart_enabled = $options[$number]['gchart'] & $use_gchart; if ($x!=0) $width = $x; if ($y!=0) $height = $y; if ($user_id!=0) $uid = $user_id; if ($table_id!=0) $tid = $table_id; //$siteurl = get_option('siteurl'); //if ("/"==substr($siteurl,strlen($siteurl)-1)) // $siteurl = substr($siteurl,0,strlen($siteurl)-1); $img_tag = NULL; if ($gchart_enabled) { $datatype = "t:"; require_once 'grapher/gchart.php'; $img_tag = "\"[Graph"; } else { $img_tag = 'Graph by www.pasi.fi/simple-graph-wordpress-plugin/'; } if (!$only_return_tag) echo $img_tag; return $img_tag; } /** * */ function pjm_graph_install() { global $wpdb; if (!current_user_can('activate_plugins')) return; $table_name = $wpdb->prefix . 'simple_graph'; // if simple_graph table doesn't exist, create it if ( $wpdb->get_var("show tables like '$table_name'") != $table_name ) { $sql = "CREATE TABLE $table_name ( id int PRIMARY KEY AUTO_INCREMENT, user_id bigint(20) NOT NULL, table_id int NOT NULL, stamp int NOT NULL, value double NOT NULL)"; require_once ABSPATH . 'wp-admin/upgrade-functions.php'; dbDelta($sql); // if pjm_graph table exists, i.e. we're upgrading from 0.9.8c or earlier version // copy values from that table to the new table, for current user's table one $old_table = $wpdb->prefix . 'pjm_graph'; if ( $wpdb->get_var("show tables like '$old_table'") == $old_table ) { global $current_user; $user_id = $current_user->data->ID; $old_data_sql = "SELECT * FROM $old_table"; $old_data = $wpdb->get_results($old_data_sql); if (!is_empty($old_data)) foreach ($old_data as $old_row) { $stamp = $old_row->stamp; $value = $old_row->value; $insert_sql = "INSERT INTO $table_name (user_id,table_id,stamp,value) values ($user_id,1,$stamp,$value);"; $wpdb->query($insert_sql); } } } } register_activation_hook(__FILE__, 'pjm_graph_install'); /** * */ function pjm_managePanel() { if (function_exists('add_management_page')) { add_management_page('Simple Graph', 'Simple Graph', 5, basename(__FILE__), 'pjm_show_manage_panel'); } } add_action('admin_menu', 'pjm_managePanel'); /** * */ function pjm_show_manage_panel() { global $wpdb, $current_user; $table_prefix = $wpdb->prefix; //if (!current_user_can('edit_pages')) { echo "Insufficient role level. You need to be an Editor."; return; } if (isset($_GET['pjm_graph_delete'])) { ?>

data->ID; $wpdb->query($sql); } if (isset($_POST['pjm_graph_value'])) { ?>

escape($_POST['pjm_graph_value']); $table_id = $wpdb->escape($_POST['pjm_graph_table_id']); $sql = "INSERT INTO ".$table_prefix."simple_graph (user_id, table_id, stamp, value) values ({$current_user->data->ID},$table_id,$date,$value)"; $wpdb->query($sql); } else if (isset($_POST['batch_insert'])) { ?>

escape($_POST['pjm_graph_table_id']); $lines = explode("\n", $_POST['batch_insert']); $accepted = 0; $rejected = 0; foreach ($lines as $line) { $reject = FALSE; $line = trim($line); $parts = explode(",", $line); if (count($parts)==2) { $dateparts = explode("-", $parts[0]); if (count($dateparts)==3) { $date = mktime(0, 0, 0, $dateparts[1], $dateparts[2], $dateparts[0]); $value = mysql_real_escape_string($parts[1]); $sql = "INSERT INTO ".$table_prefix."simple_graph (user_id, table_id, stamp, value) values ({$current_user->data->ID},$table_id,$date,$value)"; $wpdb->query($sql); } else $reject = TRUE; } else $reject = TRUE; if ($reject) { print("Rejected: $line
"); $rejected++; } else { $accepted++; } } print("Accepted $accepted data points and rejected $rejected data points."); ?>

:
: Year: Month: Day:
:

Use this to insert multiple values at once. Please enter one date-value pair per line. Dates must be in YYYY-MM-DD format, followed by a comma, and a decimal value. (For example, "2006-09-19,95.5".) Lines that can't be parsed will be rejected, while other lines are inserted into database without further validation. Therefore, this is for advanced users only!

:
Insert dates & values:

data->ID} ORDER BY table_id DESC, id DESC LIMIT $offset,$row_count"; if ( $valueset = $wpdb->get_results($sql) ) { $rows = count($valueset); print(""); foreach ($valueset as $values) { $class = ('alternate' == $class) ? '' : 'alternate'; ?> id; ?>" class="">
Graph#ID
"); if ($offset>0) { $new_off = $offset - $row_count; if ($new_off < 0) $new_off = 0; print(" « Show previous 50 "); } print("Data points $offset - ".($offset+$rows)); if ($rows>=$row_count) print(" Show next 50 » "); print("
table_id; ?> id; ?> stamp); ?> value; ?>

Compatibility check:
PHP Version:
GD Loaded:
GD Version:
Image format:

:
:
:
:
:
:
:
:
: