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 22:34:43 +01:00
|
|
|
use EesyPHP\Session;
|
2023-01-31 00:30:04 +01:00
|
|
|
use EesyPHP\Tpl;
|
|
|
|
use EesyPHP\Url;
|
2023-01-29 17:36:21 +01:00
|
|
|
|
2023-01-31 00:58:26 +01:00
|
|
|
use function EesyPHP\vardump;
|
|
|
|
|
2020-04-18 00:51:33 +02:00
|
|
|
/*
|
|
|
|
* Check values helpers
|
|
|
|
*/
|
|
|
|
function check_status($status) {
|
|
|
|
global $status_list;
|
|
|
|
return array_key_exists($status, $status_list);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Handling item POST data
|
|
|
|
*/
|
2022-04-24 17:43:44 +02:00
|
|
|
function handle_item_post_data(&$info, $enabled_fields=null, $required_fields=null, &$item=null,
|
|
|
|
&$changes=null) {
|
2020-04-18 00:51:33 +02:00
|
|
|
$field_errors=array();
|
|
|
|
if (isset($_POST['submit'])) {
|
2023-01-29 17:36:21 +01:00
|
|
|
Log :: debug('POST data : '.vardump($_POST));
|
2020-04-18 00:51:33 +02:00
|
|
|
// Name
|
|
|
|
if (!$enabled_fields || in_array('name', $enabled_fields)) {
|
|
|
|
if (isset($_POST['name'])) {
|
2023-01-29 17:36:21 +01:00
|
|
|
if (Check :: name($_POST['name'])) {
|
2020-04-18 00:51:33 +02:00
|
|
|
$info['name'] = $_POST['name'];
|
|
|
|
}
|
|
|
|
else {
|
2024-01-23 19:23:10 +01:00
|
|
|
$field_errors['name'] = "Ce nom est invalid.";
|
2020-04-18 00:51:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$field_errors['name'] = "Cette information est obligatoire.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// status
|
|
|
|
if (!$enabled_fields || in_array('status', $enabled_fields)) {
|
|
|
|
if (isset($_POST['status']) && check_status($_POST['status'])) {
|
|
|
|
$info['status'] = $_POST['status'];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$field_errors['status'] = "Cette information est obligatoire.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// description
|
2022-04-24 17:43:44 +02:00
|
|
|
if (
|
|
|
|
isset($_POST['description']) &&
|
|
|
|
(!$enabled_fields || in_array('description', $enabled_fields))
|
|
|
|
) {
|
2023-01-29 17:36:21 +01:00
|
|
|
if (Check :: is_empty(trim($_POST['description']))) {
|
2020-04-18 00:51:33 +02:00
|
|
|
$info['description'] = null;
|
|
|
|
}
|
2023-01-29 17:36:21 +01:00
|
|
|
else if (Check :: description($_POST['description'])) {
|
2020-04-18 00:51:33 +02:00
|
|
|
$info['description'] = $_POST['description'];
|
|
|
|
}
|
|
|
|
else {
|
2024-01-23 19:23:10 +01:00
|
|
|
$field_errors['description'] = "Cette description est invalid.";
|
2020-04-18 00:51:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check custom required fields
|
|
|
|
if (is_array($required_fields)) {
|
|
|
|
foreach ($required_fields as $field) {
|
|
|
|
if (array_key_exists($field, $field_errors))
|
|
|
|
continue;
|
2023-01-29 17:36:21 +01:00
|
|
|
if (array_key_exists($field, $info) && !is_null($info[$field]) && !Check :: is_empty($info))
|
2020-04-18 00:51:33 +02:00
|
|
|
continue;
|
|
|
|
$field_errors[$field] = "Cette information est obligatoire.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-18 17:21:54 +01:00
|
|
|
if (empty($field_errors) && $item && !is_null($changes)) {
|
2020-04-18 00:51:33 +02:00
|
|
|
$changes = array();
|
|
|
|
foreach ($info as $key => $value) {
|
2024-02-18 17:21:54 +01:00
|
|
|
if ($value != $item->$key)
|
2020-04-18 00:51:33 +02:00
|
|
|
$changes[$key] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $field_errors;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2024-01-23 19:23:10 +01:00
|
|
|
* Parser/formatter values helpers
|
2020-04-18 00:51:33 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
function can_modify($item) {
|
|
|
|
return can_do(
|
|
|
|
$item,
|
|
|
|
array('pending')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function can_archive($item) {
|
|
|
|
return can_do(
|
|
|
|
$item,
|
|
|
|
array('refused', 'validated')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function can_delete($item) {
|
|
|
|
return can_do(
|
|
|
|
$item,
|
|
|
|
array('archived')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function can_do($item, $status=array()) {
|
2024-02-18 17:21:54 +01:00
|
|
|
return in_array($item->status, $status);
|
2020-04-18 00:51:33 +02:00
|
|
|
}
|
|
|
|
|
2023-01-29 11:51:41 +01:00
|
|
|
# vim: tabstop=2 shiftwidth=2 softtabstop=2 expandtab
|