2016-02-19 15:30:29 +01:00

209 lines
7.4 KiB
PHP

<?php
class Question
{
public $id;
public $id_question;
public $id_section;
public $status = 0;
public $position;
public $title = array();
public $answer = array();
public $versions = array();
protected $fieldsRequired = array('id_section', 'answer');
public function __construct($id=NULL)
{
if($id !== NULL) {
$question = Question::getQuestion($id);
if($question) {
$this->id = $id;
$this->id_question = $question['id_question'];
$this->id_section = $question['id_section'];
$this->status = $question['status'];
$this->position = $question['position'];
$this->title = $question['title'];
$this->answer = $question['answer'];
/*$this->versions = $question['versions'];*/
}
}
}
public static function getQuestion($id) {
if(!($q = Db::getInstance()->ExecuteS('
SELECT *
FROM `'._DB_PREFIX_.'support_question`
WHERE `id_question` = '.(int) $id
)) || count($q) == 0) {
return false;
}
$result = array(
'id_question' => $q[0]['id_question'],
'id_section' => $q[0]['id_section'],
'status' => $q[0]['status'],
'position' => $q[0]['position'],
'title' => array(),
'answer' => array(),
);
$ql = Db::getInstance()->ExecuteS('
SELECT *
FROM `'._DB_PREFIX_.'support_question_lang`
WHERE `id_question` = '.(int) $id
);
foreach($ql as $l) {
$result['answer'][$l['id_lang']] = $l['answer'];
$result['title'][$l['id_lang']] = $l['title'];
}
/*foreach(Db::getInstance()->ExecuteS('
SELECT `version`
FROM `'._DB_PREFIX_.'support_section_site_version`
WHERE `id_section` = '.(int) $id
) as $version) {
$result['versions'][] = $version['version'];
}*/
return $result;
}
public static function getQuestionsByIdSection($id) {
if(!($questions = Db::getInstance()->ExecuteS('
SELECT *
FROM `'._DB_PREFIX_.'support_question`
WHERE `id_section` = '.(int) $id.'
ORDER BY `position`'
)) || count($questions) == 0) {
return false;
}
$result = array();
foreach($questions AS $question) {
$result[] = new Question($question['id_question']);
}
return $result;
/*$result = array();
$ids = array();
foreach ($questions as $q) {
$result[$q['id_question']] = array(
'id_question' => $q['id_question'],
'id_section' => $q['id_section'],
'status' => $q['status'],
'position' => $q['position'],
'title' => array(),
'answer' => array(),
);
$ids[] = $q['id_question'];
}
$ql = Db::getInstance()->ExecuteS('
SELECT *
FROM `'._DB_PREFIX_.'support_question_lang`
WHERE `id_question` IN ('.implode(',', $ids).')'
);
foreach($ql as $l) {
$result[$l['id_question']]['answer'][$l['id_lang']] = $l['answer'];
$result[$l['id_question']]['title'][$l['id_lang']] = $l['title'];
}*/
return $result;
}
public static function getQuestionIdsByIdSection($id) {
if(!($questions = Db::getInstance()->ExecuteS('
SELECT `id_question`
FROM `'._DB_PREFIX_.'support_question`
WHERE `id_section` = '.(int) $id
)) || count($questions) == 0) {
return false;
} else {
return $questions;
}
}
public function save() {
if($this->id !== null) {
Db::getInstance()->Execute('
UPDATE `'._DB_PREFIX_.'support_question` SET
`status` = '.(int) $this->status.',
`id_section` = '.(int) $this->id_section.',
`position` = '.(int) $this->position.'
WHERE `id_question` = '.(int) $this->id.'
');
Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'support_question_lang` WHERE `id_question` = '.(int) $this->id);
$question_i18n = array();
foreach($this->title as $k => $v) {
if(!isset($question_i18n[$k])) {
$question_i18n[$k] = array();
}
$question_i18n[$k]['title'] = $v;
}
foreach($this->answer as $k => $v) {
if(!isset($question_i18n[$k])) {
$question_i18n[$k] = array();
}
$question_i18n[$k]['answer'] = $v;
}
foreach($question_i18n as $lang => $values) {
Db::getInstance()->Execute('
INSERT INTO `'._DB_PREFIX_.'support_question_lang` VALUES (
'.$this->id.',
'.$lang.',
"'.(isset($values['title'])? pSQL($values['title']): '').'",
"'.(isset($values['answer'])? pSQL($values['answer'], true): '').'"
)
');
}
} else {
$sql_position = 'SELECT MAX(position) FROM '. _DB_PREFIX_.'support_question WHERE `id_section`='.(int) $this->id_section;
$position = Db::getInstance()->getValue($sql_position);
Db::getInstance()->Execute('
INSERT INTO `'._DB_PREFIX_.'support_question` VALUES (
DEFAULT,
'.(int) $this->id_section.',
'.(int) $this->status.',
'.($position+1).'
)
');
$this->id = Db::getInstance()->Insert_ID();
Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'support_question_lang` WHERE `id_question` = '.(int) $this->id);
$question_i18n = array();
foreach($this->title as $k => $v) {
if(!isset($question_i18n[$k])) {
$question_i18n[$k] = array();
}
$question_i18n[$k]['title'] = $v;
}
foreach($this->answer as $k => $v) {
if(!isset($question_i18n[$k])) {
$question_i18n[$k] = array();
}
$question_i18n[$k]['answer'] = $v;
}
foreach($question_i18n as $lang => $values) {
Db::getInstance()->Execute('
INSERT INTO `'._DB_PREFIX_.'support_question_lang` VALUES (
'.$this->id.',
'.$lang.',
"'.(isset($values['title'])? pSQL($values['title']): '').'",
"'.(isset($values['answer'])? pSQL($values['answer'], true): '').'"
)
');
}
}
}
public static function deleteQuestion($id) {
if($question = Question::getQuestion($id)) {
Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'support_question_lang` WHERE `id_question` = '.(int) $id);
Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'support_question` WHERE `id_question` = '.(int) $id);
}
}
}