bebeboutik/modules/ant_support/Section.php

203 lines
7.4 KiB
PHP
Raw Normal View History

<?php
include_once(_PS_ROOT_DIR_.'/modules/ant_support/Question.php');
class Section
{
public $id;
public $id_section;
public $status = 0;
public $position = 0;
public $title = array();
public $description = array();
public $versions = array();
public function __construct($id=NULL)
{
if($id !== NULL) {
$section = Section::getSection($id);
if($section) {
$this->id = $id;
$this->id_section = $section['id_section'];
$this->status = $section['status'];
$this->position = $section['position'];
$this->title = $section['title'];
$this->description = $section['description'];
/*$this->versions = $section['versions'];*/
}
}
}
public static function getSection($id) {
if(!($s = Db::getInstance()->ExecuteS('
SELECT *
FROM `'._DB_PREFIX_.'support_section`
WHERE `id_section` = '.(int) $id
)) || count($s) == 0) {
return false;
}
$result = array(
'id_section' => $s[0]['id_section'],
'status' => $s[0]['status'],
'position' => $s[0]['position'],
'title' => array(),
'description' => array(),
);
$sl = Db::getInstance()->ExecuteS('
SELECT *
FROM `'._DB_PREFIX_.'support_section_lang`
WHERE `id_section` = '.(int) $id
);
foreach($sl as $l) {
$result['description'][$l['id_lang']] = $l['description'];
$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 getSections($status=null, $lite=false, $order_by='`position` ASC', $limit=null) {
global $cookie;
$result = array();
$where = array();
$query = '
SELECT ss.`id_section`
FROM `'._DB_PREFIX_.'support_section` ss
';
$status !== null? $where[] = '`status` = '.(int) $status: true;
!empty($where)? $query .= 'WHERE '.implode(' AND ', $where):$query;
$query .= ' ORDER BY '.$order_by;
if($limit !== null) {
$query .= ' LIMIT '.$limit;
}
if($sections = Db::getInstance()->ExecuteS($query)) {
if ($lite) {
foreach($sections AS $section) {
$result[] = $section['id_section'];
}
} else {
foreach($sections AS $section) {
$result[] = new Section($section['id_section']);
}
}
}
return $result;
}
public static function getCompleteSections($status=null,$order_by='`position` ASC') {
$result = array();
$sections = self::getSections($status, $lite=false, $order_by);
if (!empty($sections)) {
foreach($sections AS $section) {
$result[$section->id_section] = array(
'section' => new Section($section->id_section),
'questions' => array()
);
$questions = Question::getQuestionsByIdSection($section->id_section);
if (!empty($questions)) {
$result[$section->id_section]['questions'] = $questions;
}
}
}
return $result;
}
public function save() {
if($this->id !== null) {
Db::getInstance()->Execute('
UPDATE `'._DB_PREFIX_.'support_section` SET
`status` = '.(int) $this->status.'
WHERE `id_section` = '.(int) $this->id.'
');
Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'support_section_lang` WHERE `id_section` = '.(int) $this->id);
$section_i18n = array();
foreach($this->title as $k => $v) {
if(!isset($section_i18n[$k])) {
$section_i18n[$k] = array();
}
$section_i18n[$k]['title'] = $v;
}
foreach($this->description as $k => $v) {
if(!isset($section_i18n[$k])) {
$section_i18n[$k] = array();
}
$section_i18n[$k]['description'] = $v;
}
foreach($section_i18n as $lang => $values) {
Db::getInstance()->Execute('
INSERT INTO `'._DB_PREFIX_.'support_section_lang` VALUES (
'.$this->id.',
'.$lang.',
"'.(isset($values['title'])? pSQL($values['title']): '').'",
"'.(isset($values['description'])? pSQL($values['description'], true): '').'"
)
');
}
} else {
$sql_position = 'SELECT MAX(position) FROM '. _DB_PREFIX_.'support_section';
$position = Db::getInstance()->getValue($sql_position);
Db::getInstance()->Execute('
INSERT INTO `'._DB_PREFIX_.'support_section` VALUES (
DEFAULT,
'.(int) $this->status.',
'.($position+1).'
)
');
$this->id = Db::getInstance()->Insert_ID();
Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'support_section_lang` WHERE `id_section` = '.(int) $this->id);
$section_i18n = array();
foreach($this->title as $k => $v) {
if(!isset($section_i18n[$k])) {
$section_i18n[$k] = array();
}
$section_i18n[$k]['title'] = $v;
}
foreach($this->description as $k => $v) {
if(!isset($section_i18n[$k])) {
$section_i18n[$k] = array();
}
$section_i18n[$k]['description'] = $v;
}
foreach($section_i18n as $lang => $values) {
Db::getInstance()->Execute('
INSERT INTO `'._DB_PREFIX_.'support_section_lang` VALUES (
'.$this->id.',
'.$lang.',
"'.(isset($values['title'])? pSQL($values['title']): '').'",
"'.(isset($values['description'])? pSQL($values['description'], true): '').'"
)
');
}
}
}
public static function deleteSection($id) {
if($section = Section::getSection($id)) {
Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'support_section_lang` WHERE `id_section` = '.(int) $id);
Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'support_section` WHERE `id_section` = '.(int) $id);
Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'support_question` WHERE `id_section` = '.(int) $id);
$question_ids = Question::getQuestionIdsByIdSection($id);
foreach ($question_ids as $key => $id_question) {
Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'support_question_lang` WHERE `id_question` = '.(int) $id_question);
}
}
}
}