[ Index ] |
PHP Cross Reference of vtigercrm-6.1.0 |
[Summary view] [Print] [Text view]
1 <?php 2 /*+********************************************************************************** 3 * The contents of this file are subject to the vtiger CRM Public License Version 1.0 4 * ("License"); You may not use this file except in compliance with the License 5 * The Original Code is: vtiger CRM Open Source 6 * The Initial Developer of the Original Code is vtiger. 7 * Portions created by vtiger are Copyright (C) vtiger. 8 * All Rights Reserved. 9 ************************************************************************************/ 10 11 require_once ('include/utils/utils.php'); 12 require_once 'include/utils/CommonUtils.php'; 13 14 Class ChartUtils { 15 16 // Function to generate Bar Chart 17 public static function getBarChart($xaxisData, $yaxisData, $title='', $width='', $height='', $charttype='vertical', $cachedFileName=false, $target=false, $color='') { 18 19 global $log, $lang_crm, $default_charset; 20 21 require_once ('include/utils/utils.php'); 22 require_once ('include/utils/GraphUtils.php'); 23 include_once ('Image/Graph.php'); 24 include_once ('Image/Canvas.php'); 25 26 $barwidth = '70'; 27 if ($cachedFileName === false) { 28 $cache_file_name = 'cache/images/bar_chart_' . time() . '.png'; 29 } else { 30 $cache_file_name = $cachedFileName; 31 } 32 if (empty($width)) 33 $width = '400'; 34 if (empty($height)) 35 $height = '300'; 36 if ($target === false) 37 $target = array(); 38 if (empty($color)) 39 $color = 'black'; 40 41 $alts = array(); 42 $temp = array(); 43 for ($i = 0; $i < count($xaxisData); $i++) { 44 $name = html_entity_decode($xaxisData[$i], ENT_QUOTES, $default_charset); 45 $pos = substr_count($name, " "); 46 $alts[] = $name; 47 //If the daatx value of a string is greater, adding '\n' to it so that it'll cme inh 2nd line 48 if (strlen($name) >= 14) 49 $name = substr($name, 0, 44); 50 if ($pos >= 2) { 51 $val = explode(" ", $name); 52 $n = count($val) - 1; 53 $x = ""; 54 for ($j = 0; $j < count($val); $j++) { 55 if ($j != $n) { 56 $x .=" " . $val[$j]; 57 } else { 58 $x .= "@#" . $val[$j]; 59 } 60 } 61 $name = $x; 62 } 63 $name = str_replace("@#", " ", $name); 64 $temp[] = html_entity_decode($name, ENT_QUOTES, $default_charset); 65 } 66 $xaxisData = $temp; 67 68 // Set the basic parameters of the graph 69 $canvas = & Image_Canvas::factory('png', array('width' => $width, 'height' => $height, 'usemap' => true)); 70 $imagemap = $canvas->getImageMap(); 71 $graph = & Image_Graph::factory('graph', $canvas); 72 73 $font = & $graph->addNew('font', calculate_font_name($lang_crm)); 74 $font->setSize(8); 75 $font_color = "#000000"; 76 $font->setColor($font_color); 77 $graph->setFont($font); 78 79 $titlestr = & Image_Graph::factory('title', array($title, 8)); 80 $plotarea = & Image_Graph::factory('plotarea', array( 81 'axis', 82 'axis', 83 $charttype 84 )); 85 $graph->add(Image_Graph::vertical($titlestr, $plotarea, 5)); 86 87 // Now create a bar plot 88 $max = 0; 89 // To create unique lables we need to keep track of lable name and its count 90 $uniquex = array(); 91 92 $xlabels = array(); 93 $dataset = & Image_Graph::factory('dataset'); 94 if ($charttype == 'horizontal') { 95 $fill = & Image_Graph::factory('gradient', array(IMAGE_GRAPH_GRAD_VERTICAL_MIRRORED, $color, 'white')); 96 } else { 97 $fill = & Image_Graph::factory('gradient', array(IMAGE_GRAPH_GRAD_HORIZONTAL_MIRRORED, $color, 'white')); 98 } 99 100 for ($i = 0; $i < count($yaxisData); $i++) { 101 $x = 1 + $i; 102 if ($yaxisData[$i] >= $max) 103 $max = $yaxisData[$i]; 104 $dataset->addPoint( 105 $x, 106 $yaxisData[$i], 107 array( 108 'url' => $target[$i], 109 'alt' => $alts[$i] . '=' . $yaxisData[$i] 110 ) 111 ); 112 $xlabels[$x] = $xaxisData[$i]; 113 114 // To have unique names even in case of duplicates let us add the id 115 $xaxisData_appearance = $uniquex[$xaxisData[$i]]; 116 if ($xaxisData_appearance == null) { 117 $uniquex[$xaxisData[$i]] = 1; 118 } else { 119 $xlabels[$x] = $xaxisData[$i] . ' [' . $xaxisData_appearance . ']'; 120 $uniquex[$xaxisData[$i]] = $xaxisData_appearance + 1; 121 } 122 } 123 $bplot = & $plotarea->addNew('bar', $dataset); 124 $bplot->setFillStyle($fill); 125 126 //You can change the width of the bars if you like 127 if (!empty($xaxisData)) 128 $bplot->setBarWidth($barwidth / count($xaxisData), "%"); 129 //$bplot->setPadding(array('top'=>10)); 130 $bplot->setBackground(Image_Graph::factory('gradient', array(IMAGE_GRAPH_GRAD_HORIZONTAL, 'white', 'white'))); 131 $xaxis = & $plotarea->getAxis(IMAGE_GRAPH_AXIS_X); 132 $yaxis = & $plotarea->getAxis(IMAGE_GRAPH_AXIS_Y); 133 $yaxis->setFontSize(8); 134 $xaxis->setFontSize(8); 135 136 if ($charttype == 'horizontal') { // Invert X-axis and put Y-axis at bottom 137 $xaxis->setInverted(false); 138 $yaxis->setAxisIntersection('max'); 139 } 140 141 // set grid 142 $gridY = & $plotarea->addNew('line_grid', IMAGE_GRAPH_AXIS_Y); 143 $gridY->setLineColor('#[email protected]'); 144 $gridY2 = & $plotarea->addNew('bar_grid', null, IMAGE_GRAPH_AXIS_Y); 145 $gridY2->setFillColor('#[email protected]'); 146 147 148 // Add some grace to y-axis so the bars doesn't go all the way to the end of the plot area 149 $yaxis->forceMaximum(round(($max * 1.1) + 0.5)); 150 $ticks = get_tickspacing(round(($max * 1.1) + 0.5)); 151 152 // First make the labels look right 153 if ($charttype == 'horizontal') 154 $yaxis->setFontAngle('vertical'); 155 $yaxis->setLabelInterval($ticks[0]); 156 $yaxis->setTickOptions(-5, 0); 157 $yaxis->setLabelInterval($ticks[1], 2); 158 $yaxis->setTickOptions(-2, 0, 2); 159 160 // Create the xaxis labels 161 $array_data = & Image_Graph::factory('Image_Graph_DataPreprocessor_Array', 162 array($xlabels) 163 ); 164 165 // The fix the tick marks 166 $xaxis->setDataPreprocessor($array_data); 167 $xaxis->forceMinimum(0.5); 168 $xaxis->forceMaximum(0.5 + count($yaxisData)); 169 if ($charttype == 'vertical') 170 $xaxis->setFontAngle('vertical'); 171 $xaxis->setLabelInterval(1); 172 $xaxis->setTickOptions(0, 0); 173 $xaxis->setLabelInterval(2, 2); 174 175 // set markers 176 if ($width > 400 && $height > 400) { 177 $marker = & $graph->addNew('value_marker', IMAGE_GRAPH_VALUE_Y); 178 $marker->setFillColor('[email protected]'); 179 $marker->setBorderColor('[email protected]'); 180 $marker->setFontSize(8); 181 // shift markers 20 pix right 182 if ($charttype == 'horizontal') { 183 $marker_pointing = & $graph->addNew('Image_Graph_Marker_Pointing', array(10, 0, & $marker)); 184 } else { 185 $marker_pointing = & $graph->addNew('Image_Graph_Marker_Pointing', array(0, -10, & $marker)); 186 } 187 $marker_pointing->setLineColor('[email protected]'); 188 $bplot->setMarker($marker_pointing); 189 } 190 191 //Getting the graph in the form of html page 192 $img = $graph->done( 193 array( 194 'tohtml' => true, 195 'border' => 0, 196 'filename' => $cache_file_name, 197 'filepath' => '', 198 'urlpath' => '' 199 )); 200 201 return $img; 202 } 203 204 // Function to generate Pie Chart 205 public static function getPieChart($xaxisData, $yaxisData, $title='', $width='', $height='', $charttype='vertical', $cachedFileName=false, $target=false, $color='') { 206 207 global $log, $lang_crm, $default_charset; 208 209 require_once ('include/utils/utils.php'); 210 require_once ('include/utils/GraphUtils.php'); 211 include_once ('Image/Graph.php'); 212 include_once ('Image/Canvas.php'); 213 214 if ($cachedFileName === false) { 215 $cache_file_name = 'cache/images/pie_chart_' . time() . '.png'; 216 } else { 217 $cache_file_name = $cachedFileName; 218 } 219 220 if (empty($width)) 221 $width = '500'; 222 if (empty($height)) 223 $height = '400'; 224 if ($target === false) 225 $target = array(); 226 227 $alts = array(); 228 $temp = array(); 229 for ($i = 0; $i < count($xaxisData); $i++) { 230 $name = html_entity_decode($xaxisData[$i], ENT_QUOTES, $default_charset); 231 $pos = substr_count($name, " "); 232 $alts[] = $name; 233 //If the datax value of a string is greater, adding '\n' to it so that it'll come in 2nd line 234 if (strlen($name) >= 14) 235 $name = substr($name, 0, 34); 236 if ($pos >= 2) { 237 $val = explode(" ", $name); 238 $n = count($val) - 1; 239 $x = ""; 240 for ($j = 0; $j < count($val); $j++) { 241 if ($j != $n) { 242 $x .=" " . $val[$j]; 243 } else { 244 $x .= "@#" . $val[$j]; 245 } 246 } 247 $name = $x; 248 } 249 $name = str_replace("@#", "\n", $name); 250 $temp[] = $name; 251 } 252 $xaxisData = $temp; 253 $width = $width + ($width / 5); 254 255 $canvas = & Image_Canvas::factory('png', array('width' => $width, 'height' => $height, 'usemap' => true)); 256 $imagemap = $canvas->getImageMap(); 257 $graph = & Image_Graph::factory('graph', $canvas); 258 $font = & $graph->addNew('font', calculate_font_name($lang_crm)); 259 $font->setSize(8); 260 $font->setColor($color); 261 $graph->setFont($font); 262 // create the plotarea layout 263 $title = & Image_Graph::factory('title', array($title, 10)); 264 $plotarea = & Image_Graph::factory('plotarea', array( 265 'category', 266 'axis' 267 )); 268 $graph->add(Image_Graph::vertical($title, $plotarea, 5)); 269 // To create unique lables we need to keep track of lable name and its count 270 $uniquex = array(); 271 // Generate colours 272 $colors = color_generator(count($yaxisData), '#33DDFF', '#3322FF'); 273 $dataset = & Image_Graph::factory('dataset'); 274 $fills = & Image_Graph::factory('Image_Graph_Fill_Array'); 275 $sum = 0; 276 $pcvalues = array(); 277 for ($i = 0; $i < count($yaxisData); $i++) { 278 $sum += $yaxisData[$i]; 279 } 280 for ($i = 0; $i < count($yaxisData); $i++) { 281 // To have unique names even in case of duplicates let us add the id 282 $datalabel = $xaxisData[$i]; 283 $xaxisData_appearance = $uniquex[$xaxisData[$i]]; 284 if ($xaxisData_appearance == null) { 285 $uniquex[$xaxisData[$i]] = 1; 286 } else { 287 $datalabel = $xaxisData[$i] . ' [' . $xaxisData_appearance . ']'; 288 $uniquex[$xaxisData[$i]] = $xaxisData_appearance + 1; 289 } 290 $dataset->addPoint( 291 $datalabel, 292 $yaxisData[$i], 293 array( 294 'url' => $target[$i], 295 'alt' => $alts[$i] . '=' . sprintf('%0.1f%%', 100 * $yaxisData[$i] / $sum) 296 ) 297 ); 298 $pcvalues[$yaxisData[$i]] = sprintf('%0.1f%%', 100 * $yaxisData[$i] / $sum); 299 $fills->addColor($colors[$i]); 300 } 301 if ($sum == 0) 302 return null; 303 // create the pie chart and associate the filling colours 304 $gbplot = & $plotarea->addNew('pie', $dataset); 305 $plotarea->setPadding(array('top' => 0, 'bottom' => 0, 'left' => 0, 'right' => ($width / 20))); 306 $plotarea->hideAxis(); 307 $gbplot->setFillStyle($fills); 308 // format the data values 309 $marker_array = & Image_Graph::factory('Image_Graph_DataPreprocessor_Array', array($pcvalues)); 310 // set markers 311 $marker = & $graph->addNew('value_marker', IMAGE_GRAPH_VALUE_Y); 312 $marker->setDataPreprocessor($marker_array); 313 $marker->setFillColor('#FFFFFF'); 314 $marker->setBorderColor($color); 315 $marker->setFontColor($color); 316 $marker->setFontSize(8); 317 $pointingMarker = & $graph->addNew('Image_Graph_Marker_Pointing_Angular', array(20, &$marker)); 318 $gbplot->setMarker($pointingMarker); 319 $legend_box = & $plotarea->addNew('legend'); 320 $legend_box->setPadding(array('top' => 20, 'bottom' => 0, 'left' => 0, 'right' => 0)); 321 $legend_box->setFillColor('#F5F5F5'); 322 $legend_box->showShadow(); 323 324 $img = $graph->done(array( 325 'tohtml' => true, 326 'border' => 0, 327 'filename' => $cache_file_name, 328 'filepath' => '', 329 'urlpath' => '' 330 )); 331 return $img; 332 } 333 334 //Generates Chart Data in form of an array from the Query Result of reports 335 public static function generateChartDataFromReports($queryResult, $groupbyField, $fieldDetails='', $reportid='') { 336 require_once 'modules/Reports/CustomReportUtils.php'; 337 require_once ('include/Webservices/Utils.php'); 338 require_once ('include/Webservices/Query.php'); 339 global $adb, $current_user, $theme, $default_charset; 340 $inventorymodules = array('Quotes', 'SalesOrder', 'PurchaseOrder', 'Invoice', 'Products', 'PriceBooks', 'Vendors', 'Services'); 341 $rows = $adb->num_rows($queryResult); 342 $condition = "is"; 343 $current_theme = $theme; 344 $groupByFields = array(); 345 $yaxisArray = array(); 346 $ChartDataArray = array(); 347 $target_val = array(); 348 349 $report = new ReportRun($reportid); 350 $restrictedModules = array(); 351 if($report->secondarymodule!='') { 352 $reportModules = explode(":",$report->secondarymodule); 353 } else { 354 $reportModules = array(); 355 } 356 array_push($reportModules,$report->primarymodule); 357 358 $restrictedModules = false; 359 foreach($reportModules as $mod) { 360 if(isPermitted($mod,'index') != "yes" || vtlib_isModuleActive($mod) == false) { 361 if(!is_array($restrictedModules)) $restrictedModules = array(); 362 $restrictedModules[] = $mod; 363 } 364 } 365 366 if(is_array($restrictedModules) && count($restrictedModules) > 0) { 367 $ChartDataArray['error'] = "<h4>".getTranslatedString('LBL_NO_ACCESS', 'Reports').' - '.implode(',', $restrictedModules)."</h4>"; 368 return $ChartDataArray; 369 } 370 371 if ($fieldDetails != '') { 372 list($tablename, $colname, $module_field, $fieldname, $single) = explode(":", $fieldDetails); 373 list($module, $field) = split("_", $module_field); 374 $dateField = false; 375 if ($single == 'D') { 376 $dateField = true; 377 $query = "SELECT * FROM vtiger_reportgroupbycolumn WHERE reportid=? ORDER BY sortid"; 378 $result = $adb->pquery($query, array($reportid)); 379 $criteria = $adb->query_result($result, 0, 'dategroupbycriteria'); 380 } 381 } 382 preg_match('/&/', $groupbyField, $matches); 383 if (!empty($matches)) { 384 $groupfield = str_replace('&', '&', $groupbyField); 385 $groupbyField = $report->replaceSpecialChar($groupfield); 386 } 387 $handler = vtws_getModuleHandlerFromName($module, $current_user); 388 $meta = $handler->getMeta(); 389 $meta->retrieveMeta(); 390 $referenceFields = $meta->getReferenceFieldDetails(); 391 392 if($rows > 0) { 393 $resultRow = $adb->query_result_rowdata($queryResult, 0); 394 if(!array_key_exists($groupbyField, $resultRow)) { 395 $ChartDataArray['error'] = "<h4>".getTranslatedString('LBL_NO_PERMISSION_FIELD', 'Dashboard')."</h4>"; 396 return $ChartDataArray; 397 } 398 } 399 for ($i = 0; $i < $rows; $i++) { 400 $groupFieldValue = $adb->query_result($queryResult, $i, strtolower($groupbyField)); 401 $decodedGroupFieldValue = html_entity_decode($groupFieldValue, ENT_QUOTES, $default_charset); 402 if (!empty($groupFieldValue)) { 403 if (in_array($module_field, $report->append_currency_symbol_to_value)) { 404 $valueComp = explode('::', $groupFieldValue); 405 $groupFieldValue = $valueComp[1]; 406 } 407 if ($dateField) { 408 if (!empty($groupFieldValue)) 409 $groupByFields[] = CustomReportUtils::getXAxisDateFieldValue($groupFieldValue, $criteria); 410 else 411 $groupByFields[] = "Null"; 412 } 413 else if (in_array($fieldname, array_keys($referenceFields))) { 414 if (count($referenceFields[$fieldname]) > 1) { 415 $refenceModule = CustomReportUtils::getEntityTypeFromName($decodedGroupFieldValue, $referenceFields[$fieldname]); 416 } 417 else { 418 $refenceModule = $referenceFields[$fieldname][0]; 419 } 420 $groupByFields[] = $groupFieldValue; 421 422 if ($fieldname == 'currency_id' && in_array($module, $inventorymodules)) { 423 $tablename = 'vtiger_currency_info'; 424 } elseif ($refenceModule == 'DocumentFolders' && $fieldname == 'folderid') { 425 $tablename = 'vtiger_attachmentsfolder'; 426 $colname = 'foldername'; 427 } else { 428 require_once "modules/$refenceModule/$refenceModule.php"; 429 $focus = new $refenceModule(); 430 $tablename = $focus->table_name; 431 $colname = $focus->list_link_field; 432 $condition = "c"; 433 } 434 } else { 435 $groupByFields[] = $groupFieldValue; 436 } 437 $yaxisArray[] = $adb->query_result($queryResult, $i, 'groupby_count'); 438 if ($fieldDetails != '') { 439 if ($dateField) { 440 $advanceSearchCondition = CustomReportUtils::getAdvanceSearchCondition($fieldDetails, $criteria, $groupFieldValue); 441 if ($module == 'Calendar') { 442 $link_val = "index.php?module=" . $module . "&query=true&action=ListView&" . $advanceSearchCondition; 443 }else 444 $link_val = "index.php?module=" . $module . "&query=true&action=index&" . $advanceSearchCondition; 445 } 446 else { 447 $cvid = getCvIdOfAll($module); 448 $esc_search_str = urlencode($decodedGroupFieldValue); 449 if ($single == 'DT') { 450 $esc_search_str = urlencode($groupFieldValue); 451 if (strtolower($fieldname) == 'modifiedtime' || strtolower($fieldname) == 'createdtime') { 452 $tablename = 'vtiger_crmentity'; 453 $colname = $fieldname; 454 } 455 } 456 if ($fieldname == 'assigned_user_id') { 457 $tablename = 'vtiger_crmentity'; 458 $colname = 'smownerid'; 459 } 460 if ($fieldname == 'serviceid' && in_array($module, getInventoryModules())) { 461 $fieldname = 'productid'; 462 } 463 if ($module == 'Calendar') { 464 $link_val = "index.php?module=" . $module . "&action=ListView&search_text=" . $esc_search_str . "&search_field=" . $fieldname . "&searchtype=BasicSearch&query=true&operator=e&viewname=" . $cvid; 465 } else { 466 $link_val = "index.php?module=" . $module . "&action=index&search_text=" . $esc_search_str . "&search_field=" . $fieldname . "&searchtype=BasicSearch&query=true&operator=e&viewname=" . $cvid; 467 } 468 } 469 470 $target_val[] = $link_val; 471 } 472 } 473 } 474 if(count($groupByFields) == 0) { 475 $ChartDataArray['error'] = "<div class='componentName'>".getTranslatedString('LBL_NO_DATA', 'Reports')."</div"; 476 } 477 $ChartDataArray['xaxisData'] = $groupByFields; 478 $ChartDataArray['yaxisData'] = $yaxisArray; 479 $ChartDataArray['targetLink'] = $target_val; 480 $theme = $current_theme; 481 return $ChartDataArray; 482 } 483 484 public static function getReportBarChart($queryResult, $groupbyField, $fieldDetails, $reportid, $charttype='horizontal') { 485 global $theme; 486 $BarChartDetails = self::generateChartDataFromReports($queryResult, $groupbyField, $fieldDetails, $reportid); 487 $groupbyFields = $BarChartDetails['xaxisData']; 488 $yaxisArray = $BarChartDetails['yaxisData']; 489 $targerLinks = $BarChartDetails['targetLink']; 490 if ($theme == "softed") { 491 $font_color = "#212473"; 492 } else { 493 $font_color = "#000000"; 494 } 495 if(!empty($BarChartDetails['error'])) { 496 return $BarChartDetails['error']; 497 } else { 498 $barChart = ChartUtils::getBarChart($groupbyFields, $yaxisArray, '', '350', '300', $charttype, false, $targerLinks, $font_color); 499 return $barChart; 500 } 501 } 502 503 public static function getReportPieChart($queryResult, $groupbyField, $fieldDetails, $reportid) { 504 global $theme; 505 $PieChartDetails = self::generateChartDataFromReports($queryResult, $groupbyField, $fieldDetails, $reportid); 506 $groupbyFields = $PieChartDetails['xaxisData']; 507 $yaxisArray = $PieChartDetails['yaxisData']; 508 $targerLinks = $PieChartDetails['targetLink']; 509 $charttype = 'vertical'; 510 if ($theme == "softed") { 511 $font_color = "#212473"; 512 } else { 513 $font_color = "#000000"; 514 } 515 if(!empty($PieChartDetails['error'])) { 516 return $PieChartDetails['error']; 517 } else { 518 $pieChart = ChartUtils::getPieChart($groupbyFields, $yaxisArray, '', '350', '300', $charttype, false, $targerLinks, $font_color); 519 return $pieChart; 520 } 521 } 522 523 } 524 525 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:08:37 2014 | Cross-referenced by PHPXref 0.7.1 |