From d45f7f5c62167bbf560cedb55c80e96a77a6c601 Mon Sep 17 00:00:00 2001 From: Riccardo Di Dato Date: Thu, 14 Jan 2016 20:38:19 +0100 Subject: [PATCH] Inserito helper per utilizzare le datatables Creato modello base per le pagine di backend delle datatables Alcune pagine di esempio di uso delle datatables Aggiunto metodo count al dao --- private/lib/dao/GenericDao.php | 35 ++ private/lib/dao/models/NotiziaModel.php | 12 + private/lib/gui/DatatablesHelper.php | 94 ++++ private/lib/gui/HTMLHelper.php | 24 +- private/lib/gui/lib.inclusion.php | 1 + public/admin/notizie.php | 33 ++ public/admin/roleAdmin.php | 500 +----------------- .../tablesBackends/notiziaTableBackend.php | 158 ++++++ 8 files changed, 369 insertions(+), 488 deletions(-) create mode 100644 private/lib/gui/DatatablesHelper.php create mode 100644 public/admin/notizie.php create mode 100644 public/admin/tablesBackends/notiziaTableBackend.php diff --git a/private/lib/dao/GenericDao.php b/private/lib/dao/GenericDao.php index 2dc0973..64c83c7 100644 --- a/private/lib/dao/GenericDao.php +++ b/private/lib/dao/GenericDao.php @@ -67,6 +67,9 @@ class GenericDao{ if (array_key_exists("sort",$options)){ $cursor->sort($options["sort"]); } + if (array_key_exists("skip",$options)){ + $cursor->skip($options["skip"]); + } if (array_key_exists("limit",$options)){ $cursor->limit($options["limit"]); } @@ -79,6 +82,38 @@ class GenericDao{ return $rval; } + + public function count($modelClass, array $filter = array(), array $options = array()){ + if (!is_null($modelClass)){ + if (!is_subclass_of($modelClass, "DaoSaveable", true)){ + throw new CoreException("Invalid return class passed to GenericDao::query ($modelClass)"); + } + } + + $collectionName = $modelClass::getCollectionName(); + $collection = $this->database->$collectionName; + + $useFilter = $filter; + + if (!array_key_exists("deleted",$useFilter)){ + $useFilter["deleted"] = false; + } + + if (array_key_exists("id",$useFilter)){ + $useFilter["_id"] = $useFilter["id"]; + unset($useFilter["id"]); + } + + if (array_key_exists("_id",$useFilter) && is_string($useFilter["_id"])){ + $useFilter["_id"] = new MongoId($useFilter["_id"]); + } + + + $cursor = $collection->find($useFilter); + + return $cursor->count(true); + } + /** * Come query ma ritorna il primo o null se non ci sono risultati * @param string $modelClass diff --git a/private/lib/dao/models/NotiziaModel.php b/private/lib/dao/models/NotiziaModel.php index 7117c32..6084a30 100644 --- a/private/lib/dao/models/NotiziaModel.php +++ b/private/lib/dao/models/NotiziaModel.php @@ -4,6 +4,18 @@ Author: Riccardo Di Dato Creation Date: 07/gen/2016 */ +/** + * Campi: + * titolo + * sottotitolo + * testo + * about { + * author + * data + * } + * @author rik + * + */ class NotiziaModel extends HasMediaModel{ public static function getCollectionName(){ diff --git a/private/lib/gui/DatatablesHelper.php b/private/lib/gui/DatatablesHelper.php new file mode 100644 index 0000000..eee9ef0 --- /dev/null +++ b/private/lib/gui/DatatablesHelper.php @@ -0,0 +1,94 @@ +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++; + $colLabels = ""; + $jsCols = array(); + foreach ($columns as $col){ + $colLabels.= ''.$col["label"].''; + + $jsCols[]= '{"data":"'.$col["data"].'"}'; + } + $colLabels.= ''; + + echo ''.$colLabels.''.$colLabels.'
'; + + echo ''; + + + + } + +} + +?> \ No newline at end of file diff --git a/private/lib/gui/HTMLHelper.php b/private/lib/gui/HTMLHelper.php index d48d04a..8223f41 100644 --- a/private/lib/gui/HTMLHelper.php +++ b/private/lib/gui/HTMLHelper.php @@ -10,7 +10,29 @@ class HTMLHelper { echo '
'; } - + public static function generateModelsTable(array $columnInfo, array $models, array $datatableOpts = array()) { + echo ''; + $colLabels = ""; + foreach ($columnInfo as $col){ + $colLabels.= ''; + } + $colLabels.= ''; + echo ''.$colLabels.''.$colLabels.''; + if (sizeof($models)>0){ + echo ''; + foreach ($models as $model){ + echo ''; + foreach ($columnInfo as $col){ + $field = $col["field"]; + echo ''; + } + echo ''; + } + echo ''; + } + + echo '
'.$col["label"].'
'.$model->$field.'
'; + } } ?> \ No newline at end of file diff --git a/private/lib/gui/lib.inclusion.php b/private/lib/gui/lib.inclusion.php index 79ee50d..02f6786 100644 --- a/private/lib/gui/lib.inclusion.php +++ b/private/lib/gui/lib.inclusion.php @@ -30,4 +30,5 @@ $systemCatalogManager = new StaticCatalogManager(array()); require_once(dirname(__FILE__)."/html.inclusion.php"); require_once(dirname(__FILE__)."/HTMLHelper.php"); +require_once(dirname(__FILE__)."/DatatablesHelper.php"); ?> \ No newline at end of file diff --git a/public/admin/notizie.php b/public/admin/notizie.php new file mode 100644 index 0000000..4dc7a23 --- /dev/null +++ b/public/admin/notizie.php @@ -0,0 +1,33 @@ +hasRole(AdminModel::ADMIN_TYPE_EDITORE_SUPERVISOR)){ + GUIHandler::redirect("admin/index.php"); +} + +MenuGenerator::generateMenu(); + +HTMLHelper::generateAdminHead(); +$columns = array( + array("label"=>"Titolo","data"=>"titolo"), + array("label"=>"Sottotitolo","data"=>"sottotitolo"), + array("label"=>"Autore","data"=>"about.author"), + array("label"=>"Data","data"=>"about.data") +); + +?> +
+ +
\ No newline at end of file diff --git a/public/admin/roleAdmin.php b/public/admin/roleAdmin.php index 9b1575b..1cba765 100644 --- a/public/admin/roleAdmin.php +++ b/public/admin/roleAdmin.php @@ -11,497 +11,23 @@ GUIHandler::generateHtmlHeaders(array(),true); if (!AdminLoginManager::isLogged()){ GUIHandler::redirect("admin/login.php"); } +else if (!AdminLoginManager::getLogged()->hasRole(AdminModel::ADMIN_TYPE_NORMALADMIN)){ + GUIHandler::redirect("admin/index.php"); +} MenuGenerator::generateMenu(); HTMLHelper::generateAdminHead(); +$columns = array( + array("label"=>"Titolo","data"=>"titolo"), + array("label"=>"Sottotitolo","data"=>"sottotitolo"), + array("label"=>"Autore","data"=>"about.author"), + array("label"=>"Data","data"=>"about.data") +); + ?>
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NamePositionOfficeAgeStart dateSalary
NamePositionOfficeAgeStart dateSalary
Tiger NixonSystem ArchitectEdinburgh612011/04/25$320,800
Garrett WintersAccountantTokyo632011/07/25$170,750
Ashton CoxJunior Technical AuthorSan Francisco662009/01/12$86,000
Cedric KellySenior Javascript DeveloperEdinburgh222012/03/29$433,060
Airi SatouAccountantTokyo332008/11/28$162,700
Brielle WilliamsonIntegration SpecialistNew York612012/12/02$372,000
Herrod ChandlerSales AssistantSan Francisco592012/08/06$137,500
Rhona DavidsonIntegration SpecialistTokyo552010/10/14$327,900
Colleen HurstJavascript DeveloperSan Francisco392009/09/15$205,500
Sonya FrostSoftware EngineerEdinburgh232008/12/13$103,600
Jena GainesOffice ManagerLondon302008/12/19$90,560
Quinn FlynnSupport LeadEdinburgh222013/03/03$342,000
Charde MarshallRegional DirectorSan Francisco362008/10/16$470,600
Haley KennedySenior Marketing DesignerLondon432012/12/18$313,500
Tatyana FitzpatrickRegional DirectorLondon192010/03/17$385,750
Michael SilvaMarketing DesignerLondon662012/11/27$198,500
Paul ByrdChief Financial Officer (CFO)New York642010/06/09$725,000
Gloria LittleSystems AdministratorNew York592009/04/10$237,500
Bradley GreerSoftware EngineerLondon412012/10/13$132,000
Dai RiosPersonnel LeadEdinburgh352012/09/26$217,500
Jenette CaldwellDevelopment LeadNew York302011/09/03$345,000
Yuri BerryChief Marketing Officer (CMO)New York402009/06/25$675,000
Caesar VancePre-Sales SupportNew York212011/12/12$106,450
Doris WilderSales AssistantSidney232010/09/20$85,600
Angelica RamosChief Executive Officer (CEO)London472009/10/09$1,200,000
Gavin JoyceDeveloperEdinburgh422010/12/22$92,575
Jennifer ChangRegional DirectorSingapore282010/11/14$357,650
Brenden WagnerSoftware EngineerSan Francisco282011/06/07$206,850
Fiona GreenChief Operating Officer (COO)San Francisco482010/03/11$850,000
Shou ItouRegional MarketingTokyo202011/08/14$163,000
Michelle HouseIntegration SpecialistSidney372011/06/02$95,400
Suki BurksDeveloperLondon532009/10/22$114,500
Prescott BartlettTechnical AuthorLondon272011/05/07$145,000
Gavin CortezTeam LeaderSan Francisco222008/10/26$235,500
Martena MccrayPost-Sales supportEdinburgh462011/03/09$324,050
Unity ButlerMarketing DesignerSan Francisco472009/12/09$85,675
Howard HatfieldOffice ManagerSan Francisco512008/12/16$164,500
Hope FuentesSecretarySan Francisco412010/02/12$109,850
Vivian HarrellFinancial ControllerSan Francisco622009/02/14$452,500
Timothy MooneyOffice ManagerLondon372008/12/11$136,200
Jackson BradshawDirectorNew York652008/09/26$645,750
Olivia LiangSupport EngineerSingapore642011/02/03$234,500
Bruno NashSoftware EngineerLondon382011/05/03$163,500
Sakura YamamotoSupport EngineerTokyo372009/08/19$139,575
Thor WaltonDeveloperNew York612013/08/11$98,540
Finn CamachoSupport EngineerSan Francisco472009/07/07$87,500
Serge BaldwinData CoordinatorSingapore642012/04/09$138,575
Zenaida FrankSoftware EngineerNew York632010/01/04$125,250
Zorita SerranoSoftware EngineerSan Francisco562012/06/01$115,000
Jennifer AcostaJunior Javascript DeveloperEdinburgh432013/02/01$75,650
Cara StevensSales AssistantNew York462011/12/06$145,600
Hermione ButlerRegional DirectorLondon472011/03/21$356,250
Lael GreerSystems AdministratorLondon212009/02/27$103,500
Jonas AlexanderDeveloperSan Francisco302010/07/14$86,500
Shad DeckerRegional DirectorEdinburgh512008/11/13$183,000
Michael BruceJavascript DeveloperSingapore292011/06/27$183,000
Donna SniderCustomer SupportNew York272011/01/25$112,000
+
- \ No newline at end of file diff --git a/public/admin/tablesBackends/notiziaTableBackend.php b/public/admin/tablesBackends/notiziaTableBackend.php new file mode 100644 index 0000000..ba3750f --- /dev/null +++ b/public/admin/tablesBackends/notiziaTableBackend.php @@ -0,0 +1,158 @@ +draw = $_POST["draw"]; + +// $arr = array(); +// $ele = new stdClass(); +// $ele->Sottotitolo = print_r($_POST,true); +// $ele->Titolo = "asd"; +// $ele->about = new stdClass(); +// $ele->about->autore = "autore"; +// $ele->about->data = "data"; + +// $arr[] = $ele; +// for ($i=0; $i<9; $i++){ +// $ele = new stdClass(); +// $ele->Sottotitolo = "asd"; +// $ele->Titolo = "asd"; +// $ele->about = new stdClass(); +// $ele->about->autore = "autore"; +// $ele->about->data = "data"; +// $arr[] = $ele; +// } + +// $ret->recordsTotal = 10; +// $ret->recordsFiltered = 10; +// $ret->data = $arr; +// echo json_encode($ret); +// die(); + +// OK + +$useModel = "NotiziaModel"; +$permissionFlagRequired = AdminModel::ADMIN_TYPE_EDITORE_SUPERVISOR; +$exposedFields = array( + "titolo"=>null, + "sottotitolo"=>null, + "about"=>null +); + +$ret = DatatablesHelper::getResult($useModel, $permissionFlagRequired, $exposedFields); +echo json_encode($ret); + +// $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 = new stdClass(); +// foreach ($exposedFields as $key=>$subfield){ +// if (is_array($subfield)){ +// $result->$key = new stdClass(); +// if (sizeof($subfield)>0){ +// foreach ($subfield as $field){ +// $result->$key->$field = $model->$key->$field; +// } +// } +// } +// else { +// $result->$key = $model->$key; +// } +// } +// $result->titolo = $model->titolo; +// $result->sottotitolo = $model->sottotitolo; +// $result->about = $model->about; + +// $ret->data[] = $result; +// } +// } +// $ret->recordsTotal = $dao->count($useModel); +// $ret->recordsFiltered = $dao->count($useModel,$filter); +// } +// } + +// echo json_encode($ret); + + + +die(); + +$table = 'datatables_demo'; + +// Table's primary key +$primaryKey = 'id'; + +// Array of database columns which should be read and sent back to DataTables. +// The `db` parameter represents the column name in the database, while the `dt` +// parameter represents the DataTables column identifier. In this case object +// parameter names +$columns = array( + array( 'db' => 'first_name', 'dt' => 'first_name' ), + array( 'db' => 'last_name', 'dt' => 'last_name' ), + array( 'db' => 'position', 'dt' => 'position' ), + array( 'db' => 'office', 'dt' => 'office' ), + array( + 'db' => 'start_date', + 'dt' => 'start_date', + 'formatter' => function( $d, $row ) { + return date( 'jS M y', strtotime($d)); + } +), +array( + 'db' => 'salary', + 'dt' => 'salary', + 'formatter' => function( $d, $row ) { + return '$'.number_format($d); + } +) +); + +// SQL server connection information +$sql_details = array( + 'user' => '', + 'pass' => '', + 'db' => '', + 'host' => '' +); + + +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * If you just want to use the basic configuration for DataTables with PHP +* server-side, there is no need to edit below this line. +*/ + +require( 'ssp.class.php' ); + +echo json_encode( + SSP::simple( $_POST, $sql_details, $table, $primaryKey, $columns ) +); + + + +?> \ No newline at end of file