From 36d878ac00f4df0a4ff54e2ea8355b323f0a87f3 Mon Sep 17 00:00:00 2001 From: vincent-peugnet Date: Sat, 12 Jan 2019 15:52:55 +0100 Subject: add timeline class and event class --- app/class/art2.php | 3 - app/class/controllerhome.php | 5 -- app/class/controllertimeline.php | 26 ++++++++ app/class/dbitem.php | 19 ++++++ app/class/event.php | 133 +++++++++++++++++++++++++++++++++++++++ app/class/modeltimeline.php | 14 +++++ app/class/routes.php | 5 +- 7 files changed, 195 insertions(+), 10 deletions(-) create mode 100644 app/class/controllertimeline.php create mode 100644 app/class/dbitem.php create mode 100644 app/class/event.php create mode 100644 app/class/modeltimeline.php (limited to 'app') diff --git a/app/class/art2.php b/app/class/art2.php index 2341ff9..a7e94f3 100644 --- a/app/class/art2.php +++ b/app/class/art2.php @@ -44,9 +44,6 @@ class Art2 const LEN = 255; const LENTEXT = 2**20; const SECUREMAX = 2; - const LENCOULEUR = 7; - const DEBUT = '(?id='; - const FIN = ')'; const TABS = ['main', 'css', 'header', 'body', 'nav', 'aside', 'footer', 'javascript']; const VAR_DATE = ['date', 'datecreation', 'datemodif', 'daterender']; diff --git a/app/class/controllerhome.php b/app/class/controllerhome.php index caeb652..f5b3068 100644 --- a/app/class/controllerhome.php +++ b/app/class/controllerhome.php @@ -17,11 +17,6 @@ class Controllerhome extends Controller public function desktop() { - $this->table2(); - } - - public function table2() - { $table = $this->modelhome->getlister(); $this->opt = $this->modelhome->optinit($table); diff --git a/app/class/controllertimeline.php b/app/class/controllertimeline.php new file mode 100644 index 0000000..5f80fc2 --- /dev/null +++ b/app/class/controllertimeline.php @@ -0,0 +1,26 @@ +eventmanager = new Modeltimeline; + } + + public function desktop() + { + var_dump($this->eventmanager->list()); + } +} + + + + + + + + +?> \ No newline at end of file diff --git a/app/class/dbitem.php b/app/class/dbitem.php new file mode 100644 index 0000000..ff41d6f --- /dev/null +++ b/app/class/dbitem.php @@ -0,0 +1,19 @@ + $value) { + $method = 'set' . $key; + + if (method_exists($this, $method)) { + $this->$method($value); + } + } + + } +} + + +?> \ No newline at end of file diff --git a/app/class/event.php b/app/class/event.php new file mode 100644 index 0000000..7661aa3 --- /dev/null +++ b/app/class/event.php @@ -0,0 +1,133 @@ +hydrate($datas); + } + + public function conform() + { + $this->user = idclean($this->user); + if(in_array($this->type, self::EVENT_ART)) { + $this->target = idclean($this->target); + } + } + + // _____________________ G E T __________________________ + + public function id() + { + return $this->id; + } + + public function date($type = 'datetime') + { + switch ($type) { + case 'datetime': + return $this->date; + break; + + case 'string': + return $this->date->format(DateTime::ISO8601); + break; + + case 'hrdi': + $now = new DateTimeImmutable(null, timezone_open("Europe/Paris")); + return hrdi($this->date->diff($now)); + break; + + } + } + + public function type() + { + return $this->type; + } + + public function user() + { + return $this->user; + } + + public function target() + { + return $this->target; + } + + public function message() + { + return $this->message; + } + + + + // ________________________ S E T ____________________ + + public function setid($id) + { + if(is_int($id)) { + $this->id = $id; + } + } + + public function setdate($date) + { + if ($date instanceof DateTimeImmutable) { + $this->date = $date; + } else { + $this->date = DateTimeImmutable::createFromFormat(DateTime::ISO8601, $date, new DateTimeZone('Europe/Paris')); + } + } + + public function settype($type) + { + if(in_array($type, self::EVENT_TYPES)) { + $this->type = $type; + } + } + + public function setuser($user) + { + if(is_string($user) && strlen($user) < Model::MAX_ID_LENGTH) { + $this->user = $user; + } + } + + public function settarget($target) + { + if(is_string($target) && strlen($target) < Model::MAX_ID_LENGTH) { + $this->target = $target; + } + } + + public function setmessage($message) + { + if(is_string($message) && strlen($message) < self::MESSAGE_MAX_LENGTH) { + $this->message = $message; + } + } + + + + + +} + + +?> \ No newline at end of file diff --git a/app/class/modeltimeline.php b/app/class/modeltimeline.php new file mode 100644 index 0000000..fd9126e --- /dev/null +++ b/app/class/modeltimeline.php @@ -0,0 +1,14 @@ +storeinit('timeline'); + } +} + + +?> \ No newline at end of file diff --git a/app/class/routes.php b/app/class/routes.php index a72d2e9..a90a8a1 100644 --- a/app/class/routes.php +++ b/app/class/routes.php @@ -22,14 +22,15 @@ class Routes ['POST', '/!media/upload', 'Controllermedia#upload', 'mediaupload'], ['POST', '/!media/folder', 'Controllermedia#folder', 'mediafolder'], ['GET', '/!font', 'Controllerfont#desktop', 'font'], + ['GET', '/!font/render', 'Controllerfont#render', 'fontrender'], + ['POST', '/!font/add', 'Controllerfont#add', 'fontadd'], ['POST', '/!admin', 'Controlleradmin#update', 'adminupdate'], ['GET', '/!admin', 'Controlleradmin#desktop', 'admin'], ['GET', '/!user', 'Controlleruser#desktop', 'user'], ['POST', '/!user/add', 'Controlleruser#add', 'useradd'], ['POST', '/!user/update', 'Controlleruser#update', 'userupdate'], ['GET', '/!info', 'Controllerinfo#desktop', 'info'], - ['GET', '/!font/render', 'Controllerfont#render', 'fontrender'], - ['POST', '/!font/add', 'Controllerfont#add', 'fontadd'], + ['GET', '/!timeline', 'Controllertimeline#desktop', 'timeline'], ['GET', '/[cid:art]/', 'Controllerart#read', 'artread/'], ['GET', '/[cid:art]', 'Controllerart#read', 'artread'], ['GET', '/[cid:art]/add', 'Controllerart#add', 'artadd'], -- cgit v1.2.3 From eb30a63c819ca50ebbc896eb293a0745dcd043be Mon Sep 17 00:00:00 2001 From: vincent-peugnet Date: Sat, 12 Jan 2019 19:49:41 +0100 Subject: timeline message working --- app/class/controllertimeline.php | 18 +++++++++-- app/class/dbitem.php | 15 ++++++++- app/class/event.php | 45 ++++++++++++++------------- app/class/modeltimeline.php | 40 ++++++++++++++++++++++++ app/class/routes.php | 1 + app/view/templates/backtopbar.php | 1 + app/view/templates/timeline.php | 65 +++++++++++++++++++++++++++++++++++++++ 7 files changed, 161 insertions(+), 24 deletions(-) create mode 100644 app/view/templates/timeline.php (limited to 'app') diff --git a/app/class/controllertimeline.php b/app/class/controllertimeline.php index 5f80fc2..b4081a4 100644 --- a/app/class/controllertimeline.php +++ b/app/class/controllertimeline.php @@ -2,7 +2,9 @@ class Controllertimeline extends Controller { - /**@var Modeltimeline */ + /** + * @var Modeltimeline + */ protected $eventmanager; public function __construct($render) { @@ -12,7 +14,19 @@ class Controllertimeline extends Controller public function desktop() { - var_dump($this->eventmanager->list()); + $eventlist = $this->eventmanager->getlister(); + + $this->showtemplate('timeline', ['eventlist' => $eventlist]); + + } + + public function add() + { + $event = new Event($_POST); + $event->stamp(); + $event->setid($this->eventmanager->getlastfreeid()); + $this->eventmanager->add($event); + $this->routedirect('timeline'); } } diff --git a/app/class/dbitem.php b/app/class/dbitem.php index ff41d6f..2035316 100644 --- a/app/class/dbitem.php +++ b/app/class/dbitem.php @@ -2,7 +2,7 @@ class Dbitem { - public function hydrate(array $datas) + public function hydrate($datas) { foreach ($datas as $key => $value) { $method = 'set' . $key; @@ -13,6 +13,19 @@ class Dbitem } } + + public function dry() + { + $array = []; + foreach (get_object_vars($this) as $var => $value) { + if (in_array($var, $this::VAR_DATE)) { + $array[$var] = $this->$var('string'); + } else { + $array[$var] = $this->$var(); + } + } + return $array; + } } diff --git a/app/class/event.php b/app/class/event.php index 7661aa3..a23fff9 100644 --- a/app/class/event.php +++ b/app/class/event.php @@ -14,17 +14,20 @@ class Event extends Dbitem const EVENT_ART = ['art_add', 'art_edit', 'art_delete']; const EVENT_MEDIA = ['media_add', 'media_delete']; const EVENT_FONT = ['font_add', 'font_delete']; - const MESSAGE_MAX_LENGTH = 2**10; + const MESSAGE_MAX_LENGTH = 2 ** 10; - public function __contruct($datas) + const VAR_DATE = ['date']; + + public function __construct($datas) { $this->hydrate($datas); } - public function conform() + public function stamp() { + $this->date = new DateTimeImmutable(null, timezone_open("Europe/Paris")); $this->user = idclean($this->user); - if(in_array($this->type, self::EVENT_ART)) { + if (in_array($this->type, self::EVENT_ART)) { $this->target = idclean($this->target); } } @@ -42,14 +45,14 @@ class Event extends Dbitem case 'datetime': return $this->date; break; - + case 'string': - return $this->date->format(DateTime::ISO8601); + return $this->date->format(DateTime::ISO8601); break; - + case 'hrdi': $now = new DateTimeImmutable(null, timezone_open("Europe/Paris")); - return hrdi($this->date->diff($now)); + return hrdi($this->date->diff($now)); break; } @@ -81,44 +84,44 @@ class Event extends Dbitem public function setid($id) { - if(is_int($id)) { + if (is_int($id)) { $this->id = $id; } } public function setdate($date) - { - if ($date instanceof DateTimeImmutable) { - $this->date = $date; - } else { - $this->date = DateTimeImmutable::createFromFormat(DateTime::ISO8601, $date, new DateTimeZone('Europe/Paris')); - } + { + if ($date instanceof DateTimeImmutable) { + $this->date = $date; + } elseif (is_string($date)) { + $this->date = DateTimeImmutable::createFromFormat(DateTime::ISO8601, $date, new DateTimeZone('Europe/Paris')); + } } - + public function settype($type) { - if(in_array($type, self::EVENT_TYPES)) { + if (in_array($type, self::EVENT_TYPES)) { $this->type = $type; } } public function setuser($user) { - if(is_string($user) && strlen($user) < Model::MAX_ID_LENGTH) { + if (is_string($user) && strlen($user) < Model::MAX_ID_LENGTH) { $this->user = $user; } } public function settarget($target) { - if(is_string($target) && strlen($target) < Model::MAX_ID_LENGTH) { + if (is_string($target) && strlen($target) < Model::MAX_ID_LENGTH) { $this->target = $target; } } public function setmessage($message) { - if(is_string($message) && strlen($message) < self::MESSAGE_MAX_LENGTH) { + if (is_string($message) && strlen($message) < self::MESSAGE_MAX_LENGTH) { $this->message = $message; } } @@ -126,7 +129,7 @@ class Event extends Dbitem - + } diff --git a/app/class/modeltimeline.php b/app/class/modeltimeline.php index fd9126e..b914605 100644 --- a/app/class/modeltimeline.php +++ b/app/class/modeltimeline.php @@ -7,7 +7,47 @@ class Modeltimeline extends Modeldb { parent::__construct(); $this->storeinit('timeline'); + } + + /** + * Retrun a list of Event objects + * + * @return array array of Event where the key is the Event id. + */ + public function getlister() : array + { + $eventlist = []; + $datalist = $this->repo->findAll(); + foreach ($datalist as $eventdata) { + $event = new Event($eventdata); + $eventlist[$event->id()] = $event; + } + return $eventlist; + } + + public function add(Event $event) + { + $eventdata = new \JamesMoss\Flywheel\Document($event->dry()); + $eventdata->setId($event->id()); + $result = $this->repo->store($eventdata); + return $result; } + + public function getlastfreeid() + { + $idlist = $this->list(); + + if(!empty($idlist)) { + $id = max($idlist); + $id ++; + } else { + $id = 1; + } + + return $id; + } + + } diff --git a/app/class/routes.php b/app/class/routes.php index a90a8a1..449b8fa 100644 --- a/app/class/routes.php +++ b/app/class/routes.php @@ -31,6 +31,7 @@ class Routes ['POST', '/!user/update', 'Controlleruser#update', 'userupdate'], ['GET', '/!info', 'Controllerinfo#desktop', 'info'], ['GET', '/!timeline', 'Controllertimeline#desktop', 'timeline'], + ['POST', '/!timeline/add', 'Controllertimeline#add', 'timelineadd'], ['GET', '/[cid:art]/', 'Controllerart#read', 'artread/'], ['GET', '/[cid:art]', 'Controllerart#read', 'artread'], ['GET', '/[cid:art]/add', 'Controllerart#add', 'artadd'], diff --git a/app/view/templates/backtopbar.php b/app/view/templates/backtopbar.php index 78e9181..07b19e8 100644 --- a/app/view/templates/backtopbar.php +++ b/app/view/templates/backtopbar.php @@ -22,6 +22,7 @@ +>timeline >id() ?> level() ?> diff --git a/app/view/templates/timeline.php b/app/view/templates/timeline.php new file mode 100644 index 0000000..8ddc7f1 --- /dev/null +++ b/app/view/templates/timeline.php @@ -0,0 +1,65 @@ +layout('layout', ['title' => 'timeline', 'css' => $css . 'home.css', 'favicon' => '']) ?> + + + + +start('page') ?> + + + + + insert('backtopbar', ['user' => $user, 'tab' => 'timeline']) ?> + +iseditor()) { ?> + +
+ +

Timeline

+ +
    + +id() === $event->user()) { + $class = 'class="self event"'; + } else { + $class = 'class="event"'; + } + echo '
  • '; + switch ($event->type()) { + case 'message': + echo '

    '. $event->user() .'

    '; + echo '

    '. $event->message() .'

    '; + echo ''. $event->date('hrdi') .' ago'; + break; + + default: + + break; + echo '
  • '; + } +} +?> + +
+ +
+ + + + + + + + +
+ +
+ + + + + + + +stop() ?> \ No newline at end of file -- cgit v1.2.3 From 8130427ec508f1d66553b52052c840c311081061 Mon Sep 17 00:00:00 2001 From: vincent-peugnet Date: Mon, 14 Jan 2019 19:24:19 +0100 Subject: fix save art out of session --- app/class/controllerart.php | 76 +++++++++++++++++++++++++-------------- app/class/controllerconnect.php | 36 ++++++++++--------- app/view/templates/backtopbar.php | 1 + app/view/templates/connect.php | 4 +++ app/view/templates/navart.php | 2 ++ 5 files changed, 75 insertions(+), 44 deletions(-) (limited to 'app') diff --git a/app/class/controllerart.php b/app/class/controllerart.php index 00b7558..20ef634 100644 --- a/app/class/controllerart.php +++ b/app/class/controllerart.php @@ -33,13 +33,19 @@ class Controllerart extends Controller public function importart() { - $art = $this->artmanager->get($this->art); + if (isset($_SESSION['artupdate']) && $_SESSION['artupdate']['id'] == $this->art->id()) { + $art = new Art2($_SESSION['artupdate']); + unset($_SESSION['artupdate']); + } else { + $art = $this->artmanager->get($this->art); + } if ($art !== false) { $this->art = $art; return true; } else { return false; } + } @@ -104,9 +110,9 @@ class Controllerart extends Controller } else { $page = ['head' => $this->art->renderhead(), 'body' => $this->art->renderbody()]; } - if($canread) { + if ($canread) { $this->art->addaffcount(); - if($this->user->level() < 2) { + if ($this->user->level() < 2) { $this->art->addvisitcount(); } } @@ -208,40 +214,56 @@ class Controllerart extends Controller public function update($id) { $this->setart($id, 'artupdate'); - $_SESSION['workspace']['showrightpanel'] = isset($_POST['workspace']['showrightpanel']); - $_SESSION['workspace']['showleftpanel'] = isset($_POST['workspace']['showleftpanel']); - - if (!empty($_POST['fontsize']) && $_POST['fontsize'] !== Config::fontsize()) { - Config::setfontsize($_POST['fontsize']); - Config::savejson(); - } - + $this->movepanels(); + $this->fontsize(); $date = new DateTimeImmutable($_POST['pdate'] . $_POST['ptime'], new DateTimeZone('Europe/Paris')); $date = ['date' => $date]; - if ($this->importart() && $this->canedit()) { + if ($this->importart()) { + if ($this->canedit()) { + + // Check if someone esle edited the page during the editing. + $oldart = clone $this->art; + $this->art->hydrate($_POST); - $oldart = clone $this->art; - $this->art->hydrate($_POST); + if (self::COMBINE && $_POST['thisdatemodif'] === $oldart->datemodif('string')) { - if (self::COMBINE) { - if ($_POST['thisdatemodif'] === $oldart->datemodif('string')) { - $compare = $this->artmanager->combine($this->art, $oldart); - if (!empty($compare['diff'])) { - $this->art->hydrate($compare['mergeart']); - } } - } - $this->art->hydrate($date); - $this->art->updateedited(); - $this->art->addauthor($this->user->id()); - $this->artmanager->update($this->art); - $this->routedirect('artedit', ['art' => $this->art->id()]); - + $this->art->hydrate($date); + $this->art->updateedited(); + $this->art->addauthor($this->user->id()); + + + $this->artmanager->update($this->art); + + $this->routedirect('artedit', ['art' => $this->art->id()]); + //$this->showtemplate('updatemerge', $compare); + } else { + // If the editor session finished during the editing, let's try to reconnect to save the editing + $_SESSION['artupdate'] = $_POST; + $_SESSION['artupdate']['id'] = $this->art->id(); + $this->routedirect('connect'); + } + + } + $this->routedirect('art'); + } + + public function movepanels() + { + $_SESSION['workspace']['showrightpanel'] = isset($_POST['workspace']['showrightpanel']); + $_SESSION['workspace']['showleftpanel'] = isset($_POST['workspace']['showleftpanel']); + } + + public function fontsize() + { + if (!empty($_POST['fontsize']) && $_POST['fontsize'] !== Config::fontsize()) { + Config::setfontsize($_POST['fontsize']); + Config::savejson(); } } diff --git a/app/class/controllerconnect.php b/app/class/controllerconnect.php index 2e3f976..d0e3ee5 100644 --- a/app/class/controllerconnect.php +++ b/app/class/controllerconnect.php @@ -6,31 +6,33 @@ class Controllerconnect extends Controller public function log() { if (isset($_POST['log'])) { - if (isset($_POST['id'])) { - $id = $_POST['id']; - } else { - $id = null; - } + $id = $_POST['id'] ?? null; + $route = $_POST['route'] ?? 'home'; if ($_POST['log'] === 'login') { - $this->login($id); + $this->login($route, $id); } elseif ($_POST['log'] === 'logout') { - $this->logout($id); + $this->logout($route, $id); } } - } public function connect() { - $this->showtemplate('connect', []); + if(isset($_SESSION['artupdate'])) { + $artupdate['route'] = 'artedit'; + $artupdate['id'] = $_SESSION['artupdate']['id']; + } else { + $artupdate = [$route = 'home']; + } + $this->showtemplate('connect', $artupdate); } - public function login($id) + public function login($route, $id = null) { if (isset($_POST['pass'])) { $this->user = $this->usermanager->login($_POST['pass']); @@ -41,21 +43,21 @@ class Controllerconnect extends Controller } } - if (!empty($id)) { - $this->routedirect('artread/', ['art' => $id]); + if ($id !== null) { + $this->routedirect($route, ['art' => $id]); } else { - $this->routedirect('home'); + $this->routedirect($route); } } - public function logout($id) + public function logout($route, $id = null) { $this->user = $this->usermanager->logout(); $this->usermanager->writesession($this->user); - if (!empty($id)) { - $this->routedirect('artread/', ['art' => $id]); + if ($id !== null && $route !== 'home') { + $this->routedirect($route, ['art' => $id]); } else { - $this->routedirect('home'); + $this->routedirect($route); } } diff --git a/app/view/templates/backtopbar.php b/app/view/templates/backtopbar.php index 07b19e8..d6b522e 100644 --- a/app/view/templates/backtopbar.php +++ b/app/view/templates/backtopbar.php @@ -15,6 +15,7 @@
+
diff --git a/app/view/templates/connect.php b/app/view/templates/connect.php index 06fe9c8..5d7ee5c 100644 --- a/app/view/templates/connect.php +++ b/app/view/templates/connect.php @@ -11,7 +11,11 @@ isvisitor()) { ?> +Your edits have been temporary saved. You need to connect and update to store it completly

' : '' ?> +
+ +
diff --git a/app/view/templates/navart.php b/app/view/templates/navart.php index be3b248..1e6b551 100644 --- a/app/view/templates/navart.php +++ b/app/view/templates/navart.php @@ -72,6 +72,7 @@
  • +
    @@ -82,6 +83,7 @@
  • +
  • -- cgit v1.2.3 From de195c2fca2f4b77a54be68fb58cd0ccee10a5b7 Mon Sep 17 00:00:00 2001 From: vincent-peugnet Date: Tue, 15 Jan 2019 03:59:32 +0100 Subject: timeline is ok for messaging --- app/class/controllertimeline.php | 28 +++++++--- app/class/event.php | 20 +++++++ app/class/modeltimeline.php | 113 ++++++++++++++++++++++++++++++++++----- app/class/routes.php | 1 + app/view/templates/timeline.php | 61 ++++++++++++++------- 5 files changed, 185 insertions(+), 38 deletions(-) (limited to 'app') diff --git a/app/class/controllertimeline.php b/app/class/controllertimeline.php index b4081a4..7e617e1 100644 --- a/app/class/controllertimeline.php +++ b/app/class/controllertimeline.php @@ -14,20 +14,36 @@ class Controllertimeline extends Controller public function desktop() { - $eventlist = $this->eventmanager->getlister(); + $eventlist = $this->eventmanager->showlast(['message'], 100, 0); - $this->showtemplate('timeline', ['eventlist' => $eventlist]); + $groupedeventlist = $this->eventmanager->group($eventlist); + + $this->showtemplate('timeline', ['eventlist' => $eventlist, 'groupedeventlist' => $groupedeventlist]); } public function add() { - $event = new Event($_POST); - $event->stamp(); - $event->setid($this->eventmanager->getlastfreeid()); - $this->eventmanager->add($event); + if($this->user->level() >= Modeluser::EDITOR && !empty($_POST['message'])) { + + $event = new Event($_POST); + $event->stamp(); + $event->setid($this->eventmanager->getlastfreeid()); + $this->eventmanager->add($event); + } $this->routedirect('timeline'); } + + public function clap() + { + if(isset($_POST['id']) && isset($_POST['clap'])) { + $event = $this->eventmanager->get(intval($_POST['id'])); + $event->addclap(); + $this->eventmanager->add($event); + } + $this->routedirect('timeline'); + + } } diff --git a/app/class/event.php b/app/class/event.php index a23fff9..9fc75d2 100644 --- a/app/class/event.php +++ b/app/class/event.php @@ -8,6 +8,7 @@ class Event extends Dbitem protected $user; protected $target; protected $message; + protected $clap = 0; const EVENT_TYPES = ['message', 'art_add', 'art_edit', 'art_delete', 'media_add', 'media_delete', 'font_add']; const EVENT_BASE = ['message']; @@ -29,9 +30,16 @@ class Event extends Dbitem $this->user = idclean($this->user); if (in_array($this->type, self::EVENT_ART)) { $this->target = idclean($this->target); + } elseif ($this->type === 'message') { + $this->message = htmlspecialchars($this->message); } } + public function addclap() + { + $this->clap ++; + } + // _____________________ G E T __________________________ public function id() @@ -78,6 +86,11 @@ class Event extends Dbitem return $this->message; } + public function clap() + { + return $this->clap; + } + // ________________________ S E T ____________________ @@ -126,6 +139,13 @@ class Event extends Dbitem } } + public function setclap($clap) + { + if(is_int($clap)) { + $this->clap = $clap; + } + } + diff --git a/app/class/modeltimeline.php b/app/class/modeltimeline.php index b914605..b575dda 100644 --- a/app/class/modeltimeline.php +++ b/app/class/modeltimeline.php @@ -2,30 +2,67 @@ class Modeltimeline extends Modeldb { + const EVENT_BASE = ['message']; + const EVENT_ART = ['art_add', 'art_edit', 'art_delete']; + const EVENT_MEDIA = ['media_add', 'media_delete']; + const EVENT_FONT = ['font_add', 'font_delete']; public function __construct() { parent::__construct(); $this->storeinit('timeline'); - } - - /** - * Retrun a list of Event objects - * - * @return array array of Event where the key is the Event id. - */ - public function getlister() : array + } + + public function get(int $id) + { + $eventdata = $this->repo->findById($id); + if ($eventdata !== false) { + return new Event($eventdata); + } else { + return false; + } + } + + /** + * Retrun a list of Event objects + * + * @return array array of Event where the key is the Event id. + */ + public function getlister() : array { $eventlist = []; $datalist = $this->repo->findAll(); foreach ($datalist as $eventdata) { $event = new Event($eventdata); - $eventlist[$event->id()] = $event; + $id = intval($event->id()); + $eventlist[$id] = $event; } return $eventlist; } - public function add(Event $event) + + public function getlisterid(array $idlist = []) : array + { + $eventdatalist = $this->repo->query() + ->where('__id', 'IN', $idlist) + ->execute(); + + $eventlist = []; + foreach ($eventdatalist as $id => $eventdata) { + $eventlist[$id] = new Event($eventdata); + } + return $eventlist; + } + + + /** + * Store event + * + * @param Event The event to be stored in the repositery + * + * @return bool retrun true if it works, false if it fails + */ + public function add(Event $event) : bool { $eventdata = new \JamesMoss\Flywheel\Document($event->dry()); $eventdata->setId($event->id()); @@ -33,20 +70,68 @@ class Modeltimeline extends Modeldb return $result; } - public function getlastfreeid() + /** + * Return last free id + * + * @return int id + */ + public function getlastfreeid() : int { $idlist = $this->list(); - if(!empty($idlist)) { + if (!empty($idlist)) { $id = max($idlist); - $id ++; + $id++; } else { $id = 1; } - return $id; } + public function group(array $events) + { + $id = 0; + $subid = 0; + $lastuser = null; + foreach ($events as $event) { + if($event->user() !== $lastuser) { + $subid = 0; + $id ++; + $groupedevents[$id]['user'] = $event->user(); + } else { + $subid ++; + } + $groupedevents[$id][$subid] = $event; + $lastuser = $event->user(); + } + return $groupedevents; + } + + public function showlast(array $types, int $qty = 25, int $offset = 0) + { + $types = array_intersect($types, $this->types()); + + $eventdatalist = $this->repo->query() + ->where('type', 'IN', $types) + ->orderBy('date DESC') + ->limit($qty, $offset) + ->execute(); + + $eventlist = []; + foreach ($eventdatalist as $id => $eventdata) { + $eventlist[] = new Event($eventdata); + } + + $eventlist = array_reverse($eventlist); + return $eventlist; + } + + + public function types() + { + return array_merge(self::EVENT_ART, self::EVENT_BASE, self::EVENT_MEDIA, self::EVENT_MEDIA); + } + } diff --git a/app/class/routes.php b/app/class/routes.php index 449b8fa..2ccc269 100644 --- a/app/class/routes.php +++ b/app/class/routes.php @@ -32,6 +32,7 @@ class Routes ['GET', '/!info', 'Controllerinfo#desktop', 'info'], ['GET', '/!timeline', 'Controllertimeline#desktop', 'timeline'], ['POST', '/!timeline/add', 'Controllertimeline#add', 'timelineadd'], + ['POST', '/!timeline/clap', 'Controllertimeline#clap', 'timelineclap'], ['GET', '/[cid:art]/', 'Controllerart#read', 'artread/'], ['GET', '/[cid:art]', 'Controllerart#read', 'artread'], ['GET', '/[cid:art]/add', 'Controllerart#add', 'artadd'], diff --git a/app/view/templates/timeline.php b/app/view/templates/timeline.php index 8ddc7f1..fcf8afd 100644 --- a/app/view/templates/timeline.php +++ b/app/view/templates/timeline.php @@ -19,25 +19,50 @@
      id() === $event->user()) { - $class = 'class="self event"'; - } else { - $class = 'class="event"'; - } - echo '
    • '; - switch ($event->type()) { - case 'message': - echo '

      '. $event->user() .'

      '; - echo '

      '. $event->message() .'

      '; - echo ''. $event->date('hrdi') .' ago'; - break; - - default: +foreach ($groupedeventlist as $eventuser) { + + if($user->id() === $eventuser['user']) { + $class = 'class="self user"'; + } else { + $class = 'class="user"'; + } + echo '
    • '; + echo '

      '. $eventuser['user'] .'

      '; + echo'
        '; + foreach ($eventuser as $key => $event) { + if($key !== 'user') { + echo '
      • '; + switch ($event->type()) { + case 'message': + echo '

        '. $event->message() .'

        '; + break; + } + + ?> + + + clap()) ? ''. $event->clap() .'' : '' ?> + + + + id() !== $eventuser['user']) {?> +
        + + +
        + + + + date('hrdi') ?> ago - break; - echo '
      • '; + + + + + '; } ?> @@ -50,7 +75,7 @@ foreach ($eventlist as $event) { - + -- cgit v1.2.3