Skip to content
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
7 changes: 7 additions & 0 deletions Api/Data/FeaturetoggleInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,12 @@ public function getName();
*/
public function setStatus($status);

/**
* Set the status
*
* @api
*
* @return mixed
*/
public function getStatus();
}
92 changes: 92 additions & 0 deletions Block/Adminhtml/Edit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

namespace Jcowie\FeatureToggle\Block\Adminhtml;

use Magento\Backend\Block\Widget\Context;
use Magento\Framework\Registry;
use Jcowie\FeatureToggle\Api\FeaturetoggleRepositoryInterface;

class Edit extends \Magento\Backend\Block\Widget\Form\Container
{
/**
* Core registry
*
* @var Registry
*/
protected $coreRegistry;

/**
* @var FeaturetoggleRepositoryInterface
*/
private $featuretoggleRepository;

/**
* @param Context $context
* @param Registry $registry
* @param FeaturetoggleRepositoryInterface $featuretoggleRepository
* @param array $data
*/
public function __construct(
Context $context,
Registry $registry,
FeaturetoggleRepositoryInterface $featuretoggleRepository,
array $data = []
)
{
$this->coreRegistry = $registry;
$this->featuretoggleRepository = $featuretoggleRepository;
parent::__construct($context, $data);
}

/**
* @return void
*/
protected function _construct()
{
$this->_objectId = 'featuretoggle_id';
$this->_blockGroup = 'Jcowie_Featuretoggle';
$this->_controller = 'adminhtml';
parent::_construct();
$this->buttonList->update('save', 'label', __('Save Feature Toggle'));
$this->buttonList->remove('delete');
}

/**
* Return the feature id.
*
* @return int|null
*/
public function getFeaturetoggleId()
{
$featuretoggleId = $this->coreRegistry->registry('current_featuretoggle_id');
return $featuretoggleId;
}

/**
* Retrieve the save and continue edit Url.
*
* @return string
*/
protected function _getSaveAndContinueUrl()
{
return $this->getUrl(
'jcowie_featuretoggle/featuretoggle/save',
['_current' => true, 'back' => 'edit', 'tab' => '{{tab_id}}']
);
}

/**
* @return \Magento\Framework\Phrase
*/
public function getHeaderText()
{
$currentFeaturetoggleId = $this->coreRegistry->registry('current_featuretoggle_id');
if ($currentFeaturetoggleId) {
$featuretoggleData = $this->featuretoggleRepository->getById($currentFeaturetoggleId);
return __("Edit Feature Toggle '%1'", $featuretoggleData->getName());
} else {
return __('New Feature Toggle');
}
}

}
104 changes: 104 additions & 0 deletions Block/Adminhtml/Edit/Form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

namespace Jcowie\FeatureToggle\Block\Adminhtml\Edit;

use Magento\Backend\Block\Template\Context;
use Magento\Framework\Api\ExtensibleDataObjectConverter;
use Magento\Framework\Data\FormFactory;
use Magento\Framework\Registry;
use Magento\Framework\Object;
use Jcowie\FeatureToggle\Api\FeaturetoggleRepositoryInterface;

class Form extends \Magento\Backend\Block\Widget\Form\Generic
{
/**
* @var FeaturetoggleRepositoryInterface
*/
private $featuretoggleRepository;

/**
* @var ExtensibleDataObjectConverter
*/
private $extensibleDataObjectConverter;

/**
* @param Context $context
* @param Registry $registry
* @param FormFactory $formFactory
* @param featureRepositoryInterface $featuretoggleRepository
* @param array $data
* @param ExtensibleDataObjectConverter $extensibleDataObjectConverter
*/
public function __construct(
Context $context,
Registry $registry,
FormFactory $formFactory,
FeaturetoggleRepositoryInterface $featuretoggleRepository,
array $data = [],
ExtensibleDataObjectConverter $extensibleDataObjectConverter
)
{
$this->featuretoggleRepository = $featuretoggleRepository;
$this->extensibleDataObjectConverter = $extensibleDataObjectConverter;
parent::__construct($context, $registry, $formFactory, $data);
}


/**
* Init Form properties
*
* @return void
*/
protected function _construct()
{
parent::_construct();
$this->setId('featuretoggle_form');
$this->setTitle(__('Feature Toggle Information'));
}

/**
* Prepare form fields
*
* @return $this
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
protected function _prepareForm()
{
/** @var \Magento\Framework\Data\Form $form */
$form = $this->_formFactory->create(
[
'data' => [
'id' => 'edit_form',
'action' => $this->getData('action'),
'method' => 'post'
]
]
);
$featuretoggleId = $this->_coreRegistry->registry('current_featuretoggle_id');
$fieldset = $form->addFieldset('base_fieldset', ['legend' => __('General')]);
$fieldset->addField(
'name',
'text',
[
'name' => 'name',
'label' => __('Name'),
'title' => __('Name'),
'required' => true
]
);

if ($featuretoggleId) {
$fieldset->addField('featuretoggle_id', 'hidden', ['name' => 'featuretoggle_id', 'value' => $featuretoggleId]);
$form->setValues(
$this->extensibleDataObjectConverter->toFlatArray(
$this->featuretoggleRepository->getById($featuretoggleId),
[],
'\Jcowie\FeatureToggle\Api\Data\FeaturetoggleInterface'
)
);
}
$form->setUseContainer(true);
$this->setForm($form);
return parent::_prepareForm();
}
}
51 changes: 51 additions & 0 deletions Controller/Adminhtml/Featuretoggle/Edit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Jcowie\FeatureToggle\Controller\Adminhtml\Featuretoggle;

use Magento\Backend\Model\View\Result\Page;
use Magento\Backend\Model\View\Result\Redirect;
use Magento\Framework\Controller\ResultFactory;
use Jcowie\FeatureToggle\Controller\Adminhtml\Manage;

class Edit extends Manage
{
/**
* @var string
*/
protected $aclAction = 'edit';

/**
* @return Page|Redirect
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function execute()
{
try {
$featuretoggle = $this->_initFeatureToggle();
} catch (\Exception $exception) {
$this->messageManager->addError(__($exception->getMessage()));
/** @var Redirect $resultRedirect */
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
$resultRedirect->setPath('featuretoggle/*');
return $resultRedirect;
}
$data = $this->_objectManager->get('Magento\Backend\Model\Session')->getPageData(true);
if (! empty($data)) {
$featuretoggle->addData($data);
}
$this->coreRegistry->register('current_featuretoggle_id', $featuretoggle->getFeaturetoggleId());
/** @var Page $resultPage */
$resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
$resultPage->getConfig()->getTitle()->prepend(__('Feature Toggle'));
$resultPage->getConfig()->getTitle()->prepend(
$featuretoggle->getId() ? $featuretoggle->getName() : __('New Feature Toggle')
);
$resultPage->getLayout()->getBlock('admin.block.mx_featuretoggle.edit')
->setData('action', $this->getUrl('featuretoggle/index/save'));
$resultPage->addBreadcrumb(
$featuretoggle->getId() ? __('Edit Feature Toggle') : __('New Feature Toggle'),
$featuretoggle->getId() ? __('Edit Feature Toggle') : __('New Feature Toggle')
);
return $resultPage;
}
}
40 changes: 39 additions & 1 deletion Controller/Adminhtml/Manage.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
use Magento\Framework\View\Result\PageFactory;
use Magento\Framework\View\Result\LayoutFactory;

use Jcowie\FeatureToggle\Api\Data\FeaturetoggleInterface;
use Jcowie\FeatureToggle\Api\Data\FeaturetoggleInterfaceFactory;
use Jcowie\FeatureToggle\Api\FeaturetoggleRepositoryInterface;

class Manage extends Action
{
/**
Expand Down Expand Up @@ -37,6 +41,16 @@ class Manage extends Action
*/
protected $coreRegistry;

/**
* @var FeaturetoggleInterfaceFactory
*/
protected $featuretoggleFactory;

/**
* @var FeaturetoggleRepositoryInterface
*/
protected $featuretoggleRepository;

/**
* @param Context $context
* @param ForwardFactory $resultForwardFactory
Expand All @@ -49,13 +63,37 @@ public function __construct(
ForwardFactory $resultForwardFactory,
LayoutFactory $resultLayoutFactory,
PageFactory $resultPageFactory,
Registry $coreRegistry
Registry $coreRegistry,
FeaturetoggleInterfaceFactory $featuretoggleFactory,
FeaturetoggleRepositoryInterface $featuretoggleRepository
) {
parent::__construct($context);
$this->resultPageFactory = $resultPageFactory;
$this->resultForwardFactory = $resultForwardFactory;
$this->resultLayoutFactory = $resultLayoutFactory;
$this->coreRegistry = $coreRegistry;
$this->featuretoggleFactory = $featuretoggleFactory;
$this->featuretoggleRepository = $featuretoggleRepository;
}

/**
* Initialise feature toggle based on request
*
* @return mixed
* @throws NoSuchEntityException
*/
public function _initFeatureToggle()
{
$featuretoggleId = (int)$this->getRequest()->getParam('featuretoggle_id');
if ($featuretoggleId) {
$featuretoggle = $this->featuretoggleRepository->getById($featuretoggleId);
if (!$featuretoggle->getFeaturetoggleId()) {
throw new NoSuchEntityException('This feature does not exist');
}
} else {
$featuretoggle = $this->featuretoggleFactory->create();
}
return $featuretoggle;
}

/**
Expand Down
8 changes: 8 additions & 0 deletions view/adminhtml/layout/jcowie_featuretoggle_edit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<block class="Jcowie\FeatureToggle\Block\Adminhtml\Edit" name="admin.block.jcowie_featuretoggle.edit"/>
</referenceContainer>
</body>
</page>