From 76afdef30b10ef39cdad853a4ae251edf907ab45 Mon Sep 17 00:00:00 2001 From: Alex Tselegidis Date: Fri, 29 Oct 2021 10:44:01 +0200 Subject: [PATCH] Moved the instance handling logic to a re-usable library --- application/config/migration.php | 2 +- application/controllers/Backend.php | 13 +- application/controllers/Console.php | 100 ++------------- application/controllers/Installation.php | 7 +- application/core/EA_Controller.php | 1 + application/core/EA_Migration.php | 1 + application/core/EA_Model.php | 1 + application/libraries/Instance.php | 154 +++++++++++++++++++++++ 8 files changed, 174 insertions(+), 105 deletions(-) create mode 100644 application/libraries/Instance.php diff --git a/application/config/migration.php b/application/config/migration.php index f98b1f39..c722db45 100755 --- a/application/config/migration.php +++ b/application/config/migration.php @@ -21,7 +21,7 @@ $config['migration_enabled'] = TRUE; | be upgraded / downgraded to. | */ -$config['migration_version'] = 22; +$config['migration_version'] = 0; /* diff --git a/application/controllers/Backend.php b/application/controllers/Backend.php index a597a08a..5c54c99e 100755 --- a/application/controllers/Backend.php +++ b/application/controllers/Backend.php @@ -38,7 +38,7 @@ class Backend extends EA_Controller { $this->load->model('users_model'); $this->load->library('accounts'); - $this->load->library('migration'); + $this->load->library('instance'); $this->load->library('timezones'); } @@ -291,7 +291,7 @@ class Backend extends EA_Controller { * Display the user/system settings. * * This page will display the user settings (name, password etc). If current user is an administrator, then he will - * be able to make change to the current Easy!Appointment installation (core settings like company name, book + * be able to make change to the current Easy!Appointment instance (core settings like company name, book * timeout). */ public function settings() @@ -334,7 +334,7 @@ class Backend extends EA_Controller { } /** - * This method will update the installation to the latest available version in the server. + * This method will update the instance to the latest available version in the server. * * IMPORTANT: The code files must exist in the server, this method will not fetch any new files but will update * the database schema. @@ -348,13 +348,10 @@ class Backend extends EA_Controller { { if ( ! $this->has_permissions(PRIV_SYSTEM_SETTINGS)) { - throw new Exception('You do not have the required privileges for this task!'); + throw new RuntimeException('You do not have the required privileges for this task.'); } - if ( ! $this->migration->current()) - { - throw new Exception($this->migration->error_string()); - } + $this->instance->migrate(); $view = ['success' => TRUE]; } diff --git a/application/controllers/Console.php b/application/controllers/Console.php index cba17f09..39352036 100644 --- a/application/controllers/Console.php +++ b/application/controllers/Console.php @@ -33,7 +33,7 @@ class Console extends EA_Controller { $this->load->dbutil(); - $this->load->library('migration'); + $this->load->library('instance'); $this->load->model('admins_model'); $this->load->model('customers_model'); @@ -53,9 +53,9 @@ class Console extends EA_Controller { */ public function install() { - $this->migrate('fresh'); + $this->instance->migrate('fresh'); - $this->seed(); + $this->instance->seed(); response(PHP_EOL . '⇾ Installation completed, login with "administrator" / "administrator".' . PHP_EOL . PHP_EOL); } @@ -63,12 +63,12 @@ class Console extends EA_Controller { /** * Migrate the database to the latest state. * - * Use this method to upgrade an existing installation to the latest database state. + * Use this method to upgrade an Easy!Appointments instance to the latest database state. * * Notice: * * Do not use this method to install the app as it will not seed the database with the initial entries (admin, - * provider, service, settings etc). Use the UI installation page for this. + * provider, service, settings etc). * * Usage: * @@ -80,15 +80,7 @@ class Console extends EA_Controller { */ public function migrate(string $type = '') { - if ($type === 'fresh' && ! $this->migration->version(0)) - { - show_error($this->migration->error_string()); - } - - if ($this->migration->current() === FALSE) - { - show_error($this->migration->error_string()); - } + $this->instance->migrate($type); } /** @@ -102,69 +94,11 @@ class Console extends EA_Controller { */ public function seed() { - // Settings - setting([ - 'company_name' => 'Company Name', - 'company_email' => 'info@example.org', - 'company_link' => 'https://example.org', - ]); - - // Admin - $this->admins_model->save([ - 'first_name' => 'John', - 'last_name' => 'Doe', - 'email' => 'john@example.org', - 'phone_number' => '+1 (000) 000-0000', - 'settings' => [ - 'username' => 'administrator', - 'password' => 'administrator', - 'notifications' => TRUE, - 'calendar_view' => CALENDAR_VIEW_DEFAULT - ], - ]); - - // Service - $service_id = $this->services_model->save([ - 'name' => 'Service', - 'duration' => '30', - 'price' => '0', - 'currency' => '', - 'availabilities_type' => 'flexible', - 'attendants_number' => '1' - ]); - - // Provider - $this->providers_model->save([ - 'first_name' => 'Jane', - 'last_name' => 'Doe', - 'email' => 'jane@example.org', - 'phone_number' => '+1 (000) 000-0000', - 'services' => [ - $service_id - ], - 'settings' => [ - 'username' => 'janedoe', - 'password' => 'janedoe', - 'working_plan' => setting('company_working_plan'), - 'notifications' => TRUE, - 'google_sync' => FALSE, - 'sync_past_days' => 30, - 'sync_future_days' => 90, - 'calendar_view' => CALENDAR_VIEW_DEFAULT - ], - ]); - - // Customer - $this->customers_model->save([ - 'first_name' => 'James', - 'last_name' => 'Doe', - 'email' => 'james@example.org', - 'phone_number' => '+1 (000) 000-0000', - ]); + $this->instance->seed(); } /** - * Create a backup file. + * Create a database backup file. * * Use this method to back up your Easy!Appointments data. * @@ -178,23 +112,7 @@ class Console extends EA_Controller { */ public function backup() { - $path = $GLOBALS['argv'][3] ?? APPPATH . '/../storage/backups'; - - if ( ! file_exists($path)) - { - throw new Exception('The backup path does not exist: ' . $path); - } - - if ( ! is_writable($path)) - { - throw new Exception('The backup path is not writable: ' . $path); - } - - $contents = $this->dbutil->backup(); - - $filename = 'easyappointments-backup-' . date('Y-m-d-His') . '.gz'; - - write_file(rtrim($path, '/') . '/' . $filename, $contents); + $this->instance->backup($GLOBALS['argv'][3]); } /** diff --git a/application/controllers/Installation.php b/application/controllers/Installation.php index b9e7517a..3b14dd1e 100644 --- a/application/controllers/Installation.php +++ b/application/controllers/Installation.php @@ -32,7 +32,7 @@ class Installation extends EA_Controller { $this->load->model('providers_model'); $this->load->model('customers_model'); - $this->load->library('migration'); + $this->load->library('instance'); } /** @@ -66,10 +66,7 @@ class Installation extends EA_Controller { $admin = request('admin'); $company = request('company'); - if ( ! $this->migration->current()) - { - throw new Exception($this->migration->error_string()); - } + $this->instance->migrate(); // Insert admin $admin['timezone'] = 'UTC'; diff --git a/application/core/EA_Controller.php b/application/core/EA_Controller.php index 2dbb5113..7f3ad562 100644 --- a/application/core/EA_Controller.php +++ b/application/core/EA_Controller.php @@ -55,6 +55,7 @@ * @property Availability $availability * @property Google_Sync $google_sync * @property Ics_file $ics_file + * @property Instance $instance * @property Notifications $notifications * @property Synchronization $synchronization * @property Timezones $timezones diff --git a/application/core/EA_Migration.php b/application/core/EA_Migration.php index 00c5a961..535f4541 100644 --- a/application/core/EA_Migration.php +++ b/application/core/EA_Migration.php @@ -55,6 +55,7 @@ * @property Availability $availability * @property Google_Sync $google_sync * @property Ics_file $ics_file + * @property Instance $instance * @property Notifications $notifications * @property Synchronization $synchronization * @property Timezones $timezones diff --git a/application/core/EA_Model.php b/application/core/EA_Model.php index bc10ec0e..9bc1745b 100644 --- a/application/core/EA_Model.php +++ b/application/core/EA_Model.php @@ -55,6 +55,7 @@ * @property Availability $availability * @property Google_Sync $google_sync * @property Ics_file $ics_file + * @property Instance $instance * @property Notifications $notifications * @property Synchronization $synchronization * @property Timezones $timezones diff --git a/application/libraries/Instance.php b/application/libraries/Instance.php new file mode 100644 index 00000000..b59f5487 --- /dev/null +++ b/application/libraries/Instance.php @@ -0,0 +1,154 @@ + + * @copyright Copyright (c) 2013 - 2020, Alex Tselegidis + * @license http://opensource.org/licenses/GPL-3.0 - GPLv3 + * @link http://easyappointments.org + * @since v1.4.0 + * ---------------------------------------------------------------------------- */ + + +/** + * Instance library. + * + * Handles all Easy!Appointments instance related functionality. + * + * @package Libraries + */ +class Instance { + /** + * @var EA_Controller + */ + protected $CI; + + /** + * Installation constructor. + */ + public function __construct() + { + $this->CI =& get_instance(); + + $this->CI->load->model('admins_model'); + $this->CI->load->model('services_model'); + $this->CI->load->model('providers_model'); + $this->CI->load->model('customers_model'); + + $this->CI->load->library('timezones'); + } + + /** + * Migrate the database to the latest state. + * + * @param string $type Provide "fresh" to revert previous migrations and start from the beginning. + */ + public function migrate(string $type = '') + { + if ($type === 'fresh' && ! $this->CI->migration->version(0)) + { + show_error($this->CI->migration->error_string()); + } + + if ($this->CI->migration->latest() === FALSE) + { + show_error($this->CI->migration->error_string()); + } + } + + /** + * Seed the database with test data. + */ + public function seed() + { + // Settings + setting([ + 'company_name' => 'Company Name', + 'company_email' => 'info@example.org', + 'company_link' => 'https://example.org', + ]); + + // Admin + $this->CI->admins_model->save([ + 'first_name' => 'John', + 'last_name' => 'Doe', + 'email' => 'john@example.org', + 'phone_number' => '+10000000000', + 'settings' => [ + 'username' => 'administrator', + 'password' => 'administrator', + 'notifications' => TRUE, + 'calendar_view' => CALENDAR_VIEW_DEFAULT + ], + ]); + + // Service + $service_id = $this->CI->services_model->save([ + 'name' => 'Service', + 'duration' => '30', + 'price' => '0', + 'currency' => '', + 'availabilities_type' => 'flexible', + 'attendants_number' => '1' + ]); + + // Provider + $this->CI->providers_model->save([ + 'first_name' => 'Jane', + 'last_name' => 'Doe', + 'email' => 'jane@example.org', + 'phone_number' => '+10000000000', + 'services' => [ + $service_id + ], + 'settings' => [ + 'username' => 'janedoe', + 'password' => 'janedoe', + 'working_plan' => setting('company_working_plan'), + 'notifications' => TRUE, + 'google_sync' => FALSE, + 'sync_past_days' => 30, + 'sync_future_days' => 90, + 'calendar_view' => CALENDAR_VIEW_DEFAULT + ], + ]); + + // Customer + $this->CI->customers_model->save([ + 'first_name' => 'James', + 'last_name' => 'Doe', + 'email' => 'james@example.org', + 'phone_number' => '+10000000000', + ]); + } + + /** + * Create a database backup file. + * + * @param string|null $path Override the default backup path (storage/backups/*). + * + * @throws Exception + */ + public function backup(string $path = NULL) + { + $path = $path ?? APPPATH . '/../storage/backups'; + + if ( ! file_exists($path)) + { + throw new Exception('The backup path does not exist: ' . $path); + } + + if ( ! is_writable($path)) + { + throw new Exception('The backup path is not writable: ' . $path); + } + + $contents = $this->CI->dbutil->backup(); + + $filename = 'easyappointments-backup-' . date('Y-m-d-His') . '.gz'; + + write_file(rtrim($path, '/') . '/' . $filename, $contents); + } +}