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:
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.");
?>
| Graph# | ID | |||
|---|---|---|---|---|
| table_id; ?> | id; ?> | stamp); ?> | value; ?> |