diff --git a/example/includes/cli.php b/example/includes/cli.php index 39e772a..5198811 100644 --- a/example/includes/cli.php +++ b/example/includes/cli.php @@ -66,7 +66,7 @@ function cli_list($command_args) { case '-s': case '--status': $i++; - if(!check_status($command_args[$i])) + if(!Item :: check_status($command_args[$i])) Cli :: usage('Invalid -s/--status clause'); $params['filters']['status'] = $command_args[$i]; break; @@ -124,7 +124,7 @@ Cli :: add_command( " - ".implode("\n - ", Item :: possible_orders()), ___("-r|--reverse Reverse order"), ___("-s|--status Filter on status. Possible values:"), - " - ".implode("\n - ", array_keys($status_list)), + " - ".implode("\n - ", array_keys(Item :: statuses())), ) ); diff --git a/example/includes/core.php b/example/includes/core.php index 0daaf07..aaaf6ed 100644 --- a/example/includes/core.php +++ b/example/includes/core.php @@ -57,16 +57,6 @@ $sentry_span = new SentrySpan('core.init', 'Core initialization'); 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('templates.php'); require_once('views/index.php'); diff --git a/example/includes/functions.php b/example/includes/functions.php index afd77b7..e7fc91f 100644 --- a/example/includes/functions.php +++ b/example/includes/functions.php @@ -6,15 +6,9 @@ use EesyPHP\Session; use EesyPHP\Tpl; use EesyPHP\Url; -use function EesyPHP\vardump; +use EesyPHPExample\Db\Item; -/* - * Check values helpers - */ -function check_status($status) { - global $status_list; - return array_key_exists($status, $status_list); -} +use function EesyPHP\vardump; /* * Handling item POST data @@ -41,7 +35,7 @@ function handle_item_post_data(&$info, $enabled_fields=null, $required_fields=nu // status if (!$enabled_fields || in_array('status', $enabled_fields)) { - if (isset($_POST['status']) && check_status($_POST['status'])) { + if (isset($_POST['status']) && Item :: check_status($_POST['status'])) { $info['status'] = $_POST['status']; } else { diff --git a/example/includes/templates.php b/example/includes/templates.php index 0304a4d..6f04c9b 100644 --- a/example/includes/templates.php +++ b/example/includes/templates.php @@ -7,6 +7,7 @@ use EesyPHP\Log; use EesyPHP\Tpl; use EesyPHP\Url; +use EesyPHPExample\Db\Item; use function EesyPHP\format_size; @@ -25,19 +26,15 @@ Tpl :: enable_security_mode( ); function define_common_template_variables($event) { - global $status_list, $admin; - Tpl :: assign( - 'status_list', - isset($status_list) && is_array($status_list)? - $status_list:array() - ); + global $admin; + Tpl :: assign('statuses', Item :: statuses()); Tpl :: assign('admin', isset($admin) && $admin); } Hook :: register('before_displaying_template', 'define_common_template_variables'); // Templates functions function smarty_item_status($params) { - global $status_list; + $statuses = Item :: statuses(); $status2class = array ( 'pending' => 'info', 'validated' => 'success', @@ -51,8 +48,8 @@ function smarty_item_status($params) { $class='danger'; echo ""; echo ( - array_key_exists($params['item']->status, $status_list)? - $status_list[$params['item']->status]: + array_key_exists($params['item']->status, $statuses)? + $statuses[$params['item']->status]: "Inconnu (".$params['item']->status.")" ); echo ""; diff --git a/example/includes/views/index.php b/example/includes/views/index.php index 310e27e..e13dd36 100644 --- a/example/includes/views/index.php +++ b/example/includes/views/index.php @@ -19,8 +19,6 @@ if (php_sapi_name() == "cli") * @return void */ function handle_search($request) { - global $status_list; - // Manage params if( (isset($_REQUEST['clear']) && $_REQUEST['clear']=='true') || @@ -38,9 +36,8 @@ function handle_search($request) { } Log :: debug('Request params : '.vardump($_REQUEST)); - $status_list['all'] = _('Any'); if (isset($_REQUEST['status'])) { - if (check_status($_REQUEST['status']) || $_REQUEST['status'] == 'all') + if (Item :: check_status($_REQUEST['status'], true)) $_SESSION['search']['filters']['status'] = $_REQUEST['status']; else Tpl :: assign('status_error', true); @@ -103,7 +100,7 @@ function handle_search($request) { Tpl :: assign('result', $result); Tpl :: assign('search', $_SESSION['search']); Tpl :: assign('nbs_by_page', $nbs_by_page); - Tpl :: assign('status_list', $status_list); + Tpl :: assign('status_choices', Item :: statuses(true)); Tpl :: add_css_file( 'lib/bootstrap5-dialog/css/bootstrap-dialog.min.css', @@ -149,8 +146,6 @@ Url :: add_url_handler('|^item/(?P[0-9]+)$|', 'handle_show'); * @return void */ function handle_create($request) { - global $status_list; - $info = array(); $field_errors = handle_item_post_data($info); if (isset($_POST['submit']) && empty($field_errors)) { @@ -171,15 +166,12 @@ function handle_create($request) { Tpl :: assign('submited', isset($_POST['submit'])); Tpl :: assign('info', $info); Tpl :: assign('field_errors', $field_errors); - Tpl :: assign('status_list', $status_list); Tpl :: display("form.tpl", _("New")); } Url :: add_url_handler('|^item/new$|', 'handle_create'); function handle_modify($request) { - global $status_list; - $item = Item :: get_from_url($request -> id); if(!$item) Url :: error_404(); @@ -217,7 +209,6 @@ function handle_modify($request) { Tpl :: assign('info', (!empty($info)?$info:$item)); Tpl :: assign('item_id', $item->id); Tpl :: assign('field_errors', $field_errors); - Tpl :: assign('status_list', $status_list); Tpl :: display("form.tpl", _("Element %s: Modification"), $item->name); } diff --git a/example/phpstan.neon b/example/phpstan.neon index eeed8e1..1d5c9e6 100644 --- a/example/phpstan.neon +++ b/example/phpstan.neon @@ -9,9 +9,3 @@ parameters: - EesyPHP\UrlRequest - EesyPHP\Auth\User treatPhpDocTypesAsCertain: false - ignoreErrors: - - - message: "#Variable \\$status_list might not be defined\\.#" - paths: - - includes/cli.php - diff --git a/example/src/Db/Item.php b/example/src/Db/Item.php index 6e81431..013a1f0 100644 --- a/example/src/Db/Item.php +++ b/example/src/Db/Item.php @@ -9,6 +9,7 @@ use EesyPHP\Tpl; use EesyPHP\Db\AttrBool; use EesyPHP\Db\AttrInt; +use EesyPHP\Db\AttrSet; use EesyPHP\Db\AttrStr; use EesyPHP\Db\AttrTimestamp; use EesyPHP\Db\DbObject; @@ -32,12 +33,43 @@ class Item extends DbObject { protected const DEFAULT_ORDER_DIRECTION = 'DESC'; 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 + */ + 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() { return [ 'id' => new AttrInt(['autoincrement' => true]), 'name' => new AttrStr(['required' => true]), 'date' => new AttrTimestamp(['default' => 'time']), - 'status' => new AttrStr(['required' => true, 'default' => 'pending']), + 'status' => new AttrSet([ + 'required' => true, + 'default' => 'pending', + 'possible_values' => array_keys(self :: statuses()), + ]), 'description' => new AttrStr(), ]; } diff --git a/example/templates/form.tpl b/example/templates/form.tpl index a7fa301..f87bb93 100644 --- a/example/templates/form.tpl +++ b/example/templates/form.tpl @@ -20,7 +20,7 @@
{if array_key_exists('status', $field_errors)}
{$field_errors['status']}
diff --git a/example/templates/search.tpl b/example/templates/search.tpl index 3f7c803..859663f 100644 --- a/example/templates/search.tpl +++ b/example/templates/search.tpl @@ -15,7 +15,7 @@