Source of file DiscountModelAdmin.php
Size: 6,551 Bytes - Last Modified: 2021-12-24T06:35:13+00:00
/var/www/docs.ssmods.com/process/src/src/Admin/DiscountModelAdmin.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 | <?php namespace SilverShop\Discounts\Admin; use SilverStripe\Admin\ModelAdmin; use SilverStripe\Forms\NumericField; use SilverStripe\Forms\FieldGroup; use SilverStripe\Forms\TextField; use SilverStripe\Forms\DropdownField; use SilverStripe\Forms\FieldList; use SilverStripe\Forms\FormAction; use SilverStripe\Forms\RequiredFields; use SilverStripe\Forms\Form; use SilverShop\Discounts\Model\OrderDiscount; use SilverShop\Discounts\Model\OrderCoupon; use SilverShop\Discounts\Model\PartialUseDiscount; use SilverShop\Discounts\Form\GridField_LinkComponent; class DiscountModelAdmin extends ModelAdmin { private static $url_segment = 'discounts'; private static $menu_title = 'Discounts'; private static $menu_icon = 'silvershop/discounts:images/icon-coupons.png'; private static $menu_priority = 2; private static $managed_models = [ OrderDiscount::class, OrderCoupon::class, PartialUseDiscount::class ]; private static $allowed_actions = [ 'generatecoupons', 'GenerateCouponsForm' ]; private static $model_descriptions = [ 'OrderDiscount' => 'Discounts are applied at the checkout, based on defined constraints. If not constraints are given, then the discount will always be applied.', 'OrderCoupon' => 'Coupons are like discounts, but have an associated code.', 'PartialUseDiscount' => "Partial use discounts are 'amount only' discounts that allow remainder amounts to be used." ]; public function getEditForm($id = null, $fields = null) { $form = parent::getEditForm($id, $fields); if ($grid = $form->Fields()->fieldByName(OrderCoupon::class)) { $grid->getConfig() ->addComponent( $link = new GridField_LinkComponent('Generate Multiple Coupons', $this->Link(). '/generatecoupons'), 'GridFieldExportButton' ); $link->addExtraClass('ss-ui-action-constructive'); } $descriptions = self::config()->model_descriptions; if (isset($descriptions[$this->modelClass])) { $form->Fields()->fieldByName($this->modelClass) ->setDescription($descriptions[$this->modelClass]); } return $form; } /** * Update results list, to include custom search filters */ public function getList() { $params = $this->request->requestVar('q'); $list = parent::getList(); if (isset($params['HasBeenUsed'])) { $list = $list ->leftJoin("SilverShop_OrderItem_Discounts", "\"SilverShop_OrderItem_Discounts\".\"DiscountID\" = \"Discount\".\"ID\"") ->leftJoin("SilverShop_OrderDiscountModifier_Discounts", "\"SilverShop_OrderDiscountModifier_Discounts\".\"DiscountID\" = \"Discount\".\"ID\"") ->innerJoin( "OrderAttribute", implode( " OR ", [ "\"SilverShop_OrderAttribute\".\"ID\" = \"SilverShop_OrderItem_Discounts\".\"Product_OrderItemID\"", "\"SilverShop_OrderAttribute\".\"ID\" = \"SilverShop_OrderDiscountModifier_Discounts\".\"SilverShop_OrderDiscountModifierID\"" ] ) ); } if (isset($params['Products'])) { $list = $list ->innerJoin("Discount_Products", "Discount_Products.DiscountID = Discount.ID") ->filter("Discount_Products.ProductID", $params['Products']); } if (isset($params['Categories'])) { $list = $list ->innerJoin("Discount_Categories", "Discount_Categories.DiscountID = Discount.ID") ->filter("Discount_Categories.ProductCategoryID", $params['Categories']); } return $list; } public function GenerateCouponsForm() { $fields = OrderCoupon::create()->getCMSFields(); $fields->removeByName('Code'); $fields->removeByName('GiftVoucherID'); $fields->removeByName('SaveNote'); $fields->addFieldsToTab( 'Root.Main', [ NumericField::create('Number', 'Number of Coupons'), FieldGroup::create( 'Code', TextField::create('Prefix', 'Code Prefix') ->setMaxLength(5), DropdownField::create( 'Length', 'Code Characters Length', array_combine(range(5, 20), range(5, 20)), OrderCoupon::config()->generated_code_length )->setDescription('This is in addition to the length of the prefix.') ) ], 'Title' ); $actions = new FieldList( new FormAction('generate', 'Generate') ); $validator = new RequiredFields( [ 'Title', 'Number', 'Type' ] ); $form = new Form($this, 'GenerateCouponsForm', $fields, $actions, $validator); $form->addExtraClass('cms-edit-form cms-panel-padded center ui-tabs-panel ui-widget-content ui-corner-bottom'); $form->setAttribute('data-pjax-fragment', 'CurrentForm'); $form->setHTMLID('Form_EditForm'); $form->loadDataFrom( [ 'Number' => 1, 'Active' => 1, 'ForCart' => 1, 'UseLimit' => 1 ] ); return $form; } public function generate($data, $form) { $count = 1; if (isset($data['Number']) && is_numeric($data['Number'])) { $count = (int) $data['Number']; } $prefix = isset($data['Prefix']) ? $data['Prefix'] : ''; $length = isset($data['Length']) ? (int) $data['Length'] : OrderCoupon::config()->generated_code_length; for ($i = 0; $i < $count; $i++) { $coupon = new OrderCoupon(); $form->saveInto($coupon); $coupon->Code = OrderCoupon::generate_code( $length, $prefix ); $coupon->write(); } $this->redirect($this->Link()); } public function generatecoupons() { return [ 'Title' => 'Generate Coupons', 'EditForm' => $this->GenerateCouponsForm(), 'SearchForm' => '', 'ImportForm' => '' ]; } } |