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