├── app ├── code │ └── community │ │ └── Web │ │ └── States │ │ ├── Model │ │ ├── Mysql4 │ │ │ ├── States.php │ │ │ └── States │ │ │ │ └── Collection.php │ │ ├── Resource │ │ │ ├── States │ │ │ │ └── Collection.php │ │ │ └── States.php │ │ └── States.php │ │ ├── sql │ │ └── web_states_setup │ │ │ └── mysql4-install-0.1.0.php │ │ ├── Helper │ │ └── Data.php │ │ ├── Block │ │ └── Adminhtml │ │ │ ├── States.php │ │ │ ├── States │ │ │ ├── Edit │ │ │ │ ├── Form.php │ │ │ │ ├── Tabs.php │ │ │ │ └── Tab │ │ │ │ │ └── Form.php │ │ │ ├── Edit.php │ │ │ └── Grid.php │ │ │ └── Renderer │ │ │ └── Name.php │ │ ├── etc │ │ ├── adminhtml.xml │ │ └── config.xml │ │ └── controllers │ │ └── Adminhtml │ │ └── StatesController.php ├── locale │ └── Web_States.csv ├── design │ └── adminhtml │ │ └── default │ │ └── default │ │ └── layout │ │ └── web_states.xml └── etc │ └── modules │ └── Web_States.xml ├── modman └── README.md /app/code/community/Web/States/Model/Mysql4/States.php: -------------------------------------------------------------------------------- 1 | _init('web_states/states'); 10 | } 11 | 12 | } -------------------------------------------------------------------------------- /app/code/community/Web/States/Model/Resource/States.php: -------------------------------------------------------------------------------- 1 | _init('web_states/states', 'region_id'); 10 | } 11 | } -------------------------------------------------------------------------------- /app/locale/Web_States.csv: -------------------------------------------------------------------------------- 1 | Default Name,默认名称 2 | State Information,州/省信息 3 | State information,州/省信息 4 | Locale zh_CN,中文名称 5 | Edit Item '%s',编辑条目 '%s' 6 | Save Item,保存条目 7 | Country Code,国家编码 8 | Region Code,州/省编码 9 | Name in Locale,译名 10 | Add Item,添加条目 11 | Country/States Manager,国家/州省管理 12 | Country States,国家/州省 13 | , -------------------------------------------------------------------------------- /app/code/community/Web/States/sql/web_states_setup/mysql4-install-0.1.0.php: -------------------------------------------------------------------------------- 1 | startSetup(); 11 | 12 | 13 | 14 | $installer->endSetup(); -------------------------------------------------------------------------------- /app/design/adminhtml/default/default/layout/web_states.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /app/etc/modules/Web_States.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | true 6 | community 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /modman: -------------------------------------------------------------------------------- 1 | app/code/community/Web app/code/community/Web 2 | app/design/adminhtml/default/default/layout/web_states.xml app/design/adminhtml/default/default/layout/web_states.xml 3 | app/etc/modules/Web_States.xml app/etc/modules/Web_States.xml 4 | app/locale/Web_States.csv app/locale/Web_States.csv 5 | -------------------------------------------------------------------------------- /app/code/community/Web/States/Helper/Data.php: -------------------------------------------------------------------------------- 1 | getStores(); 14 | $locales = array(); 15 | foreach ($stores as $store) { 16 | $v = Mage::getStoreConfig('general/locale/code', $store->getId()); 17 | $locales[$v] = $v; 18 | } 19 | return $locales; 20 | } 21 | 22 | } -------------------------------------------------------------------------------- /app/code/community/Web/States/Block/Adminhtml/States.php: -------------------------------------------------------------------------------- 1 | _controller = 'adminhtml_states'; 11 | /** @var string _blockGroup */ 12 | $this->_blockGroup = 'web_states'; 13 | /** @var string _headerText */ 14 | $this->_headerText = Mage::helper('web_states')->__('Country/States Manager'); 15 | parent::__construct(); 16 | } 17 | } -------------------------------------------------------------------------------- /app/code/community/Web/States/Block/Adminhtml/States/Edit/Form.php: -------------------------------------------------------------------------------- 1 | 'edit_form', 8 | 'action' => $this->getUrl('*/*/save', array('id' => $this->getRequest()->getParam('region_id'))), 9 | 'method' => 'post', 10 | ) 11 | ); 12 | 13 | $form->setUseContainer(true); 14 | $this->setForm($form); 15 | return parent::_prepareForm(); 16 | } 17 | } -------------------------------------------------------------------------------- /app/code/community/Web/States/etc/adminhtml.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Country States 8 | 100 9 | states/adminhtml_states 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | Country States 20 | 600 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /app/code/community/Web/States/Block/Adminhtml/States/Edit/Tabs.php: -------------------------------------------------------------------------------- 1 | setId('web_states_tabs'); 9 | $this->setDestElementId('edit_form'); 10 | $this->setTitle(Mage::helper('web_states')->__('State Information')); 11 | } 12 | 13 | protected function _beforeToHtml() 14 | { 15 | $this->addTab('form_section', array( 16 | 'label' => Mage::helper('web_states')->__('State Information'), 17 | 'title' => Mage::helper('web_states')->__('State Information'), 18 | 'content' => $this->getLayout()->createBlock('web_states/adminhtml_states_edit_tab_form')->toHtml(), 19 | )); 20 | 21 | return parent::_beforeToHtml(); 22 | } 23 | } -------------------------------------------------------------------------------- /app/code/community/Web/States/Block/Adminhtml/States/Edit.php: -------------------------------------------------------------------------------- 1 | _objectId = 'region_id'; 11 | $this->_blockGroup = 'web_states'; 12 | $this->_controller = 'adminhtml_states'; 13 | $this->_updateButton('save', 'label', Mage::helper('web_states')->__('Save Item')); 14 | $this->_updateButton('delete', 'label', Mage::helper('web_states')->__('Delete Item')); 15 | 16 | } 17 | 18 | /** 19 | * @return string 20 | */ 21 | public function getHeaderText() 22 | { 23 | if (Mage::registry('state_data') && Mage::registry('state_data')->getRegionId()) { 24 | return Mage::helper('web_states')->__("Edit Item '%s'", $this->escapeHtml(Mage::registry('state_data')->getRegionId())); 25 | } else { 26 | return Mage::helper('web_states')->__('Add Item'); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /app/code/community/Web/States/Block/Adminhtml/Renderer/Name.php: -------------------------------------------------------------------------------- 1 | getLocales(); 13 | 14 | $resource = Mage::getSingleton('core/resource'); 15 | $read = $resource->getConnection('core_read'); 16 | $regionName = $resource->getTableName('directory/country_region_name'); 17 | 18 | $select = $read->select()->from(array('region' => $regionName))->where('region.region_id=?', $row->getRegionId()); 19 | $data = $read->fetchAll($select); 20 | $arr = array(); 21 | foreach ($data as $row) { 22 | $arr[$row['locale']] = $row['name']; 23 | } 24 | foreach ($locales as $locale) { 25 | if (!array_key_exists($locale,$arr)) { 26 | $name = 'EMPTY'; 27 | }else{ 28 | $name = $arr[$locale]; 29 | } 30 | $html[] = '' . $locale . ' => ' . $name . ''; 31 | } 32 | $html = implode('
', $html); 33 | 34 | if ($html == '') { 35 | $html = ' '; 36 | } 37 | 38 | return $html; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Manage regions in magento addresses 2 | ======================= 3 | 4 | 5 | Magento provides country and regions and cities as dropdown option in addresses This module provides flexibility of managing this regions for each country with its locale translations. 6 | 7 | Its forgetten Feature that magento doesn't have which many developers requires it. 8 | 9 | Very Easy to use ( it allow you to edit in place all the values ) hover and click on the value so you will be able to edit in place ( look at the screen shots ) 10 | 11 | Test and working with Magento CE 1.5+... 1.7.0.2 , and Magento EE 1.10+.. 1.13.0.1 12 | - Features: 13 | - Complete native code ( php magento ) no Theme/layout modification required 14 | - Inline Edit which make it very easy to change data in faster way 15 | - Locale support, so you will be able to change name of the regions for each locale in your store. 16 | 17 | ##Usage : 18 | 19 | ####Install via [Magento Connect](http://www.magentocommerce.com/magento-connect/catalog/product/view/id/18954/) 20 | 21 | 22 | ####Manual Installation: 23 | 1- Copy the files into magento ROOT Directory 24 | 2- Clear magento cache 25 | 26 | 27 | 28 | ####Now Navigate from the top Menu CMS -> Country States 29 | 30 | 31 | Check the screen shots: 32 | 33 | ###Inline Edit in the Grid With locale Support ( admin panel ) 34 | 35 | 36 | ![Inline Edit](http://i.imgur.com/hEQxLFM.png) 37 | 38 | ![Inline Edit](http://i.imgur.com/Xof8LjL.png) 39 | 40 | ###Inline Edit in the Grid ( admin panel ) 41 | 42 | ![Inline Edit](http://i.imgur.com/UZAZDrE.png) 43 | 44 | ### Edit Region 45 | 46 | ![Edit Region](http://i.imgur.com/nwpYvVd.png) 47 | 48 | ###Grid List of Regions 49 | 50 | ![Grid List of regions](http://i.imgur.com/q9TfbCj.png) 51 | 52 | You will see the grid and how easy to modify the Regions/state for each country and its translation for each locale in your system. 53 | -------------------------------------------------------------------------------- /app/code/community/Web/States/etc/config.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 0.1.0 6 | 7 | 8 | 9 | 10 | 11 | Web_States_Model 12 | web_states_mysql4 13 | 14 | 15 | Web_States_Model_Mysql4 16 | 17 | 18 | directory_country_region
19 |
20 |
21 |
22 |
23 | 24 | 25 | Web_States_Block 26 | 27 | 28 | 29 | 30 | Web_States_Helper 31 | 32 | 33 | 34 | 35 | 36 | Web_States 37 | 38 | 39 | 40 | 41 | core_read 42 | 43 | 44 | 45 | 46 | core_write 47 | 48 | 49 | 50 |
51 | 52 | 53 | 54 | admin 55 | 56 | Web_States 57 | states 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | web_states.xml 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | Web_States.csv 75 | 76 | 77 | 78 | 79 | 80 |
81 | -------------------------------------------------------------------------------- /app/code/community/Web/States/Block/Adminhtml/States/Edit/Tab/Form.php: -------------------------------------------------------------------------------- 1 | setForm($form); 8 | $countries = Mage::getSingleton('directory/country')->getCollection()->loadData()->toOptionArray(false); 9 | $id = $this->getRequest()->getParam('region_id'); 10 | 11 | 12 | 13 | $fieldSet = $form->addFieldset('web_states_form', array('legend' => Mage::helper('web_states')->__('State information'))); 14 | $fieldSet->addField( 15 | 'country_id', 'select', array( 16 | 'label' => Mage::helper('web_states')->__('Country'), 17 | 'name' => 'country_id', 18 | 'required' => true, 19 | 'values' => $countries 20 | ) 21 | ); 22 | 23 | $fieldSet->addField( 24 | 'code', 'text', array( 25 | 'label' => Mage::helper('web_states')->__('Code'), 26 | 'class' => 'required-entry', 27 | 'required' => true, 28 | 'name' => 'code', 29 | ) 30 | ); 31 | $fieldSet->addField( 32 | 'default_name', 'text', array( 33 | 'label' => Mage::helper('web_states')->__('Default Name'), 34 | 'class' => 'required-entry', 35 | 'required' => true, 36 | 'name' => 'default_name', 37 | ) 38 | ); 39 | $locales = Mage::helper('web_states')->getLocales(); 40 | foreach ($locales as $locale) { 41 | $fieldSet{$locale} = $form->addFieldset('web_states_form_' . $locale, array('legend' => Mage::helper('web_states')->__('Locale ' . $locale))); 42 | $fieldSet{$locale}->addField( 43 | 'name_'.$locale, 'text', array( 44 | 'label' => Mage::helper('web_states')->__('Name'), 45 | 'name' => 'name_'.$locale, 46 | ) 47 | ); 48 | } 49 | if (Mage::getSingleton('adminhtml/session')->getStateData()) { 50 | $form->setValues(Mage::getSingleton('adminhtml/session')->getStateData()); 51 | Mage::getSingleton('adminhtml/session')->setStateData(null); 52 | } elseif (Mage::registry('state_data')) { 53 | $form->setValues(Mage::registry('state_data')->getData()); 54 | } 55 | if($id){ 56 | $resource = Mage::getSingleton('core/resource'); 57 | $read = $resource->getConnection('core_read'); 58 | $regionName = $resource->getTableName('directory/country_region_name'); 59 | 60 | $select = $read->select()->from(array('region'=>$regionName))->where('region.region_id=?', $id); 61 | $data =$read->fetchAll($select); 62 | foreach($data as $row) 63 | { 64 | $form->addValues(array('name_'.$row['locale']=> $row['name'])); 65 | } 66 | } 67 | return parent::_prepareForm(); 68 | 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /app/code/community/Web/States/Block/Adminhtml/States/Grid.php: -------------------------------------------------------------------------------- 1 | setId('web_states_grid'); 11 | $this->setDefaultSort('region_id'); 12 | $this->setDefaultDir('DESC'); 13 | $this->setSaveParametersInSession(true); 14 | } 15 | 16 | /** 17 | * @return Mage_Adminhtml_Block_Widget_Grid 18 | */ 19 | protected function _prepareCollection() 20 | { 21 | /** @var Varien_Data_Collection $collection */ 22 | $collection = Mage::getModel('web_states/states')->getCollection(); 23 | 24 | $this->setCollection($collection); 25 | 26 | $this->setLocales(Mage::helper('web_states')->getLocales()); 27 | return parent::_prepareCollection(); 28 | } 29 | 30 | /** 31 | * @return $this 32 | */ 33 | protected function _prepareColumns() 34 | { 35 | $this->addColumn( 36 | 'region_id', array( 37 | 'header' => Mage::helper('web_states')->__('ID'), 38 | 'align' => 'left', 39 | 'width' => '5', 40 | 'index' => 'region_id', 41 | 'column_css_class' => 'row_id' 42 | ) 43 | ); 44 | 45 | $this->addColumn( 46 | 'country_id', array( 47 | 'header' => Mage::helper('web_states')->__('Country Code'), 48 | 'align' => 'left', 49 | 'width' => '110px', 50 | 'index' => 'country_id', 51 | 'type' => 'country', 52 | ) 53 | ); 54 | $this->addColumn( 55 | 'code', array( 56 | 'header' => Mage::helper('web_states')->__('Region Code'), 57 | 'align' => 'left', 58 | 'width' => '110px', 59 | 'index' => 'code', 60 | //'editable' =>true, 61 | 'column_css_class' => 'code_td' 62 | ) 63 | ); 64 | $this->addColumn( 65 | 'default_name', array( 66 | 'header' => Mage::helper('web_states')->__('Default Name'), 67 | 'align' => 'left', 68 | 'width' => '110px', 69 | 'index' => 'default_name', 70 | //'editable' =>true, 71 | 'column_css_class' => 'default_name' 72 | ) 73 | ); 74 | $this->addColumn( 75 | 'name_locale', array( 76 | 'header' => Mage::helper('web_states')->__('Name in Locale'), 77 | 'align' => 'left', 78 | 'width' => '110px', 79 | 'index' => 'region_id', 80 | 'sortable' => false, 81 | 'filter' => false, 82 | 'renderer' => 'Web_States_Block_Adminhtml_Renderer_Name', 83 | 84 | //'editable' =>true, 85 | 'column_css_class' => 'name_locale' 86 | ) 87 | ); 88 | $this->addColumn( 89 | 'action', 90 | array( 91 | 'header' => Mage::helper('web_states')->__('Action'), 92 | 'width' => '50px', 93 | 'type' => 'action', 94 | 'getter' => 'getRegionId', 95 | 'actions' => array( 96 | array( 97 | 'caption' => Mage::helper('web_states')->__('View'), 98 | 'url' => array('base' => '*/*/edit'), 99 | 'field' => 'region_id' 100 | ) 101 | ), 102 | 'filter' => false, 103 | 'sortable' => false, 104 | 'index' => 'region_id', 105 | 'is_system' => true, 106 | ) 107 | ); 108 | $this->setAdditionalJavaScript($this->getScripts()); 109 | return parent::_prepareColumns(); 110 | } 111 | 112 | /** 113 | * @param $row 114 | * 115 | * @return string 116 | */ 117 | public function getRowUrl($row) 118 | { 119 | return ''; 120 | } 121 | 122 | /** 123 | * @return $this|Mage_Adminhtml_Block_Widget_Grid 124 | */ 125 | protected function _prepareMassaction() 126 | { 127 | $this->setMassactionIdField('region_id'); 128 | $this->getMassactionBlock()->setUseSelectAll(false); 129 | $this->getMassactionBlock()->setFormFieldName('web_states'); 130 | $this->getMassactionBlock()->addItem( 131 | 'delete', array( 132 | 'label' => Mage::helper('web_states')->__('Delete'), 133 | 'url' => $this->getUrl('*/*/massDelete', array('_current' => true)), 134 | ) 135 | ); 136 | return $this; 137 | } 138 | 139 | /** 140 | * @return string 141 | */ 142 | public function getScripts() 143 | { 144 | $locales = Mage::helper('web_states')->getLocales(); 145 | 146 | $nameUrl = $this->getUrl('*/*/saveName'); 147 | $codeUrl = $this->getUrl('*/*/saveCode'); 148 | $js 149 | = ' 150 | function getNameUrl(e) 151 | { 152 | return "' . $nameUrl . 'region_id/"+getId(e); 153 | } 154 | function getCodeUrl(e) 155 | { 156 | return "' . $codeUrl . 'region_id/"+getId(e); 157 | } 158 | function getNameLocaleUrl(e,url) 159 | { 160 | return url+"region_id/"+getId(e); 161 | } 162 | function getId(e) 163 | { 164 | id = e.up("tr").down("td.row_id").innerHTML; 165 | return id.trim(); 166 | } 167 | '; 168 | $js 169 | .= <<'+el.innerHTML.trim()+''); 175 | new Ajax.InPlaceEditor(el.down('span'), getNameUrl(el),{formId:idx,okText: 'Save',cancelText: 'Cancel'} ); 176 | }); 177 | $$('.code_td').each(function(el){ 178 | if(el.down('span')){return ;} 179 | idx = getId(el); 180 | el.update(''+el.innerHTML.trim()+''); 181 | new Ajax.InPlaceEditor(el.down('span'), getCodeUrl(el),{formId:idx,okText: 'Save',cancelText: 'Cancel'} ); 182 | }); 183 | 184 | EOF; 185 | foreach($locales as $locale) 186 | { 187 | $nameLocaleUrl = $this->getUrl('*/*/saveNameLocale',array('locale'=>$locale)); 188 | $e_name = $locale.'_name'; 189 | $js 190 | .= <<'+el.innerHTML.trim()+''); 194 | new Ajax.InPlaceEditor(el.down('span'), getNameLocaleUrl(el,'$nameLocaleUrl'),{formId:idx,okText: 'Save',cancelText: 'Cancel'} ); 195 | }); 196 | 197 | EOF; 198 | 199 | } 200 | $js .='});'; 201 | return $js; 202 | } 203 | 204 | } -------------------------------------------------------------------------------- /app/code/community/Web/States/controllers/Adminhtml/StatesController.php: -------------------------------------------------------------------------------- 1 | loadLayout() 10 | ->_setActiveMenu('cms/web_states'); 11 | Mage::helper('adminhtml')->setPageHelpUrl('https://github.com/Meabed/magento-regions-manager'); 12 | return $this; 13 | } 14 | 15 | /** 16 | * 17 | */ 18 | public function indexAction() 19 | { 20 | $this->_initAction(); 21 | $this->renderLayout(); 22 | } 23 | 24 | /** 25 | * 26 | */ 27 | public function editAction() 28 | { 29 | $regionId = $this->getRequest()->getParam('region_id'); 30 | $state = Mage::getModel('web_states/states')->load($regionId); 31 | 32 | if ($state->getRegionId() || $regionId == 0) { 33 | $this->_initAction(); 34 | Mage::register('state_data', $state); 35 | $this->_addBreadcrumb(Mage::helper('web_states')->__('Country/States Manager'), Mage::helper('web_states')->__('Item Manager')); 36 | $this->getLayout()->getBlock('head')->setCanLoadExtJs(true); 37 | 38 | $this->_addContent($this->getLayout()->createBlock('web_states/adminhtml_states_edit')) 39 | ->_addLeft($this->getLayout()->createBlock('web_states/adminhtml_states_edit_tabs')); 40 | 41 | $this->renderLayout(); 42 | } else { 43 | Mage::getSingleton('adminhtml/session')->addError(Mage::helper('web_states')->__('State does not exist')); 44 | $this->_redirect('*/*/'); 45 | } 46 | } 47 | 48 | /** 49 | * 50 | */ 51 | public function newAction() 52 | { 53 | $this->_forward('edit'); 54 | } 55 | 56 | /** 57 | * 58 | */ 59 | public function saveAction() 60 | { 61 | $request = $this->getRequest(); 62 | 63 | if ($this->getRequest()->getPost()) { 64 | $id = $request->getParam('id'); 65 | $code = $request->getParam('code'); 66 | $name = $request->getParam('default_name'); 67 | $countryId = $request->getParam('country_id'); 68 | if (!$name || !$code) { 69 | Mage::getSingleton('adminhtml/session')->addError(Mage::helper('adminhtml')->__('Please fill the required fields')); 70 | $this->_redirect('*/*/'); 71 | return; 72 | } 73 | $state = Mage::getModel('web_states/states')->getCollection() 74 | ->addFieldToFilter('code', $code) 75 | ->addFieldToFilter('country_id', $countryId) 76 | ->getAllIds(); 77 | if (count($state) > 0 && !in_array($id, $state)) { 78 | Mage::getSingleton('adminhtml/session')->addError(Mage::helper('adminhtml')->__('State/Country combination must be unique')); 79 | $this->_redirect('*/*/edit', array('region_id' => $id)); 80 | return; 81 | } 82 | 83 | try { 84 | $state = Mage::getModel('web_states/states'); 85 | $state->setRegionId($id) 86 | ->setCode($code) 87 | ->setCountryId($countryId) 88 | ->setDefaultName($name) 89 | ->save(); 90 | if ($state->getRegionId()) { 91 | $locales = Mage::helper('web_states')->getLocales(); 92 | $resource = Mage::getSingleton('core/resource'); 93 | $write = $resource->getConnection('core_write'); 94 | $regionName = $resource->getTableName('directory/country_region_name'); 95 | $write->delete($regionName, array('region_id =' . $state->getRegionId())); 96 | foreach ($locales as $locale) { 97 | $localeName = $request->getParam('name_' . $locale); 98 | if ($localeName) { 99 | $write->insert($regionName, array('region_id' => $state->getRegionId(), 'locale' => $locale, 'name' => trim($name))); 100 | } 101 | } 102 | } 103 | Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('Item was successfully saved')); 104 | Mage::getSingleton('adminhtml/session')->getStateData(false); 105 | $this->_redirect('*/*/'); 106 | return; 107 | } catch (Exception $e) { 108 | Mage::getSingleton('adminhtml/session')->addError($e->getMessage()); 109 | Mage::getSingleton('adminhtml/session')->setStateData($this->getRequest()->getPost()); 110 | $this->_redirect('*/*/edit', array('region_id' => $this->getRequest()->getParam('id'))); 111 | return; 112 | } 113 | } 114 | $this->_redirect('*/*/'); 115 | } 116 | 117 | /** 118 | * 119 | */ 120 | public function saveNameAction() 121 | { 122 | $request = $this->getRequest(); 123 | $editorId = $request->getParam('editorId'); 124 | $value = $request->getParam('value'); 125 | if (!$editorId) { 126 | echo $this->__('Unable to Save.'); 127 | return; 128 | } 129 | if (!$value) { 130 | echo $this->__('Value can not be empty.'); 131 | return; 132 | } 133 | $model = Mage::getModel('web_states/states')->load($editorId); 134 | $model->setDefaultName(trim($value)); 135 | try { 136 | $model->save(); 137 | } catch (Exception $e) { 138 | echo $e->getCode() . '-' . $e->getMessage(); 139 | } 140 | echo $model->getDefaultName(); 141 | 142 | } 143 | 144 | /** 145 | * 146 | */ 147 | public function saveNameLocaleAction() 148 | { 149 | $request = $this->getRequest(); 150 | $editorId = $request->getParam('editorId'); 151 | $locale = $request->getParam('locale'); 152 | $value = $request->getParam('value'); 153 | if (!$editorId) { 154 | echo $this->__('Unable to Save.'); 155 | return; 156 | } 157 | if (!$locale) { 158 | echo $this->__('Locale can not be empty.'); 159 | return; 160 | } 161 | if ($value == 'EMPTY' || $value == 'Empty value will not be saved.') { 162 | echo $this->__('Empty value will not be saved.'); 163 | return; 164 | } 165 | $resource = Mage::getSingleton('core/resource'); 166 | $write = $resource->getConnection('core_write'); 167 | $regionName = $resource->getTableName('directory/country_region_name'); 168 | $write->delete($regionName, array('region_id =' . $editorId,'locale = "' . $locale.'"')); 169 | 170 | if ($value) { 171 | $write->insert($regionName, array('region_id' => $editorId, 'locale' => $locale, 'name' => trim($value))); 172 | } 173 | $select = $write->select('*')->from(array('region' => $regionName))->where('region.region_id=?', $editorId)->where('region.locale=?', $locale); 174 | $row = $write->fetchRow($select); 175 | echo $row['name']; 176 | } 177 | 178 | /** 179 | * 180 | */ 181 | public function saveCodeAction() 182 | { 183 | $request = $this->getRequest(); 184 | $editorId = $request->getParam('editorId'); 185 | $value = $request->getParam('value'); 186 | if (!$editorId) { 187 | echo $this->__('Unable to Save.'); 188 | return; 189 | } 190 | if (!$value) { 191 | echo $this->__('Value can not be empty.'); 192 | return; 193 | } 194 | $row = Mage::getModel('web_states/states')->getCollection() 195 | ->addFieldToFilter('code', $value) 196 | ->getFirstItem(); 197 | if (($row->getRegionId() == $editorId) && (trim($value) == $row->getCode())) { 198 | echo $row->getCode() . ' not updated'; 199 | return; 200 | } 201 | if ($row->getRegionId()) { 202 | echo $this->__('State code must be unique.'); 203 | return; 204 | } 205 | 206 | $model = Mage::getModel('web_states/states')->load($editorId); 207 | $model->setCode(trim($value)); 208 | try { 209 | $model->save(); 210 | } catch (Exception $e) { 211 | echo $e->getCode() . '-' . $e->getMessage(); 212 | } 213 | echo $model->getCode(); 214 | } 215 | 216 | /** 217 | * 218 | */ 219 | public function massDeleteAction() 220 | { 221 | $stateIds = $this->getRequest()->getParam('web_states'); 222 | if (!is_array($stateIds)) { 223 | Mage::getSingleton('adminhtml/session')->addError(Mage::helper('adminhtml')->__('Please select state(s).')); 224 | } else { 225 | try { 226 | $state = Mage::getModel('web_states/states'); 227 | foreach ($stateIds as $stateId) { 228 | $state->load($stateId) 229 | ->delete(); 230 | } 231 | Mage::getSingleton('adminhtml/session')->addSuccess( 232 | Mage::helper('adminhtml')->__('Total of %d record(s) were deleted.', count($stateIds)) 233 | ); 234 | } catch (Exception $e) { 235 | Mage::getSingleton('adminhtml/session')->addError($e->getMessage()); 236 | } 237 | } 238 | 239 | $this->_redirect('*/*/index'); 240 | } 241 | 242 | } --------------------------------------------------------------------------------