Exemplo n.º 1
0
 /**
  * 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();
 }
 /**
  * set slider Id and set template.
  *
  * @param int $sliderId
  */
 public function setSliderId($sliderId)
 {
     $this->_sliderId = $sliderId;
     $slider = $this->_sliderFactory->create()->load($this->_sliderId);
     if ($slider->getId()) {
         $this->setSlider($slider);
         if ($slider->getStyleContent() == SliderModel::STYLE_CONTENT_NO) {
             $this->setTemplate(SliderModel::STYLESLIDE_CUSTOM_TEMPLATE);
         } else {
             $this->setStyleSlideTemplate($slider->getStyleSlide());
         }
     }
     return $this;
 }