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 |
|
|
|
|
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;", |
|
|
|
|
66
|
|
|
$field_id, $parent_section->get('id') |
|
|
|
|
67
|
|
|
) |
68
|
|
|
); |
69
|
|
|
|
70
|
|
|
return $association_id[0]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
private function getStatic($field_id, $search = null) |
|
|
|
|
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); |
|
|
|
|
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
private function get($database, $field_id, $search, $max) |
97
|
|
|
{ |
98
|
|
|
// Get entries |
99
|
|
|
if (!empty($search)) { |
|
|
|
|
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%'"; |
|
|
|
|
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
// Build query |
122
|
|
|
$query = sprintf( |
123
|
|
|
"SELECT * from tbl_entries_data_%d WHERE %s%s;", |
|
|
|
|
124
|
|
|
$field_id, |
125
|
|
|
implode($where, " OR "), |
|
|
|
|
126
|
|
|
$max |
127
|
|
|
); |
128
|
|
|
} else { |
129
|
|
|
$query = sprintf( |
130
|
|
|
"SELECT * from tbl_entries_data_%d%s;", |
|
|
|
|
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())) { |
|
|
|
|
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())) { |
|
|
|
|
152
|
|
|
|
153
|
|
|
// Get formatted value |
154
|
|
|
$value = $field->prepareExportValue($field_data, ExportableField::VALUE, $entry_id); |
155
|
|
|
} else { |
|
|
|
|
156
|
|
|
|
157
|
|
|
// Get value from parameter pool |
158
|
|
|
$value = $field->getParameterPoolValue($field_data, $entry_id); |
|
|
|
|
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 . '/'; |
|
|
|
|
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
.