Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Support\Facades\Storage;
- use App\Enquiry;
- use App\Shortlist;
- use Auth;
- use Request;
- class Property extends Model
- {
- protected $table = 'properties';
- // Primary key
- public $primaryKey = 'id';
- // Timestamps
- public $timestamps = true;
- protected $admin_allowed = ['for', 'in', 'ref', 'is_featured', 'minbeds', 'property_type_id', 'price_range', 'status', 'sevenDays', 'oneDay', 'popular',
- 'beds', // Added this one for the front-end...
- 'sort', // And this one...
- 'exclude_id', // And this one... (Similar Properties)
- ];
- //protected $fillable = ['is_rental'];
- /**
- * Get the user record associated with the property media
- *
- */
- public function propertyMedia()
- {
- return $this->hasMany('App\PropertyMedia')->orderBy('sequence', 'ASC');
- }
- /**
- * Get the user record associated with the property media photo
- *
- */
- public function propertyMediaPhotos()
- {
- return $this->hasMany('App\PropertyMedia')->where('type', 'photo')->orderBy('sequence', 'ASC');
- }
- /**
- * Get the user record associated with the property media floorplan
- *
- */
- public function propertyMediaFloorplans()
- {
- return $this->hasMany('App\PropertyMedia')->where('type', 'floorplan')->orderBy('sequence', 'ASC');
- }
- /**
- * Get the user record associated with the property media documents
- *
- */
- public function propertyMediaDocuments()
- {
- return $this->hasMany('App\PropertyMedia')->where('type', 'document')->orderBy('sequence', 'ASC');
- }
- /**
- * Get the user record associated with the property
- *
- */
- public function user()
- {
- return $this->belongsTo('App\User');
- }
- /**
- * Get the user record associated with the property
- *
- */
- public function users()
- {
- //Users::all();
- //return $this->hasMany('App\User');
- require $this->all('App\User');
- }
- /**
- * Get the property type record associated with the property
- *
- */
- public function type()
- {
- return $this->belongsTo('App\PropertyType', 'property_type_id', 'id');
- }
- /**
- * Get the Shortlist record associated with the property
- *
- */
- public function shortlists()
- {
- return $this->belongsTo('App\Shortlist', 'id', 'property_id');
- }
- // filter by IP or User ID
- public function shortlist_ip()
- {
- $ip = Request::ip();
- if(Auth::user())
- {
- $user_id = Auth::user()->id;
- }
- else
- {
- $user_id = null;
- }
- return $this->belongsTo('App\Shortlist', 'id', 'property_id')
- ->where('property_id', $this->id)
- ->where('user_id', $user_id)
- ->where('ip', $ip);
- }
- // Check if shortlist exist by IP
- public function getCheckShortlistIpAttribute()
- {
- $check = !empty($this->shortlist_ip) ? TRUE : FALSE;
- return $check;
- }
- /**
- * Get the property enquiries record associated with the property
- *
- */
- public function propertyEnquiries()
- {
- //return $this->hasMany('App\Enquiry', 'ref', 'ref')->where('archived_at', NULL)->orderBy('created_at', 'DESC');
- return $this->hasMany('App\Enquiry', 'ref', 'ref')->orderBy('created_at', 'DESC');
- }
- public function enquiries()
- {
- return $this->belongsTo('App\Enquiry', 'ref', 'ref');
- }
- /**
- * Fetch properties for display in the Admin's Properties page
- *
- */
- public static function getAllForSitemap()
- {
- $properties = Property::all();
- return $properties;
- }
- /**
- * Fetch properties for display in the Admin's Properties page
- *
- */
- public static function getProperties($paginate = 10)
- {
- $properties = Property::latest()->paginate($paginate);
- return $properties;
- }
- // query scope to order by the latest properties
- public function scopeLatest($query)
- {
- return $query->order_by('created_at', 'desc');
- }
- // get property type or empty
- public function getPropertyTypeNameAttribute()
- {
- $typeName = '';
- if(!empty($this->property_type_id)){
- $typeName = (!empty($this->type->name)) ? $this->type->name : '';
- }
- return $typeName;
- }
- // get property type or property
- public function getPropertyTypeNamePlaceholderAttribute()
- {
- $typeName = '';
- if(!empty($this->property_type_id)){
- $typeName = (!empty($this->type->name)) ? $this->type->name : 'Property';
- }
- return $typeName;
- }
- // get default area unit
- public function getAreaUnitAttribute()
- {
- $area_unit = settings('area_unit');
- return !empty($area_unit) ? $area_unit : 'sq m';
- }
- // get default area unit
- public function getDisplayLandUnitAttribute()
- {
- return (!empty($this->land_area_unit)) ? $this->land_area_unit : $this->getAreaUnitAttribute();
- }
- // get default area unit
- public function getDisplayInternalUnitAttribute()
- {
- return (!empty($this->internal_area_unit)) ? $this->internal_area_unit : $this->getAreaUnitAttribute();
- }
- /**
- * Accessor:
- * The full address of the property, likely used in the Admin
- *
- */
- public function getDisplayInternalAreaAttribute()
- {
- //$value = $this->internal_area.'m<sup>2</sup>';
- $value = $this->internal_area.' '.$this->DisplayInternalUnit;
- return $value;
- }
- /**
- * Accessor:
- * The headline for search results, Similar Properties, Featured Properties etc
- *
- */
- public function getSearchHeadlineAttribute()
- {
- $typeName = $this->PropertyTypeName;
- $value = sprintf('%s%s',
- !empty($this->beds) ? $this->beds.' Bed ' : '', $typeName
- );
- $value = !empty($this->name) ? $this->name : $value;
- return $value;
- }
- /**
- * Accessor:
- * The full address of the property, likely used in the Admin Template 2
- *
- */
- public function getSearchHeadlineV2Attribute()
- {
- if(settings('overseas') == 1){
- $address = '<span>'.implode(', ', array_filter([$this->complex_name, $this->town, $this->city])).'<span>';
- }else{
- $address= '<span>'.implode(', ', array_filter([$this->town, $this->city, $this->region])).'<span>';
- }
- $value = sprintf('%s%s',
- !empty($this->beds) ? $this->beds.' Bed ' : '',
- $this->PropertyTypeName
- );
- if( !empty($this->name) ){
- $value = $this->name;
- }
- $value = $value.', '.$address;
- return $value;
- }
- /**
- * Accessor:
- * The headline for Property details pages
- *
- */
- public function getDetailsHeadlineAttribute()
- {
- // format: [NUMBER OF BEDS] [PROPERTY TYPE] [FOR SALE / TO RENT] in [REGION]
- $items = [];
- if ($this->beds) $items[] = $this->beds.' Bed';
- $items[] = $this->PropertyTypeName;
- $items[] = ($this->is_rental) ? 'To Rent' : 'For Sale';
- if ($this->region) $items[] = 'in '.$this->region;
- $value = implode(' ', $items);
- $value = !empty($this->name) ? $this->name : $value;
- return $value;
- }
- /**
- * Accessor:
- * The headline for Property details v2 pages
- *
- */
- public function getDetailsHeadlineV2Attribute()
- {
- // format: [NUMBER OF BEDS] [PROPERTY TYPE] [FOR SALE / TO RENT] in [REGION]
- $items = [];
- if ($this->beds) $items[] = $this->beds.' Bed';
- $items[] = $this->PropertyTypeName;
- $items[] = ($this->is_rental) ? 'To Rent' : 'For Sale';
- $value = implode(' ', $items);
- $value = !empty($this->name) ? $this->name : $value;
- return $value;
- }
- /**
- * Accessor:
- * The full address of the property, likely used in the Admin
- *
- */
- public function getDisplayAddressAttribute()
- {
- if(settings('overseas') == 1){
- $value = implode(', ', array_filter([$this->complex_name, $this->street, $this->town, $this->city, $this->country]));
- }else{
- $value = implode(', ', array_filter([$this->street, $this->town, $this->city, $this->region, $this->country]));
- }
- return $value;
- }
- //Featured and Similar Properties line one
- public function getFsAddress1Attribute()
- {
- //Town/Village - dark
- if(settings('overseas') == 1){
- $value = implode(', ', array_filter([$this->complex_name, $this->town]));
- }else{
- $value = $this->town;
- }
- return $value;
- }
- //Featured and Similar Properties line two
- public function getFsAddress2Attribute()
- {
- if(settings('overseas') == 1){
- $value = $this->city;
- }else{
- $value = implode(', ', array_filter([$this->city, $this->region]));
- }
- return $value;
- }
- //Display generic address...
- public function getDisplayPropertyAddressAttribute()
- {
- if(settings('overseas') == 1){
- $value = implode(', ', array_filter([$this->complex_name, $this->town, $this->city]));
- }else{
- $value = implode(', ', array_filter([$this->town, $this->city, $this->region]));
- }
- return $value;
- }
- //Generic page latest properties
- public function getGenericPropertyAddressAttribute()
- {
- if(settings('overseas') == 1){
- $value = implode(', ', array_filter([$this->complex_name, $this->town, $this->city]));
- }else{
- $value = implode(', ', array_filter([$this->town, $this->city]));
- }
- return $value;
- }
- /**
- * Accessor:
- * Formatted date of the property creation date, used in the Admin
- *
- */
- public function getDisplayDateAttribute()
- {
- $value = admin_date($this->created_at);
- return $value;
- }
- /**
- * Accessor:
- * Display summary
- *
- */
- public function getSummaryAttribute()
- {
- $value = str_limit(strip_tags($this->description), 250, '...');
- return $value;
- }
- /**
- * Accessor:
- * Display excerpt
- *
- */
- public function getExcerptAttribute()
- {
- $value = str_limit(strip_tags($this->description), 150, '...');
- return $value;
- }
- /**
- * Accessor:
- * Display summary
- *
- */
- public function getSummaryDemo3Attribute()
- {
- $value = str_limit(strip_tags($this->description), 350, '...');
- return $value;
- }
- /**
- * Accessor:
- * Formatted display price of the property, including currency
- *
- */
- public function getDisplayPriceAttribute()
- {
- if (empty($this->price)) {
- return 'POA';
- }
- $rent_freq = false;
- if (!empty($this->price) && !empty($this->is_rental)) {
- switch ($this->rent_period) {
- case 1:
- $rent_freq = '/<abbr title="Daily">day<abbr>';
- break;
- case 2:
- $rent_freq = '/<abbr title="Weekly">wk.<abbr>';
- break;
- default:
- $rent_freq = '/<abbr title="Monthly">mth.<abbr>';
- break;
- }
- }
- $currency_symbol=settings('currency_symbol');
- $currency_symbol = (!empty($currency_symbol)) ? $currency_symbol : '£';
- $value = sprintf('%s%s%s',
- settings('currency_symbol'),
- number_format($this->price),
- $rent_freq
- );
- return $value;
- }
- /**
- * Accessor:
- * The full Addition info of the property, likely used in the Admin
- *
- */
- public function getDisplayAddInfoAttribute()
- {
- $html = '';
- $value = $this->add_info;
- if(!empty($value))
- {
- $delimiters = array(',', ';', '.');
- $array = $this->explode_array_delimiters($delimiters, $value);
- if(count($array)){
- $html .= '<div class="add-info-container"><div class="row">';
- foreach ($array as $value) {
- $html .= '<div class="col-md-3 col-sm-3"><div class="item-ai">';
- $html .= $value;
- $html .= '</div></div>';
- }
- $html .= '</div></div>';
- }
- }
- return $html;
- }
- public function getDisplayAddInfoDemo3Attribute()
- {
- $html = '';
- $value = $this->add_info;
- $ctr = 0;
- if(!empty($value)){
- $delimiters = array(',', ';', '.');
- $array = $this->explode_array_delimiters($delimiters, $value);
- if(count($array))
- {
- $html .= '<div class="property-specs"><div class="row ps-item">';
- foreach ($array as $value) {
- $ctr++;
- if( $ctr <= 6){
- if($ctr%2 == 1){
- $html .= '<div class="col-6"><div class="style-1"><i class="fas fa-circle"></i>';
- $html .= $value;
- $html .= '</div></div>';
- }else{
- $html .= '<div class="col-6"><div class="style-2">';
- $html .= $value;
- $html .= '</div></div>';
- }
- }
- }
- $html .= '</div></div>';
- }
- }
- return $html;
- }
- public function getModeDisplayAttribute()
- {
- $mode = ($this->is_rental) ? 'For Rent' : 'For Sale';
- return $mode;
- }
- /**
- * Accessor:
- * Get the URL for the property's details page
- *
- */
- public function getUrlAttribute()
- {
- // Format: /property/[NUMBER OF BEDS]-bed-[PROPERTY TYPE]-[FOR-SALE / TO-RENT]-in-[CITY]/[PROPERTY ID]
- $items = [];
- if ($this->beds) $items[] = $this->beds.' bed';
- $items[] = $this->PropertyTypeName;
- $items[] = ($this->is_rental) ? 'to rent' : 'for sale';
- if ($this->city) $items[] = 'in '.$this->city;
- $value = implode(' ', $items);
- $slug = str_slug($value);
- $value = sprintf('%s/%s/%s',
- 'property', // property route
- $slug, // SEO-friendly
- $this->{$this->primaryKey} // property identifier
- );
- return url($value);
- }
- /**
- * Accessor:
- * Get the status for display
- *
- */
- public function getStateDisplayAttribute()
- {
- $state_display = 'Inactive';
- $status = $this->status;
- $states_array = p_states();
- if( $status != '' || $status == '0' ){
- $state_display = $states_array[$status];
- }
- if($status == 1)
- $state_display = '';
- return $state_display;
- }
- /**
- * Accessor:
- * Get the Primary Photo
- *
- */
- public function getPrimaryPhotoAttribute()
- {
- $propertyMediaPhotos = $this->propertyMediaPhotos;
- if( count($propertyMediaPhotos) ){
- $path = (Storage::exists($propertyMediaPhotos[0]->path)) ? $propertyMediaPhotos[0]->path : default_thumbnail();
- }else{
- $path = default_thumbnail();
- }
- return $path;
- }
- public function getSecondaryPhotoAttribute()
- {
- $propertyMediaPhotos = $this->propertyMediaPhotos;
- if( count($propertyMediaPhotos) && !empty($propertyMediaPhotos[1]) ){
- $path = (Storage::exists($propertyMediaPhotos[1]->path)) ? $propertyMediaPhotos[1]->path : default_thumbnail();
- }else{
- $path = default_thumbnail();
- }
- return $path;
- }
- public function getThirdPhotoAttribute()
- {
- $propertyMediaPhotos = $this->propertyMediaPhotos;
- if( count($propertyMediaPhotos) && !empty($propertyMediaPhotos[2]) ){
- $path = (Storage::exists($propertyMediaPhotos[2]->path)) ? $propertyMediaPhotos[2]->path : default_thumbnail();
- }else{
- $path = default_thumbnail();
- }
- return $path;
- }
- public function getSecondaryPhotoFlagAttribute()
- {
- $propertyMediaPhotos = $this->propertyMediaPhotos;
- if( count($propertyMediaPhotos) && !empty($propertyMediaPhotos[1]) ){
- $flag = true;
- }else{
- $flag = false;
- }
- return $flag;
- }
- public function getThirdPhotoFlagAttribute()
- {
- $propertyMediaPhotos = $this->propertyMediaPhotos;
- if( count($propertyMediaPhotos) && !empty($propertyMediaPhotos[2]) ){
- $flag = true;
- }else{
- $flag = false;
- }
- return $flag;
- }
- public function getBedBathAreaAttribute()
- {
- $bedbatharea = [];
- if( !empty($this->beds) ){
- $bedbatharea[] = '<span>'.$this->beds.' Bed</span>';
- }
- if( !empty($this->baths) ){
- $bedbatharea[] = '<span>'.$this->baths.' Bath</span>';
- }
- if( !empty($this->internal_area) ){
- $bedbatharea[] = '<span>'.$this->internal_area.' '.$this->DisplayInternalUnit.'</span>';
- }
- $bedbathareaString = implode(' | ', $bedbatharea);
- return $bedbathareaString;
- }
- public function getBedBathAttribute()
- {
- $bedbatharea = [];
- if( !empty($this->beds) ){
- $bedbatharea[] = '<span>'.$this->beds.' Bed</span>';
- }
- if( !empty($this->baths) ){
- $bedbatharea[] = '<span>'.$this->baths.' Bath</span>';
- }
- $bedbathareaString = implode(' | ', $bedbatharea);
- return $bedbathareaString;
- }
- /**
- * Accessor:
- * The alt value for proeprty images
- *
- */
- public function getImageAltAttribute()
- {
- $property_type_txt = $this->PropertyTypeNamePlaceholder;
- $value = "$this->beds bed $property_type_txt For ".($this->is_rental ? 'Rent' : 'Sale')." in $this->city, $this->region";
- return $value;
- }
- public function locationList()
- {
- $locations_list = [];
- if(settings('overseas') == 1){
- $locations = ['street', 'town', 'city', 'complex_name', 'postcode'];
- }else{
- $locations = ['street', 'town', 'city', 'region', 'postcode'];
- }
- foreach( $locations as $location){ //FIELD TO SEARCH
- $properties = Property::where([['status', 0],[$location, '!=', '']])->distinct($location)->get();
- if(!empty($properties)){
- foreach( $properties as $property){
- if( !in_array($property->{$location}, $locations_list) ){
- $locations_list[] = $property->{$location};
- }
- }
- }
- }
- return $locations_list;
- }
- /**
- * Property Seach
- */
- public function searchWhere($criteria = [], $dashboard = FALSE, $strict_limit = FALSE)
- {
- $check_shortlist = Request::segment(1);
- $admin_allowed = $this->admin_allowed;
- $custom_sort = false;
- $criteria = array_filter(array_intersect_key($criteria, array_flip($admin_allowed)));
- $where = [];
- if($dashboard){
- if(empty($criteria['status'])){
- $where[] = ['status', '!=', 1]; // NOT ARCHIVE
- }
- if(Auth::user()->role == 'agent'){
- $where[] = ['user_id', Auth::user()->id];
- }
- }else{
- if(empty($criteria['status'])){
- $where[] = ['status', 0]; // Available
- }
- }
- foreach( $criteria as $key => $value ){
- switch ($key) {
- case 'in':break;
- case 'sevenDays':break;
- case 'oneDay':break;
- case 'popular':break;
- case 'sort':break;
- case 'exclude_id':break;
- // case 'type': // Front-end... should just be "for", huh.
- case 'for':
- if($value=='rent'){ $value=1; }
- if($value=='sale'){ $value=0; }
- $where[] = ['is_rental', $value];
- break;
- case 'is_featured':
- if($value==2){ $value=0; }
- $where[] = [$key, $value];
- break;
- case 'beds':
- case 'minbeds':
- $where[] = ['beds', '>=', $value];
- break;
- case 'price_range':
- $price_range_array = explode('-', $value);
- $where[] = ['price', '>=', $price_range_array[0]];
- $where[] = ['price', '<=', $price_range_array[1]];
- break;
- case 'status':
- if($value==2){ $value=0; }
- $where[] = [$key, $value];
- break;
- case 'ref':
- $where[] = [$key, 'LIKE', '%'.$value.'%'];
- break;
- default:
- $where[] = [$key, $value];
- break;
- }
- }
- if(!empty($criteria['sevenDays'])){
- $date_7days_before = date('Y-m-d H:i:s',strtotime('-7 days'));
- $date_now = date('Y-m-d H:i:s');
- $where[] = ['created_at', '>=', $date_7days_before];
- $where[] = ['created_at', '<=', $date_now];
- }
- if(!empty($criteria['oneDay'])){
- $date_24hrs_before = date('Y-m-d H:i:s',strtotime('-24 hours'));
- $date_now = date('Y-m-d H:i:s');
- $where[] = ['created_at', '>=', $date_24hrs_before];
- $where[] = ['created_at', '<=', $date_now];
- }
- $query = Property::where($where);
- /*--------------------------------------------
- * //Where can be placed above here...
- ----------------------------------------------*/
- if(!empty($criteria['in'])){
- $in = urldecode(trim($criteria['in']));
- $query->where(function($query) use ( $in ) {
- $query->orwhere([['street', 'LIKE', '%'.$in.'%']]);
- $query->orwhere([['town', 'LIKE', '%'.$in.'%']]);
- $query->orwhere([['city', 'LIKE', '%'.$in.'%']]);
- if(settings('overseas') == 1){
- $query->orwhere([['complex_name', 'LIKE', '%'.$in.'%']]);
- }else{
- $query->orwhere([['region', 'LIKE', '%'.$in.'%']]);
- }
- $query->orwhere([['postcode', 'LIKE', '%'.$in.'%']]);
- });
- }
- if(!empty($criteria['popular'])){
- $query->has('enquiries', '>=', 3);
- }
- if(!empty($criteria['exclude_id'])){
- $query->where('id', '!=', $criteria['exclude_id']);
- }
- if($check_shortlist == 'shortlist'){
- $query->whereHas('shortlists', function ($query) {
- $ip = Request::ip();
- $query->where('ip', $ip);
- });
- }
- if(!empty($criteria['sort'])){
- // Custom sorting
- switch ($criteria['sort']) {
- case 'most-recent':
- $query->orderby('created_at', 'desc');
- $custom_sort = true;
- break;
- case 'lowest-price':
- $query->orderby('price', 'asc');
- $custom_sort = true;
- break;
- case 'highest-price':
- $query->orderby('price', 'desc');
- $custom_sort = true;
- break;
- }
- }
- // Default sorting
- if (! $custom_sort) {
- $query->orderby('created_at', 'desc');
- }
- if ($strict_limit) {
- // I added this for "Similar Properties" where we only want three
- $properties = $query->take($strict_limit)->paginate($strict_limit);
- } else {
- $properties = $query->paginate(12);
- }
- return $properties;
- }
- function explode_array_delimiters( $delimiters, $string )
- {
- return explode( chr( 1 ), str_replace( $delimiters, chr( 1 ), $string ) );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement