Created
December 27, 2014 08:22
-
-
Save b4oshany/b16ce9e0a9b5c3d14628 to your computer and use it in GitHub Desktop.
Sample main.ini.php file of Vecni-PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
# define package usage | |
use libs\vecni\http\Response; | |
use libs\vecni\http\Request; | |
use libs\vecni\Vecni as app; | |
use libs\vecni\Session as session; | |
use controller\user\User; | |
use controller\property; | |
User::start_session(); | |
$user = User::get_current_user(); | |
$less = app::use_less(); | |
Response::init(); | |
// Set the default title of website. | |
app::$twig->addGlobal("title", app::$BRAND_NAME); | |
if(User::is_login()){ | |
app::$twig->addGlobal("user", User::get_current_user()); | |
} | |
app::set_route("/", "home"); | |
app::set_route("/home", "home"); | |
/** | |
* Render the homepage. | |
* @example http://ohomes.com | |
* @example http://ohomes.com/home | |
*/ | |
function home(){ | |
app::$twig->addGlobal("properties", property\Property::query()); | |
if(User::is_login()){ | |
return app::$twig->render("home.html", | |
array( | |
"html_class"=>"welcome" | |
)); | |
}else{ | |
return app::$twig->render('home.html', | |
array( | |
"html_class"=>"welcome", | |
"title"=>app::$BRAND_NAME | |
) | |
); | |
} | |
} | |
app::set_route("/docs", "docs"); | |
/** | |
* Render the documentation page. | |
* @example http://ohomes.com/docs/ | |
*/ | |
function docs(){ | |
if(app::in_development()){ | |
app::redirect("/docs/index.html"); | |
}else{ | |
app::abort(); | |
} | |
} | |
app::set_route("/user/signin", "signin_require"); | |
/** | |
* Render the sign in page for users | |
* @example http://ohomes.com/user/signin | |
*/ | |
function signin_require($message=""){ | |
if(!User::is_login()){ | |
echo app::$twig->render('user_signin.html', | |
array( | |
"html_class"=>"signin", | |
"title"=>"Signin Required", | |
"message"=>$message | |
) | |
); | |
die(); | |
} | |
} | |
app::set_route("/user/signin/process", "process_login"); | |
/** | |
* Process the user signin. | |
* @example http://ohomes.com/user/sigin/process | |
*/ | |
function process_login(){ | |
if(!empty($_POST['email']) && !empty($_POST['password'])){ | |
$email = $_POST['email']; | |
$pass = $_POST['password']; | |
$status = User::login($email, $pass); | |
if(Request::is_async()){ | |
if($status){ | |
return Response::json_response(200, $email); | |
}else{ | |
return Response::abort("$email, does not exists in our system. Please register for account if you don't have one"); | |
} | |
}else{ | |
if($status){ | |
return app::nav_back(); | |
}else{ | |
return signin_require(); | |
} | |
} | |
} | |
} | |
app::set_route("/user/registration", "reg_request"); | |
/** | |
* Render the registration page. | |
* @example http://ohomes.com/user/registration | |
*/ | |
function reg_request($message=""){ | |
if(User::is_login()){ | |
app::redirect(); | |
} | |
return app::$twig->render('user_registration.html', | |
array("html_class"=>"user-registration", | |
"title"=>"Registration", | |
) | |
); | |
} | |
app::set_route("/user/registration/process", "register"); | |
/** | |
* Process the user registration. | |
* @example http://ohomes.com/user/sigin/process | |
*/ | |
function register(){ | |
global $user; | |
if(($first_name = Request::POST('first_name')) && | |
($last_name = Request::POST('last_name')) && | |
($password = Request::POST('password')) && | |
($email = Request::POST('email'))){ | |
$new_user = new User(); | |
$new_user->first_name = $first_name; | |
$new_user->last_name = $last_name; | |
if($dob = Request::POST('dob')){ | |
$new_user->dob = $dob; | |
}else{ | |
$new_user->dob = "0000-00-00"; | |
} | |
$new_user->gender = Request::POST('gender', "other"); | |
$status = $new_user->register($email, $password); | |
if(Request::is_async()){ | |
if($status){ | |
return Response::json_response(200, $email); | |
}else{ | |
return Response::abort("This accound has already been registered"); | |
} | |
}else{ | |
if($status){ | |
app::redirect(); | |
}else{ | |
app::redirect(); | |
} | |
} | |
} | |
} | |
app::set_route("/user/facebook/login", "login_with_social_network"); | |
app::set_route("/user/googleplus/login", "login_with_social_network"); | |
app::set_route("/user/twitter/login", "login_with_social_network"); | |
/** | |
* Process the user sigin or registration via social netowrk. | |
* @example http://ohomes.com/user/facebook/login | |
* @example http://ohomes.com/user/googleplus/login | |
* @exmaple http://ohomes.com/user/twitter/login | |
*/ | |
function login_with_social_network(){ | |
global $user; | |
if(User::is_login()){ | |
app::redirect(); | |
} | |
if(!empty($_POST['first_name']) && !empty($_POST['last_name']) && !empty($_POST['social_network']) && !empty($_POST['social_network_id']) && !empty($_POST['email'])){ | |
$new_user = new User(); | |
$new_user->first_name = $_POST['first_name']; | |
$new_user->last_name = $_POST['last_name']; | |
$email = $_POST['email']; | |
$new_user->dob = DateTime::createFromFormat('m/d/Y', | |
$_POST['dob']); | |
$new_user->gender = $_POST['gender']; | |
if(!empty($_POST['school'])){ | |
$new_user->school = $_POST['school']; | |
} | |
$account_type = $_POST['social_network']; | |
$account_id = $_POST['social_network_id']; | |
$status = $new_user->login_with_social_network($email, $account_type, $account_id); | |
if($status){ | |
return Response::json_response(200, $email); | |
}else{ | |
return Response::json_response(204, "Something went wrong"); | |
} | |
} | |
} | |
app::set_route("/logout", "log_out"); | |
/** | |
* Log out users out of the system. | |
* @example http://ohomes.com/logout | |
*/ | |
function log_out(){ | |
if(User::is_login()){ | |
User::log_out(); | |
app::$twig->addGlobal("user", new User()); | |
} | |
app::redirect(); | |
} | |
app::set_route("/property/advertise", "property_advertise"); | |
/** | |
* Register property on the system. | |
* @example http://ohomes.com/property/advertise | |
*/ | |
function property_advertise(){ | |
if(User::is_login()){ | |
$country_data = file_get_contents("etc/data/country.json"); | |
$country_data = json_decode($country_data, true); | |
$parishes = array(); | |
foreach($country_data["Jamaica"]["parishes"] as $parish){ | |
$parish_name = $parish["name"]; | |
$parishes[$parish_name] = $parish_name; | |
} | |
return app::$twig->render('property_advertise.html', | |
array('html_class'=>'property_advertise', | |
'title'=>'Advertise Your Property', | |
'parishes'=>$parishes)); | |
}else{ | |
return signin_require(); | |
} | |
} | |
app::set_route("/property/register", "property_register"); | |
/** | |
* Process the property registration. | |
* @example http://ohomes.com/proeprty/register | |
*/ | |
function property_register(){ | |
signin_require(); | |
if(($title = Request::POST("property_title")) && | |
($property_type = Request::POST("property_type")) && | |
($property_state = Request::POST("property_state")) && | |
($exhbition = Request::POST("exhibition_type")) && | |
($cost = Request::POST("property_cost")) && | |
($street = Request::POST("property_street")) && | |
($town = Request::POST("property_town")) && | |
($parish = Request::POST("property_parish")) | |
){ | |
$property = property\Property::register($title, | |
$cost, | |
$property_state, | |
$property_type, | |
$exhbition, | |
$street, | |
$town, | |
$parish | |
); | |
$property_page = "/property/$property->property_id/"; | |
app::redirect($property_page, true, $property->property_title); | |
return property_view($property->property_id); | |
}else{ | |
return Response::abort(); | |
} | |
} | |
app::set_route("/property/v/{property_id}", "property_view"); | |
/** | |
* Render a property with the given property id. | |
* @global int $property_id - ID of property. | |
* @example http://ohomes.com/property/v/17 | |
*/ | |
function property_view($property_id=null){ | |
$property_id = Request::GET("property_id") or $property_id; | |
if(!$property_id){ | |
app::abort(); | |
} | |
$property = property\Property::get_by_id($property_id); | |
$property->get_accessories(); | |
return app::$twig->render('property_view.html', | |
array('html_class'=>'property_view', | |
'title'=>$property->property_title, | |
'property'=>$property | |
)); | |
} | |
app::set_route("/property/{property_id}/accessories/add/process", "accessory_add"); | |
/** | |
* Process the adding of accessories and/or appliances to a property. | |
* @global int $property_id - ID of property. | |
* @global string $_POST[accessory_name] Name of the accessory. | |
* @global string $_POST[accessory_type] Type of the accessory. | |
* @global string $_POST[quantity] Quantity of the accessory. | |
* @global string $_POST[accessory_description] Description of the accessory. | |
* @example http://ohomes.com/property/17/accessories/add/process | |
*/ | |
function accessory_add($property_id=null){ | |
if(empty($property_id)) | |
$property_id = Request::GET("property_id", "id"); | |
// Get the accessory/appliance basic information. | |
if(($accessory_name = Request::POST("accessory_name")) && | |
($accessory_type = Request::POST("accessory_type")) && | |
($quantity = Request::POST("quantity")) | |
){ | |
// Get the accessory description if there is any, else default the value to a null string. | |
$description = Request::POST("accessory_description", ""); | |
$result = property\Property::add_accessory($property_id, $accessory_name, $accessory_type, $quantity, $description); | |
if($result){ | |
return "$accessory_name was added"; | |
}else{ | |
app::abort(); | |
} | |
}else{ | |
Response::abort("Service is unavailable."); | |
} | |
} | |
/** | |
* Allow users to add a nearby place to property. | |
*/ | |
app::set_route("/property/{property_id}/places/nearby/add/process", "nearby_places_add"); | |
/** | |
* Process adding of places nearby. | |
* @global int $property_id - ID of property. | |
* @global string $_POST[vicinity_name] Name of vicinity. | |
* @global string $_POST[vicinity_type] Type of vicinity. | |
* @global string $_POST[description] Description of vicinity. | |
* @global string $_POST[latitude] Latitude of vicinity address cordinates. | |
* @global string $_POST[longitude] Longitude of vicinity address cordinates. | |
* @global string $_POST[street] Vicinity street. | |
* @example "http://ohomes.com/property/17/places/nearby/add/process" | |
*/ | |
function nearby_places_add(){ | |
signin_require(); | |
$property_id = Request::GET("property_id"); | |
return; | |
if(($vicinity_name = Request::POST("vicinity_name")) && | |
($vicinity_type = Request::POST("vicinity_type")) | |
){ | |
$description = Request::POST("vicinity_description", ""); | |
$latitude = Request::POST("latitude", 0); | |
$longitude = Request::POST("longitude", 0); | |
$street = Request::POST("vicinity_street", ""); | |
} | |
} | |
app::set_route("/property/{property_id}/update/address/{address_id}", "property_address_update"); | |
/** | |
* Update property address information. | |
* | |
* @global string $_GET[property_id] Property id. | |
* @global string $_GET[address_id] Address id. | |
* @gloabl string $_POST[street] Street. | |
* @global string $_POST[town] Town. | |
* @global string $_POST[parish] Parish. | |
* @global string $_POST[country] Country. | |
* @global string $_POST[description] Address description. | |
* @global double $_POST[longitude] Longitudinal cordinate of address. | |
* @gloabl double $_POST[latitude] Latitudinal cordinate of address. | |
* @uses controller\property\location\PropertyAddress:update() to update property address. | |
*/ | |
function property_address_update(){ | |
signin_require(); | |
$updates = array(); | |
if(($property_id = Request::GET("property_id")) && | |
($address_id = Request::GET("address_id"))){ | |
if($street = Request::POST("street")){ | |
$updates["street"] = "$street"; | |
} | |
if($town = Request::POST("town")){ | |
$updates["town"] = "$town"; | |
} | |
if($parish = Request::POST("parish")){ | |
$updates["parish"] = "$parish"; | |
} | |
if($country = Request::POST("country")){ | |
$updates["country"] = "$country"; | |
} | |
if($description = Request::POST("description")){ | |
$updates["description"] = "$description"; | |
} | |
if($latitude = Request::POST("latitude")){ | |
$updates["latitude"] = "$latitude"; | |
} | |
if($longitude = Request::POST("longitude")){ | |
$updates["longitude"] = $longitude; | |
} | |
if(property\location\PropertyAddress::update($property_id, $address_id, $updates)){ | |
return Response::json_response(); | |
} | |
} | |
app::abort("Something went worg"); | |
} | |
app::set_route("/property/{property_id}/comment/add/process", "property_comment_add"); | |
/** | |
* Process the adding of comments to the poperty forum. | |
* @global int $property_id ID of property. | |
* @global string $_POST[comment] User comment. | |
* @example http://ohomes.com/property/17/property/comment/add/process | |
*/ | |
function property_comment_add(){ | |
if(($property_id = Request::GET("property_id")) && | |
($comment = Request::POST("comment"))){ | |
$last_access_time = session::previous_data("time", date("m/d/Y H:i:s")); | |
$forum = property\Property::get_forum($property_id); | |
$forum->add_comment($comment); | |
$comments = $forum->get_comments("date_created >= '$last_access_time'"); | |
Response::add_header("message", "Comment has been successfully added"); | |
$format = Request::GET("format", "xml"); | |
switch($format){ | |
case "json": | |
return Response::json_feed($comments); | |
default: | |
return app::$twig->render('bit/comments.html', array( | |
"comments"=>$comments, | |
"comment_type"=>'property' | |
)); | |
} | |
} | |
Response::abort("Comment has not been added"); | |
} | |
app::set_route("/property/{property_id}/comment/{comment_id}/delete", "property_comment_delete"); | |
/** | |
* Process the deletion of property comments. | |
* URL Dispatched - /property/{property_id}/comment/{comment_id}/delete | |
* @global int $_POST[property_id] ID of property. | |
* @global int $_POST[comment_id] ID of comment. | |
* @example http://ohomes.com/property/17/comment/3/delete | |
*/ | |
function property_comment_delete(){ | |
$property_id = Request::GET("property_id"); | |
$comment_id = Request::GET("comment_id"); | |
$property = new property\Property($property_id); | |
$owner = $property->permissions->get_owner(); | |
$comments = $property->forum->get_comments("comment_id = $comment_id"); | |
$results = array(); | |
foreach($comments as $comment){ | |
$result = $comment->delete(($owner->user_id == $comment->author->user_id)); | |
array_push($results, $result); | |
} | |
return Response::json_response($results); | |
} | |
app::set_route("/property/{property_id}/image/add/process", "property_image_add"); | |
/** | |
* Process the upload of property photos. | |
* URL Dispatched - /property/{property_id}/image/add/process | |
* @global int $property_id - ID of property. | |
* @global string $_POST[title] Title of the photo. | |
* @global string $_POST[category] The category of the photo. | |
* @global string $_POST[description] The description of the photo, where applicable. | |
* @example http://ohomes.com/user/sigin/process | |
*/ | |
function property_image_add(){ | |
if(($property_id = Request::GET("property_id")) && | |
($title = Request::POST("title"))){ | |
$status = property\PropertyGallery::save_image($property_id, | |
"files", | |
Request::POST("category", ""), | |
Request::POST("title", ""), | |
Request::POST("description", "") | |
); | |
if($status == true){ | |
return "Image has been successfully uploaded"; | |
}else{ | |
app::abort($status); | |
} | |
} | |
Response::abort("No title nor id provided."); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment