diff --git a/Api/Data/FeaturetoggleInterface.php b/Api/Data/FeaturetoggleInterface.php index f617a51..095ba80 100644 --- a/Api/Data/FeaturetoggleInterface.php +++ b/Api/Data/FeaturetoggleInterface.php @@ -54,5 +54,12 @@ public function getName(); */ public function setStatus($status); + /** + * Set the status + * + * @api + * + * @return mixed + */ public function getStatus(); } diff --git a/Block/Adminhtml/Edit.php b/Block/Adminhtml/Edit.php new file mode 100644 index 0000000..16d9c37 --- /dev/null +++ b/Block/Adminhtml/Edit.php @@ -0,0 +1,92 @@ +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'); + } + } + +} diff --git a/Block/Adminhtml/Edit/Form.php b/Block/Adminhtml/Edit/Form.php new file mode 100644 index 0000000..35601f7 --- /dev/null +++ b/Block/Adminhtml/Edit/Form.php @@ -0,0 +1,104 @@ +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(); + } +} diff --git a/Controller/Adminhtml/Featuretoggle/Edit.php b/Controller/Adminhtml/Featuretoggle/Edit.php new file mode 100644 index 0000000..2bd76f8 --- /dev/null +++ b/Controller/Adminhtml/Featuretoggle/Edit.php @@ -0,0 +1,51 @@ +_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; + } +} diff --git a/Controller/Adminhtml/Manage.php b/Controller/Adminhtml/Manage.php index 36cea4b..1f43e11 100644 --- a/Controller/Adminhtml/Manage.php +++ b/Controller/Adminhtml/Manage.php @@ -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 { /** @@ -37,6 +41,16 @@ class Manage extends Action */ protected $coreRegistry; + /** + * @var FeaturetoggleInterfaceFactory + */ + protected $featuretoggleFactory; + + /** + * @var FeaturetoggleRepositoryInterface + */ + protected $featuretoggleRepository; + /** * @param Context $context * @param ForwardFactory $resultForwardFactory @@ -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; } /** diff --git a/view/adminhtml/layout/jcowie_featuretoggle_edit.xml b/view/adminhtml/layout/jcowie_featuretoggle_edit.xml new file mode 100644 index 0000000..bbad2a5 --- /dev/null +++ b/view/adminhtml/layout/jcowie_featuretoggle_edit.xml @@ -0,0 +1,8 @@ + + + + + + + +