2021-11-18 07:23:54 +03:00
|
|
|
<?php defined('BASEPATH') or exit('No direct script access allowed');
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------
|
2022-01-18 15:05:42 +03:00
|
|
|
* Easy!Appointments - Online Appointment Scheduler
|
2021-11-18 07:23:54 +03:00
|
|
|
*
|
|
|
|
* @package EasyAppointments
|
|
|
|
* @author A.Tselegidis <alextselegidis@gmail.com>
|
2021-12-18 19:43:45 +03:00
|
|
|
* @copyright Copyright (c) Alex Tselegidis
|
2021-11-18 07:23:54 +03:00
|
|
|
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
|
|
|
|
* @link https://easyappointments.org
|
|
|
|
* @since v1.0.0
|
|
|
|
* ---------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/**
|
2021-12-15 10:00:48 +03:00
|
|
|
* Categories controller.
|
2021-11-18 07:23:54 +03:00
|
|
|
*
|
2021-12-15 10:00:48 +03:00
|
|
|
* Handles the categories related operations.
|
2021-11-18 07:23:54 +03:00
|
|
|
*
|
|
|
|
* @package Controllers
|
|
|
|
*/
|
2021-12-15 10:00:48 +03:00
|
|
|
class Categories extends EA_Controller {
|
2021-11-18 07:23:54 +03:00
|
|
|
/**
|
2021-12-15 10:00:48 +03:00
|
|
|
* Categories constructor.
|
2021-11-18 07:23:54 +03:00
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
|
2021-12-15 10:00:48 +03:00
|
|
|
$this->load->model('categories_model');
|
2021-11-18 07:23:54 +03:00
|
|
|
$this->load->model('roles_model');
|
|
|
|
|
|
|
|
$this->load->library('accounts');
|
|
|
|
$this->load->library('timezones');
|
2022-06-19 20:05:45 +03:00
|
|
|
$this->load->library('webhooks_client');
|
2021-11-18 07:23:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-12-15 10:00:48 +03:00
|
|
|
* Render the backend categories page.
|
2021-11-18 07:23:54 +03:00
|
|
|
*
|
2021-12-18 19:22:40 +03:00
|
|
|
* On this page admin users will be able to manage categories, which are eventually selected by customers during the
|
2021-11-18 07:23:54 +03:00
|
|
|
* booking process.
|
|
|
|
*/
|
|
|
|
public function index()
|
|
|
|
{
|
2021-12-15 10:00:48 +03:00
|
|
|
session(['dest_url' => site_url('categories')]);
|
2021-11-18 07:23:54 +03:00
|
|
|
|
2022-01-17 20:24:02 +03:00
|
|
|
$user_id = session('user_id');
|
2022-06-19 20:05:45 +03:00
|
|
|
|
2021-12-15 10:00:48 +03:00
|
|
|
if (cannot('view', PRIV_SERVICES))
|
2021-11-18 07:23:54 +03:00
|
|
|
{
|
2022-01-17 20:24:02 +03:00
|
|
|
if ($user_id)
|
|
|
|
{
|
|
|
|
abort(403, 'Forbidden');
|
|
|
|
}
|
2021-11-18 07:23:54 +03:00
|
|
|
|
2022-01-17 20:24:02 +03:00
|
|
|
redirect('login');
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2021-11-18 07:23:54 +03:00
|
|
|
|
|
|
|
$role_slug = session('role_slug');
|
|
|
|
|
2022-01-10 11:26:28 +03:00
|
|
|
script_vars([
|
|
|
|
'user_id' => $user_id,
|
|
|
|
'role_slug' => $role_slug,
|
|
|
|
]);
|
|
|
|
|
2021-12-18 19:22:40 +03:00
|
|
|
html_vars([
|
2021-12-15 10:00:48 +03:00
|
|
|
'page_title' => lang('categories'),
|
2021-11-18 07:23:54 +03:00
|
|
|
'active_menu' => PRIV_SERVICES,
|
|
|
|
'user_display_name' => $this->accounts->get_user_display_name($user_id),
|
|
|
|
'timezones' => $this->timezones->to_array(),
|
|
|
|
'privileges' => $this->roles_model->get_permissions_by_slug($role_slug),
|
|
|
|
]);
|
2021-12-18 19:22:40 +03:00
|
|
|
|
2022-01-19 12:21:05 +03:00
|
|
|
$this->load->view('pages/categories');
|
2021-11-18 07:23:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-12-15 10:00:48 +03:00
|
|
|
* Filter categories by the provided keyword.
|
2021-11-18 07:23:54 +03:00
|
|
|
*/
|
|
|
|
public function search()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2021-12-15 10:00:48 +03:00
|
|
|
if (cannot('view', PRIV_SERVICES))
|
2021-11-18 07:23:54 +03:00
|
|
|
{
|
2021-12-18 19:22:40 +03:00
|
|
|
abort(403, 'Forbidden');
|
2021-11-18 07:23:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$keyword = request('keyword', '');
|
|
|
|
|
2022-10-16 18:07:01 +03:00
|
|
|
$order_by = 'update_datetime DESC';
|
2021-11-18 07:23:54 +03:00
|
|
|
|
|
|
|
$limit = request('limit', 1000);
|
2021-12-18 19:22:40 +03:00
|
|
|
|
|
|
|
$offset = 0;
|
2021-11-18 07:23:54 +03:00
|
|
|
|
2021-12-15 10:00:48 +03:00
|
|
|
$categories = $this->categories_model->search($keyword, $limit, $offset, $order_by);
|
2021-11-18 07:23:54 +03:00
|
|
|
|
2021-12-15 10:00:48 +03:00
|
|
|
json_response($categories);
|
2021-11-18 07:23:54 +03:00
|
|
|
}
|
|
|
|
catch (Throwable $e)
|
|
|
|
{
|
|
|
|
json_exception($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-12-15 10:00:48 +03:00
|
|
|
* Create a category.
|
2021-11-18 07:23:54 +03:00
|
|
|
*/
|
|
|
|
public function create()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2021-12-15 10:00:48 +03:00
|
|
|
if (cannot('add', PRIV_SERVICES))
|
2021-11-18 07:23:54 +03:00
|
|
|
{
|
2021-12-18 19:22:40 +03:00
|
|
|
abort(403, 'Forbidden');
|
2021-11-18 07:23:54 +03:00
|
|
|
}
|
2022-06-19 20:05:45 +03:00
|
|
|
|
2022-03-25 14:29:17 +03:00
|
|
|
$category = request('category');
|
|
|
|
|
|
|
|
$this->categories_model->only($category, [
|
|
|
|
'name',
|
|
|
|
'description'
|
|
|
|
]);
|
2021-11-18 07:23:54 +03:00
|
|
|
|
2021-12-15 10:00:48 +03:00
|
|
|
$category_id = $this->categories_model->save($category);
|
2021-11-18 07:23:54 +03:00
|
|
|
|
2022-06-19 20:05:45 +03:00
|
|
|
$category = $this->categories_model->find($category_id);
|
|
|
|
|
|
|
|
$this->webhooks_client->trigger(WEBHOOK_CATEGORY_SAVE, $category);
|
|
|
|
|
2021-11-18 07:23:54 +03:00
|
|
|
json_response([
|
|
|
|
'success' => TRUE,
|
2021-12-15 10:00:48 +03:00
|
|
|
'id' => $category_id
|
2021-11-18 07:23:54 +03:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
catch (Throwable $e)
|
|
|
|
{
|
|
|
|
json_exception($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-12-15 10:00:48 +03:00
|
|
|
* Update a category.
|
2021-11-18 07:23:54 +03:00
|
|
|
*/
|
|
|
|
public function update()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2021-12-15 10:00:48 +03:00
|
|
|
if (cannot('edit', PRIV_SERVICES))
|
2021-11-18 07:23:54 +03:00
|
|
|
{
|
2021-12-18 19:22:40 +03:00
|
|
|
abort(403, 'Forbidden');
|
2021-11-18 07:23:54 +03:00
|
|
|
}
|
|
|
|
|
2022-03-25 14:29:17 +03:00
|
|
|
$category = request('category');
|
|
|
|
|
|
|
|
$this->categories_model->only($category, [
|
|
|
|
'id',
|
|
|
|
'name',
|
|
|
|
'description'
|
|
|
|
]);
|
|
|
|
|
2021-12-15 10:00:48 +03:00
|
|
|
$category_id = $this->categories_model->save($category);
|
2021-11-18 07:23:54 +03:00
|
|
|
|
2022-06-19 20:05:45 +03:00
|
|
|
$category = $this->categories_model->find($category_id);
|
|
|
|
|
|
|
|
$this->webhooks_client->trigger(WEBHOOK_CATEGORY_SAVE, $category);
|
|
|
|
|
2021-11-18 07:23:54 +03:00
|
|
|
json_response([
|
|
|
|
'success' => TRUE,
|
2021-12-15 10:00:48 +03:00
|
|
|
'id' => $category_id
|
2021-11-18 07:23:54 +03:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
catch (Throwable $e)
|
|
|
|
{
|
|
|
|
json_exception($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-12-15 10:00:48 +03:00
|
|
|
* Remove a category.
|
2021-11-18 07:23:54 +03:00
|
|
|
*/
|
|
|
|
public function destroy()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2021-12-15 10:00:48 +03:00
|
|
|
if (cannot('delete', PRIV_SERVICES))
|
2021-11-18 07:23:54 +03:00
|
|
|
{
|
2021-12-18 19:22:40 +03:00
|
|
|
abort(403, 'Forbidden');
|
2021-11-18 07:23:54 +03:00
|
|
|
}
|
|
|
|
|
2021-12-15 10:00:48 +03:00
|
|
|
$category_id = request('category_id');
|
2021-11-18 07:23:54 +03:00
|
|
|
|
2022-06-19 20:05:45 +03:00
|
|
|
$category = $this->categories_model->find($category_id);
|
|
|
|
|
2021-12-15 10:00:48 +03:00
|
|
|
$this->categories_model->delete($category_id);
|
2021-11-18 07:23:54 +03:00
|
|
|
|
2022-06-19 20:05:45 +03:00
|
|
|
$this->webhooks_client->trigger(WEBHOOK_CATEGORY_DELETE, $category);
|
|
|
|
|
2021-11-18 07:23:54 +03:00
|
|
|
json_response([
|
|
|
|
'success' => TRUE,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
catch (Throwable $e)
|
|
|
|
{
|
|
|
|
json_exception($e);
|
|
|
|
}
|
|
|
|
}
|
2021-12-18 19:22:40 +03:00
|
|
|
|
2021-12-14 09:18:46 +03:00
|
|
|
/**
|
2021-12-15 10:00:48 +03:00
|
|
|
* Find a category.
|
2021-12-14 09:18:46 +03:00
|
|
|
*/
|
|
|
|
public function find()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2021-12-15 10:05:08 +03:00
|
|
|
if (cannot('view', PRIV_SERVICES))
|
2021-12-14 09:18:46 +03:00
|
|
|
{
|
2021-12-18 19:22:40 +03:00
|
|
|
abort(403, 'Forbidden');
|
2021-12-14 09:18:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$category_id = request('category_id');
|
|
|
|
|
2021-12-15 10:00:48 +03:00
|
|
|
$category = $this->categories_model->find($category_id);
|
2021-12-14 09:18:46 +03:00
|
|
|
|
|
|
|
json_response($category);
|
|
|
|
}
|
|
|
|
catch (Throwable $e)
|
|
|
|
{
|
|
|
|
json_exception($e);
|
|
|
|
}
|
|
|
|
}
|
2021-11-18 07:23:54 +03:00
|
|
|
}
|