Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 24.95 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App;
  4.  
  5. use Illuminate\Database\Eloquent\Model;
  6. use Illuminate\Support\Facades\Storage;
  7. use App\Enquiry;
  8. use App\Shortlist;
  9. use Auth;
  10. use Request;
  11.  
  12. class Property extends Model
  13. {
  14. protected $table = 'properties';
  15.  
  16. // Primary key
  17. public $primaryKey = 'id';
  18.  
  19. // Timestamps
  20. public $timestamps = true;
  21.  
  22. protected $admin_allowed = ['for', 'in', 'ref', 'is_featured', 'minbeds', 'property_type_id', 'price_range', 'status', 'sevenDays', 'oneDay', 'popular',
  23. 'beds', // Added this one for the front-end...
  24. 'sort', // And this one...
  25. 'exclude_id', // And this one... (Similar Properties)
  26. ];
  27.  
  28. //protected $fillable = ['is_rental'];
  29.  
  30. /**
  31. * Get the user record associated with the property media
  32. *
  33. */
  34. public function propertyMedia()
  35. {
  36. return $this->hasMany('App\PropertyMedia')->orderBy('sequence', 'ASC');
  37. }
  38.  
  39. /**
  40. * Get the user record associated with the property media photo
  41. *
  42. */
  43. public function propertyMediaPhotos()
  44. {
  45. return $this->hasMany('App\PropertyMedia')->where('type', 'photo')->orderBy('sequence', 'ASC');
  46. }
  47.  
  48. /**
  49. * Get the user record associated with the property media floorplan
  50. *
  51. */
  52. public function propertyMediaFloorplans()
  53. {
  54. return $this->hasMany('App\PropertyMedia')->where('type', 'floorplan')->orderBy('sequence', 'ASC');
  55. }
  56.  
  57. /**
  58. * Get the user record associated with the property media documents
  59. *
  60. */
  61. public function propertyMediaDocuments()
  62. {
  63. return $this->hasMany('App\PropertyMedia')->where('type', 'document')->orderBy('sequence', 'ASC');
  64. }
  65.  
  66. /**
  67. * Get the user record associated with the property
  68. *
  69. */
  70. public function user()
  71. {
  72. return $this->belongsTo('App\User');
  73. }
  74.  
  75. /**
  76. * Get the user record associated with the property
  77. *
  78. */
  79. public function users()
  80. {
  81. //Users::all();
  82. //return $this->hasMany('App\User');
  83. require $this->all('App\User');
  84. }
  85.  
  86. /**
  87. * Get the property type record associated with the property
  88. *
  89. */
  90. public function type()
  91. {
  92. return $this->belongsTo('App\PropertyType', 'property_type_id', 'id');
  93. }
  94.  
  95. /**
  96. * Get the Shortlist record associated with the property
  97. *
  98. */
  99. public function shortlists()
  100. {
  101. return $this->belongsTo('App\Shortlist', 'id', 'property_id');
  102. }
  103.  
  104. // filter by IP or User ID
  105. public function shortlist_ip()
  106. {
  107. $ip = Request::ip();
  108. if(Auth::user())
  109. {
  110. $user_id = Auth::user()->id;
  111. }
  112. else
  113. {
  114. $user_id = null;
  115. }
  116.  
  117. return $this->belongsTo('App\Shortlist', 'id', 'property_id')
  118. ->where('property_id', $this->id)
  119. ->where('user_id', $user_id)
  120. ->where('ip', $ip);
  121. }
  122.  
  123. // Check if shortlist exist by IP
  124. public function getCheckShortlistIpAttribute()
  125. {
  126. $check = !empty($this->shortlist_ip) ? TRUE : FALSE;
  127. return $check;
  128. }
  129.  
  130. /**
  131. * Get the property enquiries record associated with the property
  132. *
  133. */
  134. public function propertyEnquiries()
  135. {
  136. //return $this->hasMany('App\Enquiry', 'ref', 'ref')->where('archived_at', NULL)->orderBy('created_at', 'DESC');
  137. return $this->hasMany('App\Enquiry', 'ref', 'ref')->orderBy('created_at', 'DESC');
  138. }
  139.  
  140. public function enquiries()
  141. {
  142. return $this->belongsTo('App\Enquiry', 'ref', 'ref');
  143. }
  144.  
  145. /**
  146. * Fetch properties for display in the Admin's Properties page
  147. *
  148. */
  149. public static function getAllForSitemap()
  150. {
  151. $properties = Property::all();
  152.  
  153. return $properties;
  154. }
  155.  
  156. /**
  157. * Fetch properties for display in the Admin's Properties page
  158. *
  159. */
  160. public static function getProperties($paginate = 10)
  161. {
  162. $properties = Property::latest()->paginate($paginate);
  163.  
  164. return $properties;
  165. }
  166.  
  167. // query scope to order by the latest properties
  168. public function scopeLatest($query)
  169. {
  170. return $query->order_by('created_at', 'desc');
  171. }
  172.  
  173. // get property type or empty
  174. public function getPropertyTypeNameAttribute()
  175. {
  176. $typeName = '';
  177. if(!empty($this->property_type_id)){
  178. $typeName = (!empty($this->type->name)) ? $this->type->name : '';
  179. }
  180. return $typeName;
  181. }
  182.  
  183. // get property type or property
  184. public function getPropertyTypeNamePlaceholderAttribute()
  185. {
  186. $typeName = '';
  187. if(!empty($this->property_type_id)){
  188. $typeName = (!empty($this->type->name)) ? $this->type->name : 'Property';
  189. }
  190. return $typeName;
  191. }
  192.  
  193. // get default area unit
  194. public function getAreaUnitAttribute()
  195. {
  196. $area_unit = settings('area_unit');
  197. return !empty($area_unit) ? $area_unit : 'sq m';
  198. }
  199.  
  200. // get default area unit
  201. public function getDisplayLandUnitAttribute()
  202. {
  203. return (!empty($this->land_area_unit)) ? $this->land_area_unit : $this->getAreaUnitAttribute();
  204. }
  205.  
  206. // get default area unit
  207. public function getDisplayInternalUnitAttribute()
  208. {
  209. return (!empty($this->internal_area_unit)) ? $this->internal_area_unit : $this->getAreaUnitAttribute();
  210. }
  211.  
  212. /**
  213. * Accessor:
  214. * The full address of the property, likely used in the Admin
  215. *
  216. */
  217.  
  218. public function getDisplayInternalAreaAttribute()
  219. {
  220. //$value = $this->internal_area.'m<sup>2</sup>';
  221. $value = $this->internal_area.' '.$this->DisplayInternalUnit;
  222.  
  223. return $value;
  224. }
  225.  
  226.  
  227. /**
  228. * Accessor:
  229. * The headline for search results, Similar Properties, Featured Properties etc
  230. *
  231. */
  232. public function getSearchHeadlineAttribute()
  233. {
  234.  
  235. $typeName = $this->PropertyTypeName;
  236.  
  237. $value = sprintf('%s%s',
  238. !empty($this->beds) ? $this->beds.' Bed ' : '', $typeName
  239. );
  240.  
  241. $value = !empty($this->name) ? $this->name : $value;
  242.  
  243. return $value;
  244. }
  245.  
  246. /**
  247. * Accessor:
  248. * The full address of the property, likely used in the Admin Template 2
  249. *
  250. */
  251. public function getSearchHeadlineV2Attribute()
  252. {
  253.  
  254. if(settings('overseas') == 1){
  255. $address = '<span>'.implode(', ', array_filter([$this->complex_name, $this->town, $this->city])).'<span>';
  256. }else{
  257. $address= '<span>'.implode(', ', array_filter([$this->town, $this->city, $this->region])).'<span>';
  258. }
  259.  
  260. $value = sprintf('%s%s',
  261. !empty($this->beds) ? $this->beds.' Bed ' : '',
  262. $this->PropertyTypeName
  263. );
  264.  
  265. if( !empty($this->name) ){
  266. $value = $this->name;
  267. }
  268.  
  269. $value = $value.', '.$address;
  270.  
  271. return $value;
  272. }
  273.  
  274. /**
  275. * Accessor:
  276. * The headline for Property details pages
  277. *
  278. */
  279. public function getDetailsHeadlineAttribute()
  280. {
  281. // format: [NUMBER OF BEDS] [PROPERTY TYPE] [FOR SALE / TO RENT] in [REGION]
  282.  
  283. $items = [];
  284. if ($this->beds) $items[] = $this->beds.' Bed';
  285. $items[] = $this->PropertyTypeName;
  286. $items[] = ($this->is_rental) ? 'To Rent' : 'For Sale';
  287. if ($this->region) $items[] = 'in '.$this->region;
  288. $value = implode(' ', $items);
  289.  
  290. $value = !empty($this->name) ? $this->name : $value;
  291.  
  292. return $value;
  293. }
  294.  
  295. /**
  296. * Accessor:
  297. * The headline for Property details v2 pages
  298. *
  299. */
  300. public function getDetailsHeadlineV2Attribute()
  301. {
  302. // format: [NUMBER OF BEDS] [PROPERTY TYPE] [FOR SALE / TO RENT] in [REGION]
  303.  
  304. $items = [];
  305. if ($this->beds) $items[] = $this->beds.' Bed';
  306. $items[] = $this->PropertyTypeName;
  307. $items[] = ($this->is_rental) ? 'To Rent' : 'For Sale';
  308. $value = implode(' ', $items);
  309.  
  310. $value = !empty($this->name) ? $this->name : $value;
  311.  
  312. return $value;
  313. }
  314.  
  315. /**
  316. * Accessor:
  317. * The full address of the property, likely used in the Admin
  318. *
  319. */
  320. public function getDisplayAddressAttribute()
  321. {
  322. if(settings('overseas') == 1){
  323. $value = implode(', ', array_filter([$this->complex_name, $this->street, $this->town, $this->city, $this->country]));
  324. }else{
  325. $value = implode(', ', array_filter([$this->street, $this->town, $this->city, $this->region, $this->country]));
  326. }
  327.  
  328. return $value;
  329. }
  330.  
  331. //Featured and Similar Properties line one
  332. public function getFsAddress1Attribute()
  333. {
  334. //Town/Village - dark
  335. if(settings('overseas') == 1){
  336. $value = implode(', ', array_filter([$this->complex_name, $this->town]));
  337. }else{
  338. $value = $this->town;
  339. }
  340. return $value;
  341. }
  342.  
  343. //Featured and Similar Properties line two
  344. public function getFsAddress2Attribute()
  345. {
  346. if(settings('overseas') == 1){
  347. $value = $this->city;
  348. }else{
  349. $value = implode(', ', array_filter([$this->city, $this->region]));
  350. }
  351. return $value;
  352. }
  353.  
  354. //Display generic address...
  355. public function getDisplayPropertyAddressAttribute()
  356. {
  357. if(settings('overseas') == 1){
  358. $value = implode(', ', array_filter([$this->complex_name, $this->town, $this->city]));
  359. }else{
  360. $value = implode(', ', array_filter([$this->town, $this->city, $this->region]));
  361. }
  362. return $value;
  363. }
  364.  
  365. //Generic page latest properties
  366. public function getGenericPropertyAddressAttribute()
  367. {
  368. if(settings('overseas') == 1){
  369. $value = implode(', ', array_filter([$this->complex_name, $this->town, $this->city]));
  370. }else{
  371. $value = implode(', ', array_filter([$this->town, $this->city]));
  372. }
  373. return $value;
  374. }
  375.  
  376.  
  377. /**
  378. * Accessor:
  379. * Formatted date of the property creation date, used in the Admin
  380. *
  381. */
  382. public function getDisplayDateAttribute()
  383. {
  384. $value = admin_date($this->created_at);
  385.  
  386. return $value;
  387. }
  388.  
  389. /**
  390. * Accessor:
  391. * Display summary
  392. *
  393. */
  394. public function getSummaryAttribute()
  395. {
  396. $value = str_limit(strip_tags($this->description), 250, '...');
  397. return $value;
  398. }
  399.  
  400. /**
  401. * Accessor:
  402. * Display excerpt
  403. *
  404. */
  405. public function getExcerptAttribute()
  406. {
  407. $value = str_limit(strip_tags($this->description), 150, '...');
  408. return $value;
  409. }
  410.  
  411.  
  412. /**
  413. * Accessor:
  414. * Display summary
  415. *
  416. */
  417. public function getSummaryDemo3Attribute()
  418. {
  419. $value = str_limit(strip_tags($this->description), 350, '...');
  420. return $value;
  421. }
  422.  
  423.  
  424. /**
  425. * Accessor:
  426. * Formatted display price of the property, including currency
  427. *
  428. */
  429. public function getDisplayPriceAttribute()
  430. {
  431. if (empty($this->price)) {
  432. return 'POA';
  433. }
  434.  
  435. $rent_freq = false;
  436. if (!empty($this->price) && !empty($this->is_rental)) {
  437. switch ($this->rent_period) {
  438. case 1:
  439. $rent_freq = '/<abbr title="Daily">day<abbr>';
  440. break;
  441. case 2:
  442. $rent_freq = '/<abbr title="Weekly">wk.<abbr>';
  443. break;
  444. default:
  445. $rent_freq = '/<abbr title="Monthly">mth.<abbr>';
  446. break;
  447. }
  448. }
  449.  
  450. $currency_symbol=settings('currency_symbol');
  451. $currency_symbol = (!empty($currency_symbol)) ? $currency_symbol : '&pound;';
  452.  
  453. $value = sprintf('%s%s%s',
  454. settings('currency_symbol'),
  455. number_format($this->price),
  456. $rent_freq
  457. );
  458.  
  459. return $value;
  460. }
  461.  
  462.  
  463. /**
  464. * Accessor:
  465. * The full Addition info of the property, likely used in the Admin
  466. *
  467. */
  468.  
  469. public function getDisplayAddInfoAttribute()
  470. {
  471. $html = '';
  472. $value = $this->add_info;
  473. if(!empty($value))
  474. {
  475. $delimiters = array(',', ';', '.');
  476.  
  477. $array = $this->explode_array_delimiters($delimiters, $value);
  478.  
  479. if(count($array)){
  480. $html .= '<div class="add-info-container"><div class="row">';
  481. foreach ($array as $value) {
  482. $html .= '<div class="col-md-3 col-sm-3"><div class="item-ai">';
  483. $html .= $value;
  484. $html .= '</div></div>';
  485. }
  486. $html .= '</div></div>';
  487. }
  488. }
  489.  
  490. return $html;
  491. }
  492.  
  493. public function getDisplayAddInfoDemo3Attribute()
  494. {
  495. $html = '';
  496. $value = $this->add_info;
  497. $ctr = 0;
  498. if(!empty($value)){
  499. $delimiters = array(',', ';', '.');
  500.  
  501. $array = $this->explode_array_delimiters($delimiters, $value);
  502.  
  503. if(count($array))
  504. {
  505. $html .= '<div class="property-specs"><div class="row ps-item">';
  506. foreach ($array as $value) {
  507. $ctr++;
  508. if( $ctr <= 6){
  509.  
  510. if($ctr%2 == 1){
  511. $html .= '<div class="col-6"><div class="style-1"><i class="fas fa-circle"></i>';
  512. $html .= $value;
  513. $html .= '</div></div>';
  514. }else{
  515. $html .= '<div class="col-6"><div class="style-2">';
  516. $html .= $value;
  517. $html .= '</div></div>';
  518. }
  519.  
  520. }
  521. }
  522. $html .= '</div></div>';
  523. }
  524. }
  525.  
  526. return $html;
  527. }
  528.  
  529. public function getModeDisplayAttribute()
  530. {
  531. $mode = ($this->is_rental) ? 'For Rent' : 'For Sale';
  532. return $mode;
  533. }
  534.  
  535. /**
  536. * Accessor:
  537. * Get the URL for the property's details page
  538. *
  539. */
  540. public function getUrlAttribute()
  541. {
  542. // Format: /property/[NUMBER OF BEDS]-bed-[PROPERTY TYPE]-[FOR-SALE / TO-RENT]-in-[CITY]/[PROPERTY ID]
  543. $items = [];
  544. if ($this->beds) $items[] = $this->beds.' bed';
  545. $items[] = $this->PropertyTypeName;
  546. $items[] = ($this->is_rental) ? 'to rent' : 'for sale';
  547. if ($this->city) $items[] = 'in '.$this->city;
  548. $value = implode(' ', $items);
  549. $slug = str_slug($value);
  550.  
  551. $value = sprintf('%s/%s/%s',
  552. 'property', // property route
  553. $slug, // SEO-friendly
  554. $this->{$this->primaryKey} // property identifier
  555. );
  556.  
  557. return url($value);
  558. }
  559.  
  560. /**
  561. * Accessor:
  562. * Get the status for display
  563. *
  564. */
  565. public function getStateDisplayAttribute()
  566. {
  567. $state_display = 'Inactive';
  568. $status = $this->status;
  569. $states_array = p_states();
  570. if( $status != '' || $status == '0' ){
  571. $state_display = $states_array[$status];
  572. }
  573. if($status == 1)
  574. $state_display = '';
  575.  
  576. return $state_display;
  577. }
  578.  
  579. /**
  580. * Accessor:
  581. * Get the Primary Photo
  582. *
  583. */
  584. public function getPrimaryPhotoAttribute()
  585. {
  586. $propertyMediaPhotos = $this->propertyMediaPhotos;
  587. if( count($propertyMediaPhotos) ){
  588. $path = (Storage::exists($propertyMediaPhotos[0]->path)) ? $propertyMediaPhotos[0]->path : default_thumbnail();
  589. }else{
  590. $path = default_thumbnail();
  591. }
  592. return $path;
  593. }
  594.  
  595. public function getSecondaryPhotoAttribute()
  596. {
  597. $propertyMediaPhotos = $this->propertyMediaPhotos;
  598. if( count($propertyMediaPhotos) && !empty($propertyMediaPhotos[1]) ){
  599. $path = (Storage::exists($propertyMediaPhotos[1]->path)) ? $propertyMediaPhotos[1]->path : default_thumbnail();
  600. }else{
  601. $path = default_thumbnail();
  602. }
  603. return $path;
  604. }
  605.  
  606. public function getThirdPhotoAttribute()
  607. {
  608. $propertyMediaPhotos = $this->propertyMediaPhotos;
  609. if( count($propertyMediaPhotos) && !empty($propertyMediaPhotos[2]) ){
  610. $path = (Storage::exists($propertyMediaPhotos[2]->path)) ? $propertyMediaPhotos[2]->path : default_thumbnail();
  611. }else{
  612. $path = default_thumbnail();
  613. }
  614. return $path;
  615. }
  616.  
  617. public function getSecondaryPhotoFlagAttribute()
  618. {
  619. $propertyMediaPhotos = $this->propertyMediaPhotos;
  620. if( count($propertyMediaPhotos) && !empty($propertyMediaPhotos[1]) ){
  621. $flag = true;
  622. }else{
  623. $flag = false;
  624. }
  625. return $flag;
  626. }
  627.  
  628. public function getThirdPhotoFlagAttribute()
  629. {
  630. $propertyMediaPhotos = $this->propertyMediaPhotos;
  631. if( count($propertyMediaPhotos) && !empty($propertyMediaPhotos[2]) ){
  632. $flag = true;
  633. }else{
  634. $flag = false;
  635. }
  636. return $flag;
  637. }
  638.  
  639. public function getBedBathAreaAttribute()
  640. {
  641. $bedbatharea = [];
  642.  
  643. if( !empty($this->beds) ){
  644. $bedbatharea[] = '<span>'.$this->beds.' Bed</span>';
  645. }
  646. if( !empty($this->baths) ){
  647. $bedbatharea[] = '<span>'.$this->baths.' Bath</span>';
  648. }
  649. if( !empty($this->internal_area) ){
  650. $bedbatharea[] = '<span>'.$this->internal_area.' '.$this->DisplayInternalUnit.'</span>';
  651. }
  652.  
  653. $bedbathareaString = implode(' | ', $bedbatharea);
  654.  
  655. return $bedbathareaString;
  656. }
  657.  
  658. public function getBedBathAttribute()
  659. {
  660. $bedbatharea = [];
  661.  
  662. if( !empty($this->beds) ){
  663. $bedbatharea[] = '<span>'.$this->beds.' Bed</span>';
  664. }
  665. if( !empty($this->baths) ){
  666. $bedbatharea[] = '<span>'.$this->baths.' Bath</span>';
  667. }
  668.  
  669. $bedbathareaString = implode(' | ', $bedbatharea);
  670.  
  671. return $bedbathareaString;
  672. }
  673.  
  674. /**
  675. * Accessor:
  676. * The alt value for proeprty images
  677. *
  678. */
  679. public function getImageAltAttribute()
  680. {
  681. $property_type_txt = $this->PropertyTypeNamePlaceholder;
  682. $value = "$this->beds bed $property_type_txt For ".($this->is_rental ? 'Rent' : 'Sale')." in $this->city, $this->region";
  683.  
  684. return $value;
  685. }
  686.  
  687. public function locationList()
  688. {
  689. $locations_list = [];
  690.  
  691. if(settings('overseas') == 1){
  692. $locations = ['street', 'town', 'city', 'complex_name', 'postcode'];
  693. }else{
  694. $locations = ['street', 'town', 'city', 'region', 'postcode'];
  695. }
  696.  
  697. foreach( $locations as $location){ //FIELD TO SEARCH
  698. $properties = Property::where([['status', 0],[$location, '!=', '']])->distinct($location)->get();
  699. if(!empty($properties)){
  700.  
  701. foreach( $properties as $property){
  702. if( !in_array($property->{$location}, $locations_list) ){
  703. $locations_list[] = $property->{$location};
  704. }
  705. }
  706. }
  707. }
  708.  
  709. return $locations_list;
  710. }
  711.  
  712.  
  713. /**
  714. * Property Seach
  715. */
  716.  
  717. public function searchWhere($criteria = [], $dashboard = FALSE, $strict_limit = FALSE)
  718. {
  719.  
  720. $check_shortlist = Request::segment(1);
  721.  
  722. $admin_allowed = $this->admin_allowed;
  723. $custom_sort = false;
  724. $criteria = array_filter(array_intersect_key($criteria, array_flip($admin_allowed)));
  725.  
  726. $where = [];
  727.  
  728. if($dashboard){
  729.  
  730. if(empty($criteria['status'])){
  731. $where[] = ['status', '!=', 1]; // NOT ARCHIVE
  732. }
  733.  
  734. if(Auth::user()->role == 'agent'){
  735. $where[] = ['user_id', Auth::user()->id];
  736. }
  737.  
  738. }else{
  739.  
  740. if(empty($criteria['status'])){
  741. $where[] = ['status', 0]; // Available
  742. }
  743.  
  744. }
  745.  
  746. foreach( $criteria as $key => $value ){
  747.  
  748. switch ($key) {
  749. case 'in':break;
  750. case 'sevenDays':break;
  751. case 'oneDay':break;
  752. case 'popular':break;
  753. case 'sort':break;
  754. case 'exclude_id':break;
  755.  
  756. // case 'type': // Front-end... should just be "for", huh.
  757. case 'for':
  758. if($value=='rent'){ $value=1; }
  759. if($value=='sale'){ $value=0; }
  760. $where[] = ['is_rental', $value];
  761. break;
  762.  
  763. case 'is_featured':
  764. if($value==2){ $value=0; }
  765. $where[] = [$key, $value];
  766. break;
  767. case 'beds':
  768. case 'minbeds':
  769. $where[] = ['beds', '>=', $value];
  770. break;
  771. case 'price_range':
  772. $price_range_array = explode('-', $value);
  773. $where[] = ['price', '>=', $price_range_array[0]];
  774. $where[] = ['price', '<=', $price_range_array[1]];
  775. break;
  776. case 'status':
  777. if($value==2){ $value=0; }
  778. $where[] = [$key, $value];
  779. break;
  780. case 'ref':
  781. $where[] = [$key, 'LIKE', '%'.$value.'%'];
  782. break;
  783. default:
  784. $where[] = [$key, $value];
  785. break;
  786. }
  787. }
  788.  
  789. if(!empty($criteria['sevenDays'])){
  790. $date_7days_before = date('Y-m-d H:i:s',strtotime('-7 days'));
  791. $date_now = date('Y-m-d H:i:s');
  792. $where[] = ['created_at', '>=', $date_7days_before];
  793. $where[] = ['created_at', '<=', $date_now];
  794. }
  795.  
  796. if(!empty($criteria['oneDay'])){
  797. $date_24hrs_before = date('Y-m-d H:i:s',strtotime('-24 hours'));
  798. $date_now = date('Y-m-d H:i:s');
  799. $where[] = ['created_at', '>=', $date_24hrs_before];
  800. $where[] = ['created_at', '<=', $date_now];
  801. }
  802.  
  803. $query = Property::where($where);
  804. /*--------------------------------------------
  805. * //Where can be placed above here...
  806. ----------------------------------------------*/
  807.  
  808. if(!empty($criteria['in'])){
  809. $in = urldecode(trim($criteria['in']));
  810. $query->where(function($query) use ( $in ) {
  811. $query->orwhere([['street', 'LIKE', '%'.$in.'%']]);
  812. $query->orwhere([['town', 'LIKE', '%'.$in.'%']]);
  813. $query->orwhere([['city', 'LIKE', '%'.$in.'%']]);
  814. if(settings('overseas') == 1){
  815. $query->orwhere([['complex_name', 'LIKE', '%'.$in.'%']]);
  816. }else{
  817. $query->orwhere([['region', 'LIKE', '%'.$in.'%']]);
  818. }
  819. $query->orwhere([['postcode', 'LIKE', '%'.$in.'%']]);
  820. });
  821. }
  822.  
  823. if(!empty($criteria['popular'])){
  824. $query->has('enquiries', '>=', 3);
  825. }
  826.  
  827. if(!empty($criteria['exclude_id'])){
  828. $query->where('id', '!=', $criteria['exclude_id']);
  829. }
  830.  
  831. if($check_shortlist == 'shortlist'){
  832. $query->whereHas('shortlists', function ($query) {
  833. $ip = Request::ip();
  834. $query->where('ip', $ip);
  835. });
  836. }
  837.  
  838. if(!empty($criteria['sort'])){
  839. // Custom sorting
  840. switch ($criteria['sort']) {
  841. case 'most-recent':
  842. $query->orderby('created_at', 'desc');
  843. $custom_sort = true;
  844. break;
  845. case 'lowest-price':
  846. $query->orderby('price', 'asc');
  847. $custom_sort = true;
  848. break;
  849. case 'highest-price':
  850. $query->orderby('price', 'desc');
  851. $custom_sort = true;
  852. break;
  853. }
  854. }
  855.  
  856. // Default sorting
  857. if (! $custom_sort) {
  858. $query->orderby('created_at', 'desc');
  859. }
  860.  
  861. if ($strict_limit) {
  862. // I added this for "Similar Properties" where we only want three
  863. $properties = $query->take($strict_limit)->paginate($strict_limit);
  864. } else {
  865. $properties = $query->paginate(12);
  866. }
  867.  
  868. return $properties;
  869. }
  870.  
  871. function explode_array_delimiters( $delimiters, $string )
  872. {
  873. return explode( chr( 1 ), str_replace( $delimiters, chr( 1 ), $string ) );
  874. }
  875.  
  876. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement