eesyphp/includes/url-public.php

239 lines
7.8 KiB
PHP
Raw Normal View History

2020-04-18 00:51:33 +02:00
<?php
function handle_homepage($url_info) {
display_template("homepage.tpl", "Hello world !");
}
add_url_handler('#^$#', 'handle_homepage');
function handle_search($url_info) {
global $smarty, $status_list;
// Manage params
if((isset($_REQUEST['clear']) && $_REQUEST['clear']=='true') || !isset($_SESSION['search']) || !is_array($_SESSION['search'])) {
$_SESSION['search']=array(
'pattern' => false,
'status' => 'all',
'order' => 'name',
'order_direction' => 'ASC',
);
if (isset($_REQUEST['clear']) && $_REQUEST['clear']=='true')
redirect($url_info['current_page']);
}
logging('DEBUG', 'Request params : '.vardump($_REQUEST));
$status_list['all'] = 'Peu importe';
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;
else if (check_search_pattern($_REQUEST['pattern']))
$_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];
}
logging('DEBUG', 'Search params : '.vardump($_SESSION['search']));
$result = search_items($_SESSION['search']);
if (!is_array($result))
fatal_error("Une erreur est survenue en listant les éléments. Si le problème persiste, merci de prendre contact avec le support.");
$smarty -> assign('result', $result);
$smarty -> assign('search', $_SESSION['search']);
$smarty -> assign('nbs_by_page', $nbs_by_page);
$smarty -> assign('status_list', $status_list);
add_css_file('lib/bootstrap4-dialog/bootstrap-dialog.min.css');
add_js_file('lib/bootstrap4-dialog/bootstrap-dialog.min.js');
add_js_file('lib/myconfirm.js');
add_js_file('js/search.js');
display_template("search.tpl", "Recherche");
}
add_url_handler('|^item/?$|', 'handle_search');
/*
* One item pages
*/
function handle_show($url_info) {
global $smarty;
$item = get_item_from_url($url_info['id']);
if (!$item)
error_404();
$smarty->assign('item', $item);
// Dialog
add_css_file('lib/bootstrap4-dialog/bootstrap-dialog.min.css');
add_js_file('lib/bootstrap4-dialog/bootstrap-dialog.min.js');
add_js_file('lib/myconfirm.js');
display_template("show.tpl", "Élement ".(is_array($item)?$item['name']:"#".$url_info['id']));
}
add_url_handler('|^item/(?P<id>[0-9]+)$|', 'handle_show');
function handle_create($url_info) {
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)) {
add_message("L'élément '".$item['name']."' a bien été créé.");
redirect('item/'.$item['id']);
}
else {
add_error("Une erreur est survenue en enregistrant cet élément.");
}
}
logging('DEBUG', 'Validated data : '.vardump($info));
logging('DEBUG', 'Fields errors : '.vardump($field_errors));
if (isset($_POST['submit']) && !empty($field_errors))
add_error("Des erreurs empêchent l'enregistrement de cet élément. Merci de les corriger avant de tenter d'ajouter cet élément.");
$smarty->assign('submited', isset($_POST['submit']));
$smarty->assign('info', $info);
$smarty->assign('field_errors', $field_errors);
$smarty->assign('status_list', $status_list);
display_template("form.tpl", "Nouveau");
}
add_url_handler('|^item/new$|', 'handle_create');
function handle_modify($url_info) {
global $smarty, $status_list;
$item = get_item_from_url($url_info['id']);
if(is_array($item)) {
if (!can_modify($item)) {
add_error('Vous ne pouvez pas modifier cet élément.');
redirect('item/'.$item['id']);
}
$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;
}
logging('DEBUG', 'Changes : '.vardump($changes));
if (empty($changes)) {
add_message("Vous n'avez apporté aucune modification à l'élément '".$item['name']."'.");
redirect('item/'.$item['id']);
}
else if (update_item($item['id'], $changes) === true) {
add_message("L'élément '".$item['name']."' a bien été mise à jour.");
redirect('item/'.$item['id']);
}
else {
add_error("Une erreur est survenue en mettant à jour cet élément.");
}
}
logging('DEBUG', 'Validated data : '.vardump($info));
logging('DEBUG', 'Fields errors : '.vardump($field_errors));
$smarty->assign('submited', isset($_POST['submit']));
if (isset($_POST['submit']) && !empty($field_errors))
add_error("Des erreurs empêchent l'enregistrement de cet élément. Merci de les corriger avant de tenter d'enregistrer vos modifications.");
$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 {
error_404();
}
display_template("form.tpl", "Élement ".(is_array($item)?$item['name']:"#".$url_info['id'])." : Modification");
}
add_url_handler('|^item/(?P<id>[0-9]+)/modify$|', 'handle_modify');
function handle_archive($url_info) {
global $smarty;
$item = get_item_from_url($url_info['id']);
if(!is_array($item)) {
add_error("Élement #".$url_info['id']." introuvable.");
redirect('item');
}
elseif ($item['status'] == 'archived') {
add_message("Cet élément est déjà archivé.");
}
else if (!can_archive($item)) {
add_error('Vous ne pouvez pas archiver cet élément.');
}
else if (archive_item($item['id']) === true) {
add_message("L'élément '".$item['name']."' a bien été archivé.");
}
else {
add_error("Une erreur est survenue en archivant cet élément.");
}
redirect('item/'.$item['id']);
}
add_url_handler('|^item/(?P<id>[0-9]+)/archive$|', 'handle_archive');
function handle_delete($url_info) {
global $smarty;
$item = get_item_from_url($url_info['id']);
if(!is_array($item)) {
add_error("Élement #".$url_info['id']." introuvable.");
}
else if (!can_delete($item)) {
add_error('Vous ne pouvez pas supprimer cet élément.');
}
else if (delete_item($item['id']) === true) {
add_message("L'élément '".$item['name']."' a bien été supprimé.");
}
else {
add_error("Une erreur est survenue en supprimant cet élément.");
redirect('item/'.$item['id']);
}
redirect('item');
}
add_url_handler('|^item/(?P<id>[0-9]+)/delete$|', 'handle_delete');