function save()
 {
     //IMBAuth::checkOAuth();
     $device_id = addslashes($_POST['device_id']);
     $type = addslashes($_POST['type']);
     //completion check
     if ($device_id == "" || $type == "") {
         $json['status_code'] = 0;
         $json['status_message'] = "Incomplete Request";
         echo json_encode($json);
         die;
     }
     //check account..
     $acc = isset($_POST['acc_id']) ? addslashes($_POST['acc_id']) : 0;
     $res_id = isset($_POST['res_id']) ? addslashes($_POST['res_id']) : 0;
     //        IMBAuth::checkOAuth();
     $dn = new DeviceModel();
     $dnquery = new DeviceModel();
     // langkah 1 , device ID ada device type ada
     $arrs = $dnquery->getWhere("device_id = '{$device_id}' AND device_type = '{$type}'");
     $dn = $arrs[0];
     if ($dn->did == "") {
         $dn = new DeviceModel();
         $dn->device_id = $device_id;
         $dn->device_type = $type;
         $dn->acc_id = $acc;
         $dn->firstlogin = leap_mysqldate();
         $dn->dev_res_id = $res_id;
     } else {
         //kalau device id ada, acc di update
         $dn->load = 1;
         $dn->acc_id = $acc;
         $dn->dev_res_id = $res_id;
     }
     $dn->dev_lng = addslashes($_POST['lng']);
     $dn->dev_lat = addslashes($_POST['lat']);
     $dn->logindate = leap_mysqldate();
     //save user latlong to MasterUser -- 10 May 2016 sendy
     if (isset($_POST['lat']) && isset($_POST['lng']) && $acc != 0) {
         $user = new UserModel();
         $user->getByID($acc);
         $user->logindate = leap_mysqldate();
         $user->latitude = addslashes($_POST['lat']);
         $user->longitude = addslashes($_POST['lng']);
         $user->save();
     }
     //save user District and City to LocationModel -- 26 May 2016 sendy
     Generic::saveDistrictCityFromLatLng(addslashes($_POST['lat']), addslashes($_POST['lng']), $acc, $type == "cashier");
     if ($dn->save()) {
         $json['save_status'] = 1;
         $json['version'] = Efiwebsetting::getData('App_Version_' . strtolower($type));
         $json['url'] = Efiwebsetting::getData('App_URL_' . strtolower($type));
         //logged all device login 19 nov 2015 roy
         //            $logged = new DeviceLogger();
         //            $logged->log_acc_id = $dn->acc_id;
         //            $logged->log_date = leap_mysqldate();
         //            $logged->log_dev_id = $dn->device_id;
         //            $logged->log_dev_type = $dn->device_type;
         //            $logged->save();
     } else {
         $json['save_status'] = 0;
     }
     $json['status_code'] = 1;
     if ($_POST['do_not_die']) {
         return $json;
     } else {
         echo json_encode($json);
         die;
     }
 }
 public function userSignUp()
 {
     if (Efiwebsetting::getData('checkOAuth') == 'yes') {
         IMBAuth::checkOAuth();
     }
     $update = !Generic::IsNullOrEmptyString($_POST['id_user']);
     $idUser = Generic::IsNullOrEmptyString($_POST['id_user']) ? "" : $_POST['id_user'];
     $fullName = Generic::mustCheck($_POST['full_name'], Keys::$ERR_USER_EMPTY_FULL_NAME);
     $userName = Generic::mustCheck($_POST['user_name'], Keys::$ERR_USER_EMPTY_NICK_NAME);
     $email = Generic::mustCheck($_POST['email'], Keys::$ERR_USER_EMPTY_EMAIL);
     $password = Generic::mustCheck($_POST['pwd'], Keys::$ERR_USER_EMPTY_PASSWORD);
     //        $password2 = Generic::mustCheck($_POST['pwd2'], Keys::$ERR_USER_EMPTY_PASSWORD_2);
     $idCuisine = Generic::mustCheck($_POST['pref_cuisine'], Keys::$ERR_USER_EMPTY_CUISINE);
     $birthday = Generic::mustCheck($_POST['birthday'], Keys::$ERR_USER_EMPTY_BIRTHDAY);
     $phoneNo = Generic::mustCheck($_POST['phone_no'], Keys::$ERR_USER_EMPTY_PHONE);
     $fbId = Generic::IsNullOrEmptyString($_POST['fb_id']) ? "" : $_POST['fb_id'];
     $latitude = Generic::getOrDefault($_POST['lat'], Keys::$DEFAULT_LATITUDE);
     $longitude = Generic::getOrDefault($_POST['long'], Keys::$DEFAULT_LONGITUDE);
     $district = Generic::getOrDefault($_POST['district'], Keys::$EMPTY);
     $city = Generic::getOrDefault($_POST['city'], Keys::$EMPTY);
     $pic = Generic::getOrDefault($_POST['pic'], Keys::$EMPTY);
     ///START OF LOVELY VALIDATION TIME
     if (!Generic::isValidUserName($userName)) {
         Generic::errorMsg(Keys::$ERR_USER_INVALID_USERNAME);
     }
     if (!Generic::isValidUserNameLength($userName)) {
         Generic::errorMsg(Keys::$ERR_USER_USERNAME_TOO_LONG);
     }
     if (!Generic::isValidEmail($email)) {
         Generic::errorMsg(Keys::$ERR_USER_INVALID_EMAIL);
     }
     if (!Generic::isValidPassword($password)) {
         Generic::errorMsg(Keys::$ERR_USER_INVALID_PASSWORD);
     }
     //        if ($password != $password2)
     //            Generic::errorMsg(Keys::$ERR_USER_CONFIRM_PASSWORD_NOT_MATCH);
     $u = new UserModel();
     $arrU = $u->getWhere("user_name='{$userName}'");
     if (count($arrU) > 0) {
         Generic::errorMsg(Keys::$ERR_USER_DUPLICATE_USERNAME);
     }
     $u1 = new UserModel();
     $arrU = $u1->getWhere("email='{$email}'");
     if (count($arrU) > 0) {
         Generic::errorMsg(Keys::$ERR_USER_DUPLICATE_EMAIL);
     }
     if ($fbId != "") {
         $u2 = new UserModel();
         $arrU = $u2->getWhere("fb_id='{$fbId}'");
         if (count($arrU) > 0) {
             Generic::errorMsg(Keys::$ERR_USER_DUPLICATE_FB_ID);
         }
     }
     ///END OF LOVELY VALIDATION TIME, HOW SAD :(
     //pagar untuk regis
     if (Efiwebsetting::getData('Doku_switch')) {
         $doku = new PaymentDoku();
         $dokuId = $doku->registerCustomer($fullName, $email, $phoneNo);
         if (!$dokuId) {
             Generic::errorMsg(Keys::$ERR_USER_FAILED_CREATE_DOKU);
         }
     }
     $user = new UserModel();
     $user->full_name = $fullName;
     $user->user_name = $userName;
     $user->email = $email;
     $user->password = $password;
     $user->id_cuisine = $idCuisine;
     $user->birthday = $birthday;
     if ($pic == "") {
         $user->pic = "";
     } else {
         $user->pic = Util::savePic($pic);
     }
     $user->phone_no = $phoneNo;
     $user->fb_id = $fbId;
     $user->latitude = $latitude;
     $user->longitude = $longitude;
     $user->district = $district;
     $user->city = $city;
     $user->last_lat = $latitude;
     $user->last_long = $longitude;
     $user->last_district = $district;
     $user->last_city = $city;
     $user->status = "1";
     $user->payment_id = $dokuId;
     //        $user->load = 1;
     $uid = $user->save();
     if (!$uid) {
         Generic::errorMsg(Keys::$ERR_USER_FAILED_CREATE);
     } else {
         Generic::saveDistrictCityFromLatLng($latitude, $longitude, $uid, false);
         $results['fb_id'] = $fbId;
         $results['id_user'] = $uid;
         //$user->id_user;
         $results['user_name'] = $userName;
         $results['full_name'] = $fullName;
         $results['email'] = $email;
         $results['pic'] = Generic::insertImageUrl($pic);
         Generic::finish($results);
     }
 }