PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/catalog/controller/product/product.php

https://bitbucket.org/jjasko/opencart_serbian
PHP | 570 lines | 441 code | 128 blank | 1 comment | 106 complexity | 24cd7363c23271162f6963df08f17618 MD5 | raw file
  1. <?php
  2. class ControllerProductProduct extends Controller {
  3. private $error = array();
  4. public function index() {
  5. $this->language->load('product/product');
  6. $this->data['breadcrumbs'] = array();
  7. $this->data['breadcrumbs'][] = array(
  8. 'text' => $this->language->get('text_home'),
  9. 'href' => $this->url->link('common/home'),
  10. 'separator' => false
  11. );
  12. $this->load->model('catalog/category');
  13. if (isset($this->request->get['path'])) {
  14. $path = '';
  15. foreach (explode('_', $this->request->get['path']) as $path_id) {
  16. if (!$path) {
  17. $path = $path_id;
  18. } else {
  19. $path .= '_' . $path_id;
  20. }
  21. $category_info = $this->model_catalog_category->getCategory($path_id);
  22. if ($category_info) {
  23. $this->data['breadcrumbs'][] = array(
  24. 'text' => $category_info['name'],
  25. 'href' => $this->url->link('product/category', 'path=' . $path),
  26. 'separator' => $this->language->get('text_separator')
  27. );
  28. }
  29. }
  30. }
  31. $this->load->model('catalog/manufacturer');
  32. if (isset($this->request->get['manufacturer_id'])) {
  33. $this->data['breadcrumbs'][] = array(
  34. 'text' => $this->language->get('text_brand'),
  35. 'href' => $this->url->link('product/manufacturer'),
  36. 'separator' => $this->language->get('text_separator')
  37. );
  38. $manufacturer_info = $this->model_catalog_manufacturer->getManufacturer($this->request->get['manufacturer_id']);
  39. if ($manufacturer_info) {
  40. $this->data['breadcrumbs'][] = array(
  41. 'text' => $manufacturer_info['name'],
  42. 'href' => $this->url->link('product/manufacturer/product', 'manufacturer_id=' . $this->request->get['manufacturer_id']),
  43. 'separator' => $this->language->get('text_separator')
  44. );
  45. }
  46. }
  47. if (isset($this->request->get['filter_name']) || isset($this->request->get['filter_tag'])) {
  48. $url = '';
  49. if (isset($this->request->get['filter_name'])) {
  50. $url .= '&filter_name=' . $this->request->get['filter_name'];
  51. }
  52. if (isset($this->request->get['filter_tag'])) {
  53. $url .= '&filter_tag=' . $this->request->get['filter_tag'];
  54. }
  55. if (isset($this->request->get['filter_description'])) {
  56. $url .= '&filter_description=' . $this->request->get['filter_description'];
  57. }
  58. if (isset($this->request->get['filter_category_id'])) {
  59. $url .= '&filter_category_id=' . $this->request->get['filter_category_id'];
  60. }
  61. $this->data['breadcrumbs'][] = array(
  62. 'text' => $this->language->get('text_search'),
  63. 'href' => $this->url->link('product/search', $url),
  64. 'separator' => $this->language->get('text_separator')
  65. );
  66. }
  67. if (isset($this->request->get['product_id'])) {
  68. $product_id = $this->request->get['product_id'];
  69. } else {
  70. $product_id = 0;
  71. }
  72. $this->load->model('catalog/product');
  73. $product_info = $this->model_catalog_product->getProduct($product_id);
  74. $this->data['product_info'] = $product_info;
  75. if ($product_info) {
  76. $url = '';
  77. if (isset($this->request->get['path'])) {
  78. $url .= '&path=' . $this->request->get['path'];
  79. }
  80. if (isset($this->request->get['manufacturer_id'])) {
  81. $url .= '&manufacturer_id=' . $this->request->get['manufacturer_id'];
  82. }
  83. if (isset($this->request->get['filter_name'])) {
  84. $url .= '&filter_name=' . $this->request->get['filter_name'];
  85. }
  86. if (isset($this->request->get['filter_tag'])) {
  87. $url .= '&filter_tag=' . $this->request->get['filter_tag'];
  88. }
  89. if (isset($this->request->get['filter_description'])) {
  90. $url .= '&filter_description=' . $this->request->get['filter_description'];
  91. }
  92. if (isset($this->request->get['filter_category_id'])) {
  93. $url .= '&filter_category_id=' . $this->request->get['filter_category_id'];
  94. }
  95. $this->data['breadcrumbs'][] = array(
  96. 'text' => $product_info['name'],
  97. 'href' => $this->url->link('product/product', $url . '&product_id=' . $this->request->get['product_id']),
  98. 'separator' => $this->language->get('text_separator')
  99. );
  100. $this->document->setTitle($product_info['name']);
  101. $this->document->setDescription($product_info['meta_description']);
  102. $this->document->setKeywords($product_info['meta_keyword']);
  103. $this->document->addLink($this->url->link('product/product', 'product_id=' . $this->request->get['product_id']), 'canonical');
  104. $this->data['heading_title'] = $product_info['name'];
  105. $this->data['text_select'] = $this->language->get('text_select');
  106. $this->data['text_manufacturer'] = $this->language->get('text_manufacturer');
  107. $this->data['text_model'] = $this->language->get('text_model');
  108. $this->data['text_reward'] = $this->language->get('text_reward');
  109. $this->data['text_points'] = $this->language->get('text_points');
  110. $this->data['text_discount'] = $this->language->get('text_discount');
  111. $this->data['text_stock'] = $this->language->get('text_stock');
  112. $this->data['text_price'] = $this->language->get('text_price');
  113. $this->data['text_tax'] = $this->language->get('text_tax');
  114. $this->data['text_discount'] = $this->language->get('text_discount');
  115. $this->data['text_option'] = $this->language->get('text_option');
  116. $this->data['text_qty'] = $this->language->get('text_qty');
  117. $this->data['text_minimum'] = sprintf($this->language->get('text_minimum'), $product_info['minimum']);
  118. $this->data['text_or'] = $this->language->get('text_or');
  119. $this->data['text_write'] = $this->language->get('text_write');
  120. $this->data['text_note'] = $this->language->get('text_note');
  121. $this->data['text_share'] = $this->language->get('text_share');
  122. $this->data['text_wait'] = $this->language->get('text_wait');
  123. $this->data['text_tags'] = $this->language->get('text_tags');
  124. $this->data['entry_name'] = $this->language->get('entry_name');
  125. $this->data['entry_review'] = $this->language->get('entry_review');
  126. $this->data['entry_rating'] = $this->language->get('entry_rating');
  127. $this->data['entry_good'] = $this->language->get('entry_good');
  128. $this->data['entry_bad'] = $this->language->get('entry_bad');
  129. $this->data['entry_captcha'] = $this->language->get('entry_captcha');
  130. $this->data['button_cart'] = $this->language->get('button_cart');
  131. $this->data['button_wishlist'] = $this->language->get('button_wishlist');
  132. $this->data['button_compare'] = $this->language->get('button_compare');
  133. $this->data['button_upload'] = $this->language->get('button_upload');
  134. $this->data['button_continue'] = $this->language->get('button_continue');
  135. $this->load->model('catalog/review');
  136. $this->data['tab_description'] = $this->language->get('tab_description');
  137. $this->data['tab_attribute'] = $this->language->get('tab_attribute');
  138. $this->data['tab_review'] = sprintf($this->language->get('tab_review'), $this->model_catalog_review->getTotalReviewsByProductId($this->request->get['product_id']));
  139. $this->data['tab_related'] = $this->language->get('tab_related');
  140. $this->data['product_id'] = $this->request->get['product_id'];
  141. $this->data['manufacturer'] = $product_info['manufacturer'];
  142. $this->data['manufacturers'] = $this->url->link('product/manufacturer/product', 'manufacturer_id=' . $product_info['manufacturer_id']);
  143. $this->data['model'] = $product_info['model'];
  144. $this->data['reward'] = $product_info['reward'];
  145. $this->data['points'] = $product_info['points'];
  146. if ($product_info['quantity'] <= 0) {
  147. $this->data['stock'] = $product_info['stock_status'];
  148. } elseif ($this->config->get('config_stock_display')) {
  149. $this->data['stock'] = $product_info['quantity'];
  150. } else {
  151. $this->data['stock'] = $this->language->get('text_instock');
  152. }
  153. $this->load->model('tool/image');
  154. if ($product_info['image']) {
  155. $this->data['popup'] = $this->model_tool_image->resize($product_info['image'], $this->config->get('config_image_popup_width'), $this->config->get('config_image_popup_height'));
  156. } else {
  157. $this->data['popup'] = '';
  158. }
  159. if ($product_info['image']) {
  160. $this->data['thumb'] = $this->model_tool_image->resize($product_info['image'], $this->config->get('config_image_thumb_width'), $this->config->get('config_image_thumb_height'));
  161. } else {
  162. $this->data['thumb'] = '';
  163. }
  164. $this->data['images'] = array();
  165. $results = $this->model_catalog_product->getProductImages($this->request->get['product_id']);
  166. foreach ($results as $result) {
  167. $this->data['images'][] = array(
  168. 'popup' => $this->model_tool_image->resize($result['image'], $this->config->get('config_image_popup_width'), $this->config->get('config_image_popup_height')),
  169. 'thumb' => $this->model_tool_image->resize($result['image'], $this->config->get('config_image_additional_width'), $this->config->get('config_image_additional_height'))
  170. );
  171. }
  172. if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
  173. $this->data['price'] = $this->currency->format($this->tax->calculate($product_info['price'], $product_info['tax_class_id'], $this->config->get('config_tax')));
  174. } else {
  175. $this->data['price'] = false;
  176. }
  177. if ((float)$product_info['special']) {
  178. $this->data['special'] = $this->currency->format($this->tax->calculate($product_info['special'], $product_info['tax_class_id'], $this->config->get('config_tax')));
  179. } else {
  180. $this->data['special'] = false;
  181. }
  182. if ($this->config->get('config_tax')) {
  183. $this->data['tax'] = $this->currency->format((float)$product_info['special'] ? $product_info['special'] : $product_info['price']);
  184. } else {
  185. $this->data['tax'] = false;
  186. }
  187. $discounts = $this->model_catalog_product->getProductDiscounts($this->request->get['product_id']);
  188. $this->data['discounts'] = array();
  189. foreach ($discounts as $discount) {
  190. $this->data['discounts'][] = array(
  191. 'quantity' => $discount['quantity'],
  192. 'price' => $this->currency->format($this->tax->calculate($discount['price'], $product_info['tax_class_id'], $this->config->get('config_tax')))
  193. );
  194. }
  195. $this->data['options'] = array();
  196. foreach ($this->model_catalog_product->getProductOptions($this->request->get['product_id']) as $option) {
  197. if ($option['type'] == 'select' || $option['type'] == 'radio' || $option['type'] == 'checkbox' || $option['type'] == 'image') {
  198. $option_value_data = array();
  199. foreach ($option['option_value'] as $option_value) {
  200. if (!$option_value['subtract'] || ($option_value['quantity'] > 0)) {
  201. $option_value_data[] = array(
  202. 'product_option_value_id' => $option_value['product_option_value_id'],
  203. 'option_value_id' => $option_value['option_value_id'],
  204. 'name' => $option_value['name'],
  205. 'image' => $this->model_tool_image->resize($option_value['image'], 50, 50),
  206. 'price' => (float)$option_value['price'] ? $this->currency->format($this->tax->calculate($option_value['price'], $product_info['tax_class_id'], $this->config->get('config_tax'))) : false,
  207. 'price_prefix' => $option_value['price_prefix']
  208. );
  209. }
  210. }
  211. $this->data['options'][] = array(
  212. 'product_option_id' => $option['product_option_id'],
  213. 'option_id' => $option['option_id'],
  214. 'name' => $option['name'],
  215. 'type' => $option['type'],
  216. 'option_value' => $option_value_data,
  217. 'required' => $option['required']
  218. );
  219. } elseif ($option['type'] == 'text' || $option['type'] == 'textarea' || $option['type'] == 'file' || $option['type'] == 'date' || $option['type'] == 'datetime' || $option['type'] == 'time') {
  220. $this->data['options'][] = array(
  221. 'product_option_id' => $option['product_option_id'],
  222. 'option_id' => $option['option_id'],
  223. 'name' => $option['name'],
  224. 'type' => $option['type'],
  225. 'option_value' => $option['option_value'],
  226. 'required' => $option['required']
  227. );
  228. }
  229. }
  230. if ($product_info['minimum']) {
  231. $this->data['minimum'] = $product_info['minimum'];
  232. } else {
  233. $this->data['minimum'] = 1;
  234. }
  235. $this->data['review_status'] = $this->config->get('config_review_status');
  236. $this->data['reviews'] = sprintf($this->language->get('text_reviews'), (int)$product_info['reviews']);
  237. $this->data['rating'] = (int)$product_info['rating'];
  238. $this->data['description'] = html_entity_decode($product_info['description'], ENT_QUOTES, 'UTF-8');
  239. $this->data['attribute_groups'] = $this->model_catalog_product->getProductAttributes($this->request->get['product_id']);
  240. $this->data['products'] = array();
  241. $results = $this->model_catalog_product->getProductRelated($this->request->get['product_id']);
  242. foreach ($results as $result) {
  243. if ($result['image']) {
  244. $image = $this->model_tool_image->resize($result['image'], $this->config->get('config_image_related_width'), $this->config->get('config_image_related_height'));
  245. } else {
  246. $image = false;
  247. }
  248. if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
  249. $price = $this->currency->format($this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax')));
  250. } else {
  251. $price = false;
  252. }
  253. if ((float)$result['special']) {
  254. $special = $this->currency->format($this->tax->calculate($result['special'], $result['tax_class_id'], $this->config->get('config_tax')));
  255. } else {
  256. $special = false;
  257. }
  258. if ($this->config->get('config_review_status')) {
  259. $rating = (int)$result['rating'];
  260. } else {
  261. $rating = false;
  262. }
  263. $this->data['products'][] = array(
  264. 'product_id' => $result['product_id'],
  265. 'thumb' => $image,
  266. 'name' => $result['name'],
  267. 'price' => $price,
  268. 'special' => $special,
  269. 'rating' => $rating,
  270. 'reviews' => sprintf($this->language->get('text_reviews'), (int)$result['reviews']),
  271. 'href' => $this->url->link('product/product', 'product_id=' . $result['product_id']),
  272. );
  273. }
  274. $this->data['tags'] = array();
  275. $results = $this->model_catalog_product->getProductTags($this->request->get['product_id']);
  276. foreach ($results as $result) {
  277. $this->data['tags'][] = array(
  278. 'tag' => $result['tag'],
  279. 'href' => $this->url->link('product/search', 'filter_tag=' . $result['tag'])
  280. );
  281. }
  282. $this->model_catalog_product->updateViewed($this->request->get['product_id']);
  283. if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/product/product.tpl')) {
  284. $this->template = $this->config->get('config_template') . '/template/product/product.tpl';
  285. } else {
  286. $this->template = 'default/template/product/product.tpl';
  287. }
  288. $this->children = array(
  289. 'common/column_left',
  290. 'common/column_right',
  291. 'common/content_top',
  292. 'common/content_bottom',
  293. 'common/footer',
  294. 'common/header'
  295. );
  296. $this->response->setOutput($this->render());
  297. } else {
  298. $url = '';
  299. if (isset($this->request->get['path'])) {
  300. $url .= '&path=' . $this->request->get['path'];
  301. }
  302. if (isset($this->request->get['manufacturer_id'])) {
  303. $url .= '&manufacturer_id=' . $this->request->get['manufacturer_id'];
  304. }
  305. if (isset($this->request->get['filter_name'])) {
  306. $url .= '&filter_name=' . $this->request->get['filter_name'];
  307. }
  308. if (isset($this->request->get['filter_tag'])) {
  309. $url .= '&filter_tag=' . $this->request->get['filter_tag'];
  310. }
  311. if (isset($this->request->get['filter_description'])) {
  312. $url .= '&filter_description=' . $this->request->get['filter_description'];
  313. }
  314. if (isset($this->request->get['filter_category_id'])) {
  315. $url .= '&filter_category_id=' . $this->request->get['filter_category_id'];
  316. }
  317. $this->data['breadcrumbs'][] = array(
  318. 'text' => $this->language->get('text_error'),
  319. 'href' => $this->url->link('product/product', $url . '&product_id=' . $product_id),
  320. 'separator' => $this->language->get('text_separator')
  321. );
  322. $this->document->setTitle($this->language->get('text_error'));
  323. $this->data['heading_title'] = $this->language->get('text_error');
  324. $this->data['text_error'] = $this->language->get('text_error');
  325. $this->data['button_continue'] = $this->language->get('button_continue');
  326. $this->data['continue'] = $this->url->link('common/home');
  327. if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/error/not_found.tpl')) {
  328. $this->template = $this->config->get('config_template') . '/template/error/not_found.tpl';
  329. } else {
  330. $this->template = 'default/template/error/not_found.tpl';
  331. }
  332. $this->children = array(
  333. 'common/column_left',
  334. 'common/column_right',
  335. 'common/content_top',
  336. 'common/content_bottom',
  337. 'common/footer',
  338. 'common/header'
  339. );
  340. $this->response->setOutput($this->render());
  341. }
  342. }
  343. public function review() {
  344. $this->language->load('product/product');
  345. $this->load->model('catalog/review');
  346. $this->data['text_no_reviews'] = $this->language->get('text_no_reviews');
  347. if (isset($this->request->get['page'])) {
  348. $page = $this->request->get['page'];
  349. } else {
  350. $page = 1;
  351. }
  352. $this->data['reviews'] = array();
  353. $review_total = $this->model_catalog_review->getTotalReviewsByProductId($this->request->get['product_id']);
  354. $results = $this->model_catalog_review->getReviewsByProductId($this->request->get['product_id'], ($page - 1) * 5, 5);
  355. foreach ($results as $result) {
  356. $this->data['reviews'][] = array(
  357. 'author' => $result['author'],
  358. 'text' => strip_tags($result['text']),
  359. 'rating' => (int)$result['rating'],
  360. 'reviews' => sprintf($this->language->get('text_reviews'), (int)$review_total),
  361. 'date_added' => date($this->language->get('date_format_short'), strtotime($result['date_added']))
  362. );
  363. }
  364. $pagination = new Pagination();
  365. $pagination->total = $review_total;
  366. $pagination->page = $page;
  367. $pagination->limit = 5;
  368. $pagination->text = $this->language->get('text_pagination');
  369. $pagination->url = $this->url->link('product/product/review', 'product_id=' . $this->request->get['product_id'] . '&page={page}');
  370. $this->data['pagination'] = $pagination->render();
  371. if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/product/review.tpl')) {
  372. $this->template = $this->config->get('config_template') . '/template/product/review.tpl';
  373. } else {
  374. $this->template = 'default/template/product/review.tpl';
  375. }
  376. $this->response->setOutput($this->render());
  377. }
  378. public function write() {
  379. $this->language->load('product/product');
  380. $this->load->model('catalog/review');
  381. $json = array();
  382. if ((utf8_strlen($this->request->post['name']) < 3) || (utf8_strlen($this->request->post['name']) > 25)) {
  383. $json['error'] = $this->language->get('error_name');
  384. }
  385. if ((utf8_strlen($this->request->post['text']) < 25) || (utf8_strlen($this->request->post['text']) > 1000)) {
  386. $json['error'] = $this->language->get('error_text');
  387. }
  388. if (!$this->request->post['rating']) {
  389. $json['error'] = $this->language->get('error_rating');
  390. }
  391. if (!isset($this->session->data['captcha']) || ($this->session->data['captcha'] != $this->request->post['captcha'])) {
  392. $json['error'] = $this->language->get('error_captcha');
  393. }
  394. if (($this->request->server['REQUEST_METHOD'] == 'POST') && !isset($json['error'])) {
  395. $this->model_catalog_review->addReview($this->request->get['product_id'], $this->request->post);
  396. $json['success'] = $this->language->get('text_success');
  397. }
  398. $this->response->setOutput(json_encode($json));
  399. }
  400. public function captcha() {
  401. $this->load->library('captcha');
  402. $captcha = new Captcha();
  403. $this->session->data['captcha'] = $captcha->getCode();
  404. $captcha->showImage();
  405. }
  406. public function upload() {
  407. $this->language->load('product/product');
  408. $json = array();
  409. if (!empty($this->request->files['file']['name'])) {
  410. $filename = basename(html_entity_decode($this->request->files['file']['name'], ENT_QUOTES, 'UTF-8'));
  411. if ((strlen($filename) < 3) || (strlen($filename) > 128)) {
  412. $json['error'] = $this->language->get('error_filename');
  413. }
  414. $allowed = array();
  415. $filetypes = explode(',', $this->config->get('config_upload_allowed'));
  416. foreach ($filetypes as $filetype) {
  417. $allowed[] = trim($filetype);
  418. }
  419. if (!in_array(substr(strrchr($filename, '.'), 1), $allowed)) {
  420. $json['error'] = $this->language->get('error_filetype');
  421. }
  422. if ($this->request->files['file']['error'] != UPLOAD_ERR_OK) {
  423. $json['error'] = $this->language->get('error_upload_' . $this->request->files['file']['error']);
  424. }
  425. } else {
  426. $json['error'] = $this->language->get('error_upload');
  427. }
  428. if (($this->request->server['REQUEST_METHOD'] == 'POST') && !isset($json['error'])) {
  429. if (is_uploaded_file($this->request->files['file']['tmp_name']) && file_exists($this->request->files['file']['tmp_name'])) {
  430. $file = basename($filename) . '.' . md5(rand());
  431. // Hide the uploaded file name sop people can not link to it directly.
  432. $this->load->library('encryption');
  433. $encryption = new Encryption($this->config->get('config_encryption'));
  434. $json['file'] = $encryption->encrypt($file);
  435. move_uploaded_file($this->request->files['file']['tmp_name'], DIR_DOWNLOAD . $file);
  436. }
  437. $json['success'] = $this->language->get('text_upload');
  438. }
  439. $this->response->setOutput(json_encode($json));
  440. }
  441. }
  442. ?>