summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGeorge Halkiadakis <gchalkiadakis@sklavenitis.co.gr>2023-05-24 05:06:01 +0300
committerGeorge Halkiadakis <gchalkiadakis@sklavenitis.co.gr>2023-05-24 05:06:01 +0300
commit63dc17d0abc398c135f6d049d28174c3925416c9 (patch)
tree3c68552b737096b073368ac8d874b0cd46640adb
parent2f5f034a97fd39b372d9c23b1b04db74c4469f22 (diff)
downloadgyraf1gov-63dc17d0abc398c135f6d049d28174c3925416c9.tar.gz
gyraf1gov-63dc17d0abc398c135f6d049d28174c3925416c9.tar.bz2
gyraf1gov-63dc17d0abc398c135f6d049d28174c3925416c9.zip
forming default values for auto fields
-rw-r--r--core/classes/Registry.php49
-rw-r--r--core/classes/Request.php5
-rw-r--r--core/classes/authentication/Core/UserManager.php7
-rw-r--r--public/app/config/setup_application.php9
-rw-r--r--public/app/controllers/Auth.php56
-rw-r--r--public/app/controllers/JsonToForm.php30
-rw-r--r--public/app/controllers/Office.php35
-rw-r--r--public/app/extends/App_manager.php17
-rw-r--r--public/app/extends/App_user.php70
-rw-r--r--public/app/routes/frontend.php7
-rw-r--r--public/app/routes/user.php16
-rw-r--r--public/app/views/user/invitation.php14
-rwxr-xr-x[-rw-r--r--]tests/obj.php0
13 files changed, 250 insertions, 65 deletions
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 . '<br>(msg code: '. $check .')'
]);
} else {
- Render::view('/error/general', [
+ Render::json([
'title' => NOT_VALID_ACTIVATION_TITLE,
'message' => NOT_VALID_ACTIVATION_MESSAGE
]);
@@ -199,15 +208,50 @@ class Auth {
$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()) {
+
// user is connected;
$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` <option>s
+
+ // #4: get Form's HTML and Javascript ----------------------------------
$form = JsonToForm::json_form($form_setup, [
['name' => 'user_id', 'value' => $user_id] // pass user identity
]);
+ // #5: render the view -------------------------------------------------
Render::view('templates/application', ['form' => $form]);
}
+
+ /** render penalty form
+ *
+ */
private static function render_penalty_form($opts)
{
- $user_id = $opts['user']->getID();
+ // #1: user-id ---------------------------------------------------------
+ $user_id = $opts['user']->getID();
+ // #2: form template ---------------------------------------------------
$form_setup = json_decode(json_encode(PENALTY_FORM, JSON_UNESCAPED_UNICODE));
- // get all teachers
+ // #3: prepare values to inject; then inject ---------------------------
+
+ // #3.1: get all teachers
$teachers_array = Registry::use('database')->runQuery(
"SELECT concat(last_name, ' ', first_name) as TeacherName
FROM user ORDER BY TeacherName", []
@@ -189,9 +208,8 @@ class Office {
foreach($teachers_array as $key => $person) {
$teachers[] = $person['TeacherName'];
}
- // print_r( $teachers); die();
- // get $key of rapporteur, president and members inside the form_setup->form array
+ // #3.2: get $key of rapporteur, president and members <select>s
for($i=0; $i < sizeof($form_setup->form) ; $i++) {
if (isset($form_setup->form[$i]->name)) {
if ($form_setup->form[$i]->name == 'rapporteur') { $keyRapporteur = $i; }
@@ -200,16 +218,17 @@ class Office {
}
}
- // set option sources for rapporteur, president and members
+ // #3.3: inject <option>s for `rapporteur`, `president` and `members`
$form_setup->form[$keyRapporteur]->options = $teachers;
$form_setup->form[$keyPresident]->options = $teachers;
$form_setup->form[$keyMembers]->options = $teachers;
- // get Form's HTML and Jsvascript
+ // #4: get Form's HTML and Javascript ----------------------------------
$form = JsonToForm::json_form($form_setup, [
['name' => 'user_id', 'value' => $user_id] // pass user identity
]);
+ // #5: render the view -------------------------------------------------
Render::view('templates/penalty', ['form' => $form]);
}
diff --git a/public/app/extends/App_manager.php b/public/app/extends/App_manager.php
index b003015..7641530 100644
--- a/public/app/extends/App_manager.php
+++ b/public/app/extends/App_manager.php
@@ -20,6 +20,10 @@ class App_manager extends UserManager
protected $user;
+
+ /** constructor
+ * --- -- -- - - -
+ */
public function __construct()
{
parent::__construct();
@@ -53,6 +57,19 @@ class App_manager extends UserManager
}
+
+// /** createUserToken()
+// * == serializes user and stores it into session
+// * so $_SESSION[DEFAULT_PREFIX_KEY] has the serialized representaion of user
+// */
+// public function createUserToken(UserInterface $user): UserTokenInterface
+// {
+// $userToken = new UserToken($user);
+// $_SESSION[UserTokenInterface::DEFAULT_PREFIX_KEY] = $userToken->serialize();
+//
+// return $userToken;
+// }
+
/** ??
*
*/
diff --git a/public/app/extends/App_user.php b/public/app/extends/App_user.php
index 6e93969..bb3d61d 100644
--- a/public/app/extends/App_user.php
+++ b/public/app/extends/App_user.php
@@ -31,6 +31,21 @@ class App_user extends User
*/
private $name;
+ /** other properties
+ * to keep near-by for easy access
+ * all @var string
+ */
+ private $prifix;
+ private $first_name;
+ private $last_name;
+ private $father_name;
+ private $registration_number;
+ private $email;
+ private $sector;
+ private $belonging_school;
+ private $position;
+
+
/** SETTERS AND GETTERS
@@ -111,6 +126,61 @@ class App_user extends User
return $this;
}
+
+ // all other properties
+ // --- -- -- - - -
+
+ /** set
+ * general seter for all other properties
+ *
+ * @param $prop (string): property name
+ * @param $val (string): property value
+ * @return this (object)
+ */
+ public function set(string $prop, string $val)
+ {
+ switch ($prop) {
+ case 'prefix': $this->prefix = $val; return $this; break;
+ case 'first_name': $this->first_name = $val; return $this; break;
+ case 'last_name': $this->last_name = $val; return $this; break;
+ case 'father_name': $this->father_name = $val; return $this; break;
+ case 'email': $this->email = $val; return $this; break;
+ case 'sector': $this->sector = $val; return $this; break;
+ case 'position': $this->position = $val; return $this; break;
+ case 'registration_number':
+ $this->registration_number = $val; return $this; break;
+ case 'belonging_school':
+ $this->belonging_school = $val; return $this; break;
+
+ default:
+ return $this;
+ }
+ }
+
+
+ /** get
+ * general getter for all properties
+ *
+ * @param $prop (string): property name
+ * @return (string): property value (or null if propety not exists)
+ */
+ public function get(string $prop)
+ {
+ switch ($prop) {
+ case 'prefix': return $this->prefix; break;
+ case 'first_name': return $this->first_name; break;
+ case 'last_name': return $this->last_name; break;
+ case 'father_name': return $this->father_name; break;
+ case 'email': return $this->email; break;
+ case 'sector': return $this->sector; break;
+ case 'position': return $this->position; break;
+ case 'registration_number': return $this->registration_number; break;
+ case 'belonging_school': return $this->belonging_school; break;
+
+ default:
+ return null;
+ }
+ }
} \ No newline at end of file
diff --git a/public/app/routes/frontend.php b/public/app/routes/frontend.php
index 47b1816..14152c1 100644
--- a/public/app/routes/frontend.php
+++ b/public/app/routes/frontend.php
@@ -14,7 +14,10 @@ Route::add('/', function() { Render::view('user/login'); });
Route::add('/invitation/([0-9]*)', function($id) {
- Auth::invitation($id);
+ if ($id == "" || $id == "0") { Render::view('error/404');
+ } else {
+ Auth::invitation($id);
+ }
});
// new petition
@@ -27,6 +30,8 @@ Route::add('/petition/([0-9a-z\-_]*)/([0-9a-f]*)', function($tag) {
Office::filter_request_petition($tag, $id);
});
+
+
// Error pages
////////////////////////////////////////////////////////////////////////////////
diff --git a/public/app/routes/user.php b/public/app/routes/user.php
index a2d48d6..aa3b319 100644
--- a/public/app/routes/user.php
+++ b/public/app/routes/user.php
@@ -3,6 +3,7 @@
use app\controllers\Auth;
use app\controllers\Classroom_user;
+
/** User Connection Management Routes
* -----------------------------------------------------------------------------
*/
@@ -11,7 +12,12 @@ use app\controllers\Classroom_user;
// --- -- -- - - -
// request login ... -> then sends to POST:/account/check-login
-Route::add('/login', function() { Render::view('user/login'); });
+Route::add('/login', function() {
+ if (Auth::is_connected()) { header('Location: /panel');
+ } else {
+ Render::view('user/login');
+ }
+});
// request logout
Route::add('/logout', function() {
@@ -33,9 +39,13 @@ Route::add('/invite', function() {
});
Route::add('/panel', function() {
- Render::json(['connected' => Auth::is_connected()]);
+ Render::json([
+ 'connected' => Auth::is_connected(),
+ 'user' => Auth::user_data(),
+ ]);
});
+
// Replies to common requests (POST method)
// --- -- -- - - -
@@ -81,7 +91,9 @@ Route::add('/account/profile', function() {
// test connection
if (!PRODUCTION) {
+
Route::add('/check/connection', function() { Auth::is_connected(); });
+
}
diff --git a/public/app/views/user/invitation.php b/public/app/views/user/invitation.php
index 564b722..34cfbf6 100644
--- a/public/app/views/user/invitation.php
+++ b/public/app/views/user/invitation.php
@@ -211,13 +211,13 @@ $(document).ready(function() {
};
console.log(data);
-// var request = '/account/activate'; // set action url
-//
-// // send POST request
-// $.post(request, data)
-// .done(function( data ) {
-// $('.office .content-form').html(data.message)
-// });
+ var request = '/account/activate'; // set action url
+
+ // send POST request
+ $.post(request, data)
+ .done(function( data ) {
+ $('.office .content-form').html(`<h4>${data.title}<h4><br>${data.message}`)
+ });
}
});
diff --git a/tests/obj.php b/tests/obj.php
index 8537381..8537381 100644..100755
--- a/tests/obj.php
+++ b/tests/obj.php