]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/MySugar/MySugar.php
Release 6.4.0
[Github/sugarcrm.git] / include / MySugar / MySugar.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
39 /**
40  * Homepage dashlet manager
41  * @api
42  */
43 class MySugar{
44         var $type;
45
46         function MySugar($type){
47                 $this->type = $type;
48         }
49
50     function checkDashletDisplay () {
51
52                 if((!in_array($this->type, $GLOBALS['moduleList'])
53                                 && !in_array($this->type, $GLOBALS['modInvisList']))
54                                 && (!in_array('Activities', $GLOBALS['moduleList']))){
55                         $displayDashlet = false;
56                 }
57                 elseif (ACLController::moduleSupportsACL($this->type) ) {
58                     $bean = SugarModule::get($this->type)->loadBean();
59                     if ( !ACLController::checkAccess($this->type,'list',true,$bean->acltype)) {
60                         $displayDashlet = false;
61                     }
62                     $displayDashlet = true;
63                 }
64                 else{
65                         $displayDashlet = true;
66                 }
67
68                 return $displayDashlet;
69     }
70
71         function addDashlet(){
72                 if(!is_file(sugar_cached('dashlets/dashlets.php'))) {
73             require_once('include/Dashlets/DashletCacheBuilder.php');
74
75             $dc = new DashletCacheBuilder();
76             $dc->buildCache();
77                 }
78                 require_once sugar_cached('dashlets/dashlets.php');
79
80                 global $current_user;
81
82                 if(isset($_REQUEST['id'])){
83                         $pages = $current_user->getPreference('pages', $this->type);
84
85                     $dashlets = $current_user->getPreference('dashlets', $this->type);
86
87                     $guid = create_guid();
88                         $options = array();
89                     if (isset($_REQUEST['type']) && $_REQUEST['type'] == 'web') {
90                                 $dashlet_module = 'Home';
91                                 require_once('include/Dashlets/DashletRssFeedTitle.php');
92                                 $options['url'] = $_REQUEST['type_module'];
93                                 $webDashlet = new DashletRssFeedTitle($options['url']);
94                                 $options['title'] = $webDashlet->generateTitle();
95                                 unset($webDashlet);
96                     }
97                         elseif (!empty($_REQUEST['type_module'])) {
98                                 $dashlet_module = $_REQUEST['type_module'];
99                         }
100                         elseif (isset($dashletsFiles[$_REQUEST['id']]['module'])) {
101                                 $dashlet_module = $dashletsFiles[$_REQUEST['id']]['module'];
102                         }
103                         else {
104                                 $dashlet_module = 'Home';
105                         }
106
107                             $dashlets[$guid] = array('className' => $dashletsFiles[$_REQUEST['id']]['class'],
108                                                                                  'module' => $dashlet_module,
109                                                                                  'options' => $options,
110                                                      'fileLocation' => $dashletsFiles[$_REQUEST['id']]['file']);
111
112                     // add to beginning of the array
113                     array_unshift($pages[$_REQUEST['activeTab']]['columns'][0]['dashlets'], $guid);
114
115                     $current_user->setPreference('dashlets', $dashlets, 0, $this->type);
116
117                     echo $guid;
118                 }
119                 else {
120                     echo 'ofdaops';
121                 }
122         }
123
124         function displayDashlet(){
125                 global $current_user, $mod_strings;
126
127                 if(!empty($_REQUEST['id'])) {
128                     $id = $_REQUEST['id'];
129                     $dashlets = $current_user->getPreference('dashlets', $this->type);
130
131                     $sortOrder = '';
132                     $orderBy = '';
133                     foreach($_REQUEST as $k => $v){
134                         if($k == 'lvso'){
135                             $sortOrder = $v;
136                         }
137                         else if(preg_match('/Home2_.+_ORDER_BY/', $k)){
138                             $orderBy = $v;
139                         }
140                     }
141                     if(!empty($sortOrder) && !empty($orderBy)){
142                         $dashlets[$id]['sort_options'] = array('sortOrder' => $sortOrder, 'orderBy' => $orderBy);
143                         $current_user->setPreference('dashlets', $dashlets, 0, $this->type);
144                     }
145
146                     require_once($dashlets[$id]['fileLocation']);
147                     $dashlet = new $dashlets[$id]['className']($id, (isset($dashlets[$id]['options']) ? $dashlets[$id]['options'] : array()));
148                     if(!empty($_REQUEST['configure']) && $_REQUEST['configure']) { // save settings
149                         $dashletDefs[$id]['options'] = $dashlet->saveOptions($_REQUEST);
150                         $current_user->setPreference('dashlets', $dashletDefs, 0, $this->type);
151                     }
152                     if(!empty($_REQUEST['dynamic']) && $_REQUEST['dynamic'] == 'true' && $dashlet->hasScript) {
153                         $dashlet->isConfigurable = false;
154                         echo $dashlet->getTitle('') . $mod_strings['LBL_RELOAD_PAGE'];
155                     }
156                     else {
157                         $lvsParams = array();
158                         if(!empty($dashlets[$id]['sort_options'])){
159                             $lvsParams = $dashlets[$id]['sort_options'];
160                 }
161                         $dashlet->process($lvsParams);
162                         $contents =  $dashlet->display();
163                 // Many dashlets expect to be able to initialize in the display() function, so we have to create the header second
164                 echo $dashlet->getHeader();
165                 echo $contents;
166                 echo $dashlet->getFooter();
167                     }
168                 }
169                 else {
170                     header("Location: index.php?action=index&module=". $this->type);
171                 }
172         }
173
174         function getPredefinedChartScript(){
175                 global $current_user, $mod_strings;
176
177                 if(!empty($_REQUEST['id'])) {
178                     $id = $_REQUEST['id'];
179                     $dashlets = $current_user->getPreference('dashlets', $this->type);
180
181                     require_once($dashlets[$id]['fileLocation']);
182                     $dashlet = new $dashlets[$id]['className']($id, (isset($dashlets[$id]['options']) ? $dashlets[$id]['options'] : array()));
183                 $dashlet->process();
184                 echo $dashlet->displayScript();
185                 }
186                 else {
187                     header("Location: index.php?action=index&module=". $this->type);
188                 }
189         }
190
191
192
193         function deleteDashlet(){
194
195
196                 global $current_user;
197
198                 if(!empty($_REQUEST['id'])) {
199                     $pages = $current_user->getPreference('pages', $this->type);
200                     $dashlets = $current_user->getPreference('dashlets', $this->type);
201
202                                 $activePage = '0';
203
204                         foreach($pages[$activePage]['columns'] as $colNum => $column) {
205                         foreach($column['dashlets'] as $num => $dashletId) {
206                             if($dashletId == $_REQUEST['id']) {
207                                 unset($pages[$activePage]['columns'][$colNum]['dashlets'][$num]);
208                             }
209                         }
210                     }
211
212                     foreach($dashlets as $dashletId => $data) {
213                         if($dashletId == $_REQUEST['id']) {
214                             unset($dashlets[$dashletId]);
215                         }
216                     }
217
218                     $current_user->setPreference('dashlets', $dashlets, 0, $this->type);
219                     $current_user->setPreference('pages', $pages, 0, $this->type);
220
221                     echo '1';
222                 }
223                 else {
224                     echo 'oops';
225                 }
226         }
227
228         function dashletsDialog(){
229                 require_once('include/MySugar/DashletsDialog/DashletsDialog.php');
230
231                 global $current_language, $app_strings;
232
233                 $chartsList = array();
234                 $DashletsDialog = new DashletsDialog();
235
236                 $DashletsDialog->getDashlets();
237                 $allDashlets = $DashletsDialog->dashlets;
238
239                 $dashletsList = $allDashlets['Module Views'];
240                 $chartsList = $allDashlets['Charts'];
241                 $toolsList = $allDashlets['Tools'];
242                 $sugar_smarty = new Sugar_Smarty();
243
244                 $mod_strings = return_module_language($current_language, 'Home');
245
246                 $sugar_smarty->assign('LBL_CLOSE_DASHLETS', $mod_strings['LBL_CLOSE_DASHLETS']);
247                 $sugar_smarty->assign('LBL_ADD_DASHLETS', $mod_strings['LBL_ADD_DASHLETS']);
248                 $sugar_smarty->assign('APP', $app_strings);
249                 $sugar_smarty->assign('moduleName', $this->type);
250
251                 if ($this->type == 'Home'){
252                         $sugar_smarty->assign('modules', $dashletsList);
253                         $sugar_smarty->assign('tools', $toolsList);
254                 }
255
256                 $sugar_smarty->assign('charts', $chartsList);
257
258                 $html = $sugar_smarty->fetch('include/MySugar/tpls/addDashletsDialog.tpl');
259                 // Bug 34451 - Added hack to make the "Add Dashlet" dialog window not look weird in IE6.
260                 $script = <<<EOJS
261 if (YAHOO.env.ua.ie > 5 && YAHOO.env.ua.ie < 7) {
262     document.getElementById('dashletsList').style.width = '430px';
263     document.getElementById('dashletsList').style.overflow = 'hidden';
264 }
265 EOJS;
266
267                 $json = getJSONobj();
268                 echo 'response = ' . $json->encode(array('html' => $html, 'script' => $script));
269         }
270
271
272         function searchModuleToolsDashlets($searchStr, $category){
273                 require_once('include/MySugar/DashletsDialog/DashletsDialog.php');
274
275                 global $app_strings;
276
277                 $DashletsDialog = new DashletsDialog();
278
279                 switch($category){
280                         case 'module':
281                                 $DashletsDialog->getDashlets('module');
282                                 $dashletIndex = 'Module Views';
283                                 $searchCategoryString = $app_strings['LBL_SEARCH_MODULES'];
284                                 break;
285                         case 'tools':
286                                 $DashletsDialog->getDashlets('tools');
287                                 $dashletIndex = 'Tools';
288                                 $searchCategoryString = $app_strings['LBL_SEARCH_TOOLS'];
289                         default:
290                                 break;
291                 }
292                 $allDashlets = $DashletsDialog->dashlets;
293
294                 $searchResult = array();
295                 $searchResult[$dashletIndex] = array();
296
297                 foreach($allDashlets[$dashletIndex] as $dashlet){
298                         if (stripos($dashlet['title'], $searchStr) !== false){
299                                 array_push($searchResult[$dashletIndex], $dashlet);
300                         }
301                 }
302
303                 $sugar_smarty = new Sugar_Smarty();
304
305                 $sugar_smarty->assign('lblSearchResults', $app_strings['LBL_SEARCH_RESULTS']);
306                 $sugar_smarty->assign('lblSearchCategory', $searchCategoryString);
307
308                 $sugar_smarty->assign('moduleName', $this->type);
309                 $sugar_smarty->assign('searchString', $searchStr);
310
311                 $sugar_smarty->assign('dashlets', $searchResult[$dashletIndex]);
312
313                 return $sugar_smarty->fetch('include/MySugar/tpls/dashletsSearchResults.tpl');
314         }
315
316         function searchChartsDashlets($searchStr){
317                 require_once('include/MySugar/DashletsDialog/DashletsDialog.php');
318
319                 global $current_language, $app_strings;
320
321                 $chartsList = array();
322                 $DashletsDialog = new DashletsDialog();
323
324                 $DashletsDialog->getDashlets('charts');
325
326                 $allDashlets = $DashletsDialog->dashlets;
327
328                 foreach($allDashlets as $category=>$dashlets){
329                         $searchResult[$category] = array();
330                         foreach($dashlets as $dashlet){
331                                 if (stripos($dashlet['title'], $searchStr) !== false){
332                                         array_push($searchResult[$category],$dashlet);
333                                 }
334                         }
335                 }
336
337                 $sugar_smarty = new Sugar_Smarty();
338
339                 $sugar_smarty->assign('lblSearchResults', $app_strings['LBL_SEARCH_RESULTS']);
340                 $sugar_smarty->assign('searchString', $searchStr);
341                 $sugar_smarty->assign('charts', $searchResult['Charts']);
342
343                 return $sugar_smarty->fetch('include/MySugar/tpls/chartDashletsSearchResults.tpl');
344         }
345
346         function searchDashlets(){
347                 $searchStr = $_REQUEST['search'];
348                 $category = $_REQUEST['category'];
349
350                 if ($category == 'module' || $category == 'tools'){
351                         $html = $this->searchModuleToolsDashlets($searchStr, $category);
352                 }
353                 else if ($category == 'chart'){
354                         $html = $this->searchChartsDashlets($searchStr);
355                 }
356
357                 $json = getJSONobj();
358                 echo 'response = ' . $json->encode(array('html' => $html, 'script' => ''));
359         }
360
361         function configureDashlet(){
362                 global $current_user, $app_strings, $mod_strings;
363
364                 if(!empty($_REQUEST['id'])) {
365                     $id = $_REQUEST['id'];
366                     $dashletDefs = $current_user->getPreference('dashlets', $this->type); // load user's dashlets config
367                     $dashletLocation = $dashletDefs[$id]['fileLocation'];
368
369                     require_once($dashletDefs[$id]['fileLocation']);
370
371                     $dashlet = new $dashletDefs[$id]['className']($id, (isset($dashletDefs[$id]['options']) ? $dashletDefs[$id]['options'] : array()));
372                     if(!empty($_REQUEST['configure']) && $_REQUEST['configure']) { // save settings
373                         $dashletDefs[$id]['options'] = $dashlet->saveOptions($_REQUEST);
374                         $current_user->setPreference('dashlets', $dashletDefs, 0, $this->type);
375                     }
376                     else { // display options
377                         $json = getJSONobj();
378                         return 'result = ' . $json->encode((array('header' => $dashlet->title . ' : ' . $app_strings['LBL_OPTIONS'],
379                                                                  'body'  => $dashlet->displayOptions())));
380                     }
381                 }
382                 else {
383                     return '0';
384                 }
385         }
386
387         function saveLayout(){
388                 global $current_user;
389
390                 if(!empty($_REQUEST['layout'])) {
391                     $newColumns = array();
392
393                     $newLayout = explode('|', $_REQUEST['layout']);
394
395                         $pages = $current_user->getPreference('pages', $this->type);
396
397                         $newColumns = $pages[$_REQUEST['selectedPage']]['columns'];
398                     foreach($newLayout as $col => $ids) {
399                         $newColumns[$col]['dashlets'] = explode(',', $ids);
400                     }
401
402                         $pages[$_REQUEST['selectedPage']]['columns'] = $newColumns;
403                     $current_user->setPreference('pages', $pages, 0, $this->type);
404
405                     return '1';
406                 }
407                 else {
408                     return '0';
409                 }
410         }
411
412
413
414
415
416
417
418 }