Datatables.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace App\Core\Datatables;
  3. class Datatables {
  4. private $classObject;
  5. public function __construct($objectType) {
  6. }
  7. public static function getTable($class, $baseUrl, $list = true){
  8. $header = "";
  9. $headerItems = $class::_listable;
  10. // If the class do not have a _datatables property.
  11. // Avoid it from dying
  12. try{
  13. $headerItems = $class::_datatables;
  14. }catch(\Error $e){
  15. $headerItems = $class::_listable;
  16. }
  17. foreach($headerItems as $element){
  18. $header .= "<th data-field='$element' ";
  19. if(!in_array($element, $class::_orderable)){
  20. $header .= "data-orderable='false'";
  21. }
  22. $header .= ">".\Lang::getString($element, $baseUrl)."</th>\n";
  23. }
  24. $content = "";
  25. if($list){
  26. $content = <<<EOL
  27. <table class='table datatable' data-src="/$baseUrl/table?selective=true" style="width: 100%">
  28. <thead>
  29. <tr>
  30. $header
  31. </tr>
  32. </thead>
  33. </table>
  34. EOL;
  35. }else{
  36. $content = <<<EOL
  37. <table class='table datatable' data-src="/$baseUrl/table?selective=false" style="width: 100%">
  38. <thead>
  39. <tr>
  40. $header
  41. </tr>
  42. </thead>
  43. </table>
  44. EOL;
  45. }
  46. return $content;
  47. }
  48. public static function getActionMenu($id, $urlBase, $actions = []) {
  49. $content = "";
  50. if($actions){
  51. $content = <<<EOL
  52. <div class="dropdown">
  53. <button class="btn btn-secondary dropdown-toggle btn-sm" type="button" data-bs-toggle="dropdown" aria-expanded="false">
  54. Ações
  55. </button>
  56. <ul class="dropdown-menu">
  57. EOL;
  58. foreach($actions as $key => $action){
  59. $content .= self::formatMenuItem($id, $urlBase, $key, $action, \Lang::getString($key, $urlBase));
  60. }
  61. $content.= <<<EOL
  62. </ul>
  63. </div>
  64. EOL;
  65. }else{
  66. $content = <<<EOL
  67. <div class="dropdown">
  68. <button class="btn btn-secondary dropdown-toggle btn-sm" type="button" data-bs-toggle="dropdown" aria-expanded="false">
  69. Ações
  70. </button>
  71. <ul class="dropdown-menu">
  72. <li><a class="dropdown-item" href="$urlBase/$id/edit">Editar</a></li>
  73. </ul>
  74. </div>
  75. EOL;
  76. }
  77. return $content;
  78. }
  79. private static function formatMenuItem($id, $urlBase, $key, $action, $label = ''){
  80. $content = "<li><a class='dropdown-item' href='$action'>$label</a></li>";
  81. $content = str_replace('{id}', $id, $content);
  82. $content = str_replace('{base}', $urlBase, $content);
  83. $content = str_replace('{action}', $key, $content);
  84. return $content;
  85. }
  86. public static function getSelectMenu($id) {
  87. $content = <<<EOL
  88. <button class="btn btn-secondary btn-sm" data-id='$id'> Selecionar </button>
  89. EOL;
  90. return $content;
  91. }
  92. }