]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - soap/SoapHelperFunctions.php
Release 6.2.2
[Github/sugarcrm.git] / soap / SoapHelperFunctions.php
1 <?php
2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4  * SugarCRM Community Edition is a customer relationship management program developed by
5  * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
6  * 
7  * This program is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU Affero General Public License version 3 as published by the
9  * Free Software Foundation with the addition of the following permission added
10  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13  * 
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
17  * details.
18  * 
19  * You should have received a copy of the GNU Affero General Public License along with
20  * this program; if not, see http://www.gnu.org/licenses or write to the Free
21  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22  * 02110-1301 USA.
23  * 
24  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26  * 
27  * The interactive user interfaces in modified source and object code versions
28  * of this program must display Appropriate Legal Notices, as required under
29  * Section 5 of the GNU Affero General Public License version 3.
30  * 
31  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32  * these Appropriate Legal Notices must retain the display of the "Powered by
33  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34  * technical reasons, the Appropriate Legal Notices must display the words
35  * "Powered by SugarCRM".
36  ********************************************************************************/
37
38
39 /**
40  * Retrieve field data for a provided SugarBean.
41  *
42  * @param SugarBean $value -- The bean to retrieve the field information for.
43  * @return Array -- 'field'=>   'name' -- the name of the field
44  *                              'type' -- the data type of the field
45  *                              'label' -- the translation key for the label of the field
46  *                              'required' -- Is the field required?
47  *                              'options' -- Possible values for a drop down field
48  */
49 function get_field_list($value, $translate=true){
50         $list = array();
51
52         if(!empty($value->field_defs)){
53
54                 foreach($value->field_defs as $var){
55                         if(isset($var['source']) && ($var['source'] != 'db' && $var['source'] != 'custom_fields') && $var['name'] != 'email1' && $var['name'] != 'email2' && (!isset($var['type'])|| $var['type'] != 'relate'))continue;
56                         $required = 0;
57                         $options_dom = array();
58                         $options_ret = array();
59                         // Apparently the only purpose of this check is to make sure we only return fields
60                         //   when we've read a record.  Otherwise this function is identical to get_module_field_list
61                         if(!empty($var['required'])) {
62                                 $required = 1;
63                         }
64                         if(isset($var['options'])){
65                                 $options_dom = translate($var['options'], $value->module_dir);
66                                 if(!is_array($options_dom)) $options_dom = array();
67                                 foreach($options_dom as $key=>$oneOption)
68                                         $options_ret[] = get_name_value($key,$oneOption);
69                         }
70
71             if(!empty($var['dbType']) && $var['type'] == 'bool') {
72                 $options_ret[] = get_name_value('type', $var['dbType']);
73             }
74
75             $entry = array();
76             $entry['name'] = $var['name'];
77             $entry['type'] = $var['type'];
78             if($translate) {
79             $entry['label'] = isset($var['vname']) ? translate($var['vname'], $value->module_dir) : $var['name'];
80             } else {
81             $entry['label'] = isset($var['vname']) ? $var['vname'] : $var['name'];
82             }
83             $entry['required'] = $required;
84             $entry['options'] = $options_ret;
85                         if(isset($var['default'])) {
86                            $entry['default_value'] = $var['default'];
87                         }
88
89                         $list[$var['name']] = $entry;
90                 } //foreach
91         } //if
92
93         if($value->module_dir == 'Bugs'){
94
95                 $seedRelease = new Release();
96                 $options = $seedRelease->get_releases(TRUE, "Active");
97                 $options_ret = array();
98                 foreach($options as $name=>$value){
99                         $options_ret[] =  array('name'=> $name , 'value'=>$value);
100                 }
101                 if(isset($list['fixed_in_release'])){
102                         $list['fixed_in_release']['type'] = 'enum';
103                         $list['fixed_in_release']['options'] = $options_ret;
104                 }
105                 if(isset($list['release'])){
106                         $list['release']['type'] = 'enum';
107                         $list['release']['options'] = $options_ret;
108                 }
109                 if(isset($list['release_name'])){
110                         $list['release_name']['type'] = 'enum';
111                         $list['release_name']['options'] = $options_ret;
112                 }
113         }
114     if($value->module_dir == 'Emails'){
115         $fields = array('from_addr_name', 'reply_to_addr', 'to_addrs_names', 'cc_addrs_names', 'bcc_addrs_names');
116         foreach($fields as $field){
117             $var = $value->field_defs[$field];
118
119             $required = 0;
120             $entry = array();
121             $entry['name'] = $var['name'];
122             $entry['type'] = $var['type'];
123             if($translate) {
124             $entry['label'] = isset($var['vname']) ? translate($var['vname'], $value->module_dir) : $var['name'];
125             } else {
126             $entry['label'] = isset($var['vname']) ? $var['vname'] : $var['name'];
127             }
128             $entry['required'] = $required;
129             $entry['options'] = array();
130                         if(isset($var['default'])) {
131                            $entry['default_value'] = $var['default'];
132                         }
133
134                         $list[$var['name']] = $entry;
135         }
136     }
137
138         if(isset($value->assigned_user_name) && isset($list['assigned_user_id'])) {
139                 $list['assigned_user_name'] = $list['assigned_user_id'];
140                 $list['assigned_user_name']['name'] = 'assigned_user_name';
141         }
142         if(isset($list['modified_user_id'])) {
143                 $list['modified_by_name'] = $list['modified_user_id'];
144                 $list['modified_by_name']['name'] = 'modified_by_name';
145         }
146         if(isset($list['created_by'])) {
147                 $list['created_by_name'] = $list['created_by'];
148                 $list['created_by_name']['name'] = 'created_by_name';
149         }
150         return $list;
151 }
152
153 function new_get_field_list($value, $translate=true) {
154         $module_fields = array();
155         $link_fields = array();
156
157         if(!empty($value->field_defs)){
158
159                 foreach($value->field_defs as $var){
160                         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;
161                         if ($var['source'] == 'non_db' && (isset($var['type']) && $var['type'] != 'link')) {
162                                 continue;
163                         }
164                         $required = 0;
165                         $options_dom = array();
166                         $options_ret = array();
167                         // Apparently the only purpose of this check is to make sure we only return fields
168                         //   when we've read a record.  Otherwise this function is identical to get_module_field_list
169                         if(!empty($var['required'])) {
170                                 $required = 1;
171                         }
172                         if(isset($var['options'])){
173                                 $options_dom = translate($var['options'], $value->module_dir);
174                                 if(!is_array($options_dom)) $options_dom = array();
175                                 foreach($options_dom as $key=>$oneOption)
176                                         $options_ret[] = get_name_value($key,$oneOption);
177                         }
178
179             if(!empty($var['dbType']) && $var['type'] == 'bool') {
180                 $options_ret[] = get_name_value('type', $var['dbType']);
181             }
182
183             $entry = array();
184             $entry['name'] = $var['name'];
185             $entry['type'] = $var['type'];
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                                 if(isset($var['default'])) {
200                                    $entry['default_value'] = $var['default'];
201                                 }
202                                 $module_fields[$var['name']] = $entry;
203             } // else
204                 } //foreach
205         } //if
206
207         if($value->module_dir == 'Bugs'){
208
209                 $seedRelease = new Release();
210                 $options = $seedRelease->get_releases(TRUE, "Active");
211                 $options_ret = array();
212                 foreach($options as $name=>$value){
213                         $options_ret[] =  array('name'=> $name , 'value'=>$value);
214                 }
215                 if(isset($module_fields['fixed_in_release'])){
216                         $module_fields['fixed_in_release']['type'] = 'enum';
217                         $module_fields['fixed_in_release']['options'] = $options_ret;
218                 }
219                 if(isset($module_fields['release'])){
220                         $module_fields['release']['type'] = 'enum';
221                         $module_fields['release']['options'] = $options_ret;
222                 }
223                 if(isset($module_fields['release_name'])){
224                         $module_fields['release_name']['type'] = 'enum';
225                         $module_fields['release_name']['options'] = $options_ret;
226                 }
227         }
228
229         if(isset($value->assigned_user_name) && isset($module_fields['assigned_user_id'])) {
230                 $module_fields['assigned_user_name'] = $module_fields['assigned_user_id'];
231                 $module_fields['assigned_user_name']['name'] = 'assigned_user_name';
232         }
233         if(isset($module_fields['modified_user_id'])) {
234                 $module_fields['modified_by_name'] = $module_fields['modified_user_id'];
235                 $module_fields['modified_by_name']['name'] = 'modified_by_name';
236         }
237         if(isset($module_fields['created_by'])) {
238                 $module_fields['created_by_name'] = $module_fields['created_by'];
239                 $module_fields['created_by_name']['name'] = 'created_by_name';
240         }
241
242         return array('module_fields' => $module_fields, 'link_fields' => $link_fields);
243 } // fn
244
245 function setFaultObject($errorObject) {
246         global $soap_server_object;
247         $soap_server_object->fault($errorObject->getFaultCode(), $errorObject->getName(), '', $errorObject->getDescription());
248 } // fn
249
250 function checkSessionAndModuleAccess($session, $login_error_key, $module_name, $access_level, $module_access_level_error_key, $errorObject) {
251         if(!validate_authenticated($session)){
252                 $errorObject->set_error('invalid_login');
253                 setFaultObject($errorObject);
254                 return false;
255         } // if
256
257         global  $beanList, $beanFiles;
258         if (!empty($module_name)) {
259                 if(empty($beanList[$module_name])){
260                         $errorObject->set_error('no_module');
261                         setFaultObject($errorObject);
262                         return false;
263                 } // if
264                 global $current_user;
265                 if(!check_modules_access($current_user, $module_name, $access_level)){
266                         $errorObject->set_error('no_access');
267                         setFaultObject($errorObject);
268                         return false;
269                 }
270         } // if
271         return true;
272 } // fn
273
274 function checkACLAccess($bean, $viewType, $errorObject, $error_key) {
275         if(!$bean->ACLAccess($viewType)){
276                 $errorObject->set_error($error_key);
277                 setFaultObject($errorObject);
278                 return false;
279         } // if
280         return true;
281 } // fn
282
283 function get_name_value($field,$value){
284         return array('name'=>$field, 'value'=>$value);
285 }
286
287 function get_user_module_list($user){
288         global $app_list_strings, $current_language, $beanList, $beanFiles;
289
290         $app_list_strings = return_app_list_strings_language($current_language);
291         $modules = query_module_access_list($user);
292         ACLController :: filterModuleList($modules, false);
293         global $modInvisList;
294
295         foreach($modInvisList as $invis){
296                 $modules[$invis] = 'read_only';
297         }
298
299         $actions = ACLAction::getUserActions($user->id,true);
300         foreach($actions as $key=>$value){
301                 if($value['module']['access']['aclaccess'] < ACL_ALLOW_ENABLED){
302                         if ($value['module']['access']['aclaccess'] == ACL_ALLOW_DISABLED) {
303                                 unset($modules[$key]);
304                         } else {
305                                 $modules[$key] = 'read_only';
306                         } // else
307                 } else {
308                         $modules[$key] = '';
309                 } // else
310         } // foreach
311
312         //Remove all modules that don't have a beanFiles entry associated with it
313         foreach($modules as $module_name=>$module)
314         {
315                 $class_name = $beanList[$module_name];
316                 if(empty($beanFiles[$class_name]))
317                 {
318                    unset($modules[$module_name]);
319                 }
320         }
321
322         return $modules;
323
324 }
325
326 function check_modules_access($user, $module_name, $action='write'){
327         if(!isset($_SESSION['avail_modules'])){
328                 $_SESSION['avail_modules'] = get_user_module_list($user);
329         }
330     if(isset($_SESSION['avail_modules'][$module_name])){
331                 if($action == 'write' && $_SESSION['avail_modules'][$module_name] == 'read_only'){
332                         if(is_admin($user))return true;
333                         return false;
334                 }elseif($action == 'write' && strcmp(strtolower($module_name), 'users') == 0 && !$user->isAdminForModule($module_name)){
335             //rrs bug: 46000 - If the client is trying to write to the Users module and is not an admin then we need to stop them
336             return false;
337         }
338                 return true;
339         }
340         return false;
341
342 }
343
344 function get_name_value_list($value, $returnDomValue = false){
345         global $app_list_strings;
346         $list = array();
347         if(!empty($value->field_defs)){
348                 if(isset($value->assigned_user_name)) {
349                         $list['assigned_user_name'] = get_name_value('assigned_user_name', $value->assigned_user_name);
350                 }
351                 if(isset($value->modified_by_name)) {
352                         $list['modified_by_name'] = get_name_value('modified_by_name', $value->modified_by_name);
353                 }
354                 if(isset($value->created_by_name)) {
355                         $list['created_by_name'] = get_name_value('created_by_name', $value->created_by_name);
356                 }
357                 foreach($value->field_defs as $var){
358                         if(isset($var['source']) && ($var['source'] != 'db' && $var['source'] != 'custom_fields') && $var['name'] != 'email1' && $var['name'] != 'email2' && (!isset($var['type'])|| $var['type'] != 'relate')){
359
360                                         if($value->module_dir == 'Emails' && (($var['name'] == 'description') || ($var['name'] == 'description_html') || ($var['name'] == 'from_addr_name') || ($var['name'] == 'reply_to_addr') || ($var['name'] == 'to_addrs_names') || ($var['name'] == 'cc_addrs_names') || ($var['name'] == 'bcc_addrs_names') || ($var['name'] == 'raw_source'))) {
361
362                                         } else {
363                                                 continue;
364                                         }
365                                 }
366
367                         if(isset($value->$var['name'])){
368                                 $val = $value->$var['name'];
369                                 $type = $var['type'];
370
371                                 if(strcmp($type, 'date') == 0){
372                                         $val = substr($val, 0, 10);
373                                 }elseif(strcmp($type, 'enum') == 0 && !empty($var['options']) && $returnDomValue){
374                                         $val = $app_list_strings[$var['options']][$val];
375                                 }
376
377                                 $list[$var['name']] = get_name_value($var['name'], $val);
378                         }
379                 }
380         }
381         return $list;
382
383 }
384
385 function filter_fields($value, $fields) {
386         global $invalid_contact_fields;
387         $filterFields = array();
388         foreach($fields as $field){
389                 if (is_array($invalid_contact_fields)) {
390                         if (in_array($field, $invalid_contact_fields)) {
391                                 continue;
392                         } // if
393                 } // if
394                 if (isset($value->field_defs[$field])) {
395                         $var = $value->field_defs[$field];
396                         if(isset($var['source']) && ($var['source'] != 'db' && $var['source'] != 'custom_fields') && $var['name'] != 'email1' && $var['name'] != 'email2' && (!isset($var['type'])|| $var['type'] != 'relate')) {
397
398                                 continue;
399                         }
400                 } // if
401                 $filterFields[] = $field;
402         } // foreach
403         return $filterFields;
404 } // fn
405
406 function get_name_value_list_for_fields($value, $fields) {
407         global $app_list_strings;
408         global $invalid_contact_fields;
409
410         $list = array();
411         if(!empty($value->field_defs)){
412                 if(isset($value->assigned_user_name) && in_array('assigned_user_name', $fields)) {
413                         $list['assigned_user_name'] = get_name_value('assigned_user_name', $value->assigned_user_name);
414                 }
415                 if(isset($value->modified_by_name) && in_array('modified_by_name', $fields)) {
416                         $list['modified_by_name'] = get_name_value('modified_by_name', $value->modified_by_name);
417                 }
418                 if(isset($value->created_by_name) && in_array('created_by_name', $fields)) {
419                         $list['created_by_name'] = get_name_value('created_by_name', $value->created_by_name);
420                 }
421
422                 $filterFields = filter_fields($value, $fields);
423                 foreach($filterFields as $field){
424                         $var = $value->field_defs[$field];
425                         if(isset($value->$var['name'])){
426                                 $val = $value->$var['name'];
427                                 $type = $var['type'];
428
429                                 if(strcmp($type, 'date') == 0){
430                                         $val = substr($val, 0, 10);
431                                 }elseif(strcmp($type, 'enum') == 0 && !empty($var['options'])){
432                                         $val = $app_list_strings[$var['options']][$val];
433                                 }
434
435                                 $list[$var['name']] = get_name_value($var['name'], $val);
436                         } // if
437                 } // foreach
438         } // if
439         return $list;
440
441 } // fn
442
443
444 function array_get_name_value_list($array){
445         $list = array();
446         foreach($array as $name=>$value){
447
448                                 $list[$name] = get_name_value($name, $value);
449         }
450         return $list;
451
452 }
453
454 function array_get_name_value_lists($array){
455     $list = array();
456     foreach($array as $name=>$value){
457         $tmp_value=$value;
458         if(is_array($value)){
459             $tmp_value = array();
460             foreach($value as $k=>$v){
461                 $tmp_value[] = get_name_value($k, $v);
462             }
463         }
464         $list[] = get_name_value($name, $tmp_value);
465     }
466     return $list;
467 }
468
469 function name_value_lists_get_array($list){
470     $array = array();
471     foreach($list as $key=>$value){
472         if(isset($value['value']) && isset($value['name'])){
473             if(is_array($value['value'])){
474                 $array[$value['name']]=array();
475                 foreach($value['value'] as $v){
476                     $array[$value['name']][$v['name']]=$v['value'];
477                 }
478             }else{
479                 $array[$value['name']]=$value['value'];
480             }
481         }
482     }
483     return $array;
484 }
485
486 function array_get_return_value($array, $module){
487
488         return Array('id'=>$array['id'],
489                                 'module_name'=> $module,
490                                 'name_value_list'=>array_get_name_value_list($array)
491                                 );
492 }
493
494 function get_return_value_for_fields($value, $module, $fields) {
495         global $module_name, $current_user;
496         $module_name = $module;
497         if($module == 'Users' && $value->id != $current_user->id){
498                 $value->user_hash = '';
499         }
500         return Array('id'=>$value->id,
501                                 'module_name'=> $module,
502                                 'name_value_list'=>get_name_value_list_for_fields($value, $fields)
503                                 );
504 }
505
506 function getRelationshipResults($bean, $link_field_name, $link_module_fields, $optional_where = '') {
507         global  $beanList, $beanFiles;
508         $bean->load_relationship($link_field_name);
509         if (isset($bean->$link_field_name)) {
510                 // get the query object for this link field
511                 $query_array = $bean->$link_field_name->getQuery(true,array(),0,'',true);
512                 $params = array();
513                 $params['joined_tables'] = $query_array['join_tables'];
514
515                 // get the related module name and instantiate a bean for that.
516                 $submodulename = $bean->$link_field_name->getRelatedModuleName();
517                 $submoduleclass = $beanList[$submodulename];
518                 require_once($beanFiles[$submoduleclass]);
519
520                 $submodule = new $submoduleclass();
521                 $filterFields = filter_fields($submodule, $link_module_fields);
522                 $relFields = $bean->$link_field_name->getRelatedFields();
523                 $roleSelect = '';
524
525                 if(!empty($relFields)){
526                         foreach($link_module_fields as $field){
527                                 if(!empty($relFields[$field])){
528                                         $roleSelect .= ', ' . $query_array['join_tables'][0] . '.'. $field;
529                                 }
530                         }
531                 }
532                 // create a query
533                 $subquery = $submodule->create_new_list_query('',$optional_where ,$filterFields,$params, 0,'', true,$bean);
534                 $query =  $subquery['select'].$roleSelect .   $subquery['from'].$query_array['join']. $subquery['where'];
535
536                 $result = $submodule->db->query($query, true);
537                 $list = array();
538                 while($row = $submodule->db->fetchByAssoc($result)) {
539                         $list[] = $row;
540                 }
541                 return array('rows' => $list, 'fields_set_on_rows' => $filterFields);
542         } else {
543                 return false;
544         } // else
545
546 } // fn
547
548 function get_return_value_for_link_fields($bean, $module, $link_name_to_value_fields_array) {
549         global $module_name, $current_user;
550         $module_name = $module;
551         if($module == 'Users' && $bean->id != $current_user->id){
552                 $bean->user_hash = '';
553         }
554
555         if (empty($link_name_to_value_fields_array) || !is_array($link_name_to_value_fields_array)) {
556                 return array();
557         }
558
559         $link_output = array();
560         foreach($link_name_to_value_fields_array as $link_name_value_fields) {
561                 if (!is_array($link_name_value_fields) || !isset($link_name_value_fields['name']) || !isset($link_name_value_fields['value'])) {
562                         continue;
563                 }
564                 $link_field_name = $link_name_value_fields['name'];
565                 $link_module_fields = $link_name_value_fields['value'];
566                 if (is_array($link_module_fields) && !empty($link_module_fields)) {
567                         $result = getRelationshipResults($bean, $link_field_name, $link_module_fields);
568                         if (!$result) {
569                                 $link_output[] = array('name' => $link_field_name, 'records' => array());
570                                 continue;
571                         }
572                         $list = $result['rows'];
573                         $filterFields = $result['fields_set_on_rows'];
574                         if ($list) {
575                                 $rowArray = array();
576                                 foreach($list as $row) {
577                                         $nameValueArray = array();
578                                         foreach ($filterFields as $field) {
579                                                 $nameValue = array();
580                                                 if (isset($row[$field])) {
581                                                         $nameValue['name'] = $field;
582                                                         $nameValue['value'] = $row[$field];
583                                                         $nameValueArray[] = $nameValue;
584                                                 } // if
585                                         } // foreach
586                                         $rowArray[] = $nameValueArray;
587                                 } // foreach
588                                 $link_output[] = array('name' => $link_field_name, 'records' => $rowArray);
589                         } // if
590                 } // if
591         } // foreach
592         return $link_output;
593 } // fn
594
595 /**
596  *
597  * @param String $module_name -- The name of the module that the primary record is from.  This name should be the name the module was developed under (changing a tab name is studio does not affect the name that should be passed into this method).
598  * @param String $module_id -- The ID of the bean in the specified module
599  * @param String $link_field_name - The relationship name for which to create realtionships.
600  * @param Array $related_ids -- The array of ids for which we want to create relationships
601  * @return true on success, false on failure
602  */
603 function new_handle_set_relationship($module_name, $module_id, $link_field_name, $related_ids) {
604     global  $beanList, $beanFiles;
605
606     if(empty($beanList[$module_name])) {
607         return false;
608     } // if
609     $class_name = $beanList[$module_name];
610     require_once($beanFiles[$class_name]);
611     $mod = new $class_name();
612     $mod->retrieve($module_id);
613         if(!$mod->ACLAccess('DetailView')){
614                 return false;
615         }
616
617     foreach($related_ids as $ids) {
618         $GLOBALS['log']->debug("ids = " . $ids );
619     }
620
621         if ($mod->load_relationship($link_field_name)) {
622                 $mod->$link_field_name->add($related_ids);
623                 return true;
624         } else {
625                 return false;
626         }
627 }
628
629 function new_handle_set_entries($module_name, $name_value_lists, $select_fields = FALSE) {
630         global $beanList, $beanFiles, $app_list_strings;
631         global $current_user;
632
633         $ret_values = array();
634
635         $class_name = $beanList[$module_name];
636         require_once($beanFiles[$class_name]);
637         $ids = array();
638         $count = 1;
639         $total = sizeof($name_value_lists);
640         foreach($name_value_lists as $name_value_list){
641                 $seed = new $class_name();
642
643                 $seed->update_vcal = false;
644                 foreach($name_value_list as $value){
645                         if($value['name'] == 'id'){
646                                 $seed->retrieve($value['value']);
647                                 break;
648                         }
649                 }
650
651                 foreach($name_value_list as $value) {
652                         $val = $value['value'];
653                         if($seed->field_name_map[$value['name']]['type'] == 'enum'){
654                                 $vardef = $seed->field_name_map[$value['name']];
655                                 if(isset($app_list_strings[$vardef['options']]) && !isset($app_list_strings[$vardef['options']][$value]) ) {
656                             if ( in_array($val,$app_list_strings[$vardef['options']]) ){
657                                 $val = array_search($val,$app_list_strings[$vardef['options']]);
658                             }
659                         }
660                         }
661                         $seed->$value['name'] = $val;
662                 }
663
664                 if($count == $total){
665                         $seed->update_vcal = false;
666                 }
667                 $count++;
668
669                 //Add the account to a contact
670                 if($module_name == 'Contacts'){
671                         $GLOBALS['log']->debug('Creating Contact Account');
672                         add_create_account($seed);
673                         $duplicate_id = check_for_duplicate_contacts($seed);
674                         if($duplicate_id == null){
675                                 if($seed->ACLAccess('Save') && ($seed->deleted != 1 || $seed->ACLAccess('Delete'))){
676                                         $seed->save();
677                                         if($seed->deleted == 1){
678                                                 $seed->mark_deleted($seed->id);
679                                         }
680                                         $ids[] = $seed->id;
681                                 }
682                         }
683                         else{
684                                 //since we found a duplicate we should set the sync flag
685                                 if( $seed->ACLAccess('Save')){
686                                         $seed = new $class_name();
687                                         $seed->id = $duplicate_id;
688                                         $seed->contacts_users_id = $current_user->id;
689                                         $seed->save();
690                                         $ids[] = $duplicate_id;//we have a conflict
691                                 }
692                         }
693                 }
694                 else if($module_name == 'Meetings' || $module_name == 'Calls'){
695                         //we are going to check if we have a meeting in the system
696                         //with the same outlook_id. If we do find one then we will grab that
697                         //id and save it
698                         if( $seed->ACLAccess('Save') && ($seed->deleted != 1 || $seed->ACLAccess('Delete'))){
699                                 if(empty($seed->id) && !isset($seed->id)){
700                                         if(!empty($seed->outlook_id) && isset($seed->outlook_id)){
701                                                 //at this point we have an object that does not have
702                                                 //the id set, but does have the outlook_id set
703                                                 //so we need to query the db to find if we already
704                                                 //have an object with this outlook_id, if we do
705                                                 //then we can set the id, otherwise this is a new object
706                                                 $order_by = "";
707                                                 $query = $seed->table_name.".outlook_id = '".$seed->outlook_id."'";
708                                                 $response = $seed->get_list($order_by, $query, 0,-1,-1,0);
709                                                 $list = $response['list'];
710                                                 if(count($list) > 0){
711                                                         foreach($list as $value)
712                                                         {
713                                                                 $seed->id = $value->id;
714                                                                 break;
715                                                         }
716                                                 }//fi
717                                         }//fi
718                                 }//fi
719                                 $seed->save();
720                                 $ids[] = $seed->id;
721                         }//fi
722                 }
723                 else
724                 {
725                         if( $seed->ACLAccess('Save') && ($seed->deleted != 1 || $seed->ACLAccess('Delete'))){
726                                 $seed->save();
727                                 $ids[] = $seed->id;
728                         }
729                 }
730
731                 // if somebody is calling set_entries_detail() and wants fields returned...
732                 if ($select_fields !== FALSE) {
733                         $ret_values[$count] = array();
734
735                         foreach ($select_fields as $select_field) {
736                                 if (isset($seed->$select_field)) {
737                                         $ret_values[$count][] = get_name_value($select_field, $seed->$select_field);
738                                 }
739                         }
740                 }
741         }
742
743         // handle returns for set_entries_detail() and set_entries()
744         if ($select_fields !== FALSE) {
745                 return array(
746                         'name_value_lists' => $ret_values,
747                 );
748         }
749         else {
750                 return array(
751                         'ids' => $ids,
752                 );
753         }
754 }
755
756 function get_return_value($value, $module, $returnDomValue = false){
757         global $module_name, $current_user;
758         $module_name = $module;
759         if($module == 'Users' && $value->id != $current_user->id){
760                 $value->user_hash = '';
761         }
762         return Array('id'=>$value->id,
763                                 'module_name'=> $module,
764                                 'name_value_list'=>get_name_value_list($value, $returnDomValue)
765                                 );
766 }
767
768
769 function get_name_value_xml($val, $module_name){
770         $xml = '<item>';
771                         $xml .= '<id>'.$val['id'].'</id>';
772                         $xml .= '<module>'.$module_name.'</module>';
773                         $xml .= '<name_value_list>';
774                         foreach($val['name_value_list'] as $name=>$nv){
775                                 $xml .= '<name_value>';
776                                 $xml .= '<name>'.htmlspecialchars($nv['name']).'</name>';
777                                 $xml .= '<value>'.htmlspecialchars($nv['value']).'</value>';
778                                 $xml .= '</name_value>';
779                         }
780                         $xml .= '</name_value_list>';
781                         $xml .= '</item>';
782                         return $xml;
783 }
784
785 function new_get_return_module_fields($value, $module, $translate=true){
786         global $module_name;
787         $module_name = $module;
788         $result = new_get_field_list($value, $translate);
789         return Array('module_name'=>$module,
790                                 'module_fields'=> $result['module_fields'],
791                                 'link_fields'=> $result['link_fields'],
792                                 );
793 }
794
795 function get_return_module_fields($value, $module, $error, $translate=true){
796         global $module_name;
797         $module_name = $module;
798         return Array('module_name'=>$module,
799                                 'module_fields'=> get_field_list($value, $translate),
800                                 'error'=>get_name_value_list($value)
801                                 );
802 }
803
804 function get_return_error_value($error_num, $error_name, $error_description){
805         return Array('number'=>$error_num,
806                                 'name'=> $error_name,
807                                 'description'=> $error_description
808                                 );
809 }
810
811 function filter_field_list(&$field_list, $select_fields, $module_name){
812         return filter_return_list($field_list, $select_fields, $module_name);
813 }
814
815
816 /**
817  * Filter the results of a list query.  Limit the fields returned.
818  *
819  * @param Array $output_list -- The array of list data
820  * @param Array $select_fields -- The list of fields that should be returned.  If this array is specfied, only the fields in the array will be returned.
821  * @param String $module_name -- The name of the module this being worked on
822  * @return The filtered array of list data.
823  */
824 function filter_return_list(&$output_list, $select_fields, $module_name){
825
826         for($sug = 0; $sug < sizeof($output_list) ; $sug++){
827                 if($module_name == 'Contacts'){
828                         global $invalid_contact_fields;
829                         if(is_array($invalid_contact_fields)){
830                                 foreach($invalid_contact_fields as $name=>$val){
831                                         unset($output_list[$sug]['field_list'][$name]);
832                                         unset($output_list[$sug]['name_value_list'][$name]);
833
834                                 }
835                         }
836                 }
837
838                 if( !empty($output_list[$sug]['name_value_list']) && is_array($output_list[$sug]['name_value_list']) && !empty($select_fields) && is_array($select_fields)){
839                         foreach($output_list[$sug]['name_value_list'] as $name=>$value)
840                                         if(!in_array($value['name'], $select_fields)){
841                                                 unset($output_list[$sug]['name_value_list'][$name]);
842                                                 unset($output_list[$sug]['field_list'][$name]);
843                                         }
844                 }
845         }
846         return $output_list;
847 }
848
849 function login_success(){
850         global $current_language, $sugar_config, $app_strings, $app_list_strings;
851         $current_language = $sugar_config['default_language'];
852         $app_strings = return_application_language($current_language);
853         $app_list_strings = return_app_list_strings_language($current_language);
854 }
855
856
857 /*
858  *      Given an account_name, either create the account or assign to a contact.
859  */
860 function add_create_account($seed)
861 {
862         global $current_user;
863         $account_name = $seed->account_name;
864         $account_id = $seed->account_id;
865         $assigned_user_id = $current_user->id;
866
867         // check if it already exists
868     $focus = new Account();
869     if( $focus->ACLAccess('Save')){
870                 $class = get_class($seed);
871                 $temp = new $class();
872                 $temp->retrieve($seed->id);
873                 if ((! isset($account_name) || $account_name == ''))
874                 {
875                         return;
876                 }
877                 if (!isset($seed->accounts)){
878                         $seed->load_relationship('accounts');
879                 }
880
881                 if($seed->account_name == '' && isset($temp->account_id)){
882                         $seed->accounts->delete($seed->id, $temp->account_id);
883                         return;
884                 }
885
886             $arr = array();
887
888
889
890             $query = "select id, deleted from {$focus->table_name} WHERE name='".$seed->db->quote($account_name)."'";
891             $query .=" ORDER BY deleted ASC";
892             $result = $seed->db->query($query) or sugar_die("Error selecting sugarbean: ".mysql_error());
893
894             $row = $seed->db->fetchByAssoc($result, -1, false);
895
896                 // we found a row with that id
897             if (isset($row['id']) && $row['id'] != -1)
898             {
899                 // if it exists but was deleted, just remove it entirely
900                 if ( isset($row['deleted']) && $row['deleted'] == 1)
901                 {
902                     $query2 = "delete from {$focus->table_name} WHERE id='". $seed->db->quote($row['id'])."'";
903                     $result2 = $seed->db->query($query2) or sugar_die("Error deleting existing sugarbean: ".mysql_error());
904                         }
905                         // else just use this id to link the contact to the account
906                 else
907                 {
908                         $focus->id = $row['id'];
909                 }
910             }
911
912                 // if we didnt find the account, so create it
913             if (! isset($focus->id) || $focus->id == '')
914             {
915                 $focus->name = $account_name;
916
917                         if ( isset($assigned_user_id))
918                         {
919                    $focus->assigned_user_id = $assigned_user_id;
920                    $focus->modified_user_id = $assigned_user_id;
921                         }
922                 $focus->save();
923             }
924
925             if($seed->accounts != null && $temp->account_id != null && $temp->account_id != $focus->id){
926                 $seed->accounts->delete($seed->id, $temp->account_id);
927             }
928
929             if(isset($focus->id) && $focus->id != ''){
930                         $seed->account_id = $focus->id;
931                 }
932     }
933 }
934
935 function check_for_duplicate_contacts($seed){
936
937
938         if(isset($seed->id)){
939                 return null;
940         }
941
942         $query = '';
943
944         $trimmed_email = trim($seed->email1);
945     $trimmed_email2 = trim($seed->email2);
946         $trimmed_last = trim($seed->last_name);
947         $trimmed_first = trim($seed->first_name);
948         if(!empty($trimmed_email) || !empty($trimmed_email2)){
949
950                 //obtain a list of contacts which contain the same email address
951                 $contacts = $seed->emailAddress->getBeansByEmailAddress($trimmed_email);
952         $contacts2 = $seed->emailAddress->getBeansByEmailAddress($trimmed_email2);
953         $contacts = array_merge($contacts, $contacts2);
954                 if(count($contacts) == 0){
955                         return null;
956                 }else{
957             foreach($contacts as $contact){
958                                 if(!empty($trimmed_last) && strcmp($trimmed_last, $contact->last_name) == 0){
959                     if((!empty($trimmed_email) || !empty($trimmed_email2)) && (strcmp($trimmed_email, $contact->email1) == 0 || strcmp($trimmed_email, $contact->email2) == 0 || strcmp($trimmed_email2, $contact->email) == 0 || strcmp($trimmed_email2, $contact->email2) == 0)){
960                                                 //bug: 39234 - check if the account names are the same
961                                                 //if the incoming contact's account_name is empty OR it is not empty and is the same
962                                                 //as an existing contact's account name, then find the match.
963                         $contact->load_relationship('accounts');
964                                                 if(empty($seed->account_name) || strcmp($seed->account_name, $contact->account_name) == 0){
965                                                     $GLOBALS['log']->info('End: SoapHelperWebServices->check_for_duplicate_contacts - duplicte found ' . $contact->id);
966                                                         return $contact->id;
967                                                 }
968                                         }
969                                 }
970                         }
971                         return null;
972                 }
973         }else{
974             $query = "contacts.last_name = '".$seed->db->quote($trimmed_last,false)."'";
975         $query .= " AND contacts.first_name = '".$seed->db->quote($trimmed_first,false)."'";
976         $contacts = $seed->get_list('', $query);
977         if (count($contacts) == 0){
978             return null;
979         }else{
980             foreach($contacts['list'] as $contact){
981                 if (empty($contact->email1)){
982                     return $contact->id;
983                 }
984             }
985             return null;
986         }
987         }
988 }
989
990 /*
991  * Given a client version and a server version, determine if the right hand side(server version) is greater
992  *
993  * @param left           the client sugar version
994  * @param right          the server version
995  *
996  * return               true if the server version is greater or they are equal
997  *                      false if the client version is greater
998  */
999 function is_server_version_greater($left, $right){
1000     if(count($left) == 0 && count($right) == 0){
1001         return false;
1002     }
1003     else if(count($left) == 0 || count($right) == 0){
1004         return true;
1005     }
1006     else if($left[0] == $right[0]){
1007         array_shift($left);
1008         array_shift($right);
1009         return is_server_version_greater($left, $right);
1010     }
1011     else if($left[0] < $right[0]){
1012        return true;
1013     }
1014     else
1015         return false;
1016 }
1017
1018 function getFile( $zip_file, $file_in_zip ){
1019     global $sugar_config;
1020     $base_upgrade_dir = $sugar_config['upload_dir'] . "/upgrades";
1021     $base_tmp_upgrade_dir   = "$base_upgrade_dir/temp";
1022     $my_zip_dir = mk_temp_dir( $base_tmp_upgrade_dir );
1023     unzip_file( $zip_file, $file_in_zip, $my_zip_dir );
1024     return( "$my_zip_dir/$file_in_zip" );
1025 }
1026
1027 function getManifest( $zip_file ){
1028     ini_set("max_execution_time", "3600");
1029     return( getFile( $zip_file, "manifest.php" ) );
1030 }
1031
1032 if(!function_exists("get_encoded")){
1033 /*HELPER FUNCTIONS*/
1034 function get_encoded($object){
1035                 return  base64_encode(serialize($object));
1036 }
1037 function get_decoded($object){
1038                 return  unserialize(base64_decode($object));
1039
1040 }
1041
1042 /**
1043  * decrypt a string use the TripleDES algorithm. This meant to be
1044  * modified if the end user chooses a different algorithm
1045  *
1046  * @param $string - the string to decrypt
1047  *
1048  * @return a decrypted string if we can decrypt, the original string otherwise
1049  */
1050 function decrypt_string($string){
1051         if(function_exists('mcrypt_cbc')){
1052
1053                 $focus = new Administration();
1054                 $focus->retrieveSettings();
1055                 $key = '';
1056                 if(!empty($focus->settings['ldap_enc_key'])){
1057                         $key = $focus->settings['ldap_enc_key'];
1058                 }
1059                 if(empty($key))
1060                         return $string;
1061                 $buffer = $string;
1062                 $key = substr(md5($key),0,24);
1063             $iv = "password";
1064             return mcrypt_cbc(MCRYPT_3DES, $key, pack("H*", $buffer), MCRYPT_DECRYPT, $iv);
1065         }else{
1066                 return $string;
1067         }
1068 }
1069
1070 }
1071
1072 function canViewPath( $path, $base ){
1073   $path = realpath( $path );
1074   $base = realpath( $base );
1075   return 0 !== strncmp( $path, $base, strlen( $base ) );
1076 }
1077 /*END HELPER*/
1078
1079 ?>