Source of file ListingCollection.php
Size: 2,648 Bytes - Last Modified: 2021-12-24T05:18:05+00:00
/var/www/docs.ssmods.com/process/src/src/model/listings/ListingCollection.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 | <?php /* use SilverStripe\ORM\DataObject; */ class ListingCollection extends DataObject { /** * Human-readable singular name. * * @var string * @config */ private static $singular_name = "Collection"; /** * Human-readable plural name * * @var string * @config */ private static $plural_name = "Collections"; private static $defaut_sort = 'SortOrder'; private static $db = array( 'Title' => 'Varchar(255)', 'SortOrder' => 'Int', 'URLSegment' => 'Varchar(255)', ); private static $has_one = array( 'Page' => 'Page', ); public function getCMSFields() { $f = parent::getCMSFields(); $f->removeByName( 'SortOrder' ); return $f; } public function Listings() { $iListIds = DB::query( sprintf( "SELECT ProductID FROM Product_Collections WHERE ListingCollectionID = %d", $this->ID ) ); $oListings = Product::get()->filter( [ "ID" => $iListIds->column(), ] ); return $oListings; } public function ListingCounter() { $oListings = $this->Listings(); return $oListings ? $oListings->count() : false; } public function Link() { return Controller::join_links( CollectionPage::find_link(), $this->getItemURLSegment(), '/' ); } public function onBeforeWrite() { parent::onBeforeWrite(); if ( $this->isChanged( $this->Title ) ) { $this->GenerateURLSegment(); } } /** * @return string */ private function GenerateURLSegment() { $siteTree = Page::create(); if ( $this->Title ) { $this->Title = trim( $this->Title ); $this->URLSegment = $siteTree->GenerateURLSegment( $this->Title ); $object = DataObject::get_one( $this->ClassName, "URLSegment='" . $this->URLSegment . "' AND ID !=" . $this->ID ); if ( $object ) { $this->URLSegment = $this->URLSegment . '-' . $this->ID; } } else { $this->URLSegment = $siteTree->GenerateURLSegment( $this->ClassName . '-' . $this->ID ); } $this->write(); return $this->URLSegment; } /** * @return mixed|string */ private function getItemURLSegment() { if ( $this->URLSegment ) { return $this->URLSegment; } return $this->GenerateURLSegment(); } } |