├── Block
└── Adminhtml
│ ├── Region.php
│ └── Region
│ ├── Edit.php
│ └── Edit
│ └── Form.php
├── Controller
└── Adminhtml
│ ├── Region.php
│ └── Region
│ ├── Delete.php
│ ├── Edit.php
│ ├── Index.php
│ ├── MassDelete.php
│ ├── NewAction.php
│ └── Save.php
├── Model
├── Region.php
├── RegionName.php
└── ResourceModel
│ ├── Region.php
│ ├── Region
│ ├── Collection.php
│ ├── CollectionFactory.php
│ └── Grid
│ │ └── Collection.php
│ ├── RegionName.php
│ └── RegionName
│ └── Collection.php
├── README.md
├── Setup
├── InstallData.php
└── InstallSchema.php
├── Ui
└── Component
│ └── Listing
│ └── Column
│ └── RegionActions.php
├── composer.json
├── etc
├── acl.xml
├── adminhtml
│ ├── menu.xml
│ └── routes.xml
├── di.xml
└── module.xml
├── regions-manager.png
├── registration.php
└── view
├── adminhtml
├── layout
│ ├── phpcuong_region_edit.xml
│ ├── phpcuong_region_index.xml
│ └── phpcuong_region_new.xml
├── templates
│ └── footer
│ │ ├── copyright.phtml
│ │ ├── report.phtml
│ │ └── version.phtml
└── ui_component
│ └── region_listing.xml
└── base
└── web
└── css
└── faq-extension
├── Read Me.txt
├── demo-files
├── demo.css
└── demo.js
├── demo.html
├── fonts
├── faq-extension.eot
├── faq-extension.svg
├── faq-extension.ttf
└── faq-extension.woff
├── ie7
├── ie7.css
└── ie7.js
├── selection.json
└── style.css
/Block/Adminhtml/Region.php:
--------------------------------------------------------------------------------
1 |
6 | * @Date 2016-12-13 00:05:54
7 | * @Last modified by: nquangcuong
8 | * @Last Modified time: 2016-12-13 00:12:38
9 | */
10 |
11 | namespace PHPCuong\Region\Block\Adminhtml;
12 |
13 | /**
14 | * Adminhtml cms blocks content block
15 | */
16 | class Region extends \Magento\Backend\Block\Widget\Grid\Container
17 | {
18 | /**
19 | * @return void
20 | */
21 | protected function _construct()
22 | {
23 | $this->_blockGroup = 'PHPCuong_Region';
24 | $this->_controller = 'adminhtml_region';
25 | $this->_headerText = __('Regions Manager');
26 | $this->_addButtonLabel = __('Add New Region');
27 | parent::_construct();
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/Block/Adminhtml/Region/Edit.php:
--------------------------------------------------------------------------------
1 |
6 | * @Date 2016-12-13 00:44:50
7 | * @Last modified by: nquangcuong
8 | * @Last Modified time: 2016-12-13 00:50:31
9 | */
10 |
11 | namespace PHPCuong\Region\Block\Adminhtml\Region;
12 |
13 | use Magento\Backend\Block\Widget\Form\Container;
14 |
15 | class Edit extends Container
16 | {
17 | /**
18 | * Core registry
19 | *
20 | * @var \Magento\Framework\Registry
21 | */
22 | protected $_coreRegistry = null;
23 |
24 | /**
25 | * @param \Magento\Backend\Block\Widget\Context $context
26 | * @param \Magento\Framework\Registry $registry
27 | * @param array $data
28 | */
29 | public function __construct(
30 | \Magento\Backend\Block\Widget\Context $context,
31 | \Magento\Framework\Registry $registry,
32 | array $data = []
33 | ) {
34 | $this->_coreRegistry = $registry;
35 | parent::__construct($context, $data);
36 | }
37 |
38 | /**
39 | * Department edit block
40 | *
41 | * @return void
42 | */
43 | protected function _construct()
44 | {
45 | $this->_objectId = 'phpcuong_region_id';
46 | $this->_blockGroup = 'PHPCuong_Region';
47 | $this->_controller = 'adminhtml_region';
48 | parent::_construct();
49 |
50 | if ($this->_isAllowedAction('PHPCuong_Region::region_edit')) {
51 | $this->buttonList->update('save', 'label', __('Save Region'));
52 | $this->buttonList->add(
53 | 'saveandcontinue',
54 | [
55 | 'label' => __('Save and Continue Edit'),
56 | 'class' => 'save',
57 | 'data_attribute' => [
58 | 'mage-init' => [
59 | 'button' => [
60 | 'event' => 'saveAndContinueEdit',
61 | 'target' => '#edit_form'
62 | ],
63 | ],
64 | ]
65 | ],
66 | -100
67 | );
68 |
69 | $region = $this->_coreRegistry->registry('phpcuong_region');
70 | if(!empty($region)) {
71 | if ($region->getRegionId()) {
72 | $this->buttonList->add(
73 | 'delete',
74 | [
75 | 'label' => __('Delete'),
76 | 'class' => 'delete',
77 | 'onclick' => 'deleteConfirm("Are you sure you want to delete this Region?", "'.$this->getDeleteUrl().'")'
78 | ],
79 | -100
80 | );
81 | }
82 | }
83 | } else {
84 | $this->buttonList->remove('save');
85 | }
86 | }
87 |
88 | /**
89 | * Check permission for passed action
90 | *
91 | * @param string $resourceId
92 | * @return bool
93 | */
94 | protected function _isAllowedAction($resourceId)
95 | {
96 | return $this->_authorization->isAllowed($resourceId);
97 | }
98 |
99 | /**
100 | * Retrieve delete Url.
101 | *
102 | * @return string
103 | */
104 | public function getDeleteUrl()
105 | {
106 | return $this->getUrl(
107 | '*/*/delete',
108 | [
109 | '_current' => true,
110 | 'id' => $this->getRequest()->getParam('region_id')
111 | ]
112 | );
113 | }
114 |
115 | /**
116 | * Getter of url for "Save and Continue" button
117 | * tab_id will be replaced by desired by JS later
118 | *
119 | * @return string
120 | */
121 | protected function _getSaveAndContinueUrl()
122 | {
123 | return $this->getUrl(
124 | '*/*/save',
125 | [
126 | '_current' => true,
127 | 'back' => 'edit',
128 | 'active_tab' => ''
129 | ]
130 | );
131 | }
132 | }
133 |
--------------------------------------------------------------------------------
/Block/Adminhtml/Region/Edit/Form.php:
--------------------------------------------------------------------------------
1 |
6 | * @Date 2016-12-13 00:49:46
7 | * @Last modified by: nquangcuong
8 | * @Last Modified time: 2016-12-13 15:45:47
9 | */
10 |
11 | namespace PHPCuong\Region\Block\Adminhtml\Region\Edit;
12 |
13 | use \Magento\Backend\Block\Widget\Form\Generic;
14 |
15 | class Form extends Generic
16 | {
17 | protected $_coreRegistry = null;
18 | /**
19 | * @param \Magento\Backend\Block\Template\Context $context
20 | * @param \Magento\Framework\Registry $registry
21 | * @param \Magento\Framework\Data\FormFactory $formFactory
22 | * @param array $data
23 | */
24 | public function __construct(
25 | \Magento\Backend\Block\Template\Context $context,
26 | \Magento\Framework\Registry $registry,
27 | \Magento\Framework\Data\FormFactory $formFactory,
28 | array $data = []
29 | ) {
30 | $this->_coreRegistry = $registry;
31 | parent::__construct($context, $registry, $formFactory, $data);
32 | }
33 |
34 | /**
35 | * Init form
36 | *
37 | * @return void
38 | */
39 | protected function _construct()
40 | {
41 | parent::_construct();
42 | $this->setId('region_form');
43 | $this->setTitle(__('Region Information'));
44 | }
45 |
46 | /**
47 | * Prepare form
48 | *
49 | * @return $this
50 | */
51 | protected function _prepareForm()
52 | {
53 | $form = $this->_formFactory->create(
54 | [
55 | 'data' => [
56 | 'id' => 'edit_form',
57 | 'action' => $this->getData('action'),
58 | 'method' => 'post'
59 | ]
60 | ]
61 | );
62 |
63 | $form->setHtmlIdPrefix('region_');
64 |
65 | $fieldset = $form->addFieldset(
66 | 'base_fieldset',
67 | [
68 | 'legend' => __('General Information'),
69 | 'class' => 'fieldset-wide'
70 | ]
71 | );
72 |
73 | $country_list = $this->_coreRegistry->registry('phpcuong_region_country_list');
74 |
75 | $fieldset->addField(
76 | 'country_id',
77 | 'select',
78 | [
79 | 'name' => 'country_id',
80 | 'label' => __('Country'),
81 | 'title' => __('Country'),
82 | 'required' => true,
83 | 'values' => $country_list
84 | ]
85 | );
86 |
87 | $fieldset->addField(
88 | 'code',
89 | 'text',
90 | [
91 | 'name' => 'code',
92 | 'label' => __('State/Region/Province Code'),
93 | 'title' => __('State/Region/Province Code'),
94 | 'required' => true
95 | ]
96 | );
97 |
98 | $fieldset->addField(
99 | 'default_name',
100 | 'text',
101 | [
102 | 'name' => 'default_name',
103 | 'label' => __('State/Region/Province Name'),
104 | 'title' => __('State/Region/Province Name'),
105 | 'required' => true
106 | ]
107 | );
108 |
109 | $formData = $this->_coreRegistry->registry('phpcuong_region');
110 | if ($formData) {
111 | if ($formData->getRegionId()) {
112 | $fieldset->addField(
113 | 'region_id',
114 | 'hidden',
115 | ['name' => 'region_id']
116 | );
117 | }
118 | $form->setValues($formData->getData());
119 | }
120 |
121 | $form->setUseContainer(true);
122 | $form->setMethod('post');
123 | $this->setForm($form);
124 |
125 | return parent::_prepareForm();
126 | }
127 | }
128 |
--------------------------------------------------------------------------------
/Controller/Adminhtml/Region.php:
--------------------------------------------------------------------------------
1 |
6 | * @Date 2016-12-11 20:57:08
7 | * @Last modified by: nquangcuong
8 | * @Last Modified time: 2016-12-12 22:10:30
9 | */
10 |
11 | namespace PHPCuong\Region\Controller\Adminhtml;
12 |
13 | abstract class Region extends \Magento\Backend\App\Action
14 | {
15 | /**
16 | * Mass Action Filter
17 | *
18 | * @var \Magento\Ui\Component\MassAction\Filter
19 | */
20 | protected $_filter;
21 | /**
22 | * Collection Factory
23 | *
24 | * @var PHPCuong\Region\Model\ResourceModel\Region\CollectionFactory
25 | */
26 | protected $_collectionFactory;
27 |
28 | protected $resultPageFactory;
29 |
30 | public function __construct(
31 | \Magento\Backend\App\Action\Context $context,
32 | \Magento\Framework\View\Result\PageFactory $resultPageFactory
33 | ) {
34 | $this->resultPageFactory = $resultPageFactory;
35 | parent::__construct($context);
36 | }
37 |
38 | /**
39 | * @return $this
40 | */
41 | protected function _initAction()
42 | {
43 | $resultPage = $this->resultPageFactory->create();
44 | return $resultPage;
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/Controller/Adminhtml/Region/Delete.php:
--------------------------------------------------------------------------------
1 |
6 | * @Date 2016-12-12 21:47:12
7 | * @Last modified by: nquangcuong
8 | * @Last Modified time: 2016-12-12 22:22:47
9 | */
10 |
11 | namespace PHPCuong\Region\Controller\Adminhtml\Region;
12 |
13 | class Delete extends \Magento\Backend\App\Action
14 | {
15 | /**
16 | * Authorization level of a basic admin session
17 | *
18 | * @see _isAllowed()
19 | */
20 | const ADMIN_RESOURCE = 'PHPCuong_Region::region_delete';
21 | /**
22 | *
23 | * @return \Magento\Framework\View\Result\PageFactory
24 | */
25 | public function execute()
26 | {
27 | // check if we know what should be deleted
28 | $region_id = $this->getRequest()->getParam('region_id');
29 | /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
30 | $resultRedirect = $this->resultRedirectFactory->create();
31 | if ($region_id) {
32 | $region_name = '';
33 | try {
34 | // init model and delete
35 | $model = $this->_objectManager->create('PHPCuong\Region\Model\Region');
36 | $model->load($region_id);
37 | $region_name = $model->getDefaultName();
38 | $model->delete();
39 | $this->messageManager->addSuccess(__('The '.$region_name.' Region has been deleted.'));
40 | return $resultRedirect->setPath('*/*/');
41 | } catch (\Exception $e) {
42 | // display error message
43 | $this->messageManager->addError($e->getMessage());
44 | // go back to edit form
45 | return $resultRedirect->setPath('*/*/edit', ['region_id' => $region_id]);
46 | }
47 | }
48 | // display error message
49 | $this->messageManager->addError(__('Region to delete was not found.'));
50 | // go to grid
51 | return $resultRedirect->setPath('*/*/');
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/Controller/Adminhtml/Region/Edit.php:
--------------------------------------------------------------------------------
1 |
6 | * @Date 2016-12-12 22:29:31
7 | * @Last modified by: nquangcuong
8 | * @Last Modified time: 2016-12-13 15:12:07
9 | */
10 |
11 | namespace PHPCuong\Region\Controller\Adminhtml\Region;
12 |
13 | use Magento\Backend\App\Action;
14 |
15 | class Edit extends \Magento\Backend\App\Action
16 | {
17 | /**
18 | * Authorization level of a basic admin session
19 | *
20 | * @see _isAllowed()
21 | */
22 | const ADMIN_RESOURCE = 'PHPCuong_Region::region_edit';
23 |
24 | /**
25 | * Core registry
26 | *
27 | * @var \Magento\Framework\Registry
28 | */
29 | protected $_coreRegistry;
30 |
31 | /**
32 | * @var \Magento\Framework\View\Result\PageFactory
33 | */
34 | protected $resultPageFactory;
35 |
36 | /**
37 | * @param Action\Context $context
38 | * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
39 | * @param \Magento\Framework\Registry $registry
40 | */
41 | public function __construct(
42 | Action\Context $context,
43 | \Magento\Framework\View\Result\PageFactory $resultPageFactory,
44 | \Magento\Framework\Registry $registry
45 | ) {
46 | $this->resultPageFactory = $resultPageFactory;
47 | $this->_coreRegistry = $registry;
48 | parent::__construct($context);
49 | }
50 |
51 | /**
52 | * Init actions
53 | *
54 | * @return \Magento\Backend\Model\View\Result\Page
55 | */
56 | protected function _initAction()
57 | {
58 | // load layout, set active menu
59 | /** @var \Magento\Backend\Model\View\Result\Page $resultPage */
60 | $resultPage = $this->resultPageFactory->create();
61 | $resultPage->setActiveMenu('Magento_Customer::customer');
62 | return $resultPage;
63 | }
64 |
65 | /**
66 | * Edit Region page
67 | *
68 | * @return \Magento\Backend\Model\View\Result\Page|\Magento\Backend\Model\View\Result\Redirect
69 | * @SuppressWarnings(PHPMD.NPathComplexity)
70 | */
71 | public function execute()
72 | {
73 | // Get ID and create model
74 | $id = (int) $this->getRequest()->getParam('region_id');
75 | $model = $this->_objectManager->create('PHPCuong\Region\Model\Region');
76 | $model->setData([]);
77 | // Initial checking
78 | if ($id && $id > 0) {
79 | $model->load($id);
80 | if (!$model->getRegionId()) {
81 | $this->messageManager->addError(__('This region no longer exists.'));
82 | /** \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
83 | $resultRedirect = $this->resultRedirectFactory->create();
84 | return $resultRedirect->setPath('*/*/');
85 | }
86 | $default_name = $model->getDefaultName();
87 | }
88 |
89 | $FormData = $this->_objectManager->get('Magento\Backend\Model\Session')->getFormData(true);
90 | if (!empty($FormData)) {
91 | $model->setData($FormData);
92 | }
93 |
94 | $this->_coreRegistry->register('phpcuong_region', $model);
95 |
96 | $countryHelper = $this->_objectManager->get('Magento\Directory\Model\Config\Source\Country');
97 |
98 | $this->_coreRegistry->register('phpcuong_region_country_list', $countryHelper->toOptionArray());
99 |
100 | // Build edit form
101 | /** @var \Magento\Backend\Model\View\Result\Page $resultPage */
102 | $resultPage = $this->_initAction();
103 | $resultPage->addBreadcrumb(
104 | $id ? __('Edit Region') : __('New Region'),
105 | $id ? __('Edit Region') : __('New Region')
106 | );
107 | $resultPage->getConfig()->getTitle()->prepend(__('Regions Manager'));
108 | $resultPage->getConfig()->getTitle()
109 | ->prepend($id ? 'Edit: '.$default_name.' ('.$id.')' : __('New Region'));
110 |
111 | return $resultPage;
112 | }
113 | }
114 |
--------------------------------------------------------------------------------
/Controller/Adminhtml/Region/Index.php:
--------------------------------------------------------------------------------
1 |
6 | * @Date 2016-12-11 21:02:32
7 | * @Last modified by: nquangcuong
8 | * @Last Modified time: 2016-12-13 16:00:34
9 | */
10 |
11 | namespace PHPCuong\Region\Controller\Adminhtml\Region;
12 |
13 | class Index extends \PHPCuong\Region\Controller\Adminhtml\Region
14 | {
15 | /**
16 | * @return \Magento\Framework\View\Result\PageFactory
17 | */
18 | public function execute()
19 | {
20 | $resultPage = $this->_initAction();
21 | $resultPage->addBreadcrumb(
22 | 'Regions Manager',
23 | 'Regions Manager'
24 | );
25 | $resultPage->getConfig()->getTitle()->prepend(__('Customers'));
26 | $resultPage->getConfig()->getTitle()
27 | ->prepend('Regions Manager');
28 | return $resultPage;
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/Controller/Adminhtml/Region/MassDelete.php:
--------------------------------------------------------------------------------
1 |
6 | * @Date 2016-12-12 01:12:57
7 | * @Last modified by: nquangcuong
8 | * @Last Modified time: 2016-12-15 23:30:16
9 | */
10 |
11 | namespace PHPCuong\Region\Controller\Adminhtml\Region;
12 |
13 | use Magento\Framework\Controller\ResultFactory;
14 | use Magento\Backend\App\Action\Context;
15 | use Magento\Ui\Component\MassAction\Filter;
16 | use PHPCuong\Region\Model\ResourceModel\Region\CollectionFactory;
17 |
18 | class MassDelete extends \Magento\Backend\App\Action
19 | {
20 | /**
21 | * Authorization level of a basic admin session
22 | *
23 | * @see _isAllowed()
24 | */
25 | const ADMIN_RESOURCE = 'PHPCuong_Region::region_delete';
26 | /**
27 | * @var Filter
28 | */
29 | protected $filter;
30 |
31 | /**
32 | * @var CollectionFactory
33 | */
34 | protected $collectionFactory;
35 |
36 | /**
37 | * @param Context $context
38 | * @param Filter $filter
39 | * @param CollectionFactory $collectionFactory
40 | */
41 | public function __construct(Context $context, Filter $filter, CollectionFactory $collectionFactory)
42 | {
43 | $this->filter = $filter;
44 | $this->collectionFactory = $collectionFactory;
45 | parent::__construct($context);
46 | }
47 |
48 | /**
49 | * Execute action
50 | *
51 | * @return \Magento\Backend\Model\View\Result\Redirect
52 | * @throws \Magento\Framework\Exception\LocalizedException|\Exception
53 | */
54 | public function execute()
55 | {
56 | $collection = $this->filter->getCollection($this->collectionFactory->create());
57 | $collectionSize = $collection->getSize();
58 |
59 | foreach ($collection as $page) {
60 | $page->delete();
61 | }
62 |
63 | $this->messageManager->addSuccess(__('A total of %1 record(s) have been deleted.', $collectionSize));
64 |
65 | /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
66 | $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
67 | return $resultRedirect->setPath('*/*/');
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/Controller/Adminhtml/Region/NewAction.php:
--------------------------------------------------------------------------------
1 |
6 | * @Date 2016-12-13 01:08:32
7 | * @Last modified by: nquangcuong
8 | * @Last Modified time: 2017-11-11 23:02:48
9 | */
10 |
11 | namespace PHPCuong\Region\Controller\Adminhtml\Region;
12 |
13 | class NewAction extends \Magento\Backend\App\Action
14 | {
15 | protected $coreRegistry = null;
16 | /**
17 | * Authorization level of a basic admin session
18 | *
19 | * @see _isAllowed()
20 | */
21 | const ADMIN_RESOURCE = 'PHPCuong_Region::region_create';
22 |
23 | /**
24 | * @var \Magento\Backend\Model\View\Result\ForwardFactory
25 | */
26 | protected $resultPageFactory;
27 |
28 | /**
29 | * @param \Magento\Backend\App\Action\Context $context
30 | * @param \Magento\Framework\Registry $coreRegistry
31 | * @param \Magento\Backend\Model\View\Result\ForwardFactory $resultForwardFactory
32 | */
33 | public function __construct(
34 | \Magento\Backend\App\Action\Context $context,
35 | \Magento\Framework\Registry $coreRegistry,
36 | \Magento\Framework\View\Result\PageFactory $resultPageFactory
37 | ) {
38 | $this->coreRegistry = $coreRegistry;
39 | $this->resultPageFactory = $resultPageFactory;
40 | parent::__construct($context);
41 | }
42 |
43 | /**
44 | * Create new Region
45 | *
46 | * @return \Magento\Framework\Controller\ResultInterface
47 | */
48 | public function execute()
49 | {
50 | /** @var \Magento\Framework\Controller\Result\Forward $resultForward */
51 | $resultPageFactory = $this->resultPageFactory->create();
52 |
53 | $FormData = $this->_objectManager->get('Magento\Backend\Model\Session')->getFormData(true);
54 | if (!empty($FormData)) {
55 | $model = $this->_objectManager->create('PHPCuong\Region\Model\Region');
56 | $model->setData($FormData);
57 | $this->coreRegistry->register('phpcuong_region', $model);
58 | }
59 |
60 | $countryHelper = $this->_objectManager->get('Magento\Directory\Model\Config\Source\Country');
61 |
62 | $this->coreRegistry->register('phpcuong_region_country_list', $countryHelper->toOptionArray());
63 |
64 | return $resultPageFactory;
65 | }
66 | }
67 |
--------------------------------------------------------------------------------
/Controller/Adminhtml/Region/Save.php:
--------------------------------------------------------------------------------
1 |
6 | * @Date 2016-12-13 01:36:35
7 | * @Last modified by: nquangcuong
8 | * @Last Modified time: 2016-12-13 14:29:04
9 | */
10 |
11 | namespace PHPCuong\Region\Controller\Adminhtml\Region;
12 |
13 | use Magento\Backend\App\Action\Context;
14 | use Magento\Framework\Exception\LocalizedException;
15 | use Magento\TestFramework\Inspection\Exception;
16 |
17 | class Save extends \Magento\Backend\App\Action
18 | {
19 | /**
20 | * Core registry
21 | *
22 | * @var \Magento\Framework\Registry
23 | */
24 | protected $_coreRegistry;
25 | /**
26 | * @param Context $context
27 | * @param \Magento\Framework\Registry $coreRegistry
28 | * @param DataPersistorInterface $dataPersistor
29 | */
30 | public function __construct(
31 | Context $context,
32 | \Magento\Framework\Registry $coreRegistry
33 | ) {
34 | $this->_coreRegistry = $coreRegistry;
35 | parent::__construct($context);
36 | }
37 |
38 | /**
39 | * Save action
40 | *
41 | * @SuppressWarnings(PHPMD.CyclomaticComplexity)
42 | * @return \Magento\Framework\Controller\ResultInterface
43 | */
44 | public function execute()
45 | {
46 | /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
47 | $resultRedirect = $this->resultRedirectFactory->create();
48 |
49 | $data = $this->getRequest()->getPostValue();
50 | if ($data) {
51 |
52 | $id = $this->getRequest()->getParam('region_id');
53 |
54 | /** @var \PHPCuong\Region\Model\Region $model */
55 | $model = $this->_objectManager->create('PHPCuong\Region\Model\Region')->load($id);
56 | if (!$model->getRegionId() && $id) {
57 | $this->messageManager->addError(__('This region no longer exists.'));
58 | return $resultRedirect->setPath('*/*/');
59 | }
60 |
61 | $model->setData($data);
62 |
63 | try {
64 | $model->save();
65 | $this->messageManager->addSuccess(__('You saved the region.'));
66 |
67 | if ($this->getRequest()->getParam('back')) {
68 | return $resultRedirect->setPath('*/*/edit', ['region_id' => $model->getRegionId()]);
69 | }
70 | return $resultRedirect->setPath('*/*/');
71 | } catch (LocalizedException $e) {
72 | $this->messageManager->addError($e->getMessage());
73 | } catch (\Exception $e) {
74 | $this->messageManager->addException($e, __('Something went wrong while saving the region.'));
75 | }
76 |
77 | $this->_getSession()->setFormData($data);
78 | if ($this->getRequest()->getParam('region_id')) {
79 | return $resultRedirect->setPath('*/*/edit', ['region_id' => $this->getRequest()->getParam('region_id')]);
80 | }
81 | return $resultRedirect->setPath('*/*/new');
82 | }
83 | return $resultRedirect->setPath('*/*/');
84 | }
85 | }
86 |
--------------------------------------------------------------------------------
/Model/Region.php:
--------------------------------------------------------------------------------
1 |
6 | * @Date 2016-12-11 23:42:05
7 | * @Last modified by: nquangcuong
8 | * @Last Modified time: 2016-12-12 19:10:27
9 | */
10 |
11 | namespace PHPCuong\Region\Model;
12 |
13 | class Region extends \Magento\Framework\Model\AbstractModel
14 | {
15 | /**
16 | * Cache tag
17 | *
18 | * @var string
19 | */
20 | const CACHE_TAG = 'directory_country_region';
21 | /**
22 | * Initialize resource model
23 | *
24 | * @return void
25 | */
26 | protected function _construct()
27 | {
28 | $this->_init('PHPCuong\Region\Model\ResourceModel\Region');
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/Model/RegionName.php:
--------------------------------------------------------------------------------
1 |
6 | * @Date 2016-12-13 18:23:48
7 | * @Last modified by: nquangcuong
8 | * @Last Modified time: 2016-12-13 18:25:24
9 | */
10 |
11 | namespace PHPCuong\Region\Model;
12 |
13 | class RegionName extends \Magento\Framework\Model\AbstractModel
14 | {
15 | /**
16 | * Cache tag
17 | *
18 | * @var string
19 | */
20 | const CACHE_TAG = 'directory_country_region_name';
21 | /**
22 | * Initialize resource model
23 | *
24 | * @return void
25 | */
26 | protected function _construct()
27 | {
28 | $this->_init('PHPCuong\Region\Model\ResourceModel\RegionName');
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/Model/ResourceModel/Region.php:
--------------------------------------------------------------------------------
1 |
6 | * @Date 2016-12-11 23:45:27
7 | * @Last modified by: nquangcuong
8 | * @Last Modified time: 2016-12-13 17:43:20
9 | */
10 |
11 | namespace PHPCuong\Region\Model\ResourceModel;
12 |
13 | use Magento\Framework\Exception\LocalizedException;
14 | use Magento\Framework\Model\AbstractModel;
15 |
16 | class Region extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
17 | {
18 | /**
19 | * construct
20 | * @return void
21 | */
22 | protected function _construct()
23 | {
24 | $this->_init('directory_country_region', 'region_id');
25 | }
26 |
27 | /**
28 | * Perform operations before object save
29 | *
30 | * @param AbstractModel $object
31 | * @return $this
32 | * @throws LocalizedException
33 | */
34 | protected function _beforeSave(AbstractModel $object)
35 | {
36 | if (!$this->checkRegionAlreadyExist($object)) {
37 | throw new LocalizedException(
38 | __('The region already exists.')
39 | );
40 | }
41 | }
42 |
43 | protected function checkSubRegionAlreadyExist(AbstractModel $object, $field)
44 | {
45 | $select = $this->getConnection()->select()
46 | ->from(['dcr' => $this->getMainTable()])
47 | ->where('dcr.country_id = ?', $object->getData('country_id'))
48 | ->where('dcr.'.$field.' = ?', $object->getData($field));
49 | if ($object->getData('region_id')) {
50 | $select->where('dcr.region_id <> ?', $object->getData('region_id'));
51 | }
52 | if ($this->getConnection()->fetchRow($select)) {
53 | return false;
54 | }
55 | return true;
56 | }
57 |
58 | protected function checkRegionAlreadyExist(AbstractModel $object)
59 | {
60 | if (!$this->checkSubRegionAlreadyExist($object, 'default_name') || !$this->checkSubRegionAlreadyExist($object, 'code')) {
61 | return false;
62 | }
63 | return true;
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/Model/ResourceModel/Region/Collection.php:
--------------------------------------------------------------------------------
1 |
6 | * @Date 2016-12-11 23:48:21
7 | * @Last modified by: nquangcuong
8 | * @Last Modified time: 2016-12-12 17:58:16
9 | */
10 |
11 | namespace PHPCuong\Region\Model\ResourceModel\Region;
12 |
13 | class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
14 | {
15 | protected $_idFieldName = 'region_id';
16 | /**
17 | * Define resource model.
18 | */
19 | protected function _construct()
20 | {
21 | $this->_init('PHPCuong\Region\Model\Region', 'PHPCuong\Region\Model\ResourceModel\Region');
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/Model/ResourceModel/Region/CollectionFactory.php:
--------------------------------------------------------------------------------
1 |
6 | * @Date 2016-12-12 02:07:14
7 | * @Last modified by: nquangcuong
8 | * @Last Modified time: 2016-12-13 04:52:51
9 | */
10 |
11 | namespace PHPCuong\Region\Model\ResourceModel\Region;
12 |
13 | class CollectionFactory {
14 | /**
15 | * Object Manager instance
16 | *
17 | * @var \Magento\Framework\ObjectManagerInterface
18 | */
19 | protected $_objectManager = null;
20 |
21 | /**
22 | * Instance name to create
23 | *
24 | * @var string
25 | */
26 | protected $_instanceName = null;
27 |
28 | /**
29 | * Factory constructor
30 | *
31 | * @param \Magento\Framework\ObjectManagerInterface $objectManager
32 | * @param string $instanceName
33 | */
34 | public function __construct(\Magento\Framework\ObjectManagerInterface $objectManager, $instanceName = '\\PHPCuong\\Region\\Model\\ResourceModel\\Region\\Collection')
35 | {
36 | $this->_objectManager = $objectManager;
37 | $this->_instanceName = $instanceName;
38 | }
39 |
40 | /**
41 | * Create class instance with specified parameters
42 | *
43 | * @param array $data
44 | * @return \PHPCuong\Region\Model\ResourceModel\Region\Collection
45 | */
46 | public function create(array $data = array())
47 | {
48 | return $this->_objectManager->create($this->_instanceName, $data);
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/Model/ResourceModel/Region/Grid/Collection.php:
--------------------------------------------------------------------------------
1 |
6 | * @Date 2016-12-11 23:53:28
7 | * @Last modified by: nquangcuong
8 | * @Last Modified time: 2017-11-22 03:03:03
9 | */
10 |
11 | namespace PHPCuong\Region\Model\ResourceModel\Region\Grid;
12 |
13 | class Collection extends \PHPCuong\Region\Model\ResourceModel\Region\Collection implements \Magento\Framework\Api\Search\SearchResultInterface
14 | {
15 | /**
16 | * Aggregations
17 | *
18 | * @var \Magento\Framework\Search\AggregationInterface
19 | */
20 | protected $_aggregations;
21 |
22 | /**
23 | * constructor
24 | *
25 | * @param \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory
26 | * @param \Psr\Log\LoggerInterface $logger
27 | * @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
28 | * @param \Magento\Framework\Event\ManagerInterface $eventManager
29 | * @param $mainTable
30 | * @param $eventPrefix
31 | * @param $eventObject
32 | * @param $resourceModel
33 | * @param $model
34 | * @param $connection
35 | * @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource
36 | */
37 | public function __construct(
38 | \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory,
39 | \Psr\Log\LoggerInterface $logger,
40 | \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
41 | \Magento\Framework\Event\ManagerInterface $eventManager,
42 | $mainTable = 'directory_country_region',
43 | $eventPrefix,
44 | $eventObject,
45 | $resourceModel,
46 | $model = 'Magento\Framework\View\Element\UiComponent\DataProvider\Document',
47 | \Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
48 | \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
49 | )
50 | {
51 | parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
52 | $this->_eventPrefix = $eventPrefix;
53 | $this->_eventObject = $eventObject;
54 | $this->_init($model, $resourceModel);
55 | $this->setMainTable($mainTable);
56 | }
57 |
58 |
59 | /**
60 | * @return \Magento\Framework\Search\AggregationInterface
61 | */
62 | public function getAggregations()
63 | {
64 | return $this->_aggregations;
65 | }
66 |
67 | /**
68 | * @param \Magento\Framework\Search\AggregationInterface $aggregations
69 | * @return $this
70 | */
71 | public function setAggregations($aggregations)
72 | {
73 | $this->_aggregations = $aggregations;
74 | }
75 |
76 |
77 | /**
78 | * Retrieve all ids for collection
79 | * Backward compatibility with EAV collection
80 | *
81 | * @param int $limit
82 | * @param int $offset
83 | * @return array
84 | */
85 | public function getAllIds($limit = null, $offset = null)
86 | {
87 | return $this->getConnection()->fetchCol($this->_getAllIdsSelect($limit, $offset), $this->_bindParams);
88 | }
89 |
90 | /**
91 | * Get search criteria.
92 | *
93 | * @return \Magento\Framework\Api\SearchCriteriaInterface|null
94 | */
95 | public function getSearchCriteria()
96 | {
97 | return null;
98 | }
99 |
100 | /**
101 | * Set search criteria.
102 | *
103 | * @param \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria
104 | * @return $this
105 | * @SuppressWarnings(PHPMD.UnusedFormalParameter)
106 | */
107 | public function setSearchCriteria(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria = null)
108 | {
109 | return $this;
110 | }
111 |
112 | /**
113 | * Get total count.
114 | *
115 | * @return int
116 | */
117 | public function getTotalCount()
118 | {
119 | return $this->getSize();
120 | }
121 |
122 | /**
123 | * Set total count.
124 | *
125 | * @param int $totalCount
126 | * @return $this
127 | * @SuppressWarnings(PHPMD.UnusedFormalParameter)
128 | */
129 | public function setTotalCount($totalCount)
130 | {
131 | return $this;
132 | }
133 |
134 | /**
135 | * Set items list.
136 | *
137 | * @param \Magento\Framework\Api\ExtensibleDataInterface[] $items
138 | * @return $this
139 | * @SuppressWarnings(PHPMD.UnusedFormalParameter)
140 | */
141 | public function setItems(array $items = null)
142 | {
143 | return $this;
144 | }
145 | }
146 |
--------------------------------------------------------------------------------
/Model/ResourceModel/RegionName.php:
--------------------------------------------------------------------------------
1 |
6 | * @Date 2016-12-13 18:24:39
7 | * @Last modified by: nquangcuong
8 | * @Last Modified time: 2016-12-13 18:26:26
9 | */
10 |
11 | namespace PHPCuong\Region\Model\ResourceModel;
12 |
13 | use Magento\Framework\Exception\LocalizedException;
14 | use Magento\Framework\Model\AbstractModel;
15 |
16 | class RegionName extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
17 | {
18 | /**
19 | * construct
20 | * @return void
21 | */
22 | protected function _construct()
23 | {
24 | $this->_init('directory_country_region_name', 'region_id');
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/Model/ResourceModel/RegionName/Collection.php:
--------------------------------------------------------------------------------
1 |
6 | * @Date 2016-12-13 18:27:07
7 | * @Last modified by: nquangcuong
8 | * @Last Modified time: 2016-12-13 18:27:36
9 | */
10 |
11 | namespace PHPCuong\Region\Model\ResourceModel\RegionName;
12 |
13 | class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
14 | {
15 | protected $_idFieldName = 'region_id';
16 | /**
17 | * Define resource model.
18 | */
19 | protected function _construct()
20 | {
21 | $this->_init('PHPCuong\Region\Model\RegionName', 'PHPCuong\Region\Model\ResourceModel\RegionName');
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ## Manage regions in Magento2 addresses
2 | Magento2 provides country and regions and cities as dropdown option in addresses This module provides flexibility of managing this regions for each country.
3 |
4 | Very Easy to use
5 |
6 | Test and working with Magento CE 2.x and Magento EE 2.x
7 |
8 | ### Benefits
9 | This module provides flexibility of managing the regions for each country in your Magento Admin.
10 |
11 | #### 1 - Installation Regions Manager
12 | ##### Manual Installation
13 | Install Regions Manager for Magento2
14 | * Download the extension
15 | * Unzip the file
16 | * Create a folder {Magento root}/app/code/PHPCuong/Region
17 | * Copy the content from the unzip folder
18 |
19 |
20 | ##### Using Composer
21 |
22 | ```
23 | composer require php-cuong/magento2-regions-manager
24 | ```
25 |
26 | #### 2 - Enable Regions Manager
27 | * php bin/magento module:enable PHPCuong_Region
28 | * php bin/magento setup:upgrade
29 | * php bin/magento cache:flush
30 | * php bin/magento setup:static-content:deploy
31 |
32 | #### 3 - See results
33 | Log into your Magento admin, goto Customers -> Manage Regions in Addresses
34 |
35 | 
36 |
37 | #### See the Regions Manager Pro Extension here
38 | https://github.com/php-cuong/magento2-city-dropdown
39 |
40 | Extension link: https://www.giaphugroup.com/magento-2-city-dropdown-extension.html
41 |
42 | ### Donations
43 | Please donate if you enjoy my extension.
44 |
45 | [](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=RUYCWQ3Q9YGH2)
46 |
47 |
--------------------------------------------------------------------------------
/Setup/InstallData.php:
--------------------------------------------------------------------------------
1 |
6 | * @Date 2016-12-12 03:46:38
7 | * @Last modified by: nquangcuong
8 | * @Last Modified time: 2016-12-13 18:02:07
9 | */
10 |
11 | namespace PHPCuong\Region\Setup;
12 |
13 | use Magento\Framework\Setup\InstallDataInterface;
14 | use Magento\Framework\Setup\ModuleContextInterface;
15 | use Magento\Framework\Setup\ModuleDataSetupInterface;
16 |
17 | /**
18 | * Install Data
19 | */
20 | class InstallData implements InstallDataInterface
21 | {
22 | /**
23 | * {@inheritdoc}
24 | * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
25 | */
26 | public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
27 | {
28 | $setup->startSetup();
29 | $vietnam_regions = [
30 | ['country_id' => 'VN', 'code' => 'TPHANOI', 'default_name' => 'Thành phố Hà Nội'],
31 | ['country_id' => 'VN', 'code' => 'HAGIANG', 'default_name' => 'Hà Giang'],
32 | ['country_id' => 'VN', 'code' => 'CAOBANG', 'default_name' => 'Cao Bằng'],
33 | ['country_id' => 'VN', 'code' => 'BACKAN', 'default_name' => 'Bắc Kạn'],
34 | ['country_id' => 'VN', 'code' => 'TUYENQUANG', 'default_name' => 'Tuyên Quang'],
35 | ['country_id' => 'VN', 'code' => 'LAOCAI', 'default_name' => 'Lào Cai'],
36 | ['country_id' => 'VN', 'code' => 'DIENBIEN', 'default_name' => 'Điện Biên'],
37 | ['country_id' => 'VN', 'code' => 'LAICHAU', 'default_name' => 'Lai Châu'],
38 | ['country_id' => 'VN', 'code' => 'SONLA', 'default_name' => 'Sơn La'],
39 | ['country_id' => 'VN', 'code' => 'YENBAI', 'default_name' => 'Yên Bái'],
40 | ['country_id' => 'VN', 'code' => 'HOABINH', 'default_name' => 'Hoà Bình'],
41 | ['country_id' => 'VN', 'code' => 'THAINGUYEN', 'default_name' => 'Thái Nguyên'],
42 | ['country_id' => 'VN', 'code' => 'LANGSON', 'default_name' => 'Lạng Sơn'],
43 | ['country_id' => 'VN', 'code' => 'QUANGNINH', 'default_name' => 'Quảng Ninh'],
44 | ['country_id' => 'VN', 'code' => 'BACGIANG', 'default_name' => 'Bắc Giang'],
45 | ['country_id' => 'VN', 'code' => 'PHUTHO', 'default_name' => 'Phú Thọ'],
46 | ['country_id' => 'VN', 'code' => 'VINHPHUC', 'default_name' => 'Vĩnh Phúc'],
47 | ['country_id' => 'VN', 'code' => 'BACNINH', 'default_name' => 'Bắc Ninh'],
48 | ['country_id' => 'VN', 'code' => 'HAIDUONG', 'default_name' => 'Hải Dương'],
49 | ['country_id' => 'VN', 'code' => 'TPHAIPHONG', 'default_name' => 'Thành phố Hải Phòng'],
50 | ['country_id' => 'VN', 'code' => 'HUNGYEN', 'default_name' => 'Hưng Yên'],
51 | ['country_id' => 'VN', 'code' => 'THAIBINH', 'default_name' => 'Thái Bình'],
52 | ['country_id' => 'VN', 'code' => 'HANAM', 'default_name' => 'Hà Nam'],
53 | ['country_id' => 'VN', 'code' => 'NAMDINH', 'default_name' => 'Nam Định'],
54 | ['country_id' => 'VN', 'code' => 'NINHBINH', 'default_name' => 'Ninh Bình'],
55 | ['country_id' => 'VN', 'code' => 'THANHHOA', 'default_name' => 'Thanh Hóa'],
56 | ['country_id' => 'VN', 'code' => 'NGHEAN', 'default_name' => 'Nghệ An'],
57 | ['country_id' => 'VN', 'code' => 'HATINH', 'default_name' => 'Hà Tĩnh'],
58 | ['country_id' => 'VN', 'code' => 'QUANGBINH', 'default_name' => 'Quảng Bình'],
59 | ['country_id' => 'VN', 'code' => 'QUANGTRI', 'default_name' => 'Quảng Trị'],
60 | ['country_id' => 'VN', 'code' => 'THUATHIENHUE', 'default_name' => 'Thừa Thiên Huế'],
61 | ['country_id' => 'VN', 'code' => 'DANANG', 'default_name' => 'Thành phố Đà Nẵng'],
62 | ['country_id' => 'VN', 'code' => 'QUANGNAM', 'default_name' => 'Quảng Nam'],
63 | ['country_id' => 'VN', 'code' => 'QUANGNGAI', 'default_name' => 'Quảng Ngãi'],
64 | ['country_id' => 'VN', 'code' => 'BINHDINH', 'default_name' => 'Bình Định'],
65 | ['country_id' => 'VN', 'code' => 'PHUYEN', 'default_name' => 'Phú Yên'],
66 | ['country_id' => 'VN', 'code' => 'KHANHHOA', 'default_name' => 'Khánh Hòa'],
67 | ['country_id' => 'VN', 'code' => 'NINHTHUAN', 'default_name' => 'Ninh Thuận'],
68 | ['country_id' => 'VN', 'code' => 'BINHTHUAN', 'default_name' => 'Bình Thuận'],
69 | ['country_id' => 'VN', 'code' => 'KONTUM', 'default_name' => 'Kon Tum'],
70 | ['country_id' => 'VN', 'code' => 'GIALAI', 'default_name' => 'Gia Lai'],
71 | ['country_id' => 'VN', 'code' => 'DAKLAK', 'default_name' => 'Đắk Lắk'],
72 | ['country_id' => 'VN', 'code' => 'DAKNONG', 'default_name' => 'Đắk Nông'],
73 | ['country_id' => 'VN', 'code' => 'LAMDONG', 'default_name' => 'Lâm Đồng'],
74 | ['country_id' => 'VN', 'code' => 'BINHPHUOC', 'default_name' => 'Bình Phước'],
75 | ['country_id' => 'VN', 'code' => 'TAYNINH', 'default_name' => 'Tây Ninh'],
76 | ['country_id' => 'VN', 'code' => 'BINHDUONG', 'default_name' => 'Bình Dương'],
77 | ['country_id' => 'VN', 'code' => 'DONGNAI', 'default_name' => 'Đồng Nai'],
78 | ['country_id' => 'VN', 'code' => 'BARIAVUNGTAU', 'default_name' => 'Bà Rịa - Vũng Tàu'],
79 | ['country_id' => 'VN', 'code' => 'TPHOCHIMINH', 'default_name' => 'Thành phố Hồ Chí Minh'],
80 | ['country_id' => 'VN', 'code' => 'LONGAN', 'default_name' => 'Long An'],
81 | ['country_id' => 'VN', 'code' => 'TIENGIANG', 'default_name' => 'Tiền Giang'],
82 | ['country_id' => 'VN', 'code' => 'BENTRE', 'default_name' => 'Bến Tre'],
83 | ['country_id' => 'VN', 'code' => 'TRAVINH', 'default_name' => 'Trà Vinh'],
84 | ['country_id' => 'VN', 'code' => 'VINHLONG', 'default_name' => 'Vĩnh Long'],
85 | ['country_id' => 'VN', 'code' => 'DONGTHAP', 'default_name' => 'Đồng Tháp'],
86 | ['country_id' => 'VN', 'code' => 'ANGIANG', 'default_name' => 'An Giang'],
87 | ['country_id' => 'VN', 'code' => 'KIENGIANG', 'default_name' => 'Kiên Giang'],
88 | ['country_id' => 'VN', 'code' => 'TPCANTHO', 'default_name' => 'Thành phố Cần Thơ'],
89 | ['country_id' => 'VN', 'code' => 'HAUGIANG', 'default_name' => 'Hậu Giang'],
90 | ['country_id' => 'VN', 'code' => 'SOCTRANG', 'default_name' => 'Sóc Trăng'],
91 | ['country_id' => 'VN', 'code' => 'BACLIEU', 'default_name' => 'Bạc Liêu'],
92 | ['country_id' => 'VN', 'code' => 'CAMAU', 'default_name' => 'Cà Mau'],
93 | ];
94 | // insert the regions for Vietnam
95 | foreach ($vietnam_regions as $key => $value) {
96 | $select = $setup->getConnection()->select()
97 | ->from(['dcr' => $setup->getTable('directory_country_region')])
98 | ->where('dcr.country_id = ?', $value['country_id'])
99 | ->where('dcr.code = ?', $value['code'])
100 | ->where('dcr.default_name = ?', $value['default_name']);
101 | if (!$setup->getConnection()->fetchRow($select)) {
102 | $setup->getConnection()->insertForce(
103 | $setup->getTable('directory_country_region'),
104 | $value
105 | );
106 | }
107 | }
108 | }
109 | }
110 |
--------------------------------------------------------------------------------
/Setup/InstallSchema.php:
--------------------------------------------------------------------------------
1 |
6 | * @Date 2016-12-12 21:31:45
7 | * @Last modified by: nquangcuong
8 | * @Last Modified time: 2016-12-12 22:27:59
9 | */
10 |
11 | namespace PHPCuong\Region\Setup;
12 |
13 | class InstallSchema implements \Magento\Framework\Setup\InstallSchemaInterface
14 | {
15 | /**
16 | * install tables
17 | *
18 | * @param \Magento\Framework\Setup\SchemaSetupInterface $setup
19 | * @param \Magento\Framework\Setup\ModuleContextInterface $context
20 | * @return void
21 | * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
22 | */
23 | public function install(\Magento\Framework\Setup\SchemaSetupInterface $setup, \Magento\Framework\Setup\ModuleContextInterface $context)
24 | {
25 | $installer = $setup;
26 | $installer->startSetup();
27 | if ($installer->tableExists('directory_country_region')) {
28 | $installer->getConnection()->addIndex(
29 | $installer->getTable('directory_country_region'),
30 | $setup->getIdxName(
31 | $installer->getTable('directory_country_region'),
32 | ['default_name'],
33 | \Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_FULLTEXT
34 | ),
35 | ['default_name'],
36 | \Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_FULLTEXT
37 | );
38 | }
39 | $installer->endSetup();
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/Ui/Component/Listing/Column/RegionActions.php:
--------------------------------------------------------------------------------
1 |
6 | * @Date 2016-12-12 00:43:31
7 | * @Last modified by: nquangcuong
8 | * @Last Modified time: 2016-12-12 22:16:00
9 | */
10 |
11 | namespace PHPCuong\Region\Ui\Component\Listing\Column;
12 |
13 | class RegionActions extends \Magento\Ui\Component\Listing\Columns\Column
14 | {
15 | /**
16 | * Url path to edit
17 | *
18 | * @var string
19 | */
20 | const URL_PATH_EDIT = 'phpcuong/region/edit';
21 | /**
22 | * Url path to delete
23 | *
24 | * @var string
25 | */
26 | const URL_PATH_DELETE = 'phpcuong/region/delete';
27 | /**
28 | * URL builder
29 | *
30 | * @var \Magento\Framework\UrlInterface
31 | */
32 | protected $_urlBuilder;
33 | /**
34 | * constructor
35 | *
36 | * @param \Magento\Framework\UrlInterface $urlBuilder
37 | * @param \Magento\Framework\View\Element\UiComponent\ContextInterface $context
38 | * @param \Magento\Framework\View\Element\UiComponentFactory $uiComponentFactory
39 | * @param array $components
40 | * @param array $data
41 | */
42 | public function __construct(
43 | \Magento\Framework\UrlInterface $urlBuilder,
44 | \Magento\Framework\View\Element\UiComponent\ContextInterface $context,
45 | \Magento\Framework\View\Element\UiComponentFactory $uiComponentFactory,
46 | array $components = [],
47 | array $data = []
48 | )
49 | {
50 | $this->_urlBuilder = $urlBuilder;
51 | parent::__construct($context, $uiComponentFactory, $components, $data);
52 | }
53 | /**
54 | * Prepare Data Source
55 | *
56 | * @param array $dataSource
57 | * @return array
58 | */
59 | public function prepareDataSource(array $dataSource)
60 | {
61 | if (isset($dataSource['data']['items'])) {
62 | foreach ($dataSource['data']['items'] as & $item) {
63 | if (isset($item['region_id'])) {
64 | $item[$this->getData('name')] = [
65 | 'edit' => [
66 | 'href' => $this->_urlBuilder->getUrl(
67 | static::URL_PATH_EDIT,
68 | [
69 | 'region_id' => $item['region_id']
70 | ]
71 | ),
72 | 'label' => __('Edit')
73 | ],
74 | 'delete' => [
75 | 'href' => $this->_urlBuilder->getUrl(
76 | static::URL_PATH_DELETE,
77 | [
78 | 'region_id' => $item['region_id']
79 | ]
80 | ),
81 | 'label' => __('Delete'),
82 | 'confirm' => [
83 | 'title' => __('Delete "${ $.$data.default_name }"'),
84 | 'message' => __('Are you sure you wan\'t to delete the Region "${ $.$data.default_name }" ?')
85 | ]
86 | ]
87 | ];
88 | }
89 | }
90 | }
91 | return $dataSource;
92 | }
93 | }
94 |
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "php-cuong/magento2-regions-manager",
3 | "description":"Manage regions in magento2 addresses",
4 | "keywords": [
5 | "magento 2",
6 | "Regions",
7 | "States",
8 | "Provinces",
9 | "Addresses",
10 | "Manage regions in magento2 addresses"
11 | ],
12 | "require": {
13 | "php": "~5.6.0|7.0.2|7.0.4|~7.0.6|~7.1.0",
14 | "magento/module-backend": "100.0.*|100.1.*|100.2.*",
15 | "magento/framework": "100.0.*|100.1.*|101.0.*"
16 | },
17 | "type": "magento2-module",
18 | "version": "2.2.1",
19 | "license": [
20 | "OSL-3.0",
21 | "AFL-3.0"
22 | ],
23 | "authors": [
24 | {
25 | "name": "Cuong NQ",
26 | "email": "bestearnmoney87@gmail.com",
27 | "role": "Developer"
28 | }
29 | ],
30 | "autoload": {
31 | "files": [
32 | "registration.php"
33 | ],
34 | "psr-4": {
35 | "PHPCuong\\Region\\": ""
36 | }
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/etc/acl.xml:
--------------------------------------------------------------------------------
1 |
2 |
2 | = /* @escapeNotVerified */ base64_decode('UmVnaW9ucyBNYW5hZ2Vy') ?> 3 | = /* @escapeNotVerified */ base64_decode('Mi4yLjA='); ?> 4 |
5 | -------------------------------------------------------------------------------- /view/adminhtml/ui_component/region_listing.xml: -------------------------------------------------------------------------------- 1 | 2 |Generated by IcoMoon
185 |