<?xml version="1.0" encoding="utf-8"?>
<modification>
	<name>RozetkaApi</name>
	<code>RozetkaApi</code>
	<version>1.3</version>
	<author>RozetkaApi</author>
	<link>RozetkaApi</link>
	<file path="admin/controller/catalog/category.php">
		<operation>
			<search index="0">
				<![CDATA[$results = $this->model_catalog_category->getCategories($filter_data);]]>
			</search>
			<add position="after">
				<![CDATA[
				$results_rozetka = $this->model_catalog_category->getCategoriesRozetka($filter_data);
				foreach ($results_rozetka as $result_rozetka) 
				{
				$data['categories_rozetka'][] = array(
				'category_id' => $result_rozetka['category_id'],
				'name_rozetka'        => $result_rozetka['name'],
				
				);
				}
				]]>

			</add>
		</operation>

		<operation>
			<search index="0">
				<![CDATA[$data['entry_parent'] = $this->language->get('entry_parent');]]>
			</search>
			<add position="after">
				<![CDATA[$data['entry_category_rozetka'] = $this->language->get('entry_category_rozetka');]]>

			</add>
		</operation>

		<operation>
			<search index="0">
				<![CDATA[$categories = $this->model_catalog_category->getAllCategories();]]>
			</search>
			<add position="after">
				<![CDATA[
				$categories_rozetka = $this->model_catalog_category->getAllCategoriesRozetka();
				$data['categories_rozetka'] = $this->getAllCategories($categories_rozetka);
				if (isset($category_info)) {
				unset($data['categories'][$category_info['category_id']]);
				}

				if (isset($this->request->post['category_rozetka'])) {
				$data['category_rozetka'] = $this->request->post['category_rozetka'];
				} elseif (!empty($category_info)) {
				$data['category_rozetka'] = $category_info['category_rozetka'];
				} else {
				$data['category_rozetka'] = 0;
				}

				]]>

			</add>
		</operation>


		<operation>
			<search index="0">
				<![CDATA[private function getAllCategories($categories, $parent_id = 0, $parent_name = '')]]>
			</search>
			<add position="before">
				<![CDATA[
				private function getAllCategoriesRozetka($categories_rozetka, $parent_id = 0, $parent_name = '') 
				{
				$output = array();

				if (array_key_exists($parent_id, $categories_rozetka)) 
				{
				if ($parent_name != '') {
				//$parent_name .= $this->language->get('text_separator');
				$parent_name .= ' &gt; ';
				}

				foreach ($categories[$parent_id] as $category) 
				{
				$output[$category['category_id']] = array(
				'category_id' => $category['category_id'],
				'name'        => $parent_name . $category['name']
				);

				$output += $this->getAllCategoriesRozetka($categories_rozetka, $category['category_id'], $parent_name . $category['name']);
				}
				}

				uasort($output, array($this, 'sortByName'));

				return $output;
				}
				
				]]>

			</add>
		</operation>
	</file>

	<file path="admin/model/catalog/category.php">
						<operation>
			<search index="0">
				<![CDATA[public function getTotalCategoriesByLayoutId]]>
			</search>
			<add position="before">
				<![CDATA[
				public function getAllCategoriesRozetka() 
				{
				$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "category_rozetka ORDER BY parent_id, name");

				$category_data = array();
				foreach ($query->rows as $row) 
				{
				$category_data[$row['parent_id']][$row['category_id']] = $row;
				}

				return $category_data;
				}
				]]>

			</add>
		</operation>
				<operation>
			<search index="0">
				<![CDATA[public function editCategory($category_id, $data) {]]>
			</search>
			<add position="before">
				<![CDATA[
	private function catRozIsset($data){ return isset($data['category_rozetka']) ? (int)$data['category_rozetka'] : false; }
				]]>
			</add>
		</operation>
		<operation>
			<search index="0">
				<![CDATA[$this->db->query("UPDATE " . DB_PREFIX . "category SET parent_id = '" . (int)$data['parent_id'] . "',]]>
			</search>
			<add position="replace">
				<![CDATA[
				$this->db->query("UPDATE " . DB_PREFIX . "category SET parent_id = '" . (int)$data['parent_id'] . "', category_rozetka = '" . $this->catRozIsset($data) . "', 
				]]>

			</add>
		</operation>
		
		<operation>
			<search index="0">
				<![CDATA[$this->db->query("INSERT INTO " . DB_PREFIX . "category SET parent_id = '" . (int)$data['parent_id'] . "', ]]>
			</search>
			<add position="replace">
				<![CDATA[
				$this->db->query("INSERT INTO " . DB_PREFIX . "category SET parent_id = '" . (int)$data['parent_id'] . "', category_rozetka = '" . $this->catRozIsset($data) . "', 

				]]>

			</add>
		</operation>

		<operation>
			<search index="0">
				<![CDATA[public function getCategory($category_id) {]]>
			</search>
			<add position="before">
				<![CDATA[
				public function getCategoryRozetka($category_id) {
				$query = $this->db->query("SELECT DISTINCT *, (SELECT GROUP_CONCAT(cd1.name ORDER BY level SEPARATOR '&nbsp;&nbsp;&gt;&nbsp;&nbsp;') FROM " . DB_PREFIX . "category_path cp LEFT JOIN " . DB_PREFIX . "category_description cd1 ON (cp.path_id = cd1.category_id AND cp.category_id != cp.path_id) WHERE cp.category_id = c.category_id AND cd1.language_id = '" . (int)$this->config->get('config_language_id') . "' GROUP BY cp.category_id) AS path, (SELECT DISTINCT keyword FROM " . DB_PREFIX . "url_alias WHERE query = 'category_id=" . (int)$category_id . "') AS keyword FROM " . DB_PREFIX . "category c LEFT JOIN " . DB_PREFIX . "category_description cd2 ON (c.category_id = cd2.category_id) WHERE c.category_id = '" . (int)$category_id . "' AND cd2.language_id = '" . (int)$this->config->get('config_language_id') . "'");

				return $query->row;
				}
				]]>

			</add>
		</operation>


		<operation>
			<search index="0">
				<![CDATA[$sql = "SELECT cp.category_id AS category_id, ]]>
			</search>
			<add position="replace">
				<![CDATA[
				$sql = "SELECT cp.category_id AS category_id, c1.category_rozetka AS category_rozetka,
				]]>

			</add>
		</operation>

		<operation>
			<search index="0">
				<![CDATA[public function getCategoryDescriptions($category_id) {]]>
			</search>
			<add position="before">
				<![CDATA[
				public function getCategoriesRozetka($data = array()) {
				$sql = "SELECT cp.category_id AS category_id, GROUP_CONCAT(c1.name ORDER BY cp.level SEPARATOR '&nbsp;&nbsp;&gt;&nbsp;&nbsp;') AS name, c1.parent_id
				FROM " . DB_PREFIX . "category_rozetka_path cp 
				LEFT JOIN " . DB_PREFIX . "category_rozetka c1 ON (cp.category_id = c1.category_id) 
				LEFT JOIN " . DB_PREFIX . "category_rozetka c2 ON (cp.path_id = c2.category_id)";
				$sql .= " GROUP BY cp.category_id";
				$query = $this->db->query($sql);

				return $query->rows;
				}
				]]>

			</add>
		</operation>



	</file>



	<file path="admin/view/template/catalog/category_form.tpl">

		<operation>
			<search index="6">
				<![CDATA[<div class="form-group">]]>
			</search>
			<add position="before">
				<![CDATA[
				<div class="form-group">
					<label class="col-sm-2 control-label" for="input-category_rozetka"><?php echo $entry_category_rozetka; ?></label>
					<div class="col-sm-10">

						<select name="category_rozetka" class="form-control">
							<option value="0" selected="selected"><?php echo $text_none; ?></option>

							<?php foreach ($categories_rozetka as $category) { ?>
							<?php if ($category['category_id'] == $category_rozetka) { ?>
							<option value="<?php echo $category['category_id']; ?>" selected="selected"><?php echo $category['name']; ?></option>
							<?php } else { ?>
							<option value="<?php echo $category['category_id']; ?>"><?php echo $category['name']; ?></option>
							<?php } ?>
							<?php } ?>
						</select>
					</div>
				</div>
				]]>

			</add>
		</operation>

	</file>

	<file path="admin/language/russian/catalog/category.php">

		<operation>
			<search index="0">
				<![CDATA[$_['entry_name']]]>
			</search>
			<add position="after">
				<![CDATA[
				$_['entry_category_rozetka']          			= 'Название категории rozetka:';
				]]>

			</add>
		</operation>

	</file>
	<file path="admin/language/ru-ru/catalog/category.php">

		<operation>
			<search index="0">
				<![CDATA[$_['entry_name']]]>
			</search>
			<add position="after">
				<![CDATA[
				$_['entry_category_rozetka']          			= 'Название категории rozetka:';
				]]>

			</add>
		</operation>

	</file>




	<file path="admin/controller/sale/order.php">

		<operation>
			<search index="0">
				<![CDATA['shipping_code' => $result['shipping_code'],]]>
			</search>
			<add position="after">
				<![CDATA[
				'from_order'        => $result['from_order'],
				'order_status_rozetka'        => $result['order_status_rozetka'],
				]]>

			</add>
		</operation>
		

	</file>

	<file path="admin/model/sale/order.php">
		<operation>
			<search index="0">
				<![CDATA[$sql = "SELECT o.order_id,]]>
			</search>
			<add position="replace">
				<![CDATA[
				$sql = "SELECT o.order_id, o.from_order, o.order_status_rozetka as order_status_rozetka,
				]]>

			</add>
		</operation>
	</file>
	<file path="admin/view/template/sale/order_list.tpl">

		<operation>
			<search index="0">
				<![CDATA[<td class="text-left"><?php echo $order['order_status']; ?></td>]]>
			</search>
			<add position="after">
				<![CDATA[
				<td class="text-left"><?php echo $order['order_status_rozetka']; ?></td>
				]]>

			</add>
		</operation>
		<operation>
			<search index="0">
				<![CDATA[<td class="text-left"><?php echo $order['date_modified']; ?></td>]]>
			</search>
			<add position="after">
				<![CDATA[
				<td class="text-left"><?php echo $order['from_order']; ?></td>
				]]>

			</add>
		</operation>
		<operation>
			<search index="0">
				<![CDATA[<td class="text-right"><?php if ($sort == 'o.total') { ?>]]>
			</search>
			<add position="before">
				<![CDATA[
				<td class="text-left">Статус Rozetka</td>
				]]>

			</add>
		</operation>
		<operation>
			<search index="0">
				<![CDATA[<td class="text-right"><?php echo $column_action; ?></td>]]>
			</search>
			<add position="before">
				<![CDATA[
				<td class="text-left">Откуда заказ</td>
				]]>

			</add>
		</operation>

	</file>









	<file path="admin/controller/catalog/product.php">
		<operation>
			<search index="0">
				<![CDATA['quantity'   => $result['quantity'],]]>
			</search>
			<add position="after">
				<![CDATA[
				'status_rozetka' => $result['status_rozetka'],
				'sell_statuses_rozetka'=> $result['sell_statuses_rozetka'],
				'moderation_statuses_rozetka'=> $result['moderation_statuses_rozetka'],
				]]>

			</add>
		</operation>
		<operation>
			<search index="0">
				<![CDATA[$data['entry_mpn'] = $this->language->get('entry_mpn');]]>
			</search>
			<add position="after">
				<![CDATA[
				$data['entry_status_rozetka'] = $this->language->get('entry_status_rozetka');
				$data['entry_sell_statuses_rozetka'] = $this->language->get('entry_sell_statuses_rozetka');
				$data['entry_moderation_statuses_rozetka'] = $this->language->get('entry_moderation_statuses_rozetka');
				]]>

			</add>
		</operation>
		<operation>
			<search index="0">
				<![CDATA[if (isset($this->request->post['model'])) {]]>
			</search>
			<add position="before">
				<![CDATA[
				if (isset($this->request->post['status_rozetka'])) {
				$data['status_rozetka'] = $this->request->post['status_rozetka'];
				} elseif (!empty($product_info)) {
				$data['status_rozetka'] = $product_info['status_rozetka'];
				} else {
				$data['status_rozetka'] = '';
				}

				if (isset($this->request->post['sell_statuses_rozetka'])) {
				$data['sell_statuses_rozetka'] = $this->request->post['sell_statuses_rozetka'];
				} elseif (!empty($product_info)) {
				$data['sell_statuses_rozetka'] = $product_info['sell_statuses_rozetka'];
				} else {
				$data['sell_statuses_rozetka'] = '';
				}

				if (isset($this->request->post['moderation_statuses_rozetka'])) {
				$data['moderation_statuses_rozetka'] = $this->request->post['moderation_statuses_rozetka'];
				} elseif (!empty($product_info)) {
				$data['moderation_statuses_rozetka'] = $product_info['moderation_statuses_rozetka'];
				} else {
				$data['moderation_statuses_rozetka'] = '';
				}
				]]>

			</add>
		</operation>
	</file>


	<file path="admin/model/catalog/product.php">
		<operation>
			<search index="0">
				<![CDATA[foreach ($product_attribute['product_attribute_description'] as $language_id => $product_attribute_description) {]]>
			</search>
			<add position="after">
				<![CDATA[
				if (strlen($product_attribute_description['text'])=="")
				continue;

				]]>

			</add>
		</operation>
		<operation>
			<search index="1">
				<![CDATA[foreach ($product_attribute['product_attribute_description'] as $language_id => $product_attribute_description) {]]>
			</search>
			<add position="after">
				<![CDATA[
				if (strlen($product_attribute_description['text'])=="")
				continue;
				]]>

			</add>
		</operation>
	</file>


	<file path="admin/view/template/catalog/product_form.tpl">
		<operation>
			<search index="22">
				<![CDATA[<div class="form-group">]]>
			</search>
			<add position="before">
				<![CDATA[
				<div class="form-group">
					<label class="col-sm-2 control-label" for="input-status_rozetka"><?php echo $entry_status_rozetka; ?></label>
					<div class="col-sm-10">
						<input type="text" name="status_rozetka"  disabled="disabled" value="<?php echo $status_rozetka; ?>" id="input-status_rozetka" class="form-control" />
					</div>
				</div>
				<div class="form-group">
					<label class="col-sm-2 control-label" for="input-sell_statuses_rozetka"><?php echo $entry_sell_statuses_rozetka; ?></label>
					<div class="col-sm-10">
						<input type="text" disabled="disabled" name="sell_statuses_rozetka" value="<?php echo $sell_statuses_rozetka; ?>"  id="input-sell_statuses_rozetka" class="form-control" />
					</div>
				</div>
				<div class="form-group">
					<label class="col-sm-2 control-label" for="input-moderation_statuses_rozetka;"><?php echo $entry_moderation_statuses_rozetka; ?></label>
					<div class="col-sm-10">
						<input type="text" name="moderation_statuses_rozetka;" disabled="disabled" value="<?php echo $moderation_statuses_rozetka; ?>" id="input-moderation_statuses_rozetka;" class="form-control" />
					</div>
				</div>
				]]>

			</add>
		</operation>


		<operation>
			<search index="0">
				<![CDATA[<input type="checkbox" name="product_category[]"]]>
			</search>
			<add position="replace">
				<![CDATA[<input type="checkbox" name="product_category[]" data-rozetka ="<?php echo $category['category_rozetka']; ?>"]]>
			</add>
		</operation>
		<operation>
			<search index="0">
				<![CDATA[<td class="text-left"><?php echo $entry_text; ?></td>]]>
			</search>
			<add position="after">
				<![CDATA[<td><button type="button" onclick="AddAttributeByCategory();" data-toggle="tooltip" title="" class="btn btn-primary" data-original-title="Добавить атрибут"><i class="fa fa-plus-circle"></i></button></td>]]>
			</add>
		</operation>

		<operation>
			<search index="1">
				<![CDATA[<input type="checkbox" name="product_category[]"]]>
			</search>
			<add position="replace">
				<![CDATA[<input type="checkbox" name="product_category[]" data-rozetka ="<?php echo $category['category_rozetka']; ?>"]]>
			</add>
		</operation>


		<operation>
			<search index="0">
				<![CDATA[function attributeautocomplete(attribute_row)]]>
			</search>
			<add position="before">
				<![CDATA[
				function AddAttributeByCategory() 
				{
				document.getElementsByName("product_category[]");
				var buf = "";
				for(i = 0; i < (document.getElementsByName("product_category[]").length); i++)
				{
				if (document.getElementsByName("product_category[]")[i].checked == true)
				buf += document.getElementsByName("product_category[]")[i].getAttribute("data-rozetka")+",";
				}
				buf = buf.substr(0, buf.length-1);
				exist = "";
				var $eles = $(":input[name^='product_attribute']");
				for (i = 1; i < $eles.length; i = i+3)
				exist += $eles[i].value + ",";
				$.ajax(
				{

				url: 'index.php?route=catalog/attribute/getattributesbycategory&token=<?php echo $token; ?>&filter_name='+buf,
				dataType: 'json',
				success: function(response) 
				{

				for (i = 0; i < response.length; i++)
				{
				console.log(response[i]['attribute_id']);
				if (exist.indexOf(response[i]['attribute_id']) > -1)
				continue;
				html  = '<tr id="attribute-row' + attribute_row + '">';
				html += '  <td class="text-left" style="width: 20%;"><input type="text" name="product_attribute[' + attribute_row + '][name]" value="'+response[i]['name']+'" placeholder="<?php echo $entry_attribute; ?>" class="form-control" /><input type="hidden" name="product_attribute[' + attribute_row + '][attribute_id]" value="'+response[i]['attribute_id']+'" /></td>';

				html += '  <td class="text-left">';
				<?php foreach ($languages as $language) { ?>
				html += '<div class="input-group"><span class="input-group-addon"><img src="view/image/flags/<?php echo $language['image']; ?>" title="<?php echo $language['name']; ?>" /></span><textarea name="product_attribute[' + attribute_row + '][product_attribute_description][<?php echo $language['language_id']; ?>][text]" rows="5" placeholder="<?php echo $entry_text; ?>" class="form-control"></textarea></div>';
				<?php } ?>
				html += '  </td>';
				html += '  <td class="text-left"><button type="button" onclick="$(\'#attribute-row' + attribute_row + '\').remove();" data-toggle="tooltip" title="<?php echo $button_remove; ?>" class="btn btn-danger"><i class="fa fa-minus-circle"></i></button></td>';
				html += '</tr>';

				$('#attribute tbody').append(html);

				attribute_row++;
				}
				},
				error: function(xhr) 
				{
				alert("Ошибка RozetkaAPI");
				}
				});


				}
				]]>
			</add>
		</operation>

	</file>

	<file path="admin/controller/catalog/attribute.php">
		
		<operation>
			<search index="0">
				<![CDATA[public function autocomplete() {]]>
			</search>
			<add position="before">
				<![CDATA[
				public function getattributesbycategory()
				{
				$json = array();

				if (isset($this->request->get['filter_name'])) {
				$this->load->model('catalog/attribute');

				$filter_data = array(
				'filter_name' => $this->request->get['filter_name'],
				'start'       => 0,
				'limit'       => 5
				);

				$results = $this->model_catalog_attribute->getAttributesByCategory($filter_data);

				foreach ($results as $result) {
				$json[] = array(
				'attribute_id'    => $result['attribute_id'],
				'name'            => strip_tags(html_entity_decode($result['name'], ENT_QUOTES, 'UTF-8')),
				'attribute_group' => $result['attribute_group']
				);
				}
				}

				$sort_order = array();

				foreach ($json as $key => $value) {
				$sort_order[$key] = $value['name'];
				}

				array_multisort($sort_order, SORT_ASC, $json);

				$this->response->addHeader('Content-Type: application/json');
				$this->response->setOutput(json_encode($json));
				}


				]]>

			</add>
		</operation>

	</file>

	<file path="admin/model/catalog/attribute.php">

		<operation>
			<search index="0">
				<![CDATA[public function getAttributes($data = array()) {]]>
			</search>
			<add position="before">
				<![CDATA[
				public function getAttributesByCategory($data = array())
				{
				$sql = "SELECT *, (SELECT agd.name FROM " . DB_PREFIX . "attribute_group_description agd WHERE agd.attribute_group_id = a.attribute_group_id AND agd.language_id = '" . (int)$this->config->get('config_language_id') . "') AS attribute_group FROM " . DB_PREFIX . "attribute a LEFT JOIN " . DB_PREFIX . "attribute_description ad ON (a.attribute_id = ad.attribute_id) WHERE ad.language_id = '" . (int)$this->config->get('config_language_id') . "'";

				if (!empty($data['filter_name'])) {
				$sql .= " AND a.attribute_group_id in (" . $this->db->escape($data['filter_name']) . ")";
				}
				else
				$sql .= " AND a.attribute_group_id in (0)";

				$query = $this->db->query($sql);

				return $query->rows;

				}
				]]>

			</add>
		</operation>
	</file>
</modification>