MIOLO20
Carregando...
Procurando...
Nenhuma entrada encontrada
mcustomvalue.class
Ir para a documentação deste ficheiro.
1<?php
2
26{
31 const KEY_SEPARATOR = '|';
32
36 public $id;
37
42
47
51 public $value;
52
58 public function __construct($data=NULL)
59 {
60 if ( $data )
61 {
62 $this->setData($data);
63 }
64 }
65
69 private static function getBusiness()
70 {
72 return new MBusiness('admin');
73 }
74
80 public function setData($data)
81 {
82 $this->id = $data->id;
83 $this->customizedId = $data->customizedId;
84 $this->customFieldId = $data->customFieldId;
85 $this->value = $data->value;
86 }
87
93 public function insert($id = null)
94 {
95 $this->id = $this->getBusiness()->_db->getNewId('miolo_custom_value_id_seq', 'miolo_sequence');
96
97 $sql = new MSQL(
98 'id, customized_id, custom_field_id, value',
99 'miolo_custom_value'
100 );
101
102 $args = array(
103 $this->id,
104 $this->customizedId,
105 $id ?? $this->customFieldId,
106 $this->value,
107 );
108
109 return self::getBusiness()->execute($sql->insert($args));
110 }
111
117 public function update($customFieldId = null)
118 {
119 $sql = new MSQL(
120 'customized_id, custom_field_id, value',
121 'miolo_custom_value',
122 'id = ?'
123 );
124
125 $args = array(
126 $this->customizedId,
127 $customFieldId ?? $this->customFieldId,
128 $this->value,
129 $this->id,
130 );
131
132 return self::getBusiness()->execute($sql->update($args));
133 }
134
140 public function updateByData()
141 {
142 $sql_field = "SELECT id,
143 name
144 FROM miolo_custom_field
145 WHERE name = (SELECT name from miolo_custom_field where id = ?)
146 ORDER BY id";
147
148 $cols = array('id',
149 'nome',
150 );
151 $args[] = $this->customFieldId;
152 $fields = SAGU::resultToArray(SDatabase::query($sql_field, $args), $cols);
153
154 foreach ( $fields as $field )
155 {
156 $sql_value = new MSQL(
157 'value',
158 'miolo_custom_value',
159 'customized_id = ? AND
160 custom_field_id = ?'
161 );
162
163 $get = self::getBusiness()->query($sql_value, array($this->customizedId, $field['id']));
164
165 if ( count($get->result) == 0 )
166 {
167 $result = $this->insert($field['id']);
168 }
169 else
170 {
171 $values = array('value' => $this->value);
172 $where = array('customized_id' => $this->customizedId, 'custom_field_id' => $field['id']);
173 $sql = MSQL::updateTable('miolo_custom_value', $values, $where);
174
175 $result = self::getBusiness()->Execute($sql);
176 }
177 }
178 return $result;
179 }
180
186 public function delete()
187 {
188 if ( !isset($this->id) )
189 {
190 return false;
191 }
192
193 $sql = new MSQL('', 'miolo_custom_value', 'id = ?');
194 return self::getBusiness()->execute($sql->delete($this->id));
195 }
196
204 public static function getFieldValues($identifier, $customizedId)
205 {
206 $sql = new MSQL(
207 'miolo_custom_field.id,
208 miolo_custom_value.value',
209 '',
210 'miolo_custom_value.customized_id = ?'
211 );
212
213 $sql->setLeftJoin(
214 'miolo_custom_value',
215 'miolo_custom_field',
216 'miolo_custom_value.custom_field_id = miolo_custom_field.id AND
217 miolo_custom_field.identifier = ?'
218 );
219
220 $args = array(
221 $identifier,
223 );
224
225 $query = self::getBusiness()->query($sql, $args);
226
227 $values = new stdClass();
228
229 if ( $query->result )
230 {
231 foreach ( $query->result as $row )
232 {
233 list($id, $value) = $row;
234 $inputId = MCustomField::generateInputId($identifier, $id);
235
236 $values->{$inputId} = $value;
237 }
238 }
239
240 return $values;
241 }
242
250 public static function searchFieldValues($identifier, $searchData, $returnSql = false, $onlyCustomizedid = false, $filters = null)
251 {
253 $db = $MIOLO->getDatabase('admin');
254
255 $where = array();
256
257 // Adicionados elementos de filtragem para buscar apenas resultados vinculados aos registros filtrados no formulário.
258 if ( $filters )
259 {
260 $finalWhere = array();
261 if ($filters->filtroFormularioBusca)
262 {
263 $finalWhere[] = 'miolo_custom_field.filtroformulariobusca IS ' . ($filters->filtroFormularioBusca ? 'true' : 'false');
264 }
265
266 if ( count($filters->customizedIds) > 0 )
267 {
268 $customizedIds = "'" . implode("','", $filters->customizedIds) . "'";
269 $finalWhere[] = 'miolo_custom_value.customized_id IN (' . $customizedIds . ')';
270 }
271 }
272
273 foreach ( $searchData as $id => $value )
274 {
275 if ( $id && $value )
276 {
277 $value = addslashes($value);
278 $where[] = " ( custom_field_id = '{$id}' AND value ILIKE '{$value}%' ) ";
279 }
280 }
281
282 $countWhere = count($where);
283
284 if ( $countWhere == 0 )
285 {
286 $strWhere2 = '';
287 }
288 else
289 {
290 $strWhere1 = ' WHERE ' . implode(' OR ', $where);
291 $finalWhere[] = " customized_id IN (SELECT customized_id
292 FROM miolo_custom_value
293 INNER JOIN miolo_custom_field
294 ON custom_field_id = miolo_custom_field.id
295 AND miolo_custom_field.identifier = '{$identifier}'
296 {$strWhere1}
297 GROUP BY customized_id
298 HAVING COUNT(*) = {$countWhere}) ";
299 }
300
301 //
302 $sqlWhere = implode(' AND ', $finalWhere);
303
304 if ($onlyCustomizedid)
305 {
306 $columns = " customized_id ";
307 }
308 else
309 {
310 $columns = " miolo_custom_value.id,
311 customized_id,
312 custom_field_id,
313 value ";
314 }
315
316 $sql = "SELECT {$columns}
317 FROM miolo_custom_value
318 INNER JOIN miolo_custom_field
319 ON custom_field_id = miolo_custom_field.id
320 AND miolo_custom_field.identifier = '{$identifier}'
321 AND {$sqlWhere}
322 ORDER BY miolo_custom_value.id";
323
324 if ($returnSql)
325 {
326 $sql = (count($finalWhere) > 0 ? $sql : '');
327 return $sql;
328 }
329 else if ( $filters->associative )
330 {
331 return SDatabase::queryAssociative($sql);
332 }
333 else
334 {
335 return $db->query($sql);
336 }
337 }
338
344 public function deleteByCustomizedId()
345 {
346 $sql = new MSQL('', 'miolo_custom_value', 'customized_id = ? and custom_field_id = ?');
347 return self::getBusiness()->execute($sql->delete(array($this->customizedId, $this->customFieldId)));
348 }
349
350 public static function getMioloCustomValueByCustomized($customizedId, $mioloCustomFieldId)
351 {
353 $sql = 'SELECT value
354 FROM miolo_custom_value
355 WHERE customized_id = ?
356 AND custom_field_id = ?';
357
358 $args = [
359 $customizedId,
360 $mioloCustomFieldId,
361 ];
362
363 return $MIOLO->getDatabase()->query(SAGU::prepare($sql, $args));
364 }
365}
static generateInputId($identifier, $id, $suffix='')
update($customFieldId=null)
insert($id=null)
static getFieldValues($identifier, $customizedId)
static searchFieldValues($identifier, $searchData, $returnSql=false, $onlyCustomizedid=false, $filters=null)
__construct($data=NULL)
static getMioloCustomValueByCustomized($customizedId, $mioloCustomFieldId)
static getInstance()
Definição miolo.class:134
Definição msql.class:8
static updateTable($tableName, array $values, array $where)
Definição msql.class:850
$id
Definição base.php:5