Source of file SecondHandProductGroup.php
Size: 4,133 Bytes - Last Modified: 2021-12-23T10:41:14+00:00
/var/www/docs.ssmods.com/process/src/src/SecondHandProductGroup.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 | <?php namespace Sunnysideup\EcommerceSecondHandProduct; use SilverStripe\CMS\Model\SiteTree; use SilverStripe\Forms\ReadonlyField; use SilverStripe\ORM\DataObject; use Sunnysideup\Ecommerce\Pages\ProductGroup; class SecondHandProductGroup extends ProductGroup { /** * @var string */ protected static $groups_to_show_first = ProductGroup::class; protected static $main_second_hand_page_cache; protected static $list_of_filters = []; protected static $_page_cache_ids = []; private static $table_name = 'SecondHandProductGroup'; private static $db = [ 'RootParent' => 'Boolean', ]; private static $allowed_children = [ SecondHandProductGroup::class, SecondHandProduct::class, ]; private static $icon = 'sunnysideup/ecommerce_second_hand_product: client/images/treeicons/SecondHandProductGroup-file.gif'; /** * Standard SS variable. */ private static $singular_name = 'Second Hand Product Holder'; /** * Standard SS variable. */ private static $plural_name = 'Second Hand Product Holders'; /** * Standard SS variable. * * @var string */ private static $description = 'A product category page specifically for second had products'; private static $maximum_number_of_products_to_list = 999; public static function main_second_hand_page() { if (! isset(self::$main_second_hand_page_cache)) { self::$main_second_hand_page_cache = SecondHandProductGroup::get()->first()->TopParentGroup(); if (! self::$main_second_hand_page_cache) { self::$main_second_hand_page_cache = Injector::inst()->get(SecondHandProductGroup::class)->BestRootParentPage(); } } return self::$main_second_hand_page_cache; } public static function main_second_hand_page_id(): int { $page = self::main_second_hand_page(); return $page ? $page->ID : 0; } public function i18n_singular_name() { return self::$singular_name; } public function i18n_plural_name() { return self::$plural_name; } public function getCMSFields() { $fields = parent::getCMSFields(); $fields->addFieldToTab( 'Root.SecondHand', ReadonlyField::create( 'RootParentNice', _t('SecondHandProductGroup.LANDING_PAGE', 'Landing Page') )->setValue($this->dbObject('RootParent')->Nice()) ); return $fields; } /** * Event handler called before writing to the database. */ public function onBeforeWrite() { parent::onBeforeWrite(); $this->RootParent = false; if ($this->ParentID) { $parent = SiteTree::get_by_id($this->ParentID); if ($parent) { if ($parent instanceof SecondHandProductGroup) { $this->RootParent = false; } else { if (! $this->hasOtherSecondHandProductGroupsOnThisLevel()) { $this->RootParent = true; } } } } else { if (! $this->hasOtherSecondHandProductGroupsOnThisLevel()) { $this->RootParent = true; } } } /** * @return SecondHandProductGroup */ public function BestRootParentPage() { $obj = DataObject::get_one( SecondHandProductGroup::class, ['RootParent' => 1] ); if ($obj) { return $obj; } return SecondHandProductGroup::get()->first(); } /** * Returns the class we are working with. */ public function getBuyableClassName(): string { return SecondHandProduct::class; } protected function hasOtherSecondHandProductGroupsOnThisLevel(): bool { return SecondHandProductGroup::get() ->filter(['ParentID' => $this->ParentID ?: 0]) ->exclude(['ID' => $this->ID]) ->exists() ; } } |