PageRenderTime 50ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/include/Dashlets/DashletGenericChart.php

https://github.com/joshbhamilton/sugarcrm_dev
PHP | 354 lines | 183 code | 46 blank | 125 comment | 37 complexity | 4ba364f711dc6a9e295298c9e685b00f MD5 | raw file
  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-2011 SugarCRM Inc.
  6. *
  7. * This program is free software; you can redistribute it and/or modify it under
  8. * the terms of the GNU Affero General Public License version 3 as published by the
  9. * Free Software Foundation with the addition of the following permission added
  10. * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
  11. * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
  12. * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
  13. *
  14. * This program is distributed in the hope that it will be useful, but WITHOUT
  15. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  16. * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  17. * details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License along with
  20. * this program; if not, see http://www.gnu.org/licenses or write to the Free
  21. * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  22. * 02110-1301 USA.
  23. *
  24. * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
  25. * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
  26. *
  27. * The interactive user interfaces in modified source and object code versions
  28. * of this program must display Appropriate Legal Notices, as required under
  29. * Section 5 of the GNU Affero General Public License version 3.
  30. *
  31. * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
  32. * these Appropriate Legal Notices must retain the display of the "Powered by
  33. * SugarCRM" logo. If the display of the logo is not reasonably feasible for
  34. * technical reasons, the Appropriate Legal Notices must display the words
  35. * "Powered by SugarCRM".
  36. ********************************************************************************/
  37. require_once('include/Dashlets/Dashlet.php');
  38. require_once('include/generic/LayoutManager.php');
  39. abstract class DashletGenericChart extends Dashlet
  40. {
  41. /**
  42. * The title of the dashlet
  43. * @var string
  44. */
  45. public $title;
  46. /**
  47. * @see Dashlet::$isConfigurable
  48. */
  49. public $isConfigurable = true;
  50. /**
  51. * @see Dashlet::$isRefreshable
  52. */
  53. public $isRefreshable = true;
  54. /**
  55. * location of smarty template file for configuring
  56. * @var string
  57. */
  58. protected $_configureTpl = 'include/Dashlets/DashletGenericChartConfigure.tpl';
  59. /**
  60. * Bean file used in this Dashlet
  61. * @var object
  62. */
  63. private $_seedBean;
  64. /**
  65. * Module used in this Dashlet
  66. * @var string
  67. */
  68. protected $_seedName;
  69. /**
  70. * Array of fields and thier defintions that we are searching on
  71. * @var array
  72. */
  73. protected $_searchFields;
  74. /**
  75. * smarty object for the generic configuration template
  76. * @var object
  77. */
  78. private $_configureSS;
  79. /**
  80. * Constructor
  81. *
  82. * @param int $id
  83. * @param array $options
  84. */
  85. public function __construct(
  86. $id,
  87. array $options = null
  88. )
  89. {
  90. parent::Dashlet($id);
  91. if ( isset($options) ) {
  92. foreach ( $options as $key => $value ) {
  93. $this->$key = $value;
  94. }
  95. }
  96. // load searchfields
  97. $classname = get_class($this);
  98. if ( is_file("modules/Charts/Dashlets/$classname/$classname.data.php") ) {
  99. require("modules/Charts/Dashlets/$classname/$classname.data.php");
  100. $this->_searchFields = $dashletData[$classname]['searchFields'];
  101. }
  102. // load language files
  103. $this->loadLanguage($classname, 'modules/Charts/Dashlets/');
  104. if ( empty($options['title']) )
  105. $this->title = $this->dashletStrings['LBL_TITLE'];
  106. if ( isset($options['autoRefresh']) )
  107. $this->autoRefresh = $options['autoRefresh'];
  108. $this->layoutManager = new LayoutManager();
  109. $this->layoutManager->setAttribute('context', 'Report');
  110. // fake a reporter object here just to pass along the db type used in many widgets.
  111. // this should be taken out when sugarwidgets change
  112. $temp = (object) array('db' => &$GLOBALS['db'], 'report_def_str' => '');
  113. $this->layoutManager->setAttributePtr('reporter', $temp);
  114. }
  115. /**
  116. * @see Dashlet::setRefreshIcon()
  117. */
  118. public function setRefreshIcon()
  119. {
  120. $additionalTitle = '';
  121. if($this->isRefreshable)
  122. $additionalTitle .= '<a href="#" onclick="SUGAR.mySugar.retrieveDashlet(\''
  123. . $this->id . '\',\'predefined_chart\'); return false;"><img border="0" align="absmiddle" title="' . translate('LBL_DASHLET_REFRESH', 'Home') . '" alt="' . translate('LBL_DASHLET_REFRESH', 'Home') . '" src="'
  124. . SugarThemeRegistry::current()->getImageURL('dashlet-header-refresh.png').'"/></a>';
  125. return $additionalTitle;
  126. }
  127. /**
  128. * Displays the javascript for the dashlet
  129. *
  130. * @return string javascript to use with this dashlet
  131. */
  132. public function displayScript()
  133. {
  134. require_once('include/SugarCharts/SugarChartFactory.php');
  135. $sugarChart = SugarChartFactory::getInstance();
  136. return $sugarChart->getDashletScript($this->id);
  137. }
  138. /**
  139. * Gets the smarty object for the config window. Designed to allow lazy loading the object
  140. * when it's needed.
  141. */
  142. protected function getConfigureSmartyInstance()
  143. {
  144. if ( !($this->_configureSS instanceof Sugar_Smarty) ) {
  145. $this->_configureSS = new Sugar_Smarty();
  146. }
  147. return $this->_configureSS;
  148. }
  149. /**
  150. * Saves the chart config options
  151. * Filter the $_REQUEST and only save only the needed options
  152. *
  153. * @param array $req
  154. * @return array
  155. */
  156. public function saveOptions(
  157. $req
  158. )
  159. {
  160. global $timedate;
  161. $options = array();
  162. foreach($req as $name => $value)
  163. if(!is_array($value)) $req[$name] = trim($value);
  164. foreach($this->_searchFields as $name => $params) {
  165. $widgetDef = $params;
  166. if ( isset($this->getSeedBean()->field_defs[$name]) )
  167. $widgetDef = $this->getSeedBean()->field_defs[$name];
  168. if ( $widgetDef['type'] == 'date') // special case date types
  169. $options[$widgetDef['name']] = $timedate->swap_formats($req['type_'.$widgetDef['name']], $timedate->get_date_format(), $timedate->dbDayFormat);
  170. elseif ( $widgetDef['type'] == 'time') // special case time types
  171. $options[$widgetDef['name']] = $timedate->swap_formats($req['type_'.$widgetDef['name']], $timedate->get_time_format(), $timedate->dbTimeFormat);
  172. elseif ( $widgetDef['type'] == 'datepicker') // special case datepicker types
  173. $options[$widgetDef['name']] = $timedate->swap_formats($req[$widgetDef['name']], $timedate->get_date_format(), $timedate->dbDayFormat);
  174. elseif (!empty($req[$widgetDef['name']]))
  175. $options[$widgetDef['name']] = $req[$widgetDef['name']];
  176. }
  177. if (!empty($req['dashletTitle']))
  178. $options['title'] = $req['dashletTitle'];
  179. $options['autoRefresh'] = empty($req['autoRefresh']) ? '0' : $req['autoRefresh'];
  180. return $options;
  181. }
  182. /**
  183. * Handles displaying the chart dashlet configuration popup window
  184. *
  185. * @return string HTML to return to the browser
  186. */
  187. public function displayOptions()
  188. {
  189. $currentSearchFields = array();
  190. if ( is_array($this->_searchFields) ) {
  191. foreach($this->_searchFields as $name=>$params) {
  192. if(!empty($name)) {
  193. $name = strtolower($name);
  194. $currentSearchFields[$name] = array();
  195. $widgetDef = $params;
  196. if ( isset($this->getSeedBean()->field_defs[$name]) )
  197. $widgetDef = $this->getSeedBean()->field_defs[$name];
  198. if($widgetDef['type'] == 'enum' || $widgetDef['type'] == 'singleenum') $widgetDef['remove_blank'] = true; // remove the blank option for the dropdown
  199. if ( empty($widgetDef['input_name0']) )
  200. $widgetDef['input_name0'] = empty($this->$name) ? '' : $this->$name;
  201. $currentSearchFields[$name]['label'] = translate($widgetDef['vname'], $this->getSeedBean()->module_dir);
  202. if ( $currentSearchFields[$name]['label'] == $widgetDef['vname'] )
  203. $currentSearchFields[$name]['label'] = translate($widgetDef['vname'], 'Charts');
  204. $currentSearchFields[$name]['input'] = $this->layoutManager->widgetDisplayInput($widgetDef, true, (empty($this->$name) ? '' : $this->$name));
  205. }
  206. else { // ability to create spacers in input fields
  207. $currentSearchFields['blank' + $count]['label'] = '';
  208. $currentSearchFields['blank' + $count]['input'] = '';
  209. $count++;
  210. }
  211. }
  212. }
  213. $this->currentSearchFields = $currentSearchFields;
  214. $this->getConfigureSmartyInstance()->assign('title',translate('LBL_TITLE','Charts'));
  215. $this->getConfigureSmartyInstance()->assign('save',$GLOBALS['app_strings']['LBL_SAVE_BUTTON_LABEL']);
  216. $this->getConfigureSmartyInstance()->assign('id', $this->id);
  217. $this->getConfigureSmartyInstance()->assign('searchFields', $this->currentSearchFields);
  218. $this->getConfigureSmartyInstance()->assign('dashletTitle', $this->title);
  219. $this->getConfigureSmartyInstance()->assign('dashletType', 'predefined_chart');
  220. $this->getConfigureSmartyInstance()->assign('module', $_REQUEST['module']);
  221. if($this->isAutoRefreshable()) {
  222. $this->getConfigureSmartyInstance()->assign('isRefreshable', true);
  223. $this->getConfigureSmartyInstance()->assign('autoRefresh', $GLOBALS['app_strings']['LBL_DASHLET_CONFIGURE_AUTOREFRESH']);
  224. $this->getConfigureSmartyInstance()->assign('autoRefreshOptions', $this->getAutoRefreshOptions());
  225. $this->getConfigureSmartyInstance()->assign('autoRefreshSelect', $this->autoRefresh);
  226. }
  227. return parent::displayOptions() . $this->getConfigureSmartyInstance()->fetch($this->_configureTpl);
  228. }
  229. /**
  230. * Returns the DashletGenericChart::_seedBean object. Designed to allow lazy loading the object
  231. * when it's needed.
  232. *
  233. * @return object
  234. */
  235. protected function getSeedBean()
  236. {
  237. if ( !($this->_seedBean instanceof SugarBean) )
  238. $this->_seedBean = SugarModule::get($this->_seedName)->loadBean();
  239. return $this->_seedBean;
  240. }
  241. /**
  242. * Returns the built query read to feed into SugarChart::getData()
  243. *
  244. * @return string SQL query
  245. */
  246. protected function constructQuery()
  247. {
  248. return '';
  249. }
  250. /**
  251. * Returns the array of group by parameters for SugarChart::$group_by
  252. *
  253. * @return string SQL query
  254. */
  255. protected function constructGroupBy()
  256. {
  257. return array();
  258. }
  259. /**
  260. * Displays the Dashlet, must call process() prior to calling this
  261. *
  262. * @return string HTML that displays Dashlet
  263. */
  264. public function display()
  265. {
  266. return parent::display() . $this->processAutoRefresh();
  267. }
  268. /**
  269. * Processes and displays the auto refresh code for the dashlet
  270. *
  271. * @param int $dashletOffset
  272. * @return string HTML code
  273. */
  274. protected function processAutoRefresh($dashletOffset = 0)
  275. {
  276. global $sugar_config;
  277. if ( empty($dashletOffset) ) {
  278. $dashletOffset = 0;
  279. $module = $_REQUEST['module'];
  280. if(isset($_REQUEST[$module.'2_'.strtoupper($this->getSeedBean()->object_name).'_offset'])) {
  281. $dashletOffset = $_REQUEST[$module.'2_'.strtoupper($this->getSeedBean()->object_name).'_offset'];
  282. }
  283. }
  284. if ( !$this->isRefreshable ) {
  285. return '';
  286. }
  287. if ( !empty($sugar_config['dashlet_auto_refresh_min']) && $sugar_config['dashlet_auto_refresh_min'] == -1 ) {
  288. return '';
  289. }
  290. $autoRefreshSS = new Sugar_Smarty();
  291. $autoRefreshSS->assign('dashletOffset', $dashletOffset);
  292. $autoRefreshSS->assign('dashletId', $this->id);
  293. $autoRefreshSS->assign('strippedDashletId', str_replace("-","",$this->id)); //javascript doesn't like "-" in function names
  294. if ( empty($this->autoRefresh) ) {
  295. $this->autoRefresh = 0;
  296. }
  297. elseif ( !empty($sugar_config['dashlet_auto_refresh_min']) ) {
  298. $this->autoRefresh = min($sugar_config['dashlet_auto_refresh_min'],$this->autoRefresh);
  299. }
  300. $autoRefreshSS->assign('dashletRefreshInterval', $this->autoRefresh * 1000);
  301. $autoRefreshSS->assign('url', "predefined_chart");
  302. $tpl = 'include/Dashlets/DashletGenericAutoRefresh.tpl';
  303. if ( $_REQUEST['action'] == "DynamicAction" ) {
  304. $tpl = 'include/Dashlets/DashletGenericAutoRefreshDynamic.tpl';
  305. }
  306. return $autoRefreshSS->fetch($tpl);
  307. }
  308. }