chcreapost.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. <?php
  2. include_once _PS_MODULE_DIR_.'chcreateur/config/define.inc.php';
  3. include_once _PS_MODULE_DIR_.'chcreateur/classes/chcreaimages.php';
  4. use PrestaShop\PrestaShop\Adapter\Category\CategoryProductSearchProvider;
  5. use PrestaShop\PrestaShop\Adapter\Image\ImageRetriever;
  6. use PrestaShop\PrestaShop\Adapter\Product\PriceFormatter;
  7. use PrestaShop\PrestaShop\Adapter\Product\ProductColorsRetriever;
  8. use PrestaShop\PrestaShop\Core\Product\ProductListingPresenter;
  9. use PrestaShop\PrestaShop\Core\Product\Search\ProductSearchContext;
  10. use PrestaShop\PrestaShop\Core\Product\Search\ProductSearchQuery;
  11. use PrestaShop\PrestaShop\Core\Product\Search\SortOrder;
  12. class Chcreapost extends ObjectModel
  13. {
  14. public $id;
  15. public $id_chcreaposts;
  16. public $post_author;
  17. public $post_date;
  18. public $post_modified;
  19. public $comment_status;
  20. public $post_password;
  21. public $post_format;
  22. public $comment_count;
  23. public $post_title;
  24. public $post_excerpt;
  25. public $post_content;
  26. public $post_img;
  27. public $link_rewrite;
  28. public $position;
  29. public $active;
  30. public $video;
  31. public $audio;
  32. public $gallery;
  33. public $meta_title;
  34. public $meta_description;
  35. public $meta_keyword;
  36. public $related_products;
  37. public static $definition = [
  38. 'table' => 'chcreaposts',
  39. 'primary' => 'id_chcreaposts',
  40. 'multilang' => true,
  41. 'fields' => [
  42. 'post_title' => ['type' => self::TYPE_STRING, 'validate' => 'isString', 'lang' => true],
  43. 'meta_title' => ['type' => self::TYPE_STRING, 'validate' => 'isString', 'lang' => true],
  44. 'post_excerpt' => ['type' => self::TYPE_STRING, 'validate' => 'isString', 'lang' => true],
  45. 'meta_description' => ['type' => self::TYPE_STRING, 'validate' => 'isString', 'lang' => true],
  46. 'meta_keyword' => ['type' => self::TYPE_STRING, 'validate' => 'isString', 'lang' => true],
  47. 'post_content' => ['type' => self::TYPE_HTML, 'validate' => 'isCleanHtml', 'lang' => true],
  48. 'link_rewrite' => ['type' => self::TYPE_STRING, 'validate' => 'isString', 'lang' => true],
  49. 'related_products' => ['type' => self::TYPE_HTML, 'validate' => 'isCleanHtml'],
  50. 'video' => ['type' => self::TYPE_HTML, 'validate' => 'isCleanHtml'],
  51. 'audio' => ['type' => self::TYPE_HTML, 'validate' => 'isCleanHtml'],
  52. 'gallery' => ['type' => self::TYPE_HTML, 'validate' => 'isCleanHtml'],
  53. 'post_password' => ['type' => self::TYPE_STRING, 'validate' => 'isString'],
  54. 'post_format' => ['type' => self::TYPE_STRING, 'validate' => 'isString'],
  55. 'post_date' => ['type' => self::TYPE_DATE, 'validate' => 'isString'],
  56. 'post_modified' => ['type' => self::TYPE_DATE, 'validate' => 'isString'],
  57. 'post_img' => ['type' => self::TYPE_DATE, 'validate' => 'isString'],
  58. 'post_author' => ['type' => self::TYPE_INT, 'validate' => 'isunsignedInt'],
  59. 'comment_status' => ['type' => self::TYPE_STRING, 'validate' => 'isString'],
  60. 'comment_count' => ['type' => self::TYPE_INT, 'validate' => 'isunsignedInt'],
  61. 'position' => ['type' => self::TYPE_INT, 'validate' => 'isunsignedInt'],
  62. 'active' => ['type' => self::TYPE_BOOL, 'validate' => 'isBool'],
  63. ],
  64. ];
  65. public function __construct($id = null, $id_lang = null, $id_shop = null)
  66. {
  67. // print("<pre>".print_r($_POST, true)."</pre>");
  68. // exit();
  69. Shop::addTableAssociation('chcreaposts', ['type' => 'shop']);
  70. parent::__construct($id, $id_lang, $id_shop);
  71. }
  72. /**
  73. * @param bool $null_values
  74. *
  75. * @return [type]
  76. */
  77. public function update($null_values = false)
  78. {
  79. //print('<pre>'.print_r($_POST,true).'</pre>');
  80. // print('<pre>'.print_r($_FILES,true).'</pre>');
  81. // exit();
  82. if (isset($_FILES['post_img'], $_FILES['post_img']['tmp_name']) && !empty($_FILES['post_img']['tmp_name'][0])) {
  83. $Chcreaimages = new Chcreaimages();
  84. if ( !$Chcreaimages->uploadMedia('post_img', $this->id) ) {
  85. return false;
  86. }
  87. }
  88. $this->related_products = null;
  89. if (isset($_POST['related_products_temp']) && !empty($_POST['related_products_temp']) && is_array($_POST['related_products_temp'])) {
  90. $this->related_products = @implode(',', $_POST['related_products_temp']);
  91. }
  92. if (isset($_POST['gallery_temp_delete']) && !empty($_POST['gallery_temp_delete'])) {
  93. $gallery_temp_delete = @explode(',', $_POST['gallery_temp_delete']);
  94. if ( count($gallery_temp_delete) > 0 ) {
  95. $Chcreaimages = new Chcreaimages();
  96. $Chcreaimages->deleteSelection($gallery_temp_delete);
  97. }
  98. }
  99. if ( isset($_FILES['gallery_temp'], $_FILES['gallery_temp']['tmp_name']) && !empty($_FILES['gallery_temp']['tmp_name'][0]) ) {
  100. $Chcreaimages = new Chcreaimages();
  101. if ( !$Chcreaimages->uploadGallery('gallery_temp', $this->id) ) {
  102. return false;
  103. }
  104. }
  105. $this->post_modified = date('Y-m-d H:i:s');
  106. // if (empty($this->post_img) && isset($this->id)) {
  107. // $chcreapost = new Chcreapost($this->id);
  108. // $this->post_img = $chcreapost->post_img;
  109. // }
  110. return parent::update($null_values);
  111. }
  112. /**
  113. * @param bool $autodate
  114. * @param bool $null_values
  115. *
  116. * @return [type]
  117. */
  118. public function add($autodate = true, $null_values = false)
  119. {
  120. if (isset($_POST['related_products_temp']) && !empty($_POST['related_products_temp']) && is_array($_POST['related_products_temp'])) {
  121. $this->related_products = @implode(',', $_POST['related_products_temp']);
  122. }
  123. $this->post_author = (int) Context::getContext()->employee->id;
  124. $this->post_date = date('Y-m-d H:i:s');
  125. $this->post_modified = date('Y-m-d H:i:s');
  126. if ($this->position <= 0) {
  127. $this->position = self::getHigherPosition() + 1;
  128. }
  129. if (!parent::add($autodate, $null_values)
  130. || !Validate::isLoadedObject($this)) {
  131. return false;
  132. }
  133. if (isset($_FILES['post_img'], $_FILES['post_img']['tmp_name']) && !empty($_FILES['post_img']['tmp_name'][0])) {
  134. $Chcreaimages = new Chcreaimages();
  135. if ( !$Chcreaimages->uploadMedia('post_img', $this->id) ) {
  136. return false;
  137. }
  138. }
  139. if ( isset($_FILES['gallery_temp'], $_FILES['gallery_temp']['tmp_name']) && !empty($_FILES['gallery_temp']['tmp_name'][0]) ) {
  140. $Chcreaimages = new Chcreaimages();
  141. if ( !$Chcreaimages->uploadGallery('gallery_temp', $this->id) ) {
  142. return false;
  143. }
  144. }
  145. return true;
  146. }
  147. public function getSrcFromIframe($video_temp)
  148. {
  149. $sContent = htmlspecialchars($video_temp);
  150. $dom = new DomDocument;
  151. @$dom->loadHTML( $video_temp );
  152. $elems = $dom->getElementsByTagName('iframe');
  153. if ( count($elems) < 0 ) {
  154. return null;
  155. }
  156. foreach ( $elems as $elm ) {
  157. if ( $elm->hasAttribute('src') )
  158. $srcs[] = $elm->getAttribute('src');
  159. }
  160. return $srcs;
  161. }
  162. public static function getHigherPosition()
  163. {
  164. $sql = 'SELECT MAX(`position`)
  165. FROM `'._DB_PREFIX_.'chcreaposts`';
  166. $position = DB::getInstance()->getValue($sql);
  167. return (is_numeric($position)) ? $position : -1;
  168. }
  169. /**
  170. * @return [type]
  171. */
  172. public static function getAllSelections($limit=null)
  173. {
  174. $id_lang = (int) Context::getContext()->language->id;
  175. $id_shop = (int) Context::getContext()->shop->id;
  176. //$sql = ' SELECT xc.`id_chcreaposts`,xcl.`post_title`,xcl.`link_rewrite` ';
  177. $sql = ' SELECT * ';
  178. $sql .= ' FROM `'._DB_PREFIX_.'chcreaposts` xc';
  179. $sql .= ' INNER JOIN `'._DB_PREFIX_.'chcreaposts_lang` xcl ON (xc.`id_chcreaposts` = xcl.`id_chcreaposts` AND xcl.`id_lang` = '.$id_lang.')';
  180. $sql .= ' INNER JOIN `'._DB_PREFIX_.'chcreaposts_shop` xcs ON (xc.`id_chcreaposts` = xcs.`id_chcreaposts` AND xcs.`id_shop` = '.$id_shop.')';
  181. $sql .= ' WHERE xc.`active` = 1 ';
  182. $sql .= ' ORDER BY xc.`position` ASC';
  183. if ( $limit != null ) {
  184. $sql .= ' LIMIT '.$limit;
  185. }
  186. $queryexec = Db::getInstance()->executeS($sql);
  187. if (isset($queryexec) && !empty($queryexec)) {
  188. $i = 0;
  189. foreach ($queryexec as $qlvalue) {
  190. if (isset($qlvalue) && !empty($qlvalue)) {
  191. foreach ($qlvalue as $qkey => $qvalue) {
  192. $results[$i][$qkey] = $qvalue;
  193. // start Image
  194. if ('post_img' == $qkey) {
  195. $Chcreaimages = new Chcreaimages();
  196. $tmp = $Chcreaimages->getAllUrls($qlvalue['id_chcreaposts'], 'post');
  197. $results[$i]['post_img'] = array_shift($tmp);
  198. }
  199. // end Image
  200. if ('post_author' == $qkey) {
  201. $post_author_arr = new Employee((int) $qvalue);
  202. $results[$i]['post_author_arr']['lastname'] = $post_author_arr->lastname;
  203. $results[$i]['post_author_arr']['firstname'] = $post_author_arr->firstname;
  204. }
  205. // TODO
  206. $results[$i]['link'] = self::getBaseLink($id_shop) . Configuration::get('CHCREATEUR_URL') . '/' . $qlvalue['id_chcreaposts'] . '_' . $qlvalue['link_rewrite'];
  207. //$results[$i]['post_tags'] = self::GetPostTagsResults($qlvalue['id_xipposts'], 'tag');
  208. if (isset($qlvalue['audio']) && !empty($qlvalue['audio'])) {
  209. $results[$i]['audio'] = self::getSrcFromIframe($qlvalue['audio']);
  210. }
  211. if (isset($qlvalue['video']) && !empty($qlvalue['video'])) {
  212. $results[$i]['video'] = self::getSrcFromIframe($qlvalue['video']);
  213. }
  214. if ('gallery' == $qkey) {
  215. $Chcreaimages = new Chcreaimages();
  216. $results[$i]['gallery_lists'] = $Chcreaimages->getAllUrls($qlvalue['id_chcreaposts'], 'gallery');
  217. }
  218. }
  219. }
  220. ++$i;
  221. }
  222. }
  223. return $results;
  224. }
  225. public static function getSinglePost($id_chcreaposts = null, $post_type = 'post')
  226. {
  227. if (null == $id_chcreaposts) {
  228. return false;
  229. }
  230. $results = [];
  231. $id_lang = (int) Context::getContext()->language->id;
  232. $id_shop = (int) Context::getContext()->shop->id;
  233. $sql = 'SELECT * FROM `'._DB_PREFIX_.'chcreaposts` xc
  234. INNER JOIN `'._DB_PREFIX_.'chcreaposts_lang` xcl ON (xc.`id_chcreaposts` = xcl.`id_chcreaposts` AND xcl.`id_lang` = '.$id_lang.')
  235. INNER JOIN `'._DB_PREFIX_.'chcreaposts_shop` xcs ON (xc.`id_chcreaposts` = xcs.`id_chcreaposts` AND xcs.`id_shop` = '.$id_shop.')
  236. ';
  237. $sql .= ' WHERE xc.id_chcreaposts = '.(int) $id_chcreaposts;
  238. $queryexec = Db::getInstance()->getrow($sql);
  239. if (isset($queryexec) && !empty($queryexec)) {
  240. foreach ($queryexec as $qkey => $qvalue) {
  241. $results[$qkey] = $qvalue;
  242. // start Image
  243. if ('post_img' == $qkey) {
  244. $Chcreaimages = new Chcreaimages();
  245. $tmp = $Chcreaimages->getAllUrls($id_chcreaposts, 'post');
  246. $results['post_img'] = array_shift($tmp);
  247. }
  248. // end Image
  249. if ( 'link_rewrite' == $qkey ) {
  250. $results['link'] = self::getBaseLink($id_shop) . Configuration::get('CHCREATEUR_URL').'/' . $id_chcreaposts . '_' . $qvalue;
  251. }
  252. if ('post_author' == $qkey) {
  253. $post_author_arr = new Employee((int) $qvalue);
  254. $results['post_author_arr']['lastname'] = $post_author_arr->lastname;
  255. $results['post_author_arr']['firstname'] = $post_author_arr->firstname;
  256. }
  257. if ('audio' == $qkey && !empty($qvalue)) {
  258. $results['audio'] = self::getSrcFromIframe($qvalue);
  259. }
  260. if ('video' == $qkey && !empty($qvalue)) {
  261. $results['video'] = self::getSrcFromIframe($qvalue);
  262. }
  263. if ('gallery' == $qkey) {
  264. $Chcreaimages = new Chcreaimages();
  265. $results['gallery_lists'] = $Chcreaimages->getAllUrls($id_chcreaposts, 'gallery');
  266. }
  267. if ('related_products' == $qkey) {
  268. if ( !empty($qvalue) ) {
  269. $prod_temp = @explode(',', $qvalue);
  270. $results['related_products'] = self::getViewedProducts($prod_temp);
  271. } else {
  272. unset($results['related_products']);
  273. }
  274. }
  275. }
  276. }
  277. return $results;
  278. }
  279. public static function getsinglepath($id_chcreaposts = null, $post_type = 'post')
  280. {
  281. if (null == $id_chcreaposts) {
  282. return false;
  283. }
  284. $pipe = (Configuration::get('PS_NAVIGATION_PIPE') ? Configuration::get('PS_NAVIGATION_PIPE') : '>');
  285. $posts = self::get_the_title($id_chcreaposts, $post_type);
  286. //$category_default = $posts['category_default'] ? $posts['category_default'] : null;
  287. //$categories = self::get_the_category($category_default);
  288. // TODO title categorie
  289. $title = "Notre createur"; //$categories['name'] ? $categories['name'] : '';
  290. //
  291. $name = $posts['post_title'];
  292. $params['id'] = $categories['id_xipcategory'] ? $categories['id_xipcategory'] : 0;
  293. $params['rewrite'] = $categories['link_rewrite'] ? $categories['link_rewrite'] : '';
  294. $params['page_type'] = 'category';
  295. $params['subpage_type'] = $post_type ? $post_type : 'post';
  296. $link = xipblog::XipBlogCategoryLink($params);
  297. $meta_title = Configuration::get(xipblog::$xipblogshortname.'meta_title');
  298. $meta_title = (isset($meta_title) ? $meta_title : 'Blog');
  299. $blog_url = xipblog::XipBlogLink();
  300. $full_paths = '<a href="'.$blog_url.'" title="'.$meta_title.'" data-gg="">'.$meta_title.'</a><span class="navigation-pipe">'.$pipe.'</span>';
  301. $str = '<a href="'.$link.'" title="'.$title.'" data-gg="">'.$title.'</a><span class="navigation-pipe">'.$pipe.'</span>'.$name;
  302. return $full_paths.$str;
  303. }
  304. public static function getTopPosition()
  305. {
  306. $sql = 'SELECT MAX(`position`)
  307. FROM `'._DB_PREFIX_.'chcreaposts`';
  308. $position = DB::getInstance()->getValue($sql);
  309. return (is_numeric($position)) ? $position : -1;
  310. }
  311. public function updatePosition($way, $position)
  312. {
  313. if (!$res = Db::getInstance()->executeS(
  314. '
  315. SELECT `id_chcreaposts`, `position`
  316. FROM `'._DB_PREFIX_.'chcreaposts`
  317. ORDER BY `position` ASC'
  318. )) {
  319. return false;
  320. }
  321. if (!empty($res)) {
  322. foreach ($res as $chcreaposts) {
  323. if ((int) $chcreaposts['id_chcreaposts'] == (int) $this->id) {
  324. $moved_chcreaposts = $chcreaposts;
  325. }
  326. }
  327. }
  328. if (!isset($moved_chcreaposts) || !isset($position)) {
  329. return false;
  330. }
  331. $queryx = ' UPDATE `'._DB_PREFIX_.'chcreaposts`
  332. SET `position`= `position` '.($way ? '- 1' : '+ 1').'
  333. WHERE `position`
  334. '.($way
  335. ? '> '.(int) $moved_chcreaposts['position'].' AND `position` <= '.(int) $position
  336. : '< '.(int) $moved_chcreaposts['position'].' AND `position` >= '.(int) $position.'
  337. ');
  338. $queryy = ' UPDATE `'._DB_PREFIX_.'chcreaposts`
  339. SET `position` = '.(int) $position.'
  340. WHERE `id_chcreaposts` = '.(int) $moved_chcreaposts['id_chcreaposts'];
  341. return Db::getInstance()->execute($queryx)
  342. && Db::getInstance()->execute($queryy);
  343. }
  344. public static function get_the_title($id_chcreaposts = null, $post_type = 'post')
  345. {
  346. if (null == $id_chcreaposts) {
  347. return false;
  348. }
  349. $id_lang = (int) Context::getContext()->language->id;
  350. $id_shop = (int) Context::getContext()->shop->id;
  351. $sql = 'SELECT xc.`id_chcreaposts`,xcl.`post_title`,xcl.`link_rewrite` FROM `'._DB_PREFIX_.'chcreaposts` xc
  352. INNER JOIN `'._DB_PREFIX_.'chcreaposts_lang` xcl ON (xc.`id_chcreaposts` = xcl.`id_chcreaposts` AND xcl.`id_lang` = '.$id_lang.')
  353. INNER JOIN `'._DB_PREFIX_.'chcreaposts_shop` xcs ON (xc.`id_chcreaposts` = xcs.`id_chcreaposts` AND xcs.`id_shop` = '.$id_shop.')
  354. ';
  355. $sql .= ' WHERE xc.`post_type` = "'.($post_type ? pSQL($post_type) : 'post').'" AND xc.`id_chcreaposts` = '.$id_chcreaposts;
  356. return Db::getInstance()->getrow($sql);
  357. }
  358. public static function get_the_id($rewrite = null, $post_type = 'post')
  359. {
  360. if (null == $rewrite) {
  361. return false;
  362. }
  363. $id_lang = (int) Context::getContext()->language->id;
  364. $id_shop = (int) Context::getContext()->shop->id;
  365. $sql = 'SELECT xc.`id_chcreaposts` FROM `'._DB_PREFIX_.'chcreaposts` xc INNER JOIN `'._DB_PREFIX_.'chcreaposts_lang` xcl ON (xc.`id_chcreaposts` = xcl.`id_chcreaposts` AND xcl.`id_lang` = '.$id_lang.') INNER JOIN `'._DB_PREFIX_.'chcreaposts_shop` xcs ON (xc.`id_chcreaposts` = xcs.`id_chcreaposts` AND xcs.`id_shop` = '.$id_shop.') ';
  366. $sql .= ' WHERE xc.`post_type` = "'.($post_type ? pSQL($post_type) : 'post').'" AND xcl.`link_rewrite` = "'.pSQL($rewrite).'" ';
  367. $rslts = Db::getInstance()->getrow($sql);
  368. return isset($rslts['id_chcreaposts']) ? $rslts['id_chcreaposts'] : null;
  369. }
  370. public function getNextPost($position)
  371. {
  372. $id_lang = (int) Context::getContext()->language->id;
  373. $id_shop = (int) Context::getContext()->shop->id;
  374. $sql = ' SELECT * FROM `'._DB_PREFIX_.'chcreaposts` xc ';
  375. $sql .= ' INNER JOIN `'._DB_PREFIX_.'chcreaposts_lang` xcl ON (xc.`id_chcreaposts` = xcl.`id_chcreaposts` AND xcl.`id_lang` = '.$id_lang.')';
  376. $sql .= ' INNER JOIN `'._DB_PREFIX_.'chcreaposts_shop` xcs ON (xc.`id_chcreaposts` = xcs.`id_chcreaposts` AND xcs.`id_shop` = '.$id_shop.')';
  377. $sql .= ' WHERE xc.`position` > '.$position;
  378. $sql .= ' AND xc.`active` = 1';
  379. $sql .= ' ORDER BY xc.`position` ASC';
  380. $rslt = Db::getInstance()->getrow($sql);
  381. if (empty($rslt)) {
  382. return null;
  383. }
  384. $rslt['link'] = '/'.Configuration::get('CHCREATEUR_URL').'/'.$rslt['id_chcreaposts'].'_'.$rslt['link_rewrite'];
  385. return $rslt;
  386. }
  387. public function getPreviousPost($position)
  388. {
  389. $id_lang = (int) Context::getContext()->language->id;
  390. $id_shop = (int) Context::getContext()->shop->id;
  391. $sql = ' SELECT * FROM `'._DB_PREFIX_.'chcreaposts` xc ';
  392. $sql .= ' INNER JOIN `'._DB_PREFIX_.'chcreaposts_lang` xcl ON (xc.`id_chcreaposts` = xcl.`id_chcreaposts` AND xcl.`id_lang` = '.$id_lang.')';
  393. $sql .= ' INNER JOIN `'._DB_PREFIX_.'chcreaposts_shop` xcs ON (xc.`id_chcreaposts` = xcs.`id_chcreaposts` AND xcs.`id_shop` = '.$id_shop.')';
  394. $sql .= ' WHERE xc.`position` < '.$position;
  395. $sql .= ' AND xc.`active` = 1';
  396. $sql .= ' ORDER BY xc.`position` DESC';
  397. //print($sql);
  398. $rslt = Db::getInstance()->getrow($sql);
  399. if (empty($rslt)) {
  400. return null;
  401. }
  402. $rslt['link'] = '/'.Configuration::get('CHCREATEUR_URL').'/'.$rslt['id_chcreaposts'].'_'.$rslt['link_rewrite'];
  403. return $rslt;
  404. }
  405. public static function PostExists($id_chcreaposts = null, $post_type = 'post')
  406. {
  407. if (null == $id_chcreaposts || 0 == $id_chcreaposts) {
  408. return false;
  409. }
  410. $sql = 'SELECT xc.`id_chcreaposts` FROM `'._DB_PREFIX_.'chcreaposts` xc WHERE xc.`post_type` = "'.($post_type ? $post_type : 'post').'" AND xc.active = 1 AND xc.`id_chcreaposts` = '.$id_chcreaposts;
  411. $rslts = Db::getInstance()->getrow($sql);
  412. return (isset($rslts['id_chcreaposts']) && !empty($rslts['id_chcreaposts'])) ? true : false;
  413. }
  414. public static function get_the_rewrite($id = null, $post_type = 'post')
  415. {
  416. if (null == $id) {
  417. return false;
  418. }
  419. $id_lang = (int) Context::getContext()->language->id;
  420. $id_shop = (int) Context::getContext()->shop->id;
  421. $sql = 'SELECT xcl.`link_rewrite` FROM `'._DB_PREFIX_.'chcreaposts` xc INNER JOIN `'._DB_PREFIX_.'chcreaposts_lang` xcl ON (xc.`id_chcreaposts` = xcl.`id_chcreaposts` AND xcl.`id_lang` = '.$id_lang.') INNER JOIN `'._DB_PREFIX_.'chcreaposts_shop` xcs ON (xc.`id_chcreaposts` = xcs.`id_chcreaposts` AND xcs.`id_shop` = '.$id_shop.') ';
  422. $sql .= ' WHERE xc.`post_type` = "'.($post_type ? $post_type : 'post').'" AND xc.`id_chcreaposts` = "'.$id.'" ';
  423. $rslts = Db::getInstance()->getrow($sql);
  424. return isset($rslts['link_rewrite']) ? $rslts['link_rewrite'] : null;
  425. }
  426. public static function ImageExists($file = null)
  427. {
  428. if (null == $file) {
  429. return false;
  430. }
  431. $image = chcrea_img_dir.$file;
  432. if (file_exists($image)) {
  433. return true;
  434. }
  435. return false;
  436. }
  437. public static function PostCountUpdate($id = null)
  438. {
  439. if (null == $id || 0 == $id) {
  440. return false;
  441. }
  442. $sql = 'UPDATE '._DB_PREFIX_.'chcreaposts as xc SET xc.comment_count = (xc.comment_count+1) where xc.id_chcreaposts = '.(int) $id;
  443. if (Db::getInstance()->execute($sql)) {
  444. return true;
  445. }
  446. return false;
  447. }
  448. /**
  449. * @param mixed $productIds
  450. *
  451. * @return [type]
  452. */
  453. protected function getViewedProducts($productIds)
  454. {
  455. if (!empty($productIds)) {
  456. $context = Context::getContext();
  457. $assembler = new ProductAssembler($context);
  458. $presenterFactory = new ProductPresenterFactory($context);
  459. $presentationSettings = $presenterFactory->getPresentationSettings();
  460. $presenter = new ProductListingPresenter(
  461. new ImageRetriever(
  462. $context->link
  463. ),
  464. $context->link,
  465. new PriceFormatter(),
  466. new ProductColorsRetriever(),
  467. $context->getTranslator()
  468. );
  469. $products_for_template = [];
  470. if (is_array($productIds)) {
  471. foreach ($productIds as $productId) {
  472. // if ($this->currentProductId !== $productId) {
  473. $products_for_template[] = $presenter->present(
  474. $presentationSettings,
  475. $assembler->assembleProduct(['id_product' => $productId]),
  476. $context->language
  477. );
  478. // }
  479. }
  480. }
  481. return $products_for_template;
  482. }
  483. return false;
  484. }
  485. public static function getBaseLink($id_shop = null, $ssl = null, $relative_protocol = false)
  486. {
  487. static $force_ssl = null;
  488. if ($ssl === null) {
  489. if ($force_ssl === null) {
  490. $force_ssl = (Configuration::get('PS_SSL_ENABLED') && Configuration::get('PS_SSL_ENABLED_EVERYWHERE'));
  491. }
  492. $ssl = $force_ssl;
  493. }
  494. if (Configuration::get('PS_MULTISHOP_FEATURE_ACTIVE') && $id_shop !== null) {
  495. $shop = new Shop($id_shop);
  496. } else {
  497. $shop = Context::getContext()->shop;
  498. }
  499. if ($relative_protocol) {
  500. $base = '//'.($ssl ? $shop->domain_ssl : $shop->domain);
  501. } else {
  502. $base = (($ssl) ? 'https://'.$shop->domain_ssl : 'http://'.$shop->domain);
  503. }
  504. return $base.$shop->getBaseURI();
  505. }
  506. }