Cambiata gui exception per estendere MultiMessageException Implementata logging degli accessi all'applicazione Modificato loginmanager per loggare gli accessi Creata classe d'appoggio per generare il testo delle email Creato stile di base per le form dell'amministrazione Bugfix e migliorie grafiche
165 lines
4.8 KiB
PHP
165 lines
4.8 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, array $dataFilter = array()){
|
|
$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 = $dataFilter;
|
|
|
|
$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 {
|
|
if ($obj->$key instanceof MongoDate){
|
|
$rval->$key = date("d-m-Y H:i",$obj->$key->sec);
|
|
}
|
|
else {
|
|
$rval->$key = $obj->$key;
|
|
}
|
|
if (is_callable($val)){
|
|
$rval->$key = $val($rval->$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();'.
|
|
'}'.
|
|
'});';
|
|
$i++;
|
|
}
|
|
$buttonDiv.='<form action="" method="POST" style="display:none;"><input type="hidden" name="id" value=""/></form>';
|
|
}
|
|
|
|
|
|
$columns[] = array("label"=>"id","data"=>"id");
|
|
|
|
$colLabels = "<tr>";
|
|
$jsCols = array();
|
|
$i=0;
|
|
foreach ($columns as $col){
|
|
$colLabels.= '<th>'.$col["label"].'</th>';
|
|
|
|
$jsCols[]= '{"data":"'.$col["data"].'"}';
|
|
|
|
if (array_key_exists("sort",$col)){
|
|
$defaultOrder[]= '['.$i.',"'.$col["sort"].'"]';
|
|
}
|
|
|
|
|
|
$i++;
|
|
}
|
|
$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 '"dom":"<\'toolbar '.$toolbarClass.'\' f>rtip",'."\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);?> ],
|
|
<?php
|
|
if (sizeof($defaultOrder)>0){
|
|
echo '"order": ['. implode(",", $defaultOrder) . '],';
|
|
}
|
|
?>
|
|
"initComplete": function () {
|
|
$("div.<?php echo $toolbarClass;?>").prepend('<?php echo $buttonDiv;?>');
|
|
<?php echo $buttonJs;?>
|
|
},
|
|
"columnDefs": [
|
|
{"className":"lastVisible","targets":[<?php echo sizeof($columns)-2;?>]},
|
|
{"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;
|
|
}
|
|
|
|
}
|
|
|
|
?>
|