PageRenderTime 66ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/pav_foodstore/modules/modules/pavmegamenu/catalog/model/menu/widget.php

https://gitlab.com/12chakram/Bigkart
PHP | 459 lines | 313 code | 93 blank | 53 comment | 34 complexity | 75ca40d9f5a8774e2c3e15fb1d7e8635 MD5 | raw file
  1. <?php
  2. /******************************************************
  3. * @package Pav Megamenu module for Opencart 1.5.x
  4. * @version 2.0
  5. * @author http://www.pavothemes.com
  6. * @copyright Copyright (C) September 2013 PavoThemes.com <@emai:pavothemes@gmail.com>.All rights reserved.
  7. * @license GNU General Public License version 2
  8. *******************************************************/
  9. class ModelMenuWidget extends Model {
  10. private $widgets = array();
  11. /**
  12. * get list of supported widget types.
  13. */
  14. public function getTypes(){
  15. $this->language->load('module/pavmegamenu');
  16. return array(
  17. 'html' => $this->language->get( 'text_widget_html' ),
  18. 'product_category' => $this->language->get( 'text_widget_product_category' ),
  19. 'product_list' => $this->language->get( 'text_widget_product_list' ),
  20. 'product' => $this->language->get( 'text_widget_product' ),
  21. 'banner' => $this->language->get( 'text_widget_banner' ),
  22. 'image' => $this->language->get( 'text_widget_image' ),
  23. 'video_code' => $this->language->get( 'text_widget_video_code' ),
  24. 'feed' => $this->language->get( 'text_widget_feed' ),
  25. 'pavo_blogs' => $this->language->get( 'text_pavo_blogs' )
  26. );
  27. }
  28. /**
  29. * get list of widget rows.
  30. */
  31. public function getWidgets(){
  32. $sql = ' SELECT * FROM '.DB_PREFIX.'megamenu_widgets ';
  33. $query = $this->db->query( $sql );
  34. $a = $query->rows;
  35. return $a;
  36. }
  37. public function delete( $id ){
  38. $sql = ' DELETE FROM '.DB_PREFIX.'megamenu_widgets WHERE id='.(int)$id;
  39. return $this->db->query( $sql );
  40. }
  41. /**
  42. * get widget data row by id
  43. */
  44. public function getWidetById( $id ){
  45. $sql = ' SELECT * FROM '.DB_PREFIX.'megamenu_widgets WHERE id='.(int)$id;
  46. $query = $this->db->query( $sql );
  47. $row = $query->row;
  48. $output = array(
  49. 'id' => '',
  50. 'name' => '',
  51. 'params' => '',
  52. );
  53. if( $row ){
  54. $output = array_merge( $output, $row );
  55. $output['params'] = unserialize( $output['params'] );
  56. }
  57. return $output;
  58. }
  59. /**********/
  60. public function renderWidgetProductContent( $args, $setting, $widget_name = "" ){
  61. $output = '';
  62. $this->load->model('catalog/product');
  63. $this->load->model('tool/image');
  64. $t = array(
  65. 'product_id' => 0,
  66. 'image_height' => '320',
  67. 'image_width' => 300
  68. );
  69. $setting = array_merge( $t, $setting );
  70. $result = $this->model_catalog_product->getProduct( $setting['product_id'] );
  71. if ($result['image']) {
  72. $image = $this->model_tool_image->resize($result['image'], $setting['image_width'], $setting['image_height']);
  73. } else {
  74. $image = false;
  75. }
  76. if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
  77. $price = $this->currency->format($this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax')));
  78. } else {
  79. $price = false;
  80. }
  81. if ((float)$result['special']) {
  82. $special = $this->currency->format($this->tax->calculate($result['special'], $result['tax_class_id'], $this->config->get('config_tax')));
  83. } else {
  84. $special = false;
  85. }
  86. if ($this->config->get('config_review_status')) {
  87. $rating = $result['rating'];
  88. } else {
  89. $rating = false;
  90. }
  91. $product = array(
  92. 'product_id' => $result['product_id'],
  93. 'thumb' => $image,
  94. 'name' => $result['name'],
  95. 'price' => $price,
  96. 'special' => $special,
  97. 'rating' => $rating,
  98. 'reviews' => sprintf($this->language->get('text_reviews'), (int)$result['reviews']),
  99. 'href' => $this->url->link('product/product', 'product_id=' . $result['product_id']),
  100. );
  101. ob_start();
  102. require( DIR_TEMPLATE . $this->getLayout('product') );
  103. $output = ob_get_contents();
  104. ob_end_clean( );
  105. return $output;
  106. }
  107. /**
  108. *
  109. */
  110. public function renderWidgetFeedContent( $args, $setting, $widget_name= "" ){
  111. $t = array(
  112. 'limit' => 12,
  113. 'feed_url' => ''
  114. );
  115. $setting = array_merge( $t, $setting );
  116. $output = '';
  117. if( $setting['feed_url'] ) {
  118. $content = file_get_contents( $setting['feed_url'] );
  119. $x = new SimpleXmlElement($content);
  120. $items = $x->channel->item;
  121. ob_start();
  122. require( DIR_TEMPLATE . $this->getLayout('feed') );
  123. $output = ob_get_contents();
  124. ob_end_clean( );
  125. }
  126. return $output;
  127. }
  128. /**
  129. *
  130. */
  131. public function renderWidgetBannerContent( $args, $setting, $widget_name= "" ){
  132. $this->load->model('design/banner');
  133. $this->load->model('tool/image');
  134. $t = array(
  135. 'group_id'=> '',
  136. 'image_width' => 80,
  137. 'image_height' => 80,
  138. );
  139. $setting = array_merge( $t, $setting );
  140. $results = $this->model_design_banner->getBanner($setting['group_id']);
  141. $banners = array();
  142. foreach ($results as $result) {
  143. if (file_exists(DIR_IMAGE . $result['image'])) {
  144. $banners[] = array(
  145. 'title' => $result['title'],
  146. 'link' => $result['link'],
  147. 'image' => $this->model_tool_image->resize($result['image'], $setting['image_width'], $setting['image_height'])
  148. );
  149. }
  150. }
  151. ob_start();
  152. require( DIR_TEMPLATE . $this->getLayout('banner') );
  153. $output = ob_get_contents();
  154. ob_end_clean( );
  155. return $output;
  156. }
  157. /**
  158. *
  159. */
  160. public function renderWidgetImageContent( $args, $setting, $widget_name= "" ){
  161. $this->load->model('tool/image');
  162. $t = array(
  163. 'group_id'=> '',
  164. 'image_width' => 80,
  165. 'image_height' => 80,
  166. );
  167. $setting = array_merge( $t, $setting );
  168. $image = $this->model_tool_image->resize($setting['image_path'], $setting['image_width'], $setting['image_height']);
  169. ob_start();
  170. require( DIR_TEMPLATE . $this->getLayout('image') );
  171. $output = ob_get_contents();
  172. ob_end_clean( );
  173. return $output;
  174. }
  175. /**
  176. *
  177. */
  178. public function renderWidgetProduct_categoryContent( $args, $setting, $widget_name= "" ){
  179. $this->load->model('catalog/product');
  180. $t = array(
  181. 'category_id'=> '',
  182. 'limit' => '12',
  183. 'image_width'=>'200',
  184. 'image_height' =>'200',
  185. );
  186. $setting = array_merge( $t, $setting );
  187. $data = array(
  188. 'filter_category_id'=> $setting['category_id'],
  189. 'sort' => 'p.date_added',
  190. 'order' => 'DESC',
  191. 'start' => 0,
  192. 'limit' => $setting['limit']
  193. );
  194. $results = $this->model_catalog_product->getProducts( $data );
  195. $products = $this->buildDataProduct( $results, $setting );
  196. ob_start();
  197. require( DIR_TEMPLATE . $this->getLayout('product_list') );
  198. $output = ob_get_contents();
  199. ob_end_clean( );
  200. return $output;
  201. }
  202. /**
  203. *
  204. */
  205. public function renderWidgetHtmlContent( $args, $setting, $widget_name= "" ){
  206. $t = array(
  207. 'name'=> '',
  208. 'html' => '',
  209. );
  210. $setting = array_merge( $t, $setting );
  211. $html = '';
  212. $languageID = $this->config->get('config_language_id');
  213. if( is_array($setting['html']) && isset($setting['html'][$languageID]) ){
  214. $html = $setting['html'][$languageID];
  215. $html = html_entity_decode($html, ENT_QUOTES, 'UTF-8');
  216. }
  217. ob_start();
  218. require( DIR_TEMPLATE . $this->getLayout('html') );
  219. $output = ob_get_contents();
  220. ob_end_clean( );
  221. return $output;
  222. }
  223. /**
  224. *
  225. */
  226. public function renderWidgetVideo_codeContent( $args, $setting, $widget_name= "" ){
  227. $t = array(
  228. 'name'=> '',
  229. 'video_code' => '',
  230. );
  231. $setting = array_merge( $t, $setting );
  232. $html = $setting['video_code'];
  233. $html = html_entity_decode($html, ENT_QUOTES, 'UTF-8');
  234. ob_start();
  235. require( DIR_TEMPLATE . $this->getLayout('html') );
  236. $output = ob_get_contents();
  237. ob_end_clean( );
  238. return $output;
  239. }
  240. /**
  241. *
  242. */
  243. public function renderWidgetProduct_listContent( $args, $setting, $widget_name= "" ){
  244. $t = array(
  245. 'list_type'=> '',
  246. 'limit' => 12,
  247. 'image_width'=>'200',
  248. 'image_height' =>'200',
  249. );
  250. $this->load->model('catalog/product');
  251. $this->load->model('tool/image');
  252. $products = array();
  253. $setting = array_merge( $t, $setting );
  254. if( $setting['list_type'] == 'bestseller' ){
  255. $results = $this->model_catalog_product->getBestSellerProducts($setting['limit']);
  256. }else if( $setting['list_type'] == 'special' ){
  257. $data = array(
  258. 'sort' => 'pd.name',
  259. 'order' => 'ASC',
  260. 'start' => 0,
  261. 'limit' => $setting['limit']
  262. );
  263. $results = $this->model_catalog_product->getProductSpecials($data);
  264. }else {
  265. $data = array(
  266. 'sort' => 'p.date_added',
  267. 'order' => 'DESC',
  268. 'start' => 0,
  269. 'limit' => $setting['limit']
  270. );
  271. $results = $this->model_catalog_product->getProducts( $data );
  272. }
  273. $products = $this->buildDataProduct( $results, $setting );
  274. ob_start();
  275. require( DIR_TEMPLATE . $this->getLayout('product_list') );
  276. $output = ob_get_contents();
  277. ob_end_clean( );
  278. return $output;
  279. }
  280. /**
  281. *
  282. */
  283. private function buildDataProduct( $results, $setting ){
  284. $this->load->model('tool/image');
  285. $products = array();
  286. foreach ($results as $result) {
  287. if ($result['image']) {
  288. $image = $this->model_tool_image->resize($result['image'], $setting['image_width'], $setting['image_height']);
  289. } else {
  290. $image = false;
  291. }
  292. if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
  293. $price = $this->currency->format($this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax')));
  294. } else {
  295. $price = false;
  296. }
  297. if ((float)$result['special']) {
  298. $special = $this->currency->format($this->tax->calculate($result['special'], $result['tax_class_id'], $this->config->get('config_tax')));
  299. } else {
  300. $special = false;
  301. }
  302. if ($this->config->get('config_review_status')) {
  303. $rating = $result['rating'];
  304. } else {
  305. $rating = false;
  306. }
  307. $products[] = array(
  308. 'product_id' => $result['product_id'],
  309. 'thumb' => $image,
  310. 'name' => $result['name'],
  311. 'price' => $price,
  312. 'special' => $special,
  313. 'rating' => $rating,
  314. 'reviews' => sprintf($this->language->get('text_reviews'), (int)$result['reviews']),
  315. 'href' => $this->url->link('product/product', 'product_id=' . $result['product_id']),
  316. );
  317. }
  318. return $products;
  319. }
  320. /**
  321. *
  322. */
  323. protected function getLayout( $layout ){
  324. if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/pavmegamenu/'.$layout.'.tpl')) {
  325. $template = $this->config->get('config_template') . '/template/module/pavmegamenu/'.$layout.'.tpl';
  326. } else {
  327. $template = 'default/template/module/pavmegamenu/'.$layout.'.tpl';
  328. }
  329. return $template;
  330. }
  331. /**
  332. *
  333. */
  334. public function getWidgetContent( $type, $data, $widget_name = ""){
  335. $this->language->load('module/pavmegamenu');
  336. $method = "renderWidget".ucfirst($type).'Content';
  337. $args = array();
  338. if( method_exists( $this, $method ) ){
  339. return $this->{$method}( $args, $data, $widget_name );
  340. }
  341. return ;
  342. }
  343. /**
  344. *
  345. */
  346. public function renderContent( $id ){
  347. $output = '<div class="pavo-widget" id="wid-'.$id.'">';
  348. if( isset($this->widgets[$id]) ){
  349. $output .= $this->getWidgetContent( $this->widgets[$id]['type'], unserialize($this->widgets[$id]['params']), $this->widgets[$id]['name'] );
  350. }
  351. $output .= '</div>';
  352. return $output;
  353. }
  354. /**
  355. *
  356. */
  357. public function loadWidgets(){
  358. if( empty($this->widgets) ){
  359. $widgets = $this->getWidgets();
  360. foreach( $widgets as $widget ){
  361. $this->widgets[$widget['id']] =$widget;
  362. }
  363. }
  364. }
  365. }
  366. ?>