1 | <?php |
||||||
2 | /** |
||||||
3 | * @package content |
||||||
4 | */ |
||||||
5 | /** |
||||||
6 | * The AjaxQuery returns an JSON array of entries, associations and other |
||||||
7 | * static values, depending on the parameters received. |
||||||
8 | */ |
||||||
9 | |||||||
10 | class contentAjaxQuery extends JSONPage |
||||||
0 ignored issues
–
show
|
|||||||
11 | { |
||||||
12 | public function view() |
||||||
13 | { |
||||||
14 | $database = Symphony::Configuration()->get('db', 'database'); |
||||||
15 | $field_ids = array_map(array('General','intval'), explode(',', General::sanitize($_GET['field_id']))); |
||||||
16 | $search = MySQL::cleanValue(General::sanitize($_GET['query'])); |
||||||
17 | $types = array_map(array('MySQL','cleanValue'), explode(',', General::sanitize($_GET['types']))); |
||||||
18 | $limit = General::intval(General::sanitize($_GET['limit'])); |
||||||
19 | |||||||
20 | // Set limit |
||||||
21 | if ($limit === 0) { |
||||||
22 | $max = ''; |
||||||
23 | } elseif ($limit < 0) { |
||||||
24 | $max = ' LIMIT 100'; |
||||||
25 | } else { |
||||||
26 | $max = sprintf(' LIMIT %d', $limit); |
||||||
27 | } |
||||||
28 | |||||||
29 | // Entries |
||||||
30 | if (in_array('entry', $types)) { |
||||||
31 | foreach ($field_ids as $field_id) { |
||||||
32 | $this->get($database, intval($field_id), $search, $max); |
||||||
33 | } |
||||||
34 | } |
||||||
35 | |||||||
36 | // Associations |
||||||
37 | if (in_array('association', $types)) { |
||||||
38 | foreach ($field_ids as $field_id) { |
||||||
39 | $association_id = $this->getAssociationId($field_id); |
||||||
40 | |||||||
41 | if ($association_id) { |
||||||
42 | $this->get($database, $association_id, $search, $max); |
||||||
43 | } |
||||||
44 | } |
||||||
45 | } |
||||||
46 | |||||||
47 | // Static values |
||||||
48 | if (in_array('static', $types)) { |
||||||
49 | foreach ($field_ids as $field_id) { |
||||||
50 | $this->getStatic($field_id, $search); |
||||||
51 | } |
||||||
52 | } |
||||||
53 | |||||||
54 | // Return results |
||||||
55 | return $this->_Result; |
||||||
56 | } |
||||||
57 | |||||||
58 | private function getAssociationId($field_id) |
||||||
59 | { |
||||||
60 | $field = FieldManager::fetch($field_id); |
||||||
61 | $parent_section = SectionManager::fetch($field->get('parent_section')); |
||||||
62 | |||||||
63 | $association_id = Symphony::Database()->fetchCol('parent_section_field_id', |
||||||
64 | sprintf( |
||||||
65 | "SELECT `parent_section_field_id` FROM tbl_sections_association WHERE `child_section_field_id` = %d AND `child_section_id` = %d LIMIT 1;", |
||||||
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
The string literal
SELECT `parent_section_f...ction_id` = %d LIMIT 1; does not require double quotes, as per coding-style, please use single quotes.
PHP provides two ways to mark string literals. Either with single quotes String literals in single quotes on the other hand are evaluated very literally and the only two
characters that needs escaping in the literal are the single quote itself ( Double quoted string literals may contain other variables or more complex escape sequences. <?php
$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";
print $doubleQuoted;
will print an indented: If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear. For more information on PHP string literals and available escape sequences see the PHP core documentation. ![]() |
|||||||
66 | $field_id, $parent_section->get('id') |
||||||
0 ignored issues
–
show
It seems like
$parent_section->get('id') can also be of type array ; however, parameter $args of sprintf() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
67 | ) |
||||||
68 | ); |
||||||
69 | |||||||
70 | return $association_id[0]; |
||||||
71 | } |
||||||
72 | |||||||
73 | private function getStatic($field_id, $search = null) |
||||||
0 ignored issues
–
show
|
|||||||
74 | { |
||||||
75 | $options = array(); |
||||||
76 | |||||||
77 | if (!empty($field_id)) { |
||||||
78 | $field = FieldManager::fetch($field_id); |
||||||
79 | |||||||
80 | if (!empty($field) && $field->canPublishFilter() === true) { |
||||||
81 | if (method_exists($field, 'getToggleStates')) { |
||||||
82 | $options = $field->getToggleStates(); |
||||||
83 | } elseif (method_exists($field, 'findAllTags')) { |
||||||
84 | $options = $field->findAllTags(); |
||||||
85 | } |
||||||
86 | } |
||||||
87 | } |
||||||
88 | |||||||
89 | foreach ($options as $value => $data) { |
||||||
90 | if (!$search || strripos($data, $search) !== false || strripos($value, $search) !== false) { |
||||||
91 | $this->_Result['entries'][]['value'] = ($data ? $data : $value); |
||||||
0 ignored issues
–
show
|
|||||||
92 | } |
||||||
93 | } |
||||||
94 | } |
||||||
95 | |||||||
96 | private function get($database, $field_id, $search, $max) |
||||||
97 | { |
||||||
98 | // Get entries |
||||||
99 | if (!empty($search)) { |
||||||
0 ignored issues
–
show
|
|||||||
100 | |||||||
101 | // Get columns |
||||||
102 | $columns = Symphony::Database()->fetchCol('column_name', |
||||||
103 | sprintf( |
||||||
104 | "SELECT column_name |
||||||
105 | FROM information_schema.columns |
||||||
106 | WHERE table_schema = '%s' |
||||||
107 | AND table_name = 'tbl_entries_data_%d' |
||||||
108 | AND column_name != 'id' |
||||||
109 | AND column_name != 'entry_id';", |
||||||
110 | $database, |
||||||
111 | $field_id |
||||||
112 | ) |
||||||
113 | ); |
||||||
114 | |||||||
115 | // Build where clauses |
||||||
116 | $where = array(); |
||||||
117 | foreach ($columns as $column) { |
||||||
118 | $where[] = "`$column` LIKE '%$search%'"; |
||||||
0 ignored issues
–
show
As per coding-style, please use concatenation or
sprintf for the variable $column instead of interpolation.
It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings. // Instead of
$x = "foo $bar $baz";
// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
![]() As per coding-style, please use concatenation or
sprintf for the variable $search instead of interpolation.
It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings. // Instead of
$x = "foo $bar $baz";
// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
![]() |
|||||||
119 | } |
||||||
120 | |||||||
121 | // Build query |
||||||
122 | $query = sprintf( |
||||||
123 | "SELECT * from tbl_entries_data_%d WHERE %s%s;", |
||||||
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
The string literal
SELECT * from tbl_entries_data_%d WHERE %s%s; does not require double quotes, as per coding-style, please use single quotes.
PHP provides two ways to mark string literals. Either with single quotes String literals in single quotes on the other hand are evaluated very literally and the only two
characters that needs escaping in the literal are the single quote itself ( Double quoted string literals may contain other variables or more complex escape sequences. <?php
$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";
print $doubleQuoted;
will print an indented: If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear. For more information on PHP string literals and available escape sequences see the PHP core documentation. ![]() |
|||||||
124 | $field_id, |
||||||
125 | implode($where, " OR "), |
||||||
0 ignored issues
–
show
The call to
implode() has too many arguments starting with ' OR ' .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue. If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above. ![]() Coding Style
Comprehensibility
introduced
by
The string literal
OR does not require double quotes, as per coding-style, please use single quotes.
PHP provides two ways to mark string literals. Either with single quotes String literals in single quotes on the other hand are evaluated very literally and the only two
characters that needs escaping in the literal are the single quote itself ( Double quoted string literals may contain other variables or more complex escape sequences. <?php
$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";
print $doubleQuoted;
will print an indented: If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear. For more information on PHP string literals and available escape sequences see the PHP core documentation. ![]() |
|||||||
126 | $max |
||||||
127 | ); |
||||||
128 | } else { |
||||||
129 | $query = sprintf( |
||||||
130 | "SELECT * from tbl_entries_data_%d%s;", |
||||||
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
The string literal
SELECT * from tbl_entries_data_%d%s; does not require double quotes, as per coding-style, please use single quotes.
PHP provides two ways to mark string literals. Either with single quotes String literals in single quotes on the other hand are evaluated very literally and the only two
characters that needs escaping in the literal are the single quote itself ( Double quoted string literals may contain other variables or more complex escape sequences. <?php
$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";
print $doubleQuoted;
will print an indented: If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear. For more information on PHP string literals and available escape sequences see the PHP core documentation. ![]() |
|||||||
131 | $field_id, |
||||||
132 | $max |
||||||
133 | ); |
||||||
134 | } |
||||||
135 | |||||||
136 | // Fetch field values |
||||||
137 | $data = Symphony::Database()->fetch($query); |
||||||
138 | |||||||
139 | if (!empty($data)) { |
||||||
140 | $field = FieldManager::fetch($field_id); |
||||||
141 | $parent_section = SectionManager::fetch($field->get('parent_section')); |
||||||
142 | $parent_section_handle = $parent_section->get('handle'); |
||||||
143 | |||||||
144 | foreach ($data as $field_data) { |
||||||
145 | $entry_id = $field_data['entry_id']; |
||||||
146 | |||||||
147 | if ($field instanceof ExportableField && in_array(ExportableField::UNFORMATTED, $field->getExportModes())) { |
||||||
0 ignored issues
–
show
|
|||||||
148 | |||||||
149 | // Get unformatted value |
||||||
150 | $value = $field->prepareExportValue($field_data, ExportableField::UNFORMATTED, $entry_id); |
||||||
151 | } elseif ($field instanceof ExportableField && in_array(ExportableField::VALUE, $field->getExportModes())) { |
||||||
0 ignored issues
–
show
|
|||||||
152 | |||||||
153 | // Get formatted value |
||||||
154 | $value = $field->prepareExportValue($field_data, ExportableField::VALUE, $entry_id); |
||||||
155 | } else { |
||||||
0 ignored issues
–
show
|
|||||||
156 | |||||||
157 | // Get value from parameter pool |
||||||
158 | $value = $field->getParameterPoolValue($field_data, $entry_id); |
||||||
0 ignored issues
–
show
The method
getParameterPoolValue() does not exist on ExportableField . Since it exists in all sub-types, consider adding an abstract or default implementation to ExportableField .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
159 | } |
||||||
160 | |||||||
161 | $this->_Result['entries'][$entry_id]['value'] = $value; |
||||||
162 | $this->_Result['entries'][$entry_id]['section'] = $parent_section_handle; |
||||||
163 | $this->_Result['entries'][$entry_id]['link'] = APPLICATION_URL . '/publish/' . $parent_section_handle . '/edit/' . $entry_id . '/'; |
||||||
0 ignored issues
–
show
Are you sure
$parent_section_handle of type array|string can be used in concatenation ?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
164 | } |
||||||
165 | } |
||||||
166 | } |
||||||
167 | } |
||||||
168 |
Classes in PHP are usually named in CamelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.
Thus the name database provider becomes
DatabaseProvider
.