function edit()
 {
     $this->load->helper('security');
     $this->load->helper('transform_helper');
     $msg = '';
     $did = $this->session->userdata('dealer_id');
     $dealer = $this->Dealer->get_dealer($did);
     // validate form
     $this->load->library('form_validation');
     //$this->form_validation->set_rules('username', 'username', 'trim|required|alpha_dash|min_length[4]|is_unique[dealer_dealer.dealer_username]');
     $this->form_validation->set_rules('password', 'password', 'trim|no_space|min_length[8]');
     $this->form_validation->set_rules('newPwd', 'newPwd', 'trim|no_space|min_length[8]');
     $this->form_validation->set_rules('newPwdConfirm', 'newPwdConfirm', 'trim|no_space|matches[newPwd]');
     //$this->form_validation->set_rules('level', 'level', 'required');
     $this->form_validation->set_rules('firstname', 'firstname', 'trim|required');
     $this->form_validation->set_rules('lastname', 'lastname', 'trim|required');
     $this->form_validation->set_rules('address', 'address', 'trim|required');
     $this->form_validation->set_rules('email', 'email', 'trim|required');
     $this->form_validation->set_rules('tel', 'tel', 'trim|required');
     $this->form_validation->set_rules('line', 'line', 'trim|required');
     $this->form_validation->set_rules('shopname', 'shopname', 'trim');
     $this->form_validation->set_rules('website', 'website', 'trim');
     $this->form_validation->set_rules('fanpage', 'fanpage', 'trim');
     $this->form_validation->set_rules('facebook', 'facebook', 'trim');
     $this->form_validation->set_rules('detail', 'detail', 'trim');
     if ($this->form_validation->run() == FALSE) {
         $this->session->set_flashdata('msg', 'ข้อมูลไม่ถูกต้อง');
         echo validation_errors();
         echo "<script>window.history.back();</script>";
         return;
     }
     $usr = set_value('username');
     $pwd = set_value('password');
     $fullname = set_value('firstname') . " " . set_value('lastname');
     $addr = set_value('address');
     $email = set_value('email');
     $tel = set_value('tel');
     $line = set_value('line');
     $shopname = set_value('shopname');
     $website = set_value('website');
     $fanpage = set_value('fanpage');
     $facebook = set_value('facebook');
     $detail = set_value('detail');
     $pwd = $pwd != '' ? do_hash($pwd, 'sha256') : '';
     $website = $website == '' ? '' : set_url_pattern($website);
     $fanpage = $fanpage == '' ? '' : set_url_pattern($fanpage);
     $facebook = $facebook == '' ? '' : set_url_pattern($facebook);
     if (strlen($tel) == 10 && is_numeric($tel)) {
         $new_tel = substr($tel, 0, 3) . "-" . substr($tel, 3, 3) . "-" . substr($tel, 6);
         $tel = $new_tel;
     }
     $dealer_arr = array();
     // set change data
     if (set_value('newPwd') != '') {
         $dealer_log = $this->Dealer->get_dealer_by_login($dealer['dealer_username'], $pwd);
         if ($dealer_log != NULL) {
             $new_pwd = do_hash(set_value('newPwd'), 'sha256');
             $dealer_arr['dealer_password'] = $new_pwd;
         } else {
             $this->session->set_flashdata('msg', 'รหัสผ่านเดิมไม่ถูกต้อง');
             echo "<script>window.history.back();</script>";
             return;
         }
     }
     if ($_FILES['photo']['error'] === 0) {
         $this->load->library('upload');
         $config['upload_path'] = DEALER_IMG_PATH;
         $config['allowed_types'] = 'gif|jpg|png|jpeg';
         $config['remove_spaces'] = true;
         $config['max_size'] = '4048';
         $config['max_width'] = '2100';
         $config['max_height'] = '2100';
         $config['file_name'] = do_hash('dealers-' . $did, 'sha1');
         $config['overwrite'] = TRUE;
         $filename = 'photo';
         //input tag name
         if (file_exists($dealer['dealer_picture'])) {
             unlink($dealer['dealer_picture']);
         }
         $this->upload->initialize($config);
         if (!$this->upload->do_upload($filename)) {
             //echo "test".$this->upload->display_errors();
             $this->session->set_flashdata('msgprofile', 'ไฟล์รูปภาพไม่ถูกต้อง');
             echo "<script>window.history.back();</script>";
             return;
         }
         $ud = $this->upload->data();
         $new_img_path = DEALER_IMG_PATH . "/" . $ud['file_name'];
         //* resize image
         $this->load->library('image_lib');
         $config_img['image_library'] = 'gd2';
         $config_img['source_image'] = $new_img_path;
         $config_img['create_thumb'] = FALSE;
         $config_img['maintain_ratio'] = TRUE;
         $config_img['width'] = 400;
         $config_img['height'] = 400;
         $dim = intval($ud["image_width"]) / intval($ud["image_height"]) - $config_img['width'] / $config_img['height'];
         $config_img['master_dim'] = $dim > 0 ? "height" : "width";
         $this->image_lib->clear();
         $this->image_lib->initialize($config_img);
         $this->image_lib->resize();
         //***
         $dealer_arr['dealer_picture'] = $new_img_path;
     }
     $dealer_arr['dealer_fullname'] = $fullname;
     $dealer_arr['dealer_address'] = $addr;
     $dealer_arr['dealer_email'] = $email;
     $dealer_arr['dealer_tel'] = $tel;
     $dealer_arr['dealer_line'] = $line;
     $dealer_arr['dealer_shopname'] = $shopname;
     $dealer_arr['dealer_website'] = $website;
     $dealer_arr['dealer_facebook'] = $facebook;
     $dealer_arr['dealer_fanpage'] = $fanpage;
     $dealer_arr['dealer_detail'] = $detail;
     $o_arr = array('dealer_id' => $did);
     $up_dealer = $this->Dealer->edit($o_arr, $dealer_arr);
     if ($up_dealer == TRUE) {
         $this->session->set_flashdata('rmmsg', 'แก้ไขเรียบร้อย');
         redirect('/shop/account/profile');
         return;
     }
     $this->session->set_flashdata('msgprofile', 'ไม่สามารถแก้ไขได้');
     echo "<script>window.history.back();</script>";
 }
 function go_register()
 {
     $this->load->helper('security');
     $this->load->helper('transform_helper');
     $msg = '';
     // validate form
     $this->load->library('form_validation');
     $this->form_validation->set_rules('username', 'username', 'trim|required|alpha_dash|min_length[4]|is_unique[dealer_dealer.dealer_username]');
     $this->form_validation->set_rules('password', 'password', 'trim|required|no_space|min_length[8]');
     $this->form_validation->set_rules('passwordConfirm', 'passwordConfirm', 'trim|required|no_space|matches[password]');
     //$this->form_validation->set_rules('level', 'level', 'required');
     $this->form_validation->set_rules('firstname', 'firstname', 'trim|required');
     $this->form_validation->set_rules('lastname', 'lastname', 'trim|required');
     $this->form_validation->set_rules('address', 'address', 'trim|required');
     $this->form_validation->set_rules('email', 'email', 'trim|required|is_unique[dealer_dealer.dealer_email]');
     $this->form_validation->set_rules('tel', 'tel', 'trim|required');
     $this->form_validation->set_rules('line', 'line', 'trim|required');
     $this->form_validation->set_rules('shopname', 'shopname', 'trim');
     $this->form_validation->set_rules('website', 'website', 'trim');
     $this->form_validation->set_rules('fanpage', 'fanpage', 'trim');
     $this->form_validation->set_rules('facebook', 'facebook', 'trim');
     $this->form_validation->set_rules('detail', 'detail', 'trim');
     if ($_FILES['photo']['error'] == 4) {
         $this->form_validation->set_rules('photo', 'photo', 'required');
     }
     if ($this->form_validation->run() == FALSE) {
         $this->session->set_flashdata('msg', 'ข้อมูลไม่ถูกต้อง');
         //echo validation_errors();
         echo "<script>window.history.back();</script>";
         return;
     }
     if ($this->input->post('accept') != TRUE) {
         $msg = 'กรุณายอมรับข้อตกลง';
         echo "<script>window.history.back();</script>";
         $this->session->set_flashdata('msg', $msg);
         return;
     }
     // set variables
     foreach ($this->input->post() as $k => $v) {
         echo $k . " : " . $v . "<br/>";
     }
     $usr = set_value('username');
     $pwd = set_value('password');
     $fullname = set_value('firstname') . " " . set_value('lastname');
     $addr = set_value('address');
     $email = set_value('email');
     $tel = set_value('tel');
     $line = set_value('line');
     $shopname = set_value('shopname');
     $website = set_value('website');
     $fanpage = set_value('fanpage');
     $facebook = set_value('facebook');
     $detail = set_value('detail');
     $pwd = do_hash($pwd, 'sha256');
     $website = $website == '' ? '' : set_url_pattern($website);
     $fanpage = $fanpage == '' ? '' : set_url_pattern($fanpage);
     $facebook = $facebook == '' ? '' : set_url_pattern($facebook);
     $lv = $this->Level->get_level(array('level_order' => 0));
     if (strlen($tel) == 10 && is_numeric($tel)) {
         $new_tel = substr($tel, 0, 3) . "-" . substr($tel, 3, 3) . "-" . substr($tel, 6);
         $tel = $new_tel;
     }
     // add dealer to db
     $this->db->trans_start();
     $dealer_arr = array();
     // set array
     $dealer_arr['dealer_username'] = $usr;
     $dealer_arr['dealer_password'] = $pwd;
     $dealer_arr['dealer_fullname'] = $fullname;
     $dealer_arr['dealer_address'] = $addr;
     $dealer_arr['dealer_picture'] = '';
     $dealer_arr['dealer_email'] = $email;
     $dealer_arr['dealer_tel'] = $tel;
     $dealer_arr['dealer_line'] = $line;
     $dealer_arr['dealer_shopname'] = $shopname;
     $dealer_arr['dealer_website'] = $website;
     $dealer_arr['dealer_facebook'] = $facebook;
     $dealer_arr['dealer_fanpage'] = $fanpage;
     $dealer_arr['dealer_level_id'] = $lv['level_id'];
     $dealer_arr['dealer_detail'] = $detail;
     $dealer_arr['dealer_datetime'] = date('Y-m-d H:i:s');
     $dealer_add = $this->Dealer->add($dealer_arr);
     if ($dealer_add != NULL) {
         //echo  "added ";
         $did = $dealer_add['id'];
         $this->load->library('upload');
         $config['upload_path'] = DEALER_IMG_PATH;
         $config['allowed_types'] = 'gif|jpg|png|jpeg';
         $config['remove_spaces'] = true;
         //$config['max_size']	= '';
         $config['max_width'] = '5000';
         $config['max_height'] = '5000';
         $config['file_name'] = do_hash('dealers-' . $did, 'sha1');
         $filename = 'photo';
         //input tag name
         $this->upload->initialize($config);
         if ($this->upload->do_upload($filename)) {
             //echo "uoload";
             $ud = $this->upload->data();
             $img_path = DEALER_IMG_PATH . "/" . $ud['file_name'];
             //* resize image
             $this->load->library('image_lib');
             $config_img['image_library'] = 'gd2';
             $config_img['source_image'] = $img_path;
             $config_img['create_thumb'] = FALSE;
             $config_img['maintain_ratio'] = TRUE;
             $config_img['width'] = 400;
             $config_img['height'] = 400;
             $dim = intval($ud["image_width"]) / intval($ud["image_height"]) - $config_img['width'] / $config_img['height'];
             $config_img['master_dim'] = $dim > 0 ? "height" : "width";
             $this->image_lib->clear();
             $this->image_lib->initialize($config_img);
             $this->image_lib->resize();
             //***
             $q = array('dealer_id' => $did);
             $new_data = array('dealer_picture' => $img_path);
             $dealer_up = $this->Dealer->edit($q, $new_data);
             if ($dealer_up != 0) {
                 $this->db->trans_complete();
                 $msg = 'สมัครเรียบร้อย กรุณารอการติดต่อทางไลน์จากทางร้าน';
                 $this->session->set_flashdata('sucmsg', $msg);
                 redirect('/shop/login');
                 return;
             } else {
                 $msg = 'ไม่สามารถลงทะเบียนได้';
             }
         } else {
             //echo $this->upload->display_errors();
             $msg = 'ไฟล์รูปภาพไม่ถูกต้อง';
         }
     } else {
         $msg = 'ไม่สามารถลงทะเบียนได้';
     }
     $this->db->trans_off();
     $this->session->set_flashdata('msg', $msg);
     echo "<script>window.history.back();</script>";
 }