GoogleGraph

The GoogleGraph GoogleGraph class.

Defined (1)

The class is defined in the following location(s).

/googlegraph.php  
  1. class GoogleGraph { 
  2.  
  3. /**--------------------------------------------* 
  4. * Constants 
  5. *--------------------------------------------*/ 
  6. const name = 'GoogleGraph'; 
  7. const slug = 'googlegraph'; 
  8.  
  9. /** 
  10. * Constructor 
  11. */ 
  12. function __construct() { 
  13. //register an activation hook for the plugin 
  14. register_activation_hook( __FILE__, array( &$this, 'install_googlegraph' ) ); 
  15.  
  16. //Hook up to the init action 
  17. add_action( 'init', array( &$this, 'init_googlegraph' ) ); 
  18.  
  19. /** 
  20. * Runs when the plugin is activated 
  21. */  
  22. function install_googlegraph() { 
  23. // do not generate any output here 
  24.  
  25. /** 
  26. * Runs when the plugin is initialized 
  27. */ 
  28. function init_googlegraph() { 
  29. // Setup localization 
  30. load_plugin_textdomain( self::slug, false, dirname( plugin_basename( __FILE__ ) ) . '/lang' ); 
  31. // Load JavaScript and stylesheets 
  32. $this->register_scripts_and_styles(); 
  33.  
  34. // Register the shortcode [barGraph] 
  35. add_shortcode( 'lineChart', array( &$this, 'render_linechart' ) ); 
  36. add_shortcode( 'columnChart', array( &$this, 'render_column' ) ); 
  37. add_shortcode( 'barChart', array( &$this, 'render_bargraph' ) ); 
  38. add_shortcode( 'pieChart', array( &$this, 'render_piechart' ) ); 
  39. add_shortcode( 'geoChart', array( &$this, 'render_geochart' ) ); 
  40. add_shortcode( 'bubbleChart', array( &$this, 'render_bubblechart' ) ); 
  41. add_shortcode( 'scatterChart', array( &$this, 'render_scatterchart' ) ); 
  42.  
  43. /** PHP League bridge */ 
  44. add_shortcode( 'phpLeagueGraphPerTeam', array( &$this, 'render_phpleaguegraph' ) ); 
  45. add_shortcode( 'phpLeagueGraphPerCategory', array( &$this, 'render_phpleaguegraphtransposed' ) ); 
  46.  
  47. /** Register filters */ 
  48. add_filter( 'no_texturize_shortcodes', array( &$this, 'shortcodes_to_exempt_from_wptexturize') ); 
  49.  
  50. if ( is_admin() ) { 
  51. add_action( 'admin_menu', array( &$this, 'register_googlegraph_menu_page') ); 
  52. wp_enqueue_script('jquery');  
  53. wp_enqueue_script('jquery-ui-core'); 
  54. wp_enqueue_script('jquery-ui-accordion'); 
  55. } else { 
  56. //this will run when on the frontend 
  57.  
  58. /** 
  59. * TODO: Define custom functionality for your plugin here 
  60. * For more information:  
  61. * http://codex.wordpress.org/Plugin_API#Hooks.2C_Actions_and_Filters 
  62. */ 
  63. add_action( 'your_action_here', array( &$this, 'action_callback_method_name' ) ); 
  64. add_filter( 'your_filter_here', array( &$this, 'filter_callback_method_name' ) );  
  65.  
  66.  
  67. function shortcodes_to_exempt_from_wptexturize($shortcodes) { 
  68. $shortcodes[] = 'lineChart'; 
  69. $shortcodes[] = 'columnChart'; 
  70. $shortcodes[] = 'barChart'; 
  71. $shortcodes[] = 'pieChart'; 
  72. $shortcodes[] = 'geoChart'; 
  73. $shortcodes[] = 'bubbleChart'; 
  74. $shortcodes[] = 'scatterChart'; 
  75.  
  76. $shortcodes[] = 'phpLeagueGraphPerTeam'; 
  77. $shortcodes[] = 'phpLeagueGraphPerCategory'; 
  78. return $shortcodes; 
  79. /** 
  80. Register the GoogleGraph admin page. 
  81. */ 
  82. function register_googlegraph_menu_page() { 
  83. add_menu_page( 'GoogleGraph menu', 'GoogleGraph', 'manage_options', 'googlegraph/admin/help.php', '', 'dashicons-chart-area', 81 ); 
  84.  
  85. /** 
  86. Transposed PHPLeague Chart  
  87. */ 
  88. function render_phpleaguegraph($atts, $content = null) { 
  89. extract(shortcode_atts(array( 
  90. 'league' => '1',  
  91. 'club_list' => NULL 
  92. ), $atts)); 
  93.  
  94. if (is_plugin_active('phpleague/phpleague.php')) { 
  95. global $wpdb; 
  96. if (is_null($club_list)) { 
  97. $query = "SELECT * FROM $wpdb->table_cache WHERE id_league = $league ORDER BY points DESC"; 
  98. } else { 
  99. $query = "SELECT * FROM $wpdb->table_cache WHERE id_league = $league AND id_team IN ($club_list) ORDER BY points DESC"; 
  100.  
  101. $result = "['Club', 'Points', 'Games Played', 'Victory', 'Draw', 'Defeat', 'Goals For', 'Goals Against'], "; 
  102. foreach ($wpdb->get_results($wpdb->prepare($query, NULL)) as $row) 
  103. $result .= "["; 
  104. $result .= "'".$row->club_name."', ";  
  105. $result .= "".$row->points.", ";  
  106. $result .= "".$row->played.", ";  
  107. $result .= "".$row->victory.", ";  
  108. $result .= "".$row->draw.", ";  
  109. $result .= "".$row->defeat.", ";  
  110. $result .= "".$row->goal_for.", ";  
  111. $result .= "".$row->goal_against.", ";  
  112. $result .= "], "; 
  113.  
  114. if (!isset($atts)) 
  115. $atts = array(); 
  116. if (!isset($atts['vaxis'])) { 
  117. $atts['vaxis'] = "{title: 'Clubs'}"; 
  118. if (!isset($atts['haxis'])) { 
  119. $atts['haxis'] = "{title: ''}"; 
  120.  
  121. return $this->render_chart('BarChart', $atts, $result ); 
  122. } else { 
  123. return "PHPLeague plug-in should be installed for this shortcode to work"; 
  124.  
  125. /** 
  126. Transposed PHPLeague Chart  
  127. */ 
  128. function render_phpleaguegraphtransposed($atts, $content = null) { 
  129. extract(shortcode_atts(array( 
  130. 'league' => '1',  
  131. 'club_list' => NULL 
  132. ), $atts)); 
  133.  
  134. if (is_plugin_active('phpleague/phpleague.php')) { 
  135. global $wpdb; 
  136. if (is_null($club_list)) { 
  137. $query = "SELECT * FROM $wpdb->table_cache WHERE id_league = $league ORDER BY points DESC"; 
  138. } else { 
  139. $query = "SELECT * FROM $wpdb->table_cache WHERE id_league = $league AND id_team IN ($club_list) ORDER BY points DESC"; 
  140.  
  141. /** Create the array */ 
  142. $resArr = array(); 
  143. $resArr[1] = array( "'Categories'" ); 
  144. $resArr[2] = array( "'Points'" ); 
  145. $resArr[3] = array( "'Games Played'" ); 
  146. $resArr[4] = array( "'Victory'" ); 
  147. $resArr[5] = array( "'Draw'" ); 
  148. $resArr[6] = array( "'Defeat'" ); 
  149. $resArr[7] = array( "'Goals For'" ); 
  150. $resArr[8] = array( "'Goals Against'" ); 
  151.  
  152. foreach ($wpdb->get_results($wpdb->prepare($query, NULL)) as $row) 
  153.  
  154. $resArr[1][] = "'".$row->club_name."'";  
  155. $resArr[2][] = $row->points;  
  156. $resArr[3][] = $row->played;  
  157. $resArr[4][] = $row->victory;  
  158. $resArr[5][] = $row->draw;  
  159. $resArr[6][] = $row->defeat;  
  160. $resArr[7][] = $row->goal_for;  
  161. $resArr[8][] = $row->goal_against;  
  162.  
  163. /** Transpose the aray */ 
  164. $result = ""; 
  165. foreach ($resArr as $row) { 
  166. $result .= "[" . implode(", ", $row) . "], "; 
  167.  
  168. if (!isset($atts)) 
  169. $atts = array(); 
  170. if (!isset($atts['vaxis'])) { 
  171. $atts['vaxis'] = "{title: 'Categories'}"; 
  172. if (!isset($atts['haxis'])) { 
  173. $atts['haxis'] = "{title: ''}"; 
  174.  
  175. return $this->render_chart('BarChart', $atts, $result ); 
  176. } else { 
  177. return "PHPLeague plug-in should be installed for this shortcode to work"; 
  178.  
  179.  
  180. function render_linechart($atts, $content = null) { 
  181. return $this->render_chart('LineChart', $atts, $content); 
  182.  
  183. function render_column($atts, $content = null) { 
  184. return $this->render_chart('ColumnChart', $atts, $content); 
  185.  
  186. function render_bargraph($atts, $content = null) { 
  187. return $this->render_chart('BarChart', $atts, $content); 
  188.  
  189. function render_piechart($atts, $content = null) { 
  190. return $this->render_chart('PieChart', $atts, $content); 
  191.  
  192. function render_geochart($atts, $content = null) { 
  193. return $this->render_chart('GeoChart', $atts, $content); 
  194. }  
  195.  
  196. function render_bubblechart($atts, $content = null) { 
  197. return $this->render_chart('BubbleChart', $atts, $content); 
  198. }  
  199.  
  200. function render_scatterchart($atts, $content = null) { 
  201. return $this->render_chart('ScatterChart', $atts, $content); 
  202. }  
  203.  
  204. function action_callback_method_name() { 
  205. // TODO define your action method here 
  206.  
  207. function filter_callback_method_name() { 
  208. // TODO define your filter method here 
  209.  
  210. function render_chart($type, $atts, $content) { 
  211. // Extract the attributes 
  212. extract(shortcode_atts(array( 
  213. 'width' => '600px', //foo is a default value 
  214. 'height' => '400px',  
  215. 'title' => '',  
  216. 'stacked' => 'false',  
  217. 'legend' => "{position: 'bottom', maxLines: 3}",  
  218. 'vaxis' => "{title: 'Y', titleTextStyle: {color: 'red'}}",  
  219. 'haxis' => "{title: 'X', titleTextStyle: {color: 'red'}}",  
  220. 'curvetype' => NULL,  
  221. 'displaymode' => NULL,  
  222. 'region' => NULL,  
  223. 'colorstart' => NULL,  
  224. 'colorend' => NULL,  
  225. 'slices' => NULL,  
  226. 'bubble' => NULL,  
  227. 'chartarea' => NULL,  
  228. 'interpolate' => 'true',  
  229. 'trendlines' => NULL,  
  230. ), $atts)); 
  231. // you can now access the attribute values using $attr1 and $attr2 
  232.  
  233. global $item_id; 
  234. $item_id++; 
  235.  
  236. // Prepare the curve option for line chart types 
  237. $otheroptions=""; 
  238.  
  239. if (!is_null($chartarea)) { 
  240. $otheroptions= $otheroptions."chartArea: $chartarea, "; 
  241.  
  242. if ($type === "LineChart") { 
  243. if (!is_null($curvetype)) { 
  244. $otheroptions= $otheroptions."curveType: '$curvetype', "; 
  245. if (!($interpolate ==="false")) { 
  246. $interpolate = "true"; 
  247. $otheroptions = $otheroptions."interpolateNulls: $interpolate, "; 
  248.  
  249. if ($type === "GeoChart") { 
  250. if (!is_null($displaymode)) { 
  251. $otheroptions= $otheroptions."displayMode: '$displaymode', "; 
  252. if (!is_null($region)) { 
  253. $otheroptions= $otheroptions."region: '$region', "; 
  254. if (!is_null($colorstart) && !is_null($colorend)) { 
  255. $otheroptions= $otheroptions."colorAxis: {colors: ['$colorstart', '$colorend']} , "; 
  256.  
  257. if ($type === "ColumnChart") { 
  258. if (!is_null($trendlines)) { 
  259. $otheroptions= $otheroptions."trendlines: $trendlines , "; 
  260.  
  261. if ($type === "BarChart") { 
  262. if (!is_null($trendlines)) { 
  263. $otheroptions= $otheroptions."trendlines: $trendlines , "; 
  264.  
  265. if ($type === "ScatterChart") { 
  266. if (!is_null($trendlines)) { 
  267. $otheroptions= $otheroptions."trendlines: $trendlines , "; 
  268.  
  269. // Adding the option for "slices" as per the google documents 
  270. // Example: slices="{ 0: {offset: 0.2, color: 'black'} }" 
  271. if ($type === "PieChart") { 
  272. if (!is_null($slices)) { 
  273. $otheroptions= $otheroptions."slices: $slices, "; 
  274.  
  275. // Adding the option for "bubble" as per the google documents 
  276. // Example: bubble ="{textStyle: {auraColor: 'none'}}" 
  277. if ($type === "BubbleChart") { 
  278. if (!is_null($bubble)) { 
  279. $otheroptions= $otheroptions."bubble: $bubble, "; 
  280. if (!is_null($colorstart) && !is_null($colorend)) { 
  281. $otheroptions= $otheroptions."colorAxis: {colors: ['$colorstart', '$colorend']}, "; 
  282.  
  283.  
  284. // Remove HTML tags from the content of the shortcode 
  285. $content = html_entity_decode($content, ENT_NOQUOTES, "UTF-8"); 
  286. $content = wp_strip_all_tags($content, true); 
  287.  
  288. $str = <<<EOT 
  289. <div id="googlegraph_$item_id" class="tsba_googlegraph">  
  290. <div id="chart_div_$item_id" class="gc_$type" style="width: $width; height: $height;"></div> 
  291. <script type="text/javascript"> 
  292. google.load("visualization", "1", {packages:["corechart"]}); 
  293. google.setOnLoadCallback(drawChart); 
  294. function drawChart() { 
  295. var data = google.visualization.arrayToDataTable([ 
  296. $content 
  297. ]); 
  298.  
  299. var options = { 
  300. $otheroptions 
  301. title: '$title',  
  302. vAxis: $vaxis,  
  303. hAxis: $haxis,  
  304. isStacked: $stacked,  
  305. legend: $legend 
  306. }; 
  307.  
  308. var chart = new google.visualization.$type(document.getElementById('chart_div_$item_id')); 
  309. chart.draw(data, options); 
  310. </script> 
  311. <p style="font-size: .5em;">Powered by <a href="http://tsba.mobi" title="Powered by TSBA.mobi GoogleGraph Wordpress plugin">TSBA.mobi GoogleGraph Wordpress plugin</a></p> 
  312. </div> 
  313.  
  314. EOT; 
  315. return $str; 
  316.  
  317. /** 
  318. * Registers and enqueues stylesheets for the administration panel and the 
  319. * public facing site. 
  320. */ 
  321. private function register_scripts_and_styles() { 
  322. wp_register_script( self::slug . '-jsapi-script', 'https://www.google.com/jsapi'); 
  323. wp_enqueue_script( self::slug . '-jsapi-script'); 
  324.  
  325. } // end register_scripts_and_styles 
  326.  
  327. /** 
  328. * Helper function for registering and enqueueing scripts and styles. 
  329. * @name The ID to register with WordPress 
  330. * @file_path The path to the actual file 
  331. * @is_script Optional argument for if the incoming file_path is a JavaScript source file. 
  332. */ 
  333. private function load_file( $name, $file_path, $is_script = false ) { 
  334.  
  335. $url = plugins_url($file_path, __FILE__); 
  336. $file = plugin_dir_path(__FILE__) . $file_path; 
  337.  
  338. if( file_exists( $file ) ) { 
  339. if( $is_script ) { 
  340. wp_register_script( $name, $url, array('jquery') ); //depends on jquery 
  341. wp_enqueue_script( $name ); 
  342. } else { 
  343. wp_register_style( $name, $url ); 
  344. wp_enqueue_style( $name ); 
  345. } // end if 
  346. } // end if 
  347.  
  348. } // end load_file 
  349.  
  350. } // end class