]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/Dashlets/Dashlet.php
Release 6.5.16
[Github/sugarcrm.git] / include / Dashlets / Dashlet.php
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  * 
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
38 require_once('include/Sugar_Smarty.php');
39 require_once('include/utils/layout_utils.php');
40
41 /**
42  * Basic Dashlet
43  * @api
44  */
45 class Dashlet
46 {
47    /**
48      * Id of the Dashlet
49      * @var guid
50      */
51     var $id;
52     /**
53      * Title of the Dashlet
54      * @var string
55      */
56     var $title = 'Generic Dashlet';
57     /**
58      * true if the Dashlet has configuration options.
59      * @var bool
60      */
61     var $isConfigurable = false;
62     /**
63      * true if the Dashlet is refreshable (ie charts that provide their own refresh)
64      * @var bool
65      */
66     var $isRefreshable = true;
67     /**
68      * true if the Dashlet configuration options panel has the clear button
69      * @var bool
70      */
71     public $isConfigPanelClearShown = true;
72     /**
73      * true if the Dashlet contains javascript
74      * @var bool
75      */
76     var $hasScript = false;
77     /**
78      * Language strings, must be loaded at the Dashlet level w/ loadLanguage
79      * @var array
80      */
81     var $dashletStrings;
82     /**
83      * Time period in minutes to refresh the dashlet (0 for never)
84      * Do not refresh if $isRefreshable is set to false
85      *
86      * To support auto refresh all refreshable dashlets that override process() must call processAutoRefresh()
87      * @var int
88      */
89     var $autoRefresh = "0";
90
91     /**
92      * Constructor
93      *
94      * @param $id
95      */
96     public function Dashlet($id)
97     {
98         $this->id = $id;
99     }
100
101     /**
102      * Returns the HTML for the configure icon
103      *
104      * @return string HTML
105      */
106     public function setConfigureIcon()
107     {
108         if($this->isConfigurable) {
109             $additionalTitle = '<td nowrap width="1%" style="padding-right: 0px;"><div class="dashletToolSet"><a href="javascript:void(0)" onclick="SUGAR.mySugar.configureDashlet(\''
110                                . $this->id . '\'); return false;">'
111                                . SugarThemeRegistry::current()->getImage('dashlet-header-edit','title="' . translate('LBL_DASHLET_EDIT', 'Home') . '" border="0"  align="absmiddle"', null,null,'.gif',translate('LBL_DASHLET_EDIT', 'Home')).'</a>'
112                                . '';
113         }
114         else {
115             $additionalTitle = '<td nowrap width="1%" style="padding-right: 0px;"><div class="dashletToolSet">';
116         }
117
118         return $additionalTitle;
119     }
120
121     /**
122      * Returns the HTML for the refresh icon
123      *
124      * @return string HTML
125      */
126     public function setRefreshIcon()
127     {
128         $additionalTitle = '';
129         if($this->isRefreshable) {
130             $additionalTitle .= '<a href="javascript:void(0)" onclick="SUGAR.mySugar.retrieveDashlet(\''
131                                 . $this->id . '\'); return false;">'
132                                 . SugarThemeRegistry::current()->getImage('dashlet-header-refresh','border="0" align="absmiddle" title="' . translate('LBL_DASHLET_REFRESH', 'Home') . '"',null,null,'.gif',translate('LBL_DASHLET_REFRESH', 'Home'))
133                                 . '</a>';
134         }
135
136         return $additionalTitle;
137     }
138
139     /**
140      * Returns the HTML for the delete icon
141      *
142      * @return string HTML
143      */
144     public function setDeleteIcon()
145     {
146         global $sugar_config;
147
148         if (!empty($sugar_config['lock_homepage']) && $sugar_config['lock_homepage'] == true) {
149                         return '</div></td></tr></table>';
150                 }
151         $additionalTitle = '<a href="javascript:void(0)" onclick="SUGAR.mySugar.deleteDashlet(\''
152                             . $this->id . '\'); return false;">'
153                             . SugarThemeRegistry::current()->getImage('dashlet-header-close','border="0" align="absmiddle" title="' . translate('LBL_DASHLET_DELETE', 'Home') . '"',null,null,'.gif',translate('LBL_DASHLET_DELETE', 'Home'))
154                             . '</a></div></td></tr></table>';
155                 return $additionalTitle;
156     }
157
158     /**
159      * @deprecated No longer needed, replaced with Dashlet::getHeader() and Dashlet::getFooter()
160      *
161      * @param string $text
162      * @return string HTML
163      */
164     public function getTitle($text = '')
165     {
166         return '';
167     }
168
169     /**
170      * Called when Dashlet is displayed
171      *
172      * @param string $text text after the title
173      * @return string Header html
174      */
175     public function getHeader($text = '')
176     {
177         global $sugar_config;
178
179         $title = '<table width="100%" cellspacing="0" cellpadding="0" border="0"><tr><td width="99%">' . $text . '</td>';
180         $title .= $this->setConfigureIcon();
181         $title .= $this->setRefreshIcon();
182         $title .= $this->setDeleteIcon();
183
184         $str = '<div ';
185         if(empty($sugar_config['lock_homepage']) || $sugar_config['lock_homepage'] == false) $str .= 'onmouseover="this.style.cursor = \'move\';" ';
186         $str .= 'id="dashlet_header_' . $this->id . '" class="hd"><div class="tl"></div><div class="hd-center">' . get_form_header($this->title, $title, false) . '</div><div class="tr"></div></div><div class="bd"><div class="ml"></div><div class="bd-center">';
187
188         return $str;
189     }
190
191     /**
192      * Called when Dashlet is displayed
193      *
194      * @return string footer HTML
195      */
196     public function getFooter()
197     {
198         $footer = '</div><div class="mr"></div></div><div class="ft"><div class="bl"></div><div class="ft-center"></div><div class="br"></div></div>';
199
200         return $footer;
201     }
202
203     /**
204      * Called when Dashlet is displayed, override this
205      *
206      * @param string $text text after the title
207      * @return string title HTML
208      */
209     public function display($text = '')
210     {
211         return '';
212     }
213
214     /**
215      * Called when Dashlets configuration options are called
216      */
217     public function displayOptions()
218     {
219     }
220
221     /**
222      * Override if you need to do pre-processing before display is called
223      */
224     public function process()
225     {
226     }
227
228     /**
229      * Processes and displays the auto refresh code for the dashlet
230      *
231      * @param int $dashletOffset
232      * @return string HTML code
233      */
234     protected function processAutoRefresh($dashletOffset = 0)
235     {
236         global $sugar_config;
237
238         if ( empty($dashletOffset) ) {
239             $dashletOffset = 0;
240             $module = $_REQUEST['module'];
241             if(isset($_REQUEST[$module.'2_'.strtoupper($this->seedBean->object_name).'_offset'])) {
242                 $dashletOffset = $_REQUEST[$module.'2_'.strtoupper($this->seedBean->object_name).'_offset'];
243             }
244         }
245
246         if ( !$this->isRefreshable ) {
247             return '';
248         }
249         if ( !empty($sugar_config['dashlet_auto_refresh_min']) && $sugar_config['dashlet_auto_refresh_min'] == -1 ) {
250             return '';
251         }
252         $autoRefreshSS = new Sugar_Smarty();
253         $autoRefreshSS->assign('dashletOffset', $dashletOffset);
254         $autoRefreshSS->assign('dashletId', $this->id);
255         $autoRefreshSS->assign('strippedDashletId', str_replace("-","",$this->id)); //javascript doesn't like "-" in function names
256         $autoRefreshSS->assign('dashletRefreshInterval', $this->getAutoRefresh());
257         $tpl = 'include/Dashlets/DashletGenericAutoRefresh.tpl';
258         if ( $_REQUEST['action'] == "DynamicAction" ) {
259             $tpl = 'include/Dashlets/DashletGenericAutoRefreshDynamic.tpl';
260         }
261
262         return $autoRefreshSS->fetch($tpl);
263     }
264
265     protected function getAutoRefresh()
266     {
267         global $sugar_config;
268
269         if (empty($this->autoRefresh) || $this->autoRefresh == -1)
270         {
271             $autoRefresh = 0;
272         }
273         elseif (!empty($sugar_config['dashlet_auto_refresh_min'])
274                 && $this->autoRefresh > 0
275                 && $sugar_config['dashlet_auto_refresh_min'] > $this->autoRefresh)
276         {
277             $autoRefresh = $sugar_config['dashlet_auto_refresh_min'];
278         }
279         else
280         {
281             $autoRefresh = $this->autoRefresh;
282         }
283
284         return $autoRefresh * 1000;
285     }
286
287     /**
288      * Override this if your dashlet is configurable (this is called when the the configureDashlet form is shown)
289      * Filters the array for only the parameters it needs to save
290      *
291      * @param array $req the array to pull options from
292      * @return array options array
293      */
294     public function saveOptions($req)
295     {
296     }
297
298     /**
299      * Sets the language strings
300      *
301      * @param string $dashletClassname classname of the dashlet
302      * @param string $dashletDirectory directory path of the dashlet
303      */
304     public function loadLanguage($dashletClassname, $dashletDirectory = 'modules/Home/Dashlets/')
305     {
306         global $current_language, $dashletStrings;
307
308         if(!isset($dashletStrings[$dashletClassname])) {
309             // load current language strings for current language, else default to english
310             if(is_file($dashletDirectory . $dashletClassname . '/' . $dashletClassname . '.' . $current_language . '.lang.php')
311                     || is_file('custom/' . $dashletDirectory . $dashletClassname . '/' . $dashletClassname . '.' . $current_language . '.lang.php') ) {
312                 if(is_file($dashletDirectory . $dashletClassname . '/' . $dashletClassname . '.' . $current_language . '.lang.php')) {
313                     require($dashletDirectory . $dashletClassname . '/' . $dashletClassname . '.' . $current_language . '.lang.php');
314                 }
315                 if(is_file('custom/' . $dashletDirectory . $dashletClassname . '/' . $dashletClassname . '.' . $current_language . '.lang.php')) {
316                     require('custom/' . $dashletDirectory . $dashletClassname . '/' . $dashletClassname . '.' . $current_language . '.lang.php');
317                 }
318             }
319             else {
320                 if(is_file($dashletDirectory . $dashletClassname . '/' . $dashletClassname . '.en_us.lang.php')) {
321                     require($dashletDirectory . $dashletClassname . '/' . $dashletClassname . '.en_us.lang.php');
322                 }
323                 if(is_file('custom/' . $dashletDirectory . $dashletClassname . '/' . $dashletClassname . '.en_us.lang.php')) {
324                     require('custom/' . $dashletDirectory . $dashletClassname . '/' . $dashletClassname . '.en_us.lang.php');
325                 }
326             }
327         }
328
329         $this->dashletStrings = $dashletStrings[$dashletClassname];
330     }
331
332     /**
333      * Generic way to store an options array into UserPreferences
334      *
335      * @param array $optionsArray the array to save
336      */
337     public function storeOptions($optionsArray)
338     {
339         global $current_user;
340
341         $dashletDefs = $current_user->getPreference('dashlets', 'Home'); // load user's dashlets config
342         $dashletDefs[$this->id]['options'] = $optionsArray;
343         $current_user->setPreference('dashlets', $dashletDefs, 0, 'Home');
344     }
345
346     /**
347      * Generic way to retrieve options array from UserPreferences
348      *
349      * @return array options array stored in UserPreferences
350      */
351     public function loadOptions()
352     {
353         global $current_user;
354
355         $dashletDefs = $current_user->getPreference('dashlets', 'Home'); // load user's dashlets config
356         if(isset($dashletDefs[$this->id]['options']))
357             return $dashletDefs[$this->id]['options'];
358         else
359             return array();
360     }
361
362     /**
363      * Override this in the subclass. It is used to determine whether the dashlet can be displayed.
364      *
365      * @return bool indicating whether or not the current user has access to display this Dashlet.
366      */
367     public function hasAccess()
368     {
369         return true;
370     }
371
372     /**
373      * Returns the available auto refresh settings you can set a dashlet to
374      *
375      * @return array options available
376      */
377     protected function getAutoRefreshOptions()
378     {
379         $options = $GLOBALS['app_list_strings']['dashlet_auto_refresh_options'];
380
381         if ( isset($GLOBALS['sugar_config']['dashlet_auto_refresh_min']) ) {
382             foreach ( $options as $time => $desc ) {
383                 if ( $time != -1 && $time < $GLOBALS['sugar_config']['dashlet_auto_refresh_min'] ) {
384                     unset($options[$time]);
385                 }
386             }
387         }
388
389         return $options;
390     }
391
392     /**
393      * Returns true if the dashlet is auto refreshable
394      *
395      * @return bool
396      */
397     protected function isAutoRefreshable()
398     {
399         return $this->isRefreshable &&
400             ( isset($GLOBALS['sugar_config']['dashlet_auto_refresh_min']) ?
401                 $GLOBALS['sugar_config']['dashlet_auto_refresh_min'] != -1 : true );
402     }
403 }