Forráskód Böngészése

Plugin to help on datables creation

ahwelp 2 éve
szülő
commit
491167554f

+ 110 - 0
app/core/datatables/Datatables.php

@@ -0,0 +1,110 @@
+<?php
+
+namespace App\Core\Datatables;
+
+class Datatables {
+
+    private $classObject;
+
+    public function __construct($objectType) {
+
+    }
+
+    public static function getTable($class, $baseUrl, $list = true){
+
+        $header = "";
+        $headerItems = $class::_listable;
+
+        // If the class do not have a _datatables property.
+        // Avoid it from dying
+        try{
+            $headerItems = $class::_datatables;
+        }catch(\Error $e){
+            $headerItems = $class::_listable;
+        }
+
+        foreach($headerItems as $element){
+            $header .= "<th data-field='$element' ";
+            if(!in_array($element, $class::_orderable)){
+                $header .= "data-orderable='false'";
+            }
+            $header .= ">".\Lang::getString($element, $baseUrl)."</th>\n";
+        }
+        $content = "";
+
+        if($list){
+            $content = <<<EOL
+            <table class='table datatable' data-src="/$baseUrl/table?selective=true" style="width: 100%">
+            <thead>
+                <tr>
+                    $header
+                </tr>
+            </thead>
+            </table>
+            EOL;
+        }else{
+            $content = <<<EOL
+            <table class='table datatable' data-src="/$baseUrl/table?selective=false" style="width: 100%">
+            <thead>
+                <tr>
+                    $header
+                </tr>
+            </thead>
+            </table>
+            EOL;
+        }
+        return $content;
+    }
+
+    public static function getActionMenu($id, $urlBase, $actions = []) {
+
+        $content = "";
+
+        if($actions){
+            $content = <<<EOL
+            <div class="dropdown">
+                <button class="btn btn-secondary dropdown-toggle btn-sm" type="button" data-bs-toggle="dropdown" aria-expanded="false">
+                    Ações
+                </button>
+                <ul class="dropdown-menu">
+            EOL;
+            foreach($actions as $key => $action){
+                $content .= self::formatMenuItem($id, $urlBase, $key, $action, \Lang::getString($key, $urlBase));
+            }
+            $content.= <<<EOL
+                </ul>
+            </div>
+            EOL;
+
+        }else{
+            $content = <<<EOL
+            <div class="dropdown">
+                <button class="btn btn-secondary dropdown-toggle btn-sm" type="button" data-bs-toggle="dropdown" aria-expanded="false">
+                    Ações
+                </button>
+                <ul class="dropdown-menu">
+                    <li><a class="dropdown-item" href="$urlBase/$id/edit">Editar</a></li>
+                </ul>
+            </div>
+            EOL;
+        }
+
+        return $content;
+    }
+
+    private static function formatMenuItem($id, $urlBase, $key, $action, $label = ''){
+        $content = "<li><a class='dropdown-item' href='$action'>$label</a></li>";
+        $content = str_replace('{id}', $id, $content);
+        $content = str_replace('{base}', $urlBase, $content);
+        $content = str_replace('{action}', $key, $content);
+        return $content;
+    }
+
+    public static function getSelectMenu($id) {
+        $content = <<<EOL
+        <button class="btn btn-secondary btn-sm" data-id='$id'> Selecionar </button>
+        EOL;
+        return $content;
+    }
+
+}

+ 11 - 0
app/core/datatables/DatatablesController.php

@@ -0,0 +1,11 @@
+<?php
+
+namespace App\Core\Datatables;
+
+class DatatablesController{
+
+    public static function searchTable(){
+        
+    }
+
+}

+ 9 - 0
app/core/datatables/DatatablesInterface.php

@@ -0,0 +1,9 @@
+<?php
+
+namespace App\Core\Datatables;
+
+interface DatatablesListable{
+
+    
+
+}

+ 2 - 0
app/core/datatables/lang/en.php

@@ -0,0 +1,2 @@
+<?php
+    $lang["datatables"]["module_name"] = "Datatables";

+ 2 - 0
app/core/datatables/lang/pt_br.php

@@ -0,0 +1,2 @@
+<?php
+    $lang["datatables"]["module_name"] = "Datatables";

+ 8 - 0
app/core/datatables/routes.php

@@ -0,0 +1,8 @@
+<?php
+
+use Routes\RouteCollection as RouteCollection;
+
+RouteCollection::group("/datatables", function(){
+    RouteCollection::get("/table", "\App\Core\Datatables\DatatablesController@searchTable")->middlewareIgnore('auth');
+    RouteCollection::get("/tableData", "\App\Core\Datatables\DatatablesController@searchTable")->middlewareIgnore('auth');
+});

+ 4 - 0
app/core/datatables/views/index.mustache

@@ -0,0 +1,4 @@
+<a href='/datatables/form' >Adicionar </a>
+<div class="table-responsive">
+
+</div>