public function admin_login()
 {
     $admin_login_form = $this->input->post();
     $notifications = NULL;
     $session_data = $this->session->all_userdata();
     if (!isset($session_data['account_types'])) {
         $account_types = $this->account_types->get_account_types();
         $this->session->set_userdata('account_types', $account_types);
     } else {
         $account_types = $session_data['account_types'];
         $data['account_types'] = $session_data['account_types'];
     }
     if (isset($session_data[$account_types[0] . '_id']) || isset($session_data[$account_types[2] . '_id']) || isset($session_data[$account_types[3] . '_id']) || isset($session_data[$account_types[4] . '_id'])) {
         $admin_id = $this->_search_admin_id_in_session($session_data, $account_types);
         $admin_account = $this->account_model->get_admin_account_by_id($admin_id);
         $this->_choose_admin_account($admin_account, $account_types, $notifications);
     } else {
         if ($admin_login_form) {
             $validation_response = $this->_validate_admin_log_in_form();
             if ($validation_response) {
                 $admin_account = $this->account_model->get_admin_account_by_identification_number($admin_login_form['adminUsername']);
                 if (isset($admin_account)) {
                     $password_decrypted_from_db = _password_account_h2o($admin_account->password, $admin_account->email);
                     $admin_user_password = md5($admin_login_form['adminUserPassword']);
                     if ($password_decrypted_from_db == $admin_user_password) {
                         //call switch
                         $this->_choose_admin_account($admin_account, $account_types, $notifications);
                     }
                 }
             }
         } else {
             redirect('/admin');
         }
     }
 }
 public function log_in()
 {
     $log_in_form = $this->input->post();
     $notifications = array();
     $data['messages'] = null;
     $data['pathologies'] = null;
     $data['title'] = "Mi cuenta";
     $categories = $this->get_categories();
     $active_ingredients = $this->get_active_ingredients();
     $data['active_ingredients'] = $active_ingredients;
     $data['categories'] = $categories;
     $data['user_logged'] = false;
     // breadcrumb start
     $breadcrumb = new stdClass();
     $breadcrumb->title = "Mi cuenta";
     $breadcrumb_item = new stdClass();
     $breadcrumb_item->name = "Mi cuenta";
     $breadcrumb_item->url = "/account";
     $breadcrumb_item->active = true;
     $breadcrumb_list['register'] = $breadcrumb_item;
     $breadcrumb->items = $breadcrumb_list;
     $data['breadcrumb'] = $breadcrumb;
     //breadcrumb over
     $session_data = $this->session->all_userdata();
     if (!isset($session_data['account_types'])) {
         $account_types = $this->account_types->get_account_types();
         $this->session->set_userdata('account_types', $account_types);
     } else {
         $account_types = $session_data['account_types'];
         $data['account_types'] = $session_data['account_types'];
     }
     if (isset($session_data[$account_types[1] . '_id'])) {
         $data['user_logged'] = true;
         $notifications = $this->session->flashdata('notifications');
         $data['notifications'] = $notifications;
         $account = $this->account_model->get_account_by_id($session_data[$account_types[1] . '_id']);
         if (isset($account)) {
             $pathologies = new stdClass();
             $messages = $this->messages->get_every_messages($account->email);
             $account_pathologies = $this->accounts->get_pathologies($session_data[$account_types[1] . '_id']);
             $pathologies_DB = $this->pathologies->get_all_pathologies();
             $account_pathologies_dropdown_items_ids = $this->accounts->generate_pathologies_dropdown_items_ids($categories);
             $pathologies->dropdown_items_ids = $account_pathologies_dropdown_items_ids;
             if (isset($messages)) {
                 $messages_sorted = $this->messages->sort_messages($messages, $account->email);
                 $data['messages'] = $messages_sorted;
             }
             if (isset($account_pathologies)) {
                 $pathologies->account_pathologies = $account_pathologies;
             } else {
                 $pathologies->account_pathologies = null;
             }
             $data['pathologies'] = $pathologies;
             $address = $this->addresses->get_sign_up_address($account->id);
             if (isset($_COOKIE['shoppingcart'])) {
                 $data['shoppingcart'] = json_decode($_COOKIE['shoppingcart']);
             }
             $orders = $this->orders->orders_for_USER_account($account->id);
             if (isset($orders)) {
                 foreach ($orders as $key => $order) {
                     $orders[$key]->products = json_decode($order->products);
                 }
                 $data['orders'] = $orders;
             }
             if (isset($account->points)) {
                 $data['points'] = $account->points;
             }
             $data['address'] = $address;
             $data['user_logged_account'] = $account;
             $this->load->view('pages/account-panel', $data);
         } else {
             $notifications['warning'] = "Por favor inicie sesión!";
             $this->session->set_flashdata('notificatons', $notifications);
             redirect('/account');
         }
     } else {
         if ($log_in_form) {
             $validation_response = $this->_validate_log_in_form($log_in_form);
             if ($validation_response) {
                 $userEmail = $log_in_form['userEmail'];
                 $account = $this->account_model->get_account_by_email($userEmail);
                 if (isset($account)) {
                     $pathologies = new stdClass();
                     $messages = $this->messages->get_every_messages($account->email);
                     if ($messages) {
                         $messages_sorted = $this->messages->sort_messages($messages, $account->email);
                         $data['messages'] = $messages_sorted;
                     }
                     $account_pathologies = $this->accounts->get_pathologies($account->id);
                     $account_pathologies_dropdown_items_ids = $this->accounts->generate_pathologies_dropdown_items_ids($categories);
                     $pathologies->dropdown_items_ids = $account_pathologies_dropdown_items_ids;
                     if (isset($account_pathologies)) {
                         $pathologies->account_pathologies = $account_pathologies;
                     } else {
                         $pathologies->account_pathologies = null;
                     }
                     $data['pathologies'] = $pathologies;
                     $address = $this->addresses->get_sign_up_address($account->id);
                     $data['address'] = $address;
                     $account_password_decrypted = _password_account_h2o($account->password, $userEmail);
                     $user_password = md5($log_in_form['userPassword']);
                     if ($account_password_decrypted === $user_password) {
                         $orders = $this->orders->orders_for_USER_account($account->id);
                         if (isset($orders)) {
                             foreach ($orders as $key => $order) {
                                 $orders[$key]->products = json_decode($order->products);
                             }
                             $data['orders'] = $orders;
                         }
                         if (isset($account->points)) {
                             $data['points'] = $account->points;
                         }
                         $this->_do_login($account, $data, $account_types);
                         if (isset($_COOKIE['shoppingcart'])) {
                             $shoppingcart = json_decode($_COOKIE['shoppingcart']);
                             $data['shoppingcart'] = $shoppingcart;
                             $notifications['success'][] = "Los productos en tu carrito de compras están seguros :)!";
                             $this->session->set_flashdata("notifications", $notifications);
                             if ($shoppingcart->subtotal < $shoppingcart->minimumOrderValue) {
                                 $notifications['info'] = "Continua con tus compras, estos son nuestros productos!";
                                 $this->session->set_flashdata('notifications', $notifications);
                                 redirect("/product/show_product_by_category/nuestros_productos");
                             } else {
                                 $notifications['info'] = "Puedes finalizar tu compra!";
                                 $this->session->set_flashdata('notifications', $notifications);
                                 redirect("/checkout");
                             }
                         }
                         $this->load->view('pages/account-panel', $data);
                     } else {
                         $notifications['danger'] = "El email o password no coinciden!";
                         $this->session->set_flashdata('notifications', $notifications);
                         redirect("/account");
                     }
                 } else {
                     $notifications['warning'] = "No existe un usuario registrado con este email";
                     $this->session->set_flashdata('notifications', $notifications);
                     redirect("/account");
                 }
             }
         } else {
             redirect("/");
         }
     }
 }