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 = "";
/** The Text to Edit */
public $textEdit = "";
/** enable internal CSS */
public $enableInternalCSS = true;
/** The model file containing the database description */
private $model_file = "";
/** The model class included in the model file */
@@ -100,37 +102,104 @@ class routeSQL
$nbentries is the total number of elements
num is the number of elements displayed by page
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
$maxClickPaginator = 10;
$prePage = false;
$postPage = false;
$content = "<div class='paginator'>";
$content = " <div class='paginator'>\n";
$displayedNumbers = 0;
for ($i = 1 ; $i < ($nbentries/$num) ; $i++)
{
if ($i < ($page - $maxClickPaginator/2))
{
if ($prePage === false)
$content .= "... ";
$content .= " ...\n";
$prePage = true;
continue;
}
if ($displayedNumbers >= $maxClickPaginator)
{
if ($postPage === false)
$content .= "... ";
$content .= " ...\n";
$postPage = true;
continue;
}
$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)
$content .= "style='background-color:#FFFF66;'";
$content .= ">$i</a> ";
$content .= " class='selected'";
$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;
}
@@ -144,8 +213,8 @@ class routeSQL
$route->redirect ("/".$this->url_prefix, "");
});
$route->get ($this->url_prefix."(\?({p1}={v1})(&{p2}={v2})?)?",
function ($p1, $v1, $p2, $v2) use ($route)
$route->get ($this->url_prefix."(\?({p1}={v1})(&{p2}={v2}(&{p3}={v3})?)?)?",
function ($p1, $v1, $p2, $v2, $p3, $v3) use ($route)
{
// LIST ALL THE OBJECTS OF THE TABLE
// num is the number of elements displayed by page
@@ -153,34 +222,87 @@ class routeSQL
// Allow the parameters to be sent in any order
if ($p1 === "num") $num = $v1;
if ($p2 === "num") $num = $v2;
if ($p3 === "num") $num = $v3;
if ($p1 === "page") $page = $v1;
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();
$csrf = new csrf ();
$token = $csrf->createToken ();
$datas = $this->objectDB->read ();
$titles = $this->objectDB->titles ();
if (!isset ($page) || $page === null || $page === "") $page = 1;
if (!isset ($num) || $num === null || $num === "") $num = 10;
$page = intval ($page);
$num = intval ($num);
if ($search === "")
$datas = $this->objectDB->read ();
else
{
$criteria = array ();
foreach (array_keys($titles) as $column)
$criteria[] = array ($column, "%$search%", "LIKE");
$datas = $this->objectDB->read ($criteria, null, null, true);
}
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)
$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
if ($page*$num > count ($datas))
if (count ($datas) > 0 && ($page-1)*$num > count ($datas))
{
$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 ();
if ($this->displayActions)
$content .= "<a href='".$route->baseURL().$this->url_prefix."/add'>".
dgettext("domframework","Add new entry")."</a>\n";
$content .= $this->paginator(count ($datas), $page, $num);
$content .= " <div class='topBar'>\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 topBar
$content .= " <table>\n";
$content .= " <thead>\n";
if ($this->displayActions && $this->actionsAtEnd === false)
@@ -193,7 +315,11 @@ class routeSQL
$content .= " <tbody>\n";
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 .= "</td></tr>\n";
}
@@ -211,28 +337,16 @@ class routeSQL
$content .= " <tr class='$odd'>";
if ($odd === "odd") $odd = "even";
else $odd = "odd";
if ($this->displayActions && $this->actionsAtEnd === false)
if ($this->actionsAtEnd !== false)
{
$content .= "<td>";
$content .= " <a href='".$route->baseURL().$this->url_prefix."/".
$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>";
}
foreach ($line as $col)
$content .= "<td>".htmlentities ($col)."</td>";
if ($this->displayActions && $this->actionsAtEnd !== false)
}
if ($this->displayActions)
{
$content .= "<td>";
$content .= "<td class='action'>";
$content .= " <a href='".$route->baseURL().$this->url_prefix."/".
$line[$this->objectDB->primary]."'>".
$line[$this->objectDB->primary]."' class='edit'>".
$this->textEdit."</a>";
$content .= " <a href='".$route->baseURL().$this->url_prefix."/".
$line[$this->objectDB->primary]."/delete/$token'";
@@ -240,15 +354,26 @@ class routeSQL
$content .= " onclick=\"return confirm('".
dgettext("domframework",
"Are you sure to delete this entry?")."')\"";
$content .= ">".$this->textDelete."</a>";
$content .= "class='delete'>".$this->textDelete."</a>";
$content .= "</td>";
}
if ($this->actionsAtEnd === false)
{
foreach ($line as $col)
$content .= "<td>".htmlentities ($col)."</td>";
}
$content .= "</tr>\n";
$i++;
}
}
$content .= " </tbody>\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";
echo $content;
});