/**
  * Default callback method of the application.
  *
  * This method creates the appointment book wizard. If an appointment hash
  * is provided then it means that the customer followed the appointment
  * manage link that was send with the book success email.
  *
  * @param string $appointment_hash The db appointment hash of an existing record.
  */
 public function index($appointment_hash = '')
 {
     if (!is_ea_installed()) {
         redirect('installation/index');
         return;
     }
     $this->load->model('appointments_model');
     $this->load->model('providers_model');
     $this->load->model('services_model');
     $this->load->model('customers_model');
     $this->load->model('settings_model');
     try {
         $available_services = $this->services_model->get_available_services();
         $available_providers = $this->providers_model->get_available_providers();
         $company_name = $this->settings_model->get_setting('company_name');
         $date_format = $this->settings_model->get_setting('date_format');
         // Remove the data that are not needed inside the $available_providers array.
         foreach ($available_providers as $index => $provider) {
             $stripped_data = array('id' => $provider['id'], 'first_name' => $provider['first_name'], 'last_name' => $provider['last_name'], 'services' => $provider['services']);
             $available_providers[$index] = $stripped_data;
         }
         // If an appointment hash is provided then it means that the customer
         // is trying to edit a registered appointment record.
         if ($appointment_hash !== '') {
             // Load the appointments data and enable the manage mode of the page.
             $manage_mode = TRUE;
             $results = $this->appointments_model->get_batch(array('hash' => $appointment_hash));
             if (count($results) === 0) {
                 // The requested appointment doesn't exist in the database. Display
                 // a message to the customer.
                 $view = array('message_title' => $this->lang->line('appointment_not_found'), 'message_text' => $this->lang->line('appointment_does_not_exist_in_db'), 'message_icon' => $this->config->item('base_url') . '/assets/img/error.png');
                 $this->load->view('appointments/message', $view);
                 return;
             }
             $appointment = $results[0];
             $provider = $this->providers_model->get_row($appointment['id_users_provider']);
             $customer = $this->customers_model->get_row($appointment['id_users_customer']);
         } else {
             // The customer is going to book a new appointment so there is no
             // need for the manage functionality to be initialized.
             $manage_mode = FALSE;
             $appointment = array();
             $provider = array();
             $customer = array();
         }
         $google_analytics_code = $this->settings_model->get_setting('google_analytics_code');
         // Load the book appointment view.
         $view = array('available_services' => $available_services, 'available_providers' => $available_providers, 'company_name' => $company_name, 'manage_mode' => $manage_mode, 'date_format' => $date_format, 'appointment_data' => $appointment, 'provider_data' => $provider, 'customer_data' => $customer, 'google_analytics_code' => $google_analytics_code);
     } catch (Exception $exc) {
         $view['exceptions'][] = $exc;
     }
     $this->load->view('appointments/book', $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 {
         if (is_ea_installed()) {
             return;
         }
         // Create E!A database structure.
         $file_contents = file_get_contents(dirname(BASEPATH) . '/assets/sql/structure.sql');
         $sql_queries = explode(';', $file_contents);
         array_pop($sql_queries);
         foreach ($sql_queries as $query) {
             $this->db->query($query);
         }
         // Insert default E!A entries into the database.
         $file_contents = file_get_contents(dirname(BASEPATH) . '/assets/sql/data.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']);
         // Create sample records.
         $this->load->model('services_model');
         $this->load->model('providers_model');
         $sample_service = get_sample_service();
         $sample_service['id'] = $this->services_model->add($sample_service);
         $sample_provider = get_sample_provider();
         $sample_provider['services'][] = $sample_service['id'];
         $this->providers_model->add($sample_provider);
         echo json_encode(AJAX_SUCCESS);
     } catch (Exception $exc) {
         echo json_encode(array('exceptions' => array(exceptionToJavaScript($exc))));
     }
 }