Test Failed
Push — CI ( 0f01dd...c95a04 )
by Adam
55:13
created

SugarWebServiceUtilv4::get_data_list()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 18
rs 9.4285
cc 3
eloc 9
nc 4
nop 8

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/*********************************************************************************
3
 * SugarCRM Community Edition is a customer relationship management program developed by
4
 * SugarCRM, Inc. Copyright (C) 2004-2013 SugarCRM Inc.
5
6
 * SuiteCRM is an extension to SugarCRM Community Edition developed by Salesagility Ltd.
7
 * Copyright (C) 2011 - 2014 Salesagility Ltd.
8
 *
9
 * This program is free software; you can redistribute it and/or modify it under
10
 * the terms of the GNU Affero General Public License version 3 as published by the
11
 * Free Software Foundation with the addition of the following permission added
12
 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
13
 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
14
 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
15
 *
16
 * This program is distributed in the hope that it will be useful, but WITHOUT
17
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18
 * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
19
 * details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License along with
22
 * this program; if not, see http://www.gnu.org/licenses or write to the Free
23
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24
 * 02110-1301 USA.
25
 *
26
 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
27
 * SW2-130, Cupertino, CA 95014, USA. or at email address [email protected].
28
 *
29
 * The interactive user interfaces in modified source and object code versions
30
 * of this program must display Appropriate Legal Notices, as required under
31
 * Section 5 of the GNU Affero General Public License version 3.
32
 *
33
 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
34
 * these Appropriate Legal Notices must retain the display of the "Powered by
35
 * SugarCRM" logo and "Supercharged by SuiteCRM" logo. If the display of the logos is not
36
 * reasonably feasible for  technical reasons, the Appropriate Legal Notices must
37
 * display the words  "Powered by SugarCRM" and "Supercharged by SuiteCRM".
38
 ********************************************************************************/
39
40
require_once('service/v3_1/SugarWebServiceUtilv3_1.php');
41
42
class SugarWebServiceUtilv4 extends SugarWebServiceUtilv3_1
43
{
44
    function get_module_view_defs($moduleName, $type, $view)
45
    {
46
        require_once('include/MVC/View/SugarView.php');
47
        $metadataFile = null;
48
        $results = array();
49
        if( empty($moduleName) )
50
            return $results;
51
52
        $view = strtolower($view);
53
        switch (strtolower($type)){
54
            case 'default':
55
            default:
56
                if ($view == 'subpanel')
57
                    $results = $this->get_subpanel_defs($moduleName, $type);
58
                else
59
                {
60
                    $v = new SugarView(null,array());
61
                    $v->module = $moduleName;
62
                    $v->type = $view;
63
                    $fullView = ucfirst($view) . 'View';
64
                    $metadataFile = $v->getMetaDataFile();
65
                    require_once($metadataFile);
66
                    if($view == 'list')
67
                        $results = $listViewDefs[$moduleName];
68
                    else
69
                        $results = $viewdefs[$moduleName][$fullView];
70
                }
71
        }
72
73
        //Add field level acls.
74
        $results = $this->addFieldLevelACLs($moduleName,$type, $view, $results);
75
76
        return $results;
77
    }
78
79
80
    /**
81
     * Equivalent of get_list function within SugarBean but allows the possibility to pass in an indicator
82
     * if the list should filter for favorites.  Should eventually update the SugarBean function as well.
83
     *
84
     */
85
    function get_data_list($seed, $order_by = "", $where = "", $row_offset = 0, $limit=-1, $max=-1, $show_deleted = 0, $favorites = false)
86
	{
87
		$GLOBALS['log']->debug("get_list:  order_by = '$order_by' and where = '$where' and limit = '$limit'");
88
		if(isset($_SESSION['show_deleted']))
89
		{
90
			$show_deleted = 1;
91
		}
92
		// Fix bug with sort order in get_entry_list
93
		// $order_by=$seed->process_order_by($order_by, null);
94
95
		$params = array();
96
		if(!empty($favorites)) {
97
		  $params['favorites'] = true;
98
		}
99
100
		$query = $seed->create_new_list_query($order_by, $where,array(),$params, $show_deleted);
101
		return $seed->process_list_query($query, $row_offset, $limit, $max, $where);
102
	}
103
104
	/**
105
     * Convert modules list to Web services result
106
     *
107
     * @param array $list List of module candidates (only keys are used)
108
     * @param array $availModules List of module availability from Session
109
     */
110
    public function getModulesFromList($list, $availModules)
111
    {
112
        global $app_list_strings;
113
        $enabled_modules = array();
114
        $availModulesKey = array_flip($availModules);
115
        foreach ($list as $key=>$value)
116
        {
117
            if( isset($availModulesKey[$key]) )
118
            {
119
                $label = !empty( $app_list_strings['moduleList'][$key] ) ? $app_list_strings['moduleList'][$key] : '';
120
        	    $acl = $this->checkModuleRoleAccess($key);
121
        	    $fav = $this->is_favorites_enabled($key);
122
        	    $enabled_modules[] = array('module_key' => $key,'module_label' => $label, 'favorite_enabled' => $fav, 'acls' => $acl);
123
            }
124
        }
125
        return $enabled_modules;
126
    }
127
128
    /**
129
     * Return a boolean indicating if the bean name is favorites enabled.
130
     *
131
     * @param string The module name
132
     * @return bool true indicating bean is favorites enabled
133
     */
134
    function is_favorites_enabled($module_name)
135
    {
136
        global $beanList, $beanFiles;
137
138
        $fav = FALSE;
139
        return $fav;
140
    }
141
142
143
	/**
144
	 * Processes the filter_fields attribute to use with SugarBean::create_new_list_query()
145
	 *
146
	 * @param object $value SugarBean
147
	 * @param array $fields
148
	 * @return array
149
	 */
150
    protected function filter_fields_for_query(SugarBean $value, array $fields)
151
    {
152
        $GLOBALS['log']->info('Begin: SoapHelperWebServices->filter_fields_for_query');
153
        $filterFields = array();
154
        foreach($fields as $field)
155
        {
156
            if (isset($value->field_defs[$field]))
157
            {
158
                $filterFields[$field] = $value->field_defs[$field];
159
            }
160
        }
161
        $GLOBALS['log']->info('End: SoapHelperWebServices->filter_fields_for_query');
162
        return $filterFields;
163
    }
164
165
    function get_field_list($value,$fields,  $translate=true) {
166
167
	    $GLOBALS['log']->info('Begin: SoapHelperWebServices->get_field_list(too large a struct, '.print_r($fields, true).", $translate");
168
		$module_fields = array();
169
		$link_fields = array();
170
		if(!empty($value->field_defs)){
171
172
			foreach($value->field_defs as $var){
173
				if(!empty($fields) && !in_array( $var['name'], $fields))continue;
174
				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;
175
				if ((isset($var['source']) && $var['source'] == 'non_db') && (isset($var['type']) && $var['type'] != 'link')) {
176
					continue;
177
				}
178
				$required = 0;
179
				$options_dom = array();
180
				$options_ret = array();
181
				// Apparently the only purpose of this check is to make sure we only return fields
182
				//   when we've read a record.  Otherwise this function is identical to get_module_field_list
183
				if( isset($var['required']) && ($var['required'] || $var['required'] == 'true' ) ){
184
					$required = 1;
185
				}
186
187
				if($var['type'] == 'bool')
188
				    $var['options'] = 'checkbox_dom';
189
190
				if(isset($var['options'])){
191
					$options_dom = translate($var['options'], $value->module_dir);
192
					if(!is_array($options_dom)) $options_dom = array();
193
					foreach($options_dom as $key=>$oneOption)
194
						$options_ret[$key] = $this->get_name_value($key,$oneOption);
195
				}
196
197
	            if(!empty($var['dbType']) && $var['type'] == 'bool') {
198
	                $options_ret['type'] = $this->get_name_value('type', $var['dbType']);
199
	            }
200
201
	            $entry = array();
202
	            $entry['name'] = $var['name'];
203
	            $entry['type'] = $var['type'];
204
	            $entry['group'] = isset($var['group']) ? $var['group'] : '';
205
	            $entry['id_name'] = isset($var['id_name']) ? $var['id_name'] : '';
206
207
	            if ($var['type'] == 'link') {
208
		            $entry['relationship'] = (isset($var['relationship']) ? $var['relationship'] : '');
209
		            $entry['module'] = (isset($var['module']) ? $var['module'] : '');
210
		            $entry['bean_name'] = (isset($var['bean_name']) ? $var['bean_name'] : '');
211
					$link_fields[$var['name']] = $entry;
212
	            } else {
213
		            if($translate) {
214
		            	$entry['label'] = isset($var['vname']) ? translate($var['vname'], $value->module_dir) : $var['name'];
215
		            } else {
216
		            	$entry['label'] = isset($var['vname']) ? $var['vname'] : $var['name'];
217
		            }
218
		            $entry['required'] = $required;
219
		            $entry['options'] = $options_ret;
220
		            $entry['related_module'] = (isset($var['id_name']) && isset($var['module'])) ? $var['module'] : '';
221
		            $entry['calculated'] =  (isset($var['calculated']) && $var['calculated']) ? true : false;
222
                    $entry['len'] =  isset($var['len']) ? $var['len'] : '';
223
224
					if(isset($var['default'])) {
225
					   $entry['default_value'] = $var['default'];
226
					}
227
					if( $var['type'] == 'parent' && isset($var['type_name']) )
228
					   $entry['type_name'] = $var['type_name'];
229
230
					$module_fields[$var['name']] = $entry;
231
	            } // else
232
			} //foreach
233
		} //if
234
235
		if($value->module_dir == 'Meetings' || $value->module_dir == 'Calls')
236
		{
237
		    if( isset($module_fields['duration_minutes']) && isset($GLOBALS['app_list_strings']['duration_intervals']))
238
		    {
239
		        $options_dom = $GLOBALS['app_list_strings']['duration_intervals'];
240
		        $options_ret = array();
241
		        foreach($options_dom as $key=>$oneOption)
242
						$options_ret[$key] = $this->get_name_value($key,$oneOption);
243
244
		        $module_fields['duration_minutes']['options'] = $options_ret;
245
		    }
246
		}
247
248
		if($value->module_dir == 'Bugs'){
249
			require_once('modules/Releases/Release.php');
250
			$seedRelease = new Release();
251
			$options = $seedRelease->get_releases(TRUE, "Active");
252
			$options_ret = array();
253
			foreach($options as $name=>$value){
254
				$options_ret[] =  array('name'=> $name , 'value'=>$value);
255
			}
256
			if(isset($module_fields['fixed_in_release'])){
257
				$module_fields['fixed_in_release']['type'] = 'enum';
258
				$module_fields['fixed_in_release']['options'] = $options_ret;
259
			}
260
            if(isset($module_fields['found_in_release'])){
261
                $module_fields['found_in_release']['type'] = 'enum';
262
                $module_fields['found_in_release']['options'] = $options_ret;
263
            }
264
			if(isset($module_fields['release'])){
265
				$module_fields['release']['type'] = 'enum';
266
				$module_fields['release']['options'] = $options_ret;
267
			}
268
			if(isset($module_fields['release_name'])){
269
				$module_fields['release_name']['type'] = 'enum';
270
				$module_fields['release_name']['options'] = $options_ret;
271
			}
272
		}
273
274
		if(isset($value->assigned_user_name) && isset($module_fields['assigned_user_id'])) {
275
			$module_fields['assigned_user_name'] = $module_fields['assigned_user_id'];
276
			$module_fields['assigned_user_name']['name'] = 'assigned_user_name';
277
		}
278
		if(isset($value->assigned_name) && isset($module_fields['team_id'])) {
279
			$module_fields['team_name'] = $module_fields['team_id'];
280
			$module_fields['team_name']['name'] = 'team_name';
281
		}
282
		if(isset($module_fields['modified_user_id'])) {
283
			$module_fields['modified_by_name'] = $module_fields['modified_user_id'];
284
			$module_fields['modified_by_name']['name'] = 'modified_by_name';
285
		}
286
		if(isset($module_fields['created_by'])) {
287
			$module_fields['created_by_name'] = $module_fields['created_by'];
288
			$module_fields['created_by_name']['name'] = 'created_by_name';
289
		}
290
291
		$GLOBALS['log']->info('End: SoapHelperWebServices->get_field_list');
292
		return array('module_fields' => $module_fields, 'link_fields' => $link_fields);
293
	}
294
295
296
	function new_handle_set_entries($module_name, $name_value_lists, $select_fields = FALSE) {
297
		$GLOBALS['log']->info('Begin: SoapHelperWebServices->new_handle_set_entries');
298
		global $beanList, $beanFiles, $current_user, $app_list_strings;
299
300
		$ret_values = array();
301
302
		$class_name = $beanList[$module_name];
303
		require_once($beanFiles[$class_name]);
304
		$ids = array();
305
		$count = 1;
306
		$total = sizeof($name_value_lists);
307
		foreach($name_value_lists as $name_value_list){
308
			$seed = new $class_name();
309
310
			$seed->update_vcal = false;
311
			foreach($name_value_list as $name => $value){
312
				if(is_array($value) &&  $value['name'] == 'id'){
313
                    $seed->retrieve($value['value']);
314
                    break;
315
                }
316
                else if($name === 'id' ){
317
                    $seed->retrieve($value);
318
                }
319
			}
320
321
			foreach($name_value_list as $name => $value) {
322
			    //Normalize the input
323
				if(!is_array($value)){
324
                    $field_name = $name;
325
                    $val = $value;
326
                }
327
                else{
328
                    $field_name = $value['name'];
329
                    $val = $value['value'];
330
                }
331
332
				if($seed->field_name_map[$field_name]['type'] == 'enum'){
333
					$vardef = $seed->field_name_map[$field_name];
334
					if(isset($app_list_strings[$vardef['options']]) && !isset($app_list_strings[$vardef['options']][$val]) ) {
335
						if ( in_array($val,$app_list_strings[$vardef['options']]) ){
336
							$val = array_search($val,$app_list_strings[$vardef['options']]);
337
						}
338
					}
339
				}
340
				if($module_name == 'Users' && !empty($seed->id) && ($seed->id != $current_user->id) && $field_name == 'user_hash'){
341
					continue;
342
				}
343
				if(!empty($seed->field_name_map[$field_name]['sensitive'])) {
344
					continue;
345
				}
346
				$seed->$field_name = $val;
347
			}
348
349
			if($count == $total){
350
				$seed->update_vcal = false;
351
			}
352
			$count++;
353
354
			//Add the account to a contact
355
			if($module_name == 'Contacts'){
356
				$GLOBALS['log']->debug('Creating Contact Account');
357
				$this->add_create_account($seed);
358
				$duplicate_id = $this->check_for_duplicate_contacts($seed);
359
				if($duplicate_id == null){
360
					if($seed->ACLAccess('Save') && ($seed->deleted != 1 || $seed->ACLAccess('Delete'))){
361
						$seed->save();
362
						if($seed->deleted == 1){
363
							$seed->mark_deleted($seed->id);
364
						}
365
						$ids[] = $seed->id;
366
					}
367
				}
368
				else{
369
					//since we found a duplicate we should set the sync flag
370
					if( $seed->ACLAccess('Save')){
371
						$seed = new $class_name();
372
						$seed->id = $duplicate_id;
373
						$seed->contacts_users_id = $current_user->id;
374
						$seed->save();
375
						$ids[] = $duplicate_id;//we have a conflict
376
					}
377
				}
378
			}
379
			else if($module_name == 'Meetings' || $module_name == 'Calls'){
380
				//we are going to check if we have a meeting in the system
381
				//with the same outlook_id. If we do find one then we will grab that
382
				//id and save it
383
				if( $seed->ACLAccess('Save') && ($seed->deleted != 1 || $seed->ACLAccess('Delete'))){
384
					if(empty($seed->id) && !isset($seed->id)){
385
						if(!empty($seed->outlook_id) && isset($seed->outlook_id)){
386
							//at this point we have an object that does not have
387
							//the id set, but does have the outlook_id set
388
							//so we need to query the db to find if we already
389
							//have an object with this outlook_id, if we do
390
							//then we can set the id, otherwise this is a new object
391
							$order_by = "";
392
							$query = $seed->table_name.".outlook_id = '".$seed->outlook_id."'";
393
							$response = $seed->get_list($order_by, $query, 0,-1,-1,0);
394
							$list = $response['list'];
395
							if(count($list) > 0){
396
								foreach($list as $value)
397
								{
398
									$seed->id = $value->id;
399
									break;
400
								}
401
							}//fi
402
						}//fi
403
					}//fi
404
				    if (empty($seed->reminder_time)) {
405
                        $seed->reminder_time = -1;
406
                    }
407
                    if($seed->reminder_time == -1){
408
                        $defaultRemindrTime = $current_user->getPreference('reminder_time');
409
                        if ($defaultRemindrTime != -1){
410
                            $seed->reminder_checked = '1';
411
                            $seed->reminder_time = $defaultRemindrTime;
412
                        }
413
                    }
414
					$seed->save();
415
					if($seed->deleted == 1){
416
						$seed->mark_deleted($seed->id);
417
					}
418
					$ids[] = $seed->id;
419
				}//fi
420
			}
421
			else
422
			{
423
				if( $seed->ACLAccess('Save') && ($seed->deleted != 1 || $seed->ACLAccess('Delete'))){
424
					$seed->save();
425
					$ids[] = $seed->id;
426
				}
427
			}
428
429
			// if somebody is calling set_entries_detail() and wants fields returned...
430
			if ($select_fields !== FALSE) {
431
				$ret_values[$count] = array();
432
433
				foreach ($select_fields as $select_field) {
0 ignored issues
show
Bug introduced by
The expression $select_fields of type boolean is not traversable.
Loading history...
434
					if (isset($seed->$select_field)) {
435
						$ret_values[$count][$select_field] = $this->get_name_value($select_field, $seed->$select_field);
436
					}
437
				}
438
			}
439
		}
440
441
		// handle returns for set_entries_detail() and set_entries()
442
		if ($select_fields !== FALSE) {
443
			$GLOBALS['log']->info('End: SoapHelperWebServices->new_handle_set_entries');
444
			return array(
445
				'name_value_lists' => $ret_values,
446
			);
447
		}
448
		else {
449
			$GLOBALS['log']->info('End: SoapHelperWebServices->new_handle_set_entries');
450
			return array(
451
				'ids' => $ids,
452
			);
453
		}
454
	}
455
456
457
    function checkSessionAndModuleAccess($session, $login_error_key, $module_name, $access_level, $module_access_level_error_key, $errorObject)
458
    {
459
          if(isset($_REQUEST['oauth_token'])) {
460
              $session = $this->checkOAuthAccess($errorObject);
461
          }
462
          if(!$session) return false;
463
          return parent::checkSessionAndModuleAccess($session, $login_error_key, $module_name, $access_level, $module_access_level_error_key, $errorObject);
464
    }
465
466
    public function checkOAuthAccess($errorObject)
467
    {
468
        require_once "include/SugarOAuthServer.php";
469
        try {
470
	        $oauth = new SugarOAuthServer();
471
	        $token = $oauth->authorizedToken();
472
	        if(empty($token) || empty($token->assigned_user_id)) {
473
	            return false;
474
	        }
475
        } catch(OAuthException $e) {
476
            $GLOBALS['log']->debug("OAUTH Exception: $e");
477
            $errorObject->set_error('invalid_login');
478
			$this->setFaultObject($errorObject);
479
            return false;
480
        }
481
482
	    $user = new User();
483
	    $user->retrieve($token->assigned_user_id);
484
	    if(empty($user->id)) {
485
	        return false;
486
	    }
487
        global $current_user;
488
		$current_user = $user;
489
		ini_set("session.use_cookies", 0); // disable cookies to prevent session ID from going out
490
		session_start();
491
		session_regenerate_id();
492
		$_SESSION['oauth'] = $oauth->authorization();
493
		$_SESSION['avail_modules'] = $this->get_user_module_list($user);
494
		// TODO: handle role
495
		// handle session
496
		$_SESSION['is_valid_session']= true;
497
		$_SESSION['ip_address'] = query_client_ip();
498
		$_SESSION['user_id'] = $current_user->id;
499
		$_SESSION['type'] = 'user';
500
		$_SESSION['authenticated_user_id'] = $current_user->id;
501
        return session_id();
502
    }
503
504
505
    /**
506
     * get_subpanel_defs
507
     *
508
     * @param String $module The name of the module to get the subpanel definition for
509
     * @param String $type The type of subpanel definition ('wireless' or 'default')
510
     * @return array Array of the subpanel definition; empty array if no matching definition found
511
     */
512
	function get_subpanel_defs($module, $type)
513
	{
514
	    global $beanList, $beanFiles;
515
	    $results = array();
516
	    switch ($type)
517
	    {
518
	        case 'wireless':
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
519
520
                if (file_exists('custom/modules/'.$module.'/metadata/wireless.subpaneldefs.php'))
521
	                 require_once('custom/modules/'.$module.'/metadata/wireless.subpaneldefs.php');
522
	            else if (file_exists('modules/'.$module.'/metadata/wireless.subpaneldefs.php'))
523
	                 require_once('modules/'.$module.'/metadata/wireless.subpaneldefs.php');
524
525
                //If an Ext/WirelessLayoutdefs/wireless.subpaneldefs.ext.php file exists, then also load it as well
526
                if(file_exists('custom/modules/'.$module.'/Ext/WirelessLayoutdefs/wireless.subpaneldefs.ext.php'))
527
                {
528
                    require_once('custom/modules/'.$module.'/Ext/WirelessLayoutdefs/wireless.subpaneldefs.ext.php');
529
                }
530
	            break;
531
532
	        case 'default':
533
	        default:
534
	            if (file_exists ('modules/'.$module.'/metadata/subpaneldefs.php' ))
535
	                require ('modules/'.$module.'/metadata/subpaneldefs.php');
536
	            if ( file_exists('custom/modules/'.$module.'/Ext/Layoutdefs/layoutdefs.ext.php' ))
537
	                require ('custom/modules/'.$module.'/Ext/Layoutdefs/layoutdefs.ext.php');
538
	    }
539
540
	    //Filter results for permissions
541
	    foreach ($layout_defs[$module]['subpanel_setup'] as $subpanel => $subpaneldefs)
542
	    {
543
	        $moduleToCheck = $subpaneldefs['module'];
544
	        if(!isset($beanList[$moduleToCheck]))
545
	           continue;
546
	        $class_name = $beanList[$moduleToCheck];
547
	        $bean = new $class_name();
548
	        if($bean->ACLAccess('list'))
549
	            $results[$subpanel] = $subpaneldefs;
550
	    }
551
552
	    return $results;
553
554
	}
555
}
556