Passed
Push — scrutinizer-code-quality ( 09f5a1...c4c5fb )
by Adam
56:05 queued 14:08
created

Dashlet::Dashlet()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 2
Bugs 2 Features 1
Metric Value
cc 2
eloc 7
c 2
b 2
f 1
nc 2
nop 1
dl 0
loc 10
ccs 0
cts 7
cp 0
crap 6
rs 9.4285
1
<?php
2 1
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 [email protected].
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 1
require_once('include/Sugar_Smarty.php');
39 1
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 1
    public function __construct($id)
97
    {
98 1
        $this->id = $id;
99 1
    }
100
101
    /**
102
     * @deprecated deprecated since version 7.6, PHP4 Style Constructors are deprecated and will be remove in 7.8, please update your code, use __construct instead
103
     */
104
    public function Dashlet($id){
105
        $deprecatedMessage = 'PHP4 Style Constructors are deprecated and will be remove in 7.8, please update your code';
106
        if(isset($GLOBALS['log'])) {
107
            $GLOBALS['log']->deprecated($deprecatedMessage);
108
        }
109
        else {
110
            trigger_error($deprecatedMessage, E_USER_DEPRECATED);
111
        }
112
        self::__construct($id);
113
    }
114
115
116
    /**
117
     * Returns the HTML for the configure icon
118
     *
119
     * @return string HTML
120
     */
121 1
    public function setConfigureIcon()
122
    {
123 1
        if($this->isConfigurable) {
124
            $additionalTitle = '<td nowrap width="1%" style="padding-right: 0px;"><div class="dashletToolSet"><a href="javascript:void(0)" onclick="SUGAR.mySugar.configureDashlet(\''
125 1
                . $this->id . '\'); return false;">'
126 1
                . 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>'
127 1
                . '';
128
        }
129
        else {
130
            $additionalTitle = '<td nowrap width="1%" style="padding-right: 0px;"><div class="dashletToolSet">';
131
        }
132
133 1
        return $additionalTitle;
134
    }
135
136
    /**
137
     * Returns the HTML for the refresh icon
138
     *
139
     * @return string HTML
140
     */
141 1
    public function setRefreshIcon()
142
    {
143 1
        $additionalTitle = '';
144 1
        if($this->isRefreshable) {
145
            $additionalTitle .= '<a href="javascript:void(0)" onclick="SUGAR.mySugar.retrieveDashlet(\''
146 1
                . $this->id . '\'); return false;">'
147 1
                . SugarThemeRegistry::current()->getImage('dashlet-header-refresh','border="0" align="absmiddle" title="' . translate('LBL_DASHLET_REFRESH', 'Home') . '"',null,null,'.gif',translate('LBL_DASHLET_REFRESH', 'Home'))
148 1
                . '</a>';
149
        }
150
151 1
        return $additionalTitle;
152
    }
153
154
    /**
155
     * Returns the HTML for the delete icon
156
     *
157
     * @return string HTML
158
     */
159 1
    public function setDeleteIcon()
160
    {
161 1
        global $sugar_config;
162
163 1
        if (!empty($sugar_config['lock_homepage']) && $sugar_config['lock_homepage'] == true) {
164
            return '</div></td></tr></table>';
165
        }
166
        $additionalTitle = '<a href="javascript:void(0)" onclick="SUGAR.mySugar.deleteDashlet(\''
167 1
            . $this->id . '\'); return false;">'
168 1
            . SugarThemeRegistry::current()->getImage('dashlet-header-close','border="0" align="absmiddle" title="' . translate('LBL_DASHLET_DELETE', 'Home') . '"',null,null,'.gif',translate('LBL_DASHLET_DELETE', 'Home'))
169 1
            . '</a></div></td></tr></table>';
170 1
        return $additionalTitle;
171
    }
172
173
    /**
174
     * @deprecated No longer needed, replaced with Dashlet::getHeader() and Dashlet::getFooter()
175
     *
176
     * @param string $text
177
     * @return string HTML
178
     */
179
    public function getTitle($text = '')
180
    {
181
        return '';
182
    }
183
184
    /**
185
     * Called when Dashlet is displayed
186
     *
187
     * @param string $text text after the title
188
     * @return string Header html
189
     */
190 1
    public function getHeader($text = '')
191
    {
192 1
        global $sugar_config;
193
194 1
        $title = '<table width="100%" cellspacing="0" cellpadding="0" border="0"><tr><td width="99%">' . $text . '</td>';
195 1
        $title .= $this->setConfigureIcon();
196 1
        $title .= $this->setRefreshIcon();
197 1
        $title .= $this->setDeleteIcon();
198
199 1
        $str = '<div ';
200 1
        if(empty($sugar_config['lock_homepage']) || $sugar_config['lock_homepage'] == false) $str .= 'onmouseover="this.style.cursor = \'move\';" ';
201 1
        $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">';
202
203 1
        return $str;
204
    }
205
206
    /**
207
     * Called when Dashlet is displayed
208
     *
209
     * @return string footer HTML
210
     */
211 1
    public function getFooter()
212
    {
213 1
        $footer = '</div><div class="mr"></div></div><div class="ft"><div class="bl"></div><div class="ft-center"></div><div class="br"></div></div>';
214
215 1
        return $footer;
216
    }
217
218
    /**
219
     * Called when Dashlet is displayed, override this
220
     *
221
     * @return string title HTML
222
     */
223 1
    public function display()
224
    {
225 1
        return '';
226
    }
227
228
    /**
229
     * Called when Dashlets configuration options are called
230
     */
231
    public function displayOptions()
232
    {
233
    }
234
235
    /**
236
     * Override if you need to do pre-processing before display is called
237
     */
238
    public function process()
239
    {
240
    }
241
242
    /**
243
     * Processes and displays the auto refresh code for the dashlet
244
     *
245
     * @param int $dashletOffset
246
     * @return string HTML code
247
     */
248 1
    protected function processAutoRefresh($dashletOffset = 0)
249
    {
250 1
        global $sugar_config;
251
252 1
        if ( empty($dashletOffset) ) {
253 1
            $dashletOffset = 0;
254 1
            $module = $_REQUEST['module'];
255 1
            if(isset($_REQUEST[$module.'2_'.strtoupper($this->seedBean->object_name).'_offset'])) {
256
                $dashletOffset = $_REQUEST[$module.'2_'.strtoupper($this->seedBean->object_name).'_offset'];
257
            }
258
        }
259
260 1
        if ( !$this->isRefreshable ) {
261
            return '';
262
        }
263 1
        if ( !empty($sugar_config['dashlet_auto_refresh_min']) && $sugar_config['dashlet_auto_refresh_min'] == -1 ) {
264
            return '';
265
        }
266 1
        $autoRefreshSS = new Sugar_Smarty();
267 1
        $autoRefreshSS->assign('dashletOffset', $dashletOffset);
268 1
        $autoRefreshSS->assign('dashletId', $this->id);
269 1
        $autoRefreshSS->assign('strippedDashletId', str_replace("-","",$this->id)); //javascript doesn't like "-" in function names
270 1
        $autoRefreshSS->assign('dashletRefreshInterval', $this->getAutoRefresh());
271 1
        $tpl = 'include/Dashlets/DashletGenericAutoRefresh.tpl';
272 1
        if ( $_REQUEST['action'] == "DynamicAction" ) {
273
            $tpl = 'include/Dashlets/DashletGenericAutoRefreshDynamic.tpl';
274
        }
275
276 1
        return $autoRefreshSS->fetch($tpl);
277
    }
278
279 1
    protected function getAutoRefresh()
280
    {
281 1
        global $sugar_config;
282
283 1
        if (empty($this->autoRefresh) || $this->autoRefresh == -1)
284
        {
285 1
            $autoRefresh = 0;
286
        }
287
        elseif (!empty($sugar_config['dashlet_auto_refresh_min'])
288
            && $this->autoRefresh > 0
289
            && $sugar_config['dashlet_auto_refresh_min'] > $this->autoRefresh)
290
        {
291
            $autoRefresh = $sugar_config['dashlet_auto_refresh_min'];
292
        }
293
        else
294
        {
295
            $autoRefresh = $this->autoRefresh;
296
        }
297
298 1
        return $autoRefresh * 1000;
299
    }
300
301
    /**
302
     * Override this if your dashlet is configurable (this is called when the the configureDashlet form is shown)
303
     * Filters the array for only the parameters it needs to save
304
     *
305
     * @param array $req the array to pull options from
306
     * @return array options array
307
     */
308
    public function saveOptions($req)
309
    {
310
    }
311
312
    /**
313
     * Sets the language strings
314
     *
315
     * @param string $dashletClassname classname of the dashlet
316
     * @param string $dashletDirectory directory path of the dashlet
317
     */
318
    public function loadLanguage($dashletClassname, $dashletDirectory = 'modules/Home/Dashlets/')
319
    {
320
        global $current_language, $dashletStrings;
321
322
        if(!isset($dashletStrings[$dashletClassname])) {
323
            // load current language strings for current language, else default to english
324
            if(is_file($dashletDirectory . $dashletClassname . '/' . $dashletClassname . '.' . $current_language . '.lang.php')
325
                || is_file('custom/' . $dashletDirectory . $dashletClassname . '/' . $dashletClassname . '.' . $current_language . '.lang.php') ) {
326
                if(is_file($dashletDirectory . $dashletClassname . '/' . $dashletClassname . '.' . $current_language . '.lang.php')) {
327
                    require($dashletDirectory . $dashletClassname . '/' . $dashletClassname . '.' . $current_language . '.lang.php');
328
                }
329
                if(is_file('custom/' . $dashletDirectory . $dashletClassname . '/' . $dashletClassname . '.' . $current_language . '.lang.php')) {
330
                    require('custom/' . $dashletDirectory . $dashletClassname . '/' . $dashletClassname . '.' . $current_language . '.lang.php');
331
                }
332
            }
333
            else {
334
                if(is_file($dashletDirectory . $dashletClassname . '/' . $dashletClassname . '.en_us.lang.php')) {
335
                    require($dashletDirectory . $dashletClassname . '/' . $dashletClassname . '.en_us.lang.php');
336
                }
337
                if(is_file('custom/' . $dashletDirectory . $dashletClassname . '/' . $dashletClassname . '.en_us.lang.php')) {
338
                    require('custom/' . $dashletDirectory . $dashletClassname . '/' . $dashletClassname . '.en_us.lang.php');
339
                }
340
            }
341
        }
342
343
        $this->dashletStrings = $dashletStrings[$dashletClassname];
344
    }
345
346
    /**
347
     * Generic way to store an options array into UserPreferences
348
     *
349
     * @param array $optionsArray the array to save
350
     */
351
    public function storeOptions($optionsArray)
352
    {
353
        global $current_user;
354
355
        $dashletDefs = $current_user->getPreference('dashlets', 'Home'); // load user's dashlets config
356
        $dashletDefs[$this->id]['options'] = $optionsArray;
357
        $current_user->setPreference('dashlets', $dashletDefs, 0, 'Home');
358
    }
359
360
    /**
361
     * Generic way to retrieve options array from UserPreferences
362
     *
363
     * @return array options array stored in UserPreferences
364
     */
365
    public function loadOptions()
366
    {
367
        global $current_user;
368
369
        $dashletDefs = $current_user->getPreference('dashlets', 'Home'); // load user's dashlets config
370
        if(isset($dashletDefs[$this->id]['options']))
371
            return $dashletDefs[$this->id]['options'];
372
        else
373
            return array();
374
    }
375
376
    /**
377
     * Override this in the subclass. It is used to determine whether the dashlet can be displayed.
378
     *
379
     * @return bool indicating whether or not the current user has access to display this Dashlet.
380
     */
381
    public function hasAccess()
382
    {
383
        return true;
384
    }
385
386
    /**
387
     * Returns the available auto refresh settings you can set a dashlet to
388
     *
389
     * @return array options available
390
     */
391
    protected function getAutoRefreshOptions()
392
    {
393
        $options = $GLOBALS['app_list_strings']['dashlet_auto_refresh_options'];
394
395
        if ( isset($GLOBALS['sugar_config']['dashlet_auto_refresh_min']) ) {
396
            foreach ( $options as $time => $desc ) {
397
                if ( $time != -1 && $time < $GLOBALS['sugar_config']['dashlet_auto_refresh_min'] ) {
398
                    unset($options[$time]);
399
                }
400
            }
401
        }
402
403
        return $options;
404
    }
405
406
    /**
407
     * Returns true if the dashlet is auto refreshable
408
     *
409
     * @return bool
410
     */
411
    protected function isAutoRefreshable()
412
    {
413
        return $this->isRefreshable &&
414
        ( isset($GLOBALS['sugar_config']['dashlet_auto_refresh_min']) ?
415
            $GLOBALS['sugar_config']['dashlet_auto_refresh_min'] != -1 : true );
416
    }
417
}
418