Source of file ListingsPage.php
Size: 9,716 Bytes - Last Modified: 2021-12-23T10:51:36+00:00
/var/www/docs.ssmods.com/process/src/code/Pages/ListingsPage.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413 | <?php /** * * @package Realestate Listing System - Property Listings Page * @requires DataObjectAsPage, Mappable * @author Richard Rudy twitter:@thezenmonkey web: http://designplusawesome.com * TODO Remove DataObjectAsPage Dependency */ class ListingsPage extends DataObjectAsPageHolder { /** * Static vars * ----------------------------------*/ private static $singular_name = 'Listings'; private static $plural_name = 'Listing Pages'; private static $description = 'Displays Listing (if under main Listings page it can be configured to show only one City or Neighbouthod)'; private static $icon = 'realestate/images/listings.png'; //Attribute as Martha Ormiston, from The Noun Project private static $db = array( "City" => "Varchar", ); private static $allowed_children = array("Listing", "ListingsPage"); /** * Object vars * ----------------------------------*/ /** * Static methods * ----------------------------------*/ /** * Data model * ----------------------------------*/ /** * Get All Avaibalbe Listings (Status = Available) * * @return DataList */ public function AvailableListings() { $listings = Listing::get()->filter(array("Status" => "Available"))->sort(array("Feature" => "DESC", "ID" => "DESC")); return $listings->count() ? $listings : false; } /** * Get All Listings Marked as Sold but Not Closed * * @return DatList */ public function SoldListings() { $listings = Listing::get()->filter(array("Status" => "Sold"))->sort(array("ID" => "DESC")); return $listings->count() ? $listings : false; } /** * Get All Listings Marked as Closed * * @return DatList */ public function ClosedListings() { $listings = Listing::get()->filter(array("Status" => "Closed"))->sort(array("ID" => "DESC")); return $listings->count() ? $listings : false; } /** * Get All Listings Available AND Sold * * @return DataList */ public function AllListings() { $listings = Listing::get()->filter(array("Status" => array("Available","Sold")))->sort(array("Status" => "ASC", "ID" => "DESC")); return $listings->count() ? $listings : false; } /** * Retun ALLlistings in the system regardless of Status * * @return DataList */ public function MasterListings() { $listings = Listing::get(); return $listings->count() ? $listings : false; } /** * Retun MLS Listings * * @param $count number of listings to return * @return DataList */ public function MLSListings($count = 10) { $listings = MLSListing::get()->limit($count); return $listings->count() ? $listings : false; } /** * Retun Only Featured MLS Listings * * @param $count number of listings to return * @return DataList */ public function FeaturedMLSListings($count = null) { if($count) { $listings = MLSListing::get()->filter(array("IsFeatured" => 1))->limit($count); } else { $listings = MLSListing::get()->filter(array("IsFeatured" => 1)); } return $listings->count() ? $listings : false; } /** * Return Listings without a City * * @param $sold use 1 to include Sold Listings * @param $count number of listings to return * @return DataList */ public function TownListings($count = null, $sold = null) { $sqlQuery = new SQLQuery(); $sqlQuery->setFrom('Listing'); $sqlQuery->setWhere('CityID = 0'); if($sold == "1") { $sqlQuery->addWhere("Status IN ('Available', 'Sold')"); } else { $sqlQuery->addWhere("Status = 'Available'"); } $sqlQuery->selectField('Town'); $sqlQuery->setDistinct(true); $result = $sqlQuery->execute(); $townList = array(); foreach($result as $row){ array_push($townList, $row['Town']); } $filterList = (array_filter($townList)); if($filterList) { $listings = Listing::get()->filter(array("Town" => $filterList)); return $listings->count() ? ($count) ? $listings->limit($count) : $listings : false; } else { return false; } } /** * Common methods * ----------------------------------*/ function getCMSFields() { $fields = parent::getCMSFields(); $fields->removeByName('City'); $cityField = new DropdownField('City', 'City', MunicipalityPage::get()->map('ID', 'Title')); $cityField->setEmptyString('(Select one)'); if($this->ParentID != 0) { $fields->insertBefore($cityField, 'Content'); } return $fields; } function onBeforeWrite() { parent::onBeforeWrite(); if($this->ParentID == 0){ $this->City = "All"; $this->ItemsPerPage = 40; } } /** * Accessor methods * ----------------------------------*/ /** * Controller actions * ----------------------------------*/ function requireDefaultRecords() { if(!SiteTree::get_by_link("listings")){ $listingPage = new ListingsPage(); $listingPage->Title = "Listings"; $listingPage->URLSegment = "listings"; $listingPage->Sort = 1; $listingPage->write(); $listingPage->publish('Stage', 'Live'); $listingPage->flushCache(); DB::alteration_message('Listings page created', 'created'); } parent::requireDefaultRecords(); } /** * Template accessors * ----------------------------------*/ /* public function GetMLSNumber($number) { if($number == "all") { return MLSListing::get()->sort("Municipality"); } else { $number = (int) $number; $set = new ArrayList(); foreach(MLSListing::get()->where("IsFeatured = 1") as $obj) $set->push($obj); if($set->count() && $set->count() <= $number) { $limit = $number - $set->count(); foreach(MLSListing::get()->where("IsFeatured = 0")->limit($limit) as $obj) $set->push($obj); } elseif (!$set->count()){ foreach(MLSListing::get()->where("IsFeatured = 0")->limit($number) as $obj) $set->push($obj); } return $set; } } */ public function GetCities() { return MunicipalityPage::get(); } public function GetCity() { return $this->City ? $this->City : false; } /* public function ThisCity($City) { return MunicipalityPage::get()->byID($City); } */ //Client Specific public function OverAMillion() { $set = new ArrayList(); foreach(Listing::get()->filter(array("Sold" => '0', "Price:GreaterThan" => "1000000"))->sort(array("Feature"=>"DESC")) as $obj) $set->push($obj); foreach(Listing::get()->filter(array("Sold" => '1', "Price:GreaterThan" => "1000000"))->sort("LastEdited", "DESC")->limit(4) as $obj) $set->push($obj); //foreach(Listing::get()->filter(array("Sold" => '0', "Price:GreaterThan" => "1000000"))->where("Sold = 0")->sort(array("Feature"=>"DESC","SaleOrRent"=>"ASC")) as $obj) $set->push($obj); return $set->count() ? $set : false; } //Client Specific public function UnderAMillion() { $set = new ArrayList(); foreach(Listing::get()->filter(array("Sold" => '0', "Price:LessThan" => "1000000"))->sort(array("Feature"=>"DESC")) as $obj) $set->push($obj); foreach(Listing::get()->filter(array("Sold" => '1', "Price:LessThan" => "1000000"))->sort("LastEdited", "DESC")->limit(4) as $obj) $set->push($obj); //foreach(Listing::get()->filter(array("Sold" => '0', "Price:GreaterThan" => "1000000"))->where("Sold = 0")->sort(array("Feature"=>"DESC","SaleOrRent"=>"ASC")) as $obj) $set->push($obj); return $set->count() ? $set : false; } public function getShowCities() { return $this->config()->ShowCities; } /** * Object methods * ----------------------------------*/ } class ListingsPage_Controller extends DataObjectAsPageHolder_Controller { //This needs to know be the Class of the DataObject you want this page to list static $item_class = 'MLSListing'; //Set the sort for the items (defaults to Created DESC) static $item_sort = 'Created DESC'; public static $allowed_actions = array("ContactForm", "show"); public function show() { if($item = $this->getCurrentItem()) { if ($item->canView()) { $data = array( 'Item' => $item, 'Breadcrumbs' => $item->Breadcrumbs(), 'MetaTags' => $item->MetaTags(), 'BackLink' => base64_decode($this->request->getVar('backlink')) ); return $this->customise(new ArrayData($data)); } else { return Security::permissionFailure($this); } } else { //return $this->httpError(404); $redirect = SiteTree::get_by_link("listing-unavailable"); $this->redirect($redirect->Link(), 301); return; } } public function getAddress() { if($item = $this->getCurrentItem()) { return $item->Address; } else { return false; } } public function getMLS() { if($item = $this->getCurrentItem()) { return $item->MLS; } else { return false; } } function showgallery() { if(($item = $this->getCurrentItem())) { if ($this->getCurrentItem()->canView()) { $data = array( 'Item' => $item, 'Breadcrumbs' => $this->ItemBreadcrumbs($item), 'MetaTitle' => $item->MetaTitle, 'MetaTags' => $this->ItemMetaTags($item), 'BackLink' => base64_decode($this->request->getVar('backlink')) ); if($this->isAjax) { $this->customise(new ArrayData($data)); return $this->renderWith(array('AjaxGallery')); } else { return $this->customise(new ArrayData($data)); } } else { return Security::permissionFailure($this); } } else { return $this->httpError(404); } } public function ContactForm() { $form = new ListingRequestForm($this, 'ContactForm'); if($form->hasExtension('FormSpamProtectionExtension')) { $form->enableSpamProtection(); } return $form; } } |