PageRenderTime 34ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/Charts/Dashlets/OpportunitiesByLeadSourceDashlet/OpportunitiesByLeadSourceDashlet.php

https://bitbucket.org/allexblacker/suitecrm
PHP | 254 lines | 138 code | 33 blank | 83 comment | 13 complexity | 1f2a5f548f8f028acdf44bc9b4462614 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, BSD-3-Clause, LGPL-2.1
  1. <?php
  2. if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
  3. /*********************************************************************************
  4. * SugarCRM Community Edition is a customer relationship management program developed by
  5. * SugarCRM, Inc. Copyright (C) 2004-2013 SugarCRM Inc.
  6. * SuiteCRM is an extension to SugarCRM Community Edition developed by Salesagility Ltd.
  7. * Copyright (C) 2011 - 2014 Salesagility Ltd.
  8. *
  9. * This program is free software; you can redistribute it and/or modify it under
  10. * the terms of the GNU Affero General Public License version 3 as published by the
  11. * Free Software Foundation with the addition of the following permission added
  12. * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
  13. * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
  14. * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
  15. *
  16. * This program is distributed in the hope that it will be useful, but WITHOUT
  17. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  18. * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  19. * details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License along with
  22. * this program; if not, see http://www.gnu.org/licenses or write to the Free
  23. * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  24. * 02110-1301 USA.
  25. *
  26. * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
  27. * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
  28. *
  29. * The interactive user interfaces in modified source and object code versions
  30. * of this program must display Appropriate Legal Notices, as required under
  31. * Section 5 of the GNU Affero General Public License version 3.
  32. *
  33. * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
  34. * these Appropriate Legal Notices must retain the display of the "Powered by
  35. * SugarCRM" logo and "Supercharged by SuiteCRM" logo. If the display of the logos is not
  36. * reasonably feasible for technical reasons, the Appropriate Legal Notices must
  37. * display the words "Powered by SugarCRM" and "Supercharged by SuiteCRM".
  38. ********************************************************************************/
  39. require_once('include/Dashlets/DashletGenericChart.php');
  40. class OpportunitiesByLeadSourceDashlet extends DashletGenericChart
  41. {
  42. public $pbls_lead_sources = array();
  43. public $pbls_ids = array();
  44. /**
  45. * @see DashletGenericChart::$_seedName
  46. */
  47. protected $_seedName = 'Opportunities';
  48. /**
  49. * @see DashletGenericChart::displayOptions()
  50. */
  51. public function displayOptions()
  52. {
  53. global $app_list_strings;
  54. $selected_datax = array();
  55. if (!empty($this->pbls_lead_sources) && sizeof($this->pbls_lead_sources) > 0)
  56. foreach ($this->pbls_lead_sources as $key)
  57. $selected_datax[] = $key;
  58. else
  59. $selected_datax = array_keys($app_list_strings['lead_source_dom']);
  60. $this->_searchFields['pbls_lead_sources']['options'] = array_filter($app_list_strings['lead_source_dom']);
  61. $this->_searchFields['pbls_lead_sources']['input_name0'] = $selected_datax;
  62. if (!isset($this->pbls_ids) || count($this->pbls_ids) == 0)
  63. $this->_searchFields['pbls_ids']['input_name0'] = array_keys(get_user_array(false));
  64. return parent::displayOptions();
  65. }
  66. /**
  67. * @see DashletGenericChart::display()
  68. */
  69. public function display()
  70. {
  71. global $current_user, $sugar_config;
  72. $currency_symbol = $sugar_config['default_currency_symbol'];
  73. if ($current_user->getPreference('currency')){
  74. $currency = new Currency();
  75. $currency->retrieve($current_user->getPreference('currency'));
  76. $currency_symbol = $currency->symbol;
  77. }
  78. $thousands_symbol = translate('LBL_OPP_THOUSANDS', 'Charts');
  79. $data = $this->getChartData($this->constructQuery());
  80. $chartReadyData = $this->prepareChartData($data, $currency_symbol, $thousands_symbol);
  81. $canvasId = 'rGraphLeadSource'.uniqid();
  82. $chartWidth = 900;
  83. $chartHeight = 500;
  84. $jsonData = json_encode($chartReadyData['data']);
  85. $jsonLabels = json_encode($chartReadyData['labels']);
  86. $jsonLabelsAndValues = json_encode($chartReadyData['labelsAndValues']);
  87. $autoRefresh = $this->processAutoRefresh();
  88. $module = 'Opportunities';
  89. $action = 'index';
  90. $query ='true';
  91. $searchFormTab ='advanced_search';
  92. $colours = "['#a6cee3','#1f78b4','#b2df8a','#33a02c','#fb9a99','#e31a1c','#fdbf6f','#ff7f00','#cab2d6','#6a3d9a','#ffff99','#b15928','#8080ff','#c03f80']";
  93. if(!is_array($chartReadyData['data'])||count($chartReadyData['data']) < 1)
  94. {
  95. return "<h3 class='noGraphDataPoints'>$this->noDataMessage</h3>";
  96. }
  97. //<canvas id='$canvasId' width='$chartWidth' height='$chartHeight' class='resizableCanvas' style='width: 100%;'>[No canvas support]</canvas>
  98. //<canvas id='$canvasId' width=canvas.width height=canvas.width class='resizableCanvas'>[No canvas support]</canvas>
  99. $chart = <<<EOD
  100. <canvas id='$canvasId' width='$chartWidth' height='$chartHeight' class='resizableCanvas' >[No canvas support]</canvas>
  101. <input type='hidden' class='module' value='$module' />
  102. <input type='hidden' class='action' value='$action' />
  103. <input type='hidden' class='query' value='$query' />
  104. <input type='hidden' class='searchFormTab' value='$searchFormTab' />
  105. $autoRefresh
  106. <script>
  107. var pie = new RGraph.Pie({
  108. id: '$canvasId',
  109. data: $jsonData,
  110. options: {
  111. strokestyle: '#e8e8e8',
  112. linewidth: 2,
  113. eventsMousemove:rgraphMouseMove,
  114. eventsClick:opportunitiesByLeadSourceDashletClick,
  115. shadowBlur: 5,
  116. tooltips:$jsonLabels,
  117. tooltipsEvent:'mousemove',
  118. shadowOffsetx: 5,
  119. shadowOffsety: 5,
  120. shadowColor: '#aaa',
  121. centerx:true,
  122. key: $jsonLabelsAndValues,
  123. labels:$jsonLabels,
  124. keyPosition:'graph',
  125. keyPositionX:0,
  126. keyBackground:'rgba(255,255,255,0.7)',
  127. colors:$colours,
  128. textSize:10,
  129. tooltipsCssClass: 'rgraph_chart_tooltips_css',
  130. keyColors:$colours
  131. //keyInteractive: true
  132. }
  133. }).draw();
  134. pie.set({
  135. contextmenu: [
  136. ['Get PNG', RGraph.showPNG],
  137. null,
  138. ['Cancel', function () {}]
  139. ]
  140. });
  141. </script>
  142. EOD;
  143. return $chart;
  144. /*
  145. require("modules/Charts/chartdefs.php");
  146. $chartDef = $chartDefs['pipeline_by_lead_source'];
  147. require_once('include/SugarCharts/SugarChartFactory.php');
  148. $sugarChart = SugarChartFactory::getInstance();
  149. $sugarChart->is_currency = true;
  150. $currency_symbol = $sugar_config['default_currency_symbol'];
  151. if ($current_user->getPreference('currency')){
  152. $currency = new Currency();
  153. $currency->retrieve($current_user->getPreference('currency'));
  154. $currency_symbol = $currency->symbol;
  155. }
  156. $subtitle = translate('LBL_OPP_SIZE', 'Charts') . " " . $currency_symbol . "1" . translate('LBL_OPP_THOUSANDS', 'Charts');
  157. $sugarChart->setProperties('', $subtitle, $chartDef['chartType']);
  158. $sugarChart->base_url = $chartDef['base_url'];
  159. $sugarChart->group_by = $chartDef['groupBy'];
  160. $sugarChart->url_params = array();
  161. if ( count($this->pbls_ids) > 0 )
  162. $sugarChart->url_params['assigned_user_id'] = array_values($this->pbls_ids);
  163. $sugarChart->getData($this->constructQuery());
  164. $sugarChart->data_set = $sugarChart->sortData($sugarChart->data_set, 'lead_source', true);
  165. $xmlFile = $sugarChart->getXMLFileName($this->id);
  166. $sugarChart->saveXMLFile($xmlFile, $sugarChart->generateXML());
  167. return $this->getTitle('<div align="center"></div>') .
  168. '<div align="center">' . $sugarChart->display($this->id, $xmlFile, '100%', '480', false) . '</div>'. $this->processAutoRefresh();
  169. */
  170. }
  171. function getChartData($query)
  172. {
  173. global $app_list_strings, $db;
  174. $dataSet = array();
  175. $result = $db->query($query);
  176. $row = $db->fetchByAssoc($result);
  177. while ($row != null){
  178. $dataSet[] = $row;
  179. $row = $db->fetchByAssoc($result);
  180. }
  181. return $dataSet;
  182. }
  183. protected function prepareChartData($data,$currency_symbol, $thousands_symbol)
  184. {
  185. //return $data;
  186. $chart['labels']=array();
  187. $chart['data']=array();
  188. $total = 0;
  189. foreach($data as $i)
  190. {
  191. $chart['labelsAndValues'][]=$i['lead_source'].' ('.$currency_symbol.(int)$i['total'].$thousands_symbol.')';
  192. //$chart['labelsAndValues'][]=$currency_symbol.(int)$i['total'].$thousands_symbol;
  193. $chart['labels'][]=$i['lead_source'];
  194. $chart['data'][]=(int)$i['total'];
  195. $total+=(int)$i['total'];
  196. }
  197. $chart['total']=$total;
  198. return $chart;
  199. }
  200. /**
  201. * @see DashletGenericChart::constructQuery()
  202. */
  203. protected function constructQuery()
  204. {
  205. $query = "SELECT lead_source,sum(amount_usdollar/1000) as total,count(*) as opp_count ".
  206. "FROM opportunities ";
  207. $query .= "WHERE opportunities.deleted=0 ";
  208. if ( count($this->pbls_ids) > 0 )
  209. $query .= "AND opportunities.assigned_user_id IN ('".implode("','",$this->pbls_ids)."') ";
  210. if ( count($this->pbls_lead_sources) > 0 )
  211. $query .= "AND opportunities.lead_source IN ('".implode("','",$this->pbls_lead_sources)."') ";
  212. else
  213. $query .= "AND opportunities.lead_source IN ('".implode("','",array_keys($GLOBALS['app_list_strings']['lead_source_dom']))."') ";
  214. $query .= "GROUP BY lead_source ORDER BY total DESC";
  215. return $query;
  216. }
  217. }