/**
  * Prepare form.
  *
  * @return $this
  */
 protected function _prepareForm()
 {
     $bannerAttributes = $this->_banner->getStoreAttributes();
     $bannerAttributesInStores = ['store_id' => ''];
     foreach ($bannerAttributes as $bannerAttribute) {
         $bannerAttributesInStores[$bannerAttribute . '_in_store'] = '';
     }
     $dataObj = $this->_objectFactory->create(['data' => $bannerAttributesInStores]);
     $model = $this->_coreRegistry->registry('banner');
     if ($sliderId = $this->getRequest()->getParam('current_slider_id')) {
         $model->setSliderId($sliderId);
     }
     $dataObj->addData($model->getData());
     $storeViewId = $this->getRequest()->getParam('store');
     $attributesInStore = $this->_valueCollectionFactory->create()->addFieldToFilter('banner_id', $model->getId())->addFieldToFilter('store_id', $storeViewId)->getColumnValues('attribute_code');
     /** @var \Magento\Framework\Data\Form $form */
     $form = $this->_formFactory->create();
     $form->setHtmlIdPrefix($this->_banner->getFormFieldHtmlIdPrefix());
     $fieldset = $form->addFieldset('base_fieldset', ['legend' => __('Banner Information')]);
     if ($model->getId()) {
         $fieldset->addField('banner_id', 'hidden', ['name' => 'banner_id']);
     }
     $elements = [];
     $elements['name'] = $fieldset->addField('name', 'text', ['name' => 'name', 'label' => __('Name'), 'title' => __('Name'), 'required' => true]);
     $elements['status'] = $fieldset->addField('status', 'select', ['label' => __('Status'), 'title' => __('Banner Status'), 'name' => 'status', 'options' => Status::getAvailableStatuses()]);
     $slider = $this->_sliderFactory->create()->load($sliderId);
     if ($slider->getId()) {
         $elements['slider_id'] = $fieldset->addField('slider_id', 'select', ['label' => __('Slider'), 'name' => 'slider_id', 'values' => [['value' => $slider->getId(), 'label' => $slider->getTitle()]]]);
     } else {
         $elements['slider_id'] = $fieldset->addField('slider_id', 'select', ['label' => __('Slider'), 'name' => 'slider_id', 'values' => $model->getAvailableSlides()]);
     }
     $elements['image_alt'] = $fieldset->addField('image_alt', 'text', ['title' => __('Alt Text'), 'label' => __('Alt Text'), 'name' => 'image_alt', 'note' => 'Used for SEO']);
     $elements['click_url'] = $fieldset->addField('click_url', 'text', ['title' => __('URL'), 'label' => __('URL'), 'name' => 'click_url']);
     $elements['image'] = $fieldset->addField('image', 'image', ['title' => __('Banner Image'), 'label' => __('Banner Image'), 'name' => 'image', 'note' => 'Allow image type: jpg, jpeg, gif, png']);
     $dateFormat = $this->_localeDate->getDateFormat(\IntlDateFormatter::SHORT);
     $timeFormat = $this->_localeDate->getTimeFormat(\IntlDateFormatter::SHORT);
     if ($dataObj->hasData('start_time')) {
         $datetime = new \DateTime($dataObj->getData('start_time'));
         $dataObj->setData('start_time', $datetime->setTimezone(new \DateTimeZone($this->_localeDate->getConfigTimezone())));
     }
     if ($dataObj->hasData('end_time')) {
         $datetime = new \DateTime($dataObj->getData('end_time'));
         $dataObj->setData('end_time', $datetime->setTimezone(new \DateTimeZone($this->_localeDate->getConfigTimezone())));
     }
     $style = 'color: #000;background-color: #fff; font-weight: bold; font-size: 13px;';
     $elements['start_time'] = $fieldset->addField('start_time', 'date', ['name' => 'start_time', 'label' => __('Starting time'), 'title' => __('Starting time'), 'required' => true, 'readonly' => true, 'style' => $style, 'class' => 'required-entry', 'date_format' => $dateFormat, 'time_format' => $timeFormat, 'note' => $this->_localeDate->getDateTimeFormat(\IntlDateFormatter::SHORT)]);
     $elements['end_time'] = $fieldset->addField('end_time', 'date', ['name' => 'end_time', 'label' => __('Ending time'), 'title' => __('Ending time'), 'required' => true, 'readonly' => true, 'style' => $style, 'class' => 'required-entry', 'date_format' => $dateFormat, 'time_format' => $timeFormat, 'note' => $this->_localeDate->getDateTimeFormat(\IntlDateFormatter::SHORT)]);
     $elements['target'] = $fieldset->addField('target', 'select', ['label' => __('Target'), 'name' => 'target', 'values' => [['value' => \Magestore\Bannerslider\Model\Banner::BANNER_TARGET_SELF, 'label' => __('New Window with Browser Navigation')], ['value' => \Magestore\Bannerslider\Model\Banner::BANNER_TARGET_PARENT, 'label' => __('Parent Window with Browser Navigation')], ['value' => \Magestore\Bannerslider\Model\Banner::BANNER_TARGET_BLANK, 'label' => __('New Window without Browser Navigation')]]]);
     foreach ($attributesInStore as $attribute) {
         if (isset($elements[$attribute])) {
             $elements[$attribute]->setStoreViewId($storeViewId);
         }
     }
     $form->addValues($dataObj->getData());
     $this->setForm($form);
     return parent::_prepareForm();
 }
Exemple #2
0
 /**
  * Check if rule can be applied for specific address/quote/customer
  *
  * @param \Magento\SalesRule\Model\Rule $rule
  * @param \Magento\Quote\Model\Quote\Address $address
  * @return bool
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 public function canProcessRule($rule, $address)
 {
     if ($rule->hasIsValidForAddress($address) && !$address->isObjectNew()) {
         return $rule->getIsValidForAddress($address);
     }
     /**
      * check per coupon usage limit
      */
     if ($rule->getCouponType() != \Magento\SalesRule\Model\Rule::COUPON_TYPE_NO_COUPON) {
         $couponCode = $address->getQuote()->getCouponCode();
         if (strlen($couponCode)) {
             /** @var \Magento\SalesRule\Model\Coupon $coupon */
             $coupon = $this->couponFactory->create();
             $coupon->load($couponCode, 'code');
             if ($coupon->getId()) {
                 // check entire usage limit
                 if ($coupon->getUsageLimit() && $coupon->getTimesUsed() >= $coupon->getUsageLimit()) {
                     $rule->setIsValidForAddress($address, false);
                     return false;
                 }
                 // check per customer usage limit
                 $customerId = $address->getQuote()->getCustomerId();
                 if ($customerId && $coupon->getUsagePerCustomer()) {
                     $couponUsage = $this->objectFactory->create();
                     $this->usageFactory->create()->loadByCustomerCoupon($couponUsage, $customerId, $coupon->getId());
                     if ($couponUsage->getCouponId() && $couponUsage->getTimesUsed() >= $coupon->getUsagePerCustomer()) {
                         $rule->setIsValidForAddress($address, false);
                         return false;
                     }
                 }
             }
         }
     }
     /**
      * check per rule usage limit
      */
     $ruleId = $rule->getId();
     if ($ruleId && $rule->getUsesPerCustomer()) {
         $customerId = $address->getQuote()->getCustomerId();
         /** @var \Magento\SalesRule\Model\Rule\Customer $ruleCustomer */
         $ruleCustomer = $this->customerFactory->create();
         $ruleCustomer->loadByCustomerRule($customerId, $ruleId);
         if ($ruleCustomer->getId()) {
             if ($ruleCustomer->getTimesUsed() >= $rule->getUsesPerCustomer()) {
                 $rule->setIsValidForAddress($address, false);
                 return false;
             }
         }
     }
     $rule->afterLoad();
     /**
      * quote does not meet rule's conditions
      */
     if (!$rule->validate($address)) {
         $rule->setIsValidForAddress($address, false);
         return false;
     }
     /**
      * passed all validations, remember to be valid
      */
     $rule->setIsValidForAddress($address, true);
     return true;
 }