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