<?xml version="1.0" encoding="utf-8"?>
<modification>
	<name>Product Questions &amp; Answers</name>
	<code>questions_and_answers</code>
	<version>2.0.7</version>
	<author>DearSoft</author>
	<link>https://www.dearsoft.bd</link>

	<file path="system/config/qa.php">
		<operation info="Indicate that the OCMOD script is working" error="log">
			<search><![CDATA[<?php]]></search>
			<add position="after"><![CDATA[$_['qa_ocmod_script_working'] = true;
			]]></add>
		</operation>
	</file>

	<file path="admin/controller/design/layout.php">
		<operation info="Hack: filter out Q&amp;A from modules list" error="log">
			<search><![CDATA[foreach ($extensions as $code)]]></search>
			<add position="after"><![CDATA[
			if ($code == 'qa') continue;
			]]></add>
		</operation>
	</file>

	<file path="admin/model/setting/event.php">
		<operation info="Get event data">
			<ignoreif><![CDATA[public function getEventByCodeTriggerAction($code, $trigger, $action)]]></ignoreif>
			<search><![CDATA[
			public function addEvent
			]]></search>
			<add position="before"><![CDATA[
	public function getEventByCodeTriggerAction($code, $trigger, $action) {
		$event = $this->db->query("SELECT * FROM `" . DB_PREFIX . "event` WHERE `code` = '" . $this->db->escape($code) . "' AND `trigger` = '" . $this->db->escape($trigger) . "' AND `action` = '" . $this->db->escape($action) . "'");

		return $event->rows;
	}
			]]></add>
		</operation>
	</file>

	<file path="admin/controller/common/column_left.php">
		<operation>
			<search><![CDATA[
			if ($this->user->hasPermission('access', 'catalog/information')) {
			]]></search>
			<add position="before"><![CDATA[
		if ($this->config->get('module_qa_status') && $this->user->hasPermission('access', 'extension/module/qa')) {
			$this->load->language("extension/module/qa", "qa");
			$catalog[] = array(
				'name'     => $this->language->get("qa")->get('text_menu_qa'),
				'href'     => $this->url->link('extension/module/qa/view', 'user_token=' . $this->session->data['user_token'], true),
				'children' => array()
			);
		}
			]]></add>
		</operation>
	</file>

	<file path="admin/controller/setting/setting.php">
		<operation>
			<search><![CDATA[
			$data['captcha_pages'] = array()
			]]></search>
			<add position="after"><![CDATA[
		if ($this->config->get('module_qa_status')) {
			$this->load->language("extension/module/qa", "qa");
			$data['captcha_pages'][] = array(
				'text'  => $this->language->get("qa")->get('text_captcha_qa'),
				'value' => 'qa'
			);
		}
			]]></add>
		</operation>
	</file>

	<file path="system/library/pagination.php">
		<operation>
			<ignoreif><![CDATA[function render_custom()]]></ignoreif>
			<search><![CDATA[
			function render()
			]]></search>
			<add position="before"><![CDATA[
	public function render_custom() {
		$total = $this->total;

		if (!property_exists($this, 'page_var') || !isset($this->page_var)) {
			$this->page_var = 'page';
		}

		if ($this->page < 1) {
			$page = 1;
		} else {
			$page = $this->page;
		}

		if (!(int)$this->limit) {
			$limit = 10;
		} else {
			$limit = $this->limit;
		}

		$num_links = $this->num_links;
		$num_pages = ceil($total / $limit);

		$this->url = str_replace('%7Bpage%7D', '{page}', $this->url);

		$output = '<ul class="' . $this->style . '">';

		if ($page > 1) {
			$output .= '<li><a href="' . str_replace(array("&amp;{$this->page_var}={page}", "?{$this->page_var}={page}", "&{$this->page_var}={page}"), '', $this->url) . '">' . $this->text_first . '</a></li>';

			if ($page - 1 === 1) {
				$output .= '<li><a href="' . str_replace(array("&amp;{$this->page_var}={page}", "?{$this->page_var}={page}", "&{$this->page_var}={page}"), '', $this->url) . '">' . $this->text_prev . '</a></li>';
			} else {
				$output .= '<li><a href="' . str_replace('{page}', $page - 1, $this->url) . '">' . $this->text_prev . '</a></li>';
			}
		}

		if ($num_pages > 1) {
			if ($num_pages <= $num_links) {
				$start = 1;
				$end = $num_pages;
			} else {
				$start = $page - floor($num_links / 2);
				$end = $page + floor($num_links / 2);

				if ($start < 1) {
					$end += abs($start) + 1;
					$start = 1;
				}

				if ($end > $num_pages) {
					$start -= ($end - $num_pages);
					$end = $num_pages;
				}
			}

			if ($start > 1) {
				$output .= '<li class="disabled"><a href="#">&middot;&middot;&middot;</a></li>';
			}

			for ($i = $start; $i <= $end; $i++) {
				if ($page == $i) {
					$output .= '<li class="active"><span>' . $i . '</span></li>';
				} else {
					if ($i === 1) {
						$output .= '<li><a href="' . str_replace(array("&amp;{$this->page_var}={page}", "?{$this->page_var}={page}", "&{$this->page_var}={page}"), '', $this->url) . '">' . $i . '</a></li>';
					} else {
						$output .= '<li><a href="' . str_replace('{page}', $i, $this->url) . '">' . $i . '</a></li>';
					}
				}
			}

			if ($end < $num_pages) {
				$output .= '<li class="disabled"><a href="#">&middot;&middot;&middot;</a></li>';
			}
		}

		if ($page < $num_pages) {
			$output .= '<li><a href="' . str_replace('{page}', $page + 1, $this->url) . '">' . $this->text_next . '</a></li>';
			$output .= '<li><a href="' . str_replace('{page}', $num_pages, $this->url) . '">' . $this->text_last . '</a></li>';
		}

		$output .= '</ul>';

		if ($num_pages > 1) {
			return $output;
		} else {
			return '';
		}
	}
			]]></add>
		</operation>
	</file>

	<file path="system/library/template/twig.php">
		<operation info="Add custom Twig filters OC &lt; 3.0.3.6" error="skip">
			<search><![CDATA[
			$template = $this->twig->loadTemplate($template
			]]></search>
			<add position="before"><![CDATA[
			if (strpos($template, "extension/module/qa") !== FALSE) {
				$this->twig->addFilter(new \Twig_SimpleFilter('addslashes', 'addslashes'));
				$this->twig->addFilter(new \Twig_SimpleFilter('html_entity_decode', 'html_entity_decode'));
				$this->twig->addFilter(new \Twig_SimpleFilter('map', 'array_map'));
				$this->twig->addFilter(new \Twig_SimpleFilter('replace_nl2br', function($string) {
					return str_replace(array("\r\n", "\n\r", "\r", "\n"), "<br>", $string);
				}));
			}
			]]></add>
		</operation>
		<operation info="Add custom Twig filters OC &gt;= 3.0.3.6" error="skip">
			<search><![CDATA[
			return $twig->render($filename
			]]></search>
			<add position="before"><![CDATA[
			if (strpos($filename, "extension/module/qa") !== FALSE) {
				$twig->addFilter(new \Twig\TwigFilter('addslashes', 'addslashes'));
				$twig->addFilter(new \Twig\TwigFilter('html_entity_decode', 'html_entity_decode'));
				$twig->addFilter(new \Twig\TwigFilter('map', 'array_map'));
				$twig->addFilter(new \Twig\TwigFilter('replace_nl2br', function($string) {
					return str_replace(array("\r\n", "\n\r", "\r", "\n"), "<br>", $string);
				}));
			}
			]]></add>
		</operation>
	</file>

	<file path="catalog/view/theme/journal3/template/product/product.twig">
		<operation>
			<!--
				This is needed for the Q&A tab to load the first page of questions if no content has been preloaded
				(extension setting 'Preload questions' = None) or all content has been preloaded (extension setting
				'Preload questions' = All).

				This code block must be included somewhere on the product page, but it does not matter where exactly.
				The string '{{ footer }}' should exist on every possible custom theme product.tpl file, so
				this block can most probably be left unchanged.
			-->
			<search><![CDATA[
			{{ footer }}
			]]></search>
			<add position="before"><![CDATA[
{% if module_qa_status %}
<script type="text/javascript"><!--
$("body").on("click","#qa-ask",function(a){a.preventDefault(),$("a[href='#tab-qa']").trigger("click"),$("html,body").animate({scrollTop:$("#qa-title").offset().top},1300)}).on("click","#qa .pagination a",function(){var a=this.href;return $("#qa").slideUp("slow",function(){$("#qa").load(a,function(){$("#qa").slideDown("slow")})}),!1});{% if preload != 1 %}$('#qa').load('index.php?route=extension/module/qa/question&product_id={{ product_id }}');{% endif %}$("#button-qa").on("click",function(){$.ajax({type:"POST",url:"index.php?route=extension/module/qa/ask&product_id={{ product_id }}",dataType:"json",data:$("#q-form :input").serialize(),beforeSend:function(){$("#button-qa").button("loading")},complete:function(){$("#button-qa").button("reset")},success:function(a){$(".alert-success, .alert-danger").remove(),a.error&&$("#qa-title").before('<div class="alert alert-danger"><i class="fa fa-exclamation-circle"></i> '+a.error+"</div>"),a.success&&($("#qa-title").before('<div class="alert alert-success"><i class="fa fa-check-circle"></i> '+a.success+"</div>"),$("textarea[name='q_question']").val(""))},error:function(a){$("#qa-title").before('<div class="alert alert-danger"><i class="fa fa-exclamation-circle"></i> '+a.statusText+"</div>")}})});

//--></script>
{% endif %}
			]]></add>
		</operation>
	</file>
</modification>
