120 lines
4.8 KiB
PHP
Raw Normal View History

2016-10-10 15:24:25 +02:00
<?php
class Question extends ObjectModel {
public $id;
public $id_question;
public $id_section;
public $position;
public $is_profil;
public $title;
public static $definition = array(
'table' => 'diag_question',
'primary' => 'id_question',
'multilang' => true,
'fields' => array(
'id_question' => array('type' => self::TYPE_INT),
'id_section' => array('type' => self::TYPE_INT),
'position' => array('type' => self::TYPE_INT),
'is_profil' => array('type' => self::TYPE_INT, 'validate' => 'isBool'),
'title' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isString'),
),
);
public static function getFinal($responses, $id_lang){
$final = array();
$complements = array();
$products = array();
$id_products = array();
$id_profil = 0;
if(isset($responses) && $responses!='false'){
$sqlQuestions = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
SELECT q.*
FROM '._DB_PREFIX_.'diag_question q
WHERE 1
');
foreach($sqlQuestions as $key => $q){
if($q['is_profil'] == 1){
$id_profil = (int)$responses[$q['id_question']];
$final['profil'] = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
SELECT p.`id_profil`, pl.`title`, pl.`description`
FROM `'._DB_PREFIX_.'diag_profil` p
LEFT JOIN `'._DB_PREFIX_.'diag_profil_lang` pl ON (p.`id_profil` = pl.`id_profil`)
WHERE p.`id_profil`='.(int)$responses[$q['id_question']].' AND pl.`id_lang`='.(int)$id_lang
);
break;
}
}
foreach($sqlQuestions as $key => $q){
if($q['is_profil'] == 1){
$products = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
SELECT p.`id_category_default`,p.`ean13`, p.`out_of_stock`, p.`id_product`, pl.`name`, pl.`description_short`, pl.`description`, pl.`link_rewrite`, pl.`video_1`
FROM `'._DB_PREFIX_.'product` p
LEFT JOIN `'._DB_PREFIX_.'diag_profil_product` pp ON (p.`id_product` = pp.`id_product`)
LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.`id_product` = pl.`id_product`)
WHERE pp.`id_profil`='.(int)$responses[$q['id_question']].' AND pl.`id_lang`='.(int)$id_lang
);
foreach ($products as $key => $value) {
$id_products[] = $value['id_product'];
}
}elseif(isset($responses[$q['id_question']])){
$complement = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
SELECT p.`id_category_default`,p.`ean13`, p.`out_of_stock`, p.`id_product`, pl.`name`, pl.`description_short`, pl.`description`, pl.`link_rewrite`, pl.`video_1`
FROM `'._DB_PREFIX_.'product` p
LEFT JOIN `'._DB_PREFIX_.'diag_profil_rep_prod` prp ON (p.`id_product` = prp.`id_product`)
LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.`id_product` = pl.`id_product`)
LEFT JOIN `'._DB_PREFIX_.'diag_response` r ON (r.`id_response` = prp.`id_response`)
WHERE prp.`id_response`='.(int)$responses[$q['id_question']].' AND r.`is_product`=1 AND prp.`id_profil`='.(int)$id_profil.' AND pl.`id_lang`='.(int)$id_lang
);
if($complement){
foreach ($complement as $key => $value) {
// pour eviter les doublons
if(!in_array($value['id_product'],$id_products, true))
$id_products[] = $value['id_product'];
else
unset($complement[$key]);
}
$complements = array_merge($complements, $complement);
}
}
}
$count_id = array_count_values($id_products);
foreach ($products as $row) {
$id_image = Product::getCover($row['id_product']);
$row['id_image'] = $id_image['id_image'];
$final['product'][] = Product::getProductProperties($id_lang, $row, Context::getContext());
}
foreach($complements as $key => $row){
// pour eviter les doublons
if($count_id[$row['id_product']] > 1){
unset($complements[$key]);
$count_id[$row['id_product']]--;
}
$id_image = Product::getCover($row['id_product']);
$row['id_image'] = $id_image['id_image'];
$final['complement'][] = Product::getProductProperties($id_lang, $row, Context::getContext());
}
$final['step'] = $responses['step'];
}
return $final;
}
public static function getQuestionsByThemePosition($step, $id_lang){
$questions = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
SELECT ql.`title`, q.`id_question`, q.`id_section`, q.`is_profil`, q.`position` as question_position, s.`id_section`, s.`position`, sl.`title` as sectionTitle
FROM '._DB_PREFIX_.'diag_question q
LEFT JOIN '._DB_PREFIX_.'diag_question_lang ql on (q.`id_question` = ql.`id_question`)
LEFT JOIN '._DB_PREFIX_.'diag_section s on (s.`id_section` = q.`id_section`)
LEFT JOIN '._DB_PREFIX_.'diag_section_lang sl on (sl.`id_section` = s.`id_section`)
WHERE ql.`id_lang` = '.(int)$id_lang.' AND (s.`position` = '.$step.') AND (sl.`id_lang` = '.$id_lang.')
ORDER BY q.`position` asc
');
return $questions;
}
}