public function editUserAction()
 {
     /**
      * Check permission
      */
     if (false == $this->checkPermission('edit_user')) {
         $this->_forwardToNoPermissionPage();
         return;
     }
     $id = $this->_getParam('id', false);
     $data = $this->_getParam('data', false);
     if (false == $id) {
         $this->_redirect('user/admin/user-manager');
     }
     $objUser = new Models_User();
     $objUserExp = new Models_UserExpand();
     $errors = array();
     /**
      * Get old user
      */
     $oldUser = $objUser->getUserWithExpand($id);
     if (empty($oldUser)) {
         /**
          * User doesn't exsit
          */
         $this->session->userMessage = array('success' => false, 'message' => Vi_Language::translate('User does NOT exist'));
         $this->_redirect('user/admin/user-manager#listofuser');
     }
     if (false !== $data) {
         /**
          * Update new user
          */
         $newUser = array('group_id' => $data['group_id'], 'username' => $data['username'], 'email' => $data['email'], 'full_name' => $data['full_name'], 'password' => $data['password'], 'repeat_password' => $data['repeat_password'], 'enabled' => $data['enabled']);
         $newUserExp = array('admin_note' => $data['admin_note']);
         /**
          * Check permission
          */
         if ($oldUser['group_id'] != $newUser['group_id'] && false == $this->checkPermission('change_group')) {
             $newUser['group_id'] = $oldUser['group_id'];
         }
         $exclude = array();
         if (null == @$newUser['password'] && null == @$newUser['repeat_password']) {
             $exclude[] = 'password';
             unset($newUser['password']);
             unset($newUser['repeat_password']);
         }
         if ($newUser['email'] == $oldUser['email']) {
             $exclude[] = 'email';
             unset($newUser['email']);
         }
         if ($newUser['username'] == $oldUser['username']) {
             $exclude[] = 'username';
             unset($newUser['username']);
         }
         $errors = $objUser->validate($newUser, $exclude);
         if (true === $errors) {
             if (null != @$newUser['password']) {
                 $newUser['password'] = md5($newUser['password']);
                 unset($newUser['repeat_password']);
             }
             /**
              * TODO Read date format from language table
              */
             try {
                 $objUser->update($newUser, array('user_id=?' => $id));
                 $objUserExp->update($newUserExp, array('user_expand_id=?' => $oldUser['user_expand_id']));
                 /**
                  * Set owner email for restaurant
                  */
                 if (isset($newUser['email'])) {
                     $objRes = new Models_Restaurant();
                     $objRes->update(array('owner_email' => $newUser['email']), array('user_id=?' => $id));
                 }
                 if (isset($newUser['full_name'])) {
                     $objRes = new Models_Restaurant();
                     $objRes->update(array('owner' => $newUser['full_name']), array('user_id=?' => $id));
                 }
                 /**
                  * Reload current login user
                  */
                 $loggedUser = $this->session->backendUser;
                 if ($oldUser['user_id'] == @$loggedUser['user_id']) {
                     $this->session->backendUser = $objUser->getByUserId($oldUser['user_id'])->toArray();
                 }
                 $this->_redirect('user/admin/user-manager');
             } catch (Exception $e) {
                 $errors = array('main' => Vi_Language::translate('Can not update user now'));
             }
         }
     } else {
         /**
          * Get current user
          */
         $data = $oldUser;
     }
     /**
      * Prepare for template
      */
     $this->view->errors = $errors;
     $this->view->data = $data;
     $this->view->headTitle(Vi_Language::translate('Edit user'));
     $this->view->menu = array('usergroup', 'edituser');
     /**
      * Get all groups
      */
     $objGroup = new Models_Group();
     $this->view->allGroups = $objGroup->getAll(array('sorting ASC', 'group_id ASC'))->toArray();
     //        /**
     //        * Get all countries
     //        */
     //        $objCountry = new Models_Country();
     //        $this->view->allCountries = $objCountry->getAll(array('sorting ASC', 'country_id ASC'))->toArray();
 }
 public function restaurantRegisterAction()
 {
     /**
      * Get all provice CODE
      */
     $objCountry = new Models_Country();
     $this->view->allProvinces = $objCountry->getAllProvinces();
     /**
      * Get some list
      */
     $objCat = new Models_Category();
     $this->view->leadTimeNormal = $objCat->getAllValues('lead_time_normal');
     $this->view->leadTimeCatering = $objCat->getAllValues('lead_time_catering');
     //        echo '<pre>';print_r($this->view->allProvinces);die;
     /**
      * Get data
      */
     $objRes = new Models_Restaurant();
     $objUser = new Models_User();
     $objUserExp = new Models_UserExpand();
     /**
      * Clear all draft user
      */
     $objUser->delete(array('is_draft=?' => 1, 'created_date<?' => time() - 24 * 3600));
     $data = $this->_getParam('data', false);
     $user = $this->_getParam('user', false);
     $condition = $this->_getParam('condition', false);
     $error = '';
     /**
      * Insert restaurant
      */
     if ('1' == $condition && null != $this->session->newRes && is_array($this->session->newRes) && null != $this->session->newUserId) {
         /**
          * Insert restaurant
          */
         $res = $this->session->newRes;
         $res['user_id'] = $this->session->newUserId;
         $objRes->insert($res);
         /**
          * Make real user
          */
         $objUser->update(array('is_draft' => 0, 'enabled' => 1), array('user_id=?' => $this->session->newUserId));
         /**
          * Send email
          */
         $objUser = new Models_User();
         $objMail = new Models_Mail();
         $data = $this->session->newRes;
         unset($data['created_date']);
         $admin = $objUser->getByUserName('admin');
         $objMail->sendHtmlMail('restaurant_register', $data, array($admin['email'], $data['owner_email']));
         /**
          * Clear data
          */
         $this->session->newRes = null;
         $this->session->newUserId = null;
         /**
          * Redirect to thankyou page
          */
         $objContent = new Models_ScontentLang();
         $this->_redirect($objContent->getUrlWithoutAppBaseUrl(20));
     }
     /**
      * Check new account
      */
     $errors = array();
     if (false != $data && false != $user) {
         $newUser = array('group_id' => 2, 'username' => $user['username'], 'email' => $data['owner_email'], 'full_name' => $data['owner'], 'password' => $user['password'], 'repeat_password' => $user['retype_password'], 'company' => $data['name'], 'created_date' => time(), 'enabled' => 0, 'send_discount_code' => 0, 'is_draft' => 1, 'is_restaurant' => 1);
         $errors = $objUser->validate($newUser);
         if (true === $errors) {
             $newUser['password'] = md5($newUser['password']);
             /**
              * TODO Read date format from language table
              */
             unset($newUser['repeat_password']);
             /**
              * Insert draft user
              */
             $id = $objUser->insert($newUser);
             $newUserExp = array('user_id' => $id);
             $objUserExp->insert($newUserExp);
             $this->session->newUserId = $id;
         } else {
             /**
              * Error, display template
              */
             $this->view->user = $user;
             $this->view->data = $data;
             $this->view->errors = $errors;
             $this->view->headTitle('Restaurant Owner Register');
             $this->setLayout('front2');
             return;
         }
     }
     if (false != $data) {
         /**
          * Insert new restaurant
          */
         $newRes = $data;
         /**
          * Modify date
          */
         $dateArr = array('mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun');
         foreach ($dateArr as $item) {
             if ('1' != @$newRes["date_{$item}"]) {
                 unset($newRes["date_{$item}_start"]);
                 unset($newRes["date_{$item}_end"]);
             } else {
                 /**
                  * Change correct time
                  */
                 if (null == @$newRes["date_{$item}_start"]) {
                     $newRes["date_{$item}_start"] = '00:00';
                     //0 hours
                 } else {
                     $tmp = explode(':', $newRes["date_{$item}_start"]);
                     /**
                      * Hour
                      */
                     if (null == @$tmp[0]) {
                         $tmp[0] = 0;
                     }
                     $tmp[0] = '00' . abs(intval($tmp[0]));
                     $tmp[0] = substr($tmp[0], -2, 2);
                     if (23 < intval($tmp[0])) {
                         $tmp[0] = '00';
                     }
                     /**
                      * Minute
                      */
                     if (null == @$tmp[1]) {
                         $tmp[1] = 0;
                     }
                     $tmp[1] = '00' . abs(intval($tmp[1]));
                     $tmp[1] = substr($tmp[1], -2, 2);
                     if (59 < intval($tmp[1])) {
                         $tmp[1] = '00';
                     }
                     $newRes["date_{$item}_start"] = $tmp[0] . ':' . $tmp[1];
                 }
                 if (null == @$newRes["date_{$item}_end"]) {
                     $newRes["date_{$item}_end"] = 24 * 3600 - 60;
                     //23:59:59 hours
                 } else {
                     $tmp = explode(':', $newRes["date_{$item}_end"]);
                     /**
                      * Hour
                      */
                     if (null == @$tmp[0]) {
                         $tmp[0] = 23;
                     }
                     $tmp[0] = '00' . abs(intval($tmp[0]));
                     $tmp[0] = substr($tmp[0], -2, 2);
                     if (23 < intval($tmp[0])) {
                         $tmp[0] = '23';
                     }
                     /**
                      * Minute
                      */
                     if (null == @$tmp[1]) {
                         $tmp[1] = 59;
                     }
                     $tmp[1] = '00' . abs(intval($tmp[1]));
                     $tmp[1] = substr($tmp[1], -2, 2);
                     if (59 < intval($tmp[1])) {
                         $tmp[1] = '59';
                     }
                     $newRes["date_{$item}_end"] = $tmp[0] . ':' . $tmp[1];
                 }
             }
         }
         /**
          * Modify data
          */
         if ('1' != @$newRes['check']['pickup']) {
             unset($newRes['pickup']);
         }
         if ('1' != @$newRes['check']['curbside']) {
             unset($newRes['curbside']);
         }
         if ('1' != @$newRes['check']['delivery']) {
             unset($newRes['delivery']);
             unset($newRes['delivery_limit_hour']);
             unset($newRes['delivery_charge']);
             unset($newRes['delivery_minimum']);
         }
         if ('1' != @$newRes['check']['catering_pickup']) {
             unset($newRes['catering_pickup']);
         }
         if ('1' != @$newRes['check']['catering_delivery']) {
             unset($newRes['catering_delivery']);
             unset($newRes['catering_delivery_limit_hour']);
             unset($newRes['catering_delivery_charge']);
             unset($newRes['catering_delivery_minimum']);
         }
         /**
          * Remove tempory data
          */
         unset($newRes['check']);
         $newRes['enabled'] = 0;
         $newRes['created_date'] = time();
         //            echo '<pre>';print_r($newRes);die;
         $this->session->newRes = $newRes;
         $this->_forward('restaurant-register2', 'index', 'user');
         return;
     }
     $this->view->data = $data;
     $this->view->error = $error;
     $this->view->headTitle('Restaurant Owner Register');
     $this->setLayout('front2');
 }