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