Completed
Push — console-installer ( e2b50d...6ce748 )
by Adam
22:30
created

aow_utils.php ➔ getModuleField()   F

Complexity

Conditions 89
Paths > 20000

Size

Total Lines 282
Code Lines 168

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 89
eloc 168
nc 4294967295
nop 8
dl 0
loc 282
rs 2
c 0
b 0
f 0

How to fix   Long Method    Complexity    Many Parameters   

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:

Many Parameters

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

There are several approaches to avoid long parameter lists:

1
<?php
2
/**
3
 * Advanced OpenWorkflow, Automating SugarCRM.
4
 * @package Advanced OpenWorkflow for SugarCRM
5
 * @copyright SalesAgility Ltd http://www.salesagility.com
6
 *
7
 * This program is free software; you can redistribute it and/or modify
8
 * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
9
 * the Free Software Foundation; either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
18
 * along with this program; if not, see http://www.gnu.org/licenses
19
 * or write to the Free Software Foundation,Inc., 51 Franklin Street,
20
 * Fifth Floor, Boston, MA 02110-1301  USA
21
 *
22
 * @author SalesAgility <[email protected]>
23
 */
24
25
26
function getModuleFields($module, $view='EditView',$value = '', $valid = array())
27
{
28
    global $app_strings, $beanList;
29
30
    $fields = array(''=>$app_strings['LBL_NONE']);
31
    $unset = array();
32
33
    if ($module != '') {
34
        if(isset($beanList[$module]) && $beanList[$module]){
35
            $mod = new $beanList[$module]();
36
            foreach($mod->field_defs as $name => $arr){
37
                if($arr['type'] != 'link' &&((!isset($arr['source']) || $arr['source'] != 'non-db') || ($arr['type'] == 'relate' && isset($arr['id_name']))) && (empty($valid) || in_array($arr['type'], $valid)) && $name != 'currency_name' && $name != 'currency_symbol'){
38
                    if(isset($arr['vname']) && $arr['vname'] != ''){
39
                        $fields[$name] = rtrim(translate($arr['vname'],$mod->module_dir), ':');
40
                    } else {
41
                        $fields[$name] = $name;
42
                    }
43
                    if($arr['type'] == 'relate' && isset($arr['id_name']) && $arr['id_name'] != ''){
44
                        $unset[] = $arr['id_name'];
45
                    }
46
                }
47
            } //End loop.
48
49
            foreach($unset as $name){
50
                if(isset($fields[$name])) unset( $fields[$name]);
51
            }
52
53
        }
54
    }
55
    if($view == 'JSON'){
56
        return json_encode($fields);
57
    }
58
    if($view == 'EditView'){
59
        return get_select_options_with_id($fields, $value);
60
    } else {
61
        return $fields[$value];
62
    }
63
}
64
65
function getRelModuleFields($module, $rel_field, $view='EditView',$value = ''){
66
    global $beanList;
67
68
    if($module == $rel_field){
69
        return getModuleFields($module, $view, $value);
70
    }
71
72
    $mod = new $beanList[$module]();
73
    $data = $mod->field_defs[$rel_field];
74
75
    if(isset($data['module']) && $data['module'] != ''){
76
        return getModuleFields($data['module'], $view, $value);
77
    }
78
79
}
80
81
function getRelatedModule($module, $rel_field){
82
    global $beanList;
83
84
    if($module == $rel_field){
85
        return $module;
86
    }
87
88
    $mod = new $beanList[$module]();
89
90
    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...
91
        return $arr['module'];
92
    } else if($mod->load_relationship($rel_field)){
93
        return $mod->$rel_field->getRelatedModuleName();
94
    }
95
96
    return $module;
97
98
}
99
100
function getModuleTreeData($module){
101
    global $beanList, $app_list_strings;
102
103
    $sort_fields = array();
104
    $module_label = isset($app_list_strings['moduleList'][$module]) ? $app_list_strings['moduleList'][$module] : $module;
105
    $fields = array(
106
        $module =>  array('label' => $module_label,
107
                        'type' => 'module',
108
                        'module' => $module,
109
                        'module_label'=> $module_label)
110
    );
111
112
    if ($module != '') {
113
        if(isset($beanList[$module]) && $beanList[$module]){
114
            $mod = new $beanList[$module]();
115
116
            foreach($mod->get_linked_fields() as $name => $arr){
117
                if(isset($arr['module']) && $arr['module'] != '') {
118
                    $rel_module = $arr['module'];
119
                } else if($mod->load_relationship($name)){
120
                    $rel_module = $mod->$name->getRelatedModuleName();
121
                }
122
123
                $rel_module_label = isset($app_list_strings['moduleList'][$rel_module]) ? $app_list_strings['moduleList'][$rel_module] : $rel_module;
124
                if(isset($arr['vname']) && $arr['vname'] != '') {
125
                    $label = $rel_module_label . ' : ' . translate($arr['vname'], $mod->module_dir);
126
                    $module_label = trim(translate($arr['vname'],$mod->module_dir),':');
127
                }else {
128
                    $label = $rel_module_label . ' : '. $name;
129
                    $module_label = $name;
130
                }
131
                $sort_fields[$name] = array('label'=>$label,'type'=>'relationship','module' => $rel_module,'module_label'=>$module_label);
132
                if($arr['type'] == 'relate' && isset($arr['id_name']) && $arr['id_name'] != ''){
133
                    if(isset($fields[$arr['id_name']])){
134
                        unset( $fields[$arr['id_name']]);
135
                    }
136
                }
137
            } //End loop.
138
            uasort($sort_fields,function($a,$b){
139
                return strcmp($a['label'],$b['label']);
140
            });
141
142
            $fields = array_merge((array)$fields, (array)$sort_fields);
143
        }
144
    }
145
146
    return json_encode($fields);
147
}
148
149
function getModuleRelationships($module, $view='EditView',$value = '')
150
{
151
    global $beanList, $app_list_strings;
152
153
    $fields = array($module=>$app_list_strings['moduleList'][$module]);
154
    $sort_fields = array();
155
    $invalid_modules = array();
156
157
    if ($module != '') {
158
        if(isset($beanList[$module]) && $beanList[$module]){
159
            $mod = new $beanList[$module]();
160
161
            /*if($mod->is_AuditEnabled()){
162
                $fields['Audit'] = translate('LBL_AUDIT_TABLE','AOR_Fields');
163
            }*/
164
            foreach($mod->get_linked_fields() as $name => $arr){
165
                if(isset($arr['module']) && $arr['module'] != '') {
166
                    $rel_module = $arr['module'];
167
                } else if($mod->load_relationship($name)){
168
                    $rel_module = $mod->$name->getRelatedModuleName();
169
                }
170
                if(!in_array($rel_module,$invalid_modules)){
171
                    $relModuleName = isset($app_list_strings['moduleList'][$rel_module]) ? $app_list_strings['moduleList'][$rel_module] : $rel_module;
172
                    if(isset($arr['vname']) && $arr['vname'] != ''){
173
                        $sort_fields[$name] = $relModuleName.' : '.translate($arr['vname'],$mod->module_dir);
174
                    } else {
175
                        $sort_fields[$name] = $relModuleName.' : '. $name;
176
                    }
177
                    if($arr['type'] == 'relate' && isset($arr['id_name']) && $arr['id_name'] != ''){
178
                        if(isset($fields[$arr['id_name']])) unset( $fields[$arr['id_name']]);
179
                    }
180
                }
181
            } //End loop.
182
            array_multisort($sort_fields, SORT_ASC, $sort_fields);
183
            $fields = array_merge((array)$fields, (array)$sort_fields);
184
        }
185
    }
186
    if($view == 'EditView'){
187
        return get_select_options_with_id($fields, $value);
188
    } else {
189
        return $fields[$value];
190
    }
191
}
192
193
function getValidFieldsTypes($module, $field){
194
    global $beanFiles, $beanList;
195
196
    require_once($beanFiles[$beanList[$module]]);
197
    $focus = new $beanList[$module];
198
    $vardef = $focus->getFieldDefinition($field);
199
200
    switch($vardef['type']) {
201
        case 'double':
202
        case 'decimal':
203
        case 'float':
204
        case 'currency':
205
            $valid_type = array('double','decimal','float','currency');
206
            break;
207
        case 'uint':
208
        case 'ulong':
209
        case 'long':
210
        case 'short':
211
        case 'tinyint':
212
        case 'int':
213
            $valid_type = array('uint','ulong','long','short','tinyint','int');
214
            break;
215
        case 'date':
216
        case 'datetime':
217
        case 'datetimecombo':
218
            $valid_type = array('date','datetime', 'datetimecombo');
219
            break;
220
        case 'id':
221
        case 'relate':
222
        case 'link':
223
            $valid_type = array('relate', 'id');
224
            //if($vardef['module'] == 'Users') $valid_type = array();
225
            break;
226
        default:
227
            $valid_type = array();
228
            break;
229
    }
230
231
    return $valid_type;
232
}
233
234
235
function getModuleField($module, $fieldname, $aow_field, $view='EditView',$value = '', $alt_type = '', $currency_id = '', $params= array()){
236
    global $current_language, $app_strings, $app_list_strings, $current_user, $beanFiles, $beanList;
237
238
    // use the mod_strings for this module
239
    $mod_strings = return_module_language($current_language,$module);
240
241
    // set the filename for this control
242
    $file = create_cache_directory('modules/AOW_WorkFlow/') . $module . $view . $alt_type . $fieldname . '.tpl';
243
244
    $displayParams = array();
245
246
    if ( !is_file($file)
247
        || inDeveloperMode()
248
        || !empty($_SESSION['developerMode']) ) {
249
250
        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...
251
            require_once($beanFiles[$beanList[$module]]);
252
            $focus = new $beanList[$module];
253
            $vardef = $focus->getFieldDefinition($fieldname);
254
        }
255
256
        // Bug: check for AOR value SecurityGroups value missing
257
        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...
258
            require_once($beanFiles[$beanList['SecurityGroups']]);
259
            $module = 'SecurityGroups';
260
            $focus = new $beanList[$module];
261
            $vardef = $focus->getFieldDefinition($fieldname);
262
        }
263
264
265
        //$displayParams['formName'] = 'EditView';
266
267
        // if this is the id relation field, then don't have a pop-up selector.
268
        if( $vardef['type'] == 'relate' && $vardef['id_name'] == $vardef['name']) {
269
            $vardef['type'] = 'varchar';
270
        }
271
272
        if(isset($vardef['precision'])) unset($vardef['precision']);
273
274
        //$vardef['precision'] = $locale->getPrecedentPreference('default_currency_significant_digits', $current_user);
275
276
        //TODO Fix datetimecomebo
277
        //temp work around
278
        if( $vardef['type'] == 'datetimecombo') {
279
            $vardef['type'] = 'datetime';
280
        }
281
282
        // trim down textbox display
283
        if( $vardef['type'] == 'text' ) {
284
            $vardef['rows'] = 2;
285
            $vardef['cols'] = 32;
286
        }
287
288
        // create the dropdowns for the parent type fields
289
        if ( $vardef['type'] == 'parent_type' ) {
290
            $vardef['type'] = 'enum';
291
        }
292
293
        if($vardef['type'] == 'link'){
294
            $vardef['type'] = 'relate';
295
            $vardef['rname'] = 'name';
296
            $vardef['id_name'] = $vardef['name'].'_id';
297
            if((!isset($vardef['module']) || $vardef['module'] == '') && $focus->load_relationship($vardef['name'])) {
298
                $relName = $vardef['name'];
299
                $vardef['module'] = $focus->$relName->getRelatedModuleName();
300
            }
301
302
        }
303
304
        //check for $alt_type
305
        if ( $alt_type != '' ) {
306
            $vardef['type'] = $alt_type;
307
        }
308
309
        // remove the special text entry field function 'getEmailAddressWidget'
310
        if ( isset($vardef['function'])
311
            && ( $vardef['function'] == 'getEmailAddressWidget'
312
                || $vardef['function']['name'] == 'getEmailAddressWidget' ) )
313
            unset($vardef['function']);
314
315
        if(isset($vardef['name']) && ($vardef['name'] == 'date_entered' || $vardef['name'] == 'date_modified')){
316
            $vardef['name'] = 'aow_temp_date';
317
        }
318
319
        // load SugarFieldHandler to render the field tpl file
320
        static $sfh;
321
322
        if(!isset($sfh)) {
323
            require_once('include/SugarFields/SugarFieldHandler.php');
324
            $sfh = new SugarFieldHandler();
325
        }
326
327
        $contents = $sfh->displaySmarty('fields', $vardef, $view, $displayParams);
328
329
        // Remove all the copyright comments
330
        $contents = preg_replace('/\{\*[^\}]*?\*\}/', '', $contents);
331
332
        if( $view == 'EditView' &&  ($vardef['type'] == 'relate' || $vardef['type'] == 'parent')){
333
            $contents = str_replace('"'.$vardef['id_name'].'"','{/literal}"{$fields.'.$vardef['name'].'.id_name}"{literal}', $contents);
334
            $contents = str_replace('"'.$vardef['name'].'"','{/literal}"{$fields.'.$vardef['name'].'.name}"{literal}', $contents);
335
        }
336
337
        // hack to disable one of the js calls in this control
338
        if ( isset($vardef['function']) && ( $vardef['function'] == 'getCurrencyDropDown' || $vardef['function']['name'] == 'getCurrencyDropDown' ) )
339
            $contents .= "{literal}<script>function CurrencyConvertAll() { return; }</script>{/literal}";
340
341
        // Save it to the cache file
342
        if($fh = @sugar_fopen($file, 'w')) {
343
            fputs($fh, $contents);
344
            fclose($fh);
345
        }
346
    }
347
348
    // Now render the template we received
349
    $ss = new Sugar_Smarty();
350
351
    // Create Smarty variables for the Calendar picker widget
352
    global $timedate;
353
    $time_format = $timedate->get_user_time_format();
354
    $date_format = $timedate->get_cal_date_format();
355
    $ss->assign('USER_DATEFORMAT', $timedate->get_user_date_format());
356
    $ss->assign('TIME_FORMAT', $time_format);
357
    $time_separator = ":";
358
    $match = array();
359
    if(preg_match('/\d+([^\d])\d+([^\d]*)/s', $time_format, $match)) {
360
        $time_separator = $match[1];
361
    }
362
    $t23 = strpos($time_format, '23') !== false ? '%H' : '%I';
363
    if(!isset($match[2]) || $match[2] == '') {
364
        $ss->assign('CALENDAR_FORMAT', $date_format . ' ' . $t23 . $time_separator . "%M");
365
    }
366
    else {
367
        $pm = $match[2] == "pm" ? "%P" : "%p";
368
        $ss->assign('CALENDAR_FORMAT', $date_format . ' ' . $t23 . $time_separator . "%M" . $pm);
369
    }
370
371
    $ss->assign('CALENDAR_FDOW', $current_user->get_first_day_of_week());
372
373
    // populate the fieldlist from the vardefs
374
    $fieldlist = array();
375
    if ( !isset($focus) || !($focus instanceof SugarBean) )
376
        require_once($beanFiles[$beanList[$module]]);
377
    $focus = new $beanList[$module];
378
    // create the dropdowns for the parent type fields
379
    $vardefFields = $focus->getFieldDefinitions();
380
    if (isset($vardefFields[$fieldname]['type']) && $vardefFields[$fieldname]['type'] == 'parent_type' ) {
381
        $focus->field_defs[$fieldname]['options'] = $focus->field_defs[$vardefFields[$fieldname]['group']]['options'];
382
    }
383
    foreach ( $vardefFields as $name => $properties ) {
384
        $fieldlist[$name] = $properties;
385
        // fill in enums
386
        if(isset($fieldlist[$name]['options']) && is_string($fieldlist[$name]['options']) && isset($app_list_strings[$fieldlist[$name]['options']]))
387
            $fieldlist[$name]['options'] = $app_list_strings[$fieldlist[$name]['options']];
388
        // Bug 32626: fall back on checking the mod_strings if not in the app_list_strings
389
        elseif(isset($fieldlist[$name]['options']) && is_string($fieldlist[$name]['options']) && isset($mod_strings[$fieldlist[$name]['options']]))
390
            $fieldlist[$name]['options'] = $mod_strings[$fieldlist[$name]['options']];
391
        // Bug 22730: make sure all enums have the ability to select blank as the default value.
392
        if(!isset($fieldlist[$name]['options']['']))
393
            $fieldlist[$name]['options'][''] = '';
394
    }
395
396
    // fill in function return values
397
    if ( !in_array($fieldname,array('email1','email2')) )
398
    {
399
        if (!empty($fieldlist[$fieldname]['function']['returns']) && $fieldlist[$fieldname]['function']['returns'] == 'html')
400
        {
401
            $function = $fieldlist[$fieldname]['function']['name'];
402
            // include various functions required in the various vardefs
403
            if ( isset($fieldlist[$fieldname]['function']['include']) && is_file($fieldlist[$fieldname]['function']['include']))
404
                require_once($fieldlist[$fieldname]['function']['include']);
405
            $_REQUEST[$fieldname] = $value;
406
            $value = $function($focus, $fieldname, $value, $view);
407
408
            $value = str_ireplace($fieldname, $aow_field, $value);
409
        }
410
    }
411
412
    if(isset($fieldlist[$fieldname]['type']) && $fieldlist[$fieldname]['type'] == 'link'){
413
        $fieldlist[$fieldname]['id_name'] = $fieldlist[$fieldname]['name'].'_id';
414
415
        if((!isset($fieldlist[$fieldname]['module']) || $fieldlist[$fieldname]['module'] == '') && $focus->load_relationship($fieldlist[$fieldname]['name'])) {
416
            $relName = $fieldlist[$fieldname]['name'];
417
            $fieldlist[$fieldname]['module'] = $focus->$relName->getRelatedModuleName();
418
        }
419
    }
420
421
    if(isset($fieldlist[$fieldname]['name']) && ($fieldlist[$fieldname]['name'] == 'date_entered' || $fieldlist[$fieldname]['name'] == 'date_modified')){
422
        $fieldlist[$fieldname]['name'] = 'aow_temp_date';
423
        $fieldlist['aow_temp_date'] = $fieldlist[$fieldname];
424
        $fieldname = 'aow_temp_date';
425
    }
426
427
    $quicksearch_js = '';
428
    if(isset( $fieldlist[$fieldname]['id_name'] ) && $fieldlist[$fieldname]['id_name'] != '' && $fieldlist[$fieldname]['id_name'] != $fieldlist[$fieldname]['name']){
429
        $rel_value = $value;
430
431
        require_once("include/TemplateHandler/TemplateHandler.php");
432
        $template_handler = new TemplateHandler();
433
        $quicksearch_js = $template_handler->createQuickSearchCode($fieldlist,$fieldlist,$view);
434
        $quicksearch_js = str_replace($fieldname, $aow_field.'_display', $quicksearch_js);
435
        $quicksearch_js = str_replace($fieldlist[$fieldname]['id_name'], $aow_field, $quicksearch_js);
436
437
        echo $quicksearch_js;
438
439
        if(isset($fieldlist[$fieldname]['module']) && $fieldlist[$fieldname]['module'] == 'Users'){
440
            $rel_value = get_assigned_user_name($value);
441
        } else if(isset($fieldlist[$fieldname]['module'])){
442
            require_once($beanFiles[$beanList[$fieldlist[$fieldname]['module']]]);
443
            $rel_focus = new $beanList[$fieldlist[$fieldname]['module']];
444
            $rel_focus->retrieve($value);
445
            if(isset($fieldlist[$fieldname]['rname']) && $fieldlist[$fieldname]['rname'] != ''){
446
                $relDisplayField = $fieldlist[$fieldname]['rname'];
447
            } else {
448
                $relDisplayField = 'name';
449
            }
450
            $rel_value = $rel_focus->$relDisplayField;
451
        }
452
453
        $fieldlist[$fieldlist[$fieldname]['id_name']]['value'] = $value;
454
        $fieldlist[$fieldname]['value'] = $rel_value;
455
        $fieldlist[$fieldname]['id_name'] = $aow_field;
456
        $fieldlist[$fieldlist[$fieldname]['id_name']]['name'] = $aow_field;
457
        $fieldlist[$fieldname]['name'] = $aow_field.'_display';
458
    } else if(isset( $fieldlist[$fieldname]['type'] ) && $view == 'DetailView' && ($fieldlist[$fieldname]['type'] == 'datetimecombo' || $fieldlist[$fieldname]['type'] == 'datetime' || $fieldlist[$fieldname]['type'] == 'date')){
459
        $value = $focus->convertField($value, $fieldlist[$fieldname]);
460
        if(!empty($params['date_format']) && isset($params['date_format'])){
461
            $convert_format = "Y-m-d H:i:s";
462
            if($fieldlist[$fieldname]['type'] == 'date') $convert_format = "Y-m-d";
463
            $fieldlist[$fieldname]['value'] = $timedate->to_display($value, $convert_format, $params['date_format']);
464
        }else{
465
            $fieldlist[$fieldname]['value'] = $timedate->to_display_date_time($value, true, true);
466
        }
467
        $fieldlist[$fieldname]['name'] = $aow_field;
468
    } else if(isset( $fieldlist[$fieldname]['type'] ) && ($fieldlist[$fieldname]['type'] == 'datetimecombo' || $fieldlist[$fieldname]['type'] == 'datetime' || $fieldlist[$fieldname]['type'] == 'date')){
469
        $value = $focus->convertField($value, $fieldlist[$fieldname]);
470
        $fieldlist[$fieldname]['value'] = $timedate->to_display_date($value);
471
        //$fieldlist[$fieldname]['value'] = $timedate->to_display_date_time($value, true, true);
472
        //$fieldlist[$fieldname]['value'] = $value;
473
        $fieldlist[$fieldname]['name'] = $aow_field;
474
    } else {
475
        $fieldlist[$fieldname]['value'] = $value;
476
        $fieldlist[$fieldname]['name'] = $aow_field;
477
478
    }
479
480
    if(isset($fieldlist[$fieldname]['type']) && $fieldlist[$fieldname]['type'] == 'currency' && $view != 'EditView'){
481
        static $sfh;
482
483
        if(!isset($sfh)) {
484
            require_once('include/SugarFields/SugarFieldHandler.php');
485
            $sfh = new SugarFieldHandler();
486
        }
487
488
        if($currency_id != '' && !stripos($fieldname, '_USD')){
489
            $userCurrencyId = $current_user->getPreference('currency');
490
            if($currency_id != $userCurrencyId){
491
                $currency = new Currency();
492
                $currency->retrieve($currency_id);
493
                $value = $currency->convertToDollar($value);
494
                $currency->retrieve($userCurrencyId);
495
                $value = $currency->convertFromDollar($value);
496
            }
497
        }
498
499
        $parentfieldlist[strtoupper($fieldname)] = $value;
500
501
        return($sfh->displaySmarty($parentfieldlist, $fieldlist[$fieldname], 'ListView', $displayParams));
502
    }
503
504
    $ss->assign("QS_JS", $quicksearch_js);
505
    $ss->assign("fields",$fieldlist);
506
    $ss->assign("form_name",$view);
507
    $ss->assign("bean",$focus);
508
509
    // add in any additional strings
510
    $ss->assign("MOD", $mod_strings);
511
    $ss->assign("APP", $app_strings);
512
513
    //$return = str_replace($fieldname,$ss->fetch($file));
514
515
    return $ss->fetch($file);
516
}
517
518
519
520
function getDateField($module, $aow_field, $view='EditView', $value, $field_option = true){
521
    global $app_list_strings;
522
523
    $value = json_decode(html_entity_decode_utf8($value), true);
524
525
    if(!file_exists('modules/AOBH_BusinessHours/AOBH_BusinessHours.php')) unset($app_list_strings['aow_date_type_list']['business_hours']);
526
527
    $field = '';
528
529
    if($view == 'EditView'){
530
        $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;";
531
        $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;";
532
        $display = 'none';
533
        if($value[1] == 'plus' || $value[1] == 'minus') $display = '';
534
        $field .= "<input  type='text' style='width:40px; display:$display' name='$aow_field".'[2]'."' id='$aow_field".'[2]'."' title='' value='$value[2]' tabindex='116'>&nbsp;";
535
        $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>";
536
    }
537
    else {
538
        $field = getDateFields($module, $view, $value[0], $field_option).' '.$app_list_strings['aow_date_operator'][$value[1]];
539
        if($value[1] == 'plus' || $value[1] == 'minus'){
540
            $field .= ' '.$value[2].' '.$app_list_strings['aow_date_type_list'][$value[3]];
541
        }
542
    }
543
    return $field;
544
545
}
546
547
function getDateFields($module, $view='EditView',$value = '', $field_option = true)
548
{
549
    global $beanList, $app_list_strings;
550
551
    $fields = $app_list_strings['aow_date_options'];
552
553
    if(!$field_option) unset($fields['field']);
554
555
    if ($module != '') {
556
        if(isset($beanList[$module]) && $beanList[$module]){
557
            $mod = new $beanList[$module]();
558
            foreach($mod->field_defs as $name => $arr){
559
                if($arr['type'] == 'date' || $arr['type'] == 'datetime' || $arr['type'] == 'datetimecombo'){
560
                    if(isset($arr['vname']) && $arr['vname'] != ''){
561
                        $fields[$name] = translate($arr['vname'],$mod->module_dir);
562
                    } else {
563
                        $fields[$name] = $name;
564
                    }
565
                }
566
            } //End loop.
567
568
        }
569
    }
570
    if($view == 'EditView'){
571
        return get_select_options_with_id($fields, $value);
572
    } else {
573
        return $fields[$value];
574
    }
575
}
576
577
function getAssignField($aow_field, $view, $value){
578
    global $app_list_strings;
579
580
    $value = json_decode(html_entity_decode_utf8($value), true);
581
582
    $roles = get_bean_select_array(true, 'ACLRole','name', '','name',true);
583
584
    if(!file_exists('modules/SecurityGroups/SecurityGroup.php')){
585
        unset($app_list_strings['aow_assign_options']['security_group']);
586
    }
587
    else{
588
        $securityGroups = get_bean_select_array(true, 'SecurityGroup','name', '','name',true);
589
    }
590
591
    $field = '';
592
593
    if($view == 'EditView'){
594
        $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;";
595
        if(!file_exists('modules/SecurityGroups/SecurityGroup.php')){
596
            $field .= "<input type='hidden' name='$aow_field".'[1]'."' id='$aow_field".'[1]'."' value=''  />";
597
        }
598
        else {
599
            $display = 'none';
600
            if($value[0] == 'security_group') $display = '';
601
            $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;";
602
        }
603
        $display = 'none';
604
        if($value[0] == 'role' || $value[0] == 'security_group') $display = '';
605
        $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;";
606
    }
607
    else {
608
        $field = $app_list_strings['aow_assign_options'][$value[1]];
609
    }
610
    return $field;
611
612
}
613
614
function getDropdownList($list_id, $selected_value) {
615
    global $app_list_strings;
616
    $option = '';
617
    foreach($app_list_strings[$list_id] as $key => $value) {
618
        if(base64_decode($selected_value) == $key) {
619
            $option .= '<option value="'.$key.'" selected>'.$value.'</option>';
620
        } else if($selected_value == $key) {
621
            $option .= '<option value="'.$key.'" selected>'.$value.'</option>';
622
        }
623
        else {
624
            $option .= '<option value="'.$key.'">'.$value.'</option>';
625
        }
626
    }
627
    return $option;
628
}
629
function getLeastBusyUser($users, $field, SugarBean $bean) {
630
    $counts = array();
631
    foreach($users as $id) {
632
        $c = $bean->db->getOne("SELECT count(*) AS c FROM ".$bean->table_name." WHERE $field = '$id' AND deleted = 0");
633
        $counts[$id] = $c;
634
    }
635
    asort($counts);
636
    $countsKeys = array_flip($counts);
637
    return array_shift($countsKeys);
638
}
639
640
function getRoundRobinUser($users, $id) {
641
642
    $file = create_cache_directory('modules/AOW_WorkFlow/Users/') . $id . 'lastUser.cache.php';
643
644
    if(isset($_SESSION['lastuser'][$id]) && $_SESSION['lastuser'][$id] != '') {
645
        $users_by_key = array_flip($users); // now keys are values
646
        $key = $users_by_key[$_SESSION['lastuser'][$id]] + 1;
647
        if(!empty($users[$key])) {
648
            return $users[$key];
649
        }
650
    }
651
    else if (is_file($file)){
652
        require_once($file);
653
        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...
654
            $users_by_key = array_flip($users); // now keys are values
655
            $key = $users_by_key[$lastUser['User']] + 1;
656
            if(!empty($users[$key])) {
657
                return $users[$key];
658
            }
659
        }
660
    }
661
662
   return $users[0];
663
}
664
665
function setLastUser($user_id, $id) {
666
667
    $_SESSION['lastuser'][$id] = $user_id;
668
669
    $file = create_cache_directory('modules/AOW_WorkFlow/Users/') . $id . 'lastUser.cache.php';
670
671
    $arrayString = var_export_helper(array('User' => $user_id));
672
673
    $content =<<<eoq
674
<?php
675
	\$lastUser = {$arrayString};
676
?>
677
eoq;
678
679
    if($fh = @sugar_fopen($file, 'w')) {
680
        fputs($fh, $content);
681
        fclose($fh);
682
    }
683
    return true;
684
}
685
686
function getEmailableModules(){
687
    global $beanFiles, $beanList, $app_list_strings;
688
    $emailableModules = array();
689
    foreach($app_list_strings['aow_moduleList'] as $bean_name => $bean_dis) {
690
        if(isset($beanList[$bean_name]) && isset($beanFiles[$beanList[$bean_name]])){
691
            require_once($beanFiles[$beanList[$bean_name]]);
692
            $obj = new $beanList[$bean_name];
693
            if($obj instanceof Person || $obj instanceof Company){
694
                $emailableModules[] = $bean_name;
695
            }
696
        }
697
    }
698
    asort($emailableModules);
699
    return $emailableModules;
700
}
701
702
function getRelatedEmailableFields($module){
703
    global $beanList, $app_list_strings;
704
    $relEmailFields = array();
705
    $checked_link = array();
706
    $emailableModules = getEmailableModules();
707
    if ($module != '') {
708
        if(isset($beanList[$module]) && $beanList[$module]){
709
            $mod = new $beanList[$module]();
710
711
            foreach($mod->get_related_fields() as $field){
712
                if(isset($field['link'])) $checked_link[] = $field['link'];
713
                if(!isset($field['module']) || !in_array($field['module'],$emailableModules) || (isset($field['dbType']) && $field['dbType'] == "id")){
714
                    continue;
715
                }
716
                $relEmailFields[$field['name']] = $field['module'].": ".trim(translate($field['vname'],$mod->module_name),":");
717
            }
718
719
            foreach($mod->get_linked_fields() as $field){
720
                if(!in_array($field['name'],$checked_link) && !in_array($field['relationship'],$checked_link)){
721
                    if(isset($field['module']) && $field['module'] != '') {
722
                        $rel_module = $field['module'];
723
                    } else if($mod->load_relationship($field['name'])){
724
                        $relField = $field['name'];
725
                        $rel_module = $mod->$relField->getRelatedModuleName();
726
                    }
727
728
                    if(in_array($rel_module,$emailableModules)) {
729
                        if (isset($field['vname']) && $field['vname'] != '') {
730
                            $relEmailFields[$field['name']] = $app_list_strings['moduleList'][$rel_module] . ' : ' . translate($field['vname'], $mod->module_dir);
731
                        } else {
732
                            $relEmailFields[$field['name']] = $app_list_strings['moduleList'][$rel_module] . ' : ' . $field['name'];
733
                        }
734
                    }
735
                }
736
            }
737
738
            array_multisort($relEmailFields, SORT_ASC, $relEmailFields);
739
        }
740
    }
741
    return $relEmailFields;
742
}
743
744
function fixUpFormatting($module, $field, $value)
745
{
746
    global $timedate, $beanFiles, $beanList;
747
748
    require_once($beanFiles[$beanList[$module]]);
749
    $bean = new $beanList[$module];
750
    
751
    static $boolean_false_values = array('off', 'false', '0', 'no');
752
753
    switch($bean->field_defs[$field]['type']) {
754
        case 'datetime':
755
        case 'datetimecombo':
756
            if(empty($value)) break;
757
            if ($value == 'NULL') {
758
                $value = '';
759
                break;
760
            }
761
            if ( ! preg_match('/^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}$/',$value) ) {
762
                // This appears to be formatted in user date/time
763
                $value = $timedate->to_db($value);
764
            }
765
            break;
766
        case 'date':
767
            if(empty($value)) break;
768
            if ($value == 'NULL') {
769
                $value = '';
770
                break;
771
            }
772
            if ( ! preg_match('/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/',$value) ) {
773
                // This date appears to be formatted in the user's format
774
                $value = $timedate->to_db_date($value, false);
775
            }
776
            break;
777
        case 'time':
778
            if(empty($value)) break;
779
            if ($value == 'NULL') {
780
                $value = '';
781
                break;
782
            }
783
            if ( preg_match('/(am|pm)/i',$value) ) {
784
                // This time appears to be formatted in the user's format
785
                $value = $timedate->fromUserTime($value)->format(TimeDate::DB_TIME_FORMAT);
786
            }
787
            break;
788
        case 'double':
789
        case 'decimal':
790
        case 'currency':
791
        case 'float':
792
            if ( $value === '' || $value == NULL || $value == 'NULL') {
793
                continue;
794
            }
795
            if ( is_string($value) ) {
796
                $value = (float)unformat_number($value);
797
            }
798
            break;
799
        case 'uint':
800
        case 'ulong':
801
        case 'long':
802
        case 'short':
803
        case 'tinyint':
804
        case 'int':
805
            if ( $value === '' || $value == NULL || $value == 'NULL') {
806
                continue;
807
            }
808
            if ( is_string($value) ) {
809
                $value = (int)unformat_number($value);
810
            }
811
            break;
812
        case 'bool':
813
            if (empty($value)) {
814
                $value = false;
815
            } else if(true === $value || 1 == $value) {
816
                $value = true;
817
            } else if(in_array(strval($value), $boolean_false_values)) {
818
                $value = false;
819
            } else {
820
                $value = true;
821
            }
822
            break;
823
        case 'encrypt':
824
            $value = $this->encrpyt_before_save($value);
825
            break;
826
    }
827
    return $value;
828
829
}