@@ -1,13 +1,8 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | namespace PSFS\base\types; |
3 | 3 | |
4 | -use CORE\Models\Customer; |
|
5 | 4 | use CORE\Models\Map\CustomerTableMap; |
6 | 5 | use Propel\Runtime\ActiveQuery\ModelCriteria; |
7 | -use Propel\Runtime\ActiveRecord\ActiveRecordInterface; |
|
8 | -use Propel\Runtime\DataFetcher\ArrayDataFetcher; |
|
9 | -use Propel\Runtime\Formatter\ObjectFormatter; |
|
10 | -use Propel\Runtime\Map\ColumnMap; |
|
11 | 6 | use Propel\Runtime\Map\TableMap; |
12 | 7 | use Propel\Runtime\Propel; |
13 | 8 | use PSFS\base\config\Config; |
@@ -15,10 +10,8 @@ discard block |
||
15 | 10 | use PSFS\base\dto\Order; |
16 | 11 | use PSFS\base\Logger; |
17 | 12 | use PSFS\base\Request; |
18 | -use PSFS\base\Security; |
|
19 | 13 | use PSFS\base\Singleton; |
20 | 14 | use PSFS\base\types\helpers\ApiHelper; |
21 | -use PSFS\base\types\helpers\Inspector; |
|
22 | 15 | use PSFS\base\types\traits\Api\ManagerTrait; |
23 | 16 | |
24 | 17 | /** |
@@ -80,15 +80,15 @@ discard block |
||
80 | 80 | public function init() |
81 | 81 | { |
82 | 82 | parent::init(); |
83 | - Logger::log(get_called_class() . ' init', LOG_DEBUG); |
|
83 | + Logger::log(get_called_class().' init', LOG_DEBUG); |
|
84 | 84 | $this->domain = $this->getDomain(); |
85 | 85 | $this->hydrateRequestData(); |
86 | 86 | $this->hydrateOrders(); |
87 | - if($this instanceof CustomApi === false) { |
|
87 | + if ($this instanceof CustomApi === false) { |
|
88 | 88 | $this->createConnection($this->getTableMap()); |
89 | 89 | } |
90 | 90 | $this->setLoaded(true); |
91 | - Logger::log(get_called_class() . ' loaded', LOG_DEBUG); |
|
91 | + Logger::log(get_called_class().' loaded', LOG_DEBUG); |
|
92 | 92 | } |
93 | 93 | |
94 | 94 | /** |
@@ -97,7 +97,7 @@ discard block |
||
97 | 97 | protected function hydrateOrders() |
98 | 98 | { |
99 | 99 | if (count($this->query)) { |
100 | - Logger::log(get_called_class() . ' gathering query string', LOG_DEBUG); |
|
100 | + Logger::log(get_called_class().' gathering query string', LOG_DEBUG); |
|
101 | 101 | foreach ($this->query as $key => $value) { |
102 | 102 | if ($key === self::API_ORDER_FIELD) { |
103 | 103 | foreach ($value as $field => $direction) { |
@@ -114,10 +114,10 @@ discard block |
||
114 | 114 | */ |
115 | 115 | protected function extractPagination() |
116 | 116 | { |
117 | - Logger::log(get_called_class() . ' extract pagination start', LOG_DEBUG); |
|
117 | + Logger::log(get_called_class().' extract pagination start', LOG_DEBUG); |
|
118 | 118 | $page = (array_key_exists(self::API_PAGE_FIELD, $this->query)) ? $this->query[self::API_PAGE_FIELD] : 1; |
119 | 119 | $limit = (array_key_exists(self::API_LIMIT_FIELD, $this->query)) ? $this->query[self::API_LIMIT_FIELD] : 100; |
120 | - Logger::log(get_called_class() . ' extract pagination end', LOG_DEBUG); |
|
120 | + Logger::log(get_called_class().' extract pagination end', LOG_DEBUG); |
|
121 | 121 | return array($page, $limit); |
122 | 122 | } |
123 | 123 | |
@@ -126,9 +126,9 @@ discard block |
||
126 | 126 | * |
127 | 127 | * @param ModelCriteria $query |
128 | 128 | */ |
129 | - private function addOrders(ModelCriteria &$query) |
|
129 | + private function addOrders(ModelCriteria & $query) |
|
130 | 130 | { |
131 | - Logger::log(get_called_class() . ' extract orders start ', LOG_DEBUG); |
|
131 | + Logger::log(get_called_class().' extract orders start ', LOG_DEBUG); |
|
132 | 132 | $orderAdded = FALSE; |
133 | 133 | $tableMap = $this->getTableMap(); |
134 | 134 | foreach ($this->order->getOrders() as $field => $direction) { |
@@ -136,17 +136,17 @@ discard block |
||
136 | 136 | $orderAdded = TRUE; |
137 | 137 | if ($direction === Order::ASC) { |
138 | 138 | $query->addAscendingOrderByColumn($column->getPhpName()); |
139 | - } else { |
|
139 | + }else { |
|
140 | 140 | $query->addDescendingOrderByColumn($column->getPhpName()); |
141 | 141 | } |
142 | 142 | } |
143 | 143 | } |
144 | 144 | if (!$orderAdded) { |
145 | - foreach($this->getPkDbName() as $pk => $phpName) { |
|
145 | + foreach ($this->getPkDbName() as $pk => $phpName) { |
|
146 | 146 | $query->addAscendingOrderByColumn($pk); |
147 | 147 | } |
148 | 148 | } |
149 | - Logger::log(get_called_class() . ' extract orders end', LOG_DEBUG); |
|
149 | + Logger::log(get_called_class().' extract orders end', LOG_DEBUG); |
|
150 | 150 | } |
151 | 151 | |
152 | 152 | /** |
@@ -154,14 +154,14 @@ discard block |
||
154 | 154 | * |
155 | 155 | * @param ModelCriteria $query |
156 | 156 | */ |
157 | - protected function addFilters(ModelCriteria &$query) |
|
157 | + protected function addFilters(ModelCriteria & $query) |
|
158 | 158 | { |
159 | 159 | if (count($this->query) > 0) { |
160 | 160 | $tableMap = $this->getTableMap(); |
161 | 161 | foreach ($this->query as $field => $value) { |
162 | 162 | if (self::API_COMBO_FIELD === $field) { |
163 | 163 | ApiHelper::composerComboField($tableMap, $query, $this->extraColumns, $value); |
164 | - } elseif(!preg_match('/^__/', $field)) { |
|
164 | + } elseif (!preg_match('/^__/', $field)) { |
|
165 | 165 | ApiHelper::addModelField($tableMap, $query, $field, $value); |
166 | 166 | } |
167 | 167 | } |
@@ -182,10 +182,10 @@ discard block |
||
182 | 182 | list($page, $limit) = $this->extractPagination(); |
183 | 183 | if ($limit == -1) { |
184 | 184 | $this->list = $query->find($this->con); |
185 | - } else { |
|
185 | + }else { |
|
186 | 186 | $this->list = $query->paginate($page, $limit, $this->con); |
187 | 187 | } |
188 | - } catch (\Exception $e) { |
|
188 | + }catch (\Exception $e) { |
|
189 | 189 | Logger::log($e->getMessage(), LOG_ERR); |
190 | 190 | } |
191 | 191 | } |
@@ -204,7 +204,7 @@ discard block |
||
204 | 204 | $code = 200; |
205 | 205 | list($return, $total, $pages) = $this->getList(); |
206 | 206 | $message = null; |
207 | - if(!$total) { |
|
207 | + if (!$total) { |
|
208 | 208 | $message = _('No se han encontrado elementos para la búsqueda'); |
209 | 209 | } |
210 | 210 | |
@@ -230,7 +230,7 @@ discard block |
||
230 | 230 | $pages = 1; |
231 | 231 | $message = null; |
232 | 232 | list($code, $return) = $this->getSingleResult($pk); |
233 | - if($code !== 200) { |
|
233 | + if ($code !== 200) { |
|
234 | 234 | $message = _('No se ha encontrado el elemento solicitado'); |
235 | 235 | } |
236 | 236 | |
@@ -259,11 +259,11 @@ discard block |
||
259 | 259 | $status = 200; |
260 | 260 | $saved = TRUE; |
261 | 261 | $model = $this->model->toArray(); |
262 | - } else { |
|
262 | + }else { |
|
263 | 263 | $message = _('No se ha podido guardar el modelo seleccionado'); |
264 | 264 | } |
265 | - } catch (\Exception $e) { |
|
266 | - $message = _('Ha ocurrido un error intentando guardar el elemento: ') .'<br>'. $e->getMessage(); |
|
265 | + }catch (\Exception $e) { |
|
266 | + $message = _('Ha ocurrido un error intentando guardar el elemento: ').'<br>'.$e->getMessage(); |
|
267 | 267 | Logger::log($e->getMessage(), LOG_ERR); |
268 | 268 | } |
269 | 269 | |
@@ -297,14 +297,14 @@ discard block |
||
297 | 297 | $updated = TRUE; |
298 | 298 | $status = 200; |
299 | 299 | $model = $this->model->toArray(); |
300 | - } else { |
|
300 | + }else { |
|
301 | 301 | $message = _('Ha ocurrido un error intentando actualizar el elemento, por favor revisa los logs'); |
302 | 302 | } |
303 | - } catch (\Exception $e) { |
|
303 | + }catch (\Exception $e) { |
|
304 | 304 | $message = $e->getMessage(); |
305 | 305 | Logger::getInstance(get_class($this->model))->errorLog($e->getMessage()); |
306 | 306 | } |
307 | - } else { |
|
307 | + }else { |
|
308 | 308 | $message = _('No se ha encontrado el modelo al que se hace referencia para actualizar'); |
309 | 309 | } |
310 | 310 | |
@@ -332,13 +332,13 @@ discard block |
||
332 | 332 | $this->con->beginTransaction(); |
333 | 333 | $this->hydrateModel($pk); |
334 | 334 | if (NULL !== $this->model) { |
335 | - if(method_exists('clearAllReferences', $this->model)) { |
|
335 | + if (method_exists('clearAllReferences', $this->model)) { |
|
336 | 336 | $this->model->clearAllReferences(true); |
337 | 337 | } |
338 | 338 | $this->model->delete($this->con); |
339 | 339 | $deleted = TRUE; |
340 | 340 | } |
341 | - } catch (\Exception $e) { |
|
341 | + }catch (\Exception $e) { |
|
342 | 342 | $message = _('Ha ocurrido un error intentando eliminar el elemento, por favor verifica que no tenga otros elementos relacionados'); |
343 | 343 | Logger::getInstance(get_class($this->model))->errorLog($e->getMessage()); |
344 | 344 | } |
@@ -366,7 +366,7 @@ discard block |
||
366 | 366 | $this->saveBulk(); |
367 | 367 | $saved = true; |
368 | 368 | $status = 200; |
369 | - } catch(\Exception $e) { |
|
369 | + }catch (\Exception $e) { |
|
370 | 370 | Logger::log($e->getMessage(), LOG_ERR, $this->getRequest()->getData()); |
371 | 371 | $message = _('Bulk insert rolled back'); |
372 | 372 | } |
@@ -388,7 +388,7 @@ discard block |
||
388 | 388 | |
389 | 389 | /** @var CustomerTableMap $tableMap */ |
390 | 390 | $modelPk = ApiHelper::extractPrimaryKeyColumnName($this->getTableMap()); |
391 | - foreach($this->list->getResults() as $data) { |
|
391 | + foreach ($this->list->getResults() as $data) { |
|
392 | 392 | $return[] = ApiHelper::mapArrayObject($this->getModelNamespace(), $modelPk, $this->query, $data); |
393 | 393 | } |
394 | 394 | return $return; |
@@ -405,16 +405,16 @@ discard block |
||
405 | 405 | try { |
406 | 406 | $this->paginate(); |
407 | 407 | if (null !== $this->list) { |
408 | - if(array_key_exists(self::API_FIELDS_RESULT_FIELD, $this->query) && Config::getParam('api.field.types')) { |
|
408 | + if (array_key_exists(self::API_FIELDS_RESULT_FIELD, $this->query) && Config::getParam('api.field.types')) { |
|
409 | 409 | $return = $this->extractDataWithFormat(); |
410 | - } else { |
|
410 | + }else { |
|
411 | 411 | $return = $this->list->toArray(null, false, TableMap::TYPE_PHPNAME, false); |
412 | 412 | } |
413 | 413 | $total = $this->list->getNbResults(); |
414 | 414 | $pages = $this->list->getLastPage(); |
415 | 415 | } |
416 | - } catch (\Exception $e) { |
|
417 | - Logger::log(get_class($this) . ': ' . $e->getMessage(), LOG_ERR); |
|
416 | + }catch (\Exception $e) { |
|
417 | + Logger::log(get_class($this).': '.$e->getMessage(), LOG_ERR); |
|
418 | 418 | } |
419 | 419 | |
420 | 420 | return array($return, $total, $pages); |
@@ -432,7 +432,7 @@ discard block |
||
432 | 432 | $return = array(); |
433 | 433 | if (NULL === $model || !method_exists($model, 'toArray')) { |
434 | 434 | $code = 404; |
435 | - } else { |
|
435 | + }else { |
|
436 | 436 | $return = $model->toArray(); |
437 | 437 | } |
438 | 438 |
@@ -35,19 +35,19 @@ discard block |
||
35 | 35 | $behaviors = $tableMap->getBehaviors(); |
36 | 36 | foreach ($map::getFieldNames() as $field) { |
37 | 37 | $fDto = self::parseFormField($domain, $tableMap, $field, $behaviors); |
38 | - if(null !== $fDto) { |
|
38 | + if (null !== $fDto) { |
|
39 | 39 | $form->addField($fDto); |
40 | 40 | } |
41 | 41 | } |
42 | 42 | |
43 | - if(array_key_exists('i18n', $behaviors)) { |
|
44 | - $relateI18n = $tableMap->getRelation($tableMap->getPhpName() . 'I18n'); |
|
45 | - if(null !== $relateI18n) { |
|
43 | + if (array_key_exists('i18n', $behaviors)) { |
|
44 | + $relateI18n = $tableMap->getRelation($tableMap->getPhpName().'I18n'); |
|
45 | + if (null !== $relateI18n) { |
|
46 | 46 | $i18NTableMap = $relateI18n->getLocalTable(); |
47 | - foreach($i18NTableMap->getColumns() as $columnMap) { |
|
48 | - if(!$form->fieldExists($columnMap->getPhpName())) { |
|
47 | + foreach ($i18NTableMap->getColumns() as $columnMap) { |
|
48 | + if (!$form->fieldExists($columnMap->getPhpName())) { |
|
49 | 49 | $fDto = self::parseFormField($domain, $i18NTableMap, $columnMap->getPhpName(), $i18NTableMap->getBehaviors()); |
50 | - if(null !== $fDto) { |
|
50 | + if (null !== $fDto) { |
|
51 | 51 | $fDto->pk = false; |
52 | 52 | $form->addField($fDto); |
53 | 53 | } |
@@ -76,7 +76,7 @@ discard block |
||
76 | 76 | $fDto->entity = $relatedModel; |
77 | 77 | $relatedField = $foreignTable->getColumn($mappedColumn->getRelatedColumnName()); |
78 | 78 | $fDto->relatedField = $relatedField->getPhpName(); |
79 | - $fDto->url = Router::getInstance()->getRoute(strtolower($domain) . '-api-' . $relatedModel); |
|
79 | + $fDto->url = Router::getInstance()->getRoute(strtolower($domain).'-api-'.$relatedModel); |
|
80 | 80 | return $fDto; |
81 | 81 | } |
82 | 82 | |
@@ -184,7 +184,7 @@ discard block |
||
184 | 184 | $column = null; |
185 | 185 | try { |
186 | 186 | $column = $tableMap->getColumnByPhpName($field); |
187 | - } catch (\Exception $e) { |
|
187 | + }catch (\Exception $e) { |
|
188 | 188 | Logger::log($e->getMessage(), LOG_WARNING); |
189 | 189 | //foreach($tableMap->getRelations() as $relation) { |
190 | 190 | // $column = self::checkFieldExists($relation->getLocalTable(), $field); |
@@ -198,7 +198,7 @@ discard block |
||
198 | 198 | * @param ModelCriteria $query |
199 | 199 | * @param mixed $value |
200 | 200 | */ |
201 | - private static function addQueryFilter(ColumnMap $column, ModelCriteria &$query, $value = null) |
|
201 | + private static function addQueryFilter(ColumnMap $column, ModelCriteria & $query, $value = null) |
|
202 | 202 | { |
203 | 203 | $tableField = $column->getFullyQualifiedName(); |
204 | 204 | if (preg_match('/^\[/', $value) && preg_match('/\]$/', $value)) { |
@@ -206,11 +206,11 @@ discard block |
||
206 | 206 | } elseif (preg_match('/^(\'|\")(.*)(\'|\")$/', $value)) { |
207 | 207 | $text = preg_replace('/(\'|\")/', '', $value); |
208 | 208 | $text = preg_replace('/\ /', '%', $text); |
209 | - $query->add($tableField, '%' . $text . '%', Criteria::LIKE); |
|
210 | - } else { |
|
211 | - if(null !== $column->getValueSet()) { |
|
209 | + $query->add($tableField, '%'.$text.'%', Criteria::LIKE); |
|
210 | + }else { |
|
211 | + if (null !== $column->getValueSet()) { |
|
212 | 212 | $valueSet = $column->getValueSet(); |
213 | - if(in_array($value, $valueSet)) { |
|
213 | + if (in_array($value, $valueSet)) { |
|
214 | 214 | $value = array_search($value, $valueSet); |
215 | 215 | } |
216 | 216 | } |
@@ -224,35 +224,35 @@ discard block |
||
224 | 224 | * @param array $extraColumns |
225 | 225 | * @param mixed $value |
226 | 226 | */ |
227 | - public static function composerComboField(TableMap $tableMap, ModelCriteria &$query, array $extraColumns = [], $value = null) |
|
227 | + public static function composerComboField(TableMap $tableMap, ModelCriteria & $query, array $extraColumns = [], $value = null) |
|
228 | 228 | { |
229 | 229 | $exp = 'CONCAT('; |
230 | 230 | $sep = ''; |
231 | 231 | foreach ($tableMap->getColumns() as $column) { |
232 | 232 | if ($column->isText()) { |
233 | - $exp .= $sep . 'IFNULL(' . $column->getFullyQualifiedName() . ',"")'; |
|
233 | + $exp .= $sep.'IFNULL('.$column->getFullyQualifiedName().',"")'; |
|
234 | 234 | $sep = ', " ", '; |
235 | 235 | } |
236 | 236 | } |
237 | - foreach($tableMap->getRelations() as $relation) { |
|
238 | - if(preg_match('/I18n$/i', $relation->getName())) { |
|
237 | + foreach ($tableMap->getRelations() as $relation) { |
|
238 | + if (preg_match('/I18n$/i', $relation->getName())) { |
|
239 | 239 | $localeTableMap = $relation->getLocalTable(); |
240 | 240 | foreach ($localeTableMap->getColumns() as $column) { |
241 | 241 | if ($column->isText()) { |
242 | - $exp .= $sep . 'IFNULL(' . $column->getFullyQualifiedName() . ',"")'; |
|
242 | + $exp .= $sep.'IFNULL('.$column->getFullyQualifiedName().',"")'; |
|
243 | 243 | $sep = ', " ", '; |
244 | 244 | } |
245 | 245 | } |
246 | 246 | } |
247 | 247 | } |
248 | 248 | foreach ($extraColumns as $extra => $name) { |
249 | - $exp .= $sep . $extra; |
|
249 | + $exp .= $sep.$extra; |
|
250 | 250 | $sep = ', " ", '; |
251 | 251 | } |
252 | 252 | $exp .= ")"; |
253 | 253 | $text = preg_replace('/(\'|\")/', '', $value); |
254 | 254 | $text = preg_replace('/\ /', '%', $text); |
255 | - $query->where($exp . Criteria::LIKE . '"%' . $text . '%"'); |
|
255 | + $query->where($exp.Criteria::LIKE.'"%'.$text.'%"'); |
|
256 | 256 | } |
257 | 257 | |
258 | 258 | /** |
@@ -262,7 +262,7 @@ discard block |
||
262 | 262 | * @param string $field |
263 | 263 | * @param mixed $value |
264 | 264 | */ |
265 | - public static function addModelField(TableMap $tableMap, ModelCriteria &$query, $field, $value = null) |
|
265 | + public static function addModelField(TableMap $tableMap, ModelCriteria & $query, $field, $value = null) |
|
266 | 266 | { |
267 | 267 | if ($column = self::checkFieldExists($tableMap, $field)) { |
268 | 268 | self::addQueryFilter($column, $query, $value); |
@@ -277,7 +277,7 @@ discard block |
||
277 | 277 | */ |
278 | 278 | public static function extractQuery($modelNameNamespace, ConnectionInterface $con = null) |
279 | 279 | { |
280 | - $queryReflector = new \ReflectionClass($modelNameNamespace . "Query"); |
|
280 | + $queryReflector = new \ReflectionClass($modelNameNamespace."Query"); |
|
281 | 281 | /** @var \Propel\Runtime\ActiveQuery\ModelCriteria $query */ |
282 | 282 | $query = $queryReflector->getMethod('create')->invoke($con); |
283 | 283 | |
@@ -306,7 +306,7 @@ discard block |
||
306 | 306 | } elseif ($mappedColumn->isText()) { |
307 | 307 | if ($mappedColumn->getSize() > 100) { |
308 | 308 | $fDto = self::createField($field, Field::TEXTAREA_TYPE, $required); |
309 | - } else { |
|
309 | + }else { |
|
310 | 310 | $fDto = self::generateStringField($field, $required); |
311 | 311 | } |
312 | 312 | } elseif ($mappedColumn->getType() === PropelTypes::BOOLEAN) { |
@@ -343,7 +343,7 @@ discard block |
||
343 | 343 | */ |
344 | 344 | public static function extractPrimaryKeyColumnName(TableMap $tableMap) { |
345 | 345 | $modelPk = null; |
346 | - foreach($tableMap->getPrimaryKeys() as $pk) { |
|
346 | + foreach ($tableMap->getPrimaryKeys() as $pk) { |
|
347 | 347 | $modelPk = $pk; |
348 | 348 | break; |
349 | 349 | } |
@@ -352,15 +352,15 @@ discard block |
||
352 | 352 | |
353 | 353 | private static function mapResult(ActiveRecordInterface $model, array $data = []) { |
354 | 354 | $result = []; |
355 | - foreach($data as $key => $value) { |
|
355 | + foreach ($data as $key => $value) { |
|
356 | 356 | try { |
357 | 357 | $realValue = $model->getByName($key); |
358 | - } catch(\Exception $e) { |
|
358 | + }catch (\Exception $e) { |
|
359 | 359 | $realValue = $value; |
360 | 360 | } |
361 | - if(Api::API_MODEL_KEY_FIELD === $key) { |
|
361 | + if (Api::API_MODEL_KEY_FIELD === $key) { |
|
362 | 362 | $result[$key] = (integer)$realValue; |
363 | - } else { |
|
363 | + }else { |
|
364 | 364 | $result[$key] = $realValue; |
365 | 365 | } |
366 | 366 | } |
@@ -376,7 +376,7 @@ discard block |
||
376 | 376 | /** @var ActiveRecordInterface $obj */ |
377 | 377 | $obj = $formatter->getAllObjectsFromRow($data); |
378 | 378 | $result = self::mapResult($obj, $data); |
379 | - if(!preg_match('/' . $modelPk->getPhpName() . '/i', $query[Api::API_FIELDS_RESULT_FIELD])) { |
|
379 | + if (!preg_match('/'.$modelPk->getPhpName().'/i', $query[Api::API_FIELDS_RESULT_FIELD])) { |
|
380 | 380 | unset($result[$modelPk->getPhpName()]); |
381 | 381 | } |
382 | 382 | return $result; |