commit a07aa6ae8e2c861dc717cd6f5a84e6cea2a9c9cb Author: Alex Lushpai Date: Fri Aug 15 13:35:22 2014 +0400 setting & icml export diff --git a/README.md b/README.md new file mode 100644 index 0000000..39d6840 --- /dev/null +++ b/README.md @@ -0,0 +1,46 @@ +Opecart module +============= + +Opencart module for interaction with [IntaroCRM](http://www.intarocrm.com) through [REST API](http://docs.intarocrm.ru/rest-api/). + +Module allows: + +* Send to IntaroCRM new orders +* Configure relations between dictionaries of IntaroCRM and Opencart (statuses, payments, delivery types and etc) +* Generate [ICML](http://docs.intarocrm.ru/index.php?n=Пользователи.ФорматICML) (IntaroCRM Markup Language) for catalog loading by IntaroCRM + +Installation +------------- + +### 1. Manual installation + + +#### Clone module. +``` +git clone git@github.com:/intarocrm/opencart-module.git +``` + +#### Install Rest API Client. + +``` +cd opencart-module/system/library/intarocrm +./composer.phar install +``` + +#### Install module +``` +cp -r opencart-module/* /path/to/opecart/instance +``` + +#### Activate via Admin interface. + +Go to Modules -> Intstall module. + +#### Export + +Catalog export script will be here +``` +/index.php?route=export/intarocrm +``` + + diff --git a/admin/controller/module/intarocrm.php b/admin/controller/module/intarocrm.php new file mode 100644 index 0000000..97f1025 --- /dev/null +++ b/admin/controller/module/intarocrm.php @@ -0,0 +1,279 @@ +load->model('setting/setting'); + $this->model_setting_setting->editSetting('intarocrm', array('intarocrm_status'=>1)); + } + + public function uninstall() { + $this->load->model('setting/setting'); + $this->model_setting_setting->editSetting('intarocrm', array('intarocrm_status'=>0)); + } + + public function index() { + + $this->log = new Monolog\Logger('opencart-module'); + $this->log->pushHandler(new Monolog\Handler\StreamHandler(__DIR__ . '/../../../system/logs/module.log', Monolog\Logger::INFO)); + + $this->load->model('setting/setting'); + $this->load->model('setting/extension'); + $this->load->language('module/intarocrm'); + $this->document->setTitle($this->language->get('heading_title')); + $this->document->addStyle('/admin/view/stylesheet/intarocrm.css'); + + if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) { + $this->model_setting_setting->editSetting('intarocrm', $this->request->post); + $this->session->data['success'] = $this->language->get('text_success'); + $this->redirect($this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL')); + } + + $text_strings = array( + 'heading_title', + 'text_enabled', + 'text_disabled', + 'button_save', + 'button_cancel', + 'text_notice', + 'intarocrm_url', + 'intarocrm_apikey', + 'intarocrm_base_settings', + 'intarocrm_dict_settings', + 'intarocrm_dict_delivery', + 'intarocrm_dict_status', + 'intarocrm_dict_payment', + ); + + foreach ($text_strings as $text) { + $this->data[$text] = $this->language->get($text); + } + + $this->data['intarocrm_errors'] = array(); + $this->data['saved_settings'] = $this->model_setting_setting->getSetting('intarocrm'); + + if ($this->data['saved_settings']['intarocrm_url'] != '' && $this->data['saved_settings']['intarocrm_apikey'] != '') { + + $this->intarocrm = new \IntaroCrm\RestApi( + $this->data['saved_settings']['intarocrm_url'], + $this->data['saved_settings']['intarocrm_apikey'] + ); + + /* + * Delivery + */ + + try { + $this->deliveryTypes = $this->intarocrm->deliveryTypesList(); + } + catch (ApiException $e) + { + $this->data['intarocrm_error'][] = $e->getMessage(); + $this->log->addError('['.$this->config->get('store_name').'] RestApi::deliveryTypesList::Api:' . $e->getMessage()); + } + catch (CurlException $e) + { + $this->data['intarocrm_error'][] = $e->getMessage(); + $this->log->addError('['.$this->config->get('store_name').'] RestApi::deliveryTypesList::Curl:' . $e->getMessage()); + } + + $this->data['delivery'] = array( + 'opencart' => $this->getOpercartDeliveryMethods(), + 'intarocrm' => $this->deliveryTypes + ); + + /* + * Statuses + */ + try { + $this->statuses = $this->intarocrm->orderStatusesList(); + } + catch (ApiException $e) + { + $this->data['intarocrm_error'][] = $e->getMessage(); + $this->log->addError('['.$this->config->get('store_name').'] RestApi::orderStatusesList::Api:' . $e->getMessage()); + } + catch (CurlException $e) + { + $this->data['intarocrm_error'][] = $e->getMessage(); + $this->log->addError('['.$this->config->get('store_name').'] RestApi::orderStatusesList::Curl:' . $e->getMessage()); + } + + $this->data['statuses'] = array( + 'opencart' => $this->getOpercartOrderStatuses(), + 'intarocrm' => $this->statuses + ); + + /* + * Payment + */ + + try { + $this->payments = $this->intarocrm->paymentTypesList(); + } + catch (ApiException $e) + { + $this->data['intarocrm_error'][] = $e->getMessage(); + $this->log->addError('['.$this->config->get('store_name').'] RestApi::paymentTypesList::Api:' . $e->getMessage()); + } + catch (CurlException $e) + { + $this->data['intarocrm_error'][] = $e->getMessage(); + $this->log->addError('['.$this->config->get('store_name').'] RestApi::paymentTypesList::Curl:' . $e->getMessage()); + } + + $this->data['payments'] = array( + 'opencart' => $this->getOpercartPaymentTypes(), + 'intarocrm' => $this->payments + ); + + } + + $config_data = array( + 'intarocrm_status' + ); + + foreach ($config_data as $conf) { + if (isset($this->request->post[$conf])) { + $this->data[$conf] = $this->request->post[$conf]; + } else { + $this->data[$conf] = $this->config->get($conf); + } + } + + if (isset($this->error['warning'])) { + $this->data['error_warning'] = $this->error['warning']; + } else { + $this->data['error_warning'] = ''; + } + + $this->data['breadcrumbs'] = array(); + + $this->data['breadcrumbs'][] = array( + 'text' => $this->language->get('text_home'), + 'href' => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'), + 'separator' => false + ); + + $this->data['breadcrumbs'][] = array( + 'text' => $this->language->get('text_module'), + 'href' => $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'), + 'separator' => ' :: ' + ); + + $this->data['breadcrumbs'][] = array( + 'text' => $this->language->get('heading_title'), + 'href' => $this->url->link('module/intarocrm', 'token=' . $this->session->data['token'], 'SSL'), + 'separator' => ' :: ' + ); + + $this->data['action'] = $this->url->link('module/intarocrm', 'token=' . $this->session->data['token'], 'SSL'); + + $this->data['cancel'] = $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'); + + + $this->data['modules'] = array(); + + if (isset($this->request->post['intarocrm_module'])) { + $this->data['modules'] = $this->request->post['intarocrm_module']; + } elseif ($this->config->get('intarocrm_module')) { + $this->data['modules'] = $this->config->get('intarocrm_module'); + } + + $this->load->model('design/layout'); + + $this->data['layouts'] = $this->model_design_layout->getLayouts(); + + $this->template = 'module/intarocrm.tpl'; + $this->children = array( + 'common/header', + 'common/footer', + ); + + $this->response->setOutput($this->render()); + } + + private function validate() { + if (!$this->user->hasPermission('modify', 'module/intarocrm')) { + $this->error['warning'] = $this->language->get('error_permission'); + } + + if (!$this->error) { + return TRUE; + } else { + return FALSE; + } + } + + protected function getOpercartDeliveryMethods() + { + $extensions = $this->model_setting_extension->getInstalled('shipping'); + + foreach ($extensions as $key => $value) { + if (!file_exists(DIR_APPLICATION . 'controller/shipping/' . $value . '.php')) { + $this->model_setting_extension->uninstall('shipping', $value); + + unset($extensions[$key]); + } + } + + $deliveryMethods = array(); + + $files = glob(DIR_APPLICATION . 'controller/shipping/*.php'); + + if ($files) { + foreach ($files as $file) { + $extension = basename($file, '.php'); + + $this->load->language('shipping/' . $extension); + + if ($this->config->get($extension . '_status')) { + $deliveryMethods[] = strip_tags($this->language->get('heading_title')); + } + } + } + + return $deliveryMethods; + } + + protected function getOpercartOrderStatuses() + { + $this->load->model('localisation/order_status'); + return $this->model_localisation_order_status->getOrderStatuses(array()); + } + + protected function getOpercartPaymentTypes() + { + $extensions = $this->model_setting_extension->getInstalled('payment'); + + foreach ($extensions as $key => $value) { + if (!file_exists(DIR_APPLICATION . 'controller/payment/' . $value . '.php')) { + $this->model_setting_extension->uninstall('payment', $value); + + unset($extensions[$key]); + } + } + + $paymentTypes = array(); + + $files = glob(DIR_APPLICATION . 'controller/payment/*.php'); + + if ($files) { + foreach ($files as $file) { + $extension = basename($file, '.php'); + + $this->load->language('payment/' . $extension); + + if ($this->config->get($extension . '_status')) { + $paymentTypes[] = strip_tags($this->language->get('heading_title')); + } + } + } + + return $paymentTypes; + } +} +?> \ No newline at end of file diff --git a/admin/language/english/module/intarocrm.php b/admin/language/english/module/intarocrm.php new file mode 100644 index 0000000..93a17e5 --- /dev/null +++ b/admin/language/english/module/intarocrm.php @@ -0,0 +1,22 @@ + \ No newline at end of file diff --git a/admin/language/russian/module/intarocrm.php b/admin/language/russian/module/intarocrm.php new file mode 100644 index 0000000..1f53831 --- /dev/null +++ b/admin/language/russian/module/intarocrm.php @@ -0,0 +1,22 @@ + \ No newline at end of file diff --git a/admin/view/stylesheet/intarocrm.css b/admin/view/stylesheet/intarocrm.css new file mode 100644 index 0000000..8bf0ceb --- /dev/null +++ b/admin/view/stylesheet/intarocrm.css @@ -0,0 +1,2 @@ +.intarocrm_unit {margin-bottom: 10px;} +.intarocrm_unit input {width: 30%;} \ No newline at end of file diff --git a/admin/view/template/module/intarocrm.tpl b/admin/view/template/module/intarocrm.tpl new file mode 100644 index 0000000..7175972 --- /dev/null +++ b/admin/view/template/module/intarocrm.tpl @@ -0,0 +1,98 @@ + + +
+ + +
+ + + +
+
+

+
+
+
+
+ + +

+
+
+ +
+
+
+ +
+ + + + + +
+ + +

+ +

+ $value): ?> +
+ + +
+ + +

+ + +
+ + +
+ + +

+ $value): ?> +
+ + +
+ + + + + +
+
+
+ +
+ + + \ No newline at end of file diff --git a/catalog/export/intarocrm.php b/catalog/export/intarocrm.php new file mode 100644 index 0000000..02bd6a7 --- /dev/null +++ b/catalog/export/intarocrm.php @@ -0,0 +1,99 @@ +xml(); + } + + private function xml() + { + $this->dd = DOMDocument::loadXML(' + + + '.$this->config->get('config_name').' + + + + + '); + + $this->eCategories = $this->dd->getElementsByTagName('categories')->item(0); + $this->eOffers = $this->dd->getElementsByTagName('offers')->item(0); + + $this->addCategories(); + $this->addOffers(); + return $this->dd->saveXML(); + } + + private function addCategories() + { + $this->language->load('product/category'); + $this->load->model('catalog/category'); + + foreach ($this->model_catalog_category->getCategories() as $category) { + $e = $this->eCategories->appendChild($this->dd->createElement('category', $category['name'])); + $e->setAttribute('id',$category['category_id']); + } + + } + + private function addOffers() + { + $this->load->model('catalog/product'); + $this->load->model('tool/image'); + + foreach ($this->model_catalog_product->getProducts(array()) as $offer) { + $e = $this->eOffers->appendChild($this->dd->createElement('offer')); + $e->setAttribute('id', $offer['product_id']); + $e->setAttribute('productId', $offer['product_id']); + $e->setAttribute('quantity', $offer['quantity']); + $e->setAttribute('available', $offer['status'] ? 'true' : 'false'); + + /* + * DIRTY HACK, NEED TO REFACTOR + */ + + $sql = "SELECT * FROM `" . DB_PREFIX . "product_to_category` WHERE `product_id` = " .$offer['product_id']. ";"; + $result = $this->db->query($sql); + foreach ($result->rows as $row) { + $e->appendChild($this->dd->createElement('categoryId', $row['category_id'])); + } + + $e->appendChild($this->dd->createElement('name'))->appendChild($this->dd->createTextNode($offer['name'])); + $e->appendChild($this->dd->createElement('productName'))->appendChild($this->dd->createTextNode($offer['name'])); + $e->appendChild($this->dd->createElement('vendor'))->appendChild($this->dd->createTextNode($offer['manufacturer'])); + $e->appendChild($this->dd->createElement('price', $offer['price'])); + + if ($offer['image']) { + $image = $this->model_tool_image->resize($offer['image'], $this->config->get('config_image_product_width'), $this->config->get('config_image_product_height')); + } else { + $image = false; + } + + $e->appendChild($this->dd->createElement('picture', $image)); + $e->appendChild($this->dd->createElement('url'))->appendChild($this->dd->createTextNode(HTTP_SERVER . 'order-now/')); + + $sku = $this->dd->createElement('param'); + $sku->setAttribute('name', 'article'); + $sku->appendChild($this->dd->createTextNode($offer['sku'])); + $e->appendChild($sku); + + $weight = $this->dd->createElement('param'); + $weight->setAttribute('name', 'weight'); + $weight->appendChild($this->dd->createTextNode($offer['weight'] . ' ' . $offer['weight_class'])); + $e->appendChild($weight); + + $size = $this->dd->createElement('param'); + $size->setAttribute('name', 'size'); + $size->appendChild($this->dd->createTextNode($offer['length'] .'x'. $offer['width'] .'x'. $offer['height'])); + $e->appendChild($size); + } + } + +} +?> diff --git a/system/library/intarocrm/composer.json b/system/library/intarocrm/composer.json new file mode 100644 index 0000000..202739e --- /dev/null +++ b/system/library/intarocrm/composer.json @@ -0,0 +1,34 @@ +{ + "name": "intarocrm/opencart-module", + "description": "Prestashop integration for IntaroCRM", + "type": "library", + "keywords": ["api", "Intaro CRM", "rest"], + "homepage": "http://www.intarocrm.ru/", + "authors": [ + { + "name": "Alex Lushpai", + "email": "lushpai@intaro.ru", + "role": "Developer" + } + ], + "support": { + "email": "support@intarocrm.ru" + }, + "require": { + "php": ">=5.3", + "intarocrm/rest-api-client": "1.2.*", + "symfony/console": "dev-master", + "monolog/monolog": "dev-master" + }, + "autoload": { + "psr-0": { + "": "/" + } + }, + "repositories": [ + { + "type": "git", + "url": "https://github.com/intarocrm/rest-api-client" + } + ] +} diff --git a/system/library/intarocrm/composer.phar b/system/library/intarocrm/composer.phar new file mode 100755 index 0000000..aa9c50d Binary files /dev/null and b/system/library/intarocrm/composer.phar differ