Example #1
0
 public function names()
 {
     header('Content-Type: application/json');
     $realestates = Real_Estate::get();
     $response = array_map(function ($element) {
         return $element->name;
     }, $realestates);
     echo json_encode($response);
 }
Example #2
0
 public function login()
 {
     if ($_SERVER['REQUEST_METHOD'] === 'POST') {
         // check user type
         if ($_POST['usertype'] === 'owner') {
             // if owner
             $owner = Owner::get(['email' => $_POST['email']])[0];
             if ($owner) {
                 // check the password
                 if (validate_password($_POST['password'], $owner->password)) {
                     // password matched
                     $_SESSION['usertype'] = USERTYPE_OWNER;
                     $_SESSION['user'] = $owner;
                 } else {
                     // wrong password
                     Flash::set('error_message', 'Wrong email or password');
                 }
             } else {
                 // cannot find the user with the email
                 Flash::set('error_message', 'Wrong email or password');
             }
         } elseif ($_POST['usertype'] === 'tenant') {
             $tenant = Tenant::get(['email' => $_POST['email']])[0];
             if ($tenant) {
                 // check the password
                 if (validate_password($_POST['password'], $tenant->password)) {
                     // password matched
                     $_SESSION['usertype'] = USERTYPE_TENANT;
                     $_SESSION['user'] = $tenant;
                 } else {
                     // wrong password
                     Flash::set('error_message', 'Wrong email or password');
                 }
             } else {
                 // cannot find the user with the email
                 Flash::set('error_message', 'Wrong email or password');
             }
         } elseif ($_POST['usertype'] === 'agent') {
             $agent = Agent::get(['email' => $_POST['email']])[0];
             if ($agent) {
                 // check the password
                 if (validate_password($_POST['password'], $agent->password)) {
                     // password matched
                     $_SESSION['usertype'] = USERTYPE_AGENT;
                     $_SESSION['user'] = $agent;
                 } else {
                     // wrong password
                     Flash::set('error_message', 'Wrong email or password');
                 }
             } else {
                 // cannot find the user with the email
                 Flash::set('error_message', 'Wrong email or password');
             }
         } elseif ($_POST['usertype'] === 'real_estate') {
             // if real estate
             $realestate = Real_Estate::get(['name' => $_POST['name']])[0];
             if ($realestate) {
                 // check password
                 if (validate_password($_POST['password'], $realestate->password)) {
                     $_SESSION['usertype'] = USERTYPE_REALESTATE;
                     $_SESSION['user'] = $realestate;
                 } else {
                     // wrong password
                     Flash::set('error_message', 'Wrong password');
                 }
             }
         }
     }
     $this->redirect('/');
 }