]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - service/v3/SugarWebServiceUtilv3.php
Release 6.3.0
[Github/sugarcrm.git] / service / v3 / SugarWebServiceUtilv3.php
1 <?php
2 /*********************************************************************************
3  * SugarCRM Community Edition is a customer relationship management program developed by
4  * SugarCRM, Inc. Copyright (C) 2004-2011 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/core/SoapHelperWebService.php');
38 class SugarWebServiceUtilv3 extends SoapHelperWebServices {
39
40     function get_name_value($field,$value)
41     {
42         if($value instanceof Link2 && !method_exists($value, '__toString'))
43             $value = '';
44                 return array('name'=>$field, 'value'=>$value);
45         }
46
47     function filter_fields($value, $fields)
48     {
49         $GLOBALS['log']->info('Begin: SoapHelperWebServices->filter_fields');
50         global $invalid_contact_fields;
51         $filterFields = array();
52         foreach($fields as $field)
53         {
54             if (is_array($invalid_contact_fields))
55             {
56                 if (in_array($field, $invalid_contact_fields))
57                 {
58                     continue;
59                 }
60             }
61             if (isset($value->field_defs[$field]))
62             {
63                 $var = $value->field_defs[$field];
64                 if( isset($var['source'])
65                     && ($var['source'] != 'db' && $var['source'] != 'custom_fields' && $var['source'] != 'non-db')
66                     && $var['name'] != 'email1' && $var['name'] != 'email2'
67                     && (!isset($var['type'])|| $var['type'] != 'relate')) {
68
69                     if( $value->module_dir == 'Emails'
70                         && (($var['name'] == 'description') || ($var['name'] == 'description_html') || ($var['name'] == 'from_addr_name')
71                             || ($var['name'] == 'reply_to_addr') || ($var['name'] == 'to_addrs_names') || ($var['name'] == 'cc_addrs_names')
72                             || ($var['name'] == 'bcc_addrs_names') || ($var['name'] == 'raw_source')))
73                     {
74
75                     }
76                     else
77                     {
78                         continue;
79                     }
80                 }
81             }
82             $filterFields[] = $field;
83         }
84         $GLOBALS['log']->info('End: SoapHelperWebServices->filter_fields');
85         return $filterFields;
86     }
87
88     function getRelationshipResults($bean, $link_field_name, $link_module_fields, $optional_where = '', $order_by = '') {
89                 $GLOBALS['log']->info('Begin: SoapHelperWebServices->getRelationshipResults');
90                 require_once('include/TimeDate.php');
91                 global  $beanList, $beanFiles, $current_user;
92                 global $disable_date_format, $timedate;
93
94                 $bean->load_relationship($link_field_name);
95                 if (isset($bean->$link_field_name)) {
96                         //First get all the related beans
97             $related_beans = $bean->$link_field_name->getBeans();
98                         //Create a list of field/value rows based on $link_module_fields
99                         $list = array();
100             $filterFields = array();
101             if (!empty($order_by) && !empty($related_beans))
102             {
103                 $related_beans = order_beans($related_beans, $order_by);
104             }
105             foreach($related_beans as $id => $bean)
106             {
107                 if (empty($filterFields) && !empty($link_module_fields))
108                 {
109                     $filterFields = $this->filter_fields($bean, $link_module_fields);
110                 }
111                 $row = array();
112                 foreach ($filterFields as $field) {
113                     if (isset($bean->$field))
114                     {
115                         if (isset($bean->field_defs[$field]['type']) && $bean->field_defs[$field]['type'] == 'date') {
116                             $row[$field] = $timedate->to_display_date_time($bean->$field);
117                         }
118                         $row[$field] = $bean->$field;
119                     }
120                     else
121                     {
122                         $row[$field] = "";
123                     }
124                 }
125                 //Users can't see other user's hashes
126                 if(is_a($bean, 'User') && $current_user->id != $bean->id && isset($row['user_hash'])) {
127                     $row['user_hash'] = "";
128                 }
129                 $list[] = $row;
130             }
131             $GLOBALS['log']->info('End: SoapHelperWebServices->getRelationshipResults');
132             return array('rows' => $list, 'fields_set_on_rows' => $filterFields);
133                 } else {
134                         $GLOBALS['log']->info('End: SoapHelperWebServices->getRelationshipResults - ' . $link_field_name . ' relationship does not exists');
135                         return false;
136                 } // else
137
138         } // fn
139
140         function get_field_list($value, $fields, $translate=true) {
141
142             $GLOBALS['log']->info('Begin: SoapHelperWebServices->get_field_list');
143                 $module_fields = array();
144                 $link_fields = array();
145                 if(!empty($value->field_defs)){
146
147                         foreach($value->field_defs as $var){
148                                 if(!empty($fields) && !in_array( $var['name'], $fields))continue;
149                                 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;
150                                 if ((isset($var['source']) && $var['source'] == 'non_db') || (isset($var['type']) && $var['type'] == 'link')) {
151                                         continue;
152                                 }
153                                 $required = 0;
154                                 $options_dom = array();
155                                 $options_ret = array();
156                                 // Apparently the only purpose of this check is to make sure we only return fields
157                                 //   when we've read a record.  Otherwise this function is identical to get_module_field_list
158                                 if( isset($var['required']) && ($var['required'] || $var['required'] == 'true' ) ){
159                                         $required = 1;
160                                 }
161
162                                 if(isset($var['options'])){
163                                         $options_dom = translate($var['options'], $value->module_dir);
164                                         if(!is_array($options_dom)) $options_dom = array();
165                                         foreach($options_dom as $key=>$oneOption)
166                                                 $options_ret[$key] = $this->get_name_value($key,$oneOption);
167                                 }
168
169                     if(!empty($var['dbType']) && $var['type'] == 'bool') {
170                         $options_ret['type'] = $this->get_name_value('type', $var['dbType']);
171                     }
172
173                     $entry = array();
174                     $entry['name'] = $var['name'];
175                     $entry['type'] = $var['type'];
176                     $entry['group'] = isset($var['group']) ? $var['group'] : '';
177                     $entry['id_name'] = isset($var['id_name']) ? $var['id_name'] : '';
178
179                     if ($var['type'] == 'link') {
180                             $entry['relationship'] = (isset($var['relationship']) ? $var['relationship'] : '');
181                             $entry['module'] = (isset($var['module']) ? $var['module'] : '');
182                             $entry['bean_name'] = (isset($var['bean_name']) ? $var['bean_name'] : '');
183                                         $link_fields[$var['name']] = $entry;
184                     } else {
185                             if($translate) {
186                                 $entry['label'] = isset($var['vname']) ? translate($var['vname'], $value->module_dir) : $var['name'];
187                             } else {
188                                 $entry['label'] = isset($var['vname']) ? $var['vname'] : $var['name'];
189                             }
190                             $entry['required'] = $required;
191                             $entry['options'] = $options_ret;
192                             $entry['related_module'] = (isset($var['id_name']) && isset($var['module'])) ? $var['module'] : '';
193                                         if(isset($var['default'])) {
194                                            $entry['default_value'] = $var['default'];
195                                         }
196                                         $module_fields[$var['name']] = $entry;
197                     } // else
198                         } //foreach
199                 } //if
200
201                 if($value->module_dir == 'Bugs'){
202                         require_once('modules/Releases/Release.php');
203                         $seedRelease = new Release();
204                         $options = $seedRelease->get_releases(TRUE, "Active");
205                         $options_ret = array();
206                         foreach($options as $name=>$value){
207                                 $options_ret[] =  array('name'=> $name , 'value'=>$value);
208                         }
209                         if(isset($module_fields['fixed_in_release'])){
210                                 $module_fields['fixed_in_release']['type'] = 'enum';
211                                 $module_fields['fixed_in_release']['options'] = $options_ret;
212                         }
213                         if(isset($module_fields['release'])){
214                                 $module_fields['release']['type'] = 'enum';
215                                 $module_fields['release']['options'] = $options_ret;
216                         }
217                         if(isset($module_fields['release_name'])){
218                                 $module_fields['release_name']['type'] = 'enum';
219                                 $module_fields['release_name']['options'] = $options_ret;
220                         }
221                 }
222
223                 if(isset($value->assigned_user_name) && isset($module_fields['assigned_user_id'])) {
224                         $module_fields['assigned_user_name'] = $module_fields['assigned_user_id'];
225                         $module_fields['assigned_user_name']['name'] = 'assigned_user_name';
226                 }
227                 if(isset($value->assigned_name) && isset($module_fields['team_id'])) {
228                         $module_fields['team_name'] = $module_fields['team_id'];
229                         $module_fields['team_name']['name'] = 'team_name';
230                 }
231                 if(isset($module_fields['modified_user_id'])) {
232                         $module_fields['modified_by_name'] = $module_fields['modified_user_id'];
233                         $module_fields['modified_by_name']['name'] = 'modified_by_name';
234                 }
235                 if(isset($module_fields['created_by'])) {
236                         $module_fields['created_by_name'] = $module_fields['created_by'];
237                         $module_fields['created_by_name']['name'] = 'created_by_name';
238                 }
239
240                 $GLOBALS['log']->info('End: SoapHelperWebServices->get_field_list');
241                 return array('module_fields' => $module_fields, 'link_fields' => $link_fields);
242         }
243
244         function get_subpanel_defs($module, $type)
245         {
246             global $beanList, $beanFiles;
247             $results = array();
248             switch ($type)
249             {
250                 case 'wireless':
251                     if (file_exists('custom/modules/'.$module.'/metadata/wireless.subpaneldefs.php'))
252                          require_once('custom/modules/'.$module.'/metadata/wireless.subpaneldefs.php');
253                     else if (file_exists('modules/'.$module.'/metadata/wireless.subpaneldefs.php'))
254                          require_once('modules/'.$module.'/metadata/wireless.subpaneldefs.php');
255                     break;
256
257                 case 'default':
258                 default:
259                     if (file_exists ('modules/'.$module.'/metadata/subpaneldefs.php' ))
260                         require ('modules/'.$module.'/metadata/subpaneldefs.php');
261                     if ( file_exists('custom/modules/'.$module.'/Ext/Layoutdefs/layoutdefs.ext.php' ))
262                         require ('custom/modules/'.$module.'/Ext/Layoutdefs/layoutdefs.ext.php');
263             }
264
265             //Filter results for permissions
266             foreach ($layout_defs[$module]['subpanel_setup'] as $subpanel => $subpaneldefs)
267             {
268                 $moduleToCheck = $subpaneldefs['module'];
269                 if(!isset($beanList[$moduleToCheck]))
270                    continue;
271                 $class_name = $beanList[$moduleToCheck];
272                 $bean = new $class_name();
273                 if($bean->ACLAccess('list'))
274                     $results[$subpanel] = $subpaneldefs;
275             }
276
277             return $results;
278
279         }
280
281     function get_module_view_defs($module_name, $type, $view){
282         require_once('include/MVC/View/SugarView.php');
283         $metadataFile = null;
284         $results = array();
285         $view = strtolower($view);
286         switch (strtolower($type)){
287             case 'wireless':
288                 if( $view == 'list'){
289                     require_once('include/SugarWireless/SugarWirelessListView.php');
290                     $GLOBALS['module'] = $module_name; //WirelessView keys off global variable not instance variable...
291                     $v = new SugarWirelessListView();
292                     $results = $v->getMetaDataFile();
293                 }
294                 elseif ($view == 'subpanel')
295                     $results = $this->get_subpanel_defs($module_name, $type);
296                 else{
297                     require_once('include/SugarWireless/SugarWirelessView.php');
298                     $v = new SugarWirelessView();
299                     $v->module = $module_name;
300                     $fullView = ucfirst($view) . 'View';
301                     $meta = $v->getMetaDataFile('Wireless' . $fullView);
302                     $metadataFile = $meta['filename'];
303                     require_once($metadataFile);
304                     //Wireless detail metadata may actually be just edit metadata.
305                     $results = isset($viewdefs[$meta['module_name']][$fullView] ) ? $viewdefs[$meta['module_name']][$fullView] : $viewdefs[$meta['module_name']]['EditView'];
306                 }
307
308                 break;
309             case 'default':
310             default:
311                 if ($view == 'subpanel')
312                     $results = $this->get_subpanel_defs($module_name, $type);
313                 else
314                 {
315                     $v = new SugarView(null,array());
316                     $v->module = $module_name;
317                     $v->type = $view;
318                     $fullView = ucfirst($view) . 'View';
319                     $metadataFile = $v->getMetaDataFile();
320                     require_once($metadataFile);
321                     if($view == 'list')
322                         $results = $listViewDefs[$module_name];
323                     else
324                         $results = $viewdefs[$module_name][$fullView];
325                 }
326         }
327
328         return $results;
329     }
330
331     /**
332      * Examine the wireless_module_registry to determine which modules have been enabled for the mobile view.
333      *
334      * @param array $availModules An array of all the modules the user already has access to.
335      * @return array Modules enalbed for mobile view.
336      */
337     function get_visible_mobile_modules($availModules){
338         $enabled_modules = array();
339         $availModulesKey = array_flip($availModules);
340         foreach ( array ( '','custom/') as $prefix)
341         {
342                 if(file_exists($prefix.'include/MVC/Controller/wireless_module_registry.php'))
343                         require $prefix.'include/MVC/Controller/wireless_module_registry.php' ;
344         }
345
346         foreach ( $wireless_module_registry as $e => $def )
347         {
348                 if( isset($availModulesKey[$e]) )
349                 $enabled_modules[] = $e;
350         }
351
352         return $enabled_modules;
353     }
354
355     /**
356      * Examine the application to determine which modules have been enabled..
357      *
358      * @param array $availModules An array of all the modules the user already has access to.
359      * @return array Modules enabled within the application.
360      */
361     function get_visible_modules($availModules) {
362         require_once("modules/MySettings/TabController.php");
363         $controller = new TabController();
364         $tabs = $controller->get_tabs_system();
365         $enabled_modules= array();
366         $availModulesKey = array_flip($availModules);
367         foreach ($tabs[0] as $key=>$value)
368         {
369             if( isset($availModulesKey[$key]) )
370                 $enabled_modules[] = $key;
371         }
372
373         return $enabled_modules;
374     }
375
376     /**
377      * Retrieve all of the upcoming activities for a particular user.
378      *
379      * @return array
380      */
381     function get_upcoming_activities()
382     {
383         global $beanList;
384         $maxCount = 10;
385
386         $activityModules = array('Meetings' => array('date_field' => 'date_start','status' => 'Planned','status_field' => 'status', 'status_opp' => '='),
387                                  'Calls' => array('date_field' => 'date_start','status' => 'Planned','status_field' => 'status', 'status_opp' => '='),
388                                  'Tasks' => array('date_field' =>'date_due','status' => 'Not Started','status_field' => 'status','status_opp' => '='),
389                                  'Opportunities' => array('date_field' => 'date_closed','status' => array('Closed Won','Closed Lost'), 'status_field' => 'sales_stage', 'status_opp' => '!=') );
390         $results = array();
391         foreach ($activityModules as $module => $meta)
392         {
393             if(!self::check_modules_access($GLOBALS['current_user'], $module, 'read'))
394             {
395                 $GLOBALS['log']->debug("SugarWebServiceImpl->get_last_viewed: NO ACCESS to $module");
396                 continue;
397             }
398
399             $class_name = $beanList[$module];
400                 $seed = new $class_name();
401             $query = $this->generateUpcomingActivitiesWhereClause($seed, $meta);
402
403             $response = $seed->get_list(/* Order by date field */"{$meta['date_field']} ASC",  /*Where clause */$query, /* No Offset */ 0,
404                                         /* No limit */-1, /* Max 10 items */10, /*No Deleted */ 0 );
405
406             $result = array();
407
408             if( isset($response['list']) )
409                 $result = $this->format_upcoming_activities_entries($response['list'],$meta['date_field']);
410
411             $results = array_merge($results,$result);
412         }
413
414         //Sort the result list by the date due flag in ascending order
415         usort( $results, array( $this , "cmp_datedue" ) ) ;
416
417         //Only return a subset of the results.
418         $results = array_slice($results, 0, $maxCount);
419
420         return $results;
421     }
422
423     function generateUpcomingActivitiesWhereClause($seed,$meta)
424     {
425         $query = array();
426         $query_date = TimeDate::getInstance()->nowDb();
427         $query[] = " {$seed->table_name}.{$meta['date_field']} > '$query_date'"; //Add date filter
428         $query[] = "{$seed->table_name}.assigned_user_id = '{$GLOBALS['current_user']->id}' "; //Add assigned user filter
429         if(is_array($meta['status_field']))
430         {
431             foreach ($meta['status'] as $field)
432                 $query[] = "{$seed->table_name}.{$meta['status_field']} {$meta['status_opp']} '{$field}' ";
433         }
434         else
435             $query[] = "{$seed->table_name}.{$meta['status_field']} {$meta['status_opp']} '{$meta['status']}' ";
436
437         return implode(" AND ",$query);
438     }
439     /**
440      * Given a list of bean entries, format the expected response.
441      *
442      * @param array $list An array containing a bean list.
443      * @param string $date_field Name of the field storing the date field we are examining
444      * @return array The results.
445      */
446     function format_upcoming_activities_entries($list,$date_field)
447     {
448         $results = array();
449         foreach ($list as $bean)
450         {
451             $results[] = array('id' => $bean->id, 'module' => $bean->module_dir,'date_due' => $bean->$date_field,
452                                 'summary' => $bean->get_summary_text() );
453         }
454
455         return $results;
456     }
457
458     /**
459      * Sort the array for upcoming activities based on the date due flag ascending.
460      *
461      * @param array $a
462      * @param array $b
463      * @return int Indicates equality for date due flag
464      */
465     static function cmp_datedue( $a, $b )
466     {
467         $a_date = strtotime( $a['date_due'] ) ;
468         $b_date = strtotime( $b['date_due'] ) ;
469
470         if( $a_date == $b_date ) return 0 ;
471         return ($a_date > $b_date ) ? 1 : -1;
472   }
473
474 }