Source of file ProductGroupHolder.php
Size: 12,410 Bytes - Last Modified: 2021-12-23T10:21:36+00:00
/var/www/docs.ssmods.com/process/src/src/Model/Pages/ProductGroupHolder.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336 | <?php namespace SilverCart\Model\Pages; use SilverCart\Dev\SeoTools; use SilverCart\Dev\Tools; use SilverCart\Forms\FormFields\FieldGroup; use SilverCart\Model\Pages\ProductGroupPage; use SilverCart\View\GroupView\GroupViewHandler; use SilverStripe\CMS\Model\RedirectorPage; use SilverStripe\CMS\Model\SiteTree; use SilverStripe\Control\Controller; use SilverStripe\Forms\CheckboxField; use SilverStripe\Forms\DropdownField; use SilverStripe\Forms\TextField; use SilverStripe\Forms\ToggleCompositeField; use SilverStripe\Forms\TreeDropdownField; use SilverStripe\i18n\i18n; use SilverStripe\ORM\Map; /** * Page to display a group of products. * * @package SilverCart * @subpackage Model_Pages * @author Sebastian Diel <sdiel@pixeltricks.de> * @since 28.09.2017 * @copyright 2017 pixeltricks GmbH * @license see license file in modules root directory */ class ProductGroupHolder extends \Page { use \SilverCart\ORM\ExtensibleDataObject; /** * Attributes. * * @var array */ private static $db = [ 'productGroupsPerPage' => 'Int', 'DefaultGroupHolderView' => 'Varchar(255)', 'UseOnlyDefaultGroupHolderView' => 'Enum("no,yes,inherit","inherit")', 'DefaultGroupView' => 'Varchar(255)', 'UseOnlyDefaultGroupView' => 'Enum("no,yes,inherit","inherit")', 'RedirectToProductGroup' => 'Boolean(0)', ]; /** * Has one relations. * * @var array */ private static $has_one = [ 'LinkTo' => SiteTree::class, ]; /** * DB table name * * @var string */ private static $table_name = 'SilvercartProductGroupHolder'; /** * Allowed children in site tree * * @var array */ private static $allowed_children = [ ProductGroupPage::class, RedirectorPage::class, ]; /** * Icon to use in SiteTree * * @var string */ private static $icon = "silvercart/silvercart:client/img/page_icons/product_group_holder-file.gif"; /** * Indicator to check whether getCMSFields is called * * @var boolean */ protected $getCMSFieldsIsCalled = false; /** * Cache key parts for this product group * * @var array */ protected $cacheKeyParts = null; /** * Cache key for this product group * * @var string */ protected $cacheKey = null; /** * Singular name for this object * * @return string */ public function singular_name() { return Tools::singular_name_for($this); } /** * Plural name for this object * * @return string */ public function plural_name() { return Tools::plural_name_for($this); } /** * Field labels for display in tables. * * @param boolean $includerelations A boolean value to indicate if the labels returned include relation fields * * @return array * * @author Sebastian Diel <sdiel@pixeltricks.de> * @since 06.06.2012 */ public function fieldLabels($includerelations = true) { $this->beforeUpdateFieldLabels(function(&$labels) { $labels = array_merge( $labels, array( 'productGroupsPerPage' => _t(ProductGroupPage::class . '.PRODUCTGROUPSPERPAGE', 'Product groups per page'), 'ProductsPerPageHint' => _t(ProductGroupPage::class . '.PRODUCTSPERPAGEHINT', 'Set products or product groups per page to 0 (zero) to use the default setting.'), 'DefaultGroupHolderView' => _t(ProductGroupPage::class . '.DEFAULTGROUPHOLDERVIEW', 'Default product group view'), 'UseOnlyDefaultGroupHolderView' => _t(ProductGroupPage::class . '.USEONLYDEFAULTGROUPHOLDERVIEW', 'Allow only default view'), 'DefaultGroupView' => _t(ProductGroupPage::class . '.DEFAULTGROUPVIEW', 'Default product view'), 'DefaultGroupViewInherit' => _t(ProductGroupPage::class . '.DEFAULTGROUPVIEW_DEFAULT', 'Use view from parent pages'), 'UseOnlyDefaultGroupView' => _t(ProductGroupPage::class . '.USEONLYDEFAULTGROUPVIEW', 'Allow only default view'), 'DisplaySettings' => _t(ProductGroupPage::class . '.DisplaySettings', 'Display settings'), 'RedirectionSettings' => _t(ProductGroupHolder::class . '.RedirectionSettings', 'Redirection'), 'RedirectToProductGroup' => _t(ProductGroupHolder::class . '.RedirectToProductGroup', 'Redirect to a product group'), 'LinkTo' => _t(ProductGroupHolder::class . '.LinkTo', 'Product group to redirect to'), 'Yes' => Tools::field_label('Yes'), 'No' => Tools::field_label('No'), ) ); }); return parent::fieldLabels($includerelations); } /** * Return all fields of the backend. * * @return FieldList Fields of the CMS */ public function getCMSFields() { $this->beforeUpdateCMSFields(function($fields) { $useOnlydefaultGroupviewSource = [ 'inherit' => $this->fieldLabel('DefaultGroupViewInherit'), 'yes' => $this->fieldLabel('Yes'), 'no' => $this->fieldLabel('No'), ]; $defaultGroupViewField = GroupViewHandler::getGroupViewDropdownField('DefaultGroupView', $this->fieldLabel('DefaultGroupView'), $this->DefaultGroupView, $this->fieldLabel('DefaultGroupViewInherit')); $useOnlyDefaultGroupViewField = DropdownField::create('UseOnlyDefaultGroupView', $this->fieldLabel('UseOnlyDefaultGroupView'), $useOnlydefaultGroupviewSource, $this->UseOnlyDefaultGroupView); $productGroupsPerPageField = TextField::create('productGroupsPerPage', $this->fieldLabel('productGroupsPerPage')); $defaultGroupHolderViewField = GroupViewHandler::getGroupViewDropdownField('DefaultGroupHolderView', $this->fieldLabel('DefaultGroupHolderView'), $this->DefaultGroupHolderView, $this->fieldLabel('DefaultGroupView')); $useOnlyDefaultGroupHolderViewField = DropdownField::create('UseOnlyDefaultGroupHolderView', $this->fieldLabel('UseOnlyDefaultGroupHolderView'), $useOnlydefaultGroupviewSource, $this->UseOnlyDefaultGroupHolderView); $fieldGroup = FieldGroup::create('FieldGroup', '', $fields); $redirectionFieldGroup = FieldGroup::create('RedirectionFieldGroup', '', $fields); $redirectToProductGroupField = CheckboxField::create('RedirectToProductGroup', $this->fieldLabel('RedirectToProductGroup')); $linkToField = TreeDropdownField::create('LinkToID', $this->fieldLabel('LinkTo'), SiteTree::class); $productGroupsPerPageField->setRightTitle($this->fieldLabel('ProductsPerPageHint')); $fieldGroup->push($defaultGroupViewField); $fieldGroup->push($useOnlyDefaultGroupViewField); $fieldGroup->breakAndPush($productGroupsPerPageField); $fieldGroup->breakAndPush($defaultGroupHolderViewField); $fieldGroup->push($useOnlyDefaultGroupHolderViewField); $redirectionFieldGroup->push($redirectToProductGroupField); if ($this->exists()) { $linkToField->setTreeBaseID($this->ID); $redirectionFieldGroup->breakAndPush($linkToField); } $displaySettingsToggle = ToggleCompositeField::create( 'DisplaySettingsToggle', $this->fieldLabel('DisplaySettings'), [ $fieldGroup, ] )->setHeadingLevel(4)->setStartClosed(true); $redirectionSettingsToggle = ToggleCompositeField::create( 'RedirectionSettingsToggle', $this->fieldLabel('RedirectionSettings'), [ $redirectionFieldGroup, ] )->setHeadingLevel(4)->setStartClosed(true); $fields->insertAfter($redirectionSettingsToggle, 'Content'); $fields->insertAfter($displaySettingsToggle, 'Content'); }); $this->getCMSFieldsIsCalled = true; return parent::getCMSFields(); } /** * Returns a dynamic meta description. * * @return string */ public function getMetaDescription() { $metaDescription = $this->getField('MetaDescription'); if (!$this->getCMSFieldsIsCalled) { if (empty($metaDescription)) { $descriptionArray = [$this->Title]; $children = $this->Children(); if ($children->count() > 0) { $map = $children->map(); if ($map instanceof Map) { $map = $map->toArray(); } $descriptionArray = array_merge($descriptionArray, $map); } $metaDescription = SeoTools::extractMetaDescriptionOutOfArray($descriptionArray); } $this->extend('updateMetaDescription', $metaDescription); } return $metaDescription; } /** * Return the link that we should redirect to. * Only return a value if there is a legal redirection destination. * * @return void * * @author Sebastian Diel <sdiel@pixeltricks.de> * @since 16.070.2014 */ public function redirectionLink() { $redirectionLink = false; if ($this->RedirectToProductGroup) { $linkTo = $this->LinkToID ? ProductGroupPage::get()->byID($this->LinkToID) : null; if ($linkTo instanceof ProductGroupPage && $linkTo->exists() && $linkTo->ID != $this->ID ) { $redirectionLink = $linkTo->Link(); } } return $redirectionLink; } /** * Checks if ProductGroup has children or products. * * @return bool * * @author Sebastian Diel <sdiel@pixeltricks.de> * @since 01.02.2011 */ public function hasProductsOrChildren() { if (count($this->Children()) > 0) { return true; } return false; } /** * Returns the cache key parts for this product group holder * * @return array * * @author Sebastian Diel <sdiel@pixeltricks.de> * @since 07.03.2018 */ public function CacheKeyParts() { if (is_null($this->cacheKeyParts)) { $lastEditedChildID = 0; if ($this->Children()->Count() > 0) { $this->Children()->sort('LastEdited', 'DESC'); $lastEditedChildID = $this->Children()->First()->ID; } $ctrl = Controller::curr(); /* @var $ctrl ProductGroupHolderController */ $cacheKeyParts = [ i18n::get_locale(), $this->LastEdited, $ctrl->getSqlOffsetForProductGroups(), GroupViewHandler::getActiveGroupHolderView(), $lastEditedChildID, ]; $this->extend('updateCacheKeyParts', $cacheKeyParts); $this->cacheKeyParts = $cacheKeyParts; } return $this->cacheKeyParts; } /** * Returns the cache key for this product group holder * * @return string * * @author Sebastian Diel <sdiel@pixeltricks.de> * @since 07.03.2018 */ public function CacheKey() { if (is_null($this->cacheKey)) { $cacheKey = implode('_', $this->CacheKeyParts()); $this->extend('updateCacheKey', $cacheKey); $this->cacheKey = $cacheKey; } return $this->cacheKey; } /** * Returns whether this is a ProductGroupHolder, so true.. * * @return boolean */ public function IsProductGroupHolder() { return true; } } |