Source of file OptionGroup.php
Size: 4,276 Bytes - Last Modified: 2021-12-24T06:45:37+00:00
/var/www/docs.ssmods.com/process/src/src/Model/OptionGroup.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 | <?php namespace Dynamic\FoxyStripe\Model; use SilverStripe\Forms\RequiredFields; use SilverStripe\ORM\DataObject; use SilverStripe\ORM\ValidationResult; use SilverStripe\Security\Permission; /** * Class OptionGroup * @package Dynamic\FoxyStripe\Model * * @property \SilverStripe\ORM\FieldType\DBVarchar Title * * @method \SilverStripe\ORM\HasManyList Options */ class OptionGroup extends DataObject { /** * @var array */ private static $db = array( 'Title' => 'Varchar(100)', ); /** * @var array */ private static $has_many = array( 'Options' => OptionItem::class, ); /** * @var string */ private static $singular_name = 'Product Option Group'; /** * @var string */ private static $plural_name = 'Product Option Groups'; /** * @var string */ private static $description = 'Groups of product options, e.g. size, color, etc'; /** * @var string */ private static $table_name = 'OptionGroup'; /** * @throws \SilverStripe\ORM\ValidationException */ public function requireDefaultRecords() { parent::requireDefaultRecords(); // create a catch-all group if (!self::get()->filter(array('Title' => 'Options'))->first()) { $do = new self(); $do->Title = 'Options'; $do->write(); } if (!self::get()->filter(array('Title' => 'Size'))->first()) { $do = new self(); $do->Title = 'Size'; $do->write(); } if (!self::get()->filter(array('Title' => 'Color'))->first()) { $do = new self(); $do->Title = 'Color'; $do->write(); } if (!self::get()->filter(array('Title' => 'Type'))->first()) { $do = new self(); $do->Title = 'Type'; $do->write(); } } /** * @return RequiredFields */ public function getCMSValidator() { return new RequiredFields(array('Title')); } /** * @return ValidationResult */ public function validate() { $result = parent::validate(); $title = $this->Title; $firstChar = substr($title, 0, 1); if (preg_match('/[^a-zA-Z]/', $firstChar)) { $result->addError('The first character of the Title can only be a letter', 'bad'); } if (preg_match('/[^a-zA-Z]\s/', $title)) { $result->addError('Please only use letters, numbers and spaces in the title', 'bad'); } return $result; } /** * @throws \SilverStripe\ORM\ValidationException */ public function onBeforeDelete() { parent::onBeforeDelete(); //make sure that if we delete this option group, we reassign the group's option items to the 'None' group. $items = OptionItem::get()->filter(array('ProductOptionGroupID' => $this->ID)); if (isset($items)) { if ($noneGroup = self::get()->filter(array('Title' => 'Options'))->first()) { /** @var OptionItem $item */ foreach ($items as $item) { $item->ProductOptionGroupID = $noneGroup->ID; $item->write(); } } } } /** * @param bool $member * * @return bool */ public function canView($member = null) { return true; } /** * @param null $member * * @return bool|int */ public function canEdit($member = null) { switch ($this->Title) { case 'Options': return false; break; default: return Permission::check('Product_CANCRUD', 'any', $member); break; } } /** * @param null $member * * @return bool|int */ public function canDelete($member = null) { return $this->canEdit($member); } /** * @param null $member * @param array $context * * @return bool|int */ public function canCreate($member = null, $context = []) { return Permission::check('Product_CANCRUD', 'any', $member); } } |