SugarWidgetFieldRelate::displayList()   C
last analyzed

Complexity

Conditions 8
Paths 4

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72
Metric Value
cc 8
eloc 16
nc 4
nop 1
dl 0
loc 24
ccs 0
cts 21
cp 0
crap 72
rs 5.7377
1
<?php
2
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3
/*********************************************************************************
4
 * SugarCRM Community Edition is a customer relationship management program developed by
5
 * SugarCRM, Inc. Copyright (C) 2004-2013 SugarCRM Inc.
6
7
 * SuiteCRM is an extension to SugarCRM Community Edition developed by Salesagility Ltd.
8
 * Copyright (C) 2011 - 2014 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
42
class SugarWidgetFieldRelate extends SugarWidgetReportField
43
{
44
    /**
45
     * Method returns HTML of input on configure dashlet page
46
     *
47
     * @param array $layout_def definition of a field
48
     * @return string HTML of select for edit page
49
     */
50
    public function displayInput($layout_def)
51
    {
52
        $values = array();
53
        if (is_array($layout_def['input_name0']))
54
        {
55
            $values = $layout_def['input_name0'];
56
        }
57
        else
58
        {
59
            $values[] = $layout_def['input_name0'];
60
        }
61
        $html = '<select name="' . $layout_def['name'] . '[]" multiple="true">';
62
63
        $query = $this->displayInputQuery($layout_def);
64
        $result = $this->reporter->db->query($query);
65
        while ($row = $this->reporter->db->fetchByAssoc($result))
66
        {
67
            $html .= '<option value="' . $row['id'] . '"';
68
            if (in_array($row['id'], $values))
69
            {
70
                $html .= ' selected="selected"';
71
            }
72
            $html .= '>' . htmlspecialchars($row['title']) . '</option>';
73
        }
74
75
        $html .= '</select>';
76
        return $html;
77
    }
78
79
    /**
80
     * Method returns database query for generation HTML of input on configure dashlet page
81
     *
82
     * @param array $layout_def definition of a field
83
     * @return string database query HTML of select for edit page
84
     */
85
    private function displayInputQuery($layout_def)
86
    {
87
        $title = $layout_def['rname'];
88
        $bean = isset($layout_def['module']) ? BeanFactory::getBean($layout_def['module']) : NULL;
89
        $table = empty($bean) ? $layout_def['table'] : $bean->table_name;
90
        $concat_fields = isset($layout_def['db_concat_fields']) ? $layout_def['db_concat_fields'] : '';
91
92
        if (empty($concat_fields) && !empty($bean) && isset($bean->field_defs[$title]['db_concat_fields']))
93
        {
94
            $concat_fields = $bean->field_defs[$title]['db_concat_fields'];
95
        }
96
        if (!empty($concat_fields))
97
        {
98
            $title = $this->reporter->db->concat($table, $concat_fields);
99
        }
100
101
        $query = "SELECT
102
                id,
103
                $title title
104
            FROM $table
105
            WHERE deleted = 0
106
            ORDER BY title ASC";
107
        return $query;
108
    }
109
110
    /**
111
     * Method returns part of where in style table_alias.id IN (...) because we can't join of relation
112
     *
113
     * @param array $layout_def definition of a field
114
     * @param bool $rename_columns unused
115
     * @return string SQL where part
116
     */
117
    public function queryFilterStarts_With($layout_def, $rename_columns = true)
118
    {
119
        $ids = array();
120
121
        $relation = new Relationship();
122
        $relation->retrieve_by_name($layout_def['link']);
123
124
        global $beanList;
125
        $beanClass = $beanList[$relation->lhs_module];
126
        $seed = new $beanClass();
127
        $seed->retrieve($layout_def['input_name0']);
128
129
        $link = new Link2($layout_def['link'], $seed);
130
        $sql = $link->getQuery();
131
        $result = $this->reporter->db->query($sql);
132
        while ($row = $this->reporter->db->fetchByAssoc($result))
133
        {
134
            $ids[] = $row['id'];
135
        }
136
        $layout_def['name'] = 'id';
137
        return $this->_get_column_select($layout_def) . " IN ('" . implode("', '", $ids) . "')";
138
    }
139
140
    /**
141
     * Method returns part of where in style table_alias.id IN (...) because we can't join of relation
142
     *
143
     * @param array $layout_def definition of a field
144
     * @param bool $rename_columns unused
145
     * @return string SQL where part
146
     */
147
    public function queryFilterone_of($layout_def, $rename_columns = true)
148
    {
149
        $ids = array();
150
        if (isset($layout_def['link'])) {
151
            $relation = new Relationship();
152
            $relation->retrieve_by_name($layout_def['link']);
153
        }
154
        $module = isset($layout_def['custom_module']) ? $layout_def['custom_module'] : $layout_def['module'];
155
        $seed = BeanFactory::getBean($module);
156
157
        foreach($layout_def['input_name0'] as $beanId)
158
        {
159
            if (!empty($relation->lhs_module) && !empty($relation->rhs_module)
160
                && $relation->lhs_module == $relation->rhs_module) {
161
                    $filter = array('id');
162
            } else {
163
                $filter = array('id', $layout_def['name']);
164
            }
165
            $where = $layout_def['id_name']."='$beanId' ";
166
            $sql = $seed->create_new_list_query('', $where, $filter, array(), 0, '', false, $seed, true);
167
            $result = $this->reporter->db->query($sql);
0 ignored issues
show
Bug introduced by
It seems like $sql defined by $seed->create_new_list_q...'', false, $seed, true) on line 166 can also be of type array<string,?>; however, DBManager::query() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
168
            while ($row = $this->reporter->db->fetchByAssoc($result)) {
169
                $ids[] = $row['id'];
170
            }
171
        }
172
        $ids = array_unique($ids);
173
        $layout_def['name'] = 'id';
174
        return $this->_get_column_select($layout_def) . " IN ('" . implode("', '", $ids) . "')";
175
    }
176
177
	//for to_pdf/to_csv
178
	function displayListPlain($layout_def) {
179
	    $reporter = $this->layout_manager->getAttribute("reporter");
180
		$field_def = $reporter->all_fields[$layout_def['column_key']];
181
		$display = strtoupper($field_def['secondary_table'].'_name');
182
		//#31797  , we should get the table alias in a global registered array:selected_loaded_custom_links
183
		if(!empty($reporter->selected_loaded_custom_links) && !empty($reporter->selected_loaded_custom_links[$field_def['secondary_table']])){
184
			$display = strtoupper($reporter->selected_loaded_custom_links[$field_def['secondary_table']]['join_table_alias'].'_name');
185
		}
186
        elseif(isset($field_def['rep_rel_name']) && isset($reporter->selected_loaded_custom_links) && !empty($reporter->selected_loaded_custom_links[$field_def['secondary_table'].'_'.$field_def['rep_rel_name']]))
187
        {
188
            $display = strtoupper($reporter->selected_loaded_custom_links[$field_def['secondary_table'].'_'.$field_def['rep_rel_name']]['join_table_alias'].'_name');
189
        }
190
		elseif(!empty($reporter->selected_loaded_custom_links) && !empty($reporter->selected_loaded_custom_links[$field_def['secondary_table'].'_'.$field_def['name']])){
191
			$display = strtoupper($reporter->selected_loaded_custom_links[$field_def['secondary_table'].'_'.$field_def['name']]['join_table_alias'].'_name');
192
		}
193
		$cell = $layout_def['fields'][$display];
194
		return $cell;
195
	}
196
197
    function displayList($layout_def) {
198
        $reporter = $this->layout_manager->getAttribute("reporter");
199
        $field_def = $reporter->all_fields[$layout_def['column_key']];
200
        $display = strtoupper($field_def['secondary_table'].'_name');
201
202
        //#31797  , we should get the table alias in a global registered array:selected_loaded_custom_links
203
        if(!empty($reporter->selected_loaded_custom_links) && !empty($reporter->selected_loaded_custom_links[$field_def['secondary_table']])){
204
             $display = strtoupper($reporter->selected_loaded_custom_links[$field_def['secondary_table']]['join_table_alias'].'_name');
205
        }
206
        elseif(isset($field_def['rep_rel_name']) && isset($reporter->selected_loaded_custom_links) && !empty($reporter->selected_loaded_custom_links[$field_def['secondary_table'].'_'.$field_def['rep_rel_name']]))
207
        {
208
            $display = strtoupper($reporter->selected_loaded_custom_links[$field_def['secondary_table'].'_'.$field_def['rep_rel_name']]['join_table_alias'].'_name');
209
        }
210
        elseif(!empty($reporter->selected_loaded_custom_links) && !empty($reporter->selected_loaded_custom_links[$field_def['secondary_table'].'_'.$field_def['name']])){
211
            $display = strtoupper($reporter->selected_loaded_custom_links[$field_def['secondary_table'].'_'.$field_def['name']]['join_table_alias'].'_name');
212
        }
213
        $recordField = $this->getTruncatedColumnAlias(strtoupper($layout_def['table_alias']).'_'.strtoupper($layout_def['name']));
214
215
        $record = $layout_def['fields'][$recordField];
216
        $cell = "<a target='_blank' class=\"listViewTdLinkS1\" href=\"index.php?action=DetailView&module=".$field_def['ext2']."&record=$record\">";
217
        $cell .= $layout_def['fields'][$display];
218
        $cell .= "</a>";
219
        return $cell;
220
    }
221
}
222
223
?>
224