/** * Prepare rating edit form * * @return $this */ protected function _prepareForm() { /** @var \Magento\Framework\Data\Form $form */ $form = $this->_formFactory->create(); $this->setForm($form); $fieldset = $form->addFieldset('rating_form', array('legend' => __('Rating Title'))); $fieldset->addField('rating_code', 'text', array('name' => 'rating_code', 'label' => __('Default Value'), 'class' => 'required-entry', 'required' => true)); foreach ($this->_systemStore->getStoreCollection() as $store) { $fieldset->addField('rating_code_' . $store->getId(), 'text', array('label' => $store->getName(), 'name' => 'rating_codes[' . $store->getId() . ']')); } if ($this->_session->getRatingData()) { $form->setValues($this->_session->getRatingData()); $data = $this->_session->getRatingData(); if (isset($data['rating_codes'])) { $this->_setRatingCodes($data['rating_codes']); } $this->_session->setRatingData(null); } elseif ($this->_coreRegistry->registry('rating_data')) { $form->setValues($this->_coreRegistry->registry('rating_data')->getData()); if ($this->_coreRegistry->registry('rating_data')->getRatingCodes()) { $this->_setRatingCodes($this->_coreRegistry->registry('rating_data')->getRatingCodes()); } } if ($this->_coreRegistry->registry('rating_data')) { $collection = $this->_optionFactory->create()->getResourceCollection()->addRatingFilter($this->_coreRegistry->registry('rating_data')->getId())->load(); $i = 1; foreach ($collection->getItems() as $item) { $fieldset->addField('option_code_' . $item->getId(), 'hidden', array('required' => true, 'name' => 'option_title[' . $item->getId() . ']', 'value' => $item->getCode() ? $item->getCode() : $i)); $i++; } } else { for ($i = 1; $i <= 5; $i++) { $fieldset->addField('option_code_' . $i, 'hidden', array('required' => true, 'name' => 'option_title[add_' . $i . ']', 'value' => $i)); } } $fieldset = $form->addFieldset('visibility_form', array('legend' => __('Rating Visibility'))); if (!$this->_storeManager->isSingleStoreMode()) { $field = $fieldset->addField('stores', 'multiselect', array('label' => __('Visible In'), 'name' => 'stores[]', 'values' => $this->_systemStore->getStoreValuesForForm())); $renderer = $this->getLayout()->createBlock('Magento\\Backend\\Block\\Store\\Switcher\\Form\\Renderer\\Fieldset\\Element'); $field->setRenderer($renderer); if ($this->_coreRegistry->registry('rating_data')) { $form->getElement('stores')->setValue($this->_coreRegistry->registry('rating_data')->getStores()); } } $fieldset->addField('is_active', 'checkbox', array('label' => __('Is Active'), 'name' => 'is_active', 'value' => 1)); $fieldset->addField('position', 'text', array('label' => __('Sort Order'), 'name' => 'position')); if ($this->_coreRegistry->registry('rating_data')) { $form->getElement('position')->setValue($this->_coreRegistry->registry('rating_data')->getPosition()); $form->getElement('is_active')->setIsChecked($this->_coreRegistry->registry('rating_data')->getIsActive()); } return parent::_prepareForm(); }
/** * @param string $ratingCode * @param array $stores * @return void */ protected function createRating($ratingCode, $stores) { $stores[] = \Magento\Store\Model\Store::DEFAULT_STORE_ID; $rating = $this->getRating($ratingCode); if (!$rating->getData()) { $rating->setRatingCode($ratingCode)->setStores($stores)->setIsActive('1')->setEntityId($this->getRatingEntityId())->save(); /**Create rating options*/ $options = [1 => '1', 2 => '2', 3 => '3', 4 => '4', 5 => '5']; foreach ($options as $key => $optionCode) { $optionModel = $this->ratingOptionsFactory->create(); $optionModel->setCode($optionCode)->setValue($key)->setRatingId($rating->getId())->setPosition($key)->save(); } } }
/** * Set rating options to form * * @return void */ protected function setRatingOptions() { if ($this->_coreRegistry->registry('rating_data')) { $collection = $this->optionFactory->create()->getResourceCollection()->addRatingFilter($this->_coreRegistry->registry('rating_data')->getId())->load(); $i = 1; foreach ($collection->getItems() as $item) { $this->getFieldset('rating_form')->addField('option_code_' . $item->getId(), 'hidden', ['required' => true, 'name' => 'option_title[' . $item->getId() . ']', 'value' => $item->getCode() ? $item->getCode() : $i]); $i++; } } else { for ($i = 1; $i <= 5; $i++) { $this->getFieldset('rating_form')->addField('option_code_' . $i, 'hidden', ['required' => true, 'name' => 'option_title[add_' . $i . ']', 'value' => $i]); } } }
protected function setUp() { $this->ratingOptionCollection = $this->getMock('\\Magento\\Review\\Model\\Resource\\Rating\\Option\\Collection', [], [], '', false); $this->element = $this->getMock('\\Magento\\Framework\\Data\\Form\\Element\\Text', ['setValue', 'setIsChecked'], [], '', false); $this->session = $this->getMock('\\Magento\\Framework\\Session\\Generic', ['getRatingData', 'setRatingData'], [], '', false); $this->rating = $this->getMock('\\Magento\\Review\\Model\\Rating', ['getId', 'getRatingCodes'], [], '', false); $this->optionRating = $this->getMock('\\Magento\\Review\\Model\\Rating\\Option', [], [], '', false); $this->store = $this->getMock('\\Magento\\Store\\Model\\Store', [], [], '', false); $this->form = $this->getMock('\\Magento\\Framework\\Data\\Form', [], [], '', false); $this->directoryReadInterface = $this->getMock('\\Magento\\Framework\\Filesystem\\Directory\\ReadInterface'); $this->registry = $this->getMock('\\Magento\\Framework\\Registry'); $this->formFactory = $this->getMock('\\Magento\\Framework\\Data\\FormFactory', [], [], '', false); $this->optionFactory = $this->getMock('\\Magento\\Review\\Model\\Rating\\OptionFactory', ['create'], [], '', false); $this->systemStore = $this->getMock('\\Magento\\Store\\Model\\System\\Store', [], [], '', false); $this->viewFileSystem = $this->getMock('\\Magento\\Framework\\View\\FileSystem', [], [], '', false); $this->fileSystem = $this->getMock('\\Magento\\Framework\\Filesystem', ['getDirectoryRead'], [], '', false); $this->rating->expects($this->any())->method('getId')->will($this->returnValue('1')); $this->ratingOptionCollection->expects($this->any())->method('addRatingFilter')->will($this->returnSelf()); $this->ratingOptionCollection->expects($this->any())->method('load')->will($this->returnSelf()); $this->ratingOptionCollection->expects($this->any())->method('getItems')->will($this->returnValue([$this->optionRating])); $this->optionRating->expects($this->any())->method('getResourceCollection')->will($this->returnValue($this->ratingOptionCollection)); $this->store->expects($this->any())->method('getId')->will($this->returnValue('0')); $this->store->expects($this->any())->method('getName')->will($this->returnValue('store_name')); $this->element->expects($this->any())->method('setValue')->will($this->returnSelf()); $this->element->expects($this->any())->method('setIsChecked')->will($this->returnSelf()); $this->form->expects($this->any())->method('setForm')->will($this->returnSelf()); $this->form->expects($this->any())->method('addFieldset')->will($this->returnSelf()); $this->form->expects($this->any())->method('addField')->will($this->returnSelf()); $this->form->expects($this->any())->method('setRenderer')->will($this->returnSelf()); $this->optionFactory->expects($this->any())->method('create')->will($this->returnValue($this->optionRating)); $this->systemStore->expects($this->any())->method('getStoreCollection')->will($this->returnValue(['0' => $this->store])); $this->formFactory->expects($this->any())->method('create')->will($this->returnValue($this->form)); $this->viewFileSystem->expects($this->any())->method('getTemplateFileName')->will($this->returnValue('template_file_name.html')); $this->fileSystem->expects($this->any())->method('getDirectoryRead')->will($this->returnValue($this->directoryReadInterface)); $objectManagerHelper = new ObjectManagerHelper($this); $this->block = $objectManagerHelper->getObject('Magento\\Review\\Block\\Adminhtml\\Rating\\Edit\\Tab\\Form', ['registry' => $this->registry, 'formFactory' => $this->formFactory, 'optionFactory' => $this->optionFactory, 'systemStore' => $this->systemStore, 'session' => $this->session, 'viewFileSystem' => $this->viewFileSystem, 'filesystem' => $this->fileSystem]); }
/** * @param int $optionId * @return $this */ public function updateOptionVote($optionId) { $this->_ratingOptionFactory->create()->setOptionId($optionId)->setVoteId($this->getVoteId())->setReviewId($this->getReviewId())->setDoUpdate(1)->addVote(); return $this; }