api de gestion de ticket, basé sur php-crud-api. Le but est de décorrélé les outils de gestion des données, afin
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

OrderingInfo.php 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. namespace Tqdev\PhpCrudApi\Record;
  3. use Tqdev\PhpCrudApi\Column\Reflection\ReflectedTable;
  4. class OrderingInfo
  5. {
  6. public function getColumnOrdering(ReflectedTable $table, array $params): array
  7. {
  8. $fields = array();
  9. if (isset($params['order'])) {
  10. foreach ($params['order'] as $order) {
  11. $parts = explode(',', $order, 3);
  12. $columnName = $parts[0];
  13. if (!$table->hasColumn($columnName)) {
  14. continue;
  15. }
  16. $ascending = 'ASC';
  17. if (count($parts) > 1) {
  18. if (substr(strtoupper($parts[1]), 0, 4) == "DESC") {
  19. $ascending = 'DESC';
  20. }
  21. }
  22. $fields[] = [$columnName, $ascending];
  23. }
  24. }
  25. if (count($fields) == 0) {
  26. return $this->getDefaultColumnOrdering($table);
  27. }
  28. return $fields;
  29. }
  30. public function getDefaultColumnOrdering(ReflectedTable $table): array
  31. {
  32. $fields = array();
  33. $pk = $table->getPk();
  34. if ($pk) {
  35. $fields[] = [$pk->getName(), 'ASC'];
  36. } else {
  37. foreach ($table->getColumnNames() as $columnName) {
  38. $fields[] = [$columnName, 'ASC'];
  39. }
  40. }
  41. return $fields;
  42. }
  43. }