Files

111 lines
3.2 KiB
PHP

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