load($array); } public function load_options($options = array()) { if ((bool) !$options) { return false; } $this->options = $options; return TRUE; } public function load($array = array()) { if ((bool) !$array) { return false; } // ITS4YOU-UP SlOl | 15.7.2014 11:03 ch_image_name foreach (array('library_loaded', 'graphic_type', 'create_div', 'dashboard_div', 'filter_div', 'chart_div', 'class_filter_div', 'class_dashboard_div', 'class_chart_div', 'open_js_tag', 'control_type', 'ch_image_name') as $p) { if (isset($array[$p])) { if ($p == 'graphic_type') { $this->set_graphic_type($array[$p]); continue; } $this->$p = $array[$p]; } } } public function load_library() { $return_script = ""; if (!$this->library_loaded) { $this->library_loaded = TRUE; $onlin_jsapi = "https://www.google.com/jsapi"; $local_jsapi = "modules/ITS4YouReports/jsapi.js"; $load_script = true; if(file_get_contents($onlin_jsapi)!==false){ $scipt_path = $onlin_jsapi; }elseif(file_exists($local_jsapi)){ $scipt_path = $local_jsapi; }else{ echo "
";print_r("Save content of url: JS Api to directory modules/ITS4YouReports/jsapi.js please !");echo "
"; die(); } if($scipt_path){ $return_script .=''; } } return $return_script; } public function set_graphic_type($type = NULL) { if (is_null($type)) return false; $type = strtolower(trim($type)); $types = array( 'linechart' => 'LineChart', 'piechart' => 'PieChart', 'columnchart' => 'ColumnChart', 'areachart' => 'AreaChart', 'treemap' => 'TreeMap', 'scatterchart' => 'ScatterChart', 'gauge' => 'Gauge', 'geochart' => 'GeoChart', 'combochart' => 'ComboChart', 'barchart' => 'BarChart', 'candlestickchart' => 'CandlestickChart', 'table' => 'Table'); if (!in_array($type, array_keys($types))) { exit('Error: Type of graph is not defined. [' . $type . ']'); } $this->graphic_type = $types[$type]; return true; } public function set_options($options = array()) { if ((bool) !$options) { return array(); } $this->gen_options = $options; return true; } public function set_control_options($options = array()) { if ((bool) !$options) { return array(); } $this->control_options = $options; return true; } public function generate($data) { if ((bool) !$data) { return false; } if (is_null($this->chart_div)) { //$key = $this->gerarkey(10); //$this->chart_div = 'gcharts_' . $key; $this->chart_div = 'innerchdiv'; } if ($this->dashboard_div === TRUE) { $this->dashboard_div = 'dashboard_' . $key; $this->use_dashboard = TRUE; } if ($this->filter_div === TRUE) { $this->filter_div = 'filter_' . $key; } /*if ($this->use_dashboard === FALSE) { return $this->GenWithoutDashboard($data); } else { return $this->GenWithDashboard($data); }*/ if ($this->use_dashboard === FALSE) { return $this->GenWithoutDashboard($data); }else{ return $this->GenWithDashboard($data); } return false; } /*private function GenWithDashboard($data) { $js = NULL; $js .= $this->load_library() . "\n"; if ($this->open_js_tag === TRUE) { $js .= '' . "\n"; } /* CRIA AS DIVS * / if ($this->create_div === TRUE) { /* DASHBOARD DIV * / if (!is_null($this->dashboard_div)) { $js .= '
'; } /* FILTER DIV * / if (!is_null($this->filter_div)) { $js .= '
'; } /* CHART DIV * / $js .= '
'; /* DASHBOARD CLOSE DIV * / if (!is_null($this->dashboard_div)) { $js .= '
'; } } // FIM CREATE DIV $this->clean(); return $js; }*/ private function GenWithoutDashboard($data) { $js = NULL; $js .= $this->load_library() . "\n"; if ($this->open_js_tag === TRUE) { $js .= '' . "\n"; } /* CRIA AS DIVS */ if ($this->create_div === TRUE) { /* CHART DIV */ if($this->graphic_type=="PieChart"){ $style_params = "width:30%;margin:auto;"; }else{ $style_params = "width:90%;margin:auto;"; } $js .= '
'; $js .= '
'; } // FIM CREATE DIV $this->clean(); return $js; } /* @INPUT array: $array = array('title' => 'My Title'); or $array = array('title' => 'My Title','vAxis' => array('title' => 'Cups')); @OUTPUT string: {title: 'title'} or {title: 'My Title', vAxis: {title: 'Cups'}} */ private function array_to_jsobject($array = array()) { if ((bool) !$array) { return '{}'; } $return = NULL; foreach ($array as $k => $v) { if (is_array($v) && $k!="colors") { $return .= $k . ": " . $this->array_to_jsobject($v) . ","; }elseif($k=="colors"){ $return .= $k . ": ['" . implode("','", $v) . "'],"; } /*if (is_array($v)) { $return .= $k . ": " . $this->array_to_jsobject($v) . ","; }*/else { if (is_string($v)) { $return .= $k . ": '" . addslashes($v) . "',"; } else { $return .= $k . ": " . $v . ","; } } } return '{' . trim($return, ',') . '}'; } /* @INPUT matriz: $array = array(array('Year', 'Sales', 'Expenses'), array('2004',1000,400), array('2005',1170,460), array('2006',660,1120), array('2007',1030,540)); @OUTPUT string: [['Year','Sales','Expenses'],['2004','1000','400'],['2005','1170','460'],['2006','660','1120'],['2007','1030','540']] */ private function array_to_jsarray($array = array()) { if ((bool) !$array) { return '[]'; } $return = NULL; foreach ($array as $k => $v) { if (is_array($v)) { $return .= ',' . $this->array_to_jsarray($v); } else { if (is_string($v)) { $return .= ",'" . addslashes($v) . "'"; } else { $return .= "," . $v; } } } return '[' . trim($return, ',') . ']'; } public function clean() { //$this->library_loaded = FALSE; $this->create_div = TRUE; $this->dashboard_div = NULL; $this->class_dashboard_div = NULL; $this->filter_div = NULL; $this->class_filter_div = NULL; $this->chart_div = NULL; $this->class_chart_div = NULL; $this->open_js_tag = TRUE; $this->graphic_type = 'LineChart'; $this->control_type = 'NumberRangeFilter'; $this->gen_options = array(); $this->control_options = array(); $this->use_dashboard = FALSE; } public function is_number($num) { if ((bool) preg_match("/^([0-9\.])+$/i", $num)) return true; else return false; } public function gerarkey($length = 40) { $key = NULL; $pattern = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRTWXYZ'; for ($i = 0; $i < $length; ++$i) { $key .= $pattern{rand(0, 58)}; } return $key; } } ?>