Source of file BargainProductsWidget.php
Size: 9,083 Bytes - Last Modified: 2021-12-23T10:21:36+00:00
/var/www/docs.ssmods.com/process/src/src/Model/Widgets/BargainProductsWidget.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291 | <?php namespace SilverCart\Model\Widgets; use SilverCart\Admin\Model\Config; use SilverCart\Dev\Tools; use SilverCart\Model\ { Product\Product, Translation\TranslationTools, Widgets\BargainProductsWidgetTranslation, Widgets\Widget, Widgets\WidgetTools }; use SilverCart\View\GroupView\GroupViewHandler; use SilverStripe\ORM\ { ArrayList, FieldType\DBBoolean, FieldType\DBInt, FieldType\DBVarchar }; /** * Provides the a view of the bargain products. * You can define the number of products to be shown. * * @package SilverCart * @subpackage Model_Widgets * @author Sebastian Diel <sdiel@pixeltricks.de> * @since 22.08.2018 * @copyright 2018 pixeltricks GmbH * @license see license file in modules root directory */ class BargainProductsWidget extends Widget { /** * DB attributes of this widget * * @var array */ private static $db = [ 'numberOfProductsToFetch' => DBInt::class, 'fetchMethod' => "Enum('random,sortOrderAsc,sortOrderDesc','random')", 'GroupView' => DBVarchar::class, 'isContentView' => DBBoolean::class, 'UseAsSlider' => DBBoolean::class, ]; /** * 1:1 or 1:n relationships. * * @var array */ private static $has_many = [ 'BargainProductsWidgetTranslations' => BargainProductsWidgetTranslation::class ]; /** * Set default values. * * @var array */ private static $defaults = [ 'numberOfProductsToFetch' => 5, 'UseAsSlider' => true ]; /** * Casted Attributes. * * @var array */ private static $casting = [ 'FrontTitle' => 'Text', 'FrontContent' => 'Text', ]; /** * DB table name * * @var string */ private static $table_name = 'SilvercartBargainProductsWidget'; /** * Plain product elements * * @var ArrayList */ protected $products= null; /** * Product elements * * @var ArrayList */ protected $elements = null; /** * Getter for the front title depending on the set language * * @return string */ public function getFrontTitle() { return $this->getTranslationFieldValue('FrontTitle'); } /** * Getter for the FrontContent depending on the set language * * @return string The HTML front content */ public function getFrontContent() { return $this->getTranslationFieldValue('FrontContent'); } /** * Returns the input fields for this widget. * * @return FieldList */ public function getCMSFields() { $fields = parent::getCMSFields(); $fetchMethods = [ 'random' => $this->fieldLabel('fetchMethodRandom'), 'sortOrderAsc' => $this->fieldLabel('fetchMethodSortOrderAsc'), 'sortOrderDesc' => $this->fieldLabel('fetchMethodSortOrderDesc'), ]; $fetchMethodsField = $fields->dataFieldByName('fetchMethod'); $fetchMethodsField->setSource($fetchMethods); $fields->replaceField('GroupView', GroupViewHandler::getGroupViewDropdownField('GroupView', $this->fieldLabel('GroupView'))); return $fields; } /** * 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 22.08.2018 */ public function fieldLabels($includerelations = true) { return array_merge( parent::fieldLabels($includerelations), WidgetTools::fieldLabelsForProductSliderWidget($this), [ 'ProductGroupPage' => _t(ProductGroupItemsWidget::class . '.STOREADMIN_FIELDLABEL', 'Please choose the product group to display:'), 'useSelectionMethod' => _t(ProductGroupItemsWidget::class . '.USE_SELECTIONMETHOD', 'Selection method for products'), 'SelectionMethodProductGroup' => _t(ProductGroupItemsWidget::class . '.SELECTIONMETHOD_PRODUCTGROUP', 'From product group'), 'SelectionMethodProducts' => _t(ProductGroupItemsWidget::class . '.SELECTIONMETHOD_PRODUCTS', 'Choose manually'), 'ProductGroupTab' => _t(ProductGroupItemsWidget::class . '.CMS_PRODUCTGROUPTABNAME', 'Product group'), 'ProductsTab' => _t(ProductGroupItemsWidget::class . '.CMS_PRODUCTSTABNAME', 'Products'), 'BargainProductsWidgetTranslations' => _t(TranslationTools::class . '.TRANSLATIONS', 'Translations'), 'UseAsSlider' => _t(Widget::class . '.UseAsSlider', 'Use as a slider'), ] ); } /** * Returns the elements * * @return ArrayList */ public function getElements() { if (is_null($this->elements)) { $this->Elements(); } return $this->elements; } /** * Sets the elements * * @param ArrayList $elements Elements to set * * @return void */ public function setElements(ArrayList $elements) { $this->elements = $elements; } /** * Returns a number of bargain products. * * @return SS_List * * @author Sebastian Diel <sdiel@pixeltricks.de> * @since 27.03.2012 */ public function Elements() { if (is_null($this->elements)) { if (!$this->numberOfProductsToFetch) { $defaults = $this->config()->get('defaults'); $this->numberOfProductsToFetch = $defaults['numberOfProductsToFetch']; } $priceField = 'PriceGrossAmount'; if (Config::Pricetype() === Config::PRICE_TYPE_NET) { $priceField = 'PriceNetAmount'; } $productTable = Tools::get_table_name(Product::class); switch ($this->fetchMethod) { case 'sortOrderAsc': $sort = '"' . $productTable . '"."MSRPriceAmount" - "' . $productTable . '"."PriceGrossAmount" ASC'; break; case 'sortOrderDesc': $sort = '"' . $productTable . '"."MSRPriceAmount" - "' . $productTable . '"."PriceGrossAmount" DESC'; break; case 'random': default: $sort = "RAND()"; } $this->listFilters = []; if (count(self::$registeredFilterPlugins) > 0) { foreach (self::$registeredFilterPlugins as $registeredPlugin) { $pluginFilters = $registeredPlugin->filter(); if (is_array($pluginFilters)) { $this->listFilters = array_merge( $this->listFilters, $pluginFilters ); } } } $filter = sprintf( '"' . $productTable . '"."MSRPriceAmount" IS NOT NULL AND "' . $productTable . '"."MSRPriceAmount" > 0 AND "' . $productTable . '"."%s" < "' . $productTable . '"."MSRPriceAmount"', $priceField ); foreach ($this->listFilters as $listFilterIdentifier => $listFilter) { $filter .= ' ' . $listFilter; } $products = Product::getProducts( $filter, $sort, null, "0," . $this->numberOfProductsToFetch ); $this->elements = $products; } return $this->elements; } /** * Returns the content for non slider widgets * * @return string * * @author Sebastian Diel <sdiel@pixeltricks.de> * @since 30.05.2012 */ public function ElementsContent() { return $this->customise([ 'Elements' => $this->Elements(), ])->renderWith(WidgetTools::getGroupViewTemplateName($this)); } /** * Returns the products to inject into a product group * * @return ArrayList */ public function getProducts() { $this->products = ArrayList::create(); if (!$this->UseAsSlider) { $this->products = $this->Elements(); } return $this->products; } /** * Creates the cache key for this widget. * * @return string * * @author Sebastian Diel <sdiel@pixeltricks.de> * @since 03.07.2012 */ public function WidgetCacheKey() { return WidgetTools::ProductWidgetCacheKey($this); } } |