Poll_Widget_NAT-2.0-2.2/upload/admin/model/module/poll_widget_nat.php

163 lines
8.2 KiB
PHP

<?php
if (!defined('VERSION')) exit;
class ModelModulePollWidgetNat extends Model {
public function addPoll($data) {
$status = (int)$data['status'];
$config = $this->db->escape(json_encode($data['config']));
$colors = $this->db->escape(json_encode($data['colors']));
$custom_stats = $this->db->escape(json_encode($data['custom_stats']));
$this->db->query("INSERT INTO `" . DB_PREFIX . "poll_widget_nat` SET status = '" . $status . "', config = '" . $config . "', colors = '" . $colors . "', custom_stats = '" . $custom_stats . "', date_added = NOW()");
$poll_id = $this->db->getLastId();
if (isset($data['poll_description']) && is_array($data['poll_description'])) {
foreach ($data['poll_description'] as $language_id => $value) {
$q = $this->db->escape((string)$value['question']);
$a = $this->db->escape((string)$value['answers']);
$t_text = $this->db->escape((string)$value['thanks_text']);
$this->db->query("INSERT INTO `" . DB_PREFIX . "poll_widget_nat_description` SET poll_id = '" . (int)$poll_id . "', language_id = '" . (int)$language_id . "', question = '" . $q . "', answers = '" . $a . "', thanks_text = '" . $t_text . "'");
}
}
$lang_id = (int)$this->config->get('config_language_id');
$name = isset($data['poll_description'][$lang_id]['question']) ? (string)$data['poll_description'][$lang_id]['question'] : 'Опрос #' . $poll_id;
$setting = array(
'name' => $name,
'poll_id' => $poll_id,
'status' => $status
);
$this->db->query("INSERT INTO `" . DB_PREFIX . "module` SET `name` = '" . $this->db->escape($name) . "', `code` = 'poll_widget_nat', `setting` = '" . $this->db->escape(json_encode($setting)) . "'");
return $poll_id;
}
public function editPoll($poll_id, $data) {
$poll_id = (int)$poll_id;
$status = (int)$data['status'];
$config = $this->db->escape(json_encode($data['config']));
$colors = $this->db->escape(json_encode($data['colors']));
$custom_stats = $this->db->escape(json_encode($data['custom_stats']));
$this->db->query("UPDATE `" . DB_PREFIX . "poll_widget_nat` SET status = '" . $status . "', config = '" . $config . "', colors = '" . $colors . "', custom_stats = '" . $custom_stats . "' WHERE poll_id = '" . $poll_id . "'");
$this->db->query("DELETE FROM `" . DB_PREFIX . "poll_widget_nat_description` WHERE poll_id = '" . $poll_id . "'");
if (isset($data['poll_description']) && is_array($data['poll_description'])) {
foreach ($data['poll_description'] as $language_id => $value) {
$q = $this->db->escape((string)$value['question']);
$a = $this->db->escape((string)$value['answers']);
$t_text = $this->db->escape((string)$value['thanks_text']);
$this->db->query("INSERT INTO `" . DB_PREFIX . "poll_widget_nat_description` SET poll_id = '" . $poll_id . "', language_id = '" . (int)$language_id . "', question = '" . $q . "', answers = '" . $a . "', thanks_text = '" . $t_text . "'");
}
}
$lang_id = (int)$this->config->get('config_language_id');
$name = isset($data['poll_description'][$lang_id]['question']) ? (string)$data['poll_description'][$lang_id]['question'] : 'Опрос #' . $poll_id;
$setting = array(
'name' => $name,
'poll_id' => $poll_id,
'status' => $status
);
$module_query = $this->db->query("SELECT module_id, setting FROM `" . DB_PREFIX . "module` WHERE `code` = 'poll_widget_nat'");
$found = false;
foreach ($module_query->rows as $row) {
$mod_setting = json_decode($row['setting'], true);
if (isset($mod_setting['poll_id']) && (int)$mod_setting['poll_id'] === $poll_id) {
$this->db->query("UPDATE `" . DB_PREFIX . "module` SET `name` = '" . $this->db->escape($name) . "', `setting` = '" . $this->db->escape(json_encode($setting)) . "' WHERE module_id = '" . (int)$row['module_id'] . "'");
$found = true;
break;
}
}
if (!$found) {
$this->db->query("INSERT INTO `" . DB_PREFIX . "module` SET `name` = '" . $this->db->escape($name) . "', `code` = 'poll_widget_nat', `setting` = '" . $this->db->escape(json_encode($setting)) . "'");
}
}
public function deletePoll($poll_id) {
$poll_id = (int)$poll_id;
$this->db->query("DELETE FROM `" . DB_PREFIX . "poll_widget_nat` WHERE poll_id = '" . $poll_id . "'");
$this->db->query("DELETE FROM `" . DB_PREFIX . "poll_widget_nat_description` WHERE poll_id = '" . $poll_id . "'");
$this->db->query("DELETE FROM `" . DB_PREFIX . "poll_widget_nat_vote` WHERE poll_id = '" . $poll_id . "'");
$module_query = $this->db->query("SELECT module_id, setting FROM `" . DB_PREFIX . "module` WHERE `code` = 'poll_widget_nat'");
foreach ($module_query->rows as $row) {
$mod_setting = json_decode($row['setting'], true);
if (isset($mod_setting['poll_id']) && (int)$mod_setting['poll_id'] === $poll_id) {
$mod_id = (int)$row['module_id'];
$this->db->query("DELETE FROM `" . DB_PREFIX . "module` WHERE module_id = '" . $mod_id . "'");
$this->db->query("DELETE FROM `" . DB_PREFIX . "layout_module` WHERE `code` = 'poll_widget_nat." . $mod_id . "'");
break;
}
}
}
public function getPoll($poll_id) {
$query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "poll_widget_nat` WHERE poll_id = '" . (int)$poll_id . "'");
return $query->row;
}
public function getPollDescriptions($poll_id) {
$poll_description_data = array();
$query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "poll_widget_nat_description` WHERE poll_id = '" . (int)$poll_id . "'");
foreach ($query->rows as $result) {
$poll_description_data[$result['language_id']] = array(
'question' => $result['question'],
'answers' => $result['answers'],
'thanks_text' => $result['thanks_text']
);
}
return $poll_description_data;
}
public function getPolls() {
$lang_id = (int)$this->config->get('config_language_id');
$sql = "SELECT p.poll_id, pd.question, p.status FROM `" . DB_PREFIX . "poll_widget_nat` p LEFT JOIN `" . DB_PREFIX . "poll_widget_nat_description` pd ON (p.poll_id = pd.poll_id) WHERE pd.language_id = '" . $lang_id . "' ORDER BY p.date_added DESC";
$query = $this->db->query($sql);
return $query->rows;
}
public function getVotes($poll_id, $start = 0, $limit = 10) {
$poll_id = (int)$poll_id;
$start = (int)$start;
$limit = (int)$limit;
if ($start < 0) {
$start = 0;
}
if ($limit < 1) {
$limit = 10;
}
$sql = "SELECT v.*, c.firstname, c.lastname, c.email FROM `" . DB_PREFIX . "poll_widget_nat_vote` v LEFT JOIN `" . DB_PREFIX . "customer` c ON (v.customer_id = c.customer_id) WHERE v.poll_id = '" . $poll_id . "' ORDER BY v.date_added DESC LIMIT " . $start . "," . $limit;
$query = $this->db->query($sql);
return $query->rows;
}
public function getTotalVotes($poll_id) {
$query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "poll_widget_nat_vote` WHERE poll_id = '" . (int)$poll_id . "'");
return (int)$query->row['total'];
}
public function getVotesCountByIndex($poll_id) {
$vote_data = array();
$query = $this->db->query("SELECT answer_index, COUNT(*) AS total FROM `" . DB_PREFIX . "poll_widget_nat_vote` WHERE poll_id = '" . (int)$poll_id . "' GROUP BY answer_index");
foreach ($query->rows as $result) {
$vote_data[(int)$result['answer_index']] = (int)$result['total'];
}
return $vote_data;
}
}