| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- <?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;
- }
- }
|