chcreapost.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  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. /*
  280. public static function getsinglepath($id_chcreaposts = null, $post_type = 'post')
  281. {
  282. if (null == $id_chcreaposts) {
  283. return false;
  284. }
  285. $pipe = (Configuration::get('PS_NAVIGATION_PIPE') ? Configuration::get('PS_NAVIGATION_PIPE') : '>');
  286. $posts = self::get_the_title($id_chcreaposts, $post_type);
  287. //$category_default = $posts['category_default'] ? $posts['category_default'] : null;
  288. //$categories = self::get_the_category($category_default);
  289. // TODO title categorie
  290. $title = "Notre createur"; //$categories['name'] ? $categories['name'] : '';
  291. //
  292. $name = $posts['post_title'];
  293. $params['id'] = $categories['id_xipcategory'] ? $categories['id_xipcategory'] : 0;
  294. $params['rewrite'] = $categories['link_rewrite'] ? $categories['link_rewrite'] : '';
  295. $params['page_type'] = 'category';
  296. $params['subpage_type'] = $post_type ? $post_type : 'post';
  297. $link = xipblog::XipBlogCategoryLink($params);
  298. $meta_title = Configuration::get(xipblog::$xipblogshortname.'meta_title');
  299. $meta_title = (isset($meta_title) ? $meta_title : 'Blog');
  300. $blog_url = xipblog::XipBlogLink();
  301. $full_paths = '<a href="'.$blog_url.'" title="'.$meta_title.'" data-gg="">'.$meta_title.'</a><span class="navigation-pipe">'.$pipe.'</span>';
  302. $str = '<a href="'.$link.'" title="'.$title.'" data-gg="">'.$title.'</a><span class="navigation-pipe">'.$pipe.'</span>'.$name;
  303. return $full_paths.$str;
  304. }
  305. */
  306. public static function getTopPosition()
  307. {
  308. $sql = 'SELECT MAX(`position`)
  309. FROM `'._DB_PREFIX_.'chcreaposts`';
  310. $position = DB::getInstance()->getValue($sql);
  311. return (is_numeric($position)) ? $position : -1;
  312. }
  313. public function updatePosition($way, $position)
  314. {
  315. if (!$res = Db::getInstance()->executeS(
  316. '
  317. SELECT `id_chcreaposts`, `position`
  318. FROM `'._DB_PREFIX_.'chcreaposts`
  319. ORDER BY `position` ASC'
  320. )) {
  321. return false;
  322. }
  323. if (!empty($res)) {
  324. foreach ($res as $chcreaposts) {
  325. if ((int) $chcreaposts['id_chcreaposts'] == (int) $this->id) {
  326. $moved_chcreaposts = $chcreaposts;
  327. }
  328. }
  329. }
  330. if (!isset($moved_chcreaposts) || !isset($position)) {
  331. return false;
  332. }
  333. $queryx = ' UPDATE `'._DB_PREFIX_.'chcreaposts`
  334. SET `position`= `position` '.($way ? '- 1' : '+ 1').'
  335. WHERE `position`
  336. '.($way
  337. ? '> '.(int) $moved_chcreaposts['position'].' AND `position` <= '.(int) $position
  338. : '< '.(int) $moved_chcreaposts['position'].' AND `position` >= '.(int) $position.'
  339. ');
  340. $queryy = ' UPDATE `'._DB_PREFIX_.'chcreaposts`
  341. SET `position` = '.(int) $position.'
  342. WHERE `id_chcreaposts` = '.(int) $moved_chcreaposts['id_chcreaposts'];
  343. return Db::getInstance()->execute($queryx)
  344. && Db::getInstance()->execute($queryy);
  345. }
  346. public static function get_the_title($id_chcreaposts = null, $post_type = 'post')
  347. {
  348. if (null == $id_chcreaposts) {
  349. return false;
  350. }
  351. $id_lang = (int) Context::getContext()->language->id;
  352. $id_shop = (int) Context::getContext()->shop->id;
  353. $sql = 'SELECT xc.`id_chcreaposts`,xcl.`post_title`,xcl.`link_rewrite` FROM `'._DB_PREFIX_.'chcreaposts` xc
  354. INNER JOIN `'._DB_PREFIX_.'chcreaposts_lang` xcl ON (xc.`id_chcreaposts` = xcl.`id_chcreaposts` AND xcl.`id_lang` = '.$id_lang.')
  355. INNER JOIN `'._DB_PREFIX_.'chcreaposts_shop` xcs ON (xc.`id_chcreaposts` = xcs.`id_chcreaposts` AND xcs.`id_shop` = '.$id_shop.')
  356. ';
  357. $sql .= ' WHERE xc.`post_type` = "'.($post_type ? pSQL($post_type) : 'post').'" AND xc.`id_chcreaposts` = '.$id_chcreaposts;
  358. return Db::getInstance()->getrow($sql);
  359. }
  360. public static function get_the_id($rewrite = null, $post_type = 'post')
  361. {
  362. if (null == $rewrite) {
  363. return false;
  364. }
  365. $id_lang = (int) Context::getContext()->language->id;
  366. $id_shop = (int) Context::getContext()->shop->id;
  367. $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.') ';
  368. $sql .= ' WHERE xc.`post_type` = "'.($post_type ? pSQL($post_type) : 'post').'" AND xcl.`link_rewrite` = "'.pSQL($rewrite).'" ';
  369. $rslts = Db::getInstance()->getrow($sql);
  370. return isset($rslts['id_chcreaposts']) ? $rslts['id_chcreaposts'] : null;
  371. }
  372. public static function getNextPost($position)
  373. {
  374. $id_lang = (int) Context::getContext()->language->id;
  375. $id_shop = (int) Context::getContext()->shop->id;
  376. $sql = ' SELECT * FROM `'._DB_PREFIX_.'chcreaposts` xc ';
  377. $sql .= ' INNER JOIN `'._DB_PREFIX_.'chcreaposts_lang` xcl ON (xc.`id_chcreaposts` = xcl.`id_chcreaposts` AND xcl.`id_lang` = '.$id_lang.')';
  378. $sql .= ' INNER JOIN `'._DB_PREFIX_.'chcreaposts_shop` xcs ON (xc.`id_chcreaposts` = xcs.`id_chcreaposts` AND xcs.`id_shop` = '.$id_shop.')';
  379. $sql .= ' WHERE xc.`position` > '.$position;
  380. $sql .= ' AND xc.`active` = 1';
  381. $sql .= ' ORDER BY xc.`position` ASC';
  382. $rslt = Db::getInstance()->getrow($sql);
  383. if (empty($rslt)) {
  384. return null;
  385. }
  386. $rslt['link'] = '/'.Configuration::get('CHCREATEUR_URL').'/'.$rslt['id_chcreaposts'].'_'.$rslt['link_rewrite'];
  387. return $rslt;
  388. }
  389. public static function getPreviousPost($position)
  390. {
  391. $id_lang = (int) Context::getContext()->language->id;
  392. $id_shop = (int) Context::getContext()->shop->id;
  393. $sql = ' SELECT * FROM `'._DB_PREFIX_.'chcreaposts` xc ';
  394. $sql .= ' INNER JOIN `'._DB_PREFIX_.'chcreaposts_lang` xcl ON (xc.`id_chcreaposts` = xcl.`id_chcreaposts` AND xcl.`id_lang` = '.$id_lang.')';
  395. $sql .= ' INNER JOIN `'._DB_PREFIX_.'chcreaposts_shop` xcs ON (xc.`id_chcreaposts` = xcs.`id_chcreaposts` AND xcs.`id_shop` = '.$id_shop.')';
  396. $sql .= ' WHERE xc.`position` < '.$position;
  397. $sql .= ' AND xc.`active` = 1';
  398. $sql .= ' ORDER BY xc.`position` DESC';
  399. //print($sql);
  400. $rslt = Db::getInstance()->getrow($sql);
  401. if (empty($rslt)) {
  402. return null;
  403. }
  404. $rslt['link'] = '/'.Configuration::get('CHCREATEUR_URL').'/'.$rslt['id_chcreaposts'].'_'.$rslt['link_rewrite'];
  405. return $rslt;
  406. }
  407. public static function PostExists($id_chcreaposts = null, $post_type = 'post')
  408. {
  409. if (null == $id_chcreaposts || 0 == $id_chcreaposts) {
  410. return false;
  411. }
  412. $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;
  413. $rslts = Db::getInstance()->getrow($sql);
  414. return (isset($rslts['id_chcreaposts']) && !empty($rslts['id_chcreaposts'])) ? true : false;
  415. }
  416. public static function get_the_rewrite($id = null, $post_type = 'post')
  417. {
  418. if (null == $id) {
  419. return false;
  420. }
  421. $id_lang = (int) Context::getContext()->language->id;
  422. $id_shop = (int) Context::getContext()->shop->id;
  423. $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.') ';
  424. $sql .= ' WHERE xc.`post_type` = "'.($post_type ? $post_type : 'post').'" AND xc.`id_chcreaposts` = "'.$id.'" ';
  425. $rslts = Db::getInstance()->getrow($sql);
  426. return isset($rslts['link_rewrite']) ? $rslts['link_rewrite'] : null;
  427. }
  428. public static function ImageExists($file = null)
  429. {
  430. if (null == $file) {
  431. return false;
  432. }
  433. $image = chcrea_img_dir.$file;
  434. if (file_exists($image)) {
  435. return true;
  436. }
  437. return false;
  438. }
  439. public static function PostCountUpdate($id = null)
  440. {
  441. if (null == $id || 0 == $id) {
  442. return false;
  443. }
  444. $sql = 'UPDATE '._DB_PREFIX_.'chcreaposts as xc SET xc.comment_count = (xc.comment_count+1) where xc.id_chcreaposts = '.(int) $id;
  445. if (Db::getInstance()->execute($sql)) {
  446. return true;
  447. }
  448. return false;
  449. }
  450. /**
  451. * @param mixed $productIds
  452. *
  453. * @return [type]
  454. */
  455. protected function getViewedProducts($productIds)
  456. {
  457. if (!empty($productIds)) {
  458. $context = Context::getContext();
  459. $assembler = new ProductAssembler($context);
  460. $presenterFactory = new ProductPresenterFactory($context);
  461. $presentationSettings = $presenterFactory->getPresentationSettings();
  462. $presenter = new ProductListingPresenter(
  463. new ImageRetriever(
  464. $context->link
  465. ),
  466. $context->link,
  467. new PriceFormatter(),
  468. new ProductColorsRetriever(),
  469. $context->getTranslator()
  470. );
  471. $products_for_template = [];
  472. if (is_array($productIds)) {
  473. foreach ($productIds as $productId) {
  474. // if ($this->currentProductId !== $productId) {
  475. $products_for_template[] = $presenter->present(
  476. $presentationSettings,
  477. $assembler->assembleProduct(['id_product' => $productId]),
  478. $context->language
  479. );
  480. // }
  481. }
  482. }
  483. return $products_for_template;
  484. }
  485. return false;
  486. }
  487. public static function getBaseLink($id_shop = null, $ssl = null, $relative_protocol = false)
  488. {
  489. static $force_ssl = null;
  490. if ($ssl === null) {
  491. if ($force_ssl === null) {
  492. $force_ssl = (Configuration::get('PS_SSL_ENABLED') && Configuration::get('PS_SSL_ENABLED_EVERYWHERE'));
  493. }
  494. $ssl = $force_ssl;
  495. }
  496. if (Configuration::get('PS_MULTISHOP_FEATURE_ACTIVE') && $id_shop !== null) {
  497. $shop = new Shop($id_shop);
  498. } else {
  499. $shop = Context::getContext()->shop;
  500. }
  501. if ($relative_protocol) {
  502. $base = '//'.($ssl ? $shop->domain_ssl : $shop->domain);
  503. } else {
  504. $base = (($ssl) ? 'https://'.$shop->domain_ssl : 'http://'.$shop->domain);
  505. }
  506. return $base.$shop->getBaseURI();
  507. }
  508. }