]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - service/v3/SugarWebServiceUtilv3.php
Release 6.5.6
[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-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/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($var['type'] == 'link') continue;
65                 if( isset($var['source'])
66                     && ($var['source'] != 'db' && $var['source'] != 'custom_fields' && $var['source'] != 'non-db')
67                     && $var['name'] != 'email1' && $var['name'] != 'email2'
68                     && (!isset($var['type'])|| $var['type'] != 'relate')) {
69
70                     if( $value->module_dir == 'Emails'
71                         && (($var['name'] == 'description') || ($var['name'] == 'description_html') || ($var['name'] == 'from_addr_name')
72                             || ($var['name'] == 'reply_to_addr') || ($var['name'] == 'to_addrs_names') || ($var['name'] == 'cc_addrs_names')
73                             || ($var['name'] == 'bcc_addrs_names') || ($var['name'] == 'raw_source')))
74                     {
75
76                     }
77                     else
78                     {
79                         continue;
80                     }
81                 }
82             }
83             $filterFields[] = $field;
84         }
85         $GLOBALS['log']->info('End: SoapHelperWebServices->filter_fields');
86         return $filterFields;
87     }
88
89     function getRelationshipResults($bean, $link_field_name, $link_module_fields, $optional_where = '', $order_by = '') {
90                 $GLOBALS['log']->info('Begin: SoapHelperWebServices->getRelationshipResults');
91                 require_once('include/TimeDate.php');
92                 global  $beanList, $beanFiles, $current_user;
93                 global $disable_date_format, $timedate;
94
95                 $bean->load_relationship($link_field_name);
96                 if (isset($bean->$link_field_name)) {
97                         //First get all the related beans
98             $params = array();
99             if (!empty($optional_where))
100             {
101                 $params['where'] = $optional_where;
102             }
103             $related_beans = $bean->$link_field_name->getBeans($params);
104             //Create a list of field/value rows based on $link_module_fields
105                         $list = array();
106             $filterFields = array();
107             if (!empty($order_by) && !empty($related_beans))
108             {
109                 $related_beans = order_beans($related_beans, $order_by);
110             }
111             foreach($related_beans as $id => $bean)
112             {
113                 if (empty($filterFields) && !empty($link_module_fields))
114                 {
115                     $filterFields = $this->filter_fields($bean, $link_module_fields);
116                 }
117                 $row = array();
118                 foreach ($filterFields as $field) {
119                     if (isset($bean->$field))
120                     {
121                         if (isset($bean->field_defs[$field]['type']) && $bean->field_defs[$field]['type'] == 'date') {
122                             $row[$field] = $timedate->to_display_date_time($bean->$field);
123                         }
124                         $row[$field] = $bean->$field;
125                     }
126                     else
127                     {
128                         $row[$field] = "";
129                     }
130                 }
131                 //Users can't see other user's hashes
132                 if(is_a($bean, 'User') && $current_user->id != $bean->id && isset($row['user_hash'])) {
133                     $row['user_hash'] = "";
134                 }
135                 $row = clean_sensitive_data($bean->field_defs, $row);
136                 $list[] = $row;
137             }
138             $GLOBALS['log']->info('End: SoapHelperWebServices->getRelationshipResults');
139             return array('rows' => $list, 'fields_set_on_rows' => $filterFields);
140                 } else {
141                         $GLOBALS['log']->info('End: SoapHelperWebServices->getRelationshipResults - ' . $link_field_name . ' relationship does not exists');
142                         return false;
143                 } // else
144
145         } // fn
146
147         function get_field_list($value, $fields, $translate=true) {
148
149             $GLOBALS['log']->info('Begin: SoapHelperWebServices->get_field_list');
150                 $module_fields = array();
151                 $link_fields = array();
152                 if(!empty($value->field_defs)){
153
154                         foreach($value->field_defs as $var){
155                                 if(!empty($fields) && !in_array( $var['name'], $fields))continue;
156                                 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;
157                                 if ((isset($var['source']) && $var['source'] == 'non_db') || (isset($var['type']) && $var['type'] == 'link')) {
158                                         continue;
159                                 }
160                                 $required = 0;
161                                 $options_dom = array();
162                                 $options_ret = array();
163                                 // Apparently the only purpose of this check is to make sure we only return fields
164                                 //   when we've read a record.  Otherwise this function is identical to get_module_field_list
165                                 if( isset($var['required']) && ($var['required'] || $var['required'] == 'true' ) ){
166                                         $required = 1;
167                                 }
168
169                                 if(isset($var['options'])){
170                                         $options_dom = translate($var['options'], $value->module_dir);
171                                         if(!is_array($options_dom)) $options_dom = array();
172                                         foreach($options_dom as $key=>$oneOption)
173                                                 $options_ret[$key] = $this->get_name_value($key,$oneOption);
174                                 }
175
176                     if(!empty($var['dbType']) && $var['type'] == 'bool') {
177                         $options_ret['type'] = $this->get_name_value('type', $var['dbType']);
178                     }
179
180                     $entry = array();
181                     $entry['name'] = $var['name'];
182                     $entry['type'] = $var['type'];
183                     $entry['group'] = isset($var['group']) ? $var['group'] : '';
184                     $entry['id_name'] = isset($var['id_name']) ? $var['id_name'] : '';
185
186                     if ($var['type'] == 'link') {
187                             $entry['relationship'] = (isset($var['relationship']) ? $var['relationship'] : '');
188                             $entry['module'] = (isset($var['module']) ? $var['module'] : '');
189                             $entry['bean_name'] = (isset($var['bean_name']) ? $var['bean_name'] : '');
190                                         $link_fields[$var['name']] = $entry;
191                     } else {
192                             if($translate) {
193                                 $entry['label'] = isset($var['vname']) ? translate($var['vname'], $value->module_dir) : $var['name'];
194                             } else {
195                                 $entry['label'] = isset($var['vname']) ? $var['vname'] : $var['name'];
196                             }
197                             $entry['required'] = $required;
198                             $entry['options'] = $options_ret;
199                             $entry['related_module'] = (isset($var['id_name']) && isset($var['module'])) ? $var['module'] : '';
200                                         if(isset($var['default'])) {
201                                            $entry['default_value'] = $var['default'];
202                                         }
203                                         $module_fields[$var['name']] = $entry;
204                     } // else
205                         } //foreach
206                 } //if
207
208                 if($value->module_dir == 'Bugs'){
209                         require_once('modules/Releases/Release.php');
210                         $seedRelease = new Release();
211                         $options = $seedRelease->get_releases(TRUE, "Active");
212                         $options_ret = array();
213                         foreach($options as $name=>$value){
214                                 $options_ret[] =  array('name'=> $name , 'value'=>$value);
215                         }
216                         if(isset($module_fields['fixed_in_release'])){
217                                 $module_fields['fixed_in_release']['type'] = 'enum';
218                                 $module_fields['fixed_in_release']['options'] = $options_ret;
219                         }
220                         if(isset($module_fields['release'])){
221                                 $module_fields['release']['type'] = 'enum';
222                                 $module_fields['release']['options'] = $options_ret;
223                         }
224                         if(isset($module_fields['release_name'])){
225                                 $module_fields['release_name']['type'] = 'enum';
226                                 $module_fields['release_name']['options'] = $options_ret;
227                         }
228                 }
229
230                 if(isset($value->assigned_user_name) && isset($module_fields['assigned_user_id'])) {
231                         $module_fields['assigned_user_name'] = $module_fields['assigned_user_id'];
232                         $module_fields['assigned_user_name']['name'] = 'assigned_user_name';
233                 }
234                 if(isset($value->assigned_name) && isset($module_fields['team_id'])) {
235                         $module_fields['team_name'] = $module_fields['team_id'];
236                         $module_fields['team_name']['name'] = 'team_name';
237                 }
238                 if(isset($module_fields['modified_user_id'])) {
239                         $module_fields['modified_by_name'] = $module_fields['modified_user_id'];
240                         $module_fields['modified_by_name']['name'] = 'modified_by_name';
241                 }
242                 if(isset($module_fields['created_by'])) {
243                         $module_fields['created_by_name'] = $module_fields['created_by'];
244                         $module_fields['created_by_name']['name'] = 'created_by_name';
245                 }
246
247                 $GLOBALS['log']->info('End: SoapHelperWebServices->get_field_list');
248                 return array('module_fields' => $module_fields, 'link_fields' => $link_fields);
249         }
250
251         function get_subpanel_defs($module, $type)
252         {
253             global $beanList, $beanFiles;
254             $results = array();
255             switch ($type)
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 'default':
288             default:
289                 if ($view == 'subpanel')
290                     $results = $this->get_subpanel_defs($module_name, $type);
291                 else
292                 {
293                     $v = new SugarView(null,array());
294                     $v->module = $module_name;
295                     $v->type = $view;
296                     $fullView = ucfirst($view) . 'View';
297                     $metadataFile = $v->getMetaDataFile();
298                     require_once($metadataFile);
299                     if($view == 'list')
300                         $results = $listViewDefs[$module_name];
301                     else
302                         $results = $viewdefs[$module_name][$fullView];
303                 }
304         }
305
306         return $results;
307     }
308
309     /**
310      * Examine the application to determine which modules have been enabled..
311      *
312      * @param array $availModules An array of all the modules the user already has access to.
313      * @return array Modules enabled within the application.
314      */
315     function get_visible_modules($availModules) {
316         require_once("modules/MySettings/TabController.php");
317         $controller = new TabController();
318         $tabs = $controller->get_tabs_system();
319         $enabled_modules= array();
320         $availModulesKey = array_flip($availModules);
321         foreach ($tabs[0] as $key=>$value)
322         {
323             if( isset($availModulesKey[$key]) )
324                 $enabled_modules[] = $key;
325         }
326
327         return $enabled_modules;
328     }
329
330     /**
331      * Retrieve all of the upcoming activities for a particular user.
332      *
333      * @return array
334      */
335     function get_upcoming_activities()
336     {
337         global $beanList;
338         $maxCount = 10;
339
340         $activityModules = array('Meetings' => array('date_field' => 'date_start','status' => 'Planned','status_field' => 'status', 'status_opp' => '='),
341                                  'Calls' => array('date_field' => 'date_start','status' => 'Planned','status_field' => 'status', 'status_opp' => '='),
342                                  'Tasks' => array('date_field' =>'date_due','status' => 'Not Started','status_field' => 'status','status_opp' => '='),
343                                  'Opportunities' => array('date_field' => 'date_closed','status' => array('Closed Won','Closed Lost'), 'status_field' => 'sales_stage', 'status_opp' => '!=') );
344         $results = array();
345         foreach ($activityModules as $module => $meta)
346         {
347             if(!self::check_modules_access($GLOBALS['current_user'], $module, 'read'))
348             {
349                 $GLOBALS['log']->debug("SugarWebServiceImpl->get_last_viewed: NO ACCESS to $module");
350                 continue;
351             }
352
353             $class_name = $beanList[$module];
354                 $seed = new $class_name();
355             $query = $this->generateUpcomingActivitiesWhereClause($seed, $meta);
356
357             $response = $seed->get_list(/* Order by date field */"{$meta['date_field']} ASC",  /*Where clause */$query, /* No Offset */ 0,
358                                         /* No limit */-1, /* Max 10 items */10, /*No Deleted */ 0 );
359
360             $result = array();
361
362             if( isset($response['list']) )
363                 $result = $this->format_upcoming_activities_entries($response['list'],$meta['date_field']);
364
365             $results = array_merge($results,$result);
366         }
367
368         //Sort the result list by the date due flag in ascending order
369         usort( $results, array( $this , "cmp_datedue" ) ) ;
370
371         //Only return a subset of the results.
372         $results = array_slice($results, 0, $maxCount);
373
374         return $results;
375     }
376
377     function generateUpcomingActivitiesWhereClause($seed,$meta)
378     {
379         $query = array();
380         $query_date = TimeDate::getInstance()->nowDb();
381         $query[] = " {$seed->table_name}.{$meta['date_field']} > '$query_date'"; //Add date filter
382         $query[] = "{$seed->table_name}.assigned_user_id = '{$GLOBALS['current_user']->id}' "; //Add assigned user filter
383         if(is_array($meta['status_field']))
384         {
385             foreach ($meta['status'] as $field)
386                 $query[] = "{$seed->table_name}.{$meta['status_field']} {$meta['status_opp']} '".$GLOBALS['db']->quote($field)."' ";
387         }
388         else
389             $query[] = "{$seed->table_name}.{$meta['status_field']} {$meta['status_opp']} '".$GLOBALS['db']->quote($meta['status'])."' ";
390
391         return implode(" AND ",$query);
392     }
393     /**
394      * Given a list of bean entries, format the expected response.
395      *
396      * @param array $list An array containing a bean list.
397      * @param string $date_field Name of the field storing the date field we are examining
398      * @return array The results.
399      */
400     function format_upcoming_activities_entries($list,$date_field)
401     {
402         $results = array();
403         foreach ($list as $bean)
404         {
405             $results[] = array('id' => $bean->id, 'module' => $bean->module_dir,'date_due' => $bean->$date_field,
406                                 'summary' => $bean->get_summary_text() );
407         }
408
409         return $results;
410     }
411
412     /**
413      * Sort the array for upcoming activities based on the date due flag ascending.
414      *
415      * @param array $a
416      * @param array $b
417      * @return int Indicates equality for date due flag
418      */
419     static function cmp_datedue( $a, $b )
420     {
421         $a_date = strtotime( $a['date_due'] ) ;
422         $b_date = strtotime( $b['date_due'] ) ;
423
424         if( $a_date == $b_date ) return 0 ;
425         return ($a_date > $b_date ) ? 1 : -1;
426   }
427
428 }