Rewrite SQL

This commit is contained in:
Michael RICOIS 2017-11-20 17:35:23 +01:00
parent b996521fcb
commit abee1a1059
3 changed files with 119 additions and 82 deletions

View File

@ -308,6 +308,9 @@ class GuideCategory extends ObjectModel
WHERE `id_guide_category` = '.intval($id_guide_category);
return Db::getInstance()->getValue($sql);
}
public function findByCategoryFamily($id, $id_lang)
{
@ -328,23 +331,83 @@ class GuideCategory extends ObjectModel
return '';
}
/**
* Get Parent category
* @param int $id_category
* @param int $id_lang
* @return array
*/
public function getCategoryLang($id_category, $id_lang)
{
$sql = "SELECT gc.*, gcl.name, gcl.link_rewrite
FROM `"._DB_PREFIX_."guide_category` gc, `"._DB_PREFIX_."guide_category_lang` gcl
WHERE gc.id_guide_category=gcl.id_guide_category AND gcl.id_lang=".$id_lang.
" AND gc.id_guide_category=".$id_category;
$result = Db::getInstance()->getRow($sql);
return $result;
}
public function getAutoCategoryLang($id_category, $id_lang)
{
$sql = "SELECT * FROM ps_guide_category gc, ps_guide_category_lang gcl
WHERE gc.active=1 AND gc.id_guide_category=gcl.id_guide_category
AND gc.id_parent=".$id_category." AND gcl.id_lang=".$id_lang.
" ORDER BY position ASC";
$result = Db::getInstance()->getRow($sql);
return $result;
}
/**
* Gest All main categories
* @param int $id_lang
* @return array
*/
public static function getRoot($id_lang, $active = 1)
{
$sql = "SELECT gc.id_guide_category, gcl.name, gcl.link_rewrite, gcl.meta_title,
gcl.meta_description, gcl.meta_keywords
FROM ps_guide_category gc, ps_guide_category_lang gcl
WHERE ".($active==1?' gc.active=1 ':' 1 ')." AND gc.id_guide_category=gcl.id_guide_category
AND gc.id_parent=0 AND gcl.id_lang=".$id_lang;
$result = Db::getInstance()->ExecuteS($sql);
return $result;
}
/**
* Get category with children
* @param int $id_lang
* @param int $active
* @return array
*/
public function getTree($id_lang, $active = 1)
{
return $this->recurseData(0, $id_lang, $active);
}
/**
* Recurse data
* @param int $id_category
* @param int $id_lang
* @param int $active
* @return array
*/
protected function recurseData($id_category, $id_lang, $active = 1)
{
$data = array();
$sql = "SELECT gc.*, gcl.name, gcl.link_rewrite FROM `"._DB_PREFIX_."guide_category` gc, `"._DB_PREFIX_."guide_category_lang` gcl
$sql = "SELECT gc.*, gcl.name, gcl.link_rewrite
FROM `"._DB_PREFIX_."guide_category` gc, `"._DB_PREFIX_."guide_category_lang` gcl
WHERE gc.id_guide_category=gcl.id_guide_category AND gcl.id_lang=".$id_lang.
" AND gc.id_parent=".$id_category." ORDER BY position ASC";
($active==1?' AND gc.active=1 ':'')." AND gc.id_parent=".$id_category.
" ORDER BY position ASC";
$result = Db::getInstance()->ExecuteS($sql);
if (count($result) > 0) {
foreach ($result as $c) {
$rewrite = 'guide/';
if ($c['id_parent'] != 0) {
$parent = $this->getParent($c['id_parent'], $id_lang);
$parent = $this->getCategoryLang($c['id_parent'], $id_lang);
$rewrite.= $parent['link_rewrite'].'/';
}
$rewrite.= $c['id_guide_category'].'-'.$c['link_rewrite'];
@ -361,38 +424,4 @@ class GuideCategory extends ObjectModel
return $data;
}
/**
* Get Parent category
* @param int $id_category
* @param int $id_lang
* @return array
*/
public function getParent($id_category, $id_lang)
{
$sql = "SELECT gc.*, gcl.name, gcl.link_rewrite
FROM `"._DB_PREFIX_."guide_category` gc, `"._DB_PREFIX_."guide_category_lang` gcl
WHERE gc.id_guide_category=gcl.id_guide_category AND gcl.id_lang=".$id_lang.
" AND gc.id_guide_category=".$id_category;
$result = Db::getInstance()->getRow($sql);
return $result;
}
/**
* Gest All main categories
* @param int $id_lang
* @return array
*/
public static function getRoot($id_lang)
{
$sql = "SELECT gc.id_guide_category, gcl.name, gcl.link_rewrite, gcl.meta_title,
gcl.meta_description, gcl.meta_keywords
FROM ps_guide_category gc, ps_guide_category_lang gcl
WHERE gc.active=1 AND gc.id_guide_category=gcl.id_guide_category
AND gc.id_parent=0 AND gcl.id_lang=".$id_lang;
$result = Db::getInstance()->ExecuteS($sql);
return $result;
}
}

View File

@ -179,11 +179,33 @@ class GuidePost extends ObjectModel
public function getUrlRewriteFull(){}
public function getCategory()
/**
* Get Post With one language
* @param int $id_category
* @param int $id_lang
* @return array
*/
public function getPostLang($id_post, $id_lang, $id_category = null)
{
$sql = "SELECT * FROM ps_guide_post gp, ps_guide_post_lang gpl
WHERE gp.active=1 AND gp.id_guide_post=gpl.id_guide_post
".($id_category===null?"":" AND gp.id_guide_category=".$id_category).
"AND gp.id_guide_post=".$id_post." AND gpl.id_lang=".$id_lang;
$result = Db::getInstance()->getRow($sql);
return $result;
}
public function getAutoPostLang($id_category, $id_lang)
{
$sql = "SELECT * FROM ps_guide_post gp, ps_guide_post_lang gpl
WHERE gp.active=1 AND gp.id_guide_post=gpl.id_guide_post
AND gp.id_guide_category=".$id_category.
" AND gpl.id_lang=".$id_lang." ORDER BY position ASC";
$result = Db::getInstance()->getRow($sql);
return $result;
}
public function findByCategory($id_category, $id_lang)

View File

@ -16,12 +16,12 @@ class PostController extends FrontController
$id_post = Tools::getValue('pid', 0);
$content = null;
// @todo, si on vient de l'admin, voir les post ???
// Start form post
$postModel = new GuidePost();
if ($id_post != 0) {
$sql = "SELECT * FROM ps_guide_post gp, ps_guide_post_lang gpl
WHERE gp.active=1 AND gp.id_guide_post=gpl.id_guide_post
AND gp.id_guide_post=".$id_post." AND gpl.id_lang=".$id_lang;
$content = Db::getInstance()->getRow($sql);
$content = $postModel->getPostLang($id_post, $id_lang);
$id_category = $content['id_guide_category'];
}
@ -31,10 +31,8 @@ class PostController extends FrontController
}
else {
// Category
$sql = "SELECT * FROM ps_guide_category gc, ps_guide_category_lang gcl
WHERE gc.active=1 AND gc.id_guide_category=gcl.id_guide_category
AND gc.id_guide_category=".$id_category." AND gcl.id_lang=".$id_lang;
$category = Db::getInstance()->getRow($sql);
$categoryModel = new GuideCategory();
$category = $categoryModel->getCategoryLang($id_category, $id_lang);
// Get Parent category
$isSubCategory = false;
@ -44,47 +42,28 @@ class PostController extends FrontController
if ($isSubCategory) {
// Get Parent category
$sql = "SELECT * FROM ps_guide_category gc, ps_guide_category_lang gcl
WHERE gc.active=1 AND gc.id_guide_category=gcl.id_guide_category
AND gc.id_guide_category=".$category['id_parent']." AND gcl.id_lang=".$id_lang;
$parentCategory = Db::getInstance()->getRow($sql);
$parentCategory = $categoryModel->getCategoryLang($category['id_parent'], $id_lang);
}
else {
$parentCategory = $category;
// Auto Select category
$sql = "SELECT * FROM ps_guide_category gc, ps_guide_category_lang gcl
WHERE gc.active=1 AND gc.id_guide_category=gcl.id_guide_category
AND gc.id_parent=".$id_category." AND gcl.id_lang=".$id_lang.
" ORDER BY position ASC";
$category = Db::getInstance()->getRow($sql);
$category = $categoryModel->getAutoCategoryLang($id_category, $id_lang);
}
}
// Post
if ($content === null) {
if ($id_post != 0) {
$sql = "SELECT * FROM ps_guide_post gp, ps_guide_post_lang gpl
WHERE gp.active=1 AND gp.id_guide_post=gpl.id_guide_post
AND gp.id_guide_category=".$category['id_guide_category'].
" AND gp.id_guide_post=".$id_post." AND gpl.id_lang=".$id_lang;
$content = Db::getInstance()->getRow($sql);
$content = $postModel->getPostLang($id_post, $id_lang, $category['id_guide_category']);
}
// Auto Select Post
else {
$sql = "SELECT * FROM ps_guide_post gp, ps_guide_post_lang gpl
WHERE gp.active=1 AND gp.id_guide_post=gpl.id_guide_post
AND gp.id_guide_category=".$category['id_guide_category'].
" AND gpl.id_lang=".$id_lang." ORDER BY position ASC";
$content = Db::getInstance()->getRow($sql);
$content = $postModel->getAutoPostLang($category['id_guide_category'], $id_lang);
}
}
// Liste des Posts
$sql = "SELECT * FROM ps_guide_post gp, ps_guide_post_lang gpl
WHERE gp.active=1 AND gp.id_guide_post=gpl.id_guide_post
AND gp.id_guide_category=".$category['id_guide_category'].
" AND gpl.id_lang=".$id_lang." ORDER BY position ASC";
$postList = Db::getInstance()->ExecuteS($sql);
$postList = $postModel->findByCategory($category['id_guide_category'], $id_lang);
$parentCategoryModel = new GuideCategory($parentCategory['id_guide_category']);
$parentCategoryLink = $parentCategoryModel->getLinkRewrite($id_lang);
@ -94,15 +73,22 @@ class PostController extends FrontController
$pipe = " > ";
$path = '<a href="'.Tools::getShopDomainSsl(true).'/guide">Guide</a>'.
'<span class="navigation-pipe">'.$pipe.'</span>'.
'<a href="'.Tools::getShopDomainSsl(true).'/guide/'.$parentCategoryLink.'">'
.htmlentities($parentCategoryModel->name[$id_lang], ENT_NOQUOTES, 'UTF-8').'</a>'.
'<span class="navigation-pipe">'.$pipe.'</span>'.
'<a href="'.Tools::getShopDomainSsl(true).'/guide/'.$parentCategoryModel->link_rewrite[$id_lang].'/'.$categoryLink.'">'.
htmlentities($categoryModel->name[$id_lang], ENT_NOQUOTES, 'UTF-8').'</a>'.
'<span class="navigation-pipe">'.$pipe.'</span>'.
'<span>'.$content['meta_title'].'</span>';
$path = '<a href="'.Tools::getShopDomainSsl(true).'/guide">Guide</a>';
if ($parentCategoryModel !== null) {
$path.= '<span class="navigation-pipe">'.$pipe.'</span>'.
'<a href="'.Tools::getShopDomainSsl(true).'/guide/'.$parentCategoryLink.'">'
.htmlentities($parentCategoryModel->name[$id_lang], ENT_NOQUOTES, 'UTF-8').'</a>';
}
if ($categoryModel !== null) {
$path.= '<span class="navigation-pipe">'.$pipe.'</span>'.
'<a href="'.Tools::getShopDomainSsl(true).'/guide/'.$parentCategoryModel->link_rewrite[$id_lang].'/'.$categoryLink.'">'.
htmlentities($categoryModel->name[$id_lang], ENT_NOQUOTES, 'UTF-8').'</a>';
$path.= '<span class="navigation-pipe">'.$pipe.'</span>'.
'<span>'.$content['meta_title'].'</span>';
}
self::$smarty->assign(array(
'path' => $path,