<api_url translate="label"> <label>API URL</label> <frontend_type>text</frontend_type> <sort_order>120</sort_order> <show_in_default>1</show_in_default> <show_in_website>0</show_in_website> <show_in_store>0</show_in_store> </api_url> <api_key translate="label"> <label>API key</label> <frontend_type>text</frontend_type> <sort_order>130</sort_order> <show_in_default>1</show_in_default> <show_in_website>0</show_in_website> <show_in_store>0</show_in_store> </api_key> <enable_log translate="label"> <label>Enable log</label> <frontend_type>select</frontend_type> <source_model>adminhtml/system_config_source_yesno</source_model> <sort_order>140</sort_order> <show_in_default>1</show_in_default> <show_in_website>0</show_in_website> <show_in_store>0</show_in_store> </enable_log>
<config> ... <default> <carriers> <novaposhta> ... <api_url>http://orders.novaposhta.ua/xml.php</api_url> <enable_log>0</enable_log> </novaposhta> </carriers> </default> ... </config>
class Ak_NovaPoshta_Helper_Data extends Mage_Core_Helper_Abstract { protected $_logFile = 'novaposhta.log'; /** * @param $string * * @return Ak_NovaPoshta_Helper_Data */ public function log($string) { if ($this->getStoreConfig('enable_log')) { Mage::log($string, null, $this->_logFile); } return $this; } /** * @param string $key * @param null $storeId * * @return mixed */ public function getStoreConfig($key, $storeId = null) { return Mage::getStoreConfig("carriers/novaposhta/$key", $storeId); } }
... <global> <models> <novaposhta> <class>Ak_NovaPoshta_Model</class> <resourceModel>novaposhta_resource</resourceModel> </novaposhta> <novaposhta_resource> <class>Ak_NovaPoshta_Model_Resource</class> <entities> <city> <table>novaposhta_city</table> </city> <warehouse> <table>novaposhta_warehouse</table> </warehouse> </entities> <novaposhta_resource> </models> ... <resources> <novaposhta_setup> <setup> <module>Ak_NovaPoshta</module> </setup> </novaposhta_setup> </resources> </global> ...
/* @var $installer Mage_Core_Model_Resource_Setup */ $installer = $this; $installer->startSetup(); $installer->run(" CREATE TABLE {$this->getTable('novaposhta_city')} ( `id` int(10) unsigned NOT NULL, `name_ru` varchar(100), `name_ua` varchar(100), `updated_at` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP, PRIMARY KEY (`id`), INDEX `name_ru` (`name_ru`), INDEX `name_ua` (`name_ua`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE {$this->getTable('novaposhta_warehouse')} ( `id` int(10) unsigned NOT NULL, `city_id` int(10) unsigned NOT NULL, `address_ru` varchar(200), `address_ua` varchar(200), `phone` varchar(100), `weekday_work_hours` varchar(20), `weekday_reseiving_hours` varchar(20), `weekday_delivery_hours` varchar(20), `saturday_work_hours` varchar(20), `saturday_reseiving_hours` varchar(20), `saturday_delivery_hours` varchar(20), `max_weight_allowed` int(4), `longitude` float(10,6), `latitude` float(10,6), `number_in_city` int(3) unsigned NOT NULL, `updated_at` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP, PRIMARY KEY (`id`), CONSTRAINT FOREIGN KEY (`city_id`) REFERENCES `{$this->getTable('novaposhta_city')}` (`id`) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8; "); $installer->endSetup();
class Ak_NovaPoshta_Model_City extends Mage_Core_Model_Abstract { public function _construct() { $this->_init('novaposhta/city'); } … }
class Ak_NovaPoshta_Model_Resource_City extends Mage_Core_Model_Resource_Db_Abstract { public function _construct() { $this->_init('novaposhta/city', 'id'); } }
class Ak_NovaPoshta_Model_Resource_City_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract { public function _construct() { $this->_init('novaposhta/city'); } }
$cities = $this->_applyMap($cities, $this->_dataMapCity);
/** * @return Varien_Db_Adapter_Interface */ protected function _getConnection() { return Mage::getSingleton('core/resource')->getConnection('core_write'); }
require 'app/Mage.php'; Mage::app('default'); Mage::getModel('novaposhta/import')->runWarehouseAndCityMassImport();
<crontab> <jobs> <novaposhta_import_city_and_warehouse> <schedule> <cron_expr>1 2 * * *</cron_expr> </schedule> <run> <model>ak_novaposhta/import::runWarehouseAndCityMassImport</model> </run> </novaposhta_import_city_and_warehouse> </jobs> </crontab>
class Ak_NovaPoshta_Block_Adminhtml_Warehouses extends Mage_Adminhtml_Block_Widget_Grid_Container { public function __construct() { // $this->_blockGroup $this->_controller , _prepareLayout() (novaposhta/adminhtml_warehouses). _prepareLayout(). $this->_blockGroup = 'novaposhta'; $this->_controller = 'adminhtml_warehouses'; this->_headerText = $this->__('Manage warehouses'); parent::__construct(); // add, , $this->_removeButton('add'); // , $this->_addButton('synchronize', array( 'label' => $this->__('Synchronize with API'), 'onclick' => 'setLocation(\'' . $this->getUrl('*/*/synchronize') .'\')' )); } }
class Ak_NovaPoshta_Block_Adminhtml_Warehouses_Grid extends Mage_Adminhtml_Block_Widget_Grid { public function __construct() { parent::__construct(); $this->setDefaultSort('city_id'); $this->setId('warehousesGrid'); $this->setDefaultDir('asc'); $this->setSaveParametersInSession(true); } protected function _prepareCollection() { /** @var $collection Ak_NovaPoshta_Model_Resource_Warehouse_Collection */ $collection = Mage::getModel('novaposhta/warehouse') ->getCollection(); $this->setCollection($collection); return parent::_prepareCollection(); } protected function _prepareColumns() { // $this->addColumn('id', array( 'header' => $this->__('ID'), 'align' =>'right', 'width' => '50px', 'index' => 'id' ) ); $this->addColumn('address_ru', array( 'header' => $this->__('Address (ru)'), 'index' => 'address_ru' ) ); $this->addColumn('city_id', array( 'header' => $this->__('City'), 'index' => 'city_id', 'type' => 'options', // City “” 'options' => Mage::getModel('novaposhta/city')->getOptionArray() ) ); $this->addColumn('phone', array( 'header' => $this->__('Phone'), 'index' => 'phone' ) ); $this->addColumn('max_weight_allowed', array( 'header' => $this->__('Max weight'), 'index' => 'max_weight_allowed' ) ); return parent::_prepareColumns(); } // false - public function getRowUrl($row) { return false; } }
class Ak_NovaPoshta_WarehousesController extends Mage_Adminhtml_Controller_Action { /** * */ public function indexAction() { $this->_title($this->__('Sales'))->_title($this->__('Nova Poshta Warehouses')); $this->_initAction() ->_addContent($this->getLayout()->createBlock('novaposhta/adminhtml_warehouses')) ->renderLayout(); return $this; } /** * */ public function synchronizeAction() { try { Mage::getModel('novaposhta/import')->runWarehouseAndCityMassImport(); // , success message $this->_getSession()->addSuccess($this->__('City and Warehouse API synchronization finished')); } catch (Exception $e) { // , error message $this->_getSession()->addError($this->__('Error during synchronization: %s', $e->getMessage())); } // $this->_redirect('*/*/index'); return $this; } /** * Initialize action * * @return Ak_NovaPoshta_WarehousesController */ protected function _initAction() { $this->loadLayout() ->_setActiveMenu('sales/novaposhta/warehouses') ->_addBreadcrumb($this->__('Sales'), $this->__('Sales')) ->_addBreadcrumb($this->__('Nova Poshta Warehouses'), $this->__('Nova Poshta Warehouses')) ; return $this; } }
<config> ... <admin> <routers> <novaposhta> <use>admin</use> <args> <module>Ak_NovaPoshta</module> <frontName>novaposhta</frontName> </args> </novaposhta> </routers> </admin> ... </config>
<?xml version="1.0"?> <config> <menu> <sales> <children> <novaposhta translate="title" module="novaposhta"> <sort_order>200</sort_order> <title>Nova Poshta</title> <children> <warehouses translate="title" module="novaposhta"> <sort_order>10</sort_order> <title>Warehouses</title> <action>novaposhta/warehouses/</action> </warehouses> </children> </novaposhta> </children> </sales> </menu> <acl> <resources> <admin> <children> <sales> <children> <novaposhta translate="title" module="novaposhta"> <title>Nova Poshta</title> <sort_order>200</sort_order> <children> <warehouses translate="title" module="novaposhta"> <sort_order>10</sort_order> <title>Warehouses</title> </warehouses> </children> </novaposhta> </children> </sales> </children> </admin> </resources> </acl> </config>
Source: https://habr.com/ru/post/162313/
All Articles