<?php
/**
 * @copyright    OCTemplates
 * @support      https://octemplates.net/
 * @license      LICENSE.txt
 */

class ControllerOCTemplatesBlogOCTBlogArticle extends Controller {
	private $error = [];

	public function index() {
		if (!$this->config->get('oct_blogsettings_status')) {
			$this->response->redirect($this->url->link('common/home', '', true));
		}

		$oct_promo_data = $this->config->get('theme_oct_promo_data');

		if ($this->registry->has('oct_mobiledetect')) {
			if ($this->oct_mobiledetect->isMobile() && !$this->oct_mobiledetect->isTablet()) {
				$data['oct_isMobile'] = $this->oct_mobiledetect->isMobile();
			}

			if ($this->oct_mobiledetect->isTablet()) {
				$data['oct_isTablet'] = $this->oct_mobiledetect->isTablet();
			}
		}

		$oct_blogsettings_data = $this->config->get('oct_blogsettings_data');

		$this->load->language('octemplates/blog/oct_blogarticle');

		$data['breadcrumbs'] = [];

		$data['breadcrumbs'][] = [
			'text' => $this->language->get('text_home'),
			'href' => $this->url->link('common/home')
		];

		$this->load->model('octemplates/blog/oct_blogcategory');

		if (isset($this->request->get['blog_path'])) {
			$path = '';

			$parts = explode('_', (string)$this->request->get['blog_path']);

			$blogcategory_id = (int)array_pop($parts);

			foreach ($parts as $path_id) {
				if (!$path) {
					$path = $path_id;
				} else {
					$path .= '_' . $path_id;
				}

				$category_info = $this->model_octemplates_blog_oct_blogcategory->getBlogCategory($path_id);

				if ($category_info) {
					$data['breadcrumbs'][] = [
						'text' => $category_info['name'],
						'href' => $this->url->link('octemplates/blog/oct_blogcategory', 'blog_path=' . $path)
					];
				}
			}

			// Set the last category breadcrumb
			$category_info = $this->model_octemplates_blog_oct_blogcategory->getBlogCategory($blogcategory_id);

			if ($category_info) {
				$url = '';

				if (isset($this->request->get['sort'])) {
					$url .= '&sort=' . $this->request->get['sort'];
				}

				if (isset($this->request->get['order'])) {
					$url .= '&order=' . $this->request->get['order'];
				}

				if (isset($this->request->get['page'])) {
					$url .= '&page=' . $this->request->get['page'];
				}

				if (isset($this->request->get['limit'])) {
					$url .= '&limit=' . $this->request->get['limit'];
				}

				$data['breadcrumbs'][] = [
					'text' => $category_info['name'],
					'href' => $this->url->link('octemplates/blog/oct_blogcategory', 'blog_path=' . $this->request->get['blog_path'] . $url)
				];
			}
		}

		if (isset($this->request->get['search']) || isset($this->request->get['tag'])) {
			$url = '';

			if (isset($this->request->get['search'])) {
				$url .= '&search=' . $this->request->get['search'];
			}

			if (isset($this->request->get['tag'])) {
				$url .= '&tag=' . $this->request->get['tag'];
			}

			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}

			if (isset($this->request->get['limit'])) {
				$url .= '&limit=' . $this->request->get['limit'];
			}
		}

		if (isset($this->request->get['blogarticle_id'])) {
			$blogarticle_id = (int)$this->request->get['blogarticle_id'];
		} else {
			$blogarticle_id = 0;
		}

		$this->load->model('octemplates/blog/oct_blogarticle');
		$this->load->model('octemplates/microdata');

		$article_info = $this->model_octemplates_blog_oct_blogarticle->getArticle($blogarticle_id);

		if ($article_info) {
			$url = '';

			if (isset($this->request->get['blog_path'])) {
				$url .= '&blog_path=' . $this->request->get['blog_path'];
			}

			if (isset($this->request->get['search'])) {
				$url .= '&search=' . $this->request->get['search'];
			}

			if (isset($this->request->get['tag'])) {
				$url .= '&tag=' . $this->request->get['tag'];
			}

			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}

			if (isset($this->request->get['limit'])) {
				$url .= '&limit=' . $this->request->get['limit'];
			}

			$data['breadcrumbs'][] = [
				'text' => $article_info['name'],
				'href' => $this->url->link('octemplates/blog/oct_blogarticle', $url . '&blogarticle_id=' . $this->request->get['blogarticle_id'])
			];

			$this->document->setTitle($article_info['meta_title']);
			$this->document->setDescription($article_info['meta_description']);
			$this->document->setKeywords($article_info['meta_keyword']);
			$this->document->addLink($this->url->link('octemplates/blog/oct_blogarticle', 'blogarticle_id=' . $this->request->get['blogarticle_id']), 'canonical');

			$data['heading_title'] = $article_info['name'];

			$this->load->model('catalog/review');

			$data['blogarticle_id'] = (int)$this->request->get['blogarticle_id'];

			$data['description'] = str_replace("<img", "<img class='img-fluid'", html_entity_decode($article_info['description'], ENT_QUOTES, 'UTF-8'));

			$data['date_added'] = $this->load->controller('octemplates/main/oct_functions/OctDateTime', array($article_info['date_added'], 1));

			$this->load->model('tool/image');

			if ($article_info['image'] && (isset($oct_blogsettings_data['show_main_image']) && $oct_blogsettings_data['show_main_image'])) {
				$data['thumb'] = $this->model_tool_image->resize($article_info['image'], $oct_blogsettings_data['article_width'], $oct_blogsettings_data['article_height']);
			} else {
				$data['thumb'] = '';
			}

			$data['thumb_width'] = $oct_blogsettings_data['article_width'];
			$data['thumb_height'] = $oct_blogsettings_data['article_height'];

			if (isset($oct_promo_data['preload_images']) && $oct_promo_data['preload_images'] && !empty($data['thumb'])) {
				$this->document->setOCTPreload($data['thumb']);
			}

			$data['images'] = [];

			$results = $this->model_octemplates_blog_oct_blogarticle->getArticleImages($this->request->get['blogarticle_id']);

			foreach ($results as $result) {
				if (isset($result['image']) && !empty($result['image']) && $result['image'] && file_exists(DIR_IMAGE . $result['image'])) {
					$data['images'][] = [
						'thumb'			=> $this->model_tool_image->resize($result['image'], $oct_blogsettings_data['article_dop_width'], $oct_blogsettings_data['article_dop_height']),
						'thumb_width'	=> $oct_blogsettings_data['article_dop_width'],
						'thumb_height'	=> $oct_blogsettings_data['article_dop_height'],
						'popup'			=> $this->model_tool_image->resize($result['image'], $oct_blogsettings_data['article_width'], $oct_blogsettings_data['article_height']),
						'popup_width'	=> $oct_blogsettings_data['article_width'],
						'popup_height'	=> $oct_blogsettings_data['article_height'],
					];
				}
			}

			if (!empty($data['images'])) {
				$this->document->addScript('catalog/view/theme/oct_promo/js/fancybox/jquery.fancybox.min.js');
				$this->document->addStyle('catalog/view/theme/oct_promo/js/fancybox/jquery.fancybox.min.css');
			}

			$data['review_status'] = false;

			if (isset($oct_blogsettings_data['comments']) && $oct_blogsettings_data['comments'] == 'on') {
				$review_status = $this->config->get('config_review_status');

				if ($this->config->get('config_review_guest') || $this->customer->isLogged()) {
					$review_guest = true;
				} else {
					$review_guest = false;
				}

				if ($review_status && $review_guest) {
					$data['review_status'] = true;
				}
			}

			if ($this->customer->isLogged()) {
				$data['customer_name'] = $this->customer->getFirstName();
			} else {
				$data['customer_name'] = '';
			}

			$data['comments_total'] = sprintf($this->language->get('text_reviews'), (int)$article_info['comments_total']);
			$data['comments_viewed'] = sprintf($this->language->get('text_viewed'), (int)$article_info['viewed']);

			// Captcha
			if ($this->config->get('captcha_' . $this->config->get('config_captcha') . '_status') && in_array('review', (array)$this->config->get('config_captcha_page'))) {
				$data['captcha'] = $this->load->controller('extension/captcha/' . $this->config->get('config_captcha'));
			} else {
				$data['captcha'] = '';
			}

			$canonical_url = '';
			if (isset($this->request->get['blog_path'])) {
				$blog_path = (string)$this->request->get['blog_path'];
				if (preg_match('/^[0-9]+(?:_[0-9]+)*$/', $blog_path)) {
					$canonical_url .= '&blog_path=' . $blog_path;
				}
			}
			$canonical_url .= '&blogarticle_id=' . (int)$blogarticle_id;
			$data['canonical'] = $this->url->link('octemplates/blog/oct_blogarticle', ltrim($canonical_url, '&'));

			$data['meta_description'] = $article_info['meta_description'];
			$data['store_name'] = $this->config->get('config_name');
			$data['store_url'] = HTTP_SERVER;
			$data['logo_url'] = HTTP_SERVER . 'image/' . ($this->config->get('config_logo') ?: 'catalog/opencart-logo.png');
			$data['language_code'] = $this->language->get('code');
			$data['date_added'] = $article_info['date_added'];
			$data['date_modified'] = $article_info['date_modified'] ?? null;

			if ($article_info['image']) {
				$data['popup'] = $this->model_tool_image->resize($article_info['image'], $oct_blogsettings_data['article_width'], $oct_blogsettings_data['article_height']);
				$image_info = @getimagesize(DIR_IMAGE . $article_info['image']);
				$data['image_width'] = $image_info ? $image_info[0] : $oct_blogsettings_data['article_width'];
				$data['image_height'] = $image_info ? $image_info[1] : $oct_blogsettings_data['article_height'];
			} else {
				$data['popup'] = '';
				$data['image_width'] = 800;
				$data['image_height'] = 600;
			}

			if (!empty($article_info['tag'])) {
				$data['tags'] = array_map('trim', explode(',', $article_info['tag']));
			} else {
				$data['tags'] = [];
			}

			$this->load->model('octemplates/blog/oct_blogcomment');
			$data['reviews_total'] = $this->model_octemplates_blog_oct_blogcomment->getTotalCommentsByArticleId($blogarticle_id);

			$data['articles'] = [];

			$artusle_results = $this->model_octemplates_blog_oct_blogarticle->getArticleRelated($this->request->get['blogarticle_id']);

			foreach ($artusle_results as $result) {
				if ($result['image']) {
					$image = $this->model_tool_image->resize($result['image'], $oct_blogsettings_data['dop_article_width'], $oct_blogsettings_data['dop_article_height']);
				} else {
					$image = $this->model_tool_image->resize('placeholder.png', $oct_blogsettings_data['dop_article_width'], $oct_blogsettings_data['dop_article_height']);
				}

				// Get categories 
				$blog_category_badge = $this->model_octemplates_blog_oct_blogcategory->getBlogCategoryBadges($result['blogarticle_id']);

				$description = !empty(trim(strip_tags($result['shot_description']))) ? $result['shot_description'] : $result['description'];

				$data['articles'][] = [
					'blogarticle_id'		=> $result['blogarticle_id'],
					'thumb'					=> $image,
					'width'					=> $oct_blogsettings_data['dop_article_width'],
					'height'				=> $oct_blogsettings_data['dop_article_height'],
					'name'					=> $result['name'],
					'blog_categories'		=> $blog_category_badge,
					'description'			=> utf8_substr(trim(strip_tags(html_entity_decode($description, ENT_QUOTES, 'UTF-8'))), 0, $oct_blogsettings_data['description_length']) . '..',
					'date_added'         	=> $this->load->controller('octemplates/main/oct_functions/OctDateTime', array($result['date_added'], 0)),
					'href'			        => $this->url->link('octemplates/blog/oct_blogarticle', 'blogarticle_id=' . $result['blogarticle_id'])
				];
			}

			$data['oct_popup_view_status'] = $this->config->get('oct_popup_view_status');

			$data['products'] = [];

			$product_results = $this->model_octemplates_blog_oct_blogarticle->getArticleRelatedProduct($this->request->get['blogarticle_id']);

			$oct_product_stickers = [];

			if ($this->config->get('oct_stickers_status')) {
				$oct_stickers = $this->config->get('oct_stickers_data');

				$data['oct_sticker_you_save'] = false;

				if ($oct_stickers) {
					$data['oct_sticker_you_save'] = isset($oct_stickers['stickers']['special']['persent']) ? true : false;
				}

				$this->load->model('octemplates/stickers/oct_stickers');
			}

			foreach ($product_results as $result) {
				if (isset($oct_stickers) && $oct_stickers) {
					$oct_stickers_data = $this->model_octemplates_stickers_oct_stickers->getOCTStickers($result);

					$oct_product_stickers = [];

					if ($oct_stickers_data) {
						$oct_product_stickers = $oct_stickers_data['stickers'];
					}
				}

				if ($result['image']) {
					$image = $this->model_tool_image->resize($result['image'], $oct_blogsettings_data['product_width'], $oct_blogsettings_data['product_height']);
				} else {
					$image = $this->model_tool_image->resize('placeholder.png', $oct_blogsettings_data['product_width'], $oct_blogsettings_data['product_height']);
				}

				if ($this->customer->isLogged() || !$this->config->get('config_customer_price')) {
					$price = $this->currency->format($this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']);
				} else {
					$price = false;
				}

				if ((float)$result['special']) {
					$special = $this->currency->format($this->tax->calculate($result['special'], $result['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']);
				} else {
					$special = false;
				}

				if ($this->config->get('config_tax')) {
					$tax = $this->currency->format((float)$result['special'] ? $result['special'] : $result['price'], $this->session->data['currency']);
				} else {
					$tax = false;
				}

				if ($this->config->get('config_review_status')) {
					$rating = (int)$result['rating'];
				} else {
					$rating = false;
				}

				if ($result['quantity'] <= 0) {
					$stock = $result['stock_status'];
				} else {
					$stock = false;
				}

				$can_buy = true;

				if ($result['quantity'] <= 0 && !$this->config->get('config_stock_checkout')) {
					$can_buy = false;
				} elseif ($result['quantity'] <= 0 && $this->config->get('config_stock_checkout')) {
					$can_buy = true;
				}

				$oct_grayscale = ($this->config->get('theme_oct_promo_no_quantity_grayscale') && !$can_buy) ? true : false;

				$oct_atributes = false;

				$data['products'][] = [
					'product_id'  => $result['product_id'],
					'thumb'       => $image,
					'width'		  => $oct_blogsettings_data['product_width'],
					'height'	  => $oct_blogsettings_data['product_height'],
					'name'        => $result['name'],
					'oct_model'	  => $this->config->get('theme_oct_promo_data_model') ? $result['model'] : '',
					'description' => utf8_substr(trim(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8'))), 0, $this->config->get('theme_' . $this->config->get('config_theme') . '_product_description_length')) . '..',
					'price'       => $price,
					'special'     => $special,
					'stock'		  => $stock,
					'can_buy'     => $can_buy,
					'oct_grayscale'  => $oct_grayscale,
					'oct_atributes'  => $oct_atributes,
					'oct_stickers'  => $oct_product_stickers,
					'you_save'	  	=> $result['you_save'],
					'tax'         => $tax,
					'minimum'     => $result['minimum'] > 0 ? $result['minimum'] : 1,
					'rating'      => $rating,
					'reviews'	  => $result['reviews'],
					'quantity'	  => $result['quantity'] <= 0 ? 0 : $result['quantity'],
					'href'        => $this->url->link('product/product', 'product_id=' . $result['product_id']),
					'quantity_show' => $can_buy ? 1 : 0
				];
			}

			$data['products'] = $this->load->controller('octemplates/module/oct_products_modules', $data);

			$data['tags'] = [];

			if ($article_info['tag']) {
				$tags = explode(',', $article_info['tag']);

				foreach ($tags as $tag) {
					$data['tags'][] = [
						'tag'  => trim($tag),
						'href' => $this->url->link('octemplates/blog/oct_blogsearch', 'tag=' . trim($tag))
					];
				}
			}

			if (isset($oct_promo_data['open_graph']) && $oct_promo_data['open_graph']) {
				$site_link = $this->request->server['HTTPS'] ? HTTPS_SERVER : HTTP_SERVER;

				$config_logo = file_exists(DIR_IMAGE . $this->config->get('config_logo')) ? $this->config->get('config_logo') : 'catalog/opencart-logo.png';

				$oct_ogimage = $article_info['image'] ? $article_info['image'] : $config_logo;
				$blog_article_image = $site_link . 'image/' . $oct_ogimage;

				$image_info = getimagesize(DIR_IMAGE . $oct_ogimage);
				
				$image_width = 0;
				$image_height = 0;
				$mime_type = '';

				if ($image_info !== false) {
					$image_width = $image_info[0];
					$image_height = $image_info[1];
					$mime_type = isset($image_info['mime']) ? $image_info['mime'] : '';
				}

				$this->document->setOCTOpenGraph('og:title', htmlspecialchars(strip_tags(str_replace("\r", " ", str_replace("\n", " ", str_replace("\\", "/", str_replace("\"", "", (isset($oct_seo_title) && $oct_seo_title) ? $oct_seo_title : $article_info['meta_title'])))))));
				$this->document->setOCTOpenGraph('og:description', htmlspecialchars(strip_tags(str_replace("\r", " ", str_replace("\n", " ", str_replace("\\", "/", str_replace("\"", "", (isset($oct_seo_description) && $oct_seo_description && empty($article_info['meta_description'])) ? $oct_seo_description : $article_info['meta_description'])))))));
				$this->document->setOCTOpenGraph('og:site_name', htmlspecialchars(strip_tags(str_replace("\r", " ", str_replace("\n", " ", str_replace("\\", "/", str_replace("\"", "", $this->config->get('config_name'))))))));
				$this->document->setOCTOpenGraph('og:url', $this->url->link('octemplates/blog/oct_blogarticle', 'blogarticle_id=' . $this->request->get['blogarticle_id']));
				$this->document->setOCTOpenGraph('og:image', str_replace(" ", "%20", $blog_article_image));

				if ($mime_type) {
					$this->document->setOCTOpenGraph('og:image:type', $mime_type);
				}

				if ($image_width > 0) {
					$this->document->setOCTOpenGraph('og:image:width', $image_width);
				}

				if ($image_height > 0) {
					$this->document->setOCTOpenGraph('og:image:height', $image_height);
				}

				$this->document->setOCTOpenGraph('og:image:alt', htmlspecialchars(strip_tags(str_replace("\r", " ", str_replace("\n", " ", str_replace("\\", "/", str_replace("\"", "", $data['heading_title'])))))));
				$this->document->setOCTOpenGraph('og:type', 'article');
			}

			$this->model_octemplates_blog_oct_blogarticle->updateViewed($this->request->get['blogarticle_id']);

			$data['column_left'] = $this->load->controller('common/column_left');
			$data['column_right'] = $this->load->controller('common/column_right');
			$data['content_top'] = $this->load->controller('common/content_top');
			$data['content_bottom'] = $this->load->controller('common/content_bottom');
			$data['footer'] = $this->load->controller('common/footer');
			$data['header'] = $this->load->controller('common/header');

			$oct_data['breadcrumbs'] = $data['breadcrumbs'];

			$data['oct_breadcrumbs'] = $this->load->controller('octemplates/main/oct_functions/octBreadcrumbs', $oct_data);

			$logo_url = '';
			if ($this->config->get('config_logo') && is_file(DIR_IMAGE . $this->config->get('config_logo'))) {
				$server = $this->request->server['HTTPS'] ? $this->config->get('config_ssl') : $this->config->get('config_url');
				$logo_url = $server . 'image/' . $this->config->get('config_logo');
			}

			$data['article_microdata'] = $this->model_octemplates_microdata->generateArticleMicrodata($article_info, [
				'canonical' => $data['canonical'],
				'store_name' => $this->config->get('config_name'),
				'store_url' => $this->config->get('config_url'),
				'logo_url' => $logo_url,
				'language_code' => $this->language->get('code'),
				'date_modified' => $article_info['date_modified'],
				'thumb' => $data['thumb'] ?? null,
				'popup' => $data['popup'] ?? null,
				'image_width' => $data['image_width'] ?? 1200,
				'image_height' => $data['image_height'] ?? 900,
				'tags' => $data['tags'] ?? [],
				'reviews_total' => $data['comments_total'] ?? 0
			]);

			$this->response->setOutput($this->load->view('octemplates/blog/oct_blogarticle', $data));
		} else {
			$url = '';

			if (isset($this->request->get['blog_path'])) {
				$url .= '&blog_path=' . $this->request->get['blog_path'];
			}

			if (isset($this->request->get['search'])) {
				$url .= '&search=' . $this->request->get['search'];
			}

			if (isset($this->request->get['tag'])) {
				$url .= '&tag=' . $this->request->get['tag'];
			}

			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}

			if (isset($this->request->get['limit'])) {
				$url .= '&limit=' . $this->request->get['limit'];
			}

			$data['breadcrumbs'][] = [
				'text' => $this->language->get('text_error'),
				'href' => $this->url->link('octemplates/blog/oct_blogarticle', $url . '&blogarticle_id=' . $blogarticle_id)
			];

			$this->document->setTitle($this->language->get('text_error'));

			$data['continue'] = $this->url->link('common/home');

			$this->response->addHeader($this->request->server['SERVER_PROTOCOL'] . ' 404 Not Found');

			$data['column_left'] = $this->load->controller('common/column_left');
			$data['column_right'] = $this->load->controller('common/column_right');
			$data['content_top'] = $this->load->controller('common/content_top');
			$data['content_bottom'] = $this->load->controller('common/content_bottom');
			$data['footer'] = $this->load->controller('common/footer');
			$data['header'] = $this->load->controller('common/header');

			$oct_data['breadcrumbs'] = $data['breadcrumbs'];
			$data['oct_breadcrumbs'] = $this->load->controller('octemplates/main/oct_functions/octBreadcrumbs', $oct_data);

			$this->response->setOutput($this->load->view('error/not_found', $data));
		}
	}

	public function comment() {
		$this->load->language('octemplates/blog/oct_blogarticle');
		$this->load->model('octemplates/blog/oct_blogcomment');
	
		$page = isset($this->request->get['page']) ? (int)$this->request->get['page'] : 1;
		$data['comments'] = array();
	
		$blogarticle_id = (int)$this->request->get['blogarticle_id'];
		$data['blogarticle_id'] = $blogarticle_id;
	
		$total_comments = $this->model_octemplates_blog_oct_blogcomment->getTotalCommentsByArticleId($blogarticle_id);
		$limit  = 20;
		$offset = ($page - 1) * $limit;
	
		$results = $this->model_octemplates_blog_oct_blogcomment->getCommentsByArticleId($blogarticle_id, $offset, $limit);
		foreach ($results as $result) {
			$data['comments'][] = array(
				'author'     => $result['author'],
				'text'       => nl2br($result['text']),
				'admin_text' => nl2br($result['admin_text']),
				'date_added' => $this->load->controller('octemplates/main/oct_functions/OctDateTime', array($result['date_added'], 1))
			);
		}
	
		$has_more = ($offset + $limit < $total_comments);
		$data['has_more']  = $has_more;
		$data['next_page'] = $page + 1;
		$data['total_comments'] = $total_comments;
	
		$this->response->setOutput($this->load->view('octemplates/blog/oct_blogcomment', $data));
	}

	public function write() {
		$this->load->language('octemplates/blog/oct_blogarticle');

		$json = [];

		if ($this->request->server['REQUEST_METHOD'] == 'POST') {
			if ((utf8_strlen($this->request->post['name']) < 3) || (utf8_strlen($this->request->post['name']) > 25)) {
				$json['error']['name'] = $this->language->get('error_name');
			}

			if ((utf8_strlen($this->request->post['text']) < 25) || (utf8_strlen($this->request->post['text']) > 1000)) {
				$json['error']['text'] = $this->language->get('error_text');
			}

			if ($this->config->get('captcha_' . $this->config->get('config_captcha') . '_status') && in_array('review', (array)$this->config->get('config_captcha_page'))) {
				$captcha = $this->load->controller('extension/captcha/' . $this->config->get('config_captcha') . '/validate');

				if ($captcha) {
					$json['error']['captcha'] = $captcha;
				}
			}

			if (!isset($json['error'])) {
				$blogarticle_id = isset($this->request->get['blogarticle_id']) ? (int)$this->request->get['blogarticle_id'] : 0;

				if ($blogarticle_id > 0) {
					$this->load->model('octemplates/blog/oct_blogcomment');

					$this->model_octemplates_blog_oct_blogcomment->addComment($blogarticle_id, $this->request->post);

					$oct_sms_settings = $this->config->get('oct_sms_settings');
					$template_code = 'oct_blog_comment';
					$language_id = (int)$this->config->get('config_language_id');

					if (isset($oct_sms_settings['templates'][$template_code]['status']) && $oct_sms_settings['templates'][$template_code]['status']) {
						if (isset($oct_sms_settings['templates'][$template_code]['message'][$language_id]) && !empty($oct_sms_settings['templates'][$template_code]['message'][$language_id]) && isset($oct_sms_settings['templates'][$template_code]['edit_localization'])) {
							$sms_template = $oct_sms_settings['templates'][$template_code]['message'][$language_id];
						} else {
							$sms_template = $this->language->get('default_sms_template');
						}

						if (!empty($sms_template)) {
							$customer_name = '';
							if (isset($this->request->post['name'])) {
								$customer_name = strip_tags($this->request->post['name']);
								$customer_name = mb_substr($customer_name, 0, 50);
							}

							$replace = array(
								'[customer_name]' => $customer_name,
								'[post_link]' => $this->url->link('octemplates/blog/oct_blogarticle', 'blogarticle_id=' . $blogarticle_id),
								'[store]' => $this->config->get('config_name')
							);

							$sms_message = str_replace(array_keys($replace), array_values($replace), $sms_template);

							if (!empty($oct_sms_settings['admin_phone']) && !empty($oct_sms_settings['oct_sms_token'])) {
								$this->load->model('octemplates/module/oct_sms_notify');

								$this->model_octemplates_module_oct_sms_notify->sendNotification(array(
									'phone' => $oct_sms_settings['admin_phone'],
									'message' => $sms_message,
									'template_code' => $template_code,
									'access_token' => $oct_sms_settings['oct_sms_token']
								));
							}
						}
					}

					$json['success'] = $this->language->get('text_success');
				}
			}
		}

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