chcreapost.php 20 KB

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