2020-04-18 00:51:33 +02:00
|
|
|
<?php
|
|
|
|
|
2023-01-29 17:36:21 +01:00
|
|
|
use EesyPHP\Check;
|
|
|
|
use EesyPHP\Log;
|
2023-01-29 19:18:10 +01:00
|
|
|
use EesyPHP\Url;
|
2023-01-29 17:36:21 +01:00
|
|
|
|
|
|
|
use function EesyPHP\vardump;
|
|
|
|
|
2020-11-18 18:53:47 +01:00
|
|
|
if (php_sapi_name() == "cli")
|
|
|
|
return true;
|
|
|
|
|
2021-07-28 17:13:10 +02:00
|
|
|
function handle_homepage($request) {
|
|
|
|
display_template("homepage.tpl", _("Hello world !"));
|
2020-04-18 00:51:33 +02:00
|
|
|
}
|
2023-01-29 19:18:10 +01:00
|
|
|
Url :: add_url_handler('#^$#', 'handle_homepage');
|
2020-04-18 00:51:33 +02:00
|
|
|
|
2021-07-28 17:13:10 +02:00
|
|
|
function handle_search($request) {
|
2020-04-18 00:51:33 +02:00
|
|
|
global $smarty, $status_list;
|
|
|
|
|
|
|
|
// Manage params
|
2022-04-24 17:43:44 +02:00
|
|
|
if(
|
|
|
|
(isset($_REQUEST['clear']) && $_REQUEST['clear']=='true') ||
|
|
|
|
!isset($_SESSION['search']) ||
|
|
|
|
!is_array($_SESSION['search'])
|
|
|
|
) {
|
2020-04-18 00:51:33 +02:00
|
|
|
$_SESSION['search']=array(
|
|
|
|
'pattern' => false,
|
|
|
|
'status' => 'all',
|
|
|
|
'order' => 'name',
|
|
|
|
'order_direction' => 'ASC',
|
|
|
|
);
|
|
|
|
if (isset($_REQUEST['clear']) && $_REQUEST['clear']=='true')
|
2023-01-29 19:18:10 +01:00
|
|
|
Url :: redirect($request -> current_url);
|
2020-04-18 00:51:33 +02:00
|
|
|
}
|
2023-01-29 17:36:21 +01:00
|
|
|
Log :: debug('Request params : '.vardump($_REQUEST));
|
2020-04-18 00:51:33 +02:00
|
|
|
|
2021-07-28 17:13:10 +02:00
|
|
|
$status_list['all'] = _('Any');
|
2020-04-18 00:51:33 +02:00
|
|
|
if (isset($_REQUEST['status'])) {
|
|
|
|
if (check_status($_REQUEST['status']) || $_REQUEST['status'] == 'all')
|
|
|
|
$_SESSION['search']['status'] = $_REQUEST['status'];
|
|
|
|
else
|
|
|
|
$smarty -> assign('status_error', true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($_REQUEST['pattern'])) {
|
|
|
|
if (trim($_REQUEST['pattern']) == '')
|
|
|
|
$_SESSION['search']['pattern'] = false;
|
2023-01-29 17:36:21 +01:00
|
|
|
else if (Check :: search_pattern($_REQUEST['pattern']))
|
2020-04-18 00:51:33 +02:00
|
|
|
$_SESSION['search']['pattern'] = $_REQUEST['pattern'];
|
|
|
|
else
|
|
|
|
$smarty -> assign('pattern_error', true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Order
|
|
|
|
if(isset($_REQUEST['order'])) {
|
|
|
|
if( $_SESSION['search']['order']==$_REQUEST['order']) {
|
|
|
|
if ($_SESSION['search']['order_direction']=='ASC')
|
|
|
|
$_SESSION['search']['order_direction']='DESC';
|
|
|
|
else
|
|
|
|
$_SESSION['search']['order_direction']='ASC';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
$_SESSION['search']['order_direction']='ASC';
|
|
|
|
$_SESSION['search']['order']=$_REQUEST['order'];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if($_SESSION['search']['order']=='') {
|
|
|
|
$_SESSION['search']['order']='date';
|
|
|
|
$_SESSION['search']['order_direction']='DESC';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Page
|
|
|
|
if (isset($_REQUEST['page'])) {
|
|
|
|
$_SESSION['search']['page']=intval($_REQUEST['page']);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$_SESSION['search']['page']=1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nb par page
|
|
|
|
$nbs_by_page=array(10,25,50,100,500);
|
|
|
|
if (isset($_REQUEST['nb_by_page']) && in_array(intval($_REQUEST['nb_by_page']),$nbs_by_page)) {
|
|
|
|
$_SESSION['search']['nb_by_page']=intval($_REQUEST['nb_by_page']);
|
|
|
|
$_SESSION['search']['page']=1;
|
|
|
|
}
|
|
|
|
elseif (!isset($_SESSION['search']['nb_by_page'])) {
|
|
|
|
$_SESSION['search']['nb_by_page']=$nbs_by_page[0];
|
|
|
|
}
|
|
|
|
|
2023-01-29 17:36:21 +01:00
|
|
|
Log :: debug('Search params : '.vardump($_SESSION['search']));
|
2020-04-18 00:51:33 +02:00
|
|
|
$result = search_items($_SESSION['search']);
|
|
|
|
if (!is_array($result))
|
2022-04-24 17:43:44 +02:00
|
|
|
fatal_error(
|
|
|
|
_("An error occurred while listing the items. ".
|
|
|
|
"If the problem persists, please contact support.")
|
|
|
|
);
|
2020-04-18 00:51:33 +02:00
|
|
|
|
|
|
|
$smarty -> assign('result', $result);
|
|
|
|
$smarty -> assign('search', $_SESSION['search']);
|
|
|
|
$smarty -> assign('nbs_by_page', $nbs_by_page);
|
|
|
|
$smarty -> assign('status_list', $status_list);
|
|
|
|
|
2022-04-24 16:52:44 +02:00
|
|
|
add_js_file(array(
|
|
|
|
'lib/bootstrap4dialog/dist/js/bootstrap4dialog.min.js',
|
|
|
|
'js/myconfirm.js',
|
|
|
|
'js/search.js'
|
|
|
|
));
|
2020-04-18 00:51:33 +02:00
|
|
|
|
2021-07-28 17:13:10 +02:00
|
|
|
display_template("search.tpl", _("Search"));
|
2020-04-18 00:51:33 +02:00
|
|
|
}
|
2023-01-29 19:18:10 +01:00
|
|
|
Url :: add_url_handler('|^item/?$|', 'handle_search');
|
2020-04-18 00:51:33 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* One item pages
|
|
|
|
*/
|
|
|
|
|
2021-07-28 17:13:10 +02:00
|
|
|
function handle_show($request) {
|
2020-04-18 00:51:33 +02:00
|
|
|
global $smarty;
|
|
|
|
|
2021-07-28 17:13:10 +02:00
|
|
|
$item = get_item_from_url($request -> id);
|
2020-04-18 00:51:33 +02:00
|
|
|
if (!$item)
|
2023-01-29 19:18:10 +01:00
|
|
|
Url :: error_404();
|
2020-04-18 00:51:33 +02:00
|
|
|
|
|
|
|
$smarty->assign('item', $item);
|
|
|
|
|
|
|
|
// Dialog
|
2022-04-24 16:52:44 +02:00
|
|
|
add_js_file(array(
|
|
|
|
'lib/bootstrap4dialog/dist/js/bootstrap4dialog.min.js',
|
|
|
|
'js/myconfirm.js',
|
|
|
|
));
|
2020-04-18 00:51:33 +02:00
|
|
|
|
2022-04-24 17:43:44 +02:00
|
|
|
display_template(
|
2022-04-24 18:20:44 +02:00
|
|
|
"show.tpl", _("Element %s"),
|
|
|
|
(is_array($item)?$item['name']:"#".$request -> id)
|
2022-04-24 17:43:44 +02:00
|
|
|
);
|
2020-04-18 00:51:33 +02:00
|
|
|
}
|
2023-01-29 19:18:10 +01:00
|
|
|
Url :: add_url_handler('|^item/(?P<id>[0-9]+)$|', 'handle_show');
|
2020-04-18 00:51:33 +02:00
|
|
|
|
2021-07-28 17:13:10 +02:00
|
|
|
function handle_create($request) {
|
2020-04-18 00:51:33 +02:00
|
|
|
global $smarty, $status_list;
|
|
|
|
|
|
|
|
$info = array();
|
|
|
|
$field_errors = handle_item_post_data($info);
|
|
|
|
if (isset($_POST['submit']) && empty($field_errors)) {
|
|
|
|
$item = add_item($info);
|
|
|
|
if (is_array($item)) {
|
2022-04-24 18:20:44 +02:00
|
|
|
add_message(_("The element '% s' has been created."), $item['name']);
|
2023-01-29 19:18:10 +01:00
|
|
|
Url :: redirect('item/'.$item['id']);
|
2020-04-18 00:51:33 +02:00
|
|
|
}
|
|
|
|
else {
|
2021-07-28 17:13:10 +02:00
|
|
|
add_error(_("An error occurred while saving this item."));
|
2020-04-18 00:51:33 +02:00
|
|
|
}
|
|
|
|
}
|
2023-01-29 17:36:21 +01:00
|
|
|
Log :: debug('Validated data : '.vardump($info));
|
|
|
|
Log :: debug('Fields errors : '.vardump($field_errors));
|
2020-04-18 00:51:33 +02:00
|
|
|
if (isset($_POST['submit']) && !empty($field_errors))
|
2022-04-24 17:43:44 +02:00
|
|
|
add_error(
|
|
|
|
_("There are errors preventing this item from being saved. ".
|
|
|
|
"Please correct them before attempting to add this item."));
|
2020-04-18 00:51:33 +02:00
|
|
|
$smarty->assign('submited', isset($_POST['submit']));
|
|
|
|
$smarty->assign('info', $info);
|
|
|
|
$smarty->assign('field_errors', $field_errors);
|
|
|
|
$smarty->assign('status_list', $status_list);
|
|
|
|
|
2021-07-28 17:13:10 +02:00
|
|
|
display_template("form.tpl", _("New"));
|
2020-04-18 00:51:33 +02:00
|
|
|
}
|
2023-01-29 19:18:10 +01:00
|
|
|
Url :: add_url_handler('|^item/new$|', 'handle_create');
|
2020-04-18 00:51:33 +02:00
|
|
|
|
2021-07-28 17:13:10 +02:00
|
|
|
function handle_modify($request) {
|
2020-04-18 00:51:33 +02:00
|
|
|
global $smarty, $status_list;
|
|
|
|
|
2021-07-28 17:13:10 +02:00
|
|
|
$item = get_item_from_url($request -> id);
|
2020-04-18 00:51:33 +02:00
|
|
|
if(is_array($item)) {
|
|
|
|
if (!can_modify($item)) {
|
2021-07-28 17:13:10 +02:00
|
|
|
add_error(_('You cannot edit this item.'));
|
2023-01-29 19:18:10 +01:00
|
|
|
Url :: redirect('item/'.$item['id']);
|
2020-04-18 00:51:33 +02:00
|
|
|
}
|
|
|
|
$info = array();
|
|
|
|
$field_errors = handle_item_post_data($info);
|
|
|
|
if (isset($_POST['submit']) && empty($field_errors)) {
|
|
|
|
$changes = array();
|
|
|
|
foreach ($info as $key => $value) {
|
|
|
|
if ($value != $item[$key])
|
|
|
|
$changes[$key] = $value;
|
|
|
|
}
|
2023-01-29 17:36:21 +01:00
|
|
|
Log :: debug('Changes : '.vardump($changes));
|
2020-04-18 00:51:33 +02:00
|
|
|
if (empty($changes)) {
|
2022-04-24 18:20:44 +02:00
|
|
|
add_message(_("You have not made any changes to element '% s'."), $item['name']);
|
2023-01-29 19:18:10 +01:00
|
|
|
Url :: redirect('item/'.$item['id']);
|
2020-04-18 00:51:33 +02:00
|
|
|
}
|
|
|
|
else if (update_item($item['id'], $changes) === true) {
|
2022-04-24 18:20:44 +02:00
|
|
|
add_message(_("The element '% s' has been updated successfully."), $item['name']);
|
2023-01-29 19:18:10 +01:00
|
|
|
Url :: redirect('item/'.$item['id']);
|
2020-04-18 00:51:33 +02:00
|
|
|
}
|
|
|
|
else {
|
2021-07-28 17:13:10 +02:00
|
|
|
add_error(_("An error occurred while updating this item."));
|
2020-04-18 00:51:33 +02:00
|
|
|
}
|
|
|
|
}
|
2023-01-29 17:36:21 +01:00
|
|
|
Log :: debug('Validated data : '.vardump($info));
|
|
|
|
Log :: debug('Fields errors : '.vardump($field_errors));
|
2020-04-18 00:51:33 +02:00
|
|
|
$smarty->assign('submited', isset($_POST['submit']));
|
|
|
|
if (isset($_POST['submit']) && !empty($field_errors))
|
2022-04-24 17:43:44 +02:00
|
|
|
add_error(
|
|
|
|
_("There are errors preventing this item from being saved. ".
|
|
|
|
"Please correct them before attempting to save your changes."));
|
2020-04-18 00:51:33 +02:00
|
|
|
$smarty->assign('info', (!empty($info)?$info:$item));
|
|
|
|
$smarty->assign('item_id', $item['id']);
|
|
|
|
$smarty->assign('field_errors', $field_errors);
|
|
|
|
$smarty -> assign('status_list', $status_list);
|
|
|
|
}
|
|
|
|
else {
|
2023-01-29 19:18:10 +01:00
|
|
|
Url :: error_404();
|
2020-04-18 00:51:33 +02:00
|
|
|
}
|
|
|
|
|
2022-04-24 18:20:44 +02:00
|
|
|
display_template(
|
|
|
|
"form.tpl", _("Element %s: Modification"),
|
|
|
|
(is_array($item)?$item['name']:"#".$request -> id)
|
|
|
|
);
|
2020-04-18 00:51:33 +02:00
|
|
|
}
|
2023-01-29 19:18:10 +01:00
|
|
|
Url :: add_url_handler('|^item/(?P<id>[0-9]+)/modify$|', 'handle_modify');
|
2020-04-18 00:51:33 +02:00
|
|
|
|
2021-07-28 17:13:10 +02:00
|
|
|
function handle_archive($request) {
|
2020-04-18 00:51:33 +02:00
|
|
|
global $smarty;
|
|
|
|
|
2021-07-28 17:13:10 +02:00
|
|
|
$item = get_item_from_url($request -> id);
|
2020-04-18 00:51:33 +02:00
|
|
|
if(!is_array($item)) {
|
2022-04-24 18:20:44 +02:00
|
|
|
add_error(_("Item #% s not found."), $request -> id);
|
2023-01-29 19:18:10 +01:00
|
|
|
Url :: redirect('item');
|
2020-04-18 00:51:33 +02:00
|
|
|
}
|
|
|
|
elseif ($item['status'] == 'archived') {
|
2021-07-28 17:13:10 +02:00
|
|
|
add_message(_("This item is already archived."));
|
2020-04-18 00:51:33 +02:00
|
|
|
}
|
|
|
|
else if (!can_archive($item)) {
|
2021-07-28 17:13:10 +02:00
|
|
|
add_error(_('You cannot archive this item.'));
|
2020-04-18 00:51:33 +02:00
|
|
|
}
|
|
|
|
else if (archive_item($item['id']) === true) {
|
2022-04-24 18:20:44 +02:00
|
|
|
add_message(_("The element '% s' has been archived successfully."), $item['name']);
|
2020-04-18 00:51:33 +02:00
|
|
|
}
|
|
|
|
else {
|
2021-07-28 17:13:10 +02:00
|
|
|
add_error(_('An error occurred while archiving this item.'));
|
2020-04-18 00:51:33 +02:00
|
|
|
}
|
2023-01-29 19:18:10 +01:00
|
|
|
Url :: redirect('item/'.$item['id']);
|
2020-04-18 00:51:33 +02:00
|
|
|
}
|
2023-01-29 19:18:10 +01:00
|
|
|
Url :: add_url_handler('|^item/(?P<id>[0-9]+)/archive$|', 'handle_archive');
|
2020-04-18 00:51:33 +02:00
|
|
|
|
2021-07-28 17:13:10 +02:00
|
|
|
function handle_delete($request) {
|
2020-04-18 00:51:33 +02:00
|
|
|
global $smarty;
|
|
|
|
|
2021-07-28 17:13:10 +02:00
|
|
|
$item = get_item_from_url($request -> id);
|
2020-04-18 00:51:33 +02:00
|
|
|
if(!is_array($item)) {
|
2022-04-24 18:20:44 +02:00
|
|
|
add_error(_("Item #% s not found."), $request -> id);
|
2020-04-18 00:51:33 +02:00
|
|
|
}
|
|
|
|
else if (!can_delete($item)) {
|
2021-07-28 17:13:10 +02:00
|
|
|
add_error(_('You cannot delete this item.'));
|
2020-04-18 00:51:33 +02:00
|
|
|
}
|
|
|
|
else if (delete_item($item['id']) === true) {
|
2022-04-24 18:20:44 +02:00
|
|
|
add_message(_("The element '% s' has been deleted successfully."), $item['name']);
|
2020-04-18 00:51:33 +02:00
|
|
|
}
|
|
|
|
else {
|
2021-07-28 17:13:10 +02:00
|
|
|
add_error(_('An error occurred while deleting this item.'));
|
2023-01-29 19:18:10 +01:00
|
|
|
Url :: redirect('item/'.$item['id']);
|
2020-04-18 00:51:33 +02:00
|
|
|
}
|
2023-01-29 19:18:10 +01:00
|
|
|
Url :: redirect('item');
|
2020-04-18 00:51:33 +02:00
|
|
|
}
|
2023-01-29 19:18:10 +01:00
|
|
|
Url :: add_url_handler('|^item/(?P<id>[0-9]+)/delete$|', 'handle_delete');
|
2023-01-29 11:51:41 +01:00
|
|
|
|
|
|
|
# vim: tabstop=2 shiftwidth=2 softtabstop=2 expandtab
|