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