Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

11873-Move-All-Form-Options-From-Plus-And-Extra-To-Basic #653

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
200 changes: 200 additions & 0 deletions Block/Adminhtml/Post/RelatedProductsRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
<?php
/**
* Copyright © Magefan ([email protected]). All rights reserved.
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
*/

namespace Magefan\Blog\Block\Adminhtml\Post;

class RelatedProductsRule extends \Magento\Backend\Block\Widget\Form\Generic implements
\Magento\Ui\Component\Layout\Tabs\TabInterface
{
/**
* Core registry
*
* @var \Magento\Backend\Block\Widget\Form\Renderer\Fieldset
*/
protected $rendererFieldset;

/**
* @var \Magento\Rule\Block\Conditions
*/
protected $conditions;

/**
* @var string
*/
protected $_nameInLayout = 'conditions_apply_to';

protected $ruleFactory;

public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Framework\Registry $registry,
\Magento\Framework\Data\FormFactory $formFactory,
\Magento\Rule\Block\Conditions $conditions,
\Magento\Backend\Block\Widget\Form\Renderer\Fieldset $rendererFieldset,
\Magento\CatalogRule\Model\RuleFactory $ruleFactory,
array $data = []
) {
$this->rendererFieldset = $rendererFieldset;
$this->conditions = $conditions;
$this->ruleFactory = $ruleFactory;
parent::__construct($context, $registry, $formFactory, $data);
}

/**
* {@inheritdoc}
* @codeCoverageIgnore
*/
public function getTabClass()
{
return null;
}

/**
* {@inheritdoc}
*/
public function getTabUrl()
{
return null;
}

/**
* {@inheritdoc}
*/
public function isAjaxLoaded()
{
return false;
}

/**
* {@inheritdoc}
*/
public function getTabLabel()
{
return __('Conditions');
}

/**
* {@inheritdoc}
*/
public function getTabTitle()
{
return __('Conditions');
}

/**
* {@inheritdoc}
*/
public function canShowTab()
{
return true;
}

/**
* {@inheritdoc}
*/
public function isHidden()
{
return false;
}

/**
* Prepare form before rendering HTML
*
* @return $this
*/
protected function _prepareForm()
{
$model = $this->_coreRegistry->registry(\Magento\SalesRule\Model\RegistryConstants::CURRENT_SALES_RULE);
$form = $this->addTabToForm($model);
$this->setForm($form);

return parent::_prepareForm();
}

/**
* Handles addition of conditions tab to supplied form.
*
* @param \Magento\SalesRule\Model\Rule $model
* @param string $fieldsetId
* @param string $formName
* @return \Magento\Framework\Data\Form
* @throws \Magento\Framework\Exception\LocalizedException
*/
protected function addTabToForm($model, $fieldsetId = 'rp_conditions_serialized_fieldset', $formName = 'blog_post_form')
{

$model = $this->_coreRegistry->registry('current_model');
$rule = $this->ruleFactory->create();
$rule->setData('conditions_serialized', $model->getData('rp_conditions_serialized'));
$model = $rule;
$conditionsFieldSetId = $model->getConditionsFieldSetId($formName);
$newChildUrl = $this->getUrl(
'sales_rule/promo_quote/newConditionHtml/form/' . $conditionsFieldSetId,
['form_namespace' => $formName]
);

/** @var \Magento\Framework\Data\Form $form */
$form = $this->_formFactory->create();
$form->setHtmlIdPrefix('rule_');
$renderer = $this->rendererFieldset->setTemplate(
'Magento_CatalogRule::promo/fieldset.phtml'
)->setNameInLayout(
'rp_condition_block'
)->setNewChildUrl(
$newChildUrl
)->setFieldSetId(
$conditionsFieldSetId
);

$fieldset = $form->addFieldset(
$fieldsetId,
[
'legend' => __(
'Apply the rule only if the following conditions are met (leave blank to disable rule conditions).'
)
]
)->setRenderer(
$renderer
);
$fieldset->addField(
'rp_conditions_serialized',
'text',
[
'name' => 'rp_conditions_serialized',
'label' => __('Conditions'),
'title' => __('Conditions'),
'required' => true,
'data-form-part' => $formName
]
)->setRule(
$model
)->setRenderer(
$this->conditions
);

$form->setValues($model->getData());
$this->setConditionFormName($model->getConditions(), $formName);

return $form;
}

/**
* Handles addition of form name to condition and its conditions.
*
* @param \Magento\Rule\Model\Condition\AbstractCondition $conditions
* @param string $formName
* @return void
*/
private function setConditionFormName(\Magento\Rule\Model\Condition\AbstractCondition $conditions, $formName)
{
$conditions->setFormName($formName);
if ($conditions->getConditions() && is_array($conditions->getConditions())) {
foreach ($conditions->getConditions() as $condition) {
$this->setConditionFormName($condition, $formName);
}
}
}
}
73 changes: 73 additions & 0 deletions Model/Config/Source/Authors.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
/**
* Copyright © Magefan ([email protected]). All rights reserved.
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
*
* Glory to Ukraine! Glory to the heroes!
*/

namespace Magefan\Blog\Model\Config\Source;

/**
* Authors list
*
*/
class Authors implements \Magento\Framework\Option\ArrayInterface
{
/**
* @var \Magento\User\Model\ResourceModel\User\CollectionFactory
*/
protected $authorCollectionFactory;

/**
* @var array
*/
protected $options;

/**
* Initialize dependencies.
*
* @param \Magefan\Blog\Api\AuthorCollectionInterfaceFactory $authorCollectionFactory
* @param void
*/
public function __construct(
\Magefan\Blog\Api\AuthorCollectionInterfaceFactory $authorCollectionFactory
) {
$this->authorCollectionFactory = $authorCollectionFactory;
}

/**
* Options getter
*
* @return array
*/
public function toOptionArray()
{
if ($this->options === null) {
$collection = $this->authorCollectionFactory->create();

foreach ($collection as $item) {
$this->options[] = [
'label' => $item->getName(),
'value' => $item->getId(),
];
}
}

return $this->options;
}

/**
* Get options in "key-value" format
*
* @return array
*/
public function toArray()
{
$array = [];
foreach ($this->toOptionArray() as $item) {
$array[$item['value']] = $item['label'];
}
return $array;
}
}
60 changes: 60 additions & 0 deletions Model/Config/Source/CustomerGroups.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
/**
* Copyright © Magefan ([email protected]). All rights reserved.
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
*/

namespace Magefan\Blog\Model\Config\Source;

/**
* Class CustomerGroup
*/
class CustomerGroups implements \Magento\Framework\Data\OptionSourceInterface
{
/**
* @var \Magento\Customer\Api\GroupRepositoryInterface
*/
private $groupRepository;

/**
* @var \Magento\Framework\Api\SearchCriteriaBuilder
*/
private $searchCriteriaBuilder;

/**
* @var \Magento\Framework\Convert\DataObject
*/
private $objectConverter;

/**
* @param \Magento\Customer\Api\GroupRepositoryInterface $groupRepository
* @param \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder
* @param \Magento\Framework\Convert\DataObject $objectConverter
*/
public function __construct(
\Magento\Customer\Api\GroupRepositoryInterface $groupRepository,
\Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder,
\Magento\Framework\Convert\DataObject $objectConverter
) {
$this->groupRepository = $groupRepository;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->objectConverter = $objectConverter;
}

/**
* @return array
*/
public function toOptionArray()
{
$customerGroups = $this->groupRepository->getList($this->searchCriteriaBuilder->create())->getItems();
$options = $this->objectConverter->toOptionArray($customerGroups, 'id', 'code');
foreach ($options as $key => $option) {
if (!$option['value']) {
$options[$key]['label'] = __('All Groups');
break;
}
}

return $options;
}
}
40 changes: 40 additions & 0 deletions Model/Config/Source/NoYes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* Copyright © Magefan ([email protected]). All rights reserved.
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
*/

namespace Magefan\Blog\Model\Config\Source;

class NoYes implements \Magento\Framework\Option\ArrayInterface
{
const ENABLED = 0;
const DISABLED = 1;

/**
* Options getter
*
* @return array
*/
public function toOptionArray()
{
return [
['value' => self::DISABLED, 'label' => __('No')],
['value' => self::ENABLED, 'label' => __('Yes')],
];
}

/**
* Get options in "key-value" format
*
* @return array
*/
public function toArray()
{
$array = [];
foreach ($this->toOptionArray() as $item) {
$array[$item['value']] = $item['label'];
}
return $array;
}
}
Loading