<?xml version="1.0" encoding="UTF-8"?>
<modification>
	<name>Advance Product Videos</name>
	<version>1.0</version>
	<code>Advance Product Videos</code>
	<author>Mart Extensions</author>
	<link>www.martextensions.in</link>
	<!-- Admin Code -->
	<file path="admin/controller/catalog/product.php">
		<operation>
			<search><![CDATA[$this->load->model('design/layout');]]></search>
			<add position="before"><![CDATA[
				#XML Mart Extensions
				$data['module_me_product_video_status'] = $this->config->get('module_me_product_video_status');
				$data['me_product_videos'] = array();
				if($this->config->get('module_me_product_video_status')){
					$this->load->model('extension/me_product_video');
					$this->model_extension_me_product_video->Createtable();
					
					if (isset($this->request->post['me_product_video'])) {
						$me_product_videos = $this->request->post['me_product_video'];
					} elseif (isset($this->request->get['product_id'])) {
						$me_product_videos = $this->model_extension_me_product_video->getVideos($this->request->get['product_id']);
					} else {
						$me_product_videos = array();
					}
					
					foreach($me_product_videos as $me_product_video){
						if (is_file(DIR_IMAGE . $me_product_video['image'])) {
							$image = $me_product_video['image'];
							$thumb = $me_product_video['image'];
						} else {
							$image = '';
							$thumb = 'no_image.png';
						}
						$data['me_product_videos'][] = array(
							'image'      => $image,
							'thumb'      => $this->model_tool_image->resize($thumb, 100, 100),
							'video_type' => $me_product_video['video_type'],
							'video_link' => $me_product_video['video_link'],
							'status' => isset($me_product_video['status']) ? $me_product_video['status'] : 0,
							'sort_order' => !empty($me_product_video['sort_order']) ? $me_product_video['sort_order'] : 0,
						);
					}
				}
				#XML Mart Extensions
			]]></add>
		</operation>
	</file>
	<file path="admin/language/*/catalog/product.php">
		<operation>
			<search><![CDATA[// Help]]></search>
			<add position="before"><![CDATA[
				//XML Mart Extensions
				$_['tab_me_video']        = 'Videos';
				$_['entry_video_image']        = 'Video Image';
				$_['entry_video_type']        = 'Video Type';
				$_['entry_video_link']        = 'Video Link';
				$_['text_youtube']        = 'YouTube';
				$_['text_vimeo']        = 'Vimeo';
				$_['text_dailymotion']        = 'Dailymotion';
				$_['text_custom_upload']        = 'Custom upload';
				$_['text_generate_thumb']        = 'Generate Thumb';
				$_['button_video_add']        = 'Add Video';
			]]></add>
		</operation>
	</file>
	<file path="admin/view/template/catalog/product_form.twig">
		<operation>
			<search><![CDATA[<li><a href="#tab-reward" data-toggle="tab">{{ tab_reward }}</a></li>]]></search>
			<add position="before"><![CDATA[
				<!-- XML Mart Extensions -->
				{% if module_me_product_video_status %}
				<li><a href="#tab-me_video" data-toggle="tab">{{ tab_me_video }}</a></li>
				{% endif %}
				<!-- XML Mart Extensions -->
			]]></add>
		</operation>
		<operation>
			<search><![CDATA[<div class="tab-pane" id="tab-reward">]]></search>
			<add position="before"><![CDATA[
				{% if module_me_product_video_status %}
				<div class="tab-pane" id="tab-me_video">
					<table id="mevideos" class="table table-striped table-bordered table-hover">
						<thead>
							<tr>
								<td class="text-left">{{ entry_video_image }}</td>
								<td class="text-left">{{ entry_video_type }}</td>
								<td class="text-left">{{ entry_video_link }}</td>
								<td class="text-left" style="width:10%;">{{ entry_sort_order }}</td>
								<td class="text-right">{{ entry_status }}</td>
								<td></td>
							</tr>
						</thead>
						<tbody>
						{% set me_video_row = 0 %}
						{% for me_product_video in me_product_videos %}
						<tr id="me-video-row{{ me_video_row }}">
							<td class="text-left"><a href="" id="me-thumb-image{{ me_video_row }}" data-toggle="image" class="img-thumbnail"><img src="{{ me_product_video.thumb }}" alt="" title="" data-placeholder="{{ placeholder }}" /></a>
							  <input type="hidden" name="me_product_video[{{ me_video_row }}][image]" value="{{ me_product_video.image }}" id="me-input-image{{ me_video_row }}" /><br/>
							  {% if me_product_video.video_type != 4 %}
							  <a class="btn btn-success btn-sm" onclick="generatethumb('{{ me_video_row }}')">{{ text_generate_thumb }}</a>
							  {% endif %}
							  </td>
							<td class="text-right"><select name="me_product_video[{{ me_video_row }}][video_type]" onchange="videotype({{ me_video_row }},this.value)" id="input-video_type{{ me_video_row }}" class="form-control">
								<option value="1" {% if me_product_video.video_type == 1 %}selected="selected"{% endif %}>{{ text_youtube }}</option>
								<option value="2" {% if me_product_video.video_type == 2 %}selected="selected"{% endif %}>{{ text_vimeo }}</option>
								<option value="3" {% if me_product_video.video_type == 3 %}selected="selected"{% endif %}>{{ text_dailymotion }}</option>
								<option value="4" {% if me_product_video.video_type == 4 %}selected="selected"{% endif %}>{{ text_custom_upload }}</option>
							</select></td>
							<td class="text-right videolink">
								{% if me_product_video.video_type == 4 %}
								<div class="input-group"><input type="text" name="me_product_video[{{ me_video_row }}][video_link]" value="{{ me_product_video.video_link }}" placeholder="{{ entry_video_link }}" class="form-control" /><span class="input-group-btn"><button type="button" id="button-upload{{ me_video_row }}" data-loading-text="{{ text_loading }}" onclick="upload({{ me_video_row }})" class="btn btn-primary"><i class="fa fa-upload"></i> {{ button_upload }}</button></span></div>
								{% else %}
								<input type="text" name="me_product_video[{{ me_video_row }}][video_link]" value="{{ me_product_video.video_link }}" placeholder="{{ entry_video_link }}" class="form-control" />
								{% endif %}
							</td>
							<td class="text-left">
								<input type="text" name="me_product_video[{{ me_video_row }}][sort_order]" value="{{ me_product_video.sort_order }}" placeholder="{{ entry_sort_order }}" class="form-control" />
							</td>
							<td class="text-right">
								<label class="switch"><input type="checkbox" name="me_product_video[{{ me_video_row }}][status]" {% if me_product_video.status %}checked{% endif %}><span class="slider"></span></label>
							</td>
							<td class="text-right"><button type="button" onclick="$('#me-video-row{{ me_video_row }}').remove();" data-toggle="tooltip" title="{{ button_remove }}" class="btn btn-danger"><i class="fa fa-minus-circle"></i></button></td>
						</tr>
						{% set me_video_row = me_video_row + 1 %}
						{% endfor %}
						</tbody>
						<tfoot>
							<tr>
								<td colspan="5"></td>
								<td class="text-right"><button type="button" onclick="addVideo();" data-toggle="tooltip" title="{{ button_video_add }}" class="btn btn-primary"><i class="fa fa-plus-circle"></i></button></td>
							</tr>
						</tfoot>
					</table>
				</div>
				{% endif %}
			]]></add>
		</operation>
		<operation>
			<search><![CDATA[{{ footer }}]]></search>
			<add position="before"><![CDATA[
				{% if module_me_product_video_status %}
				<link href="view/javascript/me_product_video/style.css" rel="stylesheet">
				<script type="text/javascript"><!--
				var me_video_row = {{ me_video_row }};

				function addVideo() {
					html  = '<tr id="me-video-row' + me_video_row + '">';
					html += '  <td class="text-left"><a href="" id="me-thumb-image' + me_video_row + '" data-toggle="image" class="img-thumbnail"><img src="{{ placeholder }}" alt="" title="" data-placeholder="{{ placeholder }}" /></a><input type="hidden" name="me_product_video[' + me_video_row + '][image]" value="" id="me-input-image' + me_video_row + '" /><br/><a class="btn btn-success btn-sm" onclick="generatethumb(' + me_video_row + ')">{{ text_generate_thumb }}</a></td>';
					html += '<td class="text-right"><select name="me_product_video[' + me_video_row + '][video_type]" onchange="videotype(' + me_video_row + ',this.value)" id="input-video_type' + me_video_row + '" class="form-control">';
					html += '<option value="1">{{ text_youtube }}</option>';
					html += '<option value="2">{{ text_vimeo }}</option>';
					html += '<option value="3">{{ text_dailymotion }}</option>';
					html += '<option value="4">{{ text_custom_upload }}</option>';
					html += '</select></td>';
					html += '<td class="text-right videolink"><input type="text" name="me_product_video[' + me_video_row + '][video_link]" value="" placeholder="{{ entry_video_link }}" class="form-control" /></td>';
					html += '<td class="text-left">';
					html += '	<input type="text" name="me_product_video[' + me_video_row + '][sort_order]" value="" placeholder="{{ entry_sort_order }}" class="form-control" />';
					html += '</td>';
					html += '<td class="text-right"><label class="switch"><input type="checkbox" name="me_product_video[' + me_video_row + '][status]"><span class="slider"></span></label></td>';
					html += '  <td class="text-right"><button type="button" onclick="$(\'#me-video-row' + me_video_row + '\').remove();" data-toggle="tooltip" title="{{ button_remove }}" class="btn btn-danger"><i class="fa fa-minus-circle"></i></button></td>';
					html += '</tr>';

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

					me_video_row++;
				}

				function videotype(row,value){
					if(value == 4){
						$('#me-video-row'+ row +' .videolink').html('<div class="input-group"><input type="text" name="me_product_video[' + row + '][video_link]" value="" placeholder="{{ entry_video_link }}" class="form-control" /><span class="input-group-btn"><button type="button" id="button-upload' + row + '" data-loading-text="{{ text_loading }}" onclick="upload(' + row + ')" class="btn btn-primary"><i class="fa fa-upload"></i> {{ button_upload }}</button></span></div>');
						$('#me-video-row'+ row +' .btn-success').hide();
					}else{
						$('#me-video-row'+ row +' .videolink').html('<input type="text" name="me_product_video[' + row + '][video_link]" value="" placeholder="{{ entry_video_link }}" class="form-control" />');
						$('#me-video-row'+ row +' .btn-success').show();
					}
				}

				function generatethumb(row){
					$.ajax({
						url: 'index.php?route=extension/module/me_product_video/generatethumb&user_token={{ user_token }}',
						type: 'post',
						dataType: 'json',
						data: 'video_type=' + encodeURIComponent($('select[name=\'me_product_video[' + row + '][video_type]\']').val()) + '&video_link=' + encodeURIComponent($('input[name=\'me_product_video[' + row + '][video_link]\']').val()),
						beforeSend: function() {
							
						},
						complete: function() {
							
						},
						success: function(json) {
							if (json['error']) {
								alert(json['error']);
							}
							
							if (json['video_thumb']) {
								$('#me-video-row'+ row +' .img-thumbnail img').attr('src',json['video_thumb']);
								$('#me-video-row'+ row +' #me-input-image'+ row).val(json['video_image']);
							}
							if (json['success']) {
								alert(json['success']);

								$('#me-video-row'+ row +' .videolink input[name=\'me_product_video[' + row + '][video_link]\']').val(json['filename']);
							}
						}
					});
				}
				//--></script>
				<script type="text/javascript"><!--
				function upload(row){
					$('#form-upload').remove();

					$('body').prepend('<form enctype="multipart/form-data" id="form-upload" style="display: none;"><input type="file" name="file" /></form>');

					$('#form-upload input[name=\'file\']').trigger('click');

					if (typeof timer != 'undefined') {
						clearInterval(timer);
					}

					timer = setInterval(function() {
						if ($('#form-upload input[name=\'file\']').val() != '') {
							clearInterval(timer);

							$.ajax({
								url: 'index.php?route=extension/module/me_product_video/upload&user_token={{ user_token }}',
								type: 'post',
								dataType: 'json',
								data: new FormData($('#form-upload')[0]),
								cache: false,
								contentType: false,
								processData: false,
								beforeSend: function() {
									$('#button-upload'+row).button('loading');
								},
								complete: function() {
									$('#button-upload'+row).button('reset');
								},
								success: function(json) {
									if (json['error']) {
										alert(json['error']);
									}

									if (json['success']) {
										alert(json['success']);

										$('#me-video-row'+ row +' .videolink input[name=\'me_product_video[' + row + '][video_link]\']').val(json['filename']);
									}
								},
								error: function(xhr, ajaxOptions, thrownError) {
									alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
								}
							});
						}
					}, 500);
				}
				//--></script>
				{% endif %}
			]]></add>
		</operation>
	</file>
	<file path="admin/model/catalog/product.php">
		<operation>
			<search><![CDATA[if (isset($data['product_layout'])) {]]></search>
			<add position="before"><![CDATA[
				#XML Mart Extensions
				if(isset($data['me_product_video'])){
					$this->load->model('extension/me_product_video');
					$this->model_extension_me_product_video->addVideo($data['me_product_video'],$product_id);
				}else{
					$this->db->query("DELETE FROM `" . DB_PREFIX . "me_product_video` WHERE product_id = '" . (int)$product_id . "'");
				}
				#XML Mart Extensions
			]]></add>
		</operation>
		<operation>
			<search><![CDATA[$this->addProduct($data);]]></search>
			<add position="before"><![CDATA[
				#XML Mart Extensions
				$this->load->model('extension/me_product_video');
				$data['me_product_video'] = $this->model_extension_me_product_video->getVideos($product_id);
				#XML Mart Extensions
			]]></add>
		</operation>
		<operation>
			<search><![CDATA[$this->db->query("DELETE FROM " . DB_PREFIX . "coupon_product WHERE product_id = '" . (int)$product_id . "'");]]></search>
			<add position="after"><![CDATA[
				#XML Mart Extensions
				$this->db->query("DELETE FROM " . DB_PREFIX . "me_product_video WHERE product_id = '" . (int)$product_id . "'");
				#XML Mart Extensions
			]]></add>
		</operation>
	</file>
	<!-- Catalog Code -->
	<file path="catalog/controller/product/product.php">
		<operation>
			<search><![CDATA[$data['images'] = array();]]></search>
			<add position="after"><![CDATA[
				#XML Mart Extensions
				$data['me_product_video_status'] = $this->config->get('module_me_product_video_status');
				$data['me_product_video_position'] = $this->config->get('module_me_product_video_position');
				if($this->config->get('module_me_product_video_status')){
					$this->document->addStyle('catalog/view/javascript/me_video/style.css');
					if($this->config->get('module_me_product_video_thumbnail')){
						$data['me_product_video_thumbnail'] = $this->model_tool_image->resize($this->config->get('module_me_product_video_thumbnail'), 30, 30);
					}else{
						$data['me_product_video_thumbnail'] = '';
					}
					
					$data['me_product_video_show_video'] = $this->config->get('module_me_product_video_show_video');
					$data['me_product_video_display'] = $this->config->get('module_me_product_video_display');
					$data['me_product_video_autoplay'] = $this->config->get('module_me_product_video_autoplay');
					$data['me_product_video_controls'] = $this->config->get('module_me_product_video_controls');
					$data['me_popup_width'] = $this->config->get('module_me_product_video_popup_width');
					$data['me_popup_height'] = $this->config->get('module_me_product_video_popup_height');
					
					
					if($data['me_product_video_show_video'] && !$this->config->get('module_me_product_video_display')){
						if (defined('JOURNAL3_ACTIVE')) {
							$data['me_video_height'] = $this->journal3->settings->get('image_dimensions_thumb.height');
						}else{
							$data['me_video_height'] = $this->config->get('theme_' . $this->config->get('config_theme') . '_image_thumb_height');
						}
					}else{
						$data['me_video_height'] = !empty($this->config->get('module_me_product_video_popup_height')) ? ($this->config->get('module_me_product_video_popup_height') - 10) : $this->config->get('theme_' . $this->config->get('config_theme') . '_image_popup_height') - 10;
					}
					
					$data['me_product_video_tabname'] = $this->config->get('module_me_product_video_desc')[$this->config->get('config_language_id')]['name'];
					$data['me_product_video_tabdesc'] = html_entity_decode($this->config->get('module_me_product_video_desc')[$this->config->get('config_language_id')]['description']);
					
					$this->load->model('extension/me_product_video');
					$me_videos = $this->model_extension_me_product_video->getMeProductVideos($this->request->get['product_id']);
					
					$data['me_videos'] = array();
					
					if(($data['me_product_video_show_video'] && !$data['me_product_video_display']) || !$data['me_product_video_show_video']){
						foreach($me_videos as $me_video){
							$data['me_videos'][] = array(
								'popup' => $me_video['video_link'],
								'thumb' => $me_video['image'],
								'video_type' => $me_video['video_type']
							);
						}
					}
					
					if($data['me_product_video_position']){
						if(($data['me_product_video_show_video'])){
							foreach($me_videos as $me_video){
								$data['images'][] = array(
									'galleryThumb'  => $this->model_journal3_image->resize($me_video['journal_image'], $this->journal3->settings->get('image_dimensions_popup_thumb.width'), $this->journal3->settings->get('image_dimensions_popup_thumb.height'), $this->journal3->settings->get('image_dimensions_popup_thumb.resize')),
									'popup' => $me_video['video_link'],
									'thumb'         => $this->model_journal3_image->resize($me_video['journal_image'], $this->journal3->settings->get('image_dimensions_additional.width'), $this->journal3->settings->get('image_dimensions_additional.height'), $this->journal3->settings->get('image_dimensions_additional.resize')),
									 'thumb2x'       => $this->model_journal3_image->resize($me_video['journal_image'], $this->journal3->settings->get('image_dimensions_additional.width') * 2, $this->journal3->settings->get('image_dimensions_additional.height') * 2, $this->journal3->settings->get('image_dimensions_additional.resize')),
									 'image'         => $this->model_journal3_image->resize($me_video['journal_image'], $this->journal3->settings->get('image_dimensions_thumb.width'), $this->journal3->settings->get('image_dimensions_thumb.height'), $this->journal3->settings->get('image_dimensions_thumb.resize')),
									'image2x'       => $this->model_journal3_image->resize($me_video['journal_image'], $this->journal3->settings->get('image_dimensions_thumb.width') * 2, $this->journal3->settings->get('image_dimensions_thumb.height') * 2, $this->journal3->settings->get('image_dimensions_thumb.resize')),
									'video_type' => $me_video['video_type'],
									'type' => 'video'
								);
							}
						}
					}
				}
				#XML Mart Extensions
			]]></add>
		</operation>
		<operation>
			<search index="0"><![CDATA[if ($this->customer->isLogged() || !$this->config->get('config_customer_price')) {]]></search>
			<add position="before"><![CDATA[
				#XML Mart Extensions
				if(!$data['me_product_video_position'] && $this->config->get('module_me_product_video_status')){
					if(($data['me_product_video_show_video'])){
						foreach($me_videos as $me_video){
							$data['images'][] = array(
								'galleryThumb'  => $this->model_journal3_image->resize($me_video['journal_image'], $this->journal3->settings->get('image_dimensions_popup_thumb.width'), $this->journal3->settings->get('image_dimensions_popup_thumb.height'), $this->journal3->settings->get('image_dimensions_popup_thumb.resize')),
								'popup' => $me_video['video_link'],
								'thumb'         => $this->model_journal3_image->resize($me_video['journal_image'], $this->journal3->settings->get('image_dimensions_additional.width'), $this->journal3->settings->get('image_dimensions_additional.height'), $this->journal3->settings->get('image_dimensions_additional.resize')),
								 'thumb2x'       => $this->model_journal3_image->resize($me_video['journal_image'], $this->journal3->settings->get('image_dimensions_additional.width') * 2, $this->journal3->settings->get('image_dimensions_additional.height') * 2, $this->journal3->settings->get('image_dimensions_additional.resize')),
								 'image'         => $this->model_journal3_image->resize($me_video['journal_image'], $this->journal3->settings->get('image_dimensions_thumb.width'), $this->journal3->settings->get('image_dimensions_thumb.height'), $this->journal3->settings->get('image_dimensions_thumb.resize')),
								'image2x'       => $this->model_journal3_image->resize($me_video['journal_image'], $this->journal3->settings->get('image_dimensions_thumb.width') * 2, $this->journal3->settings->get('image_dimensions_thumb.height') * 2, $this->journal3->settings->get('image_dimensions_thumb.resize')),
								'video_type' => $me_video['video_type'],
								'type' => 'video'
							);
						}
					}
				}
				#XML Mart Extensions
			]]></add>
		</operation>
		<operation>
			<search><![CDATA[foreach ($product_tabs as $position => &$items) {]]></search>
			<add position="before"><![CDATA[
				#XML Mart Extensions
				$this->load->model('extension/me_product_video');
				#XML Mart Extensions
			]]></add>
		</operation>
		<operation>
			<search><![CDATA[foreach ($_items as $items) {]]></search>
			<add position="before"><![CDATA[
				#XML Mart Extensions
				if (!$this->config->get('module_me_product_video_show_video')) {
					$_items[$item['display']][] = array(
						'title' => $this->config->get('module_me_product_video_desc')[$this->config->get('config_language_id')]['name'],
						'sort' => 10,
						'display' => 'tabs',
						'id' => 'product-video',
						'classes' => array('tab-pane' => 1),
						'content' => $this->model_extension_me_product_video->getProductvideotab($this->request->get['product_id'])
					);
				}
				#XML Mart Extensions
			]]></add>
		</operation>
	</file>
	<file path="catalog/view/theme/*/template/product/product.twig">
		<operation>
			<search><![CDATA[{{ footer }}]]></search>
			<add position="before"><![CDATA[
				{% if me_product_video_status %}
				<script type="text/javascript" src="catalog/view/javascript/me_video/common.js"></script>
				<script type="text/javascript"><!--
				$('.image-additional a,.additional-image').each(function() {
					var index_value = $(this).data('index');
					video_href = $('.main-image .swiper-wrapper').find("[data-index='" + index_value + "']").find('img').data('largeimg');
					
					if (video_href == undefined) {
						video_href = $('.main-image .swiper-wrapper').find("[data-swiper-slide-index='" + index_value + "']").find('img').data('largeimg');
					}
					if (video_href == undefined) {
						video_href = $(this).attr('href');
					}
					
					if (video_href == undefined) {
						video_href = $(this).attr('video-href');
					}
					
					if (video_href.indexOf('#frame') >= 0 || video_href.indexOf('#local') >= 0) {
						$(this).attr('meautoplay','{{ me_product_video_autoplay }}');
						$(this).attr('mecontrol','{{ me_product_video_controls }}');
						$(this).attr('metype','{{ me_product_video_display }}');
						$(this).addClass('video-link');
					}
				});
				{% if not me_product_video_display %}
				$('.thumbnails li:first a').after('<div class="mainthumbvideo"></div>');
				{% endif %}
				//--></script> 
				<style>
				@media(min-width:768px){
				.mevideo-popup{
					width: {{ me_popup_width }}px;
					max-height: {{ me_popup_height }}px;
				}
				}
				.mevideo-video{
					max-height:{{ me_video_height }}px;
				}
				.mevideomain-video{
					height:100%;
				}
				{% if me_product_video_thumbnail %}
				.video-link:after{
					background-image: url('{{ me_product_video_thumbnail }}');
				}
				{% endif %}
				</style>
				{% endif %}
			]]></add>
		</operation>
		<operation>
			<search><![CDATA[{{ tab_review }}</a></li>]]></search>
			<add position="after" offset="1"><![CDATA[
				{% if me_product_video_status and not me_product_video_show_video and me_videos %}
				 <li><a href="#tab-me_video" data-toggle="tab">{{ me_product_video_tabname }}</a></li>
				{% endif %}
			]]></add>
		</operation>
		<operation>
			<search><![CDATA[{{ description }}</div>]]></search>
			<add position="after"><![CDATA[
				{% if me_product_video_status and not me_product_video_show_video and me_videos %}
				<div class="tab-pane" id="tab-me_video">
					{{ me_product_video_tabdesc }}
					<ul class="list-inline mevideos">
						{% for me_video in me_videos %}
							<li><a class="thumbnail video-link" href="{{ me_video.popup }}" meautoplay="{{ me_product_video_autoplay }}" mecontrol="{{ me_product_video_controls }}" title="{{ heading_title }}"> <img src="{{ me_video.thumb }}" title="{{ heading_title }}" alt="{{ heading_title }}" /></a></li>
						{% endfor %}
					</ul>
				</div>
				{% endif %}
			]]></add>
		</operation>
	</file>
</modification>