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.
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.
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
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
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.
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.
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 ********************************************************************************/
38 require_once('include/Dashlets/Dashlet.php');
39 require_once('include/generic/LayoutManager.php');
41 abstract class DashletGenericChart extends Dashlet
44 * The title of the dashlet
50 * @see Dashlet::$isConfigurable
52 public $isConfigurable = true;
55 * @see Dashlet::$isRefreshable
57 public $isRefreshable = true;
60 * location of smarty template file for configuring
63 protected $_configureTpl = 'include/Dashlets/DashletGenericChartConfigure.tpl';
66 * Bean file used in this Dashlet
72 * Module used in this Dashlet
78 * Array of fields and thier defintions that we are searching on
81 protected $_searchFields;
84 * smarty object for the generic configuration template
87 private $_configureSS;
94 * @param array $options
96 public function __construct(
101 parent::Dashlet($id);
103 if ( isset($options) ) {
104 foreach ( $options as $key => $value ) {
105 $this->$key = $value;
110 $classname = get_class($this);
111 if ( is_file("modules/Charts/Dashlets/$classname/$classname.data.php") ) {
112 require("modules/Charts/Dashlets/$classname/$classname.data.php");
113 $this->_searchFields = $dashletData[$classname]['searchFields'];
116 // load language files
117 $this->loadLanguage($classname, 'modules/Charts/Dashlets/');
119 if ( empty($options['title']) )
120 $this->title = $this->dashletStrings['LBL_TITLE'];
121 if ( isset($options['autoRefresh']) )
122 $this->autoRefresh = $options['autoRefresh'];
124 $this->layoutManager = new LayoutManager();
125 $this->layoutManager->setAttribute('context', 'Report');
126 // fake a reporter object here just to pass along the db type used in many widgets.
127 // this should be taken out when sugarwidgets change
128 $temp = (object) array('db' => &$GLOBALS['db'], 'report_def_str' => '');
129 $this->layoutManager->setAttributePtr('reporter', $temp);
133 * @see Dashlet::setRefreshIcon()
135 public function setRefreshIcon()
137 $additionalTitle = '';
138 if($this->isRefreshable)
140 $additionalTitle .= '<a href="#" onclick="SUGAR.mySugar.retrieveDashlet(\''
142 . '\',\'predefined_chart\'); return false;"><!--not_in_theme!-->'
143 . SugarThemeRegistry::current()->getImage(
144 'dashlet-header-refresh',
145 'border="0" align="absmiddle" title="'. translate('LBL_DASHLET_REFRESH', 'Home') . '"',
149 translate('LBL_DASHLET_REFRESH', 'Home')
152 return $additionalTitle;
156 * Displays the javascript for the dashlet
158 * @return string javascript to use with this dashlet
160 public function displayScript()
163 require_once('include/SugarCharts/SugarChartFactory.php');
164 $sugarChart = SugarChartFactory::getInstance();
165 return $sugarChart->getDashletScript($this->id);
170 * Gets the smarty object for the config window. Designed to allow lazy loading the object
173 protected function getConfigureSmartyInstance()
175 if ( !($this->_configureSS instanceof Sugar_Smarty) ) {
177 $this->_configureSS = new Sugar_Smarty();
180 return $this->_configureSS;
184 * Saves the chart config options
185 * Filter the $_REQUEST and only save only the needed options
190 public function saveOptions(
198 foreach($req as $name => $value)
199 if(!is_array($value)) $req[$name] = trim($value);
201 foreach($this->_searchFields as $name => $params) {
202 $widgetDef = $params;
203 if ( isset($this->getSeedBean()->field_defs[$name]) )
204 $widgetDef = $this->getSeedBean()->field_defs[$name];
205 if ( $widgetDef['type'] == 'date') // special case date types
206 $options[$widgetDef['name']] = $timedate->swap_formats($req['type_'.$widgetDef['name']], $timedate->get_date_format(), $timedate->dbDayFormat);
207 elseif ( $widgetDef['type'] == 'time') // special case time types
208 $options[$widgetDef['name']] = $timedate->swap_formats($req['type_'.$widgetDef['name']], $timedate->get_time_format(), $timedate->dbTimeFormat);
209 elseif ( $widgetDef['type'] == 'datepicker') // special case datepicker types
210 $options[$widgetDef['name']] = $timedate->swap_formats($req[$widgetDef['name']], $timedate->get_date_format(), $timedate->dbDayFormat);
211 elseif (!empty($req[$widgetDef['name']]))
212 $options[$widgetDef['name']] = $req[$widgetDef['name']];
215 if (!empty($req['dashletTitle']))
216 $options['title'] = $req['dashletTitle'];
218 $options['autoRefresh'] = empty($req['autoRefresh']) ? '0' : $req['autoRefresh'];
224 * Handles displaying the chart dashlet configuration popup window
226 * @return string HTML to return to the browser
228 public function displayOptions()
230 $currentSearchFields = array();
232 if ( is_array($this->_searchFields) ) {
233 foreach($this->_searchFields as $name=>$params) {
235 $name = strtolower($name);
236 $currentSearchFields[$name] = array();
238 $widgetDef = $params;
239 if ( isset($this->getSeedBean()->field_defs[$name]) )
240 $widgetDef = $this->getSeedBean()->field_defs[$name];
242 if($widgetDef['type'] == 'enum' || $widgetDef['type'] == 'singleenum') $widgetDef['remove_blank'] = true; // remove the blank option for the dropdown
244 if ( empty($widgetDef['input_name0']) )
245 $widgetDef['input_name0'] = empty($this->$name) ? '' : $this->$name;
246 $currentSearchFields[$name]['label'] = translate($widgetDef['vname'], $this->getSeedBean()->module_dir);
247 if ( $currentSearchFields[$name]['label'] == $widgetDef['vname'] )
248 $currentSearchFields[$name]['label'] = translate($widgetDef['vname'], 'Charts');
249 $currentSearchFields[$name]['input'] = $this->layoutManager->widgetDisplayInput($widgetDef, true, (empty($this->$name) ? '' : $this->$name));
251 else { // ability to create spacers in input fields
252 $currentSearchFields['blank' + $count]['label'] = '';
253 $currentSearchFields['blank' + $count]['input'] = '';
258 $this->currentSearchFields = $currentSearchFields;
259 $this->getConfigureSmartyInstance()->assign('title',translate('LBL_TITLE','Charts'));
260 $this->getConfigureSmartyInstance()->assign('save',$GLOBALS['app_strings']['LBL_SAVE_BUTTON_LABEL']);
261 $this->getConfigureSmartyInstance()->assign('clear',$GLOBALS['app_strings']['LBL_CLEAR_BUTTON_LABEL']);
262 $this->getConfigureSmartyInstance()->assign('id', $this->id);
263 $this->getConfigureSmartyInstance()->assign('searchFields', $this->currentSearchFields);
264 $this->getConfigureSmartyInstance()->assign('dashletTitle', $this->title);
265 $this->getConfigureSmartyInstance()->assign('dashletType', 'predefined_chart');
266 $this->getConfigureSmartyInstance()->assign('module', $_REQUEST['module']);
267 $this->getConfigureSmartyInstance()->assign('showClearButton', $this->isConfigPanelClearShown);
269 if($this->isAutoRefreshable()) {
270 $this->getConfigureSmartyInstance()->assign('isRefreshable', true);
271 $this->getConfigureSmartyInstance()->assign('autoRefresh', $GLOBALS['app_strings']['LBL_DASHLET_CONFIGURE_AUTOREFRESH']);
272 $this->getConfigureSmartyInstance()->assign('autoRefreshOptions', $this->getAutoRefreshOptions());
273 $this->getConfigureSmartyInstance()->assign('autoRefreshSelect', $this->autoRefresh);
276 return parent::displayOptions() . $this->getConfigureSmartyInstance()->fetch($this->_configureTpl);
280 * Returns the DashletGenericChart::_seedBean object. Designed to allow lazy loading the object
285 protected function getSeedBean()
287 if ( !($this->_seedBean instanceof SugarBean) )
288 $this->_seedBean = SugarModule::get($this->_seedName)->loadBean();
290 return $this->_seedBean;
294 * Returns the built query read to feed into SugarChart::getData()
296 * @return string SQL query
298 protected function constructQuery()
304 * Returns the array of group by parameters for SugarChart::$group_by
306 * @return string SQL query
308 protected function constructGroupBy()
314 * Displays the Dashlet, must call process() prior to calling this
316 * @return string HTML that displays Dashlet
318 public function display()
320 return parent::display() . $this->processAutoRefresh();
324 * Processes and displays the auto refresh code for the dashlet
326 * @param int $dashletOffset
327 * @return string HTML code
329 protected function processAutoRefresh($dashletOffset = 0)
331 global $sugar_config;
333 if ( empty($dashletOffset) ) {
335 $module = $_REQUEST['module'];
336 if(isset($_REQUEST[$module.'2_'.strtoupper($this->getSeedBean()->object_name).'_offset'])) {
337 $dashletOffset = $_REQUEST[$module.'2_'.strtoupper($this->getSeedBean()->object_name).'_offset'];
341 if ( !$this->isRefreshable ) {
344 if ( !empty($sugar_config['dashlet_auto_refresh_min']) && $sugar_config['dashlet_auto_refresh_min'] == -1 ) {
347 $autoRefreshSS = new Sugar_Smarty();
348 $autoRefreshSS->assign('dashletOffset', $dashletOffset);
349 $autoRefreshSS->assign('dashletId', $this->id);
350 $autoRefreshSS->assign('strippedDashletId', str_replace("-","",$this->id)); //javascript doesn't like "-" in function names
351 if ( empty($this->autoRefresh) ) {
352 $this->autoRefresh = 0;
354 elseif ( !empty($sugar_config['dashlet_auto_refresh_min']) && $sugar_config['dashlet_auto_refresh_min'] > $this->autoRefresh ) {
355 $this->autoRefresh = $sugar_config['dashlet_auto_refresh_min'];
357 $autoRefreshSS->assign('dashletRefreshInterval', $this->autoRefresh * 1000);
358 $autoRefreshSS->assign('url', "predefined_chart");
359 $tpl = 'include/Dashlets/DashletGenericAutoRefresh.tpl';
360 if ( $_REQUEST['action'] == "DynamicAction" ) {
361 $tpl = 'include/Dashlets/DashletGenericAutoRefreshDynamic.tpl';
364 return $autoRefreshSS->fetch($tpl);