routeSQL : firstversion with search support

git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@2005 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
2015-03-11 14:36:21 +00:00
parent 087d9eb502
commit 24276ab6f5

View File

@@ -23,6 +23,8 @@ class routeSQL
public $textDelete = ""; public $textDelete = "";
/** The Text to Edit */ /** The Text to Edit */
public $textEdit = ""; public $textEdit = "";
/** enable internal CSS */
public $enableInternalCSS = true;
/** The model file containing the database description */ /** The model file containing the database description */
private $model_file = ""; private $model_file = "";
/** The model class included in the model file */ /** The model class included in the model file */
@@ -100,37 +102,104 @@ class routeSQL
$nbentries is the total number of elements $nbentries is the total number of elements
num is the number of elements displayed by page num is the number of elements displayed by page
page is the page to display */ page is the page to display */
private function paginator ($nbentries, $page, $num) private function paginator ($nbentries, $page, $num, $search)
{ {
// The maximum of links available in the paginator // The maximum of links available in the paginator
$maxClickPaginator = 10; $maxClickPaginator = 10;
$prePage = false; $prePage = false;
$postPage = false; $postPage = false;
$content = "<div class='paginator'>"; $content = " <div class='paginator'>\n";
$displayedNumbers = 0; $displayedNumbers = 0;
for ($i = 1 ; $i < ($nbentries/$num) ; $i++) for ($i = 1 ; $i < ($nbentries/$num) ; $i++)
{ {
if ($i < ($page - $maxClickPaginator/2)) if ($i < ($page - $maxClickPaginator/2))
{ {
if ($prePage === false) if ($prePage === false)
$content .= "... "; $content .= " ...\n";
$prePage = true; $prePage = true;
continue; continue;
} }
if ($displayedNumbers >= $maxClickPaginator) if ($displayedNumbers >= $maxClickPaginator)
{ {
if ($postPage === false) if ($postPage === false)
$content .= "... "; $content .= " ...\n";
$postPage = true; $postPage = true;
continue; continue;
} }
$displayedNumbers++; $displayedNumbers++;
$content .= "<a href='".$this->url_prefix."?page=$i&amp;num=$num'"; $content .= " <a href='".$this->url_prefix.
"?page=$i&amp;num=$num&amp;search=".urlencode ($search)."'";
if ($page == $i) if ($page == $i)
$content .= "style='background-color:#FFFF66;'"; $content .= " class='selected'";
$content .= ">$i</a> "; $content .= ">$i</a>\n";
} }
$content .= "</div>"; if ($displayedNumbers === 0)
{
$i = 1;
$content .= " <a href='".$this->url_prefix.
"?page=$i&amp;num=$num&amp;search=".urlencode($search)."'";
if ($page == $i)
$content .= " class='selected'";
$content .= ">$i</a>\n";
}
$content .= " </div>\n";
return $content;
}
/** Display the actions buttons outside of the table (actually, juste the
'Add new entry' button */
private function externalActions ()
{
$content = "";
if ($this->displayActions)
{
$route = new route ();
$content .= " <div class='actionExtern'>\n";
$content .= " <a href='".$route->baseURL().$this->url_prefix."/add'>"
.dgettext("domframework","Add new entry")."</a>\n";
$content .= " </div>\n";
}
return $content;
}
/** Display the select list to choose the number of displayed entries */
private function listNumberDisplayedEntries ($num)
{
$route = new route ();
$content = "";
$content .= " <div class='listNumberDisplayedEntries'>\n";
$content .= " <form method='get' action='".$route->baseURL().
$this->url_prefix."'>\n";
$content .= " <select name='num' onchange='this.form.submit()' >\n";
$list = array (10,20,50,100,200,500,1000);
foreach ($list as $element)
{
$content .= " <option ";
if ($element == $num)
$content .= "selected='selected'";
$content .= ">$element</option>\n";
}
$content .= " </select>\n";
$content .= " ".dgettext("domframework"," elements")."\n";
$content .= " </form>\n";
$content .= " </div>\n";
return $content;
}
/** Display the search area */
public function searchArea ($search)
{
$route = new route ();
$content = "";
$content .= " <div class='searchArea'>\n";
$content .= " <form method='get' action='".$route->baseURL().
$this->url_prefix."'>\n";
$content .= " ".dgettext("domframework"," Search: ");
$content .= " <input type='text' name='search' value='".
htmlentities ($search, ENT_QUOTES)."'/>\n";
$content .= " </form>\n";
$content .= " </div>\n";
return $content; return $content;
} }
@@ -144,8 +213,8 @@ class routeSQL
$route->redirect ("/".$this->url_prefix, ""); $route->redirect ("/".$this->url_prefix, "");
}); });
$route->get ($this->url_prefix."(\?({p1}={v1})(&{p2}={v2})?)?", $route->get ($this->url_prefix."(\?({p1}={v1})(&{p2}={v2}(&{p3}={v3})?)?)?",
function ($p1, $v1, $p2, $v2) use ($route) function ($p1, $v1, $p2, $v2, $p3, $v3) use ($route)
{ {
// LIST ALL THE OBJECTS OF THE TABLE // LIST ALL THE OBJECTS OF THE TABLE
// num is the number of elements displayed by page // num is the number of elements displayed by page
@@ -153,47 +222,104 @@ class routeSQL
// Allow the parameters to be sent in any order // Allow the parameters to be sent in any order
if ($p1 === "num") $num = $v1; if ($p1 === "num") $num = $v1;
if ($p2 === "num") $num = $v2; if ($p2 === "num") $num = $v2;
if ($p3 === "num") $num = $v3;
if ($p1 === "page") $page = $v1; if ($p1 === "page") $page = $v1;
if ($p2 === "page") $page = $v2; if ($p2 === "page") $page = $v2;
if ($p3 === "page") $page = $v3;
if ($p1 === "search") $search = $v1;
if ($p2 === "search") $search = $v2;
if ($p3 === "search") $search = $v3;
if (!isset ($num) || $num === null || $num === "") $num = 10;
if (!isset ($page) || $page === null || $page === "") $page = 1;
if (!isset ($search) || $search === null || $search === "") $search = "";
$page = intval ($page);
$num = intval ($num);
$search = rawurldecode ($search);
//echo "PAGE=$page\n";
//echo "NUM=$num\n";
//echo "SEARCH=$search\n";
//$route->debug=1;
$this->connect(); $this->connect();
$csrf = new csrf (); $csrf = new csrf ();
$token = $csrf->createToken (); $token = $csrf->createToken ();
$datas = $this->objectDB->read ();
$titles = $this->objectDB->titles (); $titles = $this->objectDB->titles ();
if (!isset ($page) || $page === null || $page === "") $page = 1; if ($search === "")
if (!isset ($num) || $num === null || $num === "") $num = 10; $datas = $this->objectDB->read ();
$page = intval ($page); else
$num = intval ($num); {
$criteria = array ();
foreach (array_keys($titles) as $column)
$criteria[] = array ($column, "%$search%", "LIKE");
$datas = $this->objectDB->read ($criteria, null, null, true);
}
if ($num > 1000) if ($num > 1000)
$route->redirect ("/".$this->url_prefix."?page=$page&num=1000", ""); $route->redirect ("/".$this->url_prefix.
"?page=$page&num=1000&search=$search", "");
if ($page < 1) if ($page < 1)
$route->redirect ("/".$this->url_prefix."?page=1&num=$num", ""); $route->redirect ("/".$this->url_prefix.
"?page=1&num=$num&search=$search", "");
// Push on the last page if the values are too high // Push on the last page if the values are too high
if ($page*$num > count ($datas)) if (count ($datas) > 0 && ($page-1)*$num > count ($datas))
{ {
$maxPage = intval(count ($datas)/$num); $maxPage = intval(count ($datas)/$num);
$route->redirect ("/".$this->url_prefix."?page=$maxPage&num=$num", ""); $route->redirect ("/".$this->url_prefix.
"?page=$maxPage&num=$num&search=$search", "");
} }
$content = "<div class='routeSQL'>\n"; $content = "";
// Internal CSS
if ($this->enableInternalCSS === true)
{
$content .= "<style>\n";
$content .= ".routeSQL { width:95%; margin-left:auto; margin-right:auto; }\n";
$content .= ".routeSQL a { text-decoration:none; }\n";
$content .= ".routeSQL .topBar { display: block; overflow: auto; }\n";
$content .= ".routeSQL .bottomBar { display: block; overflow: auto; }\n";
$content .= ".routeSQL .actionExtern { border:1px solid #ddd; border-radius:5px; padding:10px; margin:3px; float:left; }\n";
$content .= ".routeSQL .listNumberDisplayedEntries { border:1px solid #ddd; border-radius:5px; padding:10px; margin:3px; float:left; }\n";
$content .= ".routeSQL .searchArea { border:1px solid #ddd; border-radius:5px; padding:10px; margin:3px; float:left; }\n";
$content .= ".routeSQL .searchArea form { margin:-5px; }\n";
$content .= ".routeSQL .listNumberDisplayedEntries form { margin:-3px; }\n";
$content .= ".routeSQL .paginator { border:1px solid #ddd; border-radius:5px; padding:10px; margin:3px; float:left; }\n";
$content .= ".routeSQL .paginator a { border:1px solid grey; border-radius:5px; padding:3px; }\n";
$content .= ".routeSQL .paginator .selected { background-color:#04d; color:white;font-weight:bold; }\n";
$content .= ".routeSQL table { width:100%;overflow:auto; border-collapse:collapse; }\n";
$content .= ".routeSQL table tr { border-top:1px solid #ccc;}\n";
$content .= ".routeSQL table th { border-bottom:3px solid #ccc; border-top:1px solid #fff;}\n";
$content .= ".routeSQL table td { empty-cells:true; padding:9px 5px 9px 1px; }\n";
$content .= ".routeSQL table .noentry { text-align:center; color:#c00; font-weight:bolder; }\n";
$content .= ".routeSQL table .action { text-align:center; }\n";
$content .= ".routeSQL table .action .edit { color:#222; font-weight:bolder;}\n";
$content .= ".routeSQL table .action .delete { color:#c00; font-weight:bolder; }\n";
$content .= ".routeSQL table .odd { background-color:#f9f9f9; }\n";
$content .= "</style>\n";
}
$content .= "<div class='routeSQL'>\n";
$content .= $this->showflash (); $content .= $this->showflash ();
if ($this->displayActions) $content .= " <div class='topBar'>\n";
$content .= "<a href='".$route->baseURL().$this->url_prefix."/add'>". $content .= $this->externalActions ();
dgettext("domframework","Add new entry")."</a>\n"; $content .= $this->listNumberDisplayedEntries ($num);
$content .= $this->paginator(count ($datas), $page, $num); $content .= $this->paginator(count ($datas), $page, $num, $search);
$content .= "<table>\n"; $content .= $this->searchArea ($search);
$content .= " <thead>\n"; $content .= " </div>\n"; // End of topBar
$content .= " <table>\n";
$content .= " <thead>\n";
if ($this->displayActions && $this->actionsAtEnd === false) if ($this->displayActions && $this->actionsAtEnd === false)
$content .= " <th>".dgettext("domframework","Actions")."</th>\n"; $content .= " <th>".dgettext("domframework","Actions")."</th>\n";
foreach ($titles as $title) foreach ($titles as $title)
$content .= " <th>".htmlentities ($title)."</th>\n"; $content .= " <th>".htmlentities ($title)."</th>\n";
if ($this->displayActions && $this->actionsAtEnd !== false) if ($this->displayActions && $this->actionsAtEnd !== false)
$content .= " <th>".dgettext("domframework","Actions")."</th>\n"; $content .= " <th>".dgettext("domframework","Actions")."</th>\n";
$content .= " </thead>\n"; $content .= " </thead>\n";
$content .= " <tbody>\n"; $content .= " <tbody>\n";
if (count ($datas) === 0) if (count ($datas) === 0)
{ {
$content .= " <tr><td colspan='".count($titles)."'>"; // Add one column more for actions
$count = count($titles);
if ($this->displayActions)
$count++;
$content .= " <tr><td colspan='$count' class='noentry'>";
$content .= dgettext("domframework","No entry available"); $content .= dgettext("domframework","No entry available");
$content .= "</td></tr>\n"; $content .= "</td></tr>\n";
} }
@@ -208,14 +334,19 @@ class routeSQL
$i++; $i++;
continue; continue;
} }
$content .= " <tr class='$odd'>"; $content .= " <tr class='$odd'>";
if ($odd === "odd") $odd = "even"; if ($odd === "odd") $odd = "even";
else $odd = "odd"; else $odd = "odd";
if ($this->displayActions && $this->actionsAtEnd === false) if ($this->actionsAtEnd !== false)
{ {
$content .= "<td>"; foreach ($line as $col)
$content .= "<td>".htmlentities ($col)."</td>";
}
if ($this->displayActions)
{
$content .= "<td class='action'>";
$content .= " <a href='".$route->baseURL().$this->url_prefix."/". $content .= " <a href='".$route->baseURL().$this->url_prefix."/".
$line[$this->objectDB->primary]."'>". $line[$this->objectDB->primary]."' class='edit'>".
$this->textEdit."</a>"; $this->textEdit."</a>";
$content .= " <a href='".$route->baseURL().$this->url_prefix."/". $content .= " <a href='".$route->baseURL().$this->url_prefix."/".
$line[$this->objectDB->primary]."/delete/$token'"; $line[$this->objectDB->primary]."/delete/$token'";
@@ -223,32 +354,26 @@ class routeSQL
$content .= " onclick=\"return confirm('". $content .= " onclick=\"return confirm('".
dgettext("domframework", dgettext("domframework",
"Are you sure to delete this entry?")."')\""; "Are you sure to delete this entry?")."')\"";
$content .= ">".$this->textDelete."</a>"; $content .= "class='delete'>".$this->textDelete."</a>";
$content .= "</td>"; $content .= "</td>";
} }
foreach ($line as $col) if ($this->actionsAtEnd === false)
$content .= "<td>".htmlentities ($col)."</td>";
if ($this->displayActions && $this->actionsAtEnd !== false)
{ {
$content .= "<td>"; foreach ($line as $col)
$content .= " <a href='".$route->baseURL().$this->url_prefix."/". $content .= "<td>".htmlentities ($col)."</td>";
$line[$this->objectDB->primary]."'>".
$this->textEdit."</a>";
$content .= " <a href='".$route->baseURL().$this->url_prefix."/".
$line[$this->objectDB->primary]."/delete/$token'";
if ($this->deleteConfirm)
$content .= " onclick=\"return confirm('".
dgettext("domframework",
"Are you sure to delete this entry?")."')\"";
$content .= ">".$this->textDelete."</a>";
$content .= "</td>";
} }
$content .= "</tr>\n"; $content .= "</tr>\n";
$i++; $i++;
} }
} }
$content .= " </tbody>\n"; $content .= " </tbody>\n";
$content .= "</table>\n"; $content .= " </table>\n";
$content .= " <div class='bottomBar'>\n";
$content .= $this->externalActions ();
$content .= $this->listNumberDisplayedEntries ($num);
$content .= $this->paginator(count ($datas), $page, $num, $search);
$content .= $this->searchArea ($search);
$content .= " </div>\n"; // End of bottomBar
$content .= "</div>\n"; $content .= "</div>\n";
echo $content; echo $content;
}); });