From 63dc17d0abc398c135f6d049d28174c3925416c9 Mon Sep 17 00:00:00 2001 From: George Halkiadakis Date: Wed, 24 May 2023 05:06:01 +0300 Subject: forming default values for auto fields --- core/classes/Registry.php | 49 ++++++++++------- core/classes/Request.php | 5 +- core/classes/authentication/Core/UserManager.php | 7 ++- public/app/config/setup_application.php | 9 +-- public/app/controllers/Auth.php | 56 +++++++++++++++++-- public/app/controllers/JsonToForm.php | 30 ++++++---- public/app/controllers/Office.php | 35 +++++++++--- public/app/extends/App_manager.php | 17 ++++++ public/app/extends/App_user.php | 70 ++++++++++++++++++++++++ public/app/routes/frontend.php | 7 ++- public/app/routes/user.php | 16 +++++- public/app/views/user/invitation.php | 14 ++--- tests/obj.php | 0 13 files changed, 250 insertions(+), 65 deletions(-) mode change 100644 => 100755 tests/obj.php diff --git a/core/classes/Registry.php b/core/classes/Registry.php index 047fd54..119e2b9 100644 --- a/core/classes/Registry.php +++ b/core/classes/Registry.php @@ -3,16 +3,18 @@ /** Registry * --- * - * Regisrty is a global repository of {key: value} pairs, - * where key is a friendly label, and... - * value can be anything (numbet|string|array|object etc) + * Regisrty is a global repository of { Key : Value } pairs, + * where Key is a friendly label, and... + * Value can be almost anything (number|string|array|object etc) * - * It has a central role in the framework. - * By default it holds the Request instance and several - * othe core instanses like database connections etc. + * Usually it has a central role in the framework; + * Can hold the Request instance and several other core instanses + * like database connections, cache services, etc. * - * It implements the Facade design pattern; - * it defines a new interface for existing objects. + * Designwise, Registry is a Singleton class + * implementing (both) the Singleton and the Facade design pattern + * allowing easy binding of core-singleton interfaces and objects + * into the application * * --- */ @@ -22,16 +24,23 @@ class Registry * ------------------------------------------------------------------------- */ - /** keeps all ready-to-use records - * --- - * These are all records defined via Registry::set(), and - * vow-records defined (Registry::vow()) AND called (Registry::use()) - * (it does not include the vows that have not carried-out yet ) + /** @var $records (array) + * keeps all ready-to-use records + * --- --- -- - - - + * + * These are set-records [defined via Registry::set()], + * or + * called vow-records [called via Registry::use()] + * + * NOTE: + * $records does not include the vows that have not carried-out yet */ private static $records = Array(); - /** keeps all records that will be carried-out later (if ever) - * --- + /** @var $wish (array) + * keeps all records that will be carried-out later (if ever) + * --- -- -- - - - + * * These are registered via Registry::vow() */ private static $wish = Array(); @@ -42,8 +51,9 @@ class Registry */ /** set - * --- + * --- -- -- - - - * store $data to the registry undel the label $key + * * @param $key (string) * @param $data * */ @@ -66,9 +76,10 @@ class Registry /** get - * --- + * --- -- - - - * get data from registry, * stored under the label $key + * * @param $key (string): the label * @return data (mixed) */ @@ -84,12 +95,12 @@ class Registry /** vow - * --- + * --- -- -- - - - * implements a promise of a function operation * when/if the registry label is called; * it can store a function, an object handler etc. * - * example: + * @example: * Registry::vow('key', function(){ return new Obj(); }) * * The main advantage of using a 'vow' vs a 'set' is diff --git a/core/classes/Request.php b/core/classes/Request.php index 89928f9..7f64d64 100644 --- a/core/classes/Request.php +++ b/core/classes/Request.php @@ -154,8 +154,5 @@ class Request // $this->userResolver = $callback; // return $this; } - - - + } - diff --git a/core/classes/authentication/Core/UserManager.php b/core/classes/authentication/Core/UserManager.php index 54cd2cb..4d9da7b 100644 --- a/core/classes/authentication/Core/UserManager.php +++ b/core/classes/authentication/Core/UserManager.php @@ -42,6 +42,8 @@ class UserManager implements UserManagerInterface /** hasUserToken() * == is user loged in * + * @param void + * @return boolean */ public function hasUserToken(): bool { @@ -55,9 +57,12 @@ class UserManager implements UserManagerInterface * checks if user is granded some role(s) * from the array of roles that are passed * - * ex. $token->isGranted(['editor', 'designer']) ... + * @example: + * $token->isGranted(['editor', 'designer']) ... * returns true if the user is editor or designer (or both) * + * @param $roles (array) : array of (int) role IDs + * @return boolean */ public function isGranted(array $roles): bool { diff --git a/public/app/config/setup_application.php b/public/app/config/setup_application.php index b87ffae..54f14de 100644 --- a/public/app/config/setup_application.php +++ b/public/app/config/setup_application.php @@ -327,12 +327,7 @@ define('APPLICATION_FORM', [ [ 'name' => 'belonging_school', 'label' => 'Σχολείο τοποθέτησης', - 'attributes' => ['required'] - ], - [ - 'name' => 'working_school', - 'label' => 'Σχολείο εργασίας', - 'attributes' => ['required'] + 'attributes' => ['required', 'auto'] ], [ 'name' => 'date', @@ -347,7 +342,7 @@ define('APPLICATION_FORM', [ 'type' => 'select', 'class' => 'col-7', 'source' => 'positions', - 'attributes' => ['required'] + 'attributes' => ['required', 'auto'] ], [ 'name' => 'request', diff --git a/public/app/controllers/Auth.php b/public/app/controllers/Auth.php index f1e9819..e191c53 100644 --- a/public/app/controllers/Auth.php +++ b/public/app/controllers/Auth.php @@ -45,7 +45,16 @@ class Auth { ->setName($record['first_name'] .' '. $record['last_name']) ->setPassword($record['password']) ->setRoles([ $record['role_id'] ]) // roles is an array - ->setEnabled($record['active']); + ->setEnabled( ($record['active'] == 0) ? false : true ) + ->set('prefix', $record['prefix']) + ->set('first_name', $record['first_name']) + ->set('last_name', $record['last_name']) + ->set('father_name', $record['father_name']) + ->set('email', $record['email']) + ->set('sector', $record['sector']) + ->set('position', $record['position']) + ->set('registration_number', $record['registration_number']) + ->set('belonging_school', $record['belonging_school']); // let user manager to validate user credentials @@ -79,7 +88,7 @@ class Auth { $userManager->createUserToken($user); return [ 'success' => true, - 'goto' => '/admin/panel' + 'goto' => '/panel' ]; } else { @@ -106,13 +115,13 @@ class Auth { $check = Access_model::activate_set_password($req->POST, $password); if ($check != 0) { - Render::view('/error/general', [ + Render::json([ 'title' => ACCOUNT_ACTIVATED_TITLE, 'message' => ACCOUNT_ACTIVATED_MESSAGE . '
(msg code: '. $check .')' ]); } else { - Render::view('/error/general', [ + Render::json([ 'title' => NOT_VALID_ACTIVATION_TITLE, 'message' => NOT_VALID_ACTIVATION_MESSAGE ]); @@ -195,6 +204,23 @@ class Auth { * @return true|false */ public static function is_connected() + { + $manager = new App_manager(); + if ($manager->hasUserToken()) { + + return true; + + } else { + // user is not connected; + return false; + } + } + + + /** user_data + * returns user's public data + */ + public static function user_data() { $manager = new App_manager(); if ($manager->hasUserToken()) { @@ -203,11 +229,29 @@ class Auth { $token = $manager->getUserToken(); $user = $token->getUser(); - return $user; + return [ + 'id' => $user->getID(), + 'name' => $user->getName(), + 'roles' => $user->getRoles(), + + 'sex' => $user->getSex(), + 'active' => $user->isEnabled(), + 'prefix' => $user->get('prefix'), + 'first_name' => $user->get('first_name'), + 'last_name' => $user->get('last_name'), + 'father_name' => $user->get('father_name'), + 'email' => $user->get('email'), + 'sector' => $user->get('sector'), + 'position' => $user->get('position'), + 'registration_number' => $user->get('registration_number'), + 'belonging_school' => $user->get('belonging_school') + + // 'token' => $token->serialize() + ]; } else { // user is not connected; - return false; + return [ 'user' => 0 ]; } } diff --git a/public/app/controllers/JsonToForm.php b/public/app/controllers/JsonToForm.php index f6e9746..1e0b99b 100644 --- a/public/app/controllers/JsonToForm.php +++ b/public/app/controllers/JsonToForm.php @@ -113,25 +113,28 @@ class JsonToForm return (mb_strlen($str) > $len) ? mb_substr($str, 0, $len-1) ."…" : $str; } + /** json_form - * + * ------------------------------------------------------------------------- * construct a form (html, javasctipt) * from a json designer * - * @param $formJson (Std Object): form descriptor - * @param $require_identity (array): array of properties and values; + * @param stdClass $formJson : form descriptor + * + * @param array $require_identity : array of properties and values; * they identify the record; injected into form as hidden fields - * @param $source (array): array of [key => value] pairs, where ... - * `key`: the name of field, `value`: the default value of this field + * + * @return array [ html=>(string), init_js=>(string), values_js=>String ] + * ------------------------------------------------------------------------- */ - public static function json_form($formJson, $require_identity = [], $source = []) + public static function json_form($formJson, $require_identity = []) { // $formJson = json_decode(json_encode($form)); $default_outer = $formJson->defaults->outer_class ?? ''; $default_inner = $formJson->defaults->inner_class ?? ''; $default_type = $formJson->defaults->type ?? 'text'; - $default_values = $formJson->defaults->values ?? ((object) ['nothing' => true]); + $default_values = $formJson->defaults ?? ((object) ['nothing' => true]); // TODO handle $form->defaults->source; @@ -186,9 +189,16 @@ class JsonToForm // handle value assignment if (isset($field->value)) { // if value property is set $set_value = true; // (flag: value seted) - $value = (($field->value == 'auto') && isset($default_values->$name)) - ? $default_values->$name // set auto value - : $field->value; // or other set-value + $value = $field->value; + // $value = (($field->value == 'auto') && isset($default_values->$name)) + // ? $default_values->$name // set auto value + // : $field->value; // or other set-value + + } else if (in_array('auto', $attributes) && isset($default_values->$name)) { + $set_value = true; // (flag: value seted) + $value = $default_values->$name; // set auto value + // echo "\n{$name}: $value"; + } else { $set_value = false; } diff --git a/public/app/controllers/Office.php b/public/app/controllers/Office.php index d0c10b9..9eb2a4c 100644 --- a/public/app/controllers/Office.php +++ b/public/app/controllers/Office.php @@ -162,25 +162,44 @@ class Office { private static function render_application_form($opts) { + // #1: user-id --------------------------------------------------------- $user_id = $opts['user']->getID(); - + + // #2: form template --------------------------------------------------- $form_setup = json_decode(json_encode(APPLICATION_FORM, JSON_UNESCAPED_UNICODE)); - // get Form's HTML and Jsvascript + // #3: inject default values ------------------------------------------- + + // #3.1: inject user data + $form_setup->defaults = json_decode( json_encode( + Auth::user_data(), JSON_UNESCAPED_UNICODE + )); + // #3.2 inject `sector` and `position`