Modifiche all'helper di datatables per permettere la creazione al volo dei bottoni nell'area superiore della tabella
141 lines
4.2 KiB
PHP
141 lines
4.2 KiB
PHP
<?php
|
|
/*
|
|
Author: Riccardo Di Dato
|
|
Creation Date: 14/gen/2016
|
|
*/
|
|
|
|
class DatatablesHelper {
|
|
private static $instCount = 0;
|
|
|
|
public static function getResult($useModel,$permissionFlagRequired, array $exposedModel){
|
|
$ret = new stdClass();
|
|
$ret->draw = $_POST["draw"];
|
|
$ret->recordsTotal = 0;
|
|
$ret->recordsFiltered = 0;
|
|
$ret->data = array();
|
|
|
|
if (AdminLoginManager::isLogged()){
|
|
$admin = AdminLoginManager::getLogged();
|
|
if ($admin->hasRole($permissionFlagRequired)){
|
|
$dao = GlobalVariables::get("dao");
|
|
$filter = array();
|
|
|
|
$options = array('limit'=>$_POST["length"], 'skip'=>$_POST["start"]);
|
|
if (sizeof($_POST["order"])>0){
|
|
foreach ($_POST["order"] as $details){
|
|
$key = $_POST["columns"][$details["column"]]["data"];
|
|
$options["sort"][$key] = strcasecmp($details["dir"], "asc")==0?1:-1;
|
|
}
|
|
}
|
|
|
|
$models = $dao->query($useModel, $filter, $options);
|
|
|
|
if (sizeof($models)>0){
|
|
foreach ($models as $model){
|
|
$result = self::getObjectFromFieldsArray($model, $exposedModel);
|
|
|
|
$ret->data[] = $result;
|
|
}
|
|
}
|
|
$ret->recordsTotal = $dao->count($useModel);
|
|
$ret->recordsFiltered = $dao->count($useModel,$filter);
|
|
}
|
|
}
|
|
return $ret;
|
|
}
|
|
|
|
private static function getObjectFromFieldsArray($obj,array $fieldArr){
|
|
$rval = new stdClass();
|
|
if (sizeof($fieldArr)>0){
|
|
foreach ($fieldArr as $key=>$val){
|
|
if (is_array($val)){
|
|
$rval->$key = self::getObjectFromFieldsArray($obj->$key, $val);
|
|
}
|
|
else {
|
|
$rval->$key = $obj->$key;
|
|
}
|
|
}
|
|
}
|
|
return $rval;
|
|
}
|
|
|
|
public static function generateTable($backendPage, array $columns, array $htmlOpts = array()){
|
|
$tabId = "datatable_".self::$instCount;
|
|
$toolbarClass = "toolbar_".self::$instCount++;
|
|
|
|
$buttonDiv = "";
|
|
$buttonJS = "";
|
|
$useButtons = false;
|
|
if (array_key_exists("buttons",$htmlOpts) && is_array($htmlOpts["buttons"]) && sizeof($htmlOpts["buttons"])>0){
|
|
$useButtons = true;
|
|
$i = 0;
|
|
foreach ($htmlOpts["buttons"] as $buttonData){
|
|
$buttonDiv.='<span id="'.$tabId."btn".$i.'" class="'.$buttonData["icon"].'" > </span>';
|
|
$buttonJs.='$("#'.$tabId."btn".$i.'").click(function(){'.
|
|
'if ($("tr.selected").length>0){'.
|
|
'var useForm = $("div.' . $toolbarClass . ' form");'.
|
|
'useForm.attr("action","'.$buttonData["page"].'").find("input").val($("tr.selected td:last-child").html());'.
|
|
'useForm.submit();'.
|
|
'}'.
|
|
'});';
|
|
}
|
|
$buttonDiv.='<form action="" style="display:none;"><input type="hidden" name="id" value=""/></form>';
|
|
}
|
|
|
|
|
|
$columns[] = array("label"=>"id","data"=>"id");
|
|
|
|
$colLabels = "<tr>";
|
|
$jsCols = array();
|
|
foreach ($columns as $col){
|
|
$colLabels.= '<th>'.$col["label"].'</th>';
|
|
|
|
$jsCols[]= '{"data":"'.$col["data"].'"}';
|
|
}
|
|
$colLabels.= '</tr>';
|
|
|
|
echo '<table id="'.$tabId.'"><thead>'.$colLabels.'</thead><tfoot>'.$colLabels.'</tfoot><tbody></tbody></table>';
|
|
|
|
?>
|
|
<script type="text/javascript">
|
|
$(document).ready(function(){
|
|
var dTab = $("#<?php echo $tabId;?>").DataTable({
|
|
<?php echo ($useButtons)?'"dom":"<\''.$toolbarClass.'\'>frtip",'."\n":"";?>
|
|
"language": {"url": "<?php echo GUIHandler::getJavascriptsUrl();?>datatables.ita.lang"},
|
|
"processing": true,
|
|
"serverSide": true,
|
|
"ajax": {
|
|
"url": "<?php echo GUIHandler::getBaseUrl();?>admin/tablesBackends/<?php echo $backendPage; ?>",
|
|
"type": "POST"
|
|
},
|
|
"columns": [ <?php echo implode(",", $jsCols);?> ],
|
|
"initComplete": function () {
|
|
$("div.<?php echo $toolbarClass;?>").html('<?php echo $buttonDiv;?>');
|
|
<?php echo $buttonJs;?>
|
|
},
|
|
"columnDefs": [
|
|
{"className":"hidden","targets":[<?php echo sizeof($columns)-1;?>]}
|
|
]
|
|
});
|
|
$('#<?php echo $tabId;?> tbody').on( 'click', 'tr', function () {
|
|
if ( $(this).hasClass('selected') ) {
|
|
$(this).removeClass('selected');
|
|
}
|
|
else {
|
|
dTab.$('tr.selected').removeClass('selected');
|
|
$(this).addClass('selected');
|
|
}
|
|
});
|
|
});
|
|
</script>
|
|
|
|
<?php
|
|
$details = array();
|
|
$details["toolbarClass"] = $toolbarClass;
|
|
$details["tableId"] = $tabId;
|
|
return $details;
|
|
}
|
|
|
|
}
|
|
|
|
?>
|