Source of file ElementProducts.php
Size: 4,295 Bytes - Last Modified: 2021-12-24T06:46:30+00:00
/var/www/docs.ssmods.com/process/src/src/Element/ElementProducts.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 | <?php namespace Dynamic\Shopify\Element; use DNADesign\Elemental\Models\BaseElement; use Dynamic\Shopify\Page\ShopifyProduct; use SilverStripe\Forms\FieldList; use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter; use SilverStripe\Forms\GridField\GridFieldAddNewButton; use SilverStripe\ORM\ArrayList; use SilverStripe\ORM\DB; use SilverStripe\ORM\FieldType\DBField; use SilverStripe\ORM\FieldType\DBHTMLText; use SilverStripe\ORM\ValidationException; use Symbiote\GridFieldExtensions\GridFieldAddExistingSearchButton; use Symbiote\GridFieldExtensions\GridFieldOrderableRows; if (!class_exists(BaseElement::class)) { return; } /** * Class ElementProducts * @package Dynamic\Shopify\Element */ class ElementProducts extends BaseElement { /** * @var string */ private static $table_name = 'ElementalProducts'; /** * @var array */ private static $db = [ 'Content' => 'HTMLText', 'Limit' => 'Int', ]; /** * @var array */ private static $many_many = [ 'Products' => ShopifyProduct::class, ]; /** * @var \string[][] */ private static $many_many_extraFields = [ 'Products' => [ 'ElementSortOrder' => 'Int', ], ]; /** * @var array */ private static $defaults = array( 'Limit' => 4, ); /** * @var bool */ private static $inline_editable = false; /** * @return \SilverStripe\Forms\FieldList */ public function getCMSFields() { $this->beforeUpdateCMSFields(function (FieldList $fields) { $fields->dataFieldByName('Content') ->setRows(10); $fields->insertBefore( 'Content', $fields->dataFieldByName('Limit') ); if ($this->ID) { $products = $fields->dataFieldByName('Products'); $config = $products->getConfig(); $config ->removeComponentsByType([ GridFieldAddExistingAutocompleter::class, GridFieldAddNewButton::class, ]) ->addComponents([ new GridFieldAddExistingSearchButton(), new GridFieldOrderableRows('ElementSortOrder') ]); } }); return parent::getCMSFields(); // TODO: Change the autogenerated stub } /** * @return mixed * @throws ValidationException */ public function getProductsList() { $random = DB::get_conn()->random(); $limit = $this->Limit; $products = $this->Products()->limit($limit)->sort('ElementSortOrder'); $count = $products->count(); $combined = ArrayList::create(); if ($count < $limit) { $backFill = ShopifyProduct::get(); if ($products->exists()) { $backFill = $backFill->exclude(['ID' => $products->column()]); } $backFill = $backFill ->sort($random) ->limit($limit - $count); foreach ($products as $product) { $combined->push($product); } foreach ($backFill as $product) { $combined->push($product); } $products = $combined; } return $products->limit($limit); } /** * @return string * @throws ValidationException */ public function getSummary() { $count = $this->exists() ? $this->getProductsList()->count() : 0; $label = _t( ShopifyProduct::class . '.PLURALS', 'A Product|{count} Products', ['count' => $count] ); return DBField::create_field('HTMLText', $label)->Summary(20); } /** * @return array * @throws ValidationException */ protected function provideBlockSchema() { $blockSchema = parent::provideBlockSchema(); $blockSchema['content'] = $this->getSummary(); return $blockSchema; } /** * @return string */ public function getType() { return _t(__CLASS__ . '.BlockType', 'Products'); } } |