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