]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - service/v3_1/SugarWebServiceUtilv3_1.php
Release 6.5.6
[Github/sugarcrm.git] / service / v3_1 / SugarWebServiceUtilv3_1.php
1 <?php
2 /*********************************************************************************
3  * SugarCRM Community Edition is a customer relationship management program developed by
4  * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
5  * 
6  * This program is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License version 3 as published by the
8  * Free Software Foundation with the addition of the following permission added
9  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  * 
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
16  * details.
17  * 
18  * You should have received a copy of the GNU Affero General Public License along with
19  * this program; if not, see http://www.gnu.org/licenses or write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  * 
23  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
24  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
25  * 
26  * The interactive user interfaces in modified source and object code versions
27  * of this program must display Appropriate Legal Notices, as required under
28  * Section 5 of the GNU Affero General Public License version 3.
29  * 
30  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31  * these Appropriate Legal Notices must retain the display of the "Powered by
32  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
33  * technical reasons, the Appropriate Legal Notices must display the words
34  * "Powered by SugarCRM".
35  ********************************************************************************/
36
37 require_once('service/v3/SugarWebServiceUtilv3.php');
38 class SugarWebServiceUtilv3_1 extends SugarWebServiceUtilv3
39 {
40
41     function get_return_module_fields($value, $module,$fields, $translate=true)
42     {
43                 $GLOBALS['log']->info('Begin: SoapHelperWebServices->get_return_module_fields');
44                 global $module_name;
45                 $module_name = $module;
46                 $result = $this->get_field_list($value,$fields,  $translate);
47                 $GLOBALS['log']->info('End: SoapHelperWebServices->get_return_module_fields');
48
49                 $tableName = $value->getTableName();
50
51                 return Array('module_name'=>$module, 'table_name' => $tableName,
52                                         'module_fields'=> $result['module_fields'],
53                                         'link_fields'=> $result['link_fields'],
54                                         );
55         } // fn
56
57
58     /**
59          * Track a view for a particular bean.
60          *
61          * @param SugarBean $seed
62          * @param string $current_view
63          */
64     function trackView($seed, $current_view)
65     {
66         $trackerManager = TrackerManager::getInstance();
67                 if($monitor = $trackerManager->getMonitor('tracker'))
68                 {
69                 $monitor->setValue('date_modified', TimeDate::getInstance()->nowDb());
70                 $monitor->setValue('user_id', $GLOBALS['current_user']->id);
71                 $monitor->setValue('module_name', $seed->module_dir);
72                 $monitor->setValue('action', $current_view);
73                 $monitor->setValue('item_id', $seed->id);
74                 $monitor->setValue('item_summary', $seed->get_summary_text());
75                 $monitor->setValue('visible',true);
76                 $trackerManager->saveMonitor($monitor, TRUE, TRUE);
77                 }
78     }
79
80     /**
81      * Convert modules list to Web services result
82      *
83      * @param array $list List of module candidates (only keys are used)
84      * @param array $availModules List of module availability from Session
85      */
86     public function getModulesFromList($list, $availModules)
87     {
88         global $app_list_strings;
89         $enabled_modules = array();
90         $availModulesKey = array_flip($availModules);
91         foreach ($list as $key=>$value)
92         {
93             if( isset($availModulesKey[$key]) )
94             {
95                 $label = !empty( $app_list_strings['moduleList'][$key] ) ? $app_list_strings['moduleList'][$key] : '';
96                     $acl = self::checkModuleRoleAccess($key);
97                     $enabled_modules[] = array('module_key' => $key,'module_label' => $label, 'acls' => $acl);
98             }
99         }
100         return $enabled_modules;
101     }
102
103
104     /**
105      * Examine the application to determine which modules have been enabled..
106      *
107      * @param array $availModules An array of all the modules the user already has access to.
108      * @return array Modules enabled within the application.
109      */
110     function get_visible_modules($availModules)
111     {
112         require_once("modules/MySettings/TabController.php");
113         $controller = new TabController();
114         $tabs = $controller->get_tabs_system();
115         return $this->getModulesFromList($tabs[0], $availModules);
116     }
117
118     /**
119      * Generate unifed search fields for a particular module even if the module does not participate in the unified search.
120      *
121      * @param string $moduleName
122      * @return array An array of fields to be searched against.
123      */
124     function generateUnifiedSearchFields($moduleName)
125     {
126         global $beanList, $beanFiles, $dictionary;
127
128         if(!isset($beanList[$moduleName]))
129             return array();
130
131         $beanName = $beanList[$moduleName];
132
133         if (!isset($beanFiles[$beanName]))
134             return array();
135
136         $beanName = BeanFactory::getObjectName($moduleName);
137
138         $manager = new VardefManager ( );
139         $manager->loadVardef( $moduleName , $beanName ) ;
140
141         // obtain the field definitions used by generateSearchWhere (duplicate code in view.list.php)
142         if(file_exists('custom/modules/'.$moduleName.'/metadata/metafiles.php')){
143             require('custom/modules/'.$moduleName.'/metadata/metafiles.php');
144         }elseif(file_exists('modules/'.$moduleName.'/metadata/metafiles.php')){
145             require('modules/'.$moduleName.'/metadata/metafiles.php');
146         }
147
148         if(!empty($metafiles[$moduleName]['searchfields']))
149             require $metafiles[$moduleName]['searchfields'] ;
150         elseif(file_exists("modules/{$moduleName}/metadata/SearchFields.php"))
151             require "modules/{$moduleName}/metadata/SearchFields.php" ;
152
153         $fields = array();
154         foreach ( $dictionary [ $beanName ][ 'fields' ] as $field => $def )
155         {
156             if (strpos($field,'email') !== false)
157                 $field = 'email' ;
158
159             //bug: 38139 - allow phone to be searched through Global Search
160             if (strpos($field,'phone') !== false)
161                 $field = 'phone' ;
162
163             if ( isset($def['unified_search']) && $def['unified_search'] && isset ( $searchFields [ $moduleName ] [ $field ]  ))
164             {
165                     $fields [ $field ] = $searchFields [ $moduleName ] [ $field ] ;
166             }
167         }
168
169         //If no fields with the unified flag have been set then lets add a default field.
170         if( empty($fields) )
171         {
172             if( isset($dictionary[$beanName]['fields']['name']) && isset($searchFields[$moduleName]['name'])  )
173                 $fields['name'] = $searchFields[$moduleName]['name'];
174             else
175             {
176                 if( isset($dictionary[$beanName]['fields']['first_name']) && isset($searchFields[$moduleName]['first_name']) )
177                     $fields['first_name'] = $searchFields[$moduleName]['first_name'];
178                 if( isset($dictionary[$beanName]['fields']['last_name']) && isset($searchFields[$moduleName]['last_name'])  )
179                     $fields['last_name'] = $searchFields[$moduleName]['last_name'];
180             }
181         }
182
183                 return $fields;
184     }
185
186     /**
187      * Check a module for acces to a set of available actions.
188      *
189      * @param string $module
190      * @return array results containing access and boolean indicating access
191      */
192     function checkModuleRoleAccess($module)
193     {
194         $results = array();
195         $actions = array('edit','delete','list','view','import','export');
196         foreach ($actions as $action)
197         {
198             $access = ACLController::checkAccess($module, $action, true);
199             $results[] = array('action' => $action, 'access' => $access);
200         }
201
202         return $results;
203     }
204
205     function get_field_list($value,$fields,  $translate=true) {
206
207             $GLOBALS['log']->info('Begin: SoapHelperWebServices->get_field_list');
208                 $module_fields = array();
209                 $link_fields = array();
210                 if(!empty($value->field_defs)){
211
212                         foreach($value->field_defs as $var){
213                                 if(!empty($fields) && !in_array( $var['name'], $fields))continue;
214                                 if(isset($var['source']) && ($var['source'] != 'db' && $var['source'] != 'non-db' &&$var['source'] != 'custom_fields') && $var['name'] != 'email1' && $var['name'] != 'email2' && (!isset($var['type'])|| $var['type'] != 'relate'))continue;
215                                 if ((isset($var['source']) && $var['source'] == 'non_db') && (isset($var['type']) && $var['type'] != 'link')) {
216                                         continue;
217                                 }
218                                 $required = 0;
219                                 $options_dom = array();
220                                 $options_ret = array();
221                                 // Apparently the only purpose of this check is to make sure we only return fields
222                                 //   when we've read a record.  Otherwise this function is identical to get_module_field_list
223                                 if( isset($var['required']) && ($var['required'] || $var['required'] == 'true' ) ){
224                                         $required = 1;
225                                 }
226
227                                 if($var['type'] == 'bool')
228                                     $var['options'] = 'checkbox_dom';
229
230                                 if(isset($var['options'])){
231                                         $options_dom = translate($var['options'], $value->module_dir);
232                                         if(!is_array($options_dom)) $options_dom = array();
233                                         foreach($options_dom as $key=>$oneOption)
234                                                 $options_ret[$key] = $this->get_name_value($key,$oneOption);
235                                 }
236
237                     if(!empty($var['dbType']) && $var['type'] == 'bool') {
238                         $options_ret['type'] = $this->get_name_value('type', $var['dbType']);
239                     }
240
241                     $entry = array();
242                     $entry['name'] = $var['name'];
243                     $entry['type'] = $var['type'];
244                     $entry['group'] = isset($var['group']) ? $var['group'] : '';
245                     $entry['id_name'] = isset($var['id_name']) ? $var['id_name'] : '';
246
247                     if ($var['type'] == 'link') {
248                             $entry['relationship'] = (isset($var['relationship']) ? $var['relationship'] : '');
249                             $entry['module'] = (isset($var['module']) ? $var['module'] : '');
250                             $entry['bean_name'] = (isset($var['bean_name']) ? $var['bean_name'] : '');
251                                         $link_fields[$var['name']] = $entry;
252                     } else {
253                             if($translate) {
254                                 $entry['label'] = isset($var['vname']) ? translate($var['vname'], $value->module_dir) : $var['name'];
255                             } else {
256                                 $entry['label'] = isset($var['vname']) ? $var['vname'] : $var['name'];
257                             }
258                             $entry['required'] = $required;
259                             $entry['options'] = $options_ret;
260                             $entry['related_module'] = (isset($var['id_name']) && isset($var['module'])) ? $var['module'] : '';
261                             $entry['calculated'] =  (isset($var['calculated']) && $var['calculated']) ? true : false;
262                                         if(isset($var['default'])) {
263                                            $entry['default_value'] = $var['default'];
264                                         }
265                                         if( $var['type'] == 'parent' && isset($var['type_name']) )
266                                            $entry['type_name'] = $var['type_name'];
267
268                                         $module_fields[$var['name']] = $entry;
269                     } // else
270                         } //foreach
271                 } //if
272
273                 if($value->module_dir == 'Meetings' || $value->module_dir == 'Calls')
274                 {
275                     if( isset($module_fields['duration_minutes']) && isset($GLOBALS['app_list_strings']['duration_intervals']))
276                     {
277                         $options_dom = $GLOBALS['app_list_strings']['duration_intervals'];
278                         $options_ret = array();
279                         foreach($options_dom as $key=>$oneOption)
280                                                 $options_ret[$key] = $this->get_name_value($key,$oneOption);
281
282                         $module_fields['duration_minutes']['options'] = $options_ret;
283                     }
284                 }
285
286                 if($value->module_dir == 'Bugs'){
287                         require_once('modules/Releases/Release.php');
288                         $seedRelease = new Release();
289                         $options = $seedRelease->get_releases(TRUE, "Active");
290                         $options_ret = array();
291                         foreach($options as $name=>$value){
292                                 $options_ret[] =  array('name'=> $name , 'value'=>$value);
293                         }
294                         if(isset($module_fields['fixed_in_release'])){
295                                 $module_fields['fixed_in_release']['type'] = 'enum';
296                                 $module_fields['fixed_in_release']['options'] = $options_ret;
297                         }
298             if(isset($module_fields['found_in_release'])){
299                 $module_fields['found_in_release']['type'] = 'enum';
300                 $module_fields['found_in_release']['options'] = $options_ret;
301             }
302                         if(isset($module_fields['release'])){
303                                 $module_fields['release']['type'] = 'enum';
304                                 $module_fields['release']['options'] = $options_ret;
305                         }
306                         if(isset($module_fields['release_name'])){
307                                 $module_fields['release_name']['type'] = 'enum';
308                                 $module_fields['release_name']['options'] = $options_ret;
309                         }
310                 }
311
312                 if(isset($value->assigned_user_name) && isset($module_fields['assigned_user_id'])) {
313                         $module_fields['assigned_user_name'] = $module_fields['assigned_user_id'];
314                         $module_fields['assigned_user_name']['name'] = 'assigned_user_name';
315                 }
316                 if(isset($value->assigned_name) && isset($module_fields['team_id'])) {
317                         $module_fields['team_name'] = $module_fields['team_id'];
318                         $module_fields['team_name']['name'] = 'team_name';
319                 }
320                 if(isset($module_fields['modified_user_id'])) {
321                         $module_fields['modified_by_name'] = $module_fields['modified_user_id'];
322                         $module_fields['modified_by_name']['name'] = 'modified_by_name';
323                 }
324                 if(isset($module_fields['created_by'])) {
325                         $module_fields['created_by_name'] = $module_fields['created_by'];
326                         $module_fields['created_by_name']['name'] = 'created_by_name';
327                 }
328
329                 $GLOBALS['log']->info('End: SoapHelperWebServices->get_field_list');
330                 return array('module_fields' => $module_fields, 'link_fields' => $link_fields);
331         }
332
333         /**
334          * Return the contents of a file base64 encoded
335          *
336          * @param string $filename - Full path of filename
337          * @param bool $remove - Indicates if the file should be removed after the contents is retrieved.
338          *
339          * @return string - Contents base64'd.
340          */
341         function get_file_contents_base64($filename, $remove = FALSE)
342         {
343             $contents = "";
344             if( file_exists($filename) )
345             {
346                 $contents =  base64_encode(file_get_contents($filename));
347                 if($remove)
348                 @unlink($filename);
349             }
350
351             return $contents;
352         }
353
354         function get_module_view_defs($module_name, $type, $view){
355         require_once('include/MVC/View/SugarView.php');
356         $metadataFile = null;
357         $results = array();
358         $view = strtolower($view);
359         switch (strtolower($type)){
360             case 'default':
361             default:
362                 if ($view == 'subpanel')
363                     $results = $this->get_subpanel_defs($module_name, $type);
364                 else
365                 {
366                     $v = new SugarView(null,array());
367                     $v->module = $module_name;
368                     $v->type = $view;
369                     $fullView = ucfirst($view) . 'View';
370                     $metadataFile = $v->getMetaDataFile();
371                     require_once($metadataFile);
372                     if($view == 'list')
373                         $results = $listViewDefs[$module_name];
374                     else
375                         $results = $viewdefs[$module_name][$fullView];
376                 }
377         }
378
379         return $results;
380     }
381
382     /**
383      * Equivalent of get_list function within SugarBean but allows the possibility to pass in an indicator
384      * if the list should filter for favorites.  Should eventually update the SugarBean function as well.
385      *
386      */
387     function get_data_list($seed, $order_by = "", $where = "", $row_offset = 0, $limit=-1, $max=-1, $show_deleted = 0, $favorites = false, $singleSelect=false)
388         {
389                 $GLOBALS['log']->debug("get_list:  order_by = '$order_by' and where = '$where' and limit = '$limit'");
390                 if(isset($_SESSION['show_deleted']))
391                 {
392                         $show_deleted = 1;
393                 }
394                 $order_by=$seed->process_order_by($order_by, null);
395
396                 if($seed->bean_implements('ACL') && ACLController::requireOwner($seed->module_dir, 'list') )
397                 {
398                         global $current_user;
399                         $owner_where = $seed->getOwnerWhere($current_user->id);
400                         if(!empty($owner_where)){
401                                 if(empty($where)){
402                                         $where = $owner_where;
403                                 }else{
404                                         $where .= ' AND '.  $owner_where;
405                                 }
406                         }
407                 }
408                 $params = array();
409                 if($favorites)
410                   $params['favorites'] = true;
411
412                 $query = $seed->create_new_list_query($order_by, $where,array(),$params, $show_deleted,'',false,null,$singleSelect);
413                 return $seed->process_list_query($query, $row_offset, $limit, $max, $where);
414         }
415
416     /**
417      * Add ACL values to metadata files.
418      *
419      * @param String $module_name
420      * @param String $view_type
421      * @param String $view  (list, detail,edit, etc)
422      * @param array $metadata The metadata for the view type and view.
423      * @return unknown
424      */
425         function addFieldLevelACLs($module_name,$view_type, $view, $metadata)
426         {
427             $functionName = "metdataAclParser" . ucfirst($view_type) . ucfirst($view);
428             if( method_exists($this, $functionName) )
429                return $this->$functionName($module_name, $metadata);
430             else
431                return $metadata;
432         }
433
434
435         /**
436          * Return the field level acl raw value.  We cannot use the hasAccess call as we do not have a valid bean
437          * record at the moment and therefore can not specify the is_owner flag.  We need the raw access value so we
438          * can do the computation on the client side.  TODO: Move function into ACLField class.
439          *
440          * @param String $module Name of the module
441          * @param String $field Name of the field
442          * @return int
443          */
444         function getFieldLevelACLValue($module, $field, $current_user = null)
445         {
446             if($current_user == null)
447                $current_user = $GLOBALS['current_user'];
448
449             if( is_admin($current_user) )
450                  return 99;
451
452             if(!isset($_SESSION['ACL'][$current_user->id][$module]['fields'][$field])){
453                          return 99;
454                 }
455
456                 return $_SESSION['ACL'][$current_user->id][$module]['fields'][$field];
457         }
458 }