templateFields.php ➔ genDropDownJS2()   C
last analyzed

Complexity

Conditions 11
Paths 5

Size

Total Lines 36
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132
Metric Value
cc 11
eloc 20
nc 5
nop 0
dl 0
loc 36
ccs 0
cts 29
cp 0
crap 132
rs 5.2653

How to fix   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
if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3
4
5
function generateFieldDefsJS2()
6
{
7
    global $app_list_strings, $beanList, $beanFiles;
8
9
10
    $badFields = array(
11
        'account_description',
12
        'contact_id',
13
        'lead_id',
14
        'opportunity_amount',
15
        'opportunity_id',
16
        'opportunity_name',
17
        'opportunity_role_id',
18
        'opportunity_role_fields',
19
        'opportunity_role',
20
        'campaign_id',
21
        // User objects
22
        'id',
23
        'user_preferences',
24
        'accept_status',
25
        'user_hash',
26
        'authenticate_id',
27
        'sugar_login',
28
        'reports_to_id',
29
        'reports_to_name',
30
        'is_admin',
31
        'receive_notifications',
32
        'modified_user_id',
33
        'modified_by_name',
34
        'created_by',
35
        'created_by_name',
36
        'accept_status_id',
37
        'accept_status_name',
38
    );
39
40
    $loopControl = array();
41
    $prefixes = array();
42
43
    foreach ($app_list_strings['moduleList'] as $key => $name) {
44
        if (isset($beanList[$key]) && isset($beanFiles[$beanList[$key]]) && !str_begin($key, 'AOW_')) {
45
46
            require_once($beanFiles[$beanList[$key]]);
47
            $focus = new $beanList[$key];
48
            $loopControl[$key][$key] = $focus;
49
            $prefixes[$key] = strtolower($focus->object_name) . '_';
50
            if ($focus->object_name == 'Case') $prefixes[$key] = 'a' . strtolower($focus->object_name) . '_';
51
        }
52
    }
53
54
    $contact = new Contact();
55
    $lead = new Lead();
56
    $prospect = new Prospect();
57
58
    $loopControl['Contacts'] = array(
59
        'Contacts' => $contact,
60
        'Leads' => $lead,
61
        'Prospects' => $prospect,
62
    );
63
64
    $prefixes['Users'] = 'contact_user_';
65
66
67
    $collection = array();
68
    foreach ($loopControl as $collectionKey => $beans) {
69
        $collection[$collectionKey] = array();
70
        foreach ($beans as $beankey => $bean) {
71
72
            foreach ($bean->field_defs as $key => $field_def) {
73
                if (    /*($field_def['type'] == 'relate' && empty($field_def['custom_type'])) ||*/
74
                    ($field_def['type'] == 'assigned_user_name' || $field_def['type'] == 'link') ||
75
                    ($field_def['type'] == 'bool') ||
76
                    (in_array($field_def['name'], $badFields))
77
                ) {
78
                    continue;
79
                }
80
                if (!isset($field_def['vname'])) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
81
                    //echo $key;
82
                }
83
                // valid def found, process
84
                $optionKey = strtolower("{$prefixes[$collectionKey]}{$key}");
85
                if (isset($field_def['vname'])) {
86
                    $optionLabel = preg_replace('/:$/', "", translate($field_def['vname'], $beankey));
87
                } else {
88
                    $optionLabel = preg_replace('/:$/', "", $field_def['name']);
89
                }
90
                $dup = 1;
91
                foreach ($collection[$collectionKey] as $value) {
92
                    if ($value['name'] == $optionKey) {
93
                        $dup = 0;
94
                        break;
95
                    }
96
                }
97
                if ($dup)
98
                    $collection[$collectionKey][] = array("name" => $optionKey, "value" => $optionLabel);
99
            }
100
        }
101
    }
102
103
    $json = getJSONobj();
104
    $ret = "var field_defs = ";
105
    $ret .= $json->encode($collection, false);
106
    $ret .= ";";
107
    return $ret;
108
}
109
110
function genDropDownJS2()
111
{
112
    global $app_list_strings, $beanList, $beanFiles;
113
114
    $lblContactAndOthers = implode('/', array(
115
        isset($app_list_strings['moduleListSingular']['Contacts']) ? $app_list_strings['moduleListSingular']['Contacts'] : 'Contact',
116
        isset($app_list_strings['moduleListSingular']['Leads']) ? $app_list_strings['moduleListSingular']['Leads'] : 'Lead',
117
        isset($app_list_strings['moduleListSingular']['Prospects']) ? $app_list_strings['moduleListSingular']['Prospects'] : 'Target',
118
    ));
119
120
    $dropdown = '';
121
122
    array_multisort($app_list_strings['moduleList'], SORT_ASC, $app_list_strings['moduleList']);
123
124
    foreach ($app_list_strings['moduleList'] as $key => $name) {
125
        if (isset($beanList[$key]) && isset($beanFiles[$beanList[$key]]) && !str_begin($key, 'AOW_') && !str_begin($key, 'zr2_')) {
126
127
            if ($key == 'Contacts') {
128
                $dropdown .= "<option value='" . $key . "'>
129
						" . $lblContactAndOthers . "
130
		  	       </option>";
131
            } else if (isset($app_list_strings['moduleListSingular'][$key])) {
132
                $dropdown .= "<option value='" . $key . "'>
133
						" . $app_list_strings['moduleListSingular'][$key] . "
134
		  	       </option>";
135
            } else {
136
                $dropdown .= "<option value='" . $key . "'>
137
						" . $app_list_strings['moduleList'][$key] . "
138
		  	       </option>";
139
            }
140
        }
141
    }
142
143
144
    return $dropdown;
145
}
146