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

184 lines
6.2 KiB
PHP

<?php
class Reason
{
public $id;
public $id_reason;
public $id_contact;
public $visible;
public $contact = array();
public $title = array();
public $versions = array();
public function __construct($id=NULL)
{
if($id !== NULL) {
$reason = Reason::getReason($id);
if($reason) {
$this->id = $id;
$this->id_reason = $reason['id_reason'];
$this->id_contact = $reason['id_contact'];
$this->visible = $reason['visible'];
$this->contact = $reason['contact'];
$this->title = $reason['title'];
/*$this->versions = $reason['versions'];*/
}
}
}
public static function getReason($id) {
if(!($r = Db::getInstance()->ExecuteS('
SELECT *
FROM `'._DB_PREFIX_.'support_reason`
WHERE `id_reason` = '.(int) $id
)) || count($r) == 0) {
return false;
}
$result = array(
'id_reason' => $r[0]['id_reason'],
'id_contact' => $r[0]['id_contact'],
'visible' => $r[0]['visible'],
'title' => array(),
);
$rl = Db::getInstance()->ExecuteS('
SELECT *
FROM `'._DB_PREFIX_.'support_reason_lang`
WHERE `id_reason` = '.(int) $id
);
foreach($rl as $l) {
$result['title'][$l['id_lang']] = $l['title'];
}
$cl = Db::getInstance()->ExecuteS('
SELECT cl.`name`, cl.`id_lang`
FROM `'._DB_PREFIX_.'contact_lang` cl
LEFT JOIN `'._DB_PREFIX_.'support_reason` sr ON sr.`id_contact` = cl.`id_contact`
WHERE sr.`id_reason` = '.(int) $id
);
foreach($cl as $l) {
$result['contact'][$l['id_lang']] = $l['name'];
}
/*foreach(Db::getInstance()->ExecuteS('
SELECT `version`
FROM `'._DB_PREFIX_.'support_reason_site_version`
WHERE `id_reason` = '.(int) $id
) as $version) {
$result['versions'][] = $version['version'];
}*/
return $result;
}
public static function getReasons($where = array(), $lite=false, $order_by='`id_reason` ASC', $limit=null) {
global $cookie;
$result = array();
$query = '
SELECT sr.`id_reason`
FROM `'._DB_PREFIX_.'support_reason` sr
';
!empty($where)? $query .= 'WHERE '.implode(' AND ', $where):$query;
$query .= ' ORDER BY '.$order_by;
if($limit !== null) {
$query .= ' LIMIT '.$limit;
}
if($reasons = Db::getInstance()->ExecuteS($query)) {
if ($lite) {
foreach($reasons AS $reason) {
$result[] = $reason['id_reason'];
}
} else {
foreach($reasons AS $reason) {
$result[] = new Reason($reason['id_reason']);
}
}
}
return $result;
}
public function save() {
if($this->id !== null) {
Db::getInstance()->Execute('
UPDATE `'._DB_PREFIX_.'support_reason` SET
`id_contact` = '.(int) $this->id_contact.'
`visible` = '.(int) $this->visible.'
WHERE `id_reason` = '.(int) $this->id.'
');
Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'support_reason_lang` WHERE `id_reason` = '.(int) $this->id);
$reason_i18n = array();
foreach($this->title as $k => $v) {
if(!isset($reason_i18n[$k])) {
$reason_i18n[$k] = array();
}
$reason_i18n[$k]['title'] = $v;
}
foreach($reason_i18n as $lang => $values) {
Db::getInstance()->Execute('
INSERT INTO `'._DB_PREFIX_.'support_reason_lang` VALUES (
'.$this->id.',
'.$lang.',
"'.(isset($values['title'])? pSQL($values['title']): '').'"
)
');
}
} else {
Db::getInstance()->Execute('
INSERT INTO `'._DB_PREFIX_.'support_reason` VALUES (
DEFAULT,
'.(int) $this->id_contact.',
'.(int) $this->visible.'
)
');
$this->id = Db::getInstance()->Insert_ID();
Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'support_reason_lang` WHERE `id_reason` = '.(int) $this->id);
$reason_i18n = array();
foreach($this->title as $k => $v) {
if(!isset($reason_i18n[$k])) {
$reason_i18n[$k] = array();
}
$reason_i18n[$k]['title'] = $v;
}
foreach($reason_i18n as $lang => $values) {
Db::getInstance()->Execute('
INSERT INTO `'._DB_PREFIX_.'support_reason_lang` VALUES (
'.$this->id.',
'.$lang.',
"'.(isset($values['title'])? pSQL($values['title']): '').'"
)
');
}
}
}
public static function deleteReason($id) {
if($reason = Reason::getReason($id)) {
Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'support_reason_lang` WHERE `id_reason` = '.(int) $id);
Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'support_reason` WHERE `id_reason` = '.(int) $id);
}
}
public function insertReasonCustomerThread($id_thread) {
if(!($result = Db::getInstance()->ExecuteS('
SELECT *
FROM `'._DB_PREFIX_.'support_reason_customerthread`
WHERE `id_customer_thread` = '.(int) $id_thread
)) || count($result) == 0) {
$insert = Db::getInstance()->Execute('
INSERT INTO `'._DB_PREFIX_.'support_reason_customerthread` VALUES (
'.$this->id.',
'.(int) $id_thread.'
)
');
}
}
}