Moved the instance handling logic to a re-usable library

This commit is contained in:
Alex Tselegidis 2021-10-29 10:44:01 +02:00
parent 18348e02a5
commit 76afdef30b
8 changed files with 174 additions and 105 deletions

View file

@ -21,7 +21,7 @@ $config['migration_enabled'] = TRUE;
| be upgraded / downgraded to. | be upgraded / downgraded to.
| |
*/ */
$config['migration_version'] = 22; $config['migration_version'] = 0;
/* /*

View file

@ -38,7 +38,7 @@ class Backend extends EA_Controller {
$this->load->model('users_model'); $this->load->model('users_model');
$this->load->library('accounts'); $this->load->library('accounts');
$this->load->library('migration'); $this->load->library('instance');
$this->load->library('timezones'); $this->load->library('timezones');
} }
@ -291,7 +291,7 @@ class Backend extends EA_Controller {
* Display the user/system settings. * Display the user/system settings.
* *
* This page will display the user settings (name, password etc). If current user is an administrator, then he will * 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). * timeout).
*/ */
public function settings() 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 * IMPORTANT: The code files must exist in the server, this method will not fetch any new files but will update
* the database schema. * the database schema.
@ -348,13 +348,10 @@ class Backend extends EA_Controller {
{ {
if ( ! $this->has_permissions(PRIV_SYSTEM_SETTINGS)) 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()) $this->instance->migrate();
{
throw new Exception($this->migration->error_string());
}
$view = ['success' => TRUE]; $view = ['success' => TRUE];
} }

View file

@ -33,7 +33,7 @@ class Console extends EA_Controller {
$this->load->dbutil(); $this->load->dbutil();
$this->load->library('migration'); $this->load->library('instance');
$this->load->model('admins_model'); $this->load->model('admins_model');
$this->load->model('customers_model'); $this->load->model('customers_model');
@ -53,9 +53,9 @@ class Console extends EA_Controller {
*/ */
public function install() 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); 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. * 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: * Notice:
* *
* Do not use this method to install the app as it will not seed the database with the initial entries (admin, * 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: * Usage:
* *
@ -80,15 +80,7 @@ class Console extends EA_Controller {
*/ */
public function migrate(string $type = '') public function migrate(string $type = '')
{ {
if ($type === 'fresh' && ! $this->migration->version(0)) $this->instance->migrate($type);
{
show_error($this->migration->error_string());
}
if ($this->migration->current() === FALSE)
{
show_error($this->migration->error_string());
}
} }
/** /**
@ -102,69 +94,11 @@ class Console extends EA_Controller {
*/ */
public function seed() public function seed()
{ {
// Settings $this->instance->seed();
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',
]);
} }
/** /**
* Create a backup file. * Create a database backup file.
* *
* Use this method to back up your Easy!Appointments data. * Use this method to back up your Easy!Appointments data.
* *
@ -178,23 +112,7 @@ class Console extends EA_Controller {
*/ */
public function backup() public function backup()
{ {
$path = $GLOBALS['argv'][3] ?? APPPATH . '/../storage/backups'; $this->instance->backup($GLOBALS['argv'][3]);
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);
} }
/** /**

View file

@ -32,7 +32,7 @@ class Installation extends EA_Controller {
$this->load->model('providers_model'); $this->load->model('providers_model');
$this->load->model('customers_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'); $admin = request('admin');
$company = request('company'); $company = request('company');
if ( ! $this->migration->current()) $this->instance->migrate();
{
throw new Exception($this->migration->error_string());
}
// Insert admin // Insert admin
$admin['timezone'] = 'UTC'; $admin['timezone'] = 'UTC';

View file

@ -55,6 +55,7 @@
* @property Availability $availability * @property Availability $availability
* @property Google_Sync $google_sync * @property Google_Sync $google_sync
* @property Ics_file $ics_file * @property Ics_file $ics_file
* @property Instance $instance
* @property Notifications $notifications * @property Notifications $notifications
* @property Synchronization $synchronization * @property Synchronization $synchronization
* @property Timezones $timezones * @property Timezones $timezones

View file

@ -55,6 +55,7 @@
* @property Availability $availability * @property Availability $availability
* @property Google_Sync $google_sync * @property Google_Sync $google_sync
* @property Ics_file $ics_file * @property Ics_file $ics_file
* @property Instance $instance
* @property Notifications $notifications * @property Notifications $notifications
* @property Synchronization $synchronization * @property Synchronization $synchronization
* @property Timezones $timezones * @property Timezones $timezones

View file

@ -55,6 +55,7 @@
* @property Availability $availability * @property Availability $availability
* @property Google_Sync $google_sync * @property Google_Sync $google_sync
* @property Ics_file $ics_file * @property Ics_file $ics_file
* @property Instance $instance
* @property Notifications $notifications * @property Notifications $notifications
* @property Synchronization $synchronization * @property Synchronization $synchronization
* @property Timezones $timezones * @property Timezones $timezones

View file

@ -0,0 +1,154 @@
<?php defined('BASEPATH') or exit('No direct script access allowed');
/* ----------------------------------------------------------------------------
* Easy!Appointments - Open Source Web Scheduler
*
* @package EasyAppointments
* @author A.Tselegidis <alextselegidis@gmail.com>
* @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);
}
}