Restructure example as skel

This commit is contained in:
Benjamin Renard 2024-02-18 17:41:36 +01:00
parent 2f38884798
commit 07b6fa1305
Signed by: bn8
GPG key ID: 3E2E1CE1907115BC
6 changed files with 29 additions and 29 deletions

View file

@ -69,7 +69,7 @@ foreach($status_list as $key => $value)
require_once('cli.php');
require_once('templates.php');
require_once('url-helpers.php');
require_once('views/index.php');
Db :: init();

View file

@ -1,24 +0,0 @@
<?php
use EesyPHP\Check;
use EesyPHP\Log;
use EesyPHP\Tpl;
use EesyPHPExample\Db\Item;
function get_item_from_url($id, $fatal=false) {
if (!Check :: id($id))
Log :: fatal(_('Invalid element identifier.'));
$item = Item :: get($id);
if(!$item instanceof Item) {
$error = sprintf(_("Item #%s not found."), $id);
if ($fatal)
Log :: fatal($error);
Tpl :: add_error($error);
return false;
}
return $item;
}
# vim: tabstop=2 shiftwidth=2 softtabstop=2 expandtab

View file

@ -12,6 +12,7 @@ use function EesyPHP\vardump;
if (php_sapi_name() == "cli")
return true;
/**
* Search page handler
* @param EesyPHP\UrlRequest $request
@ -123,7 +124,7 @@ Url :: add_url_handler('|^item/?$|', 'handle_search');
* @return void
*/
function handle_show($request) {
$item = get_item_from_url($request -> id);
$item = Item :: get_from_url($request -> id);
if (!$item)
Url :: error_404();
@ -179,7 +180,7 @@ Url :: add_url_handler('|^item/new$|', 'handle_create');
function handle_modify($request) {
global $status_list;
$item = get_item_from_url($request -> id);
$item = Item :: get_from_url($request -> id);
if(!$item)
Url :: error_404();
if (!can_modify($item)) {
@ -228,7 +229,7 @@ Url :: add_url_handler('|^item/(?P<id>[0-9]+)/modify$|', 'handle_modify');
* @return never
*/
function handle_archive($request) {
$item = get_item_from_url($request -> id);
$item = Item :: get_from_url($request -> id);
if(!$item) {
Tpl :: add_error(_("Item #%s not found."), $request -> id);
Url :: redirect('item');
@ -255,7 +256,7 @@ Url :: add_url_handler('|^item/(?P<id>[0-9]+)/archive$|', 'handle_archive');
* @return void
*/
function handle_delete($request) {
$item = get_item_from_url($request -> id);
$item = Item :: get_from_url($request -> id);
if(!$item) {
Tpl :: add_error(_("Item #%s not found."), $request -> id);
}

View file

@ -2,8 +2,10 @@
namespace EesyPHPExample\Db;
use EesyPHP\Check;
use EesyPHP\Hook;
use EesyPHP\Log;
use EesyPHP\Tpl;
use EesyPHP\Db\AttrBool;
use EesyPHP\Db\AttrInt;
@ -40,6 +42,27 @@ class Item extends DbObject {
];
}
/**
* Get item from URL
* @param mixed $id Item ID as retrieved from URL
* @param boolean $fatal Set to true to trigger fatal error if item is not found in DB
* @return Item|false
*/
public static function get_from_url($id, $fatal=false) {
if (!Check :: id($id))
Log :: fatal(_('Invalid element identifier.'));
$item = self :: get($id);
if(!$item instanceof Item) {
$error = sprintf(_("Item #%s not found."), $id);
if ($fatal)
Log :: fatal($error);
Tpl :: add_error($error);
return false;
}
return $item;
}
/**
* Change item status in DB
* @param string $status New item status