<?xml version="1.0" encoding="UTF-8"?>
<modification>
    <name>SmallProductsEditor - fast edit opencart products on one page</name>
    <code>SmallProductsEditor</code>
    <version>20200209 for opencart 2.x</version>
    <author>netruxa</author>
    <link>https://nedorogoi-internet-magazin.ru/opencart/</link>
  <file path="admin/view/template/catalog/product_list.tpl">
    <operation>
      <search><![CDATA[<form action="<?php echo $delete; ?>" method="post" enctype="multipart/form-data" id="form-product">]]></search>
      <add position="before"><![CDATA[
      		<div class="alert alert-success smallproductseditor-success">
	      		<?php echo $smallproductseditor_success; ?>
      		</div>
      		<div class="well smallproductseditor">
	        <div class="row">
	            <div class="col-sm-4 action-wrap">
		      		<select class="form-control">
		      			<option value="*"><?php echo $smallproductseditor_action; ?></option>
		      			<option value="active"><?php echo $smallproductseditor_action_active; ?></option>
		      			<option value="deactive"><?php echo $smallproductseditor_action_deactive; ?></option>
		  				<option value="move"><?php echo $smallproductseditor_action_move; ?></option>
		  				<option value="copy"><?php echo $smallproductseditor_action_copy; ?></option>
		  				<option value="price"><?php echo $smallproductseditor_action_price; ?></option>
					</select>
	           	</div>

	          	<div class="col-sm-4 to-category-wrap">
					<select class="form-control">
		  				<option value="*"></option>
		                <?php foreach ($categories as $category) { ?>
		                <option value="<?php echo $category['category_id']; ?>">&nbsp;&nbsp;<?php echo $category['name']; ?>&nbsp;&nbsp;&nbsp;&nbsp;</option>
		                <?php } ?>
					</select>
				</div>

				<div class="col-sm-4 coefficient-wrap">
					<input type="number" min="0" step="0.1" value="1" class="form-control" />
				</div>

				<div class="col-sm-4 button-wrap">
					<button type="button" class="btn btn-primary"><?php echo $smallproductseditor_button; ?> (<span></span>)</button>
				</div>
			</div>
    	</div>
      ]]></add>
    </operation>
    <operation>
      <search><![CDATA[$('input[name*=\'selected\']').prop('checked', this.checked);]]></search>
      <add position="replace"><![CDATA[$('input[name*=\'selected\']').prop('checked', this.checked);init_smallproductseditor_checked();]]></add>
    </operation>
    <operation>
      <search><![CDATA[<?php echo $footer; ?>]]></search>
      <add position="before"><![CDATA[
		<script type="text/javascript">
		function init_smallproductseditor_checked() {
			if ($('input[name="selected[]"]:checked').size()) {
			    $('.smallproductseditor').show();
			    $('.smallproductseditor .button-wrap button span').text($('input[name="selected[]"]:checked').size());
			}
			else
			 	$('.smallproductseditor').hide();
		}

		$('.smallproductseditor-success, .smallproductseditor, .smallproductseditor .to-category-wrap, .smallproductseditor .button-wrap, .smallproductseditor .coefficient-wrap').hide();

		$('#form-product').on('change','input[name="selected[]"]',init_smallproductseditor_checked);

		$('.smallproductseditor .action-wrap select').change(function(){
			if ($(this).val()=='*')
				$('.smallproductseditor .button-wrap').hide();
			else {
				if ($(this).val()=='move' || $(this).val()=='copy')
				$('.smallproductseditor .to-category-wrap').show();
				else
				$('.smallproductseditor .to-category-wrap').hide();

				if ($(this).val()=='price')
				$('.smallproductseditor .coefficient-wrap').show();
				else
				$('.smallproductseditor .coefficient-wrap').hide();

				$('.smallproductseditor .button-wrap').show();
			}
		});

		$('.smallproductseditor .button-wrap button').click(function(){
			var smallproductseditor_list=[];

			$('input[name="selected[]"]:checked').each(function(){
				smallproductseditor_list.push($(this).val());
			});

			$.ajax({
				type: 'post',
				url: 'index.php?route=catalog/product/smallproductseditor&token=<?php echo $token; ?>',
				data: {action: $('.smallproductseditor .action-wrap select').val(), products: smallproductseditor_list, category: $('.smallproductseditor .to-category-wrap select').val(), coefficient: $('.smallproductseditor .coefficient-wrap input').val()},
				success: function (html) {
					if (html==1) {
						$('#form-product').load(document.location+' #form-product .table-responsive');
						$('.smallproductseditor').slideUp();
						$('.smallproductseditor-success').slideDown().fadeOut(3000);
					}
					else
					alert(html);
				}
			});
		});
		</script>
      ]]></add>
    </operation>
  </file>
  <file path="admin/language/ru*/catalog/product.php">
    <operation>
      <search><![CDATA[<?php]]></search>
      <add position="after"><![CDATA[
$_['smallproductseditor_action'] = '- действия -';
$_['smallproductseditor_action_active'] = 'активировать';
$_['smallproductseditor_action_deactive'] = 'деактивировать';
$_['smallproductseditor_action_move'] = 'перенести в раздел';
$_['smallproductseditor_action_copy'] = 'добавить привязку к разделу';
$_['smallproductseditor_action_price'] = 'умножить цену на коэффициент';
$_['smallproductseditor_button'] = 'Применить';
$_['smallproductseditor_success'] = 'Успешно сохранено';
      ]]></add>
    </operation>
  </file>
  <file path="admin/language/en*/catalog/product.php">
    <operation>
      <search><![CDATA[<?php]]></search>
      <add position="after"><![CDATA[
$_['smallproductseditor_action'] = '- actions -';
$_['smallproductseditor_action_active'] = 'activate';
$_['smallproductseditor_action_deactive'] = 'deactivate';
$_['smallproductseditor_action_move'] = 'move to section';
$_['smallproductseditor_action_copy'] = 'add to section';
$_['smallproductseditor_action_price'] = 'multiply price by coefficient';
$_['smallproductseditor_button'] = 'Apply';
$_['smallproductseditor_success'] = 'Saved success';
      ]]></add>
    </operation>
  </file>
  <file path="admin/controller/catalog/product.php">
    <operation>
      <search><![CDATA[protected function getList() {]]></search>
      <add position="after"><![CDATA[
      	$this->load->model('catalog/category');

      	$filter_data = array(
			'sort'        => 'name',
			'order'       => 'ASC'
		);

      	$data['categories'] = $this->model_catalog_category->getCategories($filter_data);
      ]]></add>
    </operation>
    <operation>
      <search><![CDATA[$data['text_list'] = $this->language->get('text_list');]]></search>
      <add position="before"><![CDATA[
      	$data['smallproductseditor_action'] = $this->language->get('smallproductseditor_action');
      	$data['smallproductseditor_action_active'] = $this->language->get('smallproductseditor_action_active');
      	$data['smallproductseditor_action_deactive'] = $this->language->get('smallproductseditor_action_deactive');
      	$data['smallproductseditor_action_move'] = $this->language->get('smallproductseditor_action_move');
      	$data['smallproductseditor_action_copy'] = $this->language->get('smallproductseditor_action_copy');
      	$data['smallproductseditor_action_price'] = $this->language->get('smallproductseditor_action_price');
      	$data['smallproductseditor_button'] = $this->language->get('smallproductseditor_button');
      	$data['smallproductseditor_success'] = $this->language->get('smallproductseditor_success');
      ]]></add>
    </operation>
    <operation>
      <search><![CDATA[public function autocomplete() {]]></search>
      <add position="before"><![CDATA[
      	public function smallproductseditor() {
			if (isset($this->request->post['action']) && $this->validateDelete()) {
				$this->load->model('catalog/product');

				switch ($this->request->post['action']) {
				    case 'active':
						$this->model_catalog_product->editProductsStatus($this->request->post['products'], 1);
				        break;
				    case 'deactive':
						$this->model_catalog_product->editProductsStatus($this->request->post['products'], 0);
				        break;
				    case 'move':
						$this->model_catalog_product->deleteProductsCategory($this->request->post['products']);
		            case 'move':
					case 'copy':
						if ($this->request->post['category']!='*')
						$this->model_catalog_product->addProductsCategory($this->request->post['products'], $this->request->post['category']);
				        break;
					case 'price':
						$this->model_catalog_product->editProductsPrice($this->request->post['products'], str_replace(',','.',$this->request->post['coefficient']));
				        break;
				}

				$this->response->setOutput(1);
			}
			else {
				$this->response->setOutput('error post request or access to products');
			}
		}
      ]]></add>
    </operation>
  </file>
  <file path="admin/model/catalog/product.php">
    <operation>
      <search><![CDATA[public function getTotalProductsByLayoutId($layout_id) {]]></search>
      <add position="before"><![CDATA[
		public function editProductsStatus($products, $status) {
			$this->db->query("UPDATE " . DB_PREFIX . "product SET status = '" . (int)$status . "' WHERE product_id in (" . implode(',',$products) . ")");
		}

		public function deleteProductsCategory($products) {
			$this->db->query("DELETE FROM " . DB_PREFIX . "product_to_category WHERE product_id in (" . implode(',',$products) . ")");
		}

		public function addProductsCategory($products, $category_id) {
			foreach ($products as $product_id) {
				$this->db->query("INSERT IGNORE INTO " . DB_PREFIX . "product_to_category SET product_id = '" . (int)$product_id . "', category_id = '" . (int)$category_id . "'");
			}
		}

		public function editProductsPrice($products, $coefficient) {
			$this->db->query("UPDATE " . DB_PREFIX . "product SET price = price * " . $coefficient . " WHERE product_id in (" . implode(',',$products) . ")");
		}
      ]]></add>
    </operation>
  </file>
</modification>