Advertisement
Guest User

classes/SmartBlogPost.php

a guest
Jul 31st, 2016
361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 36.55 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. class SmartBlogPost extends ObjectModel
  5. {
  6.       public $id_smart_blog_post;
  7.       public $id_author;
  8.       public $id_category;
  9.       public $position = 0;
  10.       public $active = 1;
  11.       public $available;
  12.       public $created;
  13.       public $modified;
  14.       public $short_description;
  15.       public $viewed;
  16.       public $comment_status = 1;
  17.       public $post_type;
  18.       public $meta_title;
  19.       public $meta_keyword;
  20.       public $meta_description;
  21.       public $image;
  22.       public $content;
  23.       public $link_rewrite;
  24.       public $is_featured;
  25.  
  26.       public static $definition = array(
  27.         'table' => 'smart_blog_post',
  28.         'primary' => 'id_smart_blog_post',
  29.                 'multilang'=>true,
  30.         'fields' => array(
  31.                      'active' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
  32.                      'position' => array('type' => self::TYPE_INT, 'validate' => 'isunsignedInt'),
  33.                      'id_category' => array('type' => self::TYPE_INT, 'validate' => 'isunsignedInt'),
  34.                      'id_author' => array('type' => self::TYPE_INT, 'validate' => 'isunsignedInt'),
  35.                      'available' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
  36.                      'created' => array('type' => self::TYPE_DATE, 'validate' => 'isString'),
  37.                      'modified' => array('type' => self::TYPE_DATE, 'validate' => 'isString'),
  38.                      'viewed' => array('type' => self::TYPE_INT, 'validate' => 'isunsignedInt'),
  39.                      'is_featured' => array('type' => self::TYPE_INT, 'validate' => 'isunsignedInt'),
  40.                      'comment_status' => array('type' => self::TYPE_INT, 'validate' => 'isunsignedInt'),
  41.                      'post_type' => array('type' => self::TYPE_STRING, 'validate' => 'isString'),
  42.                      'image' => array('type' => self::TYPE_STRING, 'validate' => 'isString'),
  43.                      
  44.                         'meta_title' => array('type' => self::TYPE_STRING, 'validate' => 'isString','lang'=>true,'required'=>true),
  45.                         'meta_keyword' => array('type' => self::TYPE_STRING, 'lang'=>true, 'validate' => 'isString'),
  46.                         'meta_description' => array('type' => self::TYPE_STRING, 'lang'=>true, 'validate' => 'isString'),
  47.                         'short_description' => array('type' => self::TYPE_STRING, 'lang'=>true, 'validate' => 'isString','required'=>true),
  48.                         'content' => array('type' => self::TYPE_HTML, 'lang' => true, 'validate' => 'isString','required'=>true),
  49.                     'link_rewrite' => array('type' => self::TYPE_STRING, 'lang'=>true, 'validate' => 'isString','required'=>false)
  50.                      ),
  51.     );
  52.      
  53.  
  54.    
  55.     public function __construct($id = null, $id_lang = null, $id_shop = null){
  56.             Shop::addTableAssociation('smart_blog_post', array('type' => 'shop'));
  57.                     parent::__construct($id, $id_lang, $id_shop);
  58.             }
  59.            
  60.     public static function getPost($id_post, $id_lang = null){
  61.         $result = array();  
  62.         if($id_lang == null){
  63.                     $id_lang = (int)Context::getContext()->language->id;
  64.                 }
  65.                 /*preg_match('/[\d]+:', $id_post, $id_post);
  66.                 $id_post = $id_post[0];*/
  67.                
  68.         $sql = 'SELECT * FROM '._DB_PREFIX_.'smart_blog_post p INNER JOIN
  69.                '._DB_PREFIX_.'smart_blog_post_lang pl ON p.id_smart_blog_post=pl.id_smart_blog_post INNER JOIN
  70.                '._DB_PREFIX_.'smart_blog_post_shop ps ON pl.id_smart_blog_post = ps.id_smart_blog_post
  71.                WHERE pl.id_lang='.(int)$id_lang.'
  72.                AND p.active= 1 AND p.id_smart_blog_post = '.(int)$id_post;
  73.        
  74.         if (!$post = Db::getInstance()->executeS($sql))
  75.             return false;
  76.                 $result['id_post'] = $post[0]['id_smart_blog_post'];
  77.                 $result['meta_title'] = $post[0]['meta_title'];
  78.                 $result['meta_description'] = $post[0]['meta_description'];
  79.                 $result['short_description'] = $post[0]['short_description'];
  80.                 $result['meta_keyword'] = $post[0]['meta_keyword'];
  81.                 if((Module::isEnabled('smartshortcode') == 1) && (Module::isInstalled('smartshortcode') == 1)){
  82.                  require_once(_PS_MODULE_DIR_ . 'smartshortcode/smartshortcode.php');
  83.                 $smartshortcode = new SmartShortCode();
  84.                 $result['content'] = $smartshortcode->parse($post[0]['content']);
  85.                 }else{
  86.                
  87.                  $result['content'] = $post[0]['content'];
  88.                  }
  89.                 $result['active'] = $post[0]['active'];
  90.                 $result['created'] = $post[0]['created'];
  91.                 $result['comment_status'] = $post[0]['comment_status'];
  92.                 $result['viewed'] = $post[0]['viewed'];
  93.                 $result['is_featured'] = $post[0]['is_featured'];
  94.                 $result['post_type'] = $post[0]['post_type'];
  95.                 $result['id_category'] = $post[0]['id_category'];
  96.                 $employee = new  Employee($post[0]['id_author']);
  97.                 $result['lastname'] = $employee->lastname;
  98.                 $result['firstname'] = $employee->firstname;
  99.                 if (file_exists(_PS_MODULE_DIR_.'smartblog/images/' . $post[0]['id_smart_blog_post'] . '.jpg') )
  100.                 {
  101.                    $image =   $post[0]['id_smart_blog_post'] . '.jpg';
  102.                    $result['post_img'] = $image;
  103.         }
  104.                 else
  105.                 {
  106.                    $result['post_img'] =NULL;
  107.                 }
  108.         return $result;
  109.     }
  110.    
  111.     public static function getAllPost($id_lang = null,$limit_start,$limit){
  112.         if($id_lang == null){
  113.                     $id_lang = (int)Context::getContext()->language->id;
  114.                 }
  115.         if($limit_start == '')
  116.             $limit_start = 0;
  117.         if($limit == '')
  118.             $limit = 5;
  119.         $result = array();
  120.         $BlogCategory = '';
  121.      
  122.         $sql = 'SELECT * FROM '._DB_PREFIX_.'smart_blog_post p INNER JOIN
  123.                '._DB_PREFIX_.'smart_blog_post_lang pl ON p.id_smart_blog_post=pl.id_smart_blog_post INNER JOIN
  124.                '._DB_PREFIX_.'smart_blog_post_shop ps ON pl.id_smart_blog_post = ps.id_smart_blog_post AND ps.id_shop = '.(int) Context::getContext()->shop->id.'
  125.                WHERE pl.id_lang='.(int)$id_lang.'
  126.                AND p.active= 1 ORDER BY p.id_smart_blog_post DESC LIMIT '.(int)$limit_start.','.(int)$limit;
  127.        
  128.         if (!$posts = Db::getInstance()->executeS($sql))
  129.             return false;
  130.                 $BlogCategory = new BlogCategory();
  131.         $i = 0;
  132.             foreach($posts as $post){
  133.                 $result[$i]['id_post'] = $post['id_smart_blog_post'];
  134.                 $result[$i]['is_featured'] = $post['is_featured'];
  135.                 $result[$i]['viewed'] = $post['viewed'];
  136.                 $result[$i]['meta_title'] = $post['meta_title'];
  137.                 $result[$i]['meta_description'] = $post['meta_description'];
  138.                 $result[$i]['short_description'] = $post['short_description'];
  139.                 $result[$i]['content'] = $post['content'];
  140.                 $result[$i]['meta_keyword'] = $post['meta_keyword'];
  141.                 $result[$i]['id_category'] = $post['id_category'];
  142.                 $result[$i]['link_rewrite'] = $post['link_rewrite'];
  143.                 $result[$i]['cat_name'] = $BlogCategory->getCatName($post['id_category']);
  144.                 $result[$i]['cat_link_rewrite'] = $BlogCategory->getCatLinkRewrite($post['id_category']);
  145.                 $employee = new  Employee( $post['id_author']);
  146.                
  147.                 $result[$i]['lastname'] = $employee->lastname;
  148.                 $result[$i]['firstname'] = $employee->firstname;
  149.                  if (file_exists(_PS_MODULE_DIR_.'smartblog/images/' . $post['id_smart_blog_post'] . '.jpg'))
  150.                     {
  151.                        $image =   $post['id_smart_blog_post'];
  152.                        $result[$i]['post_img'] = $image;
  153.                     }
  154.                     else
  155.                     {
  156.                        $result[$i]['post_img'] = 'no';
  157.                     }
  158.                 $result[$i]['created'] = $post['created'];
  159.                 $i++;
  160.             }
  161.         return $result;
  162.     }
  163.    
  164.     public static function getToltal($id_lang = null){
  165.         if($id_lang == null){
  166.                     $id_lang = (int)Context::getContext()->language->id;
  167.                 }
  168.         $sql = 'SELECT * FROM '._DB_PREFIX_.'smart_blog_post p INNER JOIN
  169.                '._DB_PREFIX_.'smart_blog_post_lang pl ON p.id_smart_blog_post=pl.id_smart_blog_post INNER JOIN
  170.                '._DB_PREFIX_.'smart_blog_post_shop ps ON pl.id_smart_blog_post = ps.id_smart_blog_post AND ps.id_shop = '.(int) Context::getContext()->shop->id.'
  171.                WHERE pl.id_lang='.(int)$id_lang.'
  172.                AND p.active= 1';
  173.         if (!$posts = Db::getInstance()->executeS($sql))
  174.             return false;          
  175.         return count($posts);
  176.     }
  177.        
  178.      public static function getToltalByCategory($id_lang = null,$id_category = null){
  179.         if($id_lang == null){
  180.                     $id_lang = (int)Context::getContext()->language->id;
  181.                 }
  182.         if($id_category == null){
  183.                     $id_category = 1;
  184.                 }
  185.         $sql = 'SELECT * FROM '._DB_PREFIX_.'smart_blog_post p INNER JOIN
  186.                '._DB_PREFIX_.'smart_blog_post_lang pl ON p.id_smart_blog_post=pl.id_smart_blog_post INNER JOIN
  187.                '._DB_PREFIX_.'smart_blog_post_shop ps ON pl.id_smart_blog_post = ps.id_smart_blog_post AND ps.id_shop = '.(int) Context::getContext()->shop->id.'
  188.                WHERE pl.id_lang='.(int)$id_lang.'
  189.                AND p.active= 1 AND p.id_category = '.(int)$id_category;
  190.         if (!$posts = Db::getInstance()->executeS($sql))
  191.       return false;
  192.         return count($posts);
  193.     }
  194.    
  195.     public static function addTags($id_lang = null, $id_post, $tag_list, $separator = ','){
  196.                 if($id_lang == null){
  197.                             $id_lang = (int)Context::getContext()->language->id;
  198.                         }
  199.         if (!Validate::isUnsignedId($id_lang))
  200.             return false;
  201.  
  202.         if (!is_array($tag_list))
  203.             $tag_list = array_filter(array_unique(array_map('trim', preg_split('#\\'.$separator.'#', $tag_list, null, PREG_SPLIT_NO_EMPTY))));
  204.  
  205.         $list = array();
  206.         if (is_array($tag_list))
  207.             foreach ($tag_list as $tag)
  208.             {
  209.                               $id_tag = BlogTag::TagExists($tag,(int)$id_lang);
  210.                                 if(!$id_tag)
  211.                                  {
  212.                                        $tag_obj = new BlogTag(null, $tag, (int)$id_lang);
  213.                                         if (!Validate::isLoadedObject($tag_obj))
  214.                                         {
  215.                                                 $tag_obj->name = $tag;
  216.                                                 $tag_obj->id_lang = (int)$id_lang;
  217.                                                 $tag_obj->add();
  218.                                         }
  219.                                         if (!in_array($tag_obj->id, $list))
  220.                                                 $list[] = $tag_obj->id;
  221.                                 }
  222.                                 else
  223.                                 {
  224.                                          if (!in_array($id_tag, $list))
  225.                                             $list[] = $id_tag;
  226.                                 }
  227.                
  228.             }
  229.         $data = '';
  230.         foreach ($list as $tag)
  231.             $data .= '('.(int)$tag.','.(int)$id_post.'),';
  232.         $data = rtrim($data, ',');
  233.                
  234.         return Db::getInstance()->execute('
  235.         INSERT INTO `'._DB_PREFIX_.'smart_blog_post_tag` (`id_tag`, `id_post`)
  236.         VALUES '.$data);
  237.     }
  238.        
  239.     public function add($autodate = true, $null_values = false){
  240.         if (!parent::add($autodate, $null_values))
  241.             return false;
  242.         else if (isset($_POST['products']))
  243.             return $this->setProducts(Tools::getValue('products'));
  244.         return true;
  245.     }
  246.        
  247.     public static function postViewed($id_post){
  248.        
  249.         $sql = 'UPDATE '._DB_PREFIX_.'smart_blog_post as p SET p.viewed = (p.viewed+1) where p.id_smart_blog_post = '.(int)$id_post;
  250.        
  251.         return Db::getInstance()->execute($sql);
  252.    
  253.     return true;
  254.     }
  255.        
  256.     public function setProducts($array){
  257.         $result = Db::getInstance()->execute('DELETE FROM '._DB_PREFIX_.'smart_blog_post_tag WHERE id_tag = '.(int)$this->id);
  258.         if (is_array($array))
  259.         {
  260.             $array = array_map('intval', $array);
  261.             $result &= ObjectModel::updateMultishopTable('smart_blog_post_tag', array('indexed' => 0), 'a.id_post IN ('.implode(',', $array).')');
  262.             $ids = array();
  263.             foreach ($array as $id_post)
  264.                 $ids[] = '('.(int)$id_post.','.(int)$this->id.')';
  265.  
  266.             if ($result)
  267.             {
  268.                 $result &= Db::getInstance()->execute('INSERT INTO '._DB_PREFIX_.'smart_blog_post_tag (id_post, id_tag) VALUES '.implode(',', $ids));
  269.                 if (Configuration::get('PS_SEARCH_INDEXATION'))
  270.                     $result &= Search::indexation(false);
  271.             }
  272.         }
  273.         return $result;
  274.     }
  275.        
  276.     public static function deleteTagsForProduct($id_post){
  277.         return Db::getInstance()->execute('DELETE FROM `'._DB_PREFIX_.'smart_blog_post_tag` WHERE `id_post` = '.(int)$id_post);
  278.     }
  279.        
  280.     public static function getProductTags($id_post){
  281.                 $id_lang = (int)Context::getContext()->language->id;
  282.         if (!$tmp = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
  283.         SELECT  t.`name`
  284.         FROM '._DB_PREFIX_.'smart_blog_tag t
  285.         LEFT JOIN '._DB_PREFIX_.'smart_blog_post_tag pt ON (pt.id_tag = t.id_tag AND t.id_lang = '.(int)$id_lang.')
  286.         WHERE pt.`id_post`='.(int)$id_post))
  287.             return false;
  288.         return $tmp;
  289.     }
  290.        
  291.     public static function getProductTagsBylang($id_post,$id_lang = null) {
  292.                 if($id_lang == null){
  293.                             $id_lang = (int)Context::getContext()->language->id;
  294.                         }
  295.                  $tags = '';
  296.                     if (!$tmp = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
  297.                    SELECT  t.`name`
  298.                    FROM '._DB_PREFIX_.'smart_blog_tag t
  299.                    LEFT JOIN '._DB_PREFIX_.'smart_blog_post_tag pt ON (pt.id_tag = t.id_tag AND t.id_lang = '.(int)$id_lang.')
  300.                    WHERE pt.`id_post`='.(int)$id_post))
  301.                             return false;
  302.                         $i = 1;
  303.                          foreach ($tmp as $val) {
  304.                              if($i>=count($tmp)){
  305.                                 $tags .= $val['name'];
  306.                              }else{
  307.                                  $tags .= $val['name'].',';
  308.                              }
  309.                              $i++;
  310.                          }
  311.                  return $tags;
  312.             }
  313.            
  314.     public static function getPopularPosts($id_lang= null){
  315.                 if($id_lang == null){
  316.                     $id_lang = (int)Context::getContext()->language->id;
  317.                 }
  318.                 if(Configuration::get('smartshowpopularpost') != '' && Configuration::get('smartshowpopularpost') != null){
  319.                      $limit = Configuration::get('smartshowpopularpost');
  320.                 }else{
  321.                     $limit = 5;
  322.                 }
  323.                     $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('SELECT p.viewed ,p.created , p.id_smart_blog_post,pl.meta_title,pl.link_rewrite FROM '._DB_PREFIX_.'smart_blog_post p INNER JOIN
  324.                    '._DB_PREFIX_.'smart_blog_post_lang pl ON p.id_smart_blog_post=pl.id_smart_blog_post INNER JOIN
  325.                    '._DB_PREFIX_.'smart_blog_post_shop ps ON pl.id_smart_blog_post = ps.id_smart_blog_post AND ps.id_shop = '.(int) Context::getContext()->shop->id.'
  326.                    WHERE pl.id_lang='.(int)$id_lang.' AND p.active = 1 ORDER BY p.viewed DESC LIMIT 0,'.(int)$limit);
  327.    
  328.         return $result;
  329.       }
  330.      
  331.     public static function getRelatedPosts($id_lang = null,$id_cat = null,$id_post= null){
  332.             if($id_lang == null){
  333.                         $id_lang = (int)Context::getContext()->language->id;
  334.                     }
  335.          if(Configuration::get('smartshowrelatedpost') != '' && Configuration::get('smartshowrelatedpost') != null){
  336.                      $limit = Configuration::get('smartshowrelatedpost');
  337.                 }else{
  338.                     $limit = 5;
  339.                 }
  340.                 if($id_cat == null){
  341.                     $id_cat = 1;
  342.                 }
  343.                 if($id_post == null){
  344.                     $id_post = 1;
  345.                 }
  346.             $sql = 'SELECT  p.id_smart_blog_post,p.created,pl.meta_title,pl.link_rewrite FROM '._DB_PREFIX_.'smart_blog_post p INNER JOIN
  347.                '._DB_PREFIX_.'smart_blog_post_lang pl ON p.id_smart_blog_post=pl.id_smart_blog_post INNER JOIN
  348.                '._DB_PREFIX_.'smart_blog_post_shop ps ON pl.id_smart_blog_post = ps.id_smart_blog_post AND ps.id_shop = '.(int) Context::getContext()->shop->id.'
  349.                WHERE pl.id_lang='.(int)$id_lang.'  AND p.active = 1 AND p.id_category = '.(int)$id_cat.' AND p.id_smart_blog_post != '.(int)$id_post.' ORDER BY p.id_smart_blog_post DESC LIMIT 0,'.(int)$limit;
  350.  
  351.         if (!$posts = Db::getInstance()->executeS($sql))
  352.             return false;
  353.       return $posts;
  354.         }
  355.        
  356.     public static function getRecentPosts($id_lang = null){
  357.             if($id_lang == null){
  358.                     $id_lang = (int)Context::getContext()->language->id;
  359.                 }
  360.                 if(Configuration::get('smartshowrecentpost') != '' && Configuration::get('smartshowrecentpost') != null){
  361.                      $limit = Configuration::get('smartshowrecentpost');
  362.                 }else{
  363.                     $limit = 5;
  364.                 }
  365.                
  366.         $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('SELECT  p.id_smart_blog_post,p.created,pl.meta_title,pl.link_rewrite FROM '._DB_PREFIX_.'smart_blog_post p INNER JOIN
  367.                '._DB_PREFIX_.'smart_blog_post_lang pl ON p.id_smart_blog_post=pl.id_smart_blog_post INNER JOIN
  368.                '._DB_PREFIX_.'smart_blog_post_shop ps ON pl.id_smart_blog_post = ps.id_smart_blog_post AND ps.id_shop = '.(int) Context::getContext()->shop->id.'
  369.                WHERE pl.id_lang='.(int)$id_lang.'  AND p.active = 1 ORDER BY p.id_smart_blog_post DESC LIMIT 0,'.(int)$limit);
  370.        
  371.         return $result;
  372.         }
  373.    
  374.     public static function tagsPost($tags, $id_lang = null){
  375.         if($id_lang == null)
  376.             $id_lang = (int)Context::getContext()->language->id;
  377.        
  378.         $sql = 'SELECT * FROM '._DB_PREFIX_.'smart_blog_post p INNER JOIN
  379.                '._DB_PREFIX_.'smart_blog_post_lang pl ON p.id_smart_blog_post=pl.id_smart_blog_post INNER JOIN
  380.                '._DB_PREFIX_.'smart_blog_post_shop ps ON p.id_smart_blog_post=ps.id_smart_blog_post  AND  ps.id_shop = '.(int) Context::getContext()->shop->id.' INNER JOIN
  381.                '._DB_PREFIX_.'smart_blog_post_tag pt ON pl.id_smart_blog_post = pt.id_post INNER JOIN
  382.                '._DB_PREFIX_.'smart_blog_tag t ON pt.id_tag=t.id_tag
  383.                 WHERE pl.id_lang='.(int)$id_lang.'  AND p.active = 1           
  384.                AND t.name="'.pSql($tags).'"';
  385.        
  386.         if (!$posts = Db::getInstance()->executeS($sql))
  387.             return false;
  388.        
  389.                 $BlogCategory = new BlogCategory();
  390.             $i = 0;
  391.             foreach($posts as $post){
  392.                 $result[$i]['id_post'] = $post['id_smart_blog_post'];
  393.                 $result[$i]['viewed'] = $post['viewed'];
  394.                 $result[$i]['is_featured'] = $post['is_featured'];
  395.                 $result[$i]['meta_title'] = $post['meta_title'];
  396.                 $result[$i]['short_description'] = $post['short_description'];
  397.                 $result[$i]['meta_description'] = $post['meta_description'];
  398.                 $result[$i]['content'] = $post['content'];
  399.                 $result[$i]['meta_keyword'] = $post['meta_keyword'];
  400.                 $result[$i]['id_category'] = $post['id_category'];
  401.                 $result[$i]['link_rewrite'] = $post['link_rewrite'];
  402.                 $result[$i]['cat_name'] = $BlogCategory->getCatName($post['id_category']);
  403.                 $result[$i]['cat_link_rewrite'] = $BlogCategory->getCatLinkRewrite($post['id_category']);
  404.                 $employee = new  Employee( $post['id_author']);
  405.                
  406.                 $result[$i]['lastname'] = $employee->lastname;
  407.                 $result[$i]['firstname'] = $employee->firstname;
  408.                  if (file_exists(_PS_MODULE_DIR_.'smartblog/images/' . $post['id_smart_blog_post'] . '.jpg') )
  409.                 {
  410.                    $image =   $post['id_smart_blog_post'];
  411.                    $result[$i]['post_img'] = $image;
  412.         }
  413.                 else
  414.                 {
  415.                    $result[$i]['post_img'] ='no';
  416.                 }
  417.                 $result[$i]['created'] = $post['created'];
  418.                 $i++;
  419.             }
  420.         return $result;
  421.     }
  422.    
  423.     public static function getArchiveResult($month = null,$year = null,$limit_start = 0, $limit = 5){
  424.          $BlogCategory = '';
  425.          $result = array();
  426.          $id_lang = (int)Context::getContext()->language->id;
  427.          if($month != '' and $month != NULL and $year != '' and $year != NULL)
  428.          {
  429.              $sql = 'SELECT * FROM '._DB_PREFIX_.'smart_blog_post s INNER JOIN '._DB_PREFIX_.'smart_blog_post_lang sl ON s.id_smart_blog_post = sl.id_smart_blog_post
  430.                 and sl.id_lang = '.(int)$id_lang.' INNER JOIN '._DB_PREFIX_.'smart_blog_post_shop ps ON ps.id_smart_blog_post = s.id_smart_blog_post AND ps.id_shop = '.(int) Context::getContext()->shop->id. '
  431.            where s.active = 1 and MONTH(s.created) = '.(int)$month.' AND YEAR(s.created) = '.(int)$year.' ORDER BY s.id_smart_blog_post DESC';
  432.          }elseif($month == '' and $month == NULL and $year != '' and $year != NULL)
  433.          {
  434.               $sql = 'SELECT * FROM '._DB_PREFIX_.'smart_blog_post s INNER JOIN '._DB_PREFIX_.'smart_blog_post_lang sl ON s.id_smart_blog_post = sl.id_smart_blog_post
  435.                 and sl.id_lang = '.(int)$id_lang.' INNER JOIN '._DB_PREFIX_.'smart_blog_post_shop ps ON ps.id_smart_blog_post = s.id_smart_blog_post AND ps.id_shop = '.(int) Context::getContext()->shop->id. '
  436.           where s.active = 1 AND YEAR(s.created) = '.(int)$year.' ORDER BY s.id_smart_blog_post DESC';
  437.              
  438.          }elseif($month != '' and $month != NULL and $year == '' and $year == NULL)
  439.          {
  440.                $sql = 'SELECT * FROM '._DB_PREFIX_.'smart_blog_post s INNER JOIN '._DB_PREFIX_.'smart_blog_post_lang sl ON s.id_smart_blog_post = sl.id_smart_blog_post
  441.                 and sl.id_lang = '.(int)$id_lang.' INNER JOIN '._DB_PREFIX_.'smart_blog_post_shop ps ON ps.id_smart_blog_post = s.id_smart_blog_post AND ps.id_shop = '.(int) Context::getContext()->shop->id. '
  442.           where s.active = 1 AND   MONTH(s.created) = '.(int)$month.'  ORDER BY s.id_smart_blog_post DESC';
  443.                
  444.          }else{
  445.              $sql = 'SELECT * FROM '._DB_PREFIX_.'smart_blog_post s INNER JOIN '._DB_PREFIX_.'smart_blog_post_lang sl ON s.id_smart_blog_post = sl.id_smart_blog_post
  446.                 and sl.id_lang = '.(int)$id_lang.' INNER JOIN '._DB_PREFIX_.'smart_blog_post_shop ps ON ps.id_smart_blog_post = s.id_smart_blog_post AND ps.id_shop = '.(int) Context::getContext()->shop->id. '
  447.            where s.active = 1 ORDER BY s.id_smart_blog_post DESC';
  448.          }
  449.         if (!$posts = Db::getInstance()->executeS($sql))
  450.             return false;
  451.        
  452.                 $BlogCategory = new BlogCategory();
  453.             $i = 0;
  454.             foreach($posts as $post){
  455.                 $result[$i]['id_post'] = $post['id_smart_blog_post'];
  456.                 $result[$i]['viewed'] = $post['viewed'];
  457.                 $result[$i]['is_featured'] = $post['is_featured'];
  458.                 $result[$i]['meta_title'] = $post['meta_title'];
  459.                 $result[$i]['short_description'] = $post['short_description'];
  460.                 $result[$i]['meta_description'] = $post['meta_description'];
  461.                 $result[$i]['content'] = $post['content'];
  462.                 $result[$i]['meta_keyword'] = $post['meta_keyword'];
  463.                 $result[$i]['id_category'] = $post['id_category'];
  464.                 $result[$i]['link_rewrite'] = $post['link_rewrite'];
  465.                 $result[$i]['cat_name'] = $BlogCategory->getCatName($post['id_category']);
  466.                 $result[$i]['cat_link_rewrite'] = $BlogCategory->getCatLinkRewrite($post['id_category']);
  467.                 $employee = new  Employee( $post['id_author']);
  468.                
  469.                 $result[$i]['lastname'] = $employee->lastname;
  470.                 $result[$i]['firstname'] = $employee->firstname;
  471.                  if (file_exists(_PS_MODULE_DIR_.'smartblog/images/' . $post['id_smart_blog_post'] . '.jpg') )
  472.                 {
  473.                    $image =   $post['id_smart_blog_post'];
  474.                    $result[$i]['post_img'] = $image;
  475.         }
  476.                 else
  477.                 {
  478.                    $result[$i]['post_img'] ='no';
  479.                 }
  480.                 $result[$i]['created'] = $post['created'];
  481.                 $i++;
  482.             }
  483.         return $result;
  484.     }
  485.    
  486.     public static function getArchiveD($month,$year){
  487.  
  488.         $sql = 'SELECT DAY(p.created) as day FROM '._DB_PREFIX_.'smart_blog_post p INNER JOIN '._DB_PREFIX_.'smart_blog_post_shop ps ON p.id_smart_blog_post = ps.id_smart_blog_post AND ps.id_shop = '.(int) Context::getContext()->shop->id.'
  489.                 where MONTH(p.created) = '.(int)$month.' AND YEAR(p.created) = '.(int)$year.' GROUP BY DAY(p.created)';
  490.  
  491.         if (!$posts = Db::getInstance()->executeS($sql))
  492.             return false;
  493.                    
  494.         return $posts;
  495.        
  496.     }
  497.    
  498.     public static function getArchiveM($year){
  499.          
  500.         $sql = 'SELECT MONTH(p.created) as month FROM '._DB_PREFIX_.'smart_blog_post p  INNER JOIN '._DB_PREFIX_.'smart_blog_post_shop ps ON p.id_smart_blog_post = ps.id_smart_blog_post AND ps.id_shop = '.(int) Context::getContext()->shop->id.'
  501.                 where YEAR(p.created) = '.(int)$year.' GROUP BY MONTH(p.created)';
  502.        
  503.         if (!$posts = Db::getInstance()->executeS($sql))
  504.             return false;
  505.         return $posts;
  506.        
  507.     }
  508.    
  509.     public static function getArchive(){
  510.          $result = array();
  511.         $sql = 'SELECT YEAR(p.created) as year FROM '._DB_PREFIX_.'smart_blog_post p INNER JOIN '._DB_PREFIX_.'smart_blog_post_shop ps ON p.id_smart_blog_post = ps.id_smart_blog_post AND ps.id_shop = '.(int) Context::getContext()->shop->id.'
  512.                GROUP BY YEAR(p.created)';
  513.        
  514.         if (!$posts = Db::getInstance()->executeS($sql))
  515.             return false;
  516.                         $i = 0;
  517.                     foreach ($posts as $value) {
  518.                        $result[$i]['year'] = $value['year'];
  519.                        $result[$i]['month'] = SmartBlogPost::getArchiveM($value['year']);
  520.                        $months = SmartBlogPost::getArchiveM($value['year']);
  521.                        $j = 0;
  522.                        foreach ($months as $month) {
  523.                                $result[$i]['month'][$j]['day'] = SmartBlogPost::getArchiveD($month['month'],$value['year']);
  524.                                $j++;
  525.                         }
  526.                        $i++;
  527.                     }
  528.         return $result;
  529.     }
  530.    
  531.     public static function SmartBlogSearchPost($keyword = NULL, $id_lang = NULL,$limit_start = 0, $limit = 5){
  532.           if($keyword == NULL)
  533.               return false;
  534.           if($id_lang == NULL)
  535.               $id_lang = (int)Context::getContext()->language->id;
  536.          
  537.         $sql = 'SELECT * FROM '._DB_PREFIX_.'smart_blog_post_lang pl, '._DB_PREFIX_.'smart_blog_post p
  538.                WHERE pl.id_lang='.(int)$id_lang.'  AND p.active = 1
  539.                AND pl.id_smart_blog_post=p.id_smart_blog_post AND
  540.                (pl.meta_title LIKE \'%'.pSql($keyword).'%\' OR
  541.                 pl.meta_keyword LIKE \'%'.pSql($keyword).'%\' OR
  542.                 pl.meta_description LIKE \'%'.pSql($keyword).'%\' OR
  543.                 pl.content LIKE \'%'.pSql($keyword).'%\') ORDER BY p.id_smart_blog_post DESC';
  544.        if (!$posts = Db::getInstance()->executeS($sql))
  545.             return false;
  546.        
  547.                 $BlogCategory = new BlogCategory();
  548.             $i = 0;
  549.             foreach($posts as $post){
  550.                 $result[$i]['id_post'] = $post['id_smart_blog_post'];
  551.                 $result[$i]['viewed'] = $post['viewed'];
  552.                 $result[$i]['is_featured'] = $post['is_featured'];
  553.                 $result[$i]['meta_title'] = $post['meta_title'];
  554.                 $result[$i]['short_description'] = $post['short_description'];
  555.                 $result[$i]['meta_description'] = $post['meta_description'];
  556.                 $result[$i]['content'] = $post['content'];
  557.                 $result[$i]['meta_keyword'] = $post['meta_keyword'];
  558.                 $result[$i]['id_category'] = $post['id_category'];
  559.                 $result[$i]['link_rewrite'] = $post['link_rewrite'];
  560.                 $result[$i]['cat_name'] = $BlogCategory->getCatName($post['id_category']);
  561.                 $result[$i]['cat_link_rewrite'] = $BlogCategory->getCatLinkRewrite($post['id_category']);
  562.                 $employee = new  Employee( $post['id_author']);
  563.                
  564.                 $result[$i]['lastname'] = $employee->lastname;
  565.                 $result[$i]['firstname'] = $employee->firstname;
  566.                  if (file_exists(_PS_MODULE_DIR_.'smartblog/images/' . $post['id_smart_blog_post'] . '.jpg') )
  567.                 {
  568.                    $image =   $post['id_smart_blog_post'];
  569.                    $result[$i]['post_img'] = $image;
  570.         }
  571.                 else
  572.                 {
  573.                    $result[$i]['post_img'] ='no';
  574.                 }
  575.                 $result[$i]['created'] = $post['created'];
  576.                 $i++;
  577.             }
  578.         return $result;
  579.     }
  580.    
  581.     public static function SmartBlogSearchPostCount($keyword = NULL, $id_lang = NULL){
  582.           if($keyword == NULL)
  583.               return false;
  584.           if($id_lang == NULL)
  585.               $id_lang = (int)Context::getContext()->language->id;
  586.          
  587.         $sql = 'SELECT * FROM '._DB_PREFIX_.'smart_blog_post_lang pl, '._DB_PREFIX_.'smart_blog_post p
  588.                WHERE pl.id_lang='.(int)$id_lang.'
  589.                AND pl.id_smart_blog_post=p.id_smart_blog_post AND p.active = 1 AND
  590.                (pl.meta_title LIKE \'%'.pSql($keyword).'%\' OR
  591.                 pl.meta_keyword LIKE \'%'.pSql($keyword).'%\' OR
  592.                 pl.meta_description LIKE \'%'.pSql($keyword).'%\' OR
  593.                 pl.content LIKE \'%'.pSql($keyword).'%\') ORDER BY p.id_smart_blog_post DESC';
  594.        if (!$posts = Db::getInstance()->executeS($sql))
  595.             return false;
  596.         return count($posts);
  597.     }
  598.    
  599.     public static function getBlogImage(){
  600.          
  601.          $id_lang = (int)Context::getContext()->language->id;
  602.            
  603.                 $sql = 'SELECT id_smart_blog_post FROM '._DB_PREFIX_.'smart_blog_post';
  604.                
  605.                if (!$result = Db::getInstance()->executeS($sql))
  606.                                return false;
  607.                return $result;
  608.            }
  609.            
  610.     public static function GetPostSlugById($id_post,$id_lang = null){
  611.           if($id_lang == null)
  612.               $id_lang = (int)Context::getContext()->language->id;
  613.          
  614.         $sql = 'SELECT * FROM '._DB_PREFIX_.'smart_blog_post p INNER JOIN
  615.                '._DB_PREFIX_.'smart_blog_post_lang pl ON p.id_smart_blog_post=pl.id_smart_blog_post INNER JOIN
  616.                '._DB_PREFIX_.'smart_blog_post_shop ps ON pl.id_smart_blog_post = ps.id_smart_blog_post
  617.                WHERE pl.id_lang='.(int)$id_lang.'
  618.                AND p.active= 1 AND p.id_smart_blog_post = '.(int)$id_post;
  619.  
  620.         if (!$post = Db::getInstance()->executeS($sql))
  621.             return false;
  622.        
  623.         return $post[0]['link_rewrite'];
  624.     }
  625.    
  626.     public static function GetPostMetaByPost($id_post,$id_lang = null){
  627.           if($id_lang == null)
  628.               $id_lang = (int)Context::getContext()->language->id;
  629.          
  630.         $sql = 'SELECT * FROM '._DB_PREFIX_.'smart_blog_post p INNER JOIN
  631.                '._DB_PREFIX_.'smart_blog_post_lang pl ON p.id_smart_blog_post=pl.id_smart_blog_post INNER JOIN
  632.                '._DB_PREFIX_.'smart_blog_post_shop ps ON pl.id_smart_blog_post = ps.id_smart_blog_post
  633.                WHERE pl.id_lang='.(int)$id_lang.'
  634.                AND p.active= 1 AND p.id_smart_blog_post = '.(int)$id_post;
  635.  
  636.         if (!$post = Db::getInstance()->executeS($sql))
  637.             return false;
  638.        
  639.                             if($post[0]['meta_title'] == '' && $post[0]['meta_title'] == NULL){
  640.                                 $meta['meta_title'] = Configuration::get('smartblogmetatitle');
  641.                             }else{
  642.                                 $meta['meta_title'] = $post[0]['meta_title'];
  643.                             }
  644.                            
  645.                             if($post[0]['meta_description'] == '' && $post[0]['meta_description'] == NULL){
  646.                                $meta['meta_description'] = Configuration::get('smartblogmetadescrip');
  647.                             }else{
  648.                                 $meta['meta_description'] = $post[0]['meta_description'];
  649.                             }
  650.                            
  651.                             if($post[0]['meta_keyword'] == '' && $post[0]['meta_keyword'] == NULL){
  652.                                $meta['meta_keywords'] = Configuration::get('smartblogmetakeyword');
  653.                             }else{
  654.                                 $meta['meta_keywords'] = $post[0]['meta_keyword'];
  655.                             }
  656.         return $meta;
  657.     }
  658.    
  659.         public static function GetPostLatestHome($limit)
  660.             {
  661.                 if($limit == '' && $limit == null)
  662.                     $limit = 3;
  663.             $id_lang = (int)Context::getContext()->language->id;
  664.             $id_lang_defaut = Configuration::get('PS_LANG_DEFAULT');
  665.             $blogcomment = new Blogcomment();
  666.             $result = array();
  667.             $sql = 'SELECT * FROM '._DB_PREFIX_.'smart_blog_post p INNER JOIN
  668.                '._DB_PREFIX_.'smart_blog_post_lang pl ON p.id_smart_blog_post=pl.id_smart_blog_post INNER JOIN
  669.                '._DB_PREFIX_.'smart_blog_post_shop ps ON pl.id_smart_blog_post = ps.id_smart_blog_post AND ps.id_shop = '.(int) Context::getContext()->shop->id.'
  670.                 WHERE pl.id_lang='.(int)$id_lang.'     
  671.                AND p.active= 1 ORDER BY p.id_smart_blog_post DESC
  672.                LIMIT '.$limit;
  673.             $posts = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
  674.             if(empty($posts)){
  675.                 $sql2 = 'SELECT * FROM '._DB_PREFIX_.'smart_blog_post p INNER JOIN
  676.                '._DB_PREFIX_.'smart_blog_post_lang pl ON p.id_smart_blog_post=pl.id_smart_blog_post INNER JOIN
  677.                '._DB_PREFIX_.'smart_blog_post_shop ps ON pl.id_smart_blog_post = ps.id_smart_blog_post  AND ps.id_shop = '.(int) Context::getContext()->shop->id.'
  678.                 WHERE pl.id_lang='.(int)$id_lang_defaut.'      
  679.                AND p.active= 1 ORDER BY p.id_smart_blog_post DESC
  680.                LIMIT '.$limit;
  681.                 $posts = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql2);      
  682.             }
  683.              $i = 0;
  684.             foreach($posts as $post){
  685.                 $result[$i]['id'] = $post['id_smart_blog_post'];
  686.                 $result[$i]['title'] = $post['meta_title'];
  687.                 $result[$i]['meta_description'] = strip_tags($post['meta_description']);
  688.                 $result[$i]['short_description'] = strip_tags($post['short_description']);
  689.                 $result[$i]['content'] = strip_tags($post['content']);
  690.                 $result[$i]['category'] = $post['id_category'];
  691.                 $result[$i]['date_added'] = $post['created'];
  692.                 $result[$i]['viewed'] = $post['viewed'];
  693.                 $result[$i]['is_featured'] = $post['is_featured'];
  694.                 $result[$i]['link_rewrite'] = $post['link_rewrite'];
  695.                 $result[$i]['countcomment'] = $blogcomment->getToltalComment($post['id_smart_blog_post']);
  696.                 if (file_exists(_PS_MODULE_DIR_.'smartblog/images/' . $post['id_smart_blog_post'] . '.jpg'))
  697.                     {
  698.                        $image =   $post['id_smart_blog_post'];
  699.                        $result[$i]['post_img'] = $image;
  700.                     }
  701.                     else
  702.                     {
  703.                        $result[$i]['post_img'] = 'no';
  704.                     }
  705.                 $i++;
  706.             }
  707.             return $result;
  708.             }
  709. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement