Moved installation procedure from the appointments.php controller to the new installation.php file.

This commit is contained in:
Alex Tselegidis 2015-10-18 19:18:02 +02:00
parent 85c2fb4c36
commit a8cedcd9aa
2 changed files with 90 additions and 63 deletions

View file

@ -40,7 +40,10 @@ class Appointments extends CI_Controller {
* record. * record.
*/ */
public function index($appointment_hash = '') { public function index($appointment_hash = '') {
if (!$this->check_installation()) return; if (!$this->check_installation()) {
redirect('installation/index');
return;
}
$this->load->model('appointments_model'); $this->load->model('appointments_model');
$this->load->model('providers_model'); $this->load->model('providers_model');
@ -217,7 +220,6 @@ class Appointments extends CI_Controller {
// Save any exceptions to the session and redirect to another page so that the user // Save any exceptions to the session and redirect to another page so that the user
// will not add a new appointment on page reload. // will not add a new appointment on page reload.
$this->load->helper('url');
$view['exceptions'] = (!empty($view['exceptions'])) ? $view['exceptions'] : array(); $view['exceptions'] = (!empty($view['exceptions'])) ? $view['exceptions'] : array();
$this->session->set_flashdata('book_exceptions', $view['exceptions']); $this->session->set_flashdata('book_exceptions', $view['exceptions']);
redirect('appointments/book_success/'.$appointment['id']); redirect('appointments/book_success/'.$appointment['id']);
@ -610,71 +612,15 @@ class Appointments extends CI_Controller {
* *
* This method resides in this controller because the "index()" function will * This method resides in this controller because the "index()" function will
* be the first to be launched after the files are on the server. NOTE that the * be the first to be launched after the files are on the server. NOTE that the
* "configuration.php" file must be already set because we won't be able to * "config.php" file must be already set because we won't be able to connect to
* connect to the database otherwise. * the database otherwise.
*
* @return bool Returns false if an installation is required.
*/ */
public function check_installation() { public function check_installation() {
try { return $this->db->table_exists('ea_users');
if (!$this->db->table_exists('ea_users')) {
// This is the first time the website is launched an the user needs to set
// the basic settings. Display the installation view page.
$view['base_url'] = $this->config->item('base_url');
$this->load->view('general/installation', $view);
return FALSE; // Do not display the book appointment view file.
} else {
return TRUE; // Application installed, continue ...
}
} catch(Exception $exc) {
echo $exc->getTrace();
}
} }
/**
* Installs Easy!Appointments on server.
*
* @param array $_POST['admin'] Contains the initial admin user data. System needs at least
* one admin user to work.
* @param array $_POST['company'] Contains the basic company data.
*/
public function ajax_install() {
try {
// Create E!A database structure.
$file_contents = file_get_contents(__DIR__ . '/../../assets/sql/structure.sql');
$sql_queries = explode(';', $file_contents);
array_pop($sql_queries);
foreach($sql_queries as $query) {
$this->db->query($query);
}
// Insert admin
$this->load->model('admins_model');
$admin = json_decode($_POST['admin'], true);
$admin['settings']['username'] = $admin['username'];
$admin['settings']['password'] = $admin['password'];
unset($admin['username'], $admin['password']);
$admin['id'] = $this->admins_model->add($admin);
$this->load->library('session');
$this->session->set_userdata('user_id', $admin['id']);
$this->session->set_userdata('user_email', $admin['email']);
$this->session->set_userdata('role_slug', DB_SLUG_ADMIN);
$this->session->set_userdata('username', $admin['settings']['username']);
// Save company settings
$this->load->model('settings_model');
$company = json_decode($_POST['company'], true);
$this->settings_model->set_setting('company_name', $company['company_name']);
$this->settings_model->set_setting('company_email', $company['company_email']);
$this->settings_model->set_setting('company_link', $company['company_link']);
echo json_encode(AJAX_SUCCESS);
} catch (Exception $exc) {
echo json_encode(array(
'exceptions' => array(exceptionToJavaScript($exc))
));
}
}
/** /**
* GET an specific appointment book and redirect to the success screen. * GET an specific appointment book and redirect to the success screen.
* *

View file

@ -0,0 +1,81 @@
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/* ----------------------------------------------------------------------------
* Easy!Appointments - Open Source Web Scheduler
*
* @package EasyAppointments
* @author A.Tselegidis <alextselegidis@gmail.com>
* @copyright Copyright (c) 2013 - 2015, Alex Tselegidis
* @license http://opensource.org/licenses/GPL-3.0 - GPLv3
* @link http://easyappointments.org
* @since v1.1.0
* ---------------------------------------------------------------------------- */
/**
* Installation Controller
*
* This controller will handle the installation procedure of Easy!Appointments
*
* @package Controllers
*/
class Installation extends CI_Controller {
/**
* Display the installation page.
*/
public function index() {
$view['base_url'] = $this->config->item('base_url');
$this->load->view('general/installation', $view);
}
/**
* [AJAX] Installs Easy!Appointments on the server.
*
* @param array $_POST['admin'] Contains the initial admin user data. The App needs at
* least one admin user to work.
* @param array $_POST['company'] Contains the basic company data.
*/
public function ajax_install() {
try {
// Create E!A database structure.
$file_contents = file_get_contents(BASEPATH . 'assets/sql/structure.sql');
$sql_queries = explode(';', $file_contents);
array_pop($sql_queries);
foreach($sql_queries as $query) {
$this->db->query($query);
}
// Insert admin
$this->load->model('admins_model');
$admin = json_decode($_POST['admin'], true);
$admin['settings']['username'] = $admin['username'];
$admin['settings']['password'] = $admin['password'];
unset($admin['username'], $admin['password']);
$admin['id'] = $this->admins_model->add($admin);
$this->load->library('session');
$this->session->set_userdata('user_id', $admin['id']);
$this->session->set_userdata('user_email', $admin['email']);
$this->session->set_userdata('role_slug', DB_SLUG_ADMIN);
$this->session->set_userdata('username', $admin['settings']['username']);
// Save company settings
$this->load->model('settings_model');
$company = json_decode($_POST['company'], true);
$this->settings_model->set_setting('company_name', $company['company_name']);
$this->settings_model->set_setting('company_email', $company['company_email']);
$this->settings_model->set_setting('company_link', $company['company_link']);
echo json_encode(AJAX_SUCCESS);
} catch (Exception $exc) {
echo json_encode(array(
'exceptions' => array(exceptionToJavaScript($exc))
));
}
}
}
/* End of file installation.php */
/* Location: ./application/controllers/installation.php */