diff options
| author | George Halkiadakis <gchalkiadakis@sklavenitis.co.gr> | 2023-06-06 00:46:19 +0300 |
|---|---|---|
| committer | George Halkiadakis <gchalkiadakis@sklavenitis.co.gr> | 2023-06-06 00:46:19 +0300 |
| commit | 1388a64651f1c61a6aafc32f8e3e60f7a6ffd339 (patch) | |
| tree | dd5bcd7b438929ffec89d32011144831eebabb06 | |
| parent | d5475ce60729c3f743a6451b42072f1a13ffed59 (diff) | |
| download | gyraf1gov-1388a64651f1c61a6aafc32f8e3e60f7a6ffd339.tar.gz gyraf1gov-1388a64651f1c61a6aafc32f8e3e60f7a6ffd339.tar.bz2 gyraf1gov-1388a64651f1c61a6aafc32f8e3e60f7a6ffd339.zip | |
admin petition ready
25 files changed, 683 insertions, 85 deletions
diff --git a/public/app/config/setup_application.php b/public/app/config/setup_application.php index e7d2ca4..2c8b756 100644 --- a/public/app/config/setup_application.php +++ b/public/app/config/setup_application.php @@ -130,6 +130,8 @@ define('NOT_VALID_ACTIVATION_MESSAGE', 'Ο λογαριασμός δεν έχε define('ACCOUNT_UPDATED_TITLE','Ο λογαριασμός σας ενημερώθηκε!'); define('ACCOUNT_UPDATED_MESSAGE','Τώρα μπορείτε να επιστρέψετε <a href="/panel">στον πίνακα ελέγχου</a>.'); +define('RETRY_SET_PASSWORD', 'Μπορείτε <a href="/user/password_form">να ξαναδοκιμάσετε</a> ή + <br>ή να επιστρέψετε στον <a href="/panel">πίνακα ελέγψου</a>.'); @@ -204,6 +206,7 @@ define('GENDERED_ARTICLE',[ // gendered (grammar) articles + // the forms /////////////////////////////////////////////////////////////////// // ----------------------------------------------------------------------------- @@ -361,6 +364,37 @@ define('USER_PROPERTIES_FORM', [ ] ]); + + +define('SET_PASSWORD_FORM', [ + 'defaults' => [ + 'outer_class' => 'col-12', + 'inner_class' => 'form-control', + 'type' => 'text', + 'source' => '\app\controllers\User::current' + ], + 'form' => [ + [ + 'name' => 'oldpass', + 'label' => 'Τρέχον κωδικός πρόσβασης', + 'type' => 'password', + 'attributes' => ['required'] + ], + [ + 'name' => 'password', + 'label' => 'Νέος Κωδικός πρόσβασης', + 'type' => 'password', + 'attributes' => ['required'] + ], + [ + 'name' => 'confirm_password', + 'label' => 'Επαλήθευση νέου κωδικού πρόσβασης', + 'type' => 'password', + 'attributes' => ['required'] + ] + ] +]); + // Application (common request) form // --- define('APPLICATION_FORM', [ diff --git a/public/app/controllers/Auth.php b/public/app/controllers/Auth.php index 02a059f..f356dfe 100644 --- a/public/app/controllers/Auth.php +++ b/public/app/controllers/Auth.php @@ -23,7 +23,7 @@ class Auth { ## handle user's record / token / session ## ------------------------------------------------------------------------- - /** user from record + /** User from Record * * create and return a user based on the record of properties * @@ -94,7 +94,12 @@ class Auth { /** update session token + * ... for user by user-id * + * This method perfomes + * a new READ User's data from DataBase + * + * @param int $uid: user-id */ private static function reset_user_token($uid) { @@ -178,10 +183,9 @@ class Auth { } - /** AJAX POST: update_user * - * resolves call POST:/user/update_properties; + * resolves call POST:/user/update/properties; * updates user's proiperties with POSTed data * * @param void : all parametres passed via request->POST @@ -209,6 +213,46 @@ class Auth { } + /** AJAX POST: update_password + * + * resolves call POST:/user/update/password; + * updates user's proiperties with POSTed data + * + * @param void : all parametres passed via request->POST + */ + public static function update_password() + { + $req = Registry::get('REQUEST'); + $manager = new App_manager(); + $user = $manager->getUserToken()->getUser(); + + $record = Access_model::checkUser($user->get('email')); + + // confirm old-password + if ($manager->isPasswordValid($user, $req->POST['oldpass'])) { + + $check = Access_model::update_password( // then update pass + $user->getID(), + $manager->cryptPassword($req->POST['password']) + ); + + // user properties changed, so update user's session token + self::reset_user_token($user->getID()); + + Render::json([ + 'title' => ACCOUNT_UPDATED_TITLE, + 'message' => ACCOUNT_UPDATED_MESSAGE . ' (msg code: '. $check .')' + ]); + + } else { + Render::json([ + 'title' => 'Error', + 'message' => 'Λάθος Password<br>(msg code: 2)<br><br>'. RETRY_SET_PASSWORD + ]); + } + } + + ## serve user management forms ## ------------------------------------------------------------------------- @@ -311,6 +355,33 @@ class Auth { } + /** SERVE FORM: password_form + * + * renders a form for user to change password + * + * NOTE: + * after form is submited, client shall call Auth::update_user() + * + */ + public static function password_form() + { + $user = json_decode(json_encode(self::user_data(), JSON_UNESCAPED_UNICODE)); + + // format form_setup to a json array + $form_setup = json_decode(json_encode(SET_PASSWORD_FORM, JSON_UNESCAPED_UNICODE)); + + // get Form's HTML and Jsvascript + $form = JsonToForm::json_form($form_setup, [ + // pass invitation identity for security + ['name' => 'id', 'value' => $user->id] + ]); // print_r($form_setup); die(); + + Render::view('user/update_password', [ + 'form' => $form + ]); + } + + ## suplamentary methods ## ------------------------------------------------------------------------- diff --git a/public/app/controllers/Office.php b/public/app/controllers/Office.php index f57cf08..c67249a 100644 --- a/public/app/controllers/Office.php +++ b/public/app/controllers/Office.php @@ -327,9 +327,21 @@ class Office { + public static function all_petitions() + { + if (Auth::in_admin_group()) { + Render::json([ + 'success' => true, + 'data' => Content_model::all_petitions() + ]); + } + } + + + public static function petition_copy($sign) { - if (false) { //(Auth::in_admin_group()) { + if (Auth::in_admin_group()) { $petition = Content_model::get_petition(['signature' => $sign]); } else { @@ -407,7 +419,19 @@ class Office { ## ADMIN METHODS (insert, updated etc.) ## ------------------------------------------------------------------------- + /** set_protocol + * --- + */ + public static function set_protocol() + { + if (Auth::in_admin_group()) { + $post = Registry::get('REQUEST')->POST; + Content_model::set_protocol( $post['id'], $post['protocol'] ); + } + Render::json(['success' => true]); + } + /** files * echo all files * diff --git a/public/app/models/Access_model.php b/public/app/models/Access_model.php index 2d36285..c98b6c4 100644 --- a/public/app/models/Access_model.php +++ b/public/app/models/Access_model.php @@ -12,7 +12,7 @@ class Access_model ## User methods ## ------------------------------------------------------------------------- - /** check user (by index key) + /** check user (by index key = Email) * * @param $indexKey (string) : key for user identification * @param $value (string) @@ -135,8 +135,8 @@ class Access_model * * activate user and set password * - * @param $ticket (hex/MD5): activation code; - * + * @param array $post + * @param string $password: hased-password */ public static function activate_set_password($post, $password) { @@ -233,6 +233,37 @@ class Access_model } + /** update_password + * + * (re)set user's password + * + * @param int $id: user id + * @param string $hashpass: hashed password + */ + public static function update_password($uid, $hashpass) + { + // Update and set activate = true + $rowCount = Registry::use('database')->query( + "UPDATE user + SET `password` = :hashpass + WHERE id = :id", + [ + ':hashpass' => $hashpass, + ':id' => $uid + ] + )->rowCount(); + + // update history + if ($rowCount != 0) { + History_model::trackUserAccess( + $uid, TRACK_ACCOUNT, 'User password changed' + ); + } + + return $rowCount; + } + + ## Set permission methods ## ------------------------------------------------------------------------- diff --git a/public/app/models/Content_model.php b/public/app/models/Content_model.php index 1f145a0..cfe418f 100644 --- a/public/app/models/Content_model.php +++ b/public/app/models/Content_model.php @@ -91,7 +91,10 @@ class Content_model { public static function all_petitions() { return Registry::use('database')->runQuery( - "SELECT * FROM petition", + "SELECT petition.*, + CONCAT(user.last_name, ' ', user.first_name) as UserName + FROM petition + LEFT JOIN user ON user.id = petition.user_id", [] ); } diff --git a/public/app/routes/user.php b/public/app/routes/user.php index 2ecd3c1..e43eaa9 100644 --- a/public/app/routes/user.php +++ b/public/app/routes/user.php @@ -27,9 +27,9 @@ Route::add('/logout', function() { }); - -// request password reset -Route::add('/account/reset_password', function() { Auth::reset_password(); } ); +// TODO: +// // request password reset +// Route::add('/account/reset_password', function() { Auth::reset_password(); } ); // Replies to common requests (POST method) @@ -122,19 +122,34 @@ if (Auth::is_connected()) { // UPDATE user properties // ------------------------------------------------------------------------- - // request UPDADE FORM + // GET HTML: request UPDADE (properties) FORM // --- -- -- - - - Route::add('/user/update_form', function() { Auth::update_form(); }); - // update user (data via post) + // POST: update user (data via post) // --- -- -- - - - - Route::add('/user/update_properties', function() { + Route::add('/user/update/properties', function() { Auth::update_user(); }, 'post'); + // GET HTML: request (update) PASSWORD FORM + // --- -- -- - - - + Route::add('/user/password_form', function() { + Auth::password_form(); + }); + + // POST: update user (data via post) + // --- -- -- - - - + Route::add('/user/update/password', function() { + Auth::update_password(); + }, 'post'); + + + + // CRUD content // ------------------------------------------------------------------------- @@ -154,12 +169,24 @@ if (Auth::is_connected()) { if (Auth::in_admin_group()) { - // administer petitions + // GET HTML: administer petitions (page) // --- -- -- - - - Route::add('/admin/petitions', function() { - Render::view('user/panel'); + Render::view('templates/admin_petitions'); + }); + + + // GET JSON: request RESULTS list my petitions + Route::add('/admin/get/petitions', function() { + Office::all_petitions(); }); + + // POST AJAX: set petition protocol + Route::add('/admin/set/protocol', function() { + Office::set_protocol(); + }, 'post'); + // invite user(s) // --- -- -- - - - Route::add('/admin/invite', function() { diff --git a/public/app/views/components/menu.php b/public/app/views/components/menu.php index 3e1748d..30ed673 100644 --- a/public/app/views/components/menu.php +++ b/public/app/views/components/menu.php @@ -16,7 +16,7 @@ <li class="list-group-item separator">Προφίλ</li> <li class="list-group-item"><a href="/user/update_form">Ενημέρωση στοιχείων</a></li> - <li class="list-group-item"><a href="/user/change_password">Αλλαγή password</a></li> + <li class="list-group-item"><a href="/user/password_form">Αλλαγή password</a></li> <?php if ($is_admin) : ?> <li class="list-group-item separator">Διαχείριση</li> diff --git a/public/app/views/js/submit/update-password.php b/public/app/views/js/submit/update-password.php new file mode 100644 index 0000000..625d7cd --- /dev/null +++ b/public/app/views/js/submit/update-password.php @@ -0,0 +1,61 @@ +<script> + $(document).ready(function() { + + // When form is submited + //////////////////////////////////////////////////////////////////////////// + + $('form').submit( event => { + event.preventDefault(); + + if ($('input[name=password]').val() != $('input[name=confirm_password]').val()) { + alert('Τα δυο κωδικοί πρόσβασης δεν ταιριάζουν!'); + + } else if ($('input[name=password]').val() == "") { + alert('Ο κωδικός πρόσβασης δεν μπορεί να είναι κενός') + + } else if ( ($('input[name=password]').val() == '0') + || ($('input[name=password]').val() =='') ) { + alert('Παρκαλώ συμπληρώστε ένα τηλέφωνο επικοινωνίας') + + } else if ($('input[name=oldpass]').val() == "") { + alert('Παρκαλώ συμπληρώστε τον τρέχοντα κωδικό πρόβασης') + + } else { + + // prepare data to POST + var data = { + // identification fields + id: $('input[name=id]').val(), + // data fields + oldpass: $('input[name=oldpass]').val(), + password: $('input[name=password]').val(), + }; + console.log(data); + + var request = '/user/update/password'; // set action url + + // send POST request + $.post(request, data) + .done(function( data ) { + $('.office .content-form').html(`<h4>${data.title}<h4><br>${data.message}`) + }); + } + + }); + + + + // go-back event + //////////////////////////////////////////////////////////////////////////// + + $('.btn-back2panel').click( event => { + event.preventDefault(); + + let confirm_exit = confirm('<?=CONFIRM_EXIT?>'); + if (confirm_exit) { + window.location.href = '/panel'; + } + }); + + }); +</script>
\ No newline at end of file diff --git a/public/app/views/js/submit/update-properties.php b/public/app/views/js/submit/update-properties.php index 651ef3e..f793619 100644 --- a/public/app/views/js/submit/update-properties.php +++ b/public/app/views/js/submit/update-properties.php @@ -24,7 +24,7 @@ }; console.log(data); - var request = '/user/update_properties'; // set action url + var request = '/user/update/properties'; // set action url // send POST request $.post(request, data) @@ -34,5 +34,19 @@ }); + + + // go-back event + //////////////////////////////////////////////////////////////////////////// + + $('.btn-back2panel').click( event => { + event.preventDefault(); + + let confirm_exit = confirm('<?=CONFIRM_EXIT?>'); + if (confirm_exit) { + window.location.href = '/panel'; + } + }); + }); </script>
\ No newline at end of file diff --git a/public/app/views/templates/admin_petitions.php b/public/app/views/templates/admin_petitions.php new file mode 100644 index 0000000..59c85f6 --- /dev/null +++ b/public/app/views/templates/admin_petitions.php @@ -0,0 +1,135 @@ +<!DOCTYPE html> +<html lang="en"> +<head> + <meta charset="utf-8"> + <title><?=SITE_TITLE?> - Control Panel</title> + + <?php // header includes + //////////////////////////////////////////////////////////////////////// + Render::view('components/header_includes'); + ?> +<style> +:root { /* login-form overides */ + --form-content-font-size: 1rem; + --form-input-height: 34px; + --select2-arrow-top: 1px; +} +</style> +</head> +<body class="office central-form left-menu"> + + <div class='container'> + <div class='content row'> + + <div class="col-sm-12"> + +<div class="manage"> + <!-- title bar --> + <div class="title-bar"> + <div class="row"> + <h5 class="col-md-10">Διαχείριση Αιτήσεων και Δοικητικών Εγγράφων</h5> + <div class="col-md-2"> + <a type="button" class="btn btn-sm btn-back2panel pull-right" href="/panel"> + Επιστροφή + </a> + </div> + </div> + + </div> + + + <div class="row"> + <div class="col-md-12"> + + <div id="petitions"> + + <table id="dt-petitions" class="table table-responsive dt-table" style="width:100%"> + <thead> + <!--<th class="dt-id">α/α</th>--> + <th class="dt-subject">Θέμα</th> + <th class="dt-protocol">Αρ.Πρωτόκολλου</th> + <th class="dt-user">Χρήστης</th> + <!-- <th class="dt-signature">Signature</th> --> + <th class="dt-datepost">Ημερ. Καταχώρισης</th> + <th class="dt-actions"></th> + </thead> + </table> + + </div> + + </div> + </div> + +</div><!-- /manage --> + + </div> + + </div> + </div> + + + + + <!-- MODAL for set protocol number --> + <div class="modal fade" id="managePetition" aria-labelledby="myModalLabel" aria-hidden="true"> + <div class="modal-dialog modal-lg" role="document"> + <div class="modal-content"> + + <form method="post" action=""> + + <div class="modal-header"> + <h4 class="modal-title" id="myModalLabel">Ενημέρωση αριθμού πρωτόκολλου</h4> + <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button> + </div><!-- /modal-header --> + + <!-- modal-body --> + <div class="modal-body"> + + <div class="row form-group"> + <label class="control-label col-sm-12" for="protocol">Αριθμός Πρψτοκόλλου</label> + <div class="col-sm-12"> + <input class="form-control form-control-sm" type="text" name="protocol" value="" required=""> + </div> + </div> + + <div class="row form-group"><!-- hiddens --> + <input type="hidden" name="id" value="0"><!-- petition-id --> + <input type="hidden" name="signature" value=""><!-- signature --> + </div> + + </div><!-- /modal body --> + + <div class="modal-footer"> + <div class="row form-actions" style="width: 50%;"> + + <div class="col-sm-4"> + <button type="button" class="btn btn-default js-modal-close col-12" data-bs-dismiss="modal">Κλείσιμο</button> + </div> + <div class="col-sm-8"> + <button class="btn btn-primary col-12" type="submit">Καταχώριση</button> + </div> + + </div> + </div><!-- /modal-footer --> + + </form><!-- /form --> + + </div> + </div> + </div> + + </body> + +<script src="/assets/js/panel/admin_petitions.js"></script> + + <!-- scripts + * handle show petition request + + if admin: + * modal + give protocol-number + --> +<?php // credits + ////////////////////////////////////////////////////////////////////////////// + Render::view('js/credits'); +?> +</html> diff --git a/public/app/views/user/new-password.php b/public/app/views/user/new-password.php deleted file mode 100644 index 06e7fac..0000000 --- a/public/app/views/user/new-password.php +++ /dev/null @@ -1,65 +0,0 @@ -<!DOCTYPE html> -<html lang="en"> -<head> - <meta charset="utf-8"> - <title><?=SITE_TITLE?> - Επαναφορά κωδικού πρόσβασης</title> - - <link rel="preconnect" href="https://fonts.googleapis.com"> - <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin> - <link href="https://fonts.googleapis.com/css2?family=Roboto+Slab:wght@400&display=swap" rel="stylesheet"> - - <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-aFq/bzH65dt+w6FI2ooMVUpc+21e0SRygnTpmBvdBgSdnuTN7QbdgL+OapgHtvPp" crossorigin="anonymous"> - - <link href="/assets/css/overides.css" rel="stylesheet"> - - <style> - html,body{ - padding:0; margin:0; - font-size:1.25em; - color: #037; background: #eee; - line-height:1.2em; - } - h3 { - font-size: 2em; font-style: normal; - font-weight: 400; - color: #05a; - margin: 0; padding: 5px; - opacity: 0.27; - border-bottom: 1px solid #05a; - } - .container { display: flex; height: 100vh; align-items:center; } - .content { position: relative; - max-width:480px; width: 100%; - margin: auto auto; padding: 1.5em; - justify-center: center; - background: #ddd; - border-radius: 8px; - box-shadow: 1px 5px 7px #7777; - } - </style> -</head> -<body class="classroom"> - <div class='container'> - <div class='content'> - <h4>Επαναφορά κωδικού πρόσβασης</h4> - <hr /> - <form method="post" action="/login-check"> - <div class="mb-3"> - <label for="name" class="form-label">Κωδικός μίας χρήσης</label> - <input type="text" class="form-control form-control-sm" name="name" required> - </div> - <div class="mb-3"> - <label for="exampleInputPassword1" class="form-label">Νέος κωδικός πρόσβασης</label> - <input type="password" class="form-control form-control-sm" name="password" required> - </div> - <div class="mb-3"> - <label for="exampleInputPassword2" class="form-label">Επιβεβαίωση κωδικού πρόσβασης</label> - <input type="password" class="form-control form-control-sm" name="password2" required> - </div> - <br /> - <button type="submit" class="btn btn-primary btn-sm col-12">Επιβεβαίωση και Είσοδος</button> - </form> - </div> - </div> -</body> -</html> diff --git a/public/app/views/user/update_password.php b/public/app/views/user/update_password.php new file mode 100644 index 0000000..38494ad --- /dev/null +++ b/public/app/views/user/update_password.php @@ -0,0 +1,73 @@ +<!DOCTYPE html> +<html lang="en"> +<head> + <meta charset="utf-8"> + <title><?=SITE_TITLE?> - Αλλαγή Password</title> + + <?php // header includes + //////////////////////////////////////////////////////////////////////// + Render::view('components/header_includes'); + ?> +</head> +<body class="office central-form"> + + <div class='container'> + <div class="content max420px"> + + <div class='content-form'> + <form method="post"> + <div> + <h4> + Αλλαγή Password + <button class="btn btn-back2panel btn-sm"> + Επιστροφή + </button> + </h4> + <hr/> + + <?=$form['html']?> + + <br /> + <button type="submit" class="btn btn-primary btn-sm col-12">Ενημέρωση</button> + + </div> + </form> + </div> + + </div> + </div> + +</body> + +<script>// initialize needed global variables + var values = {}; + var empty = ''; +</script> + +<script> +$(document).ready(function() { + + // intializations from form-definition + //////////////////////////////////////////////////////////////////////////// + + <?=$form['init_js']?> + + + // set known values + //////////////////////////////////////////////////////////////////////////// + + <?=$form['values_js']?> + +}); +</script> + +<?php // handle form submition + //////////////////////////////////////////////////////////////////////////// + Render::view('js/submit/update-password'); +?> + +<?php // credits + ////////////////////////////////////////////////////////////////////////////// + Render::view('js/credits'); +?> +</html> diff --git a/public/app/views/user/update_properties.php b/public/app/views/user/update_properties.php index f10e36b..7da976d 100644 --- a/public/app/views/user/update_properties.php +++ b/public/app/views/user/update_properties.php @@ -17,7 +17,12 @@ <div class='content-form'> <form method="post"> <div> - <h4>Ενημέρωση στοιχείων</h4> + <h4> + Ενημέρωση στοιχείων + <button class="btn btn-back2panel btn-sm"> + Επιστροφή + </button> + </h4> <hr/> <?=$form['html']?> diff --git a/public/app/views/admin/admin-menu.php b/public/app/views/xx--admin/admin-menu.php index 52e8522..52e8522 100644 --- a/public/app/views/admin/admin-menu.php +++ b/public/app/views/xx--admin/admin-menu.php diff --git a/public/app/views/admin/categories.php b/public/app/views/xx--admin/categories.php index 26fd5a2..26fd5a2 100644 --- a/public/app/views/admin/categories.php +++ b/public/app/views/xx--admin/categories.php diff --git a/public/app/views/admin/edit_lesson.php b/public/app/views/xx--admin/edit_lesson.php index ef875b4..ef875b4 100644 --- a/public/app/views/admin/edit_lesson.php +++ b/public/app/views/xx--admin/edit_lesson.php diff --git a/public/app/views/admin/edit_page.php b/public/app/views/xx--admin/edit_page.php index 46c47bc..46c47bc 100644 --- a/public/app/views/admin/edit_page.php +++ b/public/app/views/xx--admin/edit_page.php diff --git a/public/app/views/admin/files.php b/public/app/views/xx--admin/files.php index c6d2771..c6d2771 100644 --- a/public/app/views/admin/files.php +++ b/public/app/views/xx--admin/files.php diff --git a/public/app/views/admin/lessons.php b/public/app/views/xx--admin/lessons.php index d1cd3ae..d1cd3ae 100644 --- a/public/app/views/admin/lessons.php +++ b/public/app/views/xx--admin/lessons.php diff --git a/public/app/views/admin/pages.php b/public/app/views/xx--admin/pages.php index dd8f954..dd8f954 100644 --- a/public/app/views/admin/pages.php +++ b/public/app/views/xx--admin/pages.php diff --git a/public/app/views/admin/panel.php b/public/app/views/xx--admin/panel.php index 59b8cba..59b8cba 100644 --- a/public/app/views/admin/panel.php +++ b/public/app/views/xx--admin/panel.php diff --git a/public/app/views/admin/privileges.php b/public/app/views/xx--admin/privileges.php index 587c363..587c363 100644 --- a/public/app/views/admin/privileges.php +++ b/public/app/views/xx--admin/privileges.php diff --git a/public/app/views/admin/skeleton.php b/public/app/views/xx--admin/skeleton.php index 927bd2d..927bd2d 100644 --- a/public/app/views/admin/skeleton.php +++ b/public/app/views/xx--admin/skeleton.php diff --git a/public/assets/css/class.css b/public/assets/css/class.css index 81be1f2..d1e7715 100644 --- a/public/assets/css/class.css +++ b/public/assets/css/class.css @@ -84,7 +84,7 @@ html, body { .btn-back2panel, .btn-preview { border: 1px solid #aaa7; background: #ddd; } -.btn-back2panel { float: right; } +.btn-back2panel { float: right; margin-top: calc(-1 * var(--bs-btn-padding-y)); } .btn-back2panel:hover, .btn-preview:hover { border: 1px solid #7777; background: #f5f5f5; } @@ -201,3 +201,4 @@ html, body { } .image-credits a:hover { color: var(--img-credit-hover-color); } +.manage .table tbody tr:hover { background: #eeea; }
\ No newline at end of file diff --git a/public/assets/js/panel/admin_petitions.js b/public/assets/js/panel/admin_petitions.js new file mode 100644 index 0000000..7b65f10 --- /dev/null +++ b/public/assets/js/panel/admin_petitions.js @@ -0,0 +1,184 @@ +// Globals +// ----------------------------------------------------------------------------- +var petitions = []; +var table; + + + +// Supplamentary functions +// ----------------------------------------------------------------------------- + +// array.indexOf polyfill +// --- -- -- - - - +if (!Array.prototype.indexOf) +{ + Array.prototype.indexOf = function(elt /*, from*/) + { + var len = this.length >>> 0; + + var from = Number(arguments[1]) || 0; + from = (from < 0) + ? Math.ceil(from) + : Math.floor(from); + if (from < 0) + from += len; + + for (; from < len; from++) + { + if (from in this && + this[from] === elt) + return from; + } + return -1; + }; +} + + +// return petitions record by petition-id +// --- -- -- - - - +function petition_record(id) { + var record = false; + petitions.forEach(el => { if (el.id == id) record = el; }); + + return record; +} + + +// create actions html (edit and delete buttons) +// --- -- -- - - - +function create_actions_html(id, signature) { + + var del, edit, show; + + del = ""; + edit = [ + '<button type="button" class="btn btn-xs btn-primary"', + 'data-bs-toggle="modal" data-bs-target="#managePetition"', + 'data-id="'+ id +'">', + '<i class="fa-solid fa-stamp"></i>', + '</button>' + ].join('\n'); + + show = [ + '<a type="button" target="_blank" class="btn btn-xs btn-warning"', + 'href="/petition/signature/'+ signature +'">', + '<i class="fa-regular fa-circle-down"></i>', + '</a>' + ].join('\n'); + + return show +' '+ edit +' '+ del; +} + + + +$(document).ready(function() { + + // When document is ready + // ------------------------------------------------------------------------- + + + /** init_table + * --- -- -- - - - + * get categories + * constuct categories array + * render dataTable + */ + function init_table() { + + // get all categories from server (ajax) + $.getJSON( "/admin/get/petitions") + .done(function( json ) { + // then ... + console.log(json); + + // reset categories + petitions.length = 0; + + // construct petitions data + json.data.forEach( el => { + petitions.push({ + id: parseInt(el.id), + subject: el.subject, + protocol: el.protocol, + username: el.UserName, + signature: el.signature, + datepost: el.creation_date, + actions: create_actions_html(el.id, el.signature) + }); + }); + + + // destroy previous datatable table instances + $('#dt-petitions').dataTable().fnClearTable(); + $('#dt-petitions').dataTable().fnDestroy(); + + // (re-)create categories datatable + table = $('#dt-petitions').DataTable({ + language: { url: '/assets/js/DataTables/localization/Greek.json' }, + data: petitions, + // ordering: false, + columns: [ + { data: 'subject', width: '42%' }, + { data: 'protocol' }, + { data: 'username' }, + // { data: 'signature' }, + { data: 'datepost' }, + { data: 'actions', width: '96px' } + ] + }); + + }) + .fail(function( jqxhr, textStatus, error ) { + console.log( "Request Failed (" + error +")" ); + }); + + } + init_table(); // init table on first run + + + + /** When modal show-up + * ------------------------------------------------------------------------- + * ... prepare the manage-category form + * ... update select2 with possible parents + */ + $('#managePetition').on('show.bs.modal', event => { + var button = $(event.relatedTarget); // Button that triggered the modal + var id = parseInt( button.data('id') ); // category_id from data-id + // var modal = $(this); // modal object + var petition = petition_record(id) + + $('#managePetition form input[name=protocol]').val( + petition.protocol ? petition.protocol : '' + ); + $('#managePetition form input[name=id]').val( id ); // category id (hidden) + }) + + + + // When form is submited + // ------------------------------------------------------------------------- + + $('#managePetition form').submit( event => { + event.preventDefault(); + + // prepare data to POST + var data = { + id: parseInt($('#managePetition form input[name=id]').val()), + protocol: $('#managePetition form input[name=protocol]').val() }; + + // select action url (add or update) + var request = '/admin/set/protocol'; + + // send POST request + $.post(request, data) + .done(function( data ) { + + $('#managePetition').modal('hide'); // when done, close modal + + init_table(); // reload table of categories + }); + + }); + +}); |
