aboutsummaryrefslogtreecommitdiff
path: root/app/class
diff options
context:
space:
mode:
authorvincent-peugnet <v.peugnet@free.fr>2020-04-14 16:36:45 +0200
committervincent-peugnet <v.peugnet@free.fr>2020-04-14 16:36:45 +0200
commit8ccc4ac00c974ba3659762e6c67f75ed82020e37 (patch)
treefc9a5b656ca13b34cb2eaac6e909b0b59ca839e8 /app/class
parentce3fcb72f2d5d154461a14183069bf87db1e5776 (diff)
downloadwcms-8ccc4ac00c974ba3659762e6c67f75ed82020e37.tar.gz
wcms-8ccc4ac00c974ba3659762e6c67f75ed82020e37.zip
finish flashmessages implementation
+ clean css
Diffstat (limited to 'app/class')
-rw-r--r--app/class/Controller.php14
-rw-r--r--app/class/Controllerhome.php67
-rw-r--r--app/class/Controllerpage.php36
-rw-r--r--app/class/Controlleruser.php6
-rw-r--r--app/class/Item.php12
-rw-r--r--app/class/Model.php1
-rw-r--r--app/class/Modelmedia.php14
-rw-r--r--app/class/Modelpage.php13
-rw-r--r--app/class/User.php20
9 files changed, 127 insertions, 56 deletions
diff --git a/app/class/Controller.php b/app/class/Controller.php
index ccf31fa..bcfcdba 100644
--- a/app/class/Controller.php
+++ b/app/class/Controller.php
@@ -99,6 +99,20 @@ class Controller
exit;
}
+ /**
+ *
+ */
+ public function sendstatflashmessage(int $count, int $total, string $message)
+ {
+ if ($count === $total) {
+ Model::sendflashmessage($count . ' / ' . $total . ' ' . $message, 'success');
+ } elseif ($count > 0) {
+ Model::sendflashmessage($count . ' / ' . $total . ' ' . $message, 'warning');
+ } else {
+ Model::sendflashmessage($count . ' / ' . $total . ' ' . $message, 'error');
+ }
+ }
+
}
diff --git a/app/class/Controllerhome.php b/app/class/Controllerhome.php
index dae7030..d024a19 100644
--- a/app/class/Controllerhome.php
+++ b/app/class/Controllerhome.php
@@ -146,11 +146,15 @@ class Controllerhome extends Controllerpage
{
if ($this->user->iseditor()) {
$pagelist = $this->modelhome->pagelist();
+ $count = 0;
foreach ($pagelist as $page) {
- $this->renderpage($page);
- $this->pagemanager->update($page);
+ $page = $this->renderpage($page);
+ if ($this->pagemanager->update($page)) {
+ $count ++;
+ }
}
- Model::sendflashmessage('All pages have been rendered', 'success');
+ $total = count($pagelist);
+ $this->sendstatflashmessage($count, $total, 'pages have been rendered');
}
$this->routedirect('home');
}
@@ -203,36 +207,45 @@ class Controllerhome extends Controllerpage
$this->multidelete();
break;
}
+ } else {
+ $action = $_POST['action'] ?? 'edit';
+ Model::sendflashmessage('Please select some pages to ' . $action, 'warning');
}
$this->routedirect('home');
}
public function multiedit()
{
- if (isset($_POST['pagesid'])) {
- $datas = $_POST['datas']?? [];
- $datas = array_filter($datas, function ($var) {
- return $var !== "";
- });
- $datas = array_map(function ($value) {
- if($value === "!") {
- return "";
- } else {
- return $value;
- }
- }, $datas);
- $reset = $_POST['reset'] ?? [];
- $addtag = $_POST['addtag'] ?? '';
- $addauthor = $_POST['addauthor'] ?? '';
- foreach ($_POST['pagesid'] as $id) {
- $this->pagemanager->pageedit($id, $datas, $reset, $addtag, $addauthor);
+ $pagelist = $_POST['pagesid'] ?? [];
+ $datas = $_POST['datas']?? [];
+ $datas = array_filter($datas, function ($var) {
+ return $var !== "";
+ });
+ $datas = array_map(function ($value) {
+ if($value === "!") {
+ return "";
+ } else {
+ return $value;
+ }
+ }, $datas);
+ $reset = $_POST['reset'] ?? [];
+ $addtag = $_POST['addtag'] ?? '';
+ $addauthor = $_POST['addauthor'] ?? '';
+ $count = 0;
+ $total = 0;
+ foreach ($pagelist as $id) {
+ $total ++;
+ if($this->pagemanager->pageedit($id, $datas, $reset, $addtag, $addauthor)) {
+ $count ++;
}
}
+ $this->sendstatflashmessage($count, $total, 'pages have been edited');
}
public function multirender()
{
$pagelist = $_POST['pagesid'] ?? [];
+ $total = count($pagelist);
$pagelist = $this->pagemanager->pagelistbyid($pagelist);
$count = 0;
foreach ($pagelist as $page) {
@@ -240,19 +253,25 @@ class Controllerhome extends Controllerpage
if($this->pagemanager->update($page)) {
$count ++;
}
-
}
- Model::sendflashmessage($count . ' pages have been rendered', 'success');
+ $this->sendstatflashmessage($count, $total, 'pages have been rendered');
}
public function multidelete()
{
if(isset($_POST['confirmdelete']) && $_POST['confirmdelete']) {
$pagelist = $_POST['pagesid'] ?? [];
+ $total = count($pagelist);
+ $count = 0;
foreach ($pagelist as $id) {
- $this->pagemanager->delete($id);
+ if ($this->pagemanager->delete($id)) {
+ $count ++;
+ }
}
- }
+ $this->sendstatflashmessage($count, $total, 'pages have been deleted');
+ } else {
+ Model::sendflashmessage('Confirm delete has not been cheked', 'warning');
+ }
}
}
diff --git a/app/class/Controllerpage.php b/app/class/Controllerpage.php
index 909a2c2..990f0b7 100644
--- a/app/class/Controllerpage.php
+++ b/app/class/Controllerpage.php
@@ -279,25 +279,31 @@ class Controllerpage extends Controller
{
$page = $this->pagemanager->getfromfile();
+ if($page !== false) {
- if(!empty($_POST['id'])) {
- $page->setid(idclean($_POST['id']));
- }
-
- if($_POST['datecreation']) {
- $page->setdatecreation($this->now);
- }
-
- if($_POST['author']) {
- $page->setauthors([$this->user->id()]);
- }
-
- $page->setdaterender($page->datecreation('date'));
+ if(!empty($_POST['id'])) {
+ $page->setid(idclean($_POST['id']));
+ }
+
+ if($_POST['datecreation']) {
+ $page->setdatecreation($this->now);
+ }
+
+ if($_POST['author']) {
+ $page->setauthors([$this->user->id()]);
+ }
+
+ $page->setdaterender($page->datecreation('date'));
- if($page !== false) {
if($_POST['erase'] || $this->pagemanager->get($page) === false) {
- $this->pagemanager->add($page);
+ if($this->pagemanager->add($page)) {
+ Model::sendflashmessage('Page successfully uploaded', 'success');
+ }
+ } else {
+ Model::sendflashmessage('Page ID already exist, check remplace if you want to erase it', 'warning');
}
+ } else {
+ Model::sendflashmessage('Error while importing page JSON', 'error');
}
$this->routedirect('home');
}
diff --git a/app/class/Controlleruser.php b/app/class/Controlleruser.php
index 1586679..1f53f76 100644
--- a/app/class/Controlleruser.php
+++ b/app/class/Controlleruser.php
@@ -32,7 +32,11 @@ class Controlleruser extends Controller
{
if($this->user->iseditor()) {
$user = $this->usermanager->get($this->user);
- $user->hydrate($_POST);
+ if ($user->hydrate($_POST)) {
+ Model::sendflashmessage('User preferences have been successfully updated', 'success');
+ } else {
+ Model::sendflashmessage('There was a problem when updating preferences', 'warning');
+ }
if ($_POST['passwordhash']) {
$user->hashpassword();
}
diff --git a/app/class/Item.php b/app/class/Item.php
index 8bd62d1..10d35d7 100644
--- a/app/class/Item.php
+++ b/app/class/Item.php
@@ -12,13 +12,21 @@ abstract class Item
public function hydrate($datas = [])
{
+ $error = 0;
foreach ($datas as $key => $value) {
$method = 'set' . $key;
if (method_exists($this, $method)) {
- $this->$method($value);
+ if($this->$method($value) === false) {
+ $error ++;
+ }
}
- }
+ }
+ if ($error > 0) {
+ return false;
+ } else {
+ return true;
+ }
}
diff --git a/app/class/Model.php b/app/class/Model.php
index e26a006..73a800d 100644
--- a/app/class/Model.php
+++ b/app/class/Model.php
@@ -97,6 +97,7 @@ abstract class Model
const MAX_ID_LENGTH = 64;
const PASSWORD_MIN_LENGTH = 4;
const PASSWORD_MAX_LENGTH = 32;
+ const MAX_COOKIE_CONSERVATION = 365;
/** RENDER OPTIONS */
// add class in html element indicating from witch page the content come.
diff --git a/app/class/Modelmedia.php b/app/class/Modelmedia.php
index a7ade6c..038f49e 100644
--- a/app/class/Modelmedia.php
+++ b/app/class/Modelmedia.php
@@ -303,7 +303,7 @@ class Modelmedia extends Model
$success[] = $this->deletefile($filedir);
}
}
- Model::sendflashmessage(array_count_values($success) . ' / ' . count($filelist) . ' files have been deleted', 'success');
+ Model::sendflashmessage(count(array_filter($success)) . ' / ' . count($filelist) . ' files have been deleted', 'success');
if(in_array(false, $success)) {
return false;
} else {
@@ -338,16 +338,20 @@ class Modelmedia extends Model
*/
public function multimovefile(array $filedirlist, string $dir) : bool
{
- $success = [];
+ $count = 0;
foreach ($filedirlist as $filedir ) {
if(is_string($filedir)) {
- $success[] = $this->movefile($filedir, $dir);
+ if($this->movefile($filedir, $dir)) {
+ $count ++;
+ }
}
}
- Model::sendflashmessage(count($success) . ' / ' . count($filedirlist) . ' files have been moved', 'success');
- if(in_array(false, $success)) {
+ $total = count($filedirlist);
+ if($count !== $total) {
+ Model::sendflashmessage($count . ' / ' . $total . ' files have been moved', 'error');
return false;
} else {
+ Model::sendflashmessage($count . ' / ' . $total . ' files have been moved', 'success');
return true;
}
}
diff --git a/app/class/Modelpage.php b/app/class/Modelpage.php
index 492f03e..cf07c2b 100644
--- a/app/class/Modelpage.php
+++ b/app/class/Modelpage.php
@@ -46,7 +46,7 @@ class Modelpage extends Modeldb
*
* @param array $idlist list of ID strings
*
- * @return array of Page objects
+ * @return Page[] array of Page objects
*/
public function pagelistbyid(array $idlist = []) : array
{
@@ -65,13 +65,14 @@ class Modelpage extends Modeldb
* Store new page in the database
*
* @param Page $page object
+ * @return bool depending on database storing
*/
- public function add(Page $page)
+ public function add(Page $page) : bool
{
$pagedata = new Document($page->dry());
$pagedata->setId($page->id());
- $this->repo->store($pagedata);
+ return $this->repo->store($pagedata);
}
/**
@@ -395,15 +396,17 @@ class Modelpage extends Modeldb
* @param array $reset
* @param string $addtag
* @param string $addauthor
+ *
+ * @return bool Depending on update success
*/
- public function pageedit($pageid, $datas, $reset, $addtag, $addauthor)
+ public function pageedit($pageid, $datas, $reset, $addtag, $addauthor) : bool
{
$page = $this->get($pageid);
$page = $this->reset($page, $reset);
$page->hydrate($datas);
$page->addtag($addtag);
$page->addauthor($addauthor);
- $this->update($page);
+ return $this->update($page);
}
/**
diff --git a/app/class/User.php b/app/class/User.php
index 518b096..dba43e8 100644
--- a/app/class/User.php
+++ b/app/class/User.php
@@ -118,6 +118,9 @@ class User extends Item
$id = idclean($id);
if (strlen($id) < Model::MAX_ID_LENGTH and is_string($id)) {
$this->id = $id;
+ return true;
+ } else {
+ return false;
}
}
@@ -132,7 +135,12 @@ class User extends Item
public function setpassword($password)
{
if (!empty($password) && is_string($password)) {
- $this->password = $password;
+ if (strlen($password) >= Model::PASSWORD_MIN_LENGTH && strlen($password) <= Model::PASSWORD_MAX_LENGTH) {
+ $this->password = $password;
+ return true;
+ } else {
+ return false;
+ }
}
}
@@ -152,9 +160,13 @@ class User extends Item
public function setcookie($cookie)
{
- $cookie = abs(intval($cookie));
- if($cookie >= 365) {$cookie = 365;}
- $this->cookie = $cookie;
+ $cookie = intval($cookie);
+ if($cookie <= Model::MAX_COOKIE_CONSERVATION && $cookie >= 0) {
+ $this->cookie = $cookie;
+ return true;
+ } else {
+ return false;
+ }
}
public function setcolumns($columns)