AnonSec Shell
Server IP : 54.36.91.62  /  Your IP : 216.73.217.112
Web Server : Apache
System : Linux webm013.cluster127.gra.hosting.ovh.net 5.15.206-ovh-vps-grsec-zfs-classid #1 SMP Fri May 15 02:41:25 UTC 2026 x86_64
User : coopiak ( 151928)
PHP Version : 8.3.23
Disable Function : _dyuweyrj4,_dyuweyrj4r,dl
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : ON  |  Sudo : OFF  |  Pkexec : OFF
Directory :  /home/coopiak/amisdesseniors-fr/bordeaux/administrator/components/com_djcatalog2/models/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ HOME ]     

Current File : /home/coopiak/amisdesseniors-fr/bordeaux/administrator/components/com_djcatalog2/models//item.php
<?php
/**
 * @package DJ-Catalog2
 * @copyright Copyright (C) DJ-Extensions.com, All rights reserved.
 * @license http://www.gnu.org/licenses GNU/GPL
 * @author url: http://dj-extensions.com
 * @author email contact@dj-extensions.com
 */

use Joomla\CMS\Factory as Factory;
use Joomla\CMS\Language\Text as Text;
use Joomla\Registry\Registry;
use Joomla\Utilities\ArrayHelper;
use Joomla\CMS\MVC\Model\AdminModel;
use Joomla\CMS\Table\Table;
use Joomla\CMS\Language\Associations;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Application\ApplicationHelper;
use Joomla\CMS\Filesystem\Path;
use Joomla\CMS\Language\LanguageHelper;
// No direct access.
defined('_JEXEC') or die();

jimport('joomla.application.component.helper');

class Djcatalog2ModelItem extends AdminModel
{
	protected $text_prefix = 'COM_DJCATALOG2';

	protected $_cart_attributes = array();
	protected $_customisations = array();
	protected $_customisationsGroups = array();

	// AdminModel needs to know this for storing the associations
	protected $associationsContext = 'com_djcatalog2.item';

	public function __construct($config = array()) {
		//$config['event_after_save'] = 'onItemAfterSave';
		//$config['event_after_delete'] = 'onItemAfterDelete';
		parent::__construct($config);
	}

	public function getTable($type = 'Items', $prefix = 'Djcatalog2Table', $config = array())
	{
		return Table::getInstance($type, $prefix, $config);
	}
	public function getForm($data = array(), $loadData = true)
	{
		// Initialise variables.
		$app	= Factory::getApplication();

		// Get the form.
		$form = $this->loadForm('com_djcatalog2.item', 'item', array('control' => 'jform', 'load_data' => $loadData));
		if (empty($form)) {
			return false;
		}

		return $form;
	}

	public function getItem($pk = null) {
		if ($item = parent::getItem($pk)) {
			$db = Factory::getDbo();


			if ((!isset($item->categories) || !is_array($item->categories)) && isset($item->id)){
				$db->setQuery('SELECT category_id FROM #__djc2_items_categories WHERE item_id=\''.$item->id.'\'');
				$item->categories = $db->loadColumn();
			}

			if (!isset($item->location) || !is_array($item->location)) {
				$location = array('address' => null, 'city' => null, 'postcode' => null, 'country' => null, 'state' => null, 'latitude' => null, 'longitude' => null, 'phone' => null, 'fax' => null, 'mobile' => null, 'website'=> null, 'email' => null );
				foreach($location as $k=>$v) {
					if (isset($item->$k)) {
						$location[$k] = $item->$k;
					}
				}
				$item->location = $location;
			}

			if (!is_array($item->group_id)) {
				$query = $db->getQuery(true);

				/*$query->select ('distinct f.group_id');
				$query->from('#__djc2_items_extra_fields AS f');
				$query->join('LEFT','#__djc2_items_extra_fields_values_text AS vt ON f.id=vt.field_id AND vt.item_id='.(int)$item->id);
				$query->join('LEFT','#__djc2_items_extra_fields_values_int AS vi ON f.id=vi.field_id AND vi.item_id='.(int)$item->id);
				$query->join('LEFT','#__djc2_items_extra_fields_values_date AS vd ON f.id=vd.field_id AND vd.item_id='.(int)$item->id);
				$query->where('vt.value IS NOT NULL OR vi.value IS NOT NULL OR vd.value IS NOT NULL');
				*/
				$query->select('distinct group_id');
				$query->from('#__djc2_items_groups');
				$query->where('item_id='.(int)$item->id);

				$db->setQuery($query);
				$item->group_id = $db->loadColumn();
			}

			if (!isset($item->combinations) && $item->id) {
				$query = $db->getQuery(true);
				$query->select('*');
				$query->from('#__djc2_items_combinations');
				$query->where('item_id = '.$item->id);
				$db->setQuery($query);

				$item->combinations = $db->loadObjectList('id');
				if (count($item->combinations)) {
					$ids = array_keys($item->combinations);

					$query = $db->getQuery(true);
					$query->select('i.id, img.fullname as item_image, img.caption AS image_caption, img.path AS image_path, img.fullpath AS image_fullpath');
					$query->from('#__djc2_items_combinations as i');
					$query->join('inner', '#__djc2_images as img on img.id=(select id from #__djc2_images where type=\'combination\' and item_id=i.id order by ordering asc limit 1)');
					$query->where('i.id IN ('.implode(',', $ids).')');
					$db->setQuery($query);
					$image_list = $db->loadObjectList('id');

					foreach ($item->combinations as &$combination) {

						$combination->image = null;
						if (isset($image_list[$combination->id])) {
							$combination->image = DJCatalog2ImageHelper::getImageUrl($image_list[$combination->id]->image_fullpath, 'thumb');
						}

						$query = $db->getQuery(true);
						$query->select('cf.*, f.group_id');
						$query->from('#__djc2_items_combinations_fields AS cf');
						$query->join('left', '#__djc2_items_extra_fields AS f ON f.id=cf.field_id');
						$query->where('cf.combination_id=' . $combination->id);
						$db->setQuery($query);
						$combination->fields = $db->loadObjectList();
					}
					unset($combination);
				}
			} else if (!$item->id) {
				$item->combinations = null;
			}

			if (!isset($item->price_tiers)) {
				$query = $db->getQuery(true);
				$query->select('*')->from('#__djc2_items_price_tiers')->where('item_id='.(int)$item->id)->order('quantity ASC');
				$db->setQuery($query);
				$item->price_tiers = $db->loadAssocList();
			}

			if (!isset($item->customisations) && $item->id) {

				$query = $db->getQuery(true);

				$query
					->select(array(
						'ic.*',
						'c.name',
						'g.name AS group_name',
						'ic.group_id AS group_id'
					))
					->from($db->quoteName('#__djc2_items_customisations', 'ic'))
					->join('INNER', $db->quoteName('#__djc2_customisations', 'c') . ' ON c.id = ic.customisation_id')
					->join('LEFT', $db->quoteName('#__djc2_customisations_groups', 'g') . ' ON g.id = ic.group_id')
					->where('item_id = '.$item->id)
					->order('g.id DESC, c.ordering ASC');
				$db->setQuery($query);


				$item->customisations = $db->loadObjectList();
			} else if (!$item->id) {
				$item->customisations = null;
			}

			$db->setQuery('SELECT group_id FROM #__djc2_items_customisations_groups WHERE item_id = ' . (int) $item->id);
			$item->customgroup = $db->loadColumn();

			if ((!isset($item->labels) || !is_array($item->labels)) && isset($item->id)){
				$db->setQuery('SELECT label_id FROM #__djc2_labels_items WHERE item_id=\''.$item->id.'\'');
				$item->labels = $db->loadColumn();
			}



			if (property_exists($item, 'grant_usergroups'))
			{
				$item->grant_usergroups = ($item->grant_usergroups && $item->grant_usergroups != '[]') ? json_decode((string)$item->grant_usergroups) : array();
			}



			/*
			 * Multilang
			 */
			if (Associations::isEnabled())
			{
				$item->associations = array();

				if ($item->id != null)
				{
					$associations = Associations::getAssociations('com_djcatalog2', '#__djc2_items', 'com_djcatalog2.item', (int) $item->id, 'id', '', '');

					foreach ($associations as $tag => $association)
					{
						$item->associations[$tag] = $association->id;
					}
				}
			}



			return $item;
		} else {
			return false;
		}
	}

	protected function loadFormData()
	{
		$data = Factory::getApplication()->getUserState('com_djcatalog2.edit.item.data', array());
		$filters = (array) Factory::getApplication()->getUserState('com_djcatalog2.items.filter');

		if (empty($data)) {
			$data = $this->getItem();
		}
		if($data->parent_id == null) {
			if(isset($filters['parent']) && $filters['parent'] != null) {
				$data->parent_id = $filters['parent'];
			}
		}
		// Assign parent id based on filters
		return $data;
	}

	protected function prepareTable($table)
	{
		jimport('joomla.filter.output');
		$date = Factory::getDate();
		$user = Factory::getUser();
		$db = Factory::getDbo();
		$app = Factory::getApplication();

		$table->name		= htmlspecialchars_decode($table->name, ENT_QUOTES);
		$table->alias		= ApplicationHelper::stringURLSafe($table->alias);

		$table->price = floatval($table->price);
		$table->special_price = floatval($table->special_price);
		if (\floatval($table->special_price) != \floatval(0)) {
			$start_promotion = Factory::getDate();
			if (\intval($table->start_promotion) == 0) {
				$table->start_promotion = $start_promotion->toSql();
			}
			if (\intval($table->end_promotion) == 0) {
				$params = ComponentHelper::getParams('com_djcatalog2');
				$end_promotion_days = $params->get('special_price_default_duration_days', 14);
				$end_promotion = Factory::getDate('now + '. \intval($end_promotion_days).' days');
				$table->end_promotion = $end_promotion->toSql();
			}
		} else {
			$table->start_promotion = $db->getNullDate();
			$table->end_promotion = $db->getNullDate();
		}
		$table->producer_id = (int)$table->producer_id;
		$table->hits = (int)$table->hits;
		$table->tax_rule_id = (int)$table->tax_rule_id;

		$table->latitude = (trim((string)$table->latitude) == '') ? null : $table->latitude;
		$table->longitude = (trim((string)$table->longitude) == '') ? null : $table->longitude;
		$table->stock = (trim((string)$table->stock) == '') ? null : $table->stock;
		$table->length = (trim((string)$table->length) == '') ? null : $table->length;
		$table->width = (trim((string)$table->width) == '') ? null : $table->width;
		$table->height = (trim((string)$table->height) == '') ? null : $table->height;
		$table->weight = (trim((string)$table->weight) == '') ? null : $table->weight;
		$table->publish_up = (trim((string)$table->publish_up) == '') ? null : $table->publish_up;
		$table->publish_down = (trim((string)$table->publish_down) == '') ? null : $table->publish_down;

		// TODO - just temporary
		$table->group_id = 0;

		if (empty($table->alias)) {
			$table->alias = ApplicationHelper::stringURLSafe($table->name);
		}
		if (empty($table->cat_id)) {
			$table->cat_id = 0;
		}

		if (empty($table->id)) {
			if (empty($table->ordering)) {
				$db->setQuery('SELECT MAX(ordering) FROM #__djc2_items WHERE cat_id = '.$table->cat_id);
				$max = $db->loadResult();

				$table->ordering = $max+1;
			}
		}

		if ($app->input->getCmd('task') != 'import' && ($table->latitude == 0 || $table->longitude == 0)) {
			require_once Path::clean(JPATH_ROOT.'/administrator/components/com_djcatalog2/lib/geocode.php');

			$address = array();

			if (!empty($table->address)) {
				$address[] = $table->address;
			}
			if (!empty($table->city)) {
				$address[] = $table->city;
			}
			if (!empty($table->postcode)) {
				$address[] = $table->postcode;
			}
			if (!empty($table->country)) {
				$db->setQuery('select country_name from #__djc2_countries where id='.(int)$table->country);
				$country = $db->loadResult();
				if ($country) {
					$address[] = $country;
				}
			}

			$address_str = implode(',', $address);
			if ($address_str) {
				if ($coords = DJCatalog2Geocode::getLocation($address_str)) {
					$table->latitude = (!empty($coords['lat'])) ? $coords['lat'] : null;
					$table->longitude = (!empty($coords['lng'])) ? $coords['lng'] : null;
				}
			}
		}
	}

	protected function getReorderConditions($table)
	{
		$condition = array();
		$condition[] = 'cat_id = '.(int) $table->cat_id;
		$condition[] = 'parent_id = '.(int) $table->parent_id;

		return implode(' AND ',  $condition);

		return $condition;
	}

	public function validate($form,$data,$group = null){
		return parent::validate($form,$data,$group);
	}
	public function validateAttributes($data, &$table) {
		$db = Factory::getDbo();

		//$db->setQuery('select * from #__djc2_items_extra_fields where required=1 AND (group_id=0 OR group_id='.(int)$table->group_id.')');

		$group_ids = array();
		if (!empty($table->group_id) && is_array($table->group_id)) {
			$group_ids = $table->group_id;
			Joomla\Utilities\ArrayHelper::toInteger($group_ids);
		}
		$group_ids[] = 0;
		$group_ids = array_unique($group_ids);

		$db->setQuery('select * from #__djc2_items_extra_fields where (language='.$db->quote($table->language).' OR language='.$db->quote('*').') AND required=1 AND group_id IN ('.implode(',', $group_ids).')');

		$required_fields = $db->loadObjectList();

		if (count($required_fields) == 0) {
			return true;
		}

		$all_valid = true;

		foreach($required_fields as $field) {
			$field_id = $field->id;
			$valid = false;
			if (isset($data[$field_id])) {
				if (is_array($data[$field_id])) {
					foreach($data[$field_id] as $option) {
						if (!empty($option)) {
							$valid = true;
							break;
						}
					}
				} else {
					if (!empty($data[$field_id])) {
						$valid = true;
					}
				}
			}
			if (!$valid) {
				$all_valid = false;
				$message = Text::_($field->name);
				$message = Text::sprintf('JLIB_FORM_VALIDATE_FIELD_INVALID', $message);
				$this->setError($message);
			}
		}

		return $all_valid;

	}

	public function getFields() {
		$item = $this->getItem();

		$itemId = $item->id;

		$db = Factory::getDbo();

		$query = $db->getQuery(true);
		$query->select('f.*, g.name as group_name');
		$query->from('#__djc2_items_extra_fields AS f');
		$query->select('CASE '
			.'WHEN (f.type=\'text\' OR f.type=\'textarea\' OR f.type=\'html\') '
			.'THEN vt.value '
			.'WHEN (f.type=\'calendar\') '
			.'THEN vd.value '
			.'WHEN (f.type=\'checkbox\' OR f.type=\'select\' OR f.type=\'multiselect\' OR f.type=\'radio\') '
			.'THEN GROUP_CONCAT(vi.value SEPARATOR \'|\')'
			.'ELSE "" END AS field_value');
		$query->join('LEFT','#__djc2_items_extra_fields_values_text AS vt ON f.id=vt.field_id AND vt.item_id='.(int)$itemId);
		$query->join('LEFT','#__djc2_items_extra_fields_values_int AS vi ON f.id=vi.field_id AND vi.item_id='.(int)$itemId);
		$query->join('LEFT','#__djc2_items_extra_fields_values_date AS vd ON f.id=vd.field_id AND vd.item_id='.(int)$itemId);
		$query->join('LEFT', '#__djc2_items_extra_fields_groups as g ON g.id = f.group_id');

		$query->where('f.group_id='.(int)$this->groupId.' OR f.group_id=0');
		$query->group('f.id');
		$query->order('f.group_id asc, f.ordering asc');
		//echo str_replace('#_', 'jos', (string)$query);die();
		$db->setQuery($query);

		$fields = ($db->loadObjectList('id'));

		$groupped_fields = array();

		if (count($fields)) {
			$fieldIds = array_keys($fields);
			$db->setQuery('SELECT * FROM #__djc2_items_extra_fields_options WHERE field_id IN ('.implode(',', $fieldIds).') ORDER BY field_id ASC, ordering ASC');
			$optionList = $db->loadObjectList();

			foreach($fields as $field_id => $field) {
				foreach ($optionList as $optionRow) {
					if ($optionRow->field_id == $field_id) {
						if (empty($field->optionlist)) {
							$fields[$field_id]->optionlist = array();
						}
						$fields[$field_id]->optionlist[] = $optionRow;
					}
				}

				if (array_key_exists($field->group_id, $groupped_fields) == false) {
					$groupped_fields[$field->group_id] 			= new stdClass();
					$groupped_fields[$field->group_id]->id 		= $field->group_id;
					$groupped_fields[$field->group_id]->name 	= ($field->group_id) > 0 ? $field->group_name : Text::_('COM_DJCATALOG2_FIELD_GROUP_COMMON');
					$groupped_fields[$field->group_id]->fields 	= array();
				}
				$groupped_fields[$field->group_id]->fields[$field_id] = $fields[$field_id];
			}
		}

		return $groupped_fields;
	}

	function getCartAttributes() {
		if (empty($this->_cart_attributes)) {
			$db = Factory::getDbo();

			$query = $db->getQuery(true);
			$query->select('f.*, g.name as group_name, g.label as group_label, g.id as fgroup_id');
			$query->from('#__djc2_items_extra_fields as f');
			$query->join('LEFT', '#__djc2_items_extra_fields_groups as g ON g.id=f.group_id');
			$query->where('f.cart_variant=1');
			$query->order('IFNULL(g.ordering,0) asc , g.ordering asc, f.ordering asc');
			$db->setQuery($query);

			$this->_cart_attributes = $db->loadObjectList('id');

			if (count($this->_cart_attributes)) {
				$query = $db->getQuery(true);
				$query->select('*');
				$query->from('#__djc2_items_extra_fields_options');
				$query->where('field_id in ('.implode(',',array_keys($this->_cart_attributes)).')');
				$query->order('field_id asc, ordering asc');

				$db->setQuery($query);
				$optionslist = $db->loadObjectList();

				foreach ($this->_cart_attributes as $field_id => $field) {
					$field_options = array();
					$field_optionValues = array();
					$field_optionParams = array();

					foreach($optionslist as $k => $option) {
						if ($option->field_id == $field_id) {
							$field_options[] = $option->id;
							$field_optionValues[$option->id] = $option->value;

							$optionParam = $option->params;
							if (!empty($optionParam)) {
								$optionParam = new Registry($optionParam);
							}
							$field_optionParams[$option->id] = $optionParam;

						}
					}

					$this->_cart_attributes[$field_id]->options = $field_options;//implode('|', $field_options);
					$this->_cart_attributes[$field_id]->optionValues = $field_optionValues;//implode('|', $field_optionValues);
					$this->_cart_attributes[$field_id]->optionParams = $field_optionParams;
				}
			}

		}

		return $this->_cart_attributes;
	}

	function getCustomisationsGroups() {
		if (empty($this->_customisationsGroups)) {
			$db = Factory::getDbo();



			$query = $db->getQuery(true);

			$query
				->select(array(
					'g.id as group_id',
					'g.name as group_name',
					'c.id as custom_id',
					'c.name',
					'c.price',
					'c.min_quantity',
					'c.max_quantity',
				))
				->from($db->quoteName('#__djc2_customisations_groups', 'g'))
				->join('INNER', $db->quoteName('#__djc2_customisations_groups_fields', 'gc') . ' ON gc.group_id = g.id')
				->join('INNER', $db->quoteName('#__djc2_customisations', 'c') . ' ON c.id = gc.field_id');


			$db->setQuery($query);

			$items = $db->loadObjectList();
			$groups = array();
			if(count($items)) {
				foreach ($items as $item) {
					$groups[$item->group_id]['name'] = $item->group_name;
					$groups[$item->group_id]['group_id'] = $item->group_id;
					$groups[$item->group_id]['items'][$item->custom_id] = array(
						'id' => $item->custom_id,
						'price' => $item->price,
						'min_quantity' => $item->min_quantity,
						'max_quantity' => $item->max_quantity
					);
				}
			}

			$this->_customisationsGroups = $groups;
		}

		return $this->_customisationsGroups;
	}

	function getCustomisations() {
		if (empty($this->_customisations)) {
			$db = Factory::getDbo();

			$query = $db->getQuery(true);

			$query
				->select(array(
					'c.*',
					'g.name AS group_name',
				))
				->from($db->quoteName('#__djc2_customisations', 'c'))
				->join('LEFT', $db->quoteName('#__djc2_customisations_groups_fields', 'gref') . ' ON gref.field_id = c.id')
				->join('LEFT', $db->quoteName('#__djc2_customisations_groups', 'g') . ' ON g.id = gref.group_id')
				->where('c.type='.$db->quote('i'))
				->order('g.id DESC, c.ordering ASC');
			$db->setQuery($query);

			$items = $db->loadObjectList();

			/*foreach($items as &$item) {
				$item->input_params = new Registry($item->input_params);
				$item->params = new Registry($item->params);
			}
			unset($item);*/

			$this->_customisations = $items;
		}

		return $this->_customisations;
	}

	public function saveAttributes($data, &$table) {
		$db = Factory::getDbo();
		$app = Factory::getApplication();
		$task = $app->input->getCmd('task');

		//if (!empty($data) ) {
		$non_empty_fields = array(0);

		if (!empty($data)) {
			foreach ($data as $k=>$v) {
				if (!empty($v)) {
					if (is_string($v) && trim((string)$v) != '' && $task == 'import' && trim((string)$v) == '__{{UNSET}}__') {
						unset($data[$k]);
					}
					$non_empty_fields[] = (int)$k;
				}
			}
		}

		$non_empty_fields = array_unique($non_empty_fields);
		$non_empty_fields_ids = implode(',', $non_empty_fields);


		if($non_empty_fields_ids != 0){
			$query = $db->getQuery(true);
			$query->delete();
			$query->from('#__djc2_items_extra_fields_values_text');
			//$query->where('item_id ='.$table->id.' and field_id not in (select id from #__djc2_items_extra_fields where group_id = '.$table->group_id.' or group_id = 0)');

			$query->where('item_id ='.$table->id.' and field_id not in ('.$non_empty_fields_ids.')');

			$db->setQuery($query);
			$db->execute();

			$query = $db->getQuery(true);
			$query->delete();
			$query->from('#__djc2_items_extra_fields_values_int');
			//$query->where('item_id ='.$table->id.' and field_id not in (select id from #__djc2_items_extra_fields where group_id = '.$table->group_id.' or group_id = 0)');

			$query->where('item_id ='.$table->id.' and field_id not in ('.$non_empty_fields_ids.')');

			$db->setQuery($query);
			$db->execute();


			$query = $db->getQuery(true);
			$query->delete();
			$query->from('#__djc2_items_extra_fields_values_date');
			//$query->where('item_id ='.$table->id.' and field_id not in (select id from #__djc2_items_extra_fields where group_id = '.$table->group_id.' or group_id = 0)');

			$query->where('item_id ='.$table->id.' and field_id not in ('.$non_empty_fields_ids.')');

			$db->setQuery($query);
			$db->execute();
		}
		//if ($task != 'import') {

		//}

		if (empty($data)) {
			return true;
		}

		$query = $db->getQuery(true);
		$query->select('ef.*');
		$query->from('#__djc2_items_extra_fields as ef');
		//$query->where('ef.group_id='.$table->group_id.' OR ef.group_id=0');
		$query->where('ef.id in ('.$non_empty_fields_ids.')');
		$db->setQuery($query);

		$attribs = $db->loadObjectList();
		$itemId = $table->id;
		$rows = array();

		$text_types = array('text','textarea','html', 'media');
		$int_types = array('select', 'multiselect', 'checkbox','radio', 'color', 'multicolor', 'bool');
		$date_types = array('calendar');
		/*
		foreach ($attribs as $k=>$v) {
			$fieldId = $v->id;
			$className =  DJCatalog2CustomField.ucfirst($v->type);
			if (class_exists($className) == false ){
				continue;
			}

			$field = new $className($fieldId, $itemId, $v->name, $v->required);

			if (array_key_exists($fieldId, $data) && !empty($data[$fieldId])) {
				$field->setValue($data[$fieldId]);
				$field->save();
			} else {
				$field->delete();
			}
		}

		return true;*/

		foreach ($attribs as $k=>$v) {
			$fv_table = null;
			$type_table_name = null;
			$table_type = null;
			if (in_array($v->type, $text_types)) {
				$fv_table = Table::getInstance('FieldValuesText', 'Djcatalog2Table', array());
				$type_table_name = '#__djc2_items_extra_fields_values_text';
				$table_type = 'text';
			} else if (in_array($v->type, $int_types)) {
				$fv_table = Table::getInstance('FieldValuesInt', 'Djcatalog2Table', array());
				$type_table_name = '#__djc2_items_extra_fields_values_int';
				$table_type = 'int';
			} else if (in_array($v->type, $date_types)) {
				$fv_table = Table::getInstance('FieldValuesDate', 'Djcatalog2Table', array());
				$type_table_name = '#__djc2_items_extra_fields_values_date';
				$table_type = 'date';
			} else {
				continue;
			}
			$fieldId = $v->id;
			if (array_key_exists($fieldId, $data) && isset($data[$fieldId])) {
				// add/alter data
				$value = null;
				$id = null;

				if (is_array($data[$fieldId])) {
					$db->setQuery('
									SELECT id 
									FROM '.$type_table_name.' 
									WHERE 
										item_id='.(int)$itemId.' 
										AND field_id='.$fieldId. ' order by id '
					);
					$values = $db->loadColumn();
					$count = (count($values) > count($data[$fieldId])) ? count($values) : count($data[$fieldId]);
					for ($i = 0; $i < $count; $i++) {
						if (isset($data[$fieldId][$i])) {
							$id = null;
							if (isset($values[$i])) {
								$id = $values[$i];
							}

							$rows[] = array(
								'id'=>$id,
								'item_id'=>$itemId,
								'field_id'=>$fieldId,
								'value' => $data[$fieldId][$i],
								'type' => $table_type
							);
						} else {
							$db->setQuery('
								DELETE 
								FROM '.$type_table_name.' 
								WHERE id='.(int)$values[$i]
							);
							$db->execute();
						}
					}

				} else {
					if ($v->type == 'html') {
						$data[$fieldId] = ComponentHelper::filterText($data[$fieldId]);
						$data[$fieldId] = preg_replace('/&(?![A-Za-z0-9#]{1,7};)/','&amp;',$data[$fieldId]);
					}
					if ($fv_table->load(array('item_id'=>$itemId,'field_id'=>$fieldId))) {
						$id = $fv_table->id;
					}
					$rows[] = array(
						'id'=>$id,
						'item_id'=>$itemId,
						'field_id'=>$fieldId,
						'value' => $data[$fieldId],
						'type' => $table_type
					);
				}

			} else if ($task != 'import'){
				// remove data
				$db->setQuery('
								DELETE 
								FROM '.$type_table_name.' 
								WHERE 
									field_id='.(int)$fieldId.' 
									AND item_id='.(int)$itemId
				);
				$db->execute();
			}
		}

		foreach ($rows as $key=>$row) {
			$fv_table = null;
			if (isset($row['type'])) {
				if ($row['type'] == 'text' || $row['type'] == 'int' || $row['type'] == 'date') {
					$fv_table = Table::getInstance('FieldValues'.ucfirst($row['type']), 'Djcatalog2Table', array());
					unset($row['type']);
				} else{
					continue;
				}
			} else {
				continue;
			}

			$isNew = true;
			// Load the row if saving an existing record.
			if ($row['id'] > 0) {
				$fv_table->load($row['id'], true);
				$isNew = false;
			}

			// Bind the data.
			if (!$fv_table->bind($row)) {
				$this->setError($fv_table->getError());
				return false;
			}
			// Check the data.
			if (!$fv_table->check()) {
				$this->setError($fv_table->getError());
				return false;
			}

			// Store the data.
			if (!$fv_table->store()) {
				$this->setError($fv_table->getError());
				return false;
			}

		}
		//}
		return true;
	}

	public function saveCombinations($data, &$table, $isNew) {



		$item 	= $this->getItem($table->id);
		$app = Factory::getApplication();
		$task = $app->input->getCmd('task');
//        print_r($item->combinations);
		$ids 		= (isset($data['id'])) ? (array)$data['id'] : array();
		$sku 		= (isset($data['sku'])) ? (array)$data['sku'] : array();
		$gtin 		= (isset($data['gtin'])) ? (array)$data['gtin'] : array();
		$price 		= (isset($data['price'])) ? (array)$data['price'] : array();
		$stock 		= (isset($data['stock'])) ? (array)$data['stock'] : array();
		$default 		= (isset($data['default'])) ? (array)$data['default'] : array();


		$length 		= (isset($data['length'])) ? (array)$data['length'] : array();
		$width 			= (isset($data['width'])) ? (array)$data['width'] : array();
		$height 		= (isset($data['height'])) ? (array)$data['height'] : array();
		$weight 		= (isset($data['weight'])) ? (array)$data['weight'] : array();

		$attribute 	= (isset($data['attribute'])) ? (array)$data['attribute'] : array();

		$db = Factory::getDbo();

		if (count($item->combinations)) {
			$toDelete = array();
			foreach($item->combinations as $key => $combination) {
				if (!in_array($combination->id, $ids)) {
					$toDelete[] = $combination->id;
				} /*else {
					$tmp = array();
					foreach($combination->fields as $field) {
						$tmp[] = array('field_id'=>(int)$field->field_id, 'value'=>(int)$field->value);
					}
					$existing[] = $tmp;
				}*/
			}

			if (count($toDelete)) {
				$query = $db->getQuery(true);
				$query->delete('#__djc2_items_combinations')->where('id IN ('.implode(',', $toDelete).')');
				$db->setQuery($query);
				$db->execute();

				$query = $db->getQuery(true);
				$query->delete('#__djc2_items_combinations_fields')->where('combination_id IN ('.implode(',', $toDelete).')');
				$db->setQuery($query);
				$db->execute();

				foreach($toDelete as $toDeleteId) {
					DJCatalog2ImageHelper::deleteImages('combination', $toDeleteId);
				}
			}
		}

		$existing = array ();

		if (count($ids)) {
			foreach($ids as $key => $combination_id) {
				$newCombo = (int)$combination_id ? false : true;
				if ($task == 'save2copy') {
					$newCombo = true;
				}
				$combination = new stdClass();
				$combination->id = $newCombo ? 0 : (int)$combination_id;
				$combination->item_id = $table->id;
				$combination->sku = isset($sku[$key]) ? $sku[$key] : '';
				$combination->gtin = isset($gtin[$key]) ? $gtin[$key] : '';
				$combination->price = isset($price[$key]) ? floatval($price[$key]) : 0.0;
				$combination->stock = isset($stock[$key]) ? floatval($stock[$key]) : 0.0;

				$combination->length = isset($length[$key]) ? $length[$key] : 0;
				$combination->width = isset($width[$key]) ? $width[$key] : 0;
				$combination->height = isset($height[$key]) ? $height[$key] : 0;
				$combination->weight = isset($weight[$key]) ? $weight[$key] : 0;
				$combination->default = isset($default[$key]) ? $default[$key] : 0;


				if(isset($default[$combination->id])) {
					$combination->default = $default[$combination->id];
				}

				$attributes = array();
				$verify = array();
				foreach($attribute as $field_id => $combination_values) {
					foreach($combination_values as $key_idx => $field_value) {
						if ($key_idx != $key || empty($field_value)) {
							continue;
						}
						$field = new stdClass();
						$field->field_id = (int)$field_id;
						$field->value = (int)$field_value;

						$verify[] = array('field_id'=>$field->field_id, 'value'=>$field->value);

						$attributes[] = $field;
					}
				}
				// we should not create two combinations made from the exactly same set of attrbutes
				$skip = false;
				if (count($existing) < 1) {
					$existing[] = $verify;
				} else {
					foreach($existing as $already) {
						if (serialize($already) == serialize($verify)) {
							$skip = true;
							if (!$newCombo) {
								$query = $db->getQuery(true);
								$query->delete('#__djc2_items_combinations')->where('id='.$combination->id);
								$db->setQuery($query);
								$db->execute();

								$query = $db->getQuery(true);
								$query->delete('#__djc2_items_combinations_fields')->where('combination_id='.$combination->id);
								$db->setQuery($query);
								$db->execute();
							}
						}
					}
				}
				if ($skip) {
					continue;
				}
				$existing[] = $verify;

				$success = false;
				if ($newCombo) {
					$success = $db->insertObject('#__djc2_items_combinations', $combination, 'id');
				} else {
					$success = $db->updateObject('#__djc2_items_combinations', $combination, 'id', false);
				}

				if ($success) {
					$query = $db->getQuery(true);

					$query->delete('#__djc2_items_combinations_fields')->where('combination_id='.$combination->id);
					$db->setQuery($query);
					$db->execute();

					if (count($attributes) > 0) {
						$query = $db->getQuery(true);
						$query->insert('#__djc2_items_combinations_fields');
						$query->columns(array('combination_id,field_id,value'));
						foreach($attributes as $field) {
							$query->values($combination->id.','.$field->field_id.','.$field->value);
						}
						$db->setQuery($query);
						$db->execute();
					}
				}
			}
		}

		return true;
	}

	public function saveCustomisationsGroups($data, &$table, $isNew) {
		$item 	= $this->getItem($table->id);
		$app = Factory::getApplication();

		$availableGroups = $this->getCustomisationsGroups();


		$db = Factory::getDbo();
		$query = $db->getQuery(true);


		$query->delete('#__djc2_items_customisations_groups');
		$query->where('item_id='.$table->id);

		$db->setQuery($query);
		$db->execute();

		$existing = array ();

		$itemGroups = array();
		$groupsCustomisations = array();

		if(count($data)) {
			foreach ($data as $groupId) {
				$group = ((isset($availableGroups[$groupId])) ? $availableGroups[$groupId] : null);

				if($group) {
					$itemGroups[] = $groupId;
					if(isset($group['items']) && count($group['items'])) {
						foreach ($group['items'] as $groupItem) {
							$groupItem['group_id'] = $groupId;
							$groupsCustomisations[$groupItem['id']] = $groupItem;
						}
					}

				}
			}
		}


		return true;
	}



	public function saveCustomisations($data, &$table, $isNew) {
		$item 	= $this->getItem($table->id);
		$app = Factory::getApplication();
		$task = $app->input->getCmd('task');

		$ids 		= (isset($data['customisation_id'])) ? (array)$data['customisation_id'] : array();
		$price 		= (isset($data['price'])) ? (array)$data['price'] : array();
		$min_quantity 		= (isset($data['min_quantity'])) ? (array)$data['min_quantity'] : array();
		$max_quantity 		= (isset($data['max_quantity'])) ? (array)$data['max_quantity'] : array();
		$customisation_group 		= (isset($data['customisation_group'])) ? (array)$data['customisation_group'] : array();


		$db = Factory::getDbo();

		$query = $db->getQuery(true);
		$query->delete('#__djc2_items_customisations');
		$query->where('item_id='.$table->id);
		$db->setQuery($query);
		$db->execute();

		$query = $db->getQuery(true);
		$query->delete('#__djc2_items_extra_fields_values_int');
		$query->where('item_id='.(int)$table->id);
		$db->setQuery($query);
		$db->execute();


		$existing = array ();

		if (count($ids)) {
			foreach($ids as $key => $customisation_id) {

				if (empty($customisation_id) || (isset($customisation_group[$key]) && $customisation_group[$key] > 0) || isset($table->groupCombinations) && in_array($customisation_id, $table->groupCombinations)) {
					continue;
				}



				$customisation = new stdClass();
				$customisation->customisation_id = (int)$customisation_id;
				$customisation->item_id = $table->id;
				$customisation->price = isset($price[$key]) ? floatval($price[$key]) : 0.0;
				$customisation->min_quantity = isset($min_quantity[$key]) ? (int)$min_quantity[$key] : 0;
				$customisation->max_quantity = isset($max_quantity[$key]) ? (int)$max_quantity[$key] : 0;
				$customisation->group_id = isset($customisation_group[$key]) ? (int)$customisation_group[$key] : 0;

				$success = $db->insertObject('#__djc2_items_customisations', $customisation);
			}
		}


		foreach ($ids as $customisationId => $value) {
			if(isset($price[$customisationId])) {
				$db->setQuery('UPDATE #__djc2_items_customisations SET price = ' . (double) $price[$customisationId] . ' WHERE customisation_id = ' .  $value. ' AND item_id = '.$table->id);
				$query = $db->getQuery(false);

				$db->execute();
			}

		}

		return true;
	}

	public function changeFeaturedState($pks, $value) {
		if (empty($pks)) {
			return false;
		}
		$ids = implode(',',$pks);
		$db = Factory::getDbo();
		$db->setQuery('update #__djc2_items set featured='.(int)$value.' where id in ('.$ids.')');
		if (!$db->execute()) {
			$this->setError($db->getErrorMsg());
			return false;
		}
		return true;
	}

	public function changeAvailableState($pks, $value) {
		if (empty($pks)) {
			return false;
		}
		$ids = implode(',',$pks);
		$db = Factory::getDbo();
		$db->setQuery('update #__djc2_items set available='.(int)$value.' where id in ('.$ids.')');
		if (!$db->execute()) {
			$this->setError($db->getErrorMsg());
			return false;
		}
		return true;
	}

	public function geocode($pks) {

		if (empty($pks)) {
			return false;
		}
		$ids = implode(',',$pks);
		$db = Factory::getDbo();

		$app = Factory::getApplication();

		require_once Path::clean(JPATH_ROOT.'/administrator/components/com_djcatalog2/lib/geocode.php');

		$db->setQuery('select * from #__djc2_items where id IN ('.$ids.')');
		$items = $db->loadObjectList();

		foreach ($items as $item) {
			$address = array();

			if (!empty($item->address)) {
				$address[] = $item->address;
			}
			if (!empty($item->city)) {
				$address[] = $item->city;
			}
			if (!empty($item->postcode)) {
				$address[] = $item->postcode;
			}
			if (!empty($item->country)) {
				$db->setQuery('select country_name from #__djc2_countries where id='.(int)$item->country);
				$country = $db->loadResult();
				if ($country) {
					$address[] = $country;
				}
			}

			$address_str = implode(',', $address);
			if ($address_str) {
				if ($coords = DJCatalog2Geocode::getLocation($address_str)) {

					// bypassing Google Maps limits
					usleep(150000);

					$latitude = (!empty($coords['lat'])) ? $coords['lat'] : null;
					$longitude = (!empty($coords['lng'])) ? $coords['lng'] : null;

					$db->setQuery('UPDATE #__djc2_items SET latitude = '.$latitude.', longitude = '.$longitude.' WHERE id = '.(int)$item->id);

					if (!$db->execute()) {
						$this->setError($db->getErrorMsg());
						return false;
					}
					$app->enqueueMessage(Text::sprintf('COM_DJCATALOG2_GEOLOCATION_OK', $item->id), 'message');
				} else {
					$app->enqueueMessage(Text::sprintf('COM_DJCATALOG2_ERROR_GEOLOCATION_NOT_FOUND', $item->id), 'notice');
				}
			} else {
				$app->enqueueMessage(Text::sprintf('COM_DJCATALOG2_ERROR_GEOLOCATION_EMPTY_ADDRESS', $item->id), 'notice');
			}
		}

		return true;
	}

	public function delete(&$cid)
	{
		$return = parent::delete($cid);
		$app = Factory::getApplication();


		if ($return && count($cid)) {
			$cids = implode(',', $cid);

			$this->_db->setQuery("UPDATE #__djc2_items set published=-2, parent_id=0 WHERE parent_id IN ( " . $cids . " )");
			$this->_db->execute();
			$updCnt = $this->_db->getAffectedRows();
			if ($updCnt > 0) {
				$app->enqueueMessage(Text::sprintf('COM_DJCATALOG2_PARENTS_DELETED_NOTICE', $updCnt), 'notice');
			}
		}

		return $return;
	}

	public function batch($pk, $batchData, $cursor)
	{
		$language = ((isset($batchData['language_id'])) ? $batchData['language_id'] : null);
		$language_copy = ((isset($batchData['language_copy'])) ? $batchData['language_copy'] : 'n');
		$move_copy = ((isset($batchData['move_copy'])) ? $batchData['move_copy'] : 'copy');

		$multilangEnabled = Associations::isEnabled();

		$success = false;
		$message = '';
		$error = '';
		$messages = [];

		$db = Factory::getDbo();

		$table = $this->getTable();


		if (!empty($batchData['category_moveadd'])) {
			$moveAdd = $batchData['category_moveadd'];
			if (!empty($batchData['category'])) {
				if ($moveAdd == 'm') {
					$query = $db->getQuery(true);
					$query->delete('#__djc2_items_categories');
					$query->where('item_id = ' . (int)$pk);
					$db->setQuery($query);
					if ($db->execute()) {
						$query = $db->getQuery(true);
						$query->update('#__djc2_items')->set('cat_id=' . (int)$batchData['category'])->where('id =' . (int)$pk);
						$db->setQuery($query);

						if ($db->execute()) {
							$query = $db->getQuery(true);
							$query->insert('#__djc2_items_categories')->columns('item_id, category_id');
							$query->values((int)$pk . ', ' . (int)$batchData['category']);
							$db->setQuery($query);
							$done = $db->execute();
						}
					}
				} else {
					$query = $db->getQuery(true);
					$query->delete('#__djc2_items_categories');
					$query->where('item_id = ' . (int)$pk . ' AND category_id=' . $batchData['category']);
					$db->setQuery($query);
					if ($db->execute()) {
						$query = $db->getQuery(true);
						$query->update('#__djc2_items')->set('cat_id=' . (int)$batchData['category'])->where('id= ' . (int)$pk . ' AND (cat_id IS NULL OR cat_id=0)');
						$db->setQuery($query);

						if ($db->execute()) {
							$query = $db->getQuery(true);
							$query->insert('#__djc2_items_categories')->columns('item_id, category_id');
							$query->values((int)$pk . ', ' . (int)$batchData['category']);
							$db->setQuery($query);
							$done = $db->execute();
						}
					}
				}

				$messages[] = '[ID=' . $pk . '] ' . Text::_('COM_DJCATALOG2_BATCH_ITEM_MSG_MAIN');
			}
		}

		// Base batch functions
		$updateAttrsMain = array();
		$updateAttrsCombo = array();

		if (!empty($batchData['producer'])) {
			$updateAttrsMain['producer_id'] = ($batchData['producer'] == -1) ? 0 : (int)$batchData['producer'];
		}

		$batchData['price'] = trim((string)$batchData['price']);
		if ($batchData['price'] != '' && is_numeric($batchData['price'])) {
			switch ($batchData['prices_target']) {
				case '1':
				{
					$updateAttrsMain['price'] = (float)$batchData['price'];
					break;
				}
				case '2':
				{
					$updateAttrsCombo['price'] = (float)$batchData['price'];
					break;
				}
				case '3':
				{
					$updateAttrsMain['price'] = (float)$batchData['price'];
					$updateAttrsCombo['price'] = (float)$batchData['price'];
					break;
				}
			}
		}

		$commands['special_price'] = trim((string)$batchData['special_price']);
		if ($commands['special_price'] != '' && is_numeric($batchData['special_price'])) {
			switch ($commands['prices_target']) {
				case '1':
				{
					$updateAttrsMain['special_price'] = (float)$commands['special_price'];
					break;
				}
				case '2':
				{
					break;
				}
				case '3':
				{
					$updateAttrsMain['special_price'] = (float)$commands['special_price'];
					break;
				}
			}
		}

		$commands['weight'] = trim((string)$batchData['weight']);
		if ($commands['weight'] != '' && is_numeric($batchData['weight'])) {
			switch ($commands['weight_target']) {
				case '1':
				{
					$updateAttrsMain['weight'] = (float)$commands['weight'];
					break;
				}
				case '2':
				{
					$updateAttrsCombo['weight'] = (float)$commands['weight'];
					break;
				}
				case '3':
				{
					$updateAttrsMain['weight'] = (float)$commands['weight'];
					$updateAttrsCombo['weight'] = (float)$commands['weight'];
					break;
				}
			}

			if (!empty($commands['weight_unit'])) {
				$updateAttrsMain['weight_unit'] = $commands['weight_unit'];
			}
		}

		$commands['length'] = trim((string)$batchData['length']);
		$commands['width'] = trim((string)$batchData['width']);
		$commands['height'] = trim((string)$batchData['height']);

		if (($batchData['length'] != '' && is_numeric($batchData['length'])) || ($batchData['width'] != '' && is_numeric($batchData['width'])) || ($batchData['height'] != '' && is_numeric($batchData['height']))) {

			switch ($batchData['dimensions_target']) {
				case '1':
				{
					if ($batchData['length'] != '' && is_numeric($batchData['length'])) {
						$updateAttrsMain['length'] = (float)$batchData['length'];
					}
					if ($batchData['width'] != '' && is_numeric($batchData['width'])) {
						$updateAttrsMain['width'] = (float)$batchData['width'];
					}
					if ($batchData['height'] != '' && is_numeric($batchData['height'])) {
						$updateAttrsMain['height'] = (float)$batchData['height'];
					}
					break;
				}
				case '2':
				{
					if ($batchData['length'] != '' && is_numeric($batchData['length'])) {
						$updateAttrsCombo['length'] = (float)$batchData['length'];
					}
					if ($batchData['width'] != '' && is_numeric($batchData['width'])) {
						$updateAttrsCombo['width'] = (float)$batchData['width'];
					}
					if ($batchData['height'] != '' && is_numeric($batchData['height'])) {
						$updateAttrsCombo['height'] = (float)$batchData['height'];
					}
					break;
				}
				case '3':
				{
					if ($batchData['length'] != '' && is_numeric($batchData['length'])) {
						$updateAttrsMain['length'] = (float)$batchData['length'];
						$updateAttrsCombo['length'] = (float)$batchData['length'];
					}
					if ($batchData['width'] != '' && is_numeric($batchData['width'])) {
						$updateAttrsMain['width'] = (float)$batchData['width'];
						$updateAttrsCombo['width'] = (float)$batchData['width'];
					}
					if ($batchData['height'] != '' && is_numeric($batchData['height'])) {
						$updateAttrsMain['height'] = (float)$batchData['height'];
						$updateAttrsCombo['height'] = (float)$batchData['height'];
					}
					break;
				}
			}

			if (!empty($batchData['length_unit'])) {
				$updateAttrsMain['length_unit'] = $batchData['length_unit'];
			}
		}

		if (count($updateAttrsMain)) {
			$query = $db->getQuery(true);
			$query->update('#__djc2_items');
			foreach ($updateAttrsMain as $key => $value) {
				$query->set($db->quoteName($key) . '=' . $value);
			}
			$query->where('id = ' . (int)$pk);
			$db->setQuery($query);
			$db->execute();

			$messages[] = '[ID=' . $pk . '] ' . Text::_('COM_DJCATALOG2_BATCH_ITEM_MSG_MAIN');
		}

		if (count($updateAttrsCombo)) {
			$query = $db->getQuery(true);
			$query->update('#__djc2_items_combinations');
			foreach ($updateAttrsCombo as $key => $value) {
				$query->set($db->quoteName($key) . '=' . $value);
			}
			$query->where('item_id = ' . (int)$pk);
			$db->setQuery($query);
			$db->execute();

			$messages[] = '[ID=' . $pk . '] ' . Text::_('COM_DJCATALOG2_BATCH_ITEM_MSG_COMBO');
		}

		if ($language && $language_copy) {

			$item = $this->getItem($pk);

			$item->sections = json_decode((string)$item->sections);

			// Get item images
			$query = $db->getQuery(true);
			$query
				->select('id')
				->from($db->quoteName('#__djc2_images', 'i'))
				->where('i.type = ' . $db->quote('item'))
				->where('i.item_id = ' . (int)$pk)
				->order('ordering ASC');

			$db->setQuery($query);
			$images = $db->loadColumn();

			// Get item files
			$query = $db->getQuery(true);
			$query
				->select('id')
				->from($db->quoteName('#__djc2_files', 'i'))
				->where('i.type = ' . $db->quote('item'))
				->where('i.item_id = ' . (int)$pk)
				->order('ordering ASC');

			$db->setQuery($query);
			$files = $db->loadColumn();

			// Get item videos
			$query = $db->getQuery(true);
			$query
				->select('id')
				->from($db->quoteName('#__djc2_videos', 'i'))
				->where('i.type = ' . $db->quote('item'))
				->where('i.item_id = ' . (int)$pk)
				->order('ordering ASC');

			$db->setQuery($query);
			$videos = $db->loadColumn();

			$baseLang = $item->language;
			$baseName = $item->name;

			if ($baseLang == $language) {
				$error = Text::sprintf('COM_DJCATALOG2_MOVE_DUPLICATE_LANG', $pk);
			}

			$app = Factory::getApplication();
			$db = Factory::getDbo();

			if ($move_copy == 'copy') {
				$item->id = 0;
				$app->input->set('id', 0);
				$this->setState('item.id', 0);
			}


			$db->setQuery('SELECT Count(*) FROM #__djc2_items WHERE alias LIKE ' . $db->quote($db->escape('%' . $item->alias . '%')));
			$aliasesCount = $db->loadResult();
			$item->alias = $item->name . '-' . $aliasesCount;

			// Multilang
			if (isset($item->parent_id) && $item->parent_id && $multilangEnabled && $language_copy !== 'n') {
				$associations = Associations::getAssociations('com_djcatalog2', '#__djc2_items', 'com_djcatalog2.item', (int)$item->parent_id, 'id', '', '');


				if (isset($associations[$language]->id)) {
					$item->parent_id = $associations[$language]->id;
				}
			}


			// Categories change batch
			if (isset($batchData['category']) && $batchData['category']) {
				$associations = Associations::getAssociations('com_djcatalog2', '#__djc2_categories', 'com_djcatalog2.category', (int)$batchData['category'], 'id', '', '');

				if (isset($associations[$language])) {
					$item->cat_id = $associations[$language]->id;
				}
			} else if (isset($item->cat_id) && $item->cat_id) {
				$associations = Associations::getAssociations('com_djcatalog2', '#__djc2_categories', 'com_djcatalog2.category', (int)$item->cat_id, 'id', '', '');

				if (isset($associations[$language])) {
					$item->cat_id = $associations[$language]->id;
				}
			}

			// Multi categories
			if (isset($item->categories) && count($item->categories)) {
				foreach ($item->categories as &$category) {
					$associations = Associations::getAssociations('com_djcatalog2', '#__djc2_categories', 'com_djcatalog2.category', (int)$category, 'id', '', '');
					if (isset($associations[$language])) {
						$category = $associations[$language]->id;
					}
				}

				$jform = array(
					'categories' => $item->categories,
					'group_id' => $item->group_id
				);

				$app->input->set('jform', $jform);
			}


			// Attributes
			$attributes = $this->getItemAttributesBatch($pk);


			$itemAttr = array();
			$newOpts = array();
			if (count($attributes)) {

				foreach ($attributes as $attribute) {

					if ($attribute->language == '*') {
						$itemAttr[$attribute->field_id][] = $attribute->value_id;
					} else {
						$associations = Associations::getAssociations('com_djcatalog2', '#__djc2_items_extra_fields', 'com_djcatalog2.field', (int)$attribute->field_id, 'id', '', '');

						if (isset($associations[$language]->id)) {
							$newFieldId = $associations[$language]->id;

							if (!isset($newOpts[$newFieldId])) {
								$query = $db->getQuery(true);

								$query
									->select(array(
										'id',
										'ordering'
									))
									->from($db->quoteName('#__djc2_items_extra_fields_options', 'o'))
									->where('o.field_id = ' . (int)$newFieldId);

								$db->setQuery($query);
								$newOpts[$newFieldId] = $db->loadObjectList('ordering');
							}


							if (isset($newOpts[$newFieldId][$attribute->ordering]->id)) {
								$itemAttr[$newFieldId][] = $newOpts[$newFieldId][$attribute->ordering]->id;
							}

						}
					}
				}


				$app->input->set('attribute', $itemAttr);
			}


			// Default values
			$item->published = 0;
			$item->language = $language;


			if ($language_copy == 'n') {
				$item->associations = array();
			} else {
				$item->associations[$baseLang] = $pk;
			}


			$app->input->set('image_file_id', $images);
			$app->input->set('file_file_id', $files);
			$app->input->set('video_file_id', $videos);
			$currentTask = $app->input->get('task');
			$app->input->set('task', 'save2copy');


//			foreach ($item->combinations as $combo) {
//				$combo->item_id = $lastId;
//			}


			$success = $this->save(Joomla\Utilities\ArrayHelper::fromObject($item));


			$db = Factory::getDbo();

			$query = $db->getQuery(true);

			$query->select('MAX(id)')
				->from($db->quoteName('#__djc2_items')); // Use your actual table name

			$db->setQuery($query);

			$lastId = (int)$db->loadResult();


			$arrayNew = [];


			if (!$success) {
				$error = $this->getError();
				$messages[] = '[ID=' . $pk . '] ' . $error;
			} else {
				// Zapisz kombinacje
				$query = $db->getQuery(true)
					->select(array(
						$db->quoteName('id'),
						$db->quoteName('item_id'),
						$db->quoteName('sku'),
						$db->quoteName('gtin'),
						$db->quoteName('price'),
						$db->quoteName('avg_price'),
						$db->quoteName('avg_of_days'),
						$db->quoteName('stock'),
						$db->quoteName('length'),
						$db->quoteName('width'),
						$db->quoteName('height'),
						$db->quoteName('dimensions_unit'),
						$db->quoteName('weight'),
						$db->quoteName('weight_unit'),
						$db->quoteName('default'),
						$db->quoteName('ordering') // Properly quoting the 'ordering' field
					))
					->from($db->quoteName('#__djc2_items_combinations'))
					->where($db->quoteName('item_id') . ' = ' . (int)$pk);


				$db->setQuery($query);
				$rows = $db->loadObjectList();

				foreach ($rows as $row){
					$query = $db->getQuery(true)
						->select("*")
						->from($db->quoteName('#__djc2_images'))
						->where($db->quoteName('item_id') . ' = ' . (int)$row->id);


					$db->setQuery($query);
					$images = $db->loadObjectList();
					$row->images = $images;
				}
				if ($rows) {
					// Step 2: Prepare the new item_id (You can change it to your logic)
					$new_item_id = $lastId; // Replace with your desired new item_id

					// Step 3: Insert the modified rows back with the new item_id
					foreach ($rows as $row) {
						$columns = array(
							'item_id', 'sku', 'gtin', 'price', 'avg_price', 'avg_of_days',
							'stock', 'length', 'width', 'height', 'dimensions_unit',
							'weight', 'weight_unit', 'default', 'ordering'
						);

						// Prepare the values to insert, modify item_id here
						$values = array(
							$new_item_id,
							$db->quote($row->sku),
							$db->quote($row->gtin),
							$db->quote($row->price),
							$db->quote($row->avg_price),
							$db->quote($row->avg_of_days),
							$db->quote($row->stock),
							$db->quote($row->length),
							$db->quote($row->width),
							$db->quote($row->height),
							$db->quote($row->dimensions_unit),
							$db->quote($row->weight),
							$db->quote($row->weight_unit),
							$db->quote($row->default),
							$db->quote($row->ordering)
						);

						try {
							// Prepare the insert query
							$insertQuery = $db->getQuery(true)
								->insert($db->quoteName('#__djc2_items_combinations'))
								->columns($db->quoteName($columns))
								->values(implode(',', $values));
							// Execute the insert query
							$db->setQuery($insertQuery);
							$db->execute();




						} catch (Exception $e) {
							$messages[] = $e;
							break;
						}


						$new_combination_id = $db->insertid(); // This will get the last inserted combination_id


						foreach ($row->images as $image){

							$columnsImages = array(
								'item_id', 'type', 'fullname', 'name', 'ext', 'path',
								'fullpath', 'caption', 'ordering', 'exclude'
							);
							$valuesImages = array(
								$new_combination_id,
								$db->quote("combination"),
								$db->quote($image->fullname),
								$db->quote($image->name),
								$db->quote($image->ext),
								$db->quote($image->path),
								$db->quote($image->fullpath),
								$db->quote($image->caption),
								$db->quote($image->ordering),
								$db->quote($image->exclude)
							);

							//Images
							$insertQueryImages = $db->getQuery(true)
								->insert($db->quoteName('#__djc2_images'))
								->columns($db->quoteName($columnsImages))
								->values(implode(',', $valuesImages));
							// Execute the insert query
							$db->setQuery($insertQueryImages);
							$db->execute();
						}
						$lastCombinationRow = $row->id;


						if ($row) {
							$combinationIdOfField = $row->id;

							try {
								//Get fields from combinationOfField
								$query = $db->getQuery(true)
									->select('*')
									->from($db->quoteName('#__djc2_items_combinations_fields'))
									->where($db->quoteName('combination_id') . ' = ' . (int)$combinationIdOfField);


								$db->setQuery($query);
								$fields = $db->loadObjectList();
							} catch (Exception $e) {
								$messages[] = $e;
								break;
							}


							if (!empty($fields))
								foreach ($fields as $field) {
									try {
										$associationsFieldsComb = Associations::getAssociations('com_djcatalog2', '#__djc2_items_extra_fields', 'com_djcatalog2.field', (int)$field->field_id, 'id', '', '');
									} catch (Exception $e) {
										$messages[] = $e;
										break;
									}
									if ($associationsFieldsComb) {
										$associatedField = $associationsFieldsComb[$language]->id;
										$optionField = $field->value;
										//get Option value
										try {
											$query = $db->getQuery(true)
												->select('ordering'
												)
												->from($db->quoteName('#__djc2_items_extra_fields_options'))
												->where($db->quoteName('id') . ' = ' . (int)$optionField);


											$db->setQuery($query);
											$objValue = $db->loadObject();
										} catch (Exception $e) {
											$messages[] = $e;
											break;
										}



										$oldFieldValue = $objValue->ordering;
										if ($oldFieldValue) {

											try {
												$query = $db->getQuery(true)
													->select('id'
													)
													->from($db->quoteName('#__djc2_items_extra_fields_options'))
													->where($db->quoteName('field_id') . ' = ' . (int)$associatedField . ' AND ' . $db->quoteName('ordering') . ' = "' . $oldFieldValue . '"');


												$db->setQuery($query);
												$newFieldOptionId = $db->loadObject();
											} catch (Exception $e) {
												$messages[] = $e;
												break;
											}


											if ($newFieldOptionId && $oldFieldValue) {
												$fieldsColumns = array(
													'combination_id', // New combination_id
													'field_id',       // Replace with your actual field names
													'value'           // Replace with your actual field names
												);

												$fieldsValues = array(
													(int)$new_combination_id, // Set the new combination_id
													$db->quote($associatedField),
													$db->quote($newFieldOptionId->id)
												);

												// Step 1: Check if this combination already exists
												$checkQuery = $db->getQuery(true)
													->select('COUNT(*)')
													->from($db->quoteName('#__djc2_items_combinations_fields'))
													->where($db->quoteName('combination_id') . ' = ' . (int)$new_combination_id)
													->where($db->quoteName('field_id') . ' = ' . $db->quote($associatedField))
													->where($db->quoteName('value') . ' = ' . $db->quote($newFieldOptionId->id));

												// Execute the check query
												$db->setQuery($checkQuery);
												$exists = (int) $db->loadResult();

												if ($exists === 0) {
													// Step 2: Proceed with the insert if no duplicate is found
													try {
														$insertFieldsQuery = $db->getQuery(true)
															->insert($db->quoteName('#__djc2_items_combinations_fields'))
															->columns($db->quoteName($fieldsColumns))
															->values(implode(',', $fieldsValues));

														// Execute the insert query for the fields
														$db->setQuery($insertFieldsQuery);
														$messages[] = "QUERY: " . $insertFieldsQuery . " /QUERY";
														$db->execute();
													} catch (Exception $e) {
														$messages[] = $e;
														break;
													}
												} else {
													// Log that the record already exists
													$messages[] = "Record already exists for combination_id: $new_combination_id, field_id: $associatedField, value: $newFieldOptionId->id";
												}
											}

										}

									}


								}



						} else {
							$messages[] = "There are no associated extra fields for this item";
						}
					}

					// Optionally, you can return the number of inserted rows or a success message
					$messages[] = "Rows successfully duplicated with new item_id!";
				} else {
					$messages[] = "No rows found to duplicate.";
				}


				$messages[] = '[ID=' . $pk . '] ' . Text::_('COM_DJCATALOG2_BATCH_ITEM_MSG_COPY') . ' ' . ('[' . $baseLang . '] ' . $baseName . ' (' . $move_copy . ') => ' . '[' . $language . ']' . $table->name . (($language_copy == 'y') ? ' (' . Text::_('COM_DJCATALOG2_BATCH_LANGUAGE_COPY_ASSIGNMENTS') . ')' : ''));
			}
		}

		$message = implode(PHP_EOL, $messages);

		if ($success) {
			$app = Factory::getApplication();
			$cid = $app->getUserState('com_djcatalog2.list.items');

			unset($cid[$cursor]);
			$cid = array_values($cid);
			$app->setUserState('com_djcatalog2.list.items', $cid);

		}

		return array(
			'success' => $success,
			'message' => $message,
			'error' => $error
		);

	}

	public function getItemAttributesBatch($pk)
	{
		$db = Factory::getDbo();
		$attribs = array();

		// Int
		$query = $db->getQuery(true);

		$query
			->select(array(
				'f.id AS field_id',
				'v.id AS value_id',
				'f.language',
				'o.ordering'
			))
			->from($db->quoteName('#__djc2_items_extra_fields', 'f'))
			->join('INNER', $db->quoteName('#__djc2_items_extra_fields_values_int', 'v') . ' ON v.field_id = f.id')
			->join('INNER', $db->quoteName('#__djc2_items_extra_fields_options', 'o') . ' ON o.id = v.value')
			->where('v.item_id = ' . (int)$pk);

		$db->setQuery($query);
		$attributesInt = $db->loadObjectList();
		$attribs = array_merge($attribs, $attributesInt);

		// Date
		$query = $db->getQuery(true);

		$query
			->select(array(
				'f.id AS field_id',
				'v.id AS value_id',
				'o.ordering'
			))
			->from($db->quoteName('#__djc2_items_extra_fields', 'f'))
			->join('INNER', $db->quoteName('#__djc2_items_extra_fields_values_date', 'v') . ' ON v.field_id = f.id')
			->join('INNER', $db->quoteName('#__djc2_items_extra_fields_options', 'o') . ' ON o.id = v.value')
			->where('v.item_id = ' . (int)$pk);

		$db->setQuery($query);
		$attributesDate = $db->loadObjectList();
		$attribs = array_merge($attribs, $attributesDate);

		// Text
		$query = $db->getQuery(true);

		$query
			->select(array(
				'f.id AS field_id',
				'v.id AS value_id',
				'o.ordering'
			))
			->from($db->quoteName('#__djc2_items_extra_fields', 'f'))
			->join('INNER', $db->quoteName('#__djc2_items_extra_fields_values_text', 'v') . ' ON v.field_id = f.id')
			->join('INNER', $db->quoteName('#__djc2_items_extra_fields_options', 'o') . ' ON o.id = v.value')
			->where('v.item_id = ' . (int)$pk);

		$db->setQuery($query);
		$attributesTxt = $db->loadObjectList();
		$attribs = array_merge($attribs, $attributesTxt);

		return $attribs;
	}

	public function batchOld($commands, $pks, $contexts)
	{
		$pks = array_unique($pks);
		$pks = ArrayHelper::toInteger($pks);

		// Remove any values of zero.
		if (array_search(0, $pks, true)) {
			unset($pks[array_search(0, $pks, true)]);
		}

		if (empty($pks)) {
			$this->setError(Text::_('JGLOBAL_NO_ITEM_SELECTED'));
			return false;
		}

		$done = false;
		$db = Factory::getDbo();

		if (!empty($commands['category_moveadd'])) {
			$moveAdd = $commands['category_moveadd'];
			if (!empty($commands['category'])) {
				if ($moveAdd == 'm') {
					$query = $db->getQuery(true);
					$query->delete('#__djc2_items_categories');
					$query->where('item_id IN (' . implode(',', $pks) . ')');
					$db->setQuery($query);
					if ($db->execute()) {
						$query = $db->getQuery(true);
						$query->update('#__djc2_items')->set('cat_id=' . (int)$commands['category'])->where('id IN (' . implode(',', $pks) . ')');
						$db->setQuery($query);

						if ($db->execute()) {
							$query = $db->getQuery(true);
							$query->insert('#__djc2_items_categories')->columns('item_id, category_id');
							foreach ($pks as $id) {
								$query->values($id . ', ' . (int)$commands['category']);
							}
							$db->setQuery($query);
							$done = $db->execute();
						}
					}
				} else {
					$query = $db->getQuery(true);
					$query->delete('#__djc2_items_categories');
					$query->where('item_id IN (' . implode(',', $pks) . ') AND category_id=' . $commands['category']);
					$db->setQuery($query);
					if ($db->execute()) {
						$query = $db->getQuery(true);
						$query->update('#__djc2_items')->set('cat_id=' . (int)$commands['category'])->where('id IN (' . implode(',', $pks) . ') AND (cat_id IS NULL OR cat_id=0)');
						$db->setQuery($query);

						if ($db->execute()) {
							$query = $db->getQuery(true);
							$query->insert('#__djc2_items_categories')->columns('item_id, category_id');
							foreach ($pks as $id) {
								$query->values($id . ', ' . (int)$commands['category']);
							}
							$db->setQuery($query);
							$done = $db->execute();
						}
					}
				}
			}
		}

		$updateAttrsMain = array();
		$updateAttrsCombo = array();

		if (!empty($commands['producer'])) {
			$updateAttrsMain['producer_id'] = ($commands['producer'] == -1) ? 0 : (int)$commands['producer'];
		}

		$commands['price'] = trim((string)$commands['price']);
		if ($commands['price'] != '' && is_numeric($commands['price'])) {
			switch ($commands['prices_target']) {
				case '1':
				{
					$updateAttrsMain['price'] = (float)$commands['price'];
					break;
				}
				case '2':
				{
					$updateAttrsCombo['price'] = (float)$commands['price'];
					break;
				}
				case '3':
				{
					$updateAttrsMain['price'] = (float)$commands['price'];
					$updateAttrsCombo['price'] = (float)$commands['price'];
					break;
				}
			}
		}

		$commands['special_price'] = trim((string)$commands['special_price']);
		if ($commands['special_price'] != '' && is_numeric($commands['special_price'])) {
			switch ($commands['prices_target']) {
				case '1':
				{
					$updateAttrsMain['special_price'] = (float)$commands['special_price'];
					break;
				}
				case '2':
				{
					break;
				}
				case '3':
				{
					$updateAttrsMain['special_price'] = (float)$commands['special_price'];
					break;
				}
			}
		}

		$commands['weight'] = trim((string)$commands['weight']);
		if ($commands['weight'] != '' && is_numeric($commands['weight'])) {
			switch ($commands['weight_target']) {
				case '1':
				{
					$updateAttrsMain['weight'] = (float)$commands['weight'];
					break;
				}
				case '2':
				{
					$updateAttrsCombo['weight'] = (float)$commands['weight'];
					break;
				}
				case '3':
				{
					$updateAttrsMain['weight'] = (float)$commands['weight'];
					$updateAttrsCombo['weight'] = (float)$commands['weight'];
					break;
				}
			}

			if (!empty($commands['weight_unit'])) {
				$updateAttrsMain['weight_unit'] = $commands['weight_unit'];
			}
		}

		$commands['length'] = trim((string)$commands['length']);
		$commands['width'] = trim((string)$commands['width']);
		$commands['height'] = trim((string)$commands['height']);

		if (($commands['length'] != '' && is_numeric($commands['length'])) || ($commands['width'] != '' && is_numeric($commands['width'])) || ($commands['height'] != '' && is_numeric($commands['height']))) {

			switch ($commands['dimensions_target']) {
				case '1':
				{
					if ($commands['length'] != '' && is_numeric($commands['length'])) {
						$updateAttrsMain['length'] = (float)$commands['length'];
					}
					if ($commands['width'] != '' && is_numeric($commands['width'])) {
						$updateAttrsMain['width'] = (float)$commands['width'];
					}
					if ($commands['height'] != '' && is_numeric($commands['height'])) {
						$updateAttrsMain['height'] = (float)$commands['height'];
					}
					break;
				}
				case '2':
				{
					if ($commands['length'] != '' && is_numeric($commands['length'])) {
						$updateAttrsCombo['length'] = (float)$commands['length'];
					}
					if ($commands['width'] != '' && is_numeric($commands['width'])) {
						$updateAttrsCombo['width'] = (float)$commands['width'];
					}
					if ($commands['height'] != '' && is_numeric($commands['height'])) {
						$updateAttrsCombo['height'] = (float)$commands['height'];
					}
					break;
				}
				case '3':
				{
					if ($commands['length'] != '' && is_numeric($commands['length'])) {
						$updateAttrsMain['length'] = (float)$commands['length'];
						$updateAttrsCombo['length'] = (float)$commands['length'];
					}
					if ($commands['width'] != '' && is_numeric($commands['width'])) {
						$updateAttrsMain['width'] = (float)$commands['width'];
						$updateAttrsCombo['width'] = (float)$commands['width'];
					}
					if ($commands['height'] != '' && is_numeric($commands['height'])) {
						$updateAttrsMain['height'] = (float)$commands['height'];
						$updateAttrsCombo['height'] = (float)$commands['height'];
					}
					break;
				}
			}

			if (!empty($commands['length_unit'])) {
				$updateAttrsMain['length_unit'] = $commands['length_unit'];
			}
		}

		if (count($updateAttrsMain)) {
			$query = $db->getQuery(true);
			$query->update('#__djc2_items');
			foreach ($updateAttrsMain as $key => $value) {
				$query->set($db->quoteName($key) . '=' . $value);
			}
			$query->where('id IN (' . implode(',', $pks) . ')');
			$db->setQuery($query);
			$done = $db->execute();
		}

		if (count($updateAttrsCombo)) {
			$query = $db->getQuery(true);
			$query->update('#__djc2_items_combinations');
			foreach ($updateAttrsCombo as $key => $value) {
				$query->set($db->quoteName($key) . '=' . $value);
			}
			$query->where('item_id IN (' . implode(',', $pks) . ')');
			$db->setQuery($query);
			$done = $db->execute();
		}

		if (!empty($commands['language_id'])) {
			$lang = $commands['language_id'];
			$langAssign = !empty($commands['language_copy']) ? $commands['language_copy'] : 'n';

			echo 'new lang: ' . $langAssign . ' ' . $lang;
			die();
		}

		if (!$done) {
			$this->setError(Text::_('JLIB_APPLICATION_ERROR_INSUFFICIENT_BATCH_INFORMATION'));

			return false;
		}

		// Clear the cache
		$this->cleanCache();

		return true;
	}

	public function save($data) {
		if(isset($data['sections'])) {
			if (!empty($data['sections'])) {
				if (is_array($data['sections'])) {
					$data['sections'] = json_encode($data['sections']);
				}
			} else {
				$data['sections'] = null;
			}
		}

		if(isset($data['config_conditions']) && is_array($data['config_conditions'])) {
			$data['config_conditions'] = json_encode($data['config_conditions']);
		}

		if(isset($data['bundle_items']) && is_array($data['bundle_items'])) {
			$data['bundle_items'] = json_encode($data['bundle_items']);
		}

		if(isset($data['grant_usergroups']) && is_array($data['grant_usergroups'])) {
			$data['grant_usergroups'] = json_encode($data['grant_usergroups']);
		}

		return parent::save($data);
	}

	/**
	 * Method to test whether a record can be deleted.
	 *
	 * @param   object  $record  A record object.
	 *
	 * @return  boolean  True if allowed to delete the record. Defaults to the permission for the component.
	 *
	 * @since   1.6
	 */
	protected function canDelete($record)
	{
		return Factory::getUser()->authorise('core.delete', $this->option) || Factory::getUser()->authorise('djcatalog2.admin.catalogue', $this->option);
	}

	/**
	 * Method to test whether a record can have its state changed.
	 *
	 * @param   object  $record  A record object.
	 *
	 * @return  boolean  True if allowed to change the state of the record. Defaults to the permission for the component.
	 *
	 * @since   1.6
	 */
	protected function canEditState($record)
	{
		return Factory::getUser()->authorise('core.edit.state', $this->option)  || Factory::getUser()->authorise('djcatalog2.admin.catalogue', $this->option);
	}

	/**
	 * Method to preprocess the form to add the association fields dynamically
	 *
	 * @return     none
	 */
	protected function preprocessForm(Joomla\CMS\Form\Form $form, $data, $group = '')
	{
		// Association content items
		if (Associations::isEnabled())
		{
			$languages = LanguageHelper::getContentLanguages(false, true, null, 'ordering', 'asc');

			if (count($languages) > 1)
			{
				$addform = new SimpleXMLElement('<form />');
				$fields = $addform->addChild('fields');
				$fields->addAttribute('name', 'associations');
				$fieldset = $fields->addChild('fieldset');
				$fieldset->addAttribute('name', 'item_associations');

				foreach ($languages as $language)
				{
					$field = $fieldset->addChild('field');
					$field->addAttribute('name', $language->lang_code);
					$field->addAttribute('type', 'djitem');
					$field->addAttribute('language', $language->lang_code);
					$field->addAttribute('label', $language->title);
					$field->addAttribute('translate_label', 'false');
				}

				$form->load($addform, false);
			}
		}
		parent::preprocessForm($form, $data, $group);
	}

	public function notifyStockUpdates($item_id, $item_type, $product) {
		$db = Factory::getDbo();
		$query = $db->getQuery(true);
		$query->select('sn.*');
		$query->from('#__djc2_stocknotify AS sn');
		$query->where('sn.item_id='. (int)$item_id);
		$query->where('sn.item_type=' . $db->quote($item_type));

		$db->setQuery($query);
		$rows = $db->loadObjectList('id');

		if (count($rows)) {

			$query = $db->getQuery(true);
			$query->select('alias')->from('#__djc2_categories')->where('id=' . $product->cat_id);
			$db->setQuery($query);
			$alias = $db->loadResult();

			require_once JPATH_ROOT.'/administrator/components/com_djcatalog2/helpers/route.php';
			$productLink = DJCatalog2HelperSiteRoute::buildRoute('getItemRoute', array($product->id.':'.$product->alias, $product->cat_id.':'.$alias), null, true);

			foreach($rows as $row) {
				$mailopts = [
					'product_name' => $product->name,
					'product_link' =>  $productLink
				];

				$messenger = new DJCatalog2HelperMessenger();
				$messenger->notify($row->email, ['type' => 'stock_available'], null, $mailopts );

			}

			$query = $db->getQuery(true);
			$query->delete('#__djc2_stocknotify')->where('id IN ('.implode(',', array_keys($rows)).')');
			$db->setQuery($query);
			$db->execute();

			return count($rows);
		}

		return false;
	}
}

Anon7 - 2022
AnonSec Team