Compare commits
No commits in common. "49074421227223e7b5f1394fee24adc0924f517c" and "07b6fa13058196a89c599e7494e04e4af9452721" have entirely different histories.
4907442122
...
07b6fa1305
10 changed files with 50 additions and 95 deletions
|
@ -66,7 +66,7 @@ function cli_list($command_args) {
|
||||||
case '-s':
|
case '-s':
|
||||||
case '--status':
|
case '--status':
|
||||||
$i++;
|
$i++;
|
||||||
if(!Item :: check_status($command_args[$i]))
|
if(!check_status($command_args[$i]))
|
||||||
Cli :: usage('Invalid -s/--status clause');
|
Cli :: usage('Invalid -s/--status clause');
|
||||||
$params['filters']['status'] = $command_args[$i];
|
$params['filters']['status'] = $command_args[$i];
|
||||||
break;
|
break;
|
||||||
|
@ -124,7 +124,7 @@ Cli :: add_command(
|
||||||
" - ".implode("\n - ", Item :: possible_orders()),
|
" - ".implode("\n - ", Item :: possible_orders()),
|
||||||
___("-r|--reverse Reverse order"),
|
___("-r|--reverse Reverse order"),
|
||||||
___("-s|--status Filter on status. Possible values:"),
|
___("-s|--status Filter on status. Possible values:"),
|
||||||
" - ".implode("\n - ", array_keys(Item :: statuses())),
|
" - ".implode("\n - ", array_keys($status_list)),
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
@ -57,6 +57,16 @@ $sentry_span = new SentrySpan('core.init', 'Core initialization');
|
||||||
|
|
||||||
require_once('functions.php');
|
require_once('functions.php');
|
||||||
|
|
||||||
|
// Nomenclatures
|
||||||
|
$status_list = array (
|
||||||
|
'pending' => ___('Pending'),
|
||||||
|
'validated' => ___('Validated'),
|
||||||
|
'refused' => ___('Refused'),
|
||||||
|
'archived' => ___('Archived'),
|
||||||
|
);
|
||||||
|
foreach($status_list as $key => $value)
|
||||||
|
$status_list[$key] = _($value);
|
||||||
|
|
||||||
require_once('cli.php');
|
require_once('cli.php');
|
||||||
require_once('templates.php');
|
require_once('templates.php');
|
||||||
require_once('views/index.php');
|
require_once('views/index.php');
|
||||||
|
|
|
@ -6,10 +6,16 @@ use EesyPHP\Session;
|
||||||
use EesyPHP\Tpl;
|
use EesyPHP\Tpl;
|
||||||
use EesyPHP\Url;
|
use EesyPHP\Url;
|
||||||
|
|
||||||
use EesyPHPExample\Db\Item;
|
|
||||||
|
|
||||||
use function EesyPHP\vardump;
|
use function EesyPHP\vardump;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Check values helpers
|
||||||
|
*/
|
||||||
|
function check_status($status) {
|
||||||
|
global $status_list;
|
||||||
|
return array_key_exists($status, $status_list);
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Handling item POST data
|
* Handling item POST data
|
||||||
*/
|
*/
|
||||||
|
@ -35,7 +41,7 @@ function handle_item_post_data(&$info, $enabled_fields=null, $required_fields=nu
|
||||||
|
|
||||||
// status
|
// status
|
||||||
if (!$enabled_fields || in_array('status', $enabled_fields)) {
|
if (!$enabled_fields || in_array('status', $enabled_fields)) {
|
||||||
if (isset($_POST['status']) && Item :: check_status($_POST['status'])) {
|
if (isset($_POST['status']) && check_status($_POST['status'])) {
|
||||||
$info['status'] = $_POST['status'];
|
$info['status'] = $_POST['status'];
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
|
@ -7,7 +7,6 @@ use EesyPHP\Log;
|
||||||
use EesyPHP\Tpl;
|
use EesyPHP\Tpl;
|
||||||
use EesyPHP\Url;
|
use EesyPHP\Url;
|
||||||
|
|
||||||
use EesyPHPExample\Db\Item;
|
|
||||||
|
|
||||||
use function EesyPHP\format_size;
|
use function EesyPHP\format_size;
|
||||||
|
|
||||||
|
@ -26,15 +25,19 @@ Tpl :: enable_security_mode(
|
||||||
);
|
);
|
||||||
|
|
||||||
function define_common_template_variables($event) {
|
function define_common_template_variables($event) {
|
||||||
global $admin;
|
global $status_list, $admin;
|
||||||
Tpl :: assign('statuses', Item :: statuses());
|
Tpl :: assign(
|
||||||
|
'status_list',
|
||||||
|
isset($status_list) && is_array($status_list)?
|
||||||
|
$status_list:array()
|
||||||
|
);
|
||||||
Tpl :: assign('admin', isset($admin) && $admin);
|
Tpl :: assign('admin', isset($admin) && $admin);
|
||||||
}
|
}
|
||||||
Hook :: register('before_displaying_template', 'define_common_template_variables');
|
Hook :: register('before_displaying_template', 'define_common_template_variables');
|
||||||
|
|
||||||
// Templates functions
|
// Templates functions
|
||||||
function smarty_item_status($params) {
|
function smarty_item_status($params) {
|
||||||
$statuses = Item :: statuses();
|
global $status_list;
|
||||||
$status2class = array (
|
$status2class = array (
|
||||||
'pending' => 'info',
|
'pending' => 'info',
|
||||||
'validated' => 'success',
|
'validated' => 'success',
|
||||||
|
@ -48,8 +51,8 @@ function smarty_item_status($params) {
|
||||||
$class='danger';
|
$class='danger';
|
||||||
echo "<span class='badge bg-$class'>";
|
echo "<span class='badge bg-$class'>";
|
||||||
echo (
|
echo (
|
||||||
array_key_exists($params['item']->status, $statuses)?
|
array_key_exists($params['item']->status, $status_list)?
|
||||||
$statuses[$params['item']->status]:
|
$status_list[$params['item']->status]:
|
||||||
"Inconnu (".$params['item']->status.")"
|
"Inconnu (".$params['item']->status.")"
|
||||||
);
|
);
|
||||||
echo "</span>";
|
echo "</span>";
|
||||||
|
|
|
@ -19,6 +19,8 @@ if (php_sapi_name() == "cli")
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
function handle_search($request) {
|
function handle_search($request) {
|
||||||
|
global $status_list;
|
||||||
|
|
||||||
// Manage params
|
// Manage params
|
||||||
if(
|
if(
|
||||||
(isset($_REQUEST['clear']) && $_REQUEST['clear']=='true') ||
|
(isset($_REQUEST['clear']) && $_REQUEST['clear']=='true') ||
|
||||||
|
@ -36,8 +38,9 @@ function handle_search($request) {
|
||||||
}
|
}
|
||||||
Log :: debug('Request params : '.vardump($_REQUEST));
|
Log :: debug('Request params : '.vardump($_REQUEST));
|
||||||
|
|
||||||
|
$status_list['all'] = _('Any');
|
||||||
if (isset($_REQUEST['status'])) {
|
if (isset($_REQUEST['status'])) {
|
||||||
if (Item :: check_status($_REQUEST['status'], true))
|
if (check_status($_REQUEST['status']) || $_REQUEST['status'] == 'all')
|
||||||
$_SESSION['search']['filters']['status'] = $_REQUEST['status'];
|
$_SESSION['search']['filters']['status'] = $_REQUEST['status'];
|
||||||
else
|
else
|
||||||
Tpl :: assign('status_error', true);
|
Tpl :: assign('status_error', true);
|
||||||
|
@ -100,7 +103,7 @@ function handle_search($request) {
|
||||||
Tpl :: assign('result', $result);
|
Tpl :: assign('result', $result);
|
||||||
Tpl :: assign('search', $_SESSION['search']);
|
Tpl :: assign('search', $_SESSION['search']);
|
||||||
Tpl :: assign('nbs_by_page', $nbs_by_page);
|
Tpl :: assign('nbs_by_page', $nbs_by_page);
|
||||||
Tpl :: assign('status_choices', Item :: statuses(true));
|
Tpl :: assign('status_list', $status_list);
|
||||||
|
|
||||||
Tpl :: add_css_file(
|
Tpl :: add_css_file(
|
||||||
'lib/bootstrap5-dialog/css/bootstrap-dialog.min.css',
|
'lib/bootstrap5-dialog/css/bootstrap-dialog.min.css',
|
||||||
|
@ -146,6 +149,8 @@ Url :: add_url_handler('|^item/(?P<id>[0-9]+)$|', 'handle_show');
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
function handle_create($request) {
|
function handle_create($request) {
|
||||||
|
global $status_list;
|
||||||
|
|
||||||
$info = array();
|
$info = array();
|
||||||
$field_errors = handle_item_post_data($info);
|
$field_errors = handle_item_post_data($info);
|
||||||
if (isset($_POST['submit']) && empty($field_errors)) {
|
if (isset($_POST['submit']) && empty($field_errors)) {
|
||||||
|
@ -166,12 +171,15 @@ function handle_create($request) {
|
||||||
Tpl :: assign('submited', isset($_POST['submit']));
|
Tpl :: assign('submited', isset($_POST['submit']));
|
||||||
Tpl :: assign('info', $info);
|
Tpl :: assign('info', $info);
|
||||||
Tpl :: assign('field_errors', $field_errors);
|
Tpl :: assign('field_errors', $field_errors);
|
||||||
|
Tpl :: assign('status_list', $status_list);
|
||||||
|
|
||||||
Tpl :: display("form.tpl", _("New"));
|
Tpl :: display("form.tpl", _("New"));
|
||||||
}
|
}
|
||||||
Url :: add_url_handler('|^item/new$|', 'handle_create');
|
Url :: add_url_handler('|^item/new$|', 'handle_create');
|
||||||
|
|
||||||
function handle_modify($request) {
|
function handle_modify($request) {
|
||||||
|
global $status_list;
|
||||||
|
|
||||||
$item = Item :: get_from_url($request -> id);
|
$item = Item :: get_from_url($request -> id);
|
||||||
if(!$item)
|
if(!$item)
|
||||||
Url :: error_404();
|
Url :: error_404();
|
||||||
|
@ -209,6 +217,7 @@ function handle_modify($request) {
|
||||||
Tpl :: assign('info', (!empty($info)?$info:$item));
|
Tpl :: assign('info', (!empty($info)?$info:$item));
|
||||||
Tpl :: assign('item_id', $item->id);
|
Tpl :: assign('item_id', $item->id);
|
||||||
Tpl :: assign('field_errors', $field_errors);
|
Tpl :: assign('field_errors', $field_errors);
|
||||||
|
Tpl :: assign('status_list', $status_list);
|
||||||
|
|
||||||
Tpl :: display("form.tpl", _("Element %s: Modification"), $item->name);
|
Tpl :: display("form.tpl", _("Element %s: Modification"), $item->name);
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,3 +9,9 @@ parameters:
|
||||||
- EesyPHP\UrlRequest
|
- EesyPHP\UrlRequest
|
||||||
- EesyPHP\Auth\User
|
- EesyPHP\Auth\User
|
||||||
treatPhpDocTypesAsCertain: false
|
treatPhpDocTypesAsCertain: false
|
||||||
|
ignoreErrors:
|
||||||
|
-
|
||||||
|
message: "#Variable \\$status_list might not be defined\\.#"
|
||||||
|
paths:
|
||||||
|
- includes/cli.php
|
||||||
|
|
||||||
|
|
|
@ -9,7 +9,6 @@ use EesyPHP\Tpl;
|
||||||
|
|
||||||
use EesyPHP\Db\AttrBool;
|
use EesyPHP\Db\AttrBool;
|
||||||
use EesyPHP\Db\AttrInt;
|
use EesyPHP\Db\AttrInt;
|
||||||
use EesyPHP\Db\AttrSet;
|
|
||||||
use EesyPHP\Db\AttrStr;
|
use EesyPHP\Db\AttrStr;
|
||||||
use EesyPHP\Db\AttrTimestamp;
|
use EesyPHP\Db\AttrTimestamp;
|
||||||
use EesyPHP\Db\DbObject;
|
use EesyPHP\Db\DbObject;
|
||||||
|
@ -33,43 +32,12 @@ class Item extends DbObject {
|
||||||
protected const DEFAULT_ORDER_DIRECTION = 'DESC';
|
protected const DEFAULT_ORDER_DIRECTION = 'DESC';
|
||||||
protected const POSSIBLE_ORDERS = ['id', 'name', 'date', 'status'];
|
protected const POSSIBLE_ORDERS = ['id', 'name', 'date', 'status'];
|
||||||
|
|
||||||
/**
|
|
||||||
* Get status possible values with their translated label
|
|
||||||
* @param boolean $with_all_choice Set to true to add the 'all' value (optional, default: false)
|
|
||||||
* @return array<string,string>
|
|
||||||
*/
|
|
||||||
public static function statuses($with_all_choice=false) {
|
|
||||||
$statuses = array (
|
|
||||||
'pending' => _('Pending'),
|
|
||||||
'validated' => _('Validated'),
|
|
||||||
'refused' => _('Refused'),
|
|
||||||
'archived' => _('Archived'),
|
|
||||||
);
|
|
||||||
if ($with_all_choice)
|
|
||||||
$statuses['all'] = _('Any');
|
|
||||||
return $statuses;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Check item status value
|
|
||||||
* @param mixed $value Value to check
|
|
||||||
* @param boolean $allow_all Set to true to allow the 'all' value (optional, default: false)
|
|
||||||
* @return boolean
|
|
||||||
*/
|
|
||||||
public static function check_status($value, $allow_all=false) {
|
|
||||||
return array_key_exists($value, self :: statuses($allow_all));
|
|
||||||
}
|
|
||||||
|
|
||||||
protected static function get_schema() {
|
protected static function get_schema() {
|
||||||
return [
|
return [
|
||||||
'id' => new AttrInt(['autoincrement' => true]),
|
'id' => new AttrInt(['autoincrement' => true]),
|
||||||
'name' => new AttrStr(['required' => true]),
|
'name' => new AttrStr(['required' => true]),
|
||||||
'date' => new AttrTimestamp(['default' => 'time']),
|
'date' => new AttrTimestamp(['default' => 'time']),
|
||||||
'status' => new AttrSet([
|
'status' => new AttrStr(['required' => true, 'default' => 'pending']),
|
||||||
'required' => true,
|
|
||||||
'default' => 'pending',
|
|
||||||
'possible_values' => array_keys(self :: statuses()),
|
|
||||||
]),
|
|
||||||
'description' => new AttrStr(),
|
'description' => new AttrStr(),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,7 +20,7 @@
|
||||||
<div class="col-sm-10">
|
<div class="col-sm-10">
|
||||||
<select name="status" id="status" required
|
<select name="status" id="status" required
|
||||||
class="form-select{if array_key_exists('status', $field_errors)} is-invalid{else if $submitted} is-valid{/if}">
|
class="form-select{if array_key_exists('status', $field_errors)} is-invalid{else if $submitted} is-valid{/if}">
|
||||||
{html_options options=$statuses selected=$info->status}
|
{html_options options=$status_list selected=$info->status}
|
||||||
</select>
|
</select>
|
||||||
{if array_key_exists('status', $field_errors)}
|
{if array_key_exists('status', $field_errors)}
|
||||||
<div class="invalid-feedback">{$field_errors['status']}</div>
|
<div class="invalid-feedback">{$field_errors['status']}</div>
|
||||||
|
|
|
@ -15,7 +15,7 @@
|
||||||
<label class="input-group-text" for="status">{t}Status{/t}</label>
|
<label class="input-group-text" for="status">{t}Status{/t}</label>
|
||||||
<select name="status" id="status"
|
<select name="status" id="status"
|
||||||
class="form-select{if isset($status_error) && $status_error} is-invalid{/if}">
|
class="form-select{if isset($status_error) && $status_error} is-invalid{/if}">
|
||||||
{html_options options=$status_choices selected=$search.filters.status}
|
{html_options options=$status_list selected=$search.filters.status}
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -1,47 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
namespace EesyPHP\Db;
|
|
||||||
|
|
||||||
class AttrSet extends Attr {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Possible values
|
|
||||||
* @var array<mixed>
|
|
||||||
*/
|
|
||||||
protected $possible_values = [];
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Compute attribute value from DB
|
|
||||||
* @param string|null $value The value as retrieved from debug
|
|
||||||
* @return string|null The attribute value
|
|
||||||
*/
|
|
||||||
public function from_db($value) {
|
|
||||||
$value = parent::from_db($value);
|
|
||||||
if (is_null($value))
|
|
||||||
return null;
|
|
||||||
if (!in_array($value, $this -> possible_values))
|
|
||||||
throw new DbException(
|
|
||||||
"Unexpected value '%s' retrieved from database. Should be one of the following values: %s",
|
|
||||||
$value, implode(', ', $this -> possible_values)
|
|
||||||
);
|
|
||||||
return $value;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Compute attribute value for DB
|
|
||||||
* @param string|null $value The value as handled in PHP
|
|
||||||
* @return string|null The attribute value as stored in DB
|
|
||||||
*/
|
|
||||||
public function to_db($value) {
|
|
||||||
$value = parent::from_db($value);
|
|
||||||
if (is_null($value))
|
|
||||||
return null;
|
|
||||||
if (!in_array($value, $this -> possible_values))
|
|
||||||
throw new DbException(
|
|
||||||
"Unexpected value '%s'. Should be one of the following values: %s",
|
|
||||||
$value, implode(', ', $this -> possible_values)
|
|
||||||
);
|
|
||||||
return $value;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Loading…
Reference in a new issue