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