Source of file ProductCustomisation.php
Size: 4,883 Bytes - Last Modified: 2021-12-24T05:16:36+00:00
/var/www/docs.ssmods.com/process/src/code/model/ProductCustomisation.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 | <?php class ProductCustomisation extends DataObject { private static $db = array( 'Title' => 'Varchar', 'Required' => 'Boolean', 'DisplayAs' => "Enum('Dropdown,Radio,Checkboxes,TextEntry','Dropdown')", 'MaxLength' => "Int", 'Sort' => 'Int' ); private static $has_one = array( 'Parent' => 'CustomisableProduct', 'List' => 'ProductCustomisationList', ); private static $has_many = array( 'Options' => 'ProductCustomisationOption' ); private static $summary_fields = array( 'Title', 'DisplayAs' ); private static $default_sort = "\"Sort\" ASC"; public function getCMSFields() { $fields = parent::getCMSFields(); $fields->removeByName('Options'); $fields->removeByName('ParentID'); $fields->removeByName('Sort'); $fields->removeByName('MaxLength'); if ($this->ID && $this->DisplayAs != "TextEntry") { $field_types = singleton('ProductCustomisationOption')->getFieldTypes(); // Deal with product features $add_button = new GridFieldAddNewInlineButton('toolbar-header-left'); $add_button->setTitle('Add Customisation Option'); $options_field = new GridField( 'Options', '', $this->Options(), GridFieldConfig::create() ->addComponent(new GridFieldButtonRow('before')) ->addComponent(new GridFieldToolbarHeader()) ->addComponent(new GridFieldTitleHeader()) ->addComponent(new GridFieldEditableColumns()) ->addComponent(new GridFieldDeleteAction()) ->addComponent($add_button) ->addComponent(new GridFieldOrderableRows('Sort')) ); $fields->addFieldToTab('Root.Main', $options_field); } if ($this->ID && $this->DisplayAs == "TextEntry") { $fields->addFieldToTab("Root.Main", TextField::create("MaxLength")); } if (!$this->ID) { $fields->addFieldToTab('Root.Main', LiteralField::create('CreateWarning', '<p>You need to create this before you can add options</p>')); } $this->extend('updateCMSFields', $fields); return $fields; } // Get the default options for this customisation public function DefaultOptions() { $options = $this->Options()->filter('Default', 1); $this->extend('updateDefaultOptions', $options); return $options; } /** * Method that turns this object into a field type, to be loaded into a form * * @return FormField */ public function Field() { if ($this->Title && $this->DisplayAs) { $name = "customise_{$this->ID}_" . Convert::raw2url($this->Title); $title = ($this->Required) ? $this->Title . ' *' : $this->Title; $options = $this->Options()->map('Title', 'ItemSummary'); $defaults = $this->DefaultOptions(); $default = ($defaults->exists()) ? $defaults->first()->Title : null; switch ($this->DisplayAs) { case 'Dropdown': $field = DropdownField::create($name, $title, $options, $default) ->setEmptyString(_t('Commerce.PleaseSelect', 'Please Select')); break; case 'Radio': $field = OptionSetField::create($name, $title, $options, $default); break; case 'Checkboxes': $field = CheckboxSetField::create($name, $title, $options, $defaults->column('ID')); break; case 'TextEntry': $field = TextField::create($name, $title); if ($this->MaxLength) { $field->setMaxLength($this->MaxLength); } break; } $this->extend('updateField', $field); return $field; } else { return false; } } public function onBeforeDelete() { // Delete all options when this opbect is deleted foreach ($this->Options() as $option) { $option->delete(); } parent::onBeforeDelete(); } public function canView($member = false) { return $this->Parent()->canView($member); } public function canCreate($member = null) { return $this->Parent()->canCreate($member); } public function canEdit($member = null) { return $this->Parent()->canEdit($member); } public function canDelete($member = null) { return $this->Parent()->canDelete($member); } } |