mirror of
https://github.com/alextselegidis/easyappointments.git
synced 2024-11-28 10:53:40 +03:00
Add API settings page
This commit is contained in:
parent
1aac411c7f
commit
27d58effdf
4 changed files with 303 additions and 0 deletions
105
application/controllers/Api_settings.php
Normal file
105
application/controllers/Api_settings.php
Normal file
|
@ -0,0 +1,105 @@
|
|||
<?php defined('BASEPATH') or exit('No direct script access allowed');
|
||||
|
||||
/* ----------------------------------------------------------------------------
|
||||
* Easy!Appointments - Online Appointment Scheduler
|
||||
*
|
||||
* @package EasyAppointments
|
||||
* @author A.Tselegidis <alextselegidis@gmail.com>
|
||||
* @copyright Copyright (c) Alex Tselegidis
|
||||
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
|
||||
* @link https://easyappointments.org
|
||||
* @since v1.5.0
|
||||
* ---------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* API settings controller.
|
||||
*
|
||||
* Handles API settings related operations.
|
||||
*
|
||||
* @package Controllers
|
||||
*/
|
||||
class Api_settings extends EA_Controller {
|
||||
/**
|
||||
* Calendar constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
$this->load->model('settings_model');
|
||||
|
||||
$this->load->library('accounts');
|
||||
}
|
||||
|
||||
/**
|
||||
* Render the settings page.
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
session(['dest_url' => site_url('api_settings')]);
|
||||
|
||||
$user_id = session('user_id');
|
||||
|
||||
if (cannot('view', PRIV_SYSTEM_SETTINGS))
|
||||
{
|
||||
if ($user_id)
|
||||
{
|
||||
abort(403, 'Forbidden');
|
||||
}
|
||||
|
||||
redirect('login');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$role_slug = session('role_slug');
|
||||
|
||||
script_vars([
|
||||
'user_id' => $user_id,
|
||||
'role_slug' => $role_slug,
|
||||
'api_settings' => $this->settings_model->get('name like "api_%"'),
|
||||
]);
|
||||
|
||||
html_vars([
|
||||
'page_title' => lang('api'),
|
||||
'active_menu' => PRIV_SYSTEM_SETTINGS,
|
||||
'user_display_name' => $this->accounts->get_user_display_name($user_id),
|
||||
]);
|
||||
|
||||
$this->load->view('pages/api_settings');
|
||||
}
|
||||
|
||||
/**
|
||||
* Save general settings.
|
||||
*/
|
||||
public function save()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (cannot('edit', PRIV_SYSTEM_SETTINGS))
|
||||
{
|
||||
throw new RuntimeException('You do not have the required permissions for this task.');
|
||||
}
|
||||
|
||||
$settings = request('api_settings', []);
|
||||
|
||||
foreach ($settings as $setting)
|
||||
{
|
||||
$existing_setting = $this->settings_model->query()->where('name', $setting['name'])->get()->row_array();
|
||||
|
||||
if ( ! empty($existing_setting))
|
||||
{
|
||||
$setting['id'] = $existing_setting['id'];
|
||||
}
|
||||
|
||||
$this->settings_model->save($setting);
|
||||
}
|
||||
|
||||
response();
|
||||
}
|
||||
catch (Throwable $e)
|
||||
{
|
||||
json_exception($e);
|
||||
}
|
||||
}
|
||||
}
|
52
application/views/pages/api_settings.php
Normal file
52
application/views/pages/api_settings.php
Normal file
|
@ -0,0 +1,52 @@
|
|||
<?php extend('layouts/backend_layout') ?>
|
||||
|
||||
<?php section('content') ?>
|
||||
|
||||
<div id="api-settings-page" class="container backend-page">
|
||||
<div id="api-settings">
|
||||
<div class="row">
|
||||
<div class="col-lg-8 offset-lg-2">
|
||||
<form>
|
||||
<fieldset>
|
||||
<legend class="d-flex justify-content-between align-items-center border-bottom mb-4 py-2">
|
||||
<?= lang('api') ?>
|
||||
|
||||
<?php if (can('edit', PRIV_SYSTEM_SETTINGS)): ?>
|
||||
<button type="button" id="save-settings" class="btn btn-primary">
|
||||
<i class="fas fa-check-square me-2"></i>
|
||||
<?= lang('save') ?>
|
||||
</button>
|
||||
<?php endif ?>
|
||||
</legend>
|
||||
|
||||
<div class="row">
|
||||
<div class="col-12">
|
||||
<div class="mb-3">
|
||||
<label class="form-label" for="api-token">
|
||||
<?= lang('api_token') ?>
|
||||
</label>
|
||||
<input id="api-token" class="form-control" data-field="api_token">
|
||||
<div class="form-text text-muted">
|
||||
<small>
|
||||
<?= lang('api_token_hint') ?>
|
||||
</small>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</fieldset>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<?php section('content') ?>
|
||||
|
||||
<?php section('scripts') ?>
|
||||
|
||||
<script src="<?= asset_url('assets/js/utils/url.js') ?>"></script>
|
||||
<script src="<?= asset_url('assets/js/http/api_settings_http_client.js') ?>"></script>
|
||||
<script src="<?= asset_url('assets/js/pages/api_settings.js') ?>"></script>
|
||||
|
||||
<?php section('scripts') ?>
|
39
assets/js/http/api_settings_http_client.js
Normal file
39
assets/js/http/api_settings_http_client.js
Normal file
|
@ -0,0 +1,39 @@
|
|||
/* ----------------------------------------------------------------------------
|
||||
* Easy!Appointments - Online Appointment Scheduler
|
||||
*
|
||||
* @package EasyAppointments
|
||||
* @author A.Tselegidis <alextselegidis@gmail.com>
|
||||
* @copyright Copyright (c) Alex Tselegidis
|
||||
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
|
||||
* @link https://easyappointments.org
|
||||
* @since v1.5.0
|
||||
* ---------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* API Settings HTTP client.
|
||||
*
|
||||
* This module implements the API settings related HTTP requests.
|
||||
*/
|
||||
App.Http.ApiSettings = (function () {
|
||||
/**
|
||||
* Save API settings.
|
||||
*
|
||||
* @param {Object} apiSettings
|
||||
*
|
||||
* @return {Object}
|
||||
*/
|
||||
function save(apiSettings) {
|
||||
const url = App.Utils.Url.siteUrl('api_settings/save');
|
||||
|
||||
const data = {
|
||||
csrf_token: vars('csrf_token'),
|
||||
api_settings: apiSettings
|
||||
};
|
||||
|
||||
return $.post(url, data);
|
||||
}
|
||||
|
||||
return {
|
||||
save
|
||||
};
|
||||
})();
|
107
assets/js/pages/api_settings.js
Normal file
107
assets/js/pages/api_settings.js
Normal file
|
@ -0,0 +1,107 @@
|
|||
/* ----------------------------------------------------------------------------
|
||||
* Easy!Appointments - Online Appointment Scheduler
|
||||
*
|
||||
* @package EasyAppointments
|
||||
* @author A.Tselegidis <alextselegidis@gmail.com>
|
||||
* @copyright Copyright (c) Alex Tselegidis
|
||||
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
|
||||
* @link https://easyappointments.org
|
||||
* @since v1.5.0
|
||||
* ---------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* API settings page.
|
||||
*
|
||||
* This module implements the functionality of the API settings page.
|
||||
*/
|
||||
App.Pages.ApiSettings = (function () {
|
||||
const $saveSettings = $('#save-settings');
|
||||
|
||||
/**
|
||||
* Check if the form has invalid values.
|
||||
*
|
||||
* @return {Boolean}
|
||||
*/
|
||||
function isInvalid() {
|
||||
try {
|
||||
$('#api-settings .is-invalid').removeClass('is-invalid');
|
||||
|
||||
// Validate required fields.
|
||||
|
||||
let missingRequiredFields = false;
|
||||
|
||||
$('#api-settings .required').each((index, requiredField) => {
|
||||
const $requiredField = $(requiredField);
|
||||
|
||||
if (!$requiredField.val()) {
|
||||
$requiredField.addClass('is-invalid');
|
||||
missingRequiredFields = true;
|
||||
}
|
||||
});
|
||||
|
||||
if (missingRequiredFields) {
|
||||
throw new Error(lang('fields_are_required'));
|
||||
}
|
||||
|
||||
return false;
|
||||
} catch (error) {
|
||||
App.Layouts.Backend.displayNotification(error.message);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
function deserialize(apiSettings) {
|
||||
apiSettings.forEach((apiSetting) => {
|
||||
$('[data-field="' + apiSetting.name + '"]').val(apiSetting.value);
|
||||
});
|
||||
}
|
||||
|
||||
function serialize() {
|
||||
const apiSettings = [];
|
||||
|
||||
$('[data-field]').each((index, field) => {
|
||||
const $field = $(field);
|
||||
|
||||
apiSettings.push({
|
||||
name: $field.data('field'),
|
||||
value: $field.val()
|
||||
});
|
||||
});
|
||||
|
||||
return apiSettings;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save the account information.
|
||||
*/
|
||||
function onSaveSettingsClick() {
|
||||
if (isInvalid()) {
|
||||
App.Layouts.Backend.displayNotification(lang('settings_are_invalid'));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
const apiSettings = serialize();
|
||||
|
||||
App.Http.ApiSettings.save(apiSettings).done(() => {
|
||||
App.Layouts.Backend.displayNotification(lang('settings_saved'));
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the module.
|
||||
*/
|
||||
function initialize() {
|
||||
$saveSettings.on('click', onSaveSettingsClick);
|
||||
|
||||
const apiSettings = vars('api_settings');
|
||||
|
||||
deserialize(apiSettings);
|
||||
|
||||
App.Layouts.Backend.placeFooterToBottom();
|
||||
}
|
||||
|
||||
document.addEventListener('DOMContentLoaded', initialize);
|
||||
|
||||
return {};
|
||||
})();
|
Loading…
Reference in a new issue