Source of file Attribute.php
Size: 8,139 Bytes - Last Modified: 2021-12-23T10:47:54+00:00
/var/www/docs.ssmods.com/process/src/code/product/Attribute.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328 | <?php /** * Represents a {@link Product} Attribute, e.g: Size, Color, Material etc. * Attributes are created in the {@link ShopAdmin} where they can be set with default * Options. They are then selected on each product they relate to. Once an attribute * is added to a Product, that Product needs to define some Options for that Attribute * and also have some Variations. If the Product does not have Variations when it needs to * then it cannot be purchased. * * @author Frank Mullenger <frankmullenger@gmail.com> * @copyright Copyright (c) 2011, Frank Mullenger * @package swipestripe * @subpackage product */ class Attribute extends DataObject implements PermissionProvider { private static $singular_name = 'Attribute'; private static $plural_name = 'Attributes'; public $firstWrite = false; /** * DB fields for the Attribute * * @see Product_Controller::AddToCartForm() * @var Array */ private static $db = array( 'Title' => 'Varchar(100)', 'Description' => 'Text', 'SortOrder' => 'Int' ); /** * Has many relations for the Attribute * * @var Array */ private static $has_many = array( 'Options' => 'Option' ); private static $has_one = array( 'Product' => 'Product', 'DefaultAttribute' => 'Attribute_Default' ); /** * Searchable fields for Attributes * * @var Array */ private static $searchable_fields = array( 'Title' ); /** * Summary fields for Attributes * * @var Array */ private static $summary_fields = array( 'Title' => 'Title', 'Description' => 'Description', 'OptionSummary' => 'Options' ); private static $default_sort = 'SortOrder'; public function providePermissions() { return array( 'EDIT_ATTRIBUTES' => 'Edit Attributes', ); } public function canEdit($member = null) { $extended = $this->extendedCan(__FUNCTION__, $member); if($extended !== null) { return $extended; } return Permission::check('EDIT_ATTRIBUTES'); } public function canView($member = null) { $extended = $this->extendedCan(__FUNCTION__, $member); if($extended !== null) { return $extended; } return true; } public function canDelete($member = null) { $extended = $this->extendedCan(__FUNCTION__, $member); if($extended !== null) { return $extended; } return Permission::check('EDIT_ATTRIBUTES'); } public function canCreate($member = null) { $extended = $this->extendedCan(__FUNCTION__, $member); if($extended !== null) { return $extended; } return Permission::check('EDIT_ATTRIBUTES'); } /** * Add some fields to the CMS for managing Attributes. * * @see DataObject::getCMSFields() * @return FieldList */ function getCMSFields() { $fields = new FieldList( $rootTab = new TabSet('Root', $tabMain = new Tab('Attribute', TextField::create('Title') ->setRightTitle('For displaying on the product page'), TextField::create('Description') ->setRightTitle('For displaying on the order'), HiddenField::create('ProductID') ) ) ); if (!$this->ID) { $defaultAttributes = Attribute_Default::get(); if ($defaultAttributes && $defaultAttributes->exists()) { $fields->addFieldToTab( 'Root.Attribute', DropdownField::create( 'DefaultAttributeID', 'Use existing attribute', $defaultAttributes->map('ID', 'TitleOptionSummary')->toArray() )->setHasEmptyDefault(true), 'Title' ); $fields->addFieldToTab('Root.Attribute', new HeaderField('AttributeOr', 'Or create new one...', 5), 'Title'); } } if ($this->ID) { $fields->addFieldToTab('Root.Options', GridField::create( 'Options', 'Options', $this->Options(), GridFieldConfig_BasicSortable::create() )); } $this->extend('updateCMSFields', $fields); return $fields; } public function OptionSummary() { $summary = ''; $options = $this->Options(); if ($options && $options->exists()) { $summary = implode(', ', $options->map()->values()); } return $summary; } public function TitleOptionSummary() { $optionString = ''; $options = $this->Options(); if ($options && $options->exists()) { $optionString = implode(', ', $options->map()->toArray()); } return $this->Title . " - $optionString"; } public function onBeforeWrite() { parent::onBeforeWrite(); $this->firstWrite = !$this->isInDB(); if ($this->firstWrite) { $defaultAttribute = $this->DefaultAttribute(); if ($defaultAttribute && $defaultAttribute->exists()) { $this->Title = $defaultAttribute->Title; $this->Description = $defaultAttribute->Description; } } } public function onAfterWrite() { parent::onAfterWrite(); //Check if first write if ($this->firstWrite) { $defaultAttribute = $this->DefaultAttribute(); if ($defaultAttribute && $defaultAttribute->exists()) { $options = $defaultAttribute->Options(); if ($options && $options->exists()) foreach ($options as $option) { $newOption = new Option(); $newOption->update($option->tomap()); $newOption->ID = null; $newOption->AttributeID = $this->ID; $newOption->write(); } } } //If product variation does not have a complete set of valid options, then disable it $product = $this->Product(); $variations = $product->Variations(); if ($variations) foreach ($variations as $variation) { if (!$variation->hasValidOptions()) { $variation->Status = 'Disabled'; $variation->write(); } } } public function getOptionField($prev = null) { return Attribute_OptionField::create($this, $prev); } } class Attribute_OptionField extends DropdownField { public function __construct($attr, $prev = null) { Requirements::javascript(THIRDPARTY_DIR . '/jquery/jquery.js'); Requirements::javascript(THIRDPARTY_DIR . '/jquery-entwine/dist/jquery.entwine-dist.js'); Requirements::javascript('swipestripe/javascript/Attribute_OptionField.js'); $product = $attr->Product(); //Pass in the attribute ID $name = "Options[" . $attr->ID . "]"; $title = $attr->Title; $source = $product->getOptionsForAttribute($attr->ID)->map(); $value = null; $this->addExtraClass('dropdown'); //If previous attribute field exists, listen to it and react with new options if ($prev && $prev->exists()) { $this->setAttribute('data-prev', "Options[" . $prev->ID . "]"); $variations = $product->Variations(); $options = array(); $temp = array(); if ($variations && $variations->exists()) foreach ($variations as $variation) { $prevOption = $variation->getOptionForAttribute($prev->ID); $option = $variation->getOptionForAttribute($attr->ID); if ($prevOption && $prevOption->exists() && $option && $option->exists()) { $temp[$prevOption->ID][$option->SortOrder][$option->ID] = $option->Title; } } //Using SortOrder to sort the options foreach ($temp as $prevID => $optionArray) { ksort($optionArray); $sorted = array(); foreach ($optionArray as $sort => $optionData) { $sorted += $optionData; } $options[$prevID] = $sorted; } $this->setAttribute('data-map', json_encode($options)); } parent::__construct($name, $title, $source, $value); } } class Attribute_Default extends Attribute { private static $singular_name = 'Attribute'; private static $plural_name = 'Attributes'; private static $has_one = array( 'ShopConfig' => 'ShopConfig' ); public function onBeforeWrite() { parent::onBeforeWrite(); $this->ProductID = 0; } function getCMSFields() { $fields = new FieldList( $rootTab = new TabSet('Root', $tabMain = new Tab('Attribute', TextField::create('Title') ->setRightTitle('For displaying on the product page'), TextField::create('Description') ->setRightTitle('For displaying on the order'), HiddenField::create('ProductID') ) ) ); if ($this->ID) { $fields->addFieldToTab('Root.Options', GridField::create( 'Options', 'Options', $this->Options(), GridFieldConfig_Basic::create() )); } $this->extend('updateCMSFields', $fields); return $fields; } } |