PageRenderTime 42ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/leoblog/controllers/front/category.php

https://gitlab.com/ptisky/API_prestashop
PHP | 248 lines | 188 code | 37 blank | 23 comment | 17 complexity | 5d101a32c602d58e10ace45885ec8335 MD5 | raw file
  1. <?php
  2. /**
  3. * Leo Prestashop Blockleoblogs for Prestashop 1.6.x
  4. *
  5. * @package blockleoblogs
  6. * @version 3.0
  7. * @author http://www.leotheme.com
  8. * @copyright Copyright (C) October 2013 LeoThemes.com <@emai:leotheme@gmail.com>
  9. * <info@leotheme.com>.All rights reserved.
  10. * @license GNU General Public License version 2
  11. */
  12. include_once(_PS_MODULE_DIR_.'leoblog/loader.php');
  13. class LeoblogcategoryModuleFrontController extends ModuleFrontController
  14. {
  15. public $php_self;
  16. protected $template_path = '';
  17. public function __construct()
  18. {
  19. parent::__construct();
  20. $this->context = Context::getContext();
  21. $this->template_path = _PS_MODULE_DIR_.'leoblog/views/templates/front/';
  22. }
  23. /**
  24. * @see FrontController::initContent()
  25. */
  26. public function initContent()
  27. {
  28. $config = LeoBlogConfig::getInstance();
  29. /* Load Css and JS File */
  30. LeoBlogHelper::loadMedia($this->context, $this);
  31. $this->php_self = 'category';
  32. parent::initContent();
  33. $id_category = (int)Tools::getValue('id');
  34. $helper = LeoBlogHelper::getInstance();
  35. $limit_leading_blogs = (int)$config->get('listing_leading_limit_items', 1);
  36. $limit_secondary_blogs = (int)$config->get('listing_secondary_limit_items', 6);
  37. $limit = (int)$limit_leading_blogs + (int)$limit_secondary_blogs;
  38. $n = $limit;
  39. $p = abs((int)(Tools::getValue('p', 1)));
  40. $category = new Leoblogcat($id_category, $this->context->language->id);
  41. $template = isset($category->template) && $category->template ? $category->template : $config->get('template', 'default');
  42. if ($category->id_leoblogcat && $category->active)
  43. {
  44. // $_GET['rewrite'] = $category->link_rewrite;
  45. $this->template_path .= $template.'/';
  46. if ($category->image)
  47. {
  48. # validate module
  49. $category->image = _LEOBLOG_BLOG_IMG_URI_.'c/'.$category->image;
  50. }
  51. $blogs = LeoBlogBlog::getListBlogs($id_category, $this->context->language->id, $p, $limit, 'id_leoblog_blog', 'DESC', array(), true);
  52. $count = LeoBlogBlog::countBlogs($id_category, $this->context->language->id, true);
  53. $authors = array();
  54. $leading_blogs = array();
  55. $secondary_blogs = array();
  56. // $links = array();
  57. if (count($blogs))
  58. {
  59. $leading_blogs = array_slice($blogs, 0, $limit_leading_blogs);
  60. $secondary_blogs = array_splice($blogs, $limit_leading_blogs, count($blogs));
  61. }
  62. $image_w = (int)$config->get('listing_leading_img_width', 690);
  63. $image_h = (int)$config->get('listing_leading_img_height', 300);
  64. foreach ($leading_blogs as $key => $blog)
  65. {
  66. $blog = LeoBlogHelper::buildBlog($helper, $blog, $image_w, $image_h, $config);
  67. if ($blog['id_employee'])
  68. {
  69. if (!isset($authors[$blog['id_employee']]))
  70. {
  71. # validate module
  72. $authors[$blog['id_employee']] = new Employee($blog['id_employee']);
  73. }
  74. $blog['author'] = $authors[$blog['id_employee']]->firstname.' '.$authors[$blog['id_employee']]->lastname;
  75. $blog['author_link'] = $helper->getBlogAuthorLink($authors[$blog['id_employee']]->id);
  76. }
  77. else
  78. {
  79. $blog['author'] = '';
  80. $blog['author_link'] = '';
  81. }
  82. $leading_blogs[$key] = $blog;
  83. }
  84. $image_w = (int)$config->get('listing_secondary_img_width', 390);
  85. $image_h = (int)$config->get('listing_secondary_img_height', 200);
  86. foreach ($secondary_blogs as $key => $blog)
  87. {
  88. $blog = LeoBlogHelper::buildBlog($helper, $blog, $image_w, $image_h, $config);
  89. if ($blog['id_employee'])
  90. {
  91. if (!isset($authors[$blog['id_employee']]))
  92. {
  93. # validate module
  94. $authors[$blog['id_employee']] = new Employee($blog['id_employee']);
  95. }
  96. $blog['author'] = $authors[$blog['id_employee']]->firstname.' '.$authors[$blog['id_employee']]->lastname;
  97. $blog['author_link'] = $helper->getBlogAuthorLink($authors[$blog['id_employee']]->id);
  98. }
  99. else
  100. {
  101. $blog['author'] = '';
  102. $blog['author_link'] = '';
  103. }
  104. $secondary_blogs[$key] = $blog;
  105. }
  106. $nb_blogs = $count;
  107. $range = 2; /* how many pages around page selected */
  108. if ($p > (($nb_blogs / $n) + 1))
  109. Tools::redirect(preg_replace('/[&?]p=\d+/', '', $_SERVER['REQUEST_URI']));
  110. $pages_nb = ceil($nb_blogs / (int)($n));
  111. $start = (int)($p - $range);
  112. if ($start < 1)
  113. $start = 1;
  114. $stop = (int)($p + $range);
  115. if ($stop > $pages_nb)
  116. $stop = (int)($pages_nb);
  117. $params = array(
  118. 'rewrite' => $category->link_rewrite,
  119. 'id' => $category->id_leoblogcat
  120. );
  121. /* breadcrumb */
  122. $r = $helper->getPaginationLink('module-leoblog-category', 'category', $params, false, true);
  123. $path = '';
  124. $all_cats = array();
  125. self::parentCategories($category, $all_cats);
  126. foreach ($all_cats as $key => $cat)
  127. {
  128. if ($cat->id == 1)
  129. {
  130. # validate module
  131. $path .= '<a href="'.$helper->getFontBlogLink().'">'.htmlentities($config->get('blog_link_title_'.$this->context->language->id, 'Blog'), ENT_NOQUOTES, 'UTF-8').'</a><span class="navigation-pipe">'.Configuration::get('PS_NAVIGATION_PIPE').'</span>';
  132. }
  133. elseif ((count($all_cats) - 1) == $key)
  134. {
  135. # validate module
  136. $path .= $cat->title;
  137. }
  138. else
  139. {
  140. $params = array(
  141. 'rewrite' => $cat->link_rewrite,
  142. 'id' => $cat->id
  143. );
  144. $path .= '<a href="'.$helper->getBlogCatLink($params).'">'.htmlentities($cat->title, ENT_NOQUOTES, 'UTF-8').'</a><span class="navigation-pipe">'.Configuration::get('PS_NAVIGATION_PIPE').'</span>';
  145. }
  146. }
  147. /* sub categories */
  148. $categories = $category->getChild($category->id_leoblogcat, $this->context->language->id);
  149. $childrens = array();
  150. if ($categories)
  151. {
  152. foreach ($categories as $child)
  153. {
  154. $params = array(
  155. 'rewrite' => $child['link_rewrite'],
  156. 'id' => $child['id_leoblogcat']
  157. );
  158. $child['thumb'] = _LEOBLOG_BLOG_IMG_URI_.'c/'.$child['image'];
  159. $child['category_link'] = $helper->getBlogCatLink($params);
  160. $childrens[] = $child;
  161. }
  162. }
  163. $this->context->smarty->assign(array(
  164. 'leading_blogs' => $leading_blogs,
  165. 'secondary_blogs' => $secondary_blogs,
  166. 'listing_leading_column' => $config->get('listing_leading_column', 1),
  167. 'listing_secondary_column' => $config->get('listing_secondary_column', 3),
  168. 'module_tpl' => $this->template_path,
  169. 'config' => $config,
  170. 'range' => $range,
  171. 'category' => $category,
  172. 'start' => $start,
  173. 'childrens' => $childrens,
  174. 'stop' => $stop,
  175. 'path' => $path,
  176. 'pages_nb' => $pages_nb,
  177. 'nb_items' => $count,
  178. 'p' => (int)$p,
  179. 'n' => (int)$n,
  180. 'meta_title' => Tools::ucfirst($category->title).' - '.$this->context->shop->name,
  181. 'meta_keywords' => $category->meta_keywords,
  182. 'meta_description' => $category->meta_description,
  183. 'requestPage' => $r['requestUrl'],
  184. 'requestNb' => $r,
  185. 'category' => $category
  186. ));
  187. }
  188. else
  189. {
  190. $path = '<a href="'.$helper->getFontBlogLink().'">'.htmlentities($config->get('blog_link_title_'.$this->context->language->id, 'Blog'), ENT_NOQUOTES, 'UTF-8').'</a><span class="navigation-pipe">'.Configuration::get('PS_NAVIGATION_PIPE').'</span>';
  191. $this->context->smarty->assign(array(
  192. 'active' => '0',
  193. 'path' => $path,
  194. 'leading_blogs' => array(),
  195. 'secondary_blogs' => array(),
  196. 'controller' => 'category',
  197. 'category' => $category
  198. ));
  199. }
  200. $this->setTemplate($template.'/category.tpl');
  201. }
  202. public static function parentCategories($current, &$return)
  203. {
  204. if ($current->id_parent)
  205. {
  206. $obj = new Leoblogcat($current->id_parent, Context::getContext()->language->id);
  207. self::parentCategories($obj, $return);
  208. }
  209. $return[] = $current;
  210. }
  211. }