Completed
Push — develop ( edae1e...431cf5 )
by Adam
20:48
created

aow_utils.php ➔ getModuleFields()   C

Complexity

Conditions 27
Paths 9

Size

Total Lines 74
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 27
eloc 47
nc 9
nop 4
dl 0
loc 74
rs 5.2581
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 *
4
 * SugarCRM Community Edition is a customer relationship management program developed by
5
 * SugarCRM, Inc. Copyright (C) 2004-2013 SugarCRM Inc.
6
 *
7
 * SuiteCRM is an extension to SugarCRM Community Edition developed by SalesAgility Ltd.
8
 * Copyright (C) 2011 - 2017 SalesAgility Ltd.
9
 *
10
 * This program is free software; you can redistribute it and/or modify it under
11
 * the terms of the GNU Affero General Public License version 3 as published by the
12
 * Free Software Foundation with the addition of the following permission added
13
 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
14
 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
15
 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
16
 *
17
 * This program is distributed in the hope that it will be useful, but WITHOUT
18
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19
 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
20
 * details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License along with
23
 * this program; if not, see http://www.gnu.org/licenses or write to the Free
24
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25
 * 02110-1301 USA.
26
 *
27
 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
28
 * SW2-130, Cupertino, CA 95014, USA. or at email address [email protected].
29
 *
30
 * The interactive user interfaces in modified source and object code versions
31
 * of this program must display Appropriate Legal Notices, as required under
32
 * Section 5 of the GNU Affero General Public License version 3.
33
 *
34
 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
35
 * these Appropriate Legal Notices must retain the display of the "Powered by
36
 * SugarCRM" logo and "Supercharged by SuiteCRM" logo. If the display of the logos is not
37
 * reasonably feasible for technical reasons, the Appropriate Legal Notices must
38
 * display the words "Powered by SugarCRM" and "Supercharged by SuiteCRM".
39
 */
40
41
if (!defined('sugarEntry') || !sugarEntry) {
42
    die('Not A Valid Entry Point');
43
}
44
45
function getModuleFields($module, $view='EditView', $value = '', $valid = array())
46
{
47
    global $app_strings, $beanList, $current_user;
48
49
50
    $blockedModuleFields = array(
51
        // module = array( ... fields )
52
        'Users' => array(
53
            'id',
54
            'is_admin',
55
            'name',
56
            'user_hash',
57
            'user_name',
58
            'system_generated_password',
59
            'pwd_last_changed',
60
            'authenticate_id',
61
            'sugar_login',
62
            'external_auth_only',
63
            'deleted',
64
            'is_group',
65
        )
66
    );
67
68
    $fields = array('' => $app_strings['LBL_NONE']);
69
    $unset = array();
70
71
    if ($module !== '') {
72
        if (isset($beanList[$module]) && $beanList[$module]) {
73
            $mod = new $beanList[$module]();
74
            foreach ($mod->field_defs as $name => $arr) {
75
                if (ACLController::checkAccess($mod->module_dir, 'list', true)) {
76
77
                    if (array_key_exists($mod->module_dir, $blockedModuleFields)) {
78
                        if (in_array($arr['name'],
79
                                $blockedModuleFields[$mod->module_dir]
80
                            ) && !$current_user->isAdmin()
81
                        ) {
82
                            $GLOBALS['log']->debug('hiding ' . $arr['name'] . ' field from ' . $current_user->name);
83
                            continue;
84
                        }
85
                    }
86
                    if ($arr['type'] != 'link' && ((!isset($arr['source']) || $arr['source'] != 'non-db') || ($arr['type'] == 'relate' && isset($arr['id_name']))) && (empty($valid) || in_array($arr['type'],
87
                                $valid)) && $name != 'currency_name' && $name != 'currency_symbol'
88
                    ) {
89
                        if (isset($arr['vname']) && $arr['vname'] !== '') {
90
                            $fields[$name] = rtrim(translate($arr['vname'], $mod->module_dir), ':');
91
                        } else {
92
                            $fields[$name] = $name;
93
                        }
94
                        if ($arr['type'] === 'relate' && isset($arr['id_name']) && $arr['id_name'] !== '') {
95
                            $unset[] = $arr['id_name'];
96
                        }
97
                    }
98
                }
99
            } //End loop.
100
101
            foreach ($unset as $name) {
102
                if (isset($fields[$name])) {
103
                    unset($fields[$name]);
104
                }
105
            }
106
107
        }
108
    }
109
    asort($fields);
110
    if($view == 'JSON'){
111
        return json_encode($fields);
112
    }
113
    if($view == 'EditView'){
114
        return get_select_options_with_id($fields, $value);
115
    } else {
116
        return $fields[$value];
117
    }
118
}
119
120
function getRelModuleFields($module, $rel_field, $view='EditView',$value = ''){
121
    global $beanList;
122
123
    if($module == $rel_field){
124
        return getModuleFields($module, $view, $value);
125
    }
126
127
    $mod = new $beanList[$module]();
128
    $data = $mod->field_defs[$rel_field];
129
130
    if(isset($data['module']) && $data['module'] != ''){
131
        return getModuleFields($data['module'], $view, $value);
132
    }
133
134
}
135
136
function getRelatedModule($module, $rel_field){
137
    global $beanList;
138
139
    if($module == $rel_field){
140
        return $module;
141
    }
142
143
    $mod = new $beanList[$module]();
144
145
    if(isset($arr['module']) && $arr['module'] != '') {
0 ignored issues
show
Bug introduced by
The variable $arr seems to never exist, and therefore isset should always return false. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
146
        return $arr['module'];
147
    } else if($mod->load_relationship($rel_field)){
148
        return $mod->$rel_field->getRelatedModuleName();
149
    }
150
151
    return $module;
152
153
}
154
155
function getModuleTreeData($module){
156
    global $beanList, $app_list_strings;
157
158
    $sort_fields = array();
159
    $module_label = isset($app_list_strings['moduleList'][$module]) ? $app_list_strings['moduleList'][$module] : $module;
160
    $fields = array(
161
        $module =>  array('label' => $module_label,
162
                        'type' => 'module',
163
                        'module' => $module,
164
                        'module_label'=> $module_label)
165
    );
166
167
    if ($module != '') {
168
        if(isset($beanList[$module]) && $beanList[$module]){
169
            $mod = new $beanList[$module]();
170
171
            foreach($mod->get_linked_fields() as $name => $arr){
172
                if(isset($arr['module']) && $arr['module'] != '') {
173
                    $rel_module = $arr['module'];
174
                } else if($mod->load_relationship($name)){
175
                    $rel_module = $mod->$name->getRelatedModuleName();
176
                }
177
178
                $rel_module_label = isset($app_list_strings['moduleList'][$rel_module]) ? $app_list_strings['moduleList'][$rel_module] : $rel_module;
179
                if(isset($arr['vname']) && $arr['vname'] != '') {
180
                    $label = $rel_module_label . ' : ' . translate($arr['vname'], $mod->module_dir);
181
                    $module_label = trim(translate($arr['vname'],$mod->module_dir),':');
182
                }else {
183
                    $label = $rel_module_label . ' : '. $name;
184
                    $module_label = $name;
185
                }
186
                $sort_fields[$name] = array('label'=>$label,'type'=>'relationship','module' => $rel_module,'module_label'=>$module_label);
187
                if($arr['type'] == 'relate' && isset($arr['id_name']) && $arr['id_name'] != ''){
188
                    if(isset($fields[$arr['id_name']])){
189
                        unset( $fields[$arr['id_name']]);
190
                    }
191
                }
192
            } //End loop.
193
            uasort($sort_fields,function($a,$b){
194
                return strcmp($a['label'],$b['label']);
195
            });
196
197
            $fields = array_merge((array)$fields, (array)$sort_fields);
198
        }
199
    }
200
201
    return json_encode($fields);
202
}
203
204
function getModuleRelationships($module, $view='EditView',$value = '')
205
{
206
    global $beanList, $app_list_strings;
207
208
    $fields = array($module=>$app_list_strings['moduleList'][$module]);
209
    $sort_fields = array();
210
    $invalid_modules = array();
211
212
    if ($module != '') {
213
        if(isset($beanList[$module]) && $beanList[$module]){
214
            $mod = new $beanList[$module]();
215
216
            /*if($mod->is_AuditEnabled()){
217
                $fields['Audit'] = translate('LBL_AUDIT_TABLE','AOR_Fields');
218
            }*/
219
            foreach($mod->get_linked_fields() as $name => $arr){
220
                if(isset($arr['module']) && $arr['module'] != '') {
221
                    $rel_module = $arr['module'];
222
                } else if($mod->load_relationship($name)){
223
                    $rel_module = $mod->$name->getRelatedModuleName();
224
                }
225
                if(!in_array($rel_module,$invalid_modules)){
226
                    $relModuleName = isset($app_list_strings['moduleList'][$rel_module]) ? $app_list_strings['moduleList'][$rel_module] : $rel_module;
227
                    if(isset($arr['vname']) && $arr['vname'] != ''){
228
                        $sort_fields[$name] = $relModuleName.' : '.translate($arr['vname'],$mod->module_dir);
229
                    } else {
230
                        $sort_fields[$name] = $relModuleName.' : '. $name;
231
                    }
232
                    if($arr['type'] == 'relate' && isset($arr['id_name']) && $arr['id_name'] != ''){
233
                        if(isset($fields[$arr['id_name']])) unset( $fields[$arr['id_name']]);
234
                    }
235
                }
236
            } //End loop.
237
            array_multisort($sort_fields, SORT_ASC, $sort_fields);
238
            $fields = array_merge((array)$fields, (array)$sort_fields);
239
        }
240
    }
241
    if($view == 'EditView'){
242
        return get_select_options_with_id($fields, $value);
243
    } else {
244
        return $fields[$value];
245
    }
246
}
247
248
function getValidFieldsTypes($module, $field){
249
    global $beanFiles, $beanList;
250
251
    require_once($beanFiles[$beanList[$module]]);
252
    $focus = new $beanList[$module];
253
    $vardef = $focus->getFieldDefinition($field);
254
255
    switch($vardef['type']) {
256
        case 'double':
257
        case 'decimal':
258
        case 'float':
259
        case 'currency':
260
            $valid_type = array('double','decimal','float','currency');
261
            break;
262
        case 'uint':
263
        case 'ulong':
264
        case 'long':
265
        case 'short':
266
        case 'tinyint':
267
        case 'int':
268
            $valid_type = array('uint','ulong','long','short','tinyint','int');
269
            break;
270
        case 'date':
271
        case 'datetime':
272
        case 'datetimecombo':
273
            $valid_type = array('date','datetime', 'datetimecombo');
274
            break;
275
        case 'id':
276
        case 'relate':
277
        case 'link':
278
            $valid_type = array('relate', 'id');
279
            //if($vardef['module'] == 'Users') $valid_type = array();
280
            break;
281
        default:
282
            $valid_type = array();
283
            break;
284
    }
285
286
    return $valid_type;
287
}
288
289
290
function getModuleField($module, $fieldname, $aow_field, $view='EditView',$value = '', $alt_type = '', $currency_id = '', $params= array()){
291
    global $current_language, $app_strings, $app_list_strings, $current_user, $beanFiles, $beanList;
292
293
    // use the mod_strings for this module
294
    $mod_strings = return_module_language($current_language,$module);
295
296
    // set the filename for this control
297
    $file = create_cache_directory('modules/AOW_WorkFlow/') . $module . $view . $alt_type . $fieldname . '.tpl';
298
299
    $displayParams = array();
300
301
    if ( !is_file($file)
302
        || inDeveloperMode()
303
        || !empty($_SESSION['developerMode']) ) {
304
305
        if ( !isset($vardef) ) {
0 ignored issues
show
Bug introduced by
The variable $vardef seems only to be defined at a later point. As such the call to isset() seems to always evaluate to false.

This check marks calls to isset(...) or empty(...) that are found before the variable itself is defined. These will always have the same result.

This is likely the result of code being shifted around. Consider removing these calls.

Loading history...
306
            require_once($beanFiles[$beanList[$module]]);
307
            $focus = new $beanList[$module];
308
            $vardef = $focus->getFieldDefinition($fieldname);
309
        }
310
311
        // Bug: check for AOR value SecurityGroups value missing
312
        if(stristr($fieldname, 'securitygroups') != false && empty($vardef)) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing stristr($fieldname, 'securitygroups') of type string to the boolean false. If you are specifically checking for a non-empty string, consider using the more explicit !== '' instead.
Loading history...
313
            require_once($beanFiles[$beanList['SecurityGroups']]);
314
            $module = 'SecurityGroups';
315
            $focus = new $beanList[$module];
316
            $vardef = $focus->getFieldDefinition($fieldname);
317
        }
318
319
320
        //$displayParams['formName'] = 'EditView';
321
322
        // if this is the id relation field, then don't have a pop-up selector.
323
        if( $vardef['type'] == 'relate' && $vardef['id_name'] == $vardef['name']) {
324
            $vardef['type'] = 'varchar';
325
        }
326
327
        if(isset($vardef['precision'])) unset($vardef['precision']);
328
329
        //$vardef['precision'] = $locale->getPrecedentPreference('default_currency_significant_digits', $current_user);
330
331
        //TODO Fix datetimecomebo
332
        //temp work around
333
        if( $vardef['type'] == 'datetimecombo') {
334
            $vardef['type'] = 'datetime';
335
        }
336
337
        // trim down textbox display
338
        if( $vardef['type'] == 'text' ) {
339
            $vardef['rows'] = 2;
340
            $vardef['cols'] = 32;
341
        }
342
343
        // create the dropdowns for the parent type fields
344
        if ( $vardef['type'] == 'parent_type' ) {
345
            $vardef['type'] = 'enum';
346
        }
347
348
        if($vardef['type'] == 'link'){
349
            $vardef['type'] = 'relate';
350
            $vardef['rname'] = 'name';
351
            $vardef['id_name'] = $vardef['name'].'_id';
352
            if((!isset($vardef['module']) || $vardef['module'] == '') && $focus->load_relationship($vardef['name'])) {
353
                $relName = $vardef['name'];
354
                $vardef['module'] = $focus->$relName->getRelatedModuleName();
355
            }
356
357
        }
358
359
        //check for $alt_type
360
        if ( $alt_type != '' ) {
361
            $vardef['type'] = $alt_type;
362
        }
363
364
        // remove the special text entry field function 'getEmailAddressWidget'
365
        if ( isset($vardef['function'])
366
            && ( $vardef['function'] == 'getEmailAddressWidget'
367
                || $vardef['function']['name'] == 'getEmailAddressWidget' ) )
368
            unset($vardef['function']);
369
370
        if(isset($vardef['name']) && ($vardef['name'] == 'date_entered' || $vardef['name'] == 'date_modified')){
371
            $vardef['name'] = 'aow_temp_date';
372
        }
373
374
        // load SugarFieldHandler to render the field tpl file
375
        static $sfh;
376
377
        if(!isset($sfh)) {
378
            require_once('include/SugarFields/SugarFieldHandler.php');
379
            $sfh = new SugarFieldHandler();
380
        }
381
382
        $contents = $sfh->displaySmarty('fields', $vardef, $view, $displayParams);
383
384
        // Remove all the copyright comments
385
        $contents = preg_replace('/\{\*[^\}]*?\*\}/', '', $contents);
386
387
        if( $view == 'EditView' &&  ($vardef['type'] == 'relate' || $vardef['type'] == 'parent')){
388
            $contents = str_replace('"'.$vardef['id_name'].'"','{/literal}"{$fields.'.$vardef['name'].'.id_name}"{literal}', $contents);
389
            $contents = str_replace('"'.$vardef['name'].'"','{/literal}"{$fields.'.$vardef['name'].'.name}"{literal}', $contents);
390
        }
391
392
        // hack to disable one of the js calls in this control
393
        if ( isset($vardef['function']) && ( $vardef['function'] == 'getCurrencyDropDown' || $vardef['function']['name'] == 'getCurrencyDropDown' ) )
394
            $contents .= "{literal}<script>function CurrencyConvertAll() { return; }</script>{/literal}";
395
396
        // Save it to the cache file
397
        if($fh = @sugar_fopen($file, 'w')) {
398
            fputs($fh, $contents);
399
            fclose($fh);
400
        }
401
    }
402
403
    // Now render the template we received
404
    $ss = new Sugar_Smarty();
405
406
    // Create Smarty variables for the Calendar picker widget
407
    global $timedate;
408
    $time_format = $timedate->get_user_time_format();
409
    $date_format = $timedate->get_cal_date_format();
410
    $ss->assign('USER_DATEFORMAT', $timedate->get_user_date_format());
411
    $ss->assign('TIME_FORMAT', $time_format);
412
    $time_separator = ":";
413
    $match = array();
414
    if(preg_match('/\d+([^\d])\d+([^\d]*)/s', $time_format, $match)) {
415
        $time_separator = $match[1];
416
    }
417
    $t23 = strpos($time_format, '23') !== false ? '%H' : '%I';
418
    if(!isset($match[2]) || $match[2] == '') {
419
        $ss->assign('CALENDAR_FORMAT', $date_format . ' ' . $t23 . $time_separator . "%M");
420
    }
421
    else {
422
        $pm = $match[2] == "pm" ? "%P" : "%p";
423
        $ss->assign('CALENDAR_FORMAT', $date_format . ' ' . $t23 . $time_separator . "%M" . $pm);
424
    }
425
426
    $ss->assign('CALENDAR_FDOW', $current_user->get_first_day_of_week());
427
428
    // populate the fieldlist from the vardefs
429
    $fieldlist = array();
430
    if ( !isset($focus) || !($focus instanceof SugarBean) )
431
        require_once($beanFiles[$beanList[$module]]);
432
    $focus = new $beanList[$module];
433
    // create the dropdowns for the parent type fields
434
    $vardefFields = $focus->getFieldDefinitions();
435
    if (isset($vardefFields[$fieldname]['type']) && $vardefFields[$fieldname]['type'] == 'parent_type' ) {
436
        $focus->field_defs[$fieldname]['options'] = $focus->field_defs[$vardefFields[$fieldname]['group']]['options'];
437
    }
438
    foreach ( $vardefFields as $name => $properties ) {
439
        $fieldlist[$name] = $properties;
440
        // fill in enums
441
        if(isset($fieldlist[$name]['options']) && is_string($fieldlist[$name]['options']) && isset($app_list_strings[$fieldlist[$name]['options']]))
442
            $fieldlist[$name]['options'] = $app_list_strings[$fieldlist[$name]['options']];
443
        // Bug 32626: fall back on checking the mod_strings if not in the app_list_strings
444
        elseif(isset($fieldlist[$name]['options']) && is_string($fieldlist[$name]['options']) && isset($mod_strings[$fieldlist[$name]['options']]))
445
            $fieldlist[$name]['options'] = $mod_strings[$fieldlist[$name]['options']];
446
        // Bug 22730: make sure all enums have the ability to select blank as the default value.
447
        if(!isset($fieldlist[$name]['options']['']))
448
            $fieldlist[$name]['options'][''] = '';
449
    }
450
451
    // fill in function return values
452
    if ( !in_array($fieldname,array('email1','email2')) )
453
    {
454
        if (!empty($fieldlist[$fieldname]['function']['returns']) && $fieldlist[$fieldname]['function']['returns'] == 'html')
455
        {
456
            $function = $fieldlist[$fieldname]['function']['name'];
457
            // include various functions required in the various vardefs
458
            if ( isset($fieldlist[$fieldname]['function']['include']) && is_file($fieldlist[$fieldname]['function']['include']))
459
                require_once($fieldlist[$fieldname]['function']['include']);
460
            $_REQUEST[$fieldname] = $value;
461
            $value = $function($focus, $fieldname, $value, $view);
462
463
            $value = str_ireplace($fieldname, $aow_field, $value);
464
        }
465
    }
466
467
    if(isset($fieldlist[$fieldname]['type']) && $fieldlist[$fieldname]['type'] == 'link'){
468
        $fieldlist[$fieldname]['id_name'] = $fieldlist[$fieldname]['name'].'_id';
469
470
        if((!isset($fieldlist[$fieldname]['module']) || $fieldlist[$fieldname]['module'] == '') && $focus->load_relationship($fieldlist[$fieldname]['name'])) {
471
            $relName = $fieldlist[$fieldname]['name'];
472
            $fieldlist[$fieldname]['module'] = $focus->$relName->getRelatedModuleName();
473
        }
474
    }
475
476
    if(isset($fieldlist[$fieldname]['name']) && ($fieldlist[$fieldname]['name'] == 'date_entered' || $fieldlist[$fieldname]['name'] == 'date_modified')){
477
        $fieldlist[$fieldname]['name'] = 'aow_temp_date';
478
        $fieldlist['aow_temp_date'] = $fieldlist[$fieldname];
479
        $fieldname = 'aow_temp_date';
480
    }
481
482
    $quicksearch_js = '';
483
    if(isset( $fieldlist[$fieldname]['id_name'] ) && $fieldlist[$fieldname]['id_name'] != '' && $fieldlist[$fieldname]['id_name'] != $fieldlist[$fieldname]['name']){
484
        $rel_value = $value;
485
486
        require_once("include/TemplateHandler/TemplateHandler.php");
487
        $template_handler = new TemplateHandler();
488
        $quicksearch_js = $template_handler->createQuickSearchCode($fieldlist,$fieldlist,$view);
489
        $quicksearch_js = str_replace($fieldname, $aow_field.'_display', $quicksearch_js);
490
        $quicksearch_js = str_replace($fieldlist[$fieldname]['id_name'], $aow_field, $quicksearch_js);
491
492
        echo $quicksearch_js;
493
494
        if(isset($fieldlist[$fieldname]['module']) && $fieldlist[$fieldname]['module'] == 'Users'){
495
            $rel_value = get_assigned_user_name($value);
496
        } else if(isset($fieldlist[$fieldname]['module'])){
497
            require_once($beanFiles[$beanList[$fieldlist[$fieldname]['module']]]);
498
            $rel_focus = new $beanList[$fieldlist[$fieldname]['module']];
499
            $rel_focus->retrieve($value);
500
            if(isset($fieldlist[$fieldname]['rname']) && $fieldlist[$fieldname]['rname'] != ''){
501
                $relDisplayField = $fieldlist[$fieldname]['rname'];
502
            } else {
503
                $relDisplayField = 'name';
504
            }
505
            $rel_value = $rel_focus->$relDisplayField;
506
        }
507
508
        $fieldlist[$fieldlist[$fieldname]['id_name']]['value'] = $value;
509
        $fieldlist[$fieldname]['value'] = $rel_value;
510
        $fieldlist[$fieldname]['id_name'] = $aow_field;
511
        $fieldlist[$fieldlist[$fieldname]['id_name']]['name'] = $aow_field;
512
        $fieldlist[$fieldname]['name'] = $aow_field.'_display';
513
    } else if(isset( $fieldlist[$fieldname]['type'] ) && $view == 'DetailView' && ($fieldlist[$fieldname]['type'] == 'datetimecombo' || $fieldlist[$fieldname]['type'] == 'datetime' || $fieldlist[$fieldname]['type'] == 'date')){
514
        $value = $focus->convertField($value, $fieldlist[$fieldname]);
515
        if(!empty($params['date_format']) && isset($params['date_format'])){
516
            $convert_format = "Y-m-d H:i:s";
517
            if($fieldlist[$fieldname]['type'] == 'date') $convert_format = "Y-m-d";
518
            $fieldlist[$fieldname]['value'] = $timedate->to_display($value, $convert_format, $params['date_format']);
519
        }else{
520
            $fieldlist[$fieldname]['value'] = $timedate->to_display_date_time($value, true, true);
521
        }
522
        $fieldlist[$fieldname]['name'] = $aow_field;
523
    } else if(isset( $fieldlist[$fieldname]['type'] ) && ($fieldlist[$fieldname]['type'] == 'datetimecombo' || $fieldlist[$fieldname]['type'] == 'datetime' || $fieldlist[$fieldname]['type'] == 'date')){
524
        $value = $focus->convertField($value, $fieldlist[$fieldname]);
525
        $fieldlist[$fieldname]['value'] = $timedate->to_display_date($value);
526
        //$fieldlist[$fieldname]['value'] = $timedate->to_display_date_time($value, true, true);
527
        //$fieldlist[$fieldname]['value'] = $value;
528
        $fieldlist[$fieldname]['name'] = $aow_field;
529
    } else {
530
        $fieldlist[$fieldname]['value'] = $value;
531
        $fieldlist[$fieldname]['name'] = $aow_field;
532
533
    }
534
535
    if(isset($fieldlist[$fieldname]['type']) && $fieldlist[$fieldname]['type'] == 'currency' && $view != 'EditView'){
536
        static $sfh;
537
538
        if(!isset($sfh)) {
539
            require_once('include/SugarFields/SugarFieldHandler.php');
540
            $sfh = new SugarFieldHandler();
541
        }
542
543
        if($currency_id != '' && !stripos($fieldname, '_USD')){
544
            $userCurrencyId = $current_user->getPreference('currency');
545
            if($currency_id != $userCurrencyId){
546
                $currency = new Currency();
547
                $currency->retrieve($currency_id);
548
                $value = $currency->convertToDollar($value);
549
                $currency->retrieve($userCurrencyId);
550
                $value = $currency->convertFromDollar($value);
551
            }
552
        }
553
554
        $parentfieldlist[strtoupper($fieldname)] = $value;
555
556
        return($sfh->displaySmarty($parentfieldlist, $fieldlist[$fieldname], 'ListView', $displayParams));
557
    }
558
559
    $ss->assign("QS_JS", $quicksearch_js);
560
    $ss->assign("fields",$fieldlist);
561
    $ss->assign("form_name",$view);
562
    $ss->assign("bean",$focus);
563
564
    // add in any additional strings
565
    $ss->assign("MOD", $mod_strings);
566
    $ss->assign("APP", $app_strings);
567
568
    //$return = str_replace($fieldname,$ss->fetch($file));
569
570
    return $ss->fetch($file);
571
}
572
573
574
575
function getDateField($module, $aow_field, $view, $value, $field_option = true){
576
    global $app_list_strings;
577
578
    // set $view = 'EditView' as default
579
    if (!$view) {
580
        $view = 'EditView';
581
    }
582
583
    $value = json_decode(html_entity_decode_utf8($value), true);
584
585
    if(!file_exists('modules/AOBH_BusinessHours/AOBH_BusinessHours.php')) unset($app_list_strings['aow_date_type_list']['business_hours']);
586
587
    $field = '';
588
589
    if($view == 'EditView'){
590
        $field .= "<select type='text' name='$aow_field".'[0]'."' id='$aow_field".'[0]'."' title='' tabindex='116'>". getDateFields($module, $view, $value[0], $field_option) ."</select>&nbsp;&nbsp;";
591
        $field .= "<select type='text' name='$aow_field".'[1]'."' id='$aow_field".'[1]'."' onchange='date_field_change(\"$aow_field\")'  title='' tabindex='116'>". get_select_options_with_id($app_list_strings['aow_date_operator'], $value[1]) ."</select>&nbsp;";
592
        $display = 'none';
593
        if($value[1] == 'plus' || $value[1] == 'minus') $display = '';
594
        $field .= "<input  type='text' style='display:$display' name='$aow_field".'[2]'."' id='$aow_field".'[2]'."' title='' value='$value[2]' tabindex='116'>&nbsp;";
595
        $field .= "<select type='text' style='display:$display' name='$aow_field".'[3]'."' id='$aow_field".'[3]'."' title='' tabindex='116'>". get_select_options_with_id($app_list_strings['aow_date_type_list'], $value[3]) ."</select>";
596
    }
597
    else {
598
        $field = getDateFields($module, $view, $value[0], $field_option).' '.$app_list_strings['aow_date_operator'][$value[1]];
599
        if($value[1] == 'plus' || $value[1] == 'minus'){
600
            $field .= ' '.$value[2].' '.$app_list_strings['aow_date_type_list'][$value[3]];
601
        }
602
    }
603
    return $field;
604
605
}
606
607
function getDateFields($module, $view='EditView',$value = '', $field_option = true)
608
{
609
    global $beanList, $app_list_strings;
610
611
    $fields = $app_list_strings['aow_date_options'];
612
613
    if(!$field_option) unset($fields['field']);
614
615
    if ($module != '') {
616
        if(isset($beanList[$module]) && $beanList[$module]){
617
            $mod = new $beanList[$module]();
618
            foreach($mod->field_defs as $name => $arr){
619
                if($arr['type'] == 'date' || $arr['type'] == 'datetime' || $arr['type'] == 'datetimecombo'){
620
                    if(isset($arr['vname']) && $arr['vname'] != ''){
621
                        $fields[$name] = translate($arr['vname'],$mod->module_dir);
622
                    } else {
623
                        $fields[$name] = $name;
624
                    }
625
                }
626
            } //End loop.
627
628
        }
629
    }
630
    if($view == 'EditView'){
631
        return get_select_options_with_id($fields, $value);
632
    } else {
633
        return $fields[$value];
634
    }
635
}
636
637
function getAssignField($aow_field, $view, $value){
638
    global $app_list_strings;
639
640
    $value = json_decode(html_entity_decode_utf8($value), true);
641
642
    $roles = get_bean_select_array(true, 'ACLRole','name', '','name',true);
643
644
    if(!file_exists('modules/SecurityGroups/SecurityGroup.php')){
645
        unset($app_list_strings['aow_assign_options']['security_group']);
646
    }
647
    else{
648
        $securityGroups = get_bean_select_array(true, 'SecurityGroup','name', '','name',true);
649
    }
650
651
    $field = '';
652
653
    if($view == 'EditView'){
654
        $field .= "<select type='text' name='$aow_field".'[0]'."' id='$aow_field".'[0]'."' onchange='assign_field_change(\"$aow_field\")' title='' tabindex='116'>". get_select_options_with_id($app_list_strings['aow_assign_options'], $value[0]) ."</select>&nbsp;&nbsp;";
655
        if(!file_exists('modules/SecurityGroups/SecurityGroup.php')){
656
            $field .= "<input type='hidden' name='$aow_field".'[1]'."' id='$aow_field".'[1]'."' value=''  />";
657
        }
658
        else {
659
            $display = 'none';
660
            if($value[0] == 'security_group') $display = '';
661
            $field .= "<select type='text' style='display:$display' name='$aow_field".'[1]'."' id='$aow_field".'[1]'."' title='' tabindex='116'>". get_select_options_with_id($securityGroups, $value[1]) ."</select>&nbsp;&nbsp;";
662
        }
663
        $display = 'none';
664
        if($value[0] == 'role' || $value[0] == 'security_group') $display = '';
665
        $field .= "<select type='text' style='display:$display' name='$aow_field".'[2]'."' id='$aow_field".'[2]'."' title='' tabindex='116'>". get_select_options_with_id($roles, $value[2]) ."</select>&nbsp;&nbsp;";
666
    }
667
    else {
668
        $field = $app_list_strings['aow_assign_options'][$value[1]];
669
    }
670
    return $field;
671
672
}
673
674
function getDropdownList($list_id, $selected_value) {
675
    global $app_list_strings;
676
    $option = '';
677
    foreach($app_list_strings[$list_id] as $key => $value) {
678
        if(base64_decode($selected_value) == $key) {
679
            $option .= '<option value="'.$key.'" selected>'.$value.'</option>';
680
        } else if($selected_value == $key) {
681
            $option .= '<option value="'.$key.'" selected>'.$value.'</option>';
682
        }
683
        else {
684
            $option .= '<option value="'.$key.'">'.$value.'</option>';
685
        }
686
    }
687
    return $option;
688
}
689
function getLeastBusyUser($users, $field, SugarBean $bean) {
690
    $counts = array();
691
    foreach($users as $id) {
692
        $c = $bean->db->getOne("SELECT count(*) AS c FROM ".$bean->table_name." WHERE $field = '$id' AND deleted = 0");
693
        $counts[$id] = $c;
694
    }
695
    asort($counts);
696
    $countsKeys = array_flip($counts);
697
    return array_shift($countsKeys);
698
}
699
700
function getRoundRobinUser($users, $id) {
701
702
    $file = create_cache_directory('modules/AOW_WorkFlow/Users/') . $id . 'lastUser.cache.php';
703
704
    if(isset($_SESSION['lastuser'][$id]) && $_SESSION['lastuser'][$id] != '') {
705
        $users_by_key = array_flip($users); // now keys are values
706
        $key = $users_by_key[$_SESSION['lastuser'][$id]] + 1;
707
        if(!empty($users[$key])) {
708
            return $users[$key];
709
        }
710
    }
711
    else if (is_file($file)){
712
        require_once($file);
713
        if(isset($lastUser['User']) && $lastUser['User'] != '') {
0 ignored issues
show
Bug introduced by
The variable $lastUser seems to never exist, and therefore isset should always return false. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
714
            $users_by_key = array_flip($users); // now keys are values
715
            $key = $users_by_key[$lastUser['User']] + 1;
716
            if(!empty($users[$key])) {
717
                return $users[$key];
718
            }
719
        }
720
    }
721
722
   return $users[0];
723
}
724
725
function setLastUser($user_id, $id) {
726
727
    $_SESSION['lastuser'][$id] = $user_id;
728
729
    $file = create_cache_directory('modules/AOW_WorkFlow/Users/') . $id . 'lastUser.cache.php';
730
731
    $arrayString = var_export_helper(array('User' => $user_id));
732
733
    $content =<<<eoq
734
<?php
735
	\$lastUser = {$arrayString};
736
?>
737
eoq;
738
739
    if($fh = @sugar_fopen($file, 'w')) {
740
        fputs($fh, $content);
741
        fclose($fh);
742
    }
743
    return true;
744
}
745
746
function getEmailableModules(){
747
    global $beanFiles, $beanList, $app_list_strings;
748
    $emailableModules = array();
749
    foreach($app_list_strings['aow_moduleList'] as $bean_name => $bean_dis) {
750
        if(isset($beanList[$bean_name]) && isset($beanFiles[$beanList[$bean_name]])){
751
            require_once($beanFiles[$beanList[$bean_name]]);
752
            $obj = new $beanList[$bean_name];
753
            if($obj instanceof Person || $obj instanceof Company){
754
                $emailableModules[] = $bean_name;
755
            }
756
        }
757
    }
758
    asort($emailableModules);
759
    return $emailableModules;
760
}
761
762
function getRelatedEmailableFields($module){
763
    global $beanList, $app_list_strings;
764
    $relEmailFields = array();
765
    $checked_link = array();
766
    $emailableModules = getEmailableModules();
767
    if ($module != '') {
768
        if(isset($beanList[$module]) && $beanList[$module]){
769
            $mod = new $beanList[$module]();
770
771
            foreach($mod->get_related_fields() as $field){
772
                if(isset($field['link'])) $checked_link[] = $field['link'];
773
                if(!isset($field['module']) || !in_array($field['module'],$emailableModules) || (isset($field['dbType']) && $field['dbType'] == "id")){
774
                    continue;
775
                }
776
                $relEmailFields[$field['name']] = $field['module'].": ".trim(translate($field['vname'],$mod->module_name),":");
777
            }
778
779
            foreach($mod->get_linked_fields() as $field){
780
                if(!in_array($field['name'],$checked_link) && !in_array($field['relationship'],$checked_link)){
781
                    if(isset($field['module']) && $field['module'] != '') {
782
                        $rel_module = $field['module'];
783
                    } else if($mod->load_relationship($field['name'])){
784
                        $relField = $field['name'];
785
                        $rel_module = $mod->$relField->getRelatedModuleName();
786
                    }
787
788
                    if(in_array($rel_module,$emailableModules)) {
789
                        if (isset($field['vname']) && $field['vname'] != '') {
790
                            $relEmailFields[$field['name']] = $app_list_strings['moduleList'][$rel_module] . ' : ' . translate($field['vname'], $mod->module_dir);
791
                        } else {
792
                            $relEmailFields[$field['name']] = $app_list_strings['moduleList'][$rel_module] . ' : ' . $field['name'];
793
                        }
794
                    }
795
                }
796
            }
797
798
            array_multisort($relEmailFields, SORT_ASC, $relEmailFields);
799
        }
800
    }
801
    return $relEmailFields;
802
}
803
804
function fixUpFormatting($module, $field, $value)
805
{
806
    global $timedate, $beanFiles, $beanList;
807
808
    require_once($beanFiles[$beanList[$module]]);
809
    $bean = new $beanList[$module];
810
    
811
    static $boolean_false_values = array('off', 'false', '0', 'no');
812
813
    switch($bean->field_defs[$field]['type']) {
814
        case 'datetime':
815
        case 'datetimecombo':
816
            if(empty($value)) break;
817
            if ($value == 'NULL') {
818
                $value = '';
819
                break;
820
            }
821
            if ( ! preg_match('/^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}$/',$value) ) {
822
                // This appears to be formatted in user date/time
823
                $value = $timedate->to_db($value);
824
            }
825
            break;
826
        case 'date':
827
            if(empty($value)) break;
828
            if ($value == 'NULL') {
829
                $value = '';
830
                break;
831
            }
832
            if ( ! preg_match('/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/',$value) ) {
833
                // This date appears to be formatted in the user's format
834
                $value = $timedate->to_db_date($value, false);
835
            }
836
            break;
837
        case 'time':
838
            if(empty($value)) break;
839
            if ($value == 'NULL') {
840
                $value = '';
841
                break;
842
            }
843
            if ( preg_match('/(am|pm)/i',$value) ) {
844
                // This time appears to be formatted in the user's format
845
                $value = $timedate->fromUserTime($value)->format(TimeDate::DB_TIME_FORMAT);
846
            }
847
            break;
848
        case 'double':
849
        case 'decimal':
850
        case 'currency':
851
        case 'float':
852
            if ( $value === '' || $value == NULL || $value == 'NULL') {
853
                continue;
854
            }
855
            if ( is_string($value) ) {
856
                $value = (float)unformat_number($value);
857
            }
858
            break;
859
        case 'uint':
860
        case 'ulong':
861
        case 'long':
862
        case 'short':
863
        case 'tinyint':
864
        case 'int':
865
            if ( $value === '' || $value == NULL || $value == 'NULL') {
866
                continue;
867
            }
868
            if ( is_string($value) ) {
869
                $value = (int)unformat_number($value);
870
            }
871
            break;
872
        case 'bool':
873
            if (empty($value)) {
874
                $value = false;
875
            } else if(true === $value || 1 == $value) {
876
                $value = true;
877
            } else if(in_array(strval($value), $boolean_false_values)) {
878
                $value = false;
879
            } else {
880
                $value = true;
881
            }
882
            break;
883
        case 'encrypt':
884
            $value = $this->encrpyt_before_save($value);
885
            break;
886
    }
887
    return $value;
888
889
}
890