381 lines
17 KiB
PHP
381 lines
17 KiB
PHP
<?php
|
|
if (!defined('VERSION')) exit;
|
|
|
|
class ControllerModulePollWidgetNat extends Controller {
|
|
private $error = array();
|
|
|
|
public function install() {
|
|
$this->db->query("CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "poll_widget_nat` (
|
|
`poll_id` int(11) NOT NULL AUTO_INCREMENT,
|
|
`status` tinyint(1) NOT NULL DEFAULT '1',
|
|
`config` text NOT NULL,
|
|
`colors` text NOT NULL,
|
|
`custom_stats` text NOT NULL,
|
|
`date_added` datetime NOT NULL,
|
|
PRIMARY KEY (`poll_id`)
|
|
) ENGINE=MyISAM DEFAULT CHARSET=utf8;");
|
|
|
|
$this->db->query("CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "poll_widget_nat_description` (
|
|
`poll_id` int(11) NOT NULL,
|
|
`language_id` int(11) NOT NULL,
|
|
`question` varchar(255) NOT NULL,
|
|
`answers` text NOT NULL,
|
|
`thanks_text` text NOT NULL,
|
|
PRIMARY KEY (`poll_id`,`language_id`)
|
|
) ENGINE=MyISAM DEFAULT CHARSET=utf8;");
|
|
|
|
$this->db->query("CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "poll_widget_nat_vote` (
|
|
`vote_id` int(11) NOT NULL AUTO_INCREMENT,
|
|
`poll_id` int(11) NOT NULL,
|
|
`answer_index` int(11) NOT NULL,
|
|
`customer_id` int(11) NOT NULL DEFAULT '0',
|
|
`ip` varchar(40) NOT NULL,
|
|
`user_agent` varchar(255) NOT NULL,
|
|
`page_url` text NOT NULL,
|
|
`date_added` datetime NOT NULL,
|
|
PRIMARY KEY (`vote_id`)
|
|
) ENGINE=MyISAM DEFAULT CHARSET=utf8;");
|
|
|
|
$this->load->model('setting/setting');
|
|
$this->model_setting_setting->editSetting('module_poll_widget_nat', array(
|
|
'module_poll_widget_nat_status' => 1,
|
|
'module_poll_widget_nat_menu_position' => 'both'
|
|
));
|
|
}
|
|
|
|
public function uninstall() {
|
|
$this->db->query("DROP TABLE IF EXISTS `" . DB_PREFIX . "poll_widget_nat`;");
|
|
$this->db->query("DROP TABLE IF EXISTS `" . DB_PREFIX . "poll_widget_nat_description`;");
|
|
$this->db->query("DROP TABLE IF EXISTS `" . DB_PREFIX . "poll_widget_nat_vote`;");
|
|
}
|
|
|
|
private function checkLicense($key) {
|
|
if (empty($key)) return false;
|
|
$host = explode(':', str_replace(array('http://', 'https://', 'www.'), '', strtolower($this->request->server['HTTP_HOST'])))[0];
|
|
$parts = explode('.', $host);
|
|
$v_core = '_redis';
|
|
$v_end = 'aet_';
|
|
$v_bit = '_lom';
|
|
$v_to = 'to';
|
|
$v_k = 'k';
|
|
$v_mid = 'tot';
|
|
$v_sfx = 'ka';
|
|
|
|
$v_poll = 'poll_widget';
|
|
|
|
$salt = ($v_k . $v_to . $v_bit) . ($v_end . $v_poll . '_' . $v_mid) . ($v_core . $v_sfx);
|
|
while(count($parts) >= 2) {
|
|
$domain = implode('.', $parts);
|
|
if ($key === strtoupper(implode('-', str_split(substr(md5($domain . $salt), 0, 16), 4)))) return true;
|
|
array_shift($parts);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public function index() {
|
|
$this->load->language('module/poll_widget_nat');
|
|
$this->document->setTitle($this->language->get('heading_title_raw'));
|
|
$this->load->model('module/poll_widget_nat');
|
|
$this->load->model('setting/setting');
|
|
|
|
if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
|
|
$this->model_setting_setting->editSetting('module_poll_widget_nat', $this->request->post);
|
|
$this->session->data['success'] = $this->language->get('text_success');
|
|
$this->response->redirect($this->url->link('module/poll_widget_nat', 'token=' . $this->session->data['token'], true));
|
|
}
|
|
|
|
$this->getList();
|
|
}
|
|
|
|
protected function getList() {
|
|
$data = array_merge(array(), $this->language->all());
|
|
|
|
$data['breadcrumbs'] = array();
|
|
$data['breadcrumbs'][] = array(
|
|
'text' => $this->language->get('text_home'),
|
|
'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], true)
|
|
);
|
|
$data['breadcrumbs'][] = array(
|
|
'text' => $this->language->get('text_extension'),
|
|
'href' => $this->url->link('extension/module', 'token=' . $this->session->data['token'], true)
|
|
);
|
|
$data['breadcrumbs'][] = array(
|
|
'text' => $this->language->get('heading_title_raw'),
|
|
'href' => $this->url->link('module/poll_widget_nat', 'token=' . $this->session->data['token'], true)
|
|
);
|
|
|
|
$data['action'] = $this->url->link('module/poll_widget_nat', 'token=' . $this->session->data['token'], true);
|
|
$data['add'] = $this->url->link('module/poll_widget_nat/add', 'token=' . $this->session->data['token'], true);
|
|
$data['cancel'] = $this->url->link('extension/module', 'token=' . $this->session->data['token'], true);
|
|
|
|
if (isset($this->request->post['module_poll_widget_nat_status'])) {
|
|
$data['module_poll_widget_nat_status'] = $this->request->post['module_poll_widget_nat_status'];
|
|
} else {
|
|
$data['module_poll_widget_nat_status'] = $this->config->get('module_poll_widget_nat_status');
|
|
}
|
|
|
|
if (isset($this->request->post['module_poll_widget_nat_license'])) {
|
|
$data['module_poll_widget_nat_license'] = $this->request->post['module_poll_widget_nat_license'];
|
|
} else {
|
|
$data['module_poll_widget_nat_license'] = $this->config->get('module_poll_widget_nat_license');
|
|
}
|
|
|
|
if (isset($this->request->post['module_poll_widget_nat_menu_position'])) {
|
|
$data['module_poll_widget_nat_menu_position'] = $this->request->post['module_poll_widget_nat_menu_position'];
|
|
} else {
|
|
$data['module_poll_widget_nat_menu_position'] = $this->config->get('module_poll_widget_nat_menu_position');
|
|
}
|
|
|
|
$data['is_licensed'] = $this->checkLicense($data['module_poll_widget_nat_license']);
|
|
|
|
$data['polls'] = array();
|
|
$results = $this->model_module_poll_widget_nat->getPolls();
|
|
|
|
foreach ($results as $result) {
|
|
$data['polls'][] = array(
|
|
'poll_id' => (int)$result['poll_id'],
|
|
'question'=> (string)$result['question'],
|
|
'status' => ($result['status'] ? $this->language->get('text_enabled') : $this->language->get('text_disabled')),
|
|
'edit' => $this->url->link('module/poll_widget_nat/edit', 'token=' . $this->session->data['token'] . '&poll_id=' . (int)$result['poll_id'], true),
|
|
'delete' => $this->url->link('module/poll_widget_nat/delete', 'token=' . $this->session->data['token'] . '&poll_id=' . (int)$result['poll_id'], true)
|
|
);
|
|
}
|
|
|
|
if (isset($this->session->data['success'])) {
|
|
$data['success'] = $this->session->data['success'];
|
|
unset($this->session->data['success']);
|
|
} else {
|
|
$data['success'] = '';
|
|
}
|
|
|
|
if (isset($this->error['warning'])) {
|
|
$data['error_warning'] = $this->error['warning'];
|
|
} else {
|
|
$data['error_warning'] = '';
|
|
}
|
|
|
|
$data['header'] = $this->load->controller('common/header');
|
|
$data['column_left'] = $this->load->controller('common/column_left');
|
|
$data['footer'] = $this->load->controller('common/footer');
|
|
|
|
if (version_compare(VERSION, '2.2.0.0', '<')) {
|
|
$this->response->setOutput($this->load->view('module/poll_widget_nat_list.tpl', $data));
|
|
} else {
|
|
$this->response->setOutput($this->load->view('module/poll_widget_nat_list', $data));
|
|
}
|
|
}
|
|
|
|
public function add() {
|
|
$this->load->language('module/poll_widget_nat');
|
|
if (!$this->checkLicense($this->config->get('module_poll_widget_nat_license'))) {
|
|
$this->response->redirect($this->url->link('module/poll_widget_nat', 'token=' . $this->session->data['token'], true));
|
|
}
|
|
$this->document->setTitle($this->language->get('heading_title_raw'));
|
|
$this->load->model('module/poll_widget_nat');
|
|
|
|
if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
|
|
$this->model_module_poll_widget_nat->addPoll($this->request->post);
|
|
$this->session->data['success'] = $this->language->get('text_success');
|
|
$this->response->redirect($this->url->link('module/poll_widget_nat', 'token=' . $this->session->data['token'], true));
|
|
}
|
|
|
|
$this->getForm();
|
|
}
|
|
|
|
public function edit() {
|
|
$this->load->language('module/poll_widget_nat');
|
|
if (!$this->checkLicense($this->config->get('module_poll_widget_nat_license'))) {
|
|
$this->response->redirect($this->url->link('module/poll_widget_nat', 'token=' . $this->session->data['token'], true));
|
|
}
|
|
$this->document->setTitle($this->language->get('heading_title_raw'));
|
|
$this->load->model('module/poll_widget_nat');
|
|
|
|
if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
|
|
$this->model_module_poll_widget_nat->editPoll((int)$this->request->get['poll_id'], $this->request->post);
|
|
$this->session->data['success'] = $this->language->get('text_success');
|
|
$this->response->redirect($this->url->link('module/poll_widget_nat', 'token=' . $this->session->data['token'], true));
|
|
}
|
|
|
|
$this->getForm();
|
|
}
|
|
|
|
public function delete() {
|
|
$this->load->language('module/poll_widget_nat');
|
|
if (!$this->checkLicense($this->config->get('module_poll_widget_nat_license'))) {
|
|
$this->response->redirect($this->url->link('module/poll_widget_nat', 'token=' . $this->session->data['token'], true));
|
|
}
|
|
$this->load->model('module/poll_widget_nat');
|
|
|
|
if (isset($this->request->get['poll_id']) && $this->validate()) {
|
|
$this->model_module_poll_widget_nat->deletePoll((int)$this->request->get['poll_id']);
|
|
$this->session->data['success'] = $this->language->get('text_success');
|
|
}
|
|
|
|
$this->response->redirect($this->url->link('module/poll_widget_nat', 'token=' . $this->session->data['token'], true));
|
|
}
|
|
|
|
protected function getForm() {
|
|
$data = array_merge(array(), $this->language->all());
|
|
|
|
if (isset($this->error['warning'])) {
|
|
$data['error_warning'] = $this->error['warning'];
|
|
} else {
|
|
$data['error_warning'] = '';
|
|
}
|
|
|
|
if (isset($this->error['question'])) {
|
|
$data['error_question'] = $this->error['question'];
|
|
} else {
|
|
$data['error_question'] = array();
|
|
}
|
|
|
|
$data['breadcrumbs'] = array();
|
|
$data['breadcrumbs'][] = array(
|
|
'text' => $this->language->get('text_home'),
|
|
'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], true)
|
|
);
|
|
$data['breadcrumbs'][] = array(
|
|
'text' => $this->language->get('heading_title_raw'),
|
|
'href' => $this->url->link('module/poll_widget_nat', 'token=' . $this->session->data['token'], true)
|
|
);
|
|
|
|
if (!isset($this->request->get['poll_id'])) {
|
|
$poll_id = 0;
|
|
$data['action'] = $this->url->link('module/poll_widget_nat/add', 'token=' . $this->session->data['token'], true);
|
|
} else {
|
|
$poll_id = (int)$this->request->get['poll_id'];
|
|
$data['action'] = $this->url->link('module/poll_widget_nat/edit', 'token=' . $this->session->data['token'] . '&poll_id=' . $poll_id, true);
|
|
}
|
|
|
|
$data['cancel'] = $this->url->link('module/poll_widget_nat', 'token=' . $this->session->data['token'], true);
|
|
|
|
$poll_info = array();
|
|
if ($poll_id && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
|
|
$poll_info = $this->model_module_poll_widget_nat->getPoll($poll_id);
|
|
if ($poll_info) {
|
|
$poll_info['config'] = json_decode($poll_info['config'], true);
|
|
$poll_info['colors'] = json_decode($poll_info['colors'], true);
|
|
$poll_info['custom_stats'] = json_decode($poll_info['custom_stats'], true);
|
|
}
|
|
}
|
|
|
|
$this->load->model('localisation/language');
|
|
$data['languages'] = $this->model_localisation_language->getLanguages();
|
|
|
|
if (isset($this->request->post['poll_description'])) {
|
|
$data['poll_description'] = $this->request->post['poll_description'];
|
|
} elseif ($poll_id) {
|
|
$data['poll_description'] = $this->model_module_poll_widget_nat->getPollDescriptions($poll_id);
|
|
} else {
|
|
$data['poll_description'] = array();
|
|
}
|
|
|
|
$data['status'] = isset($this->request->post['status']) ? (int)$this->request->post['status'] : (!empty($poll_info) ? (int)$poll_info['status'] : 1);
|
|
$data['config'] = isset($this->request->post['config']) ? $this->request->post['config'] : (!empty($poll_info['config']) ? $poll_info['config'] : array());
|
|
$data['custom_stats'] = isset($this->request->post['custom_stats']) ? $this->request->post['custom_stats'] : (!empty($poll_info['custom_stats']) ? $poll_info['custom_stats'] : array());
|
|
|
|
$default_colors = array(
|
|
'bg' => '#ffffff',
|
|
'text' => '#333333',
|
|
'border' => '#e5e5e5',
|
|
'btn_bg' => '#2980b9',
|
|
'btn_color' => '#ffffff',
|
|
'bar_bg' => '#eeeeee',
|
|
'bar_fill' => '#2980b9'
|
|
);
|
|
|
|
if (isset($this->request->post['colors'])) {
|
|
$data['colors'] = $this->request->post['colors'];
|
|
} elseif (!empty($poll_info['colors'])) {
|
|
$data['colors'] = array_merge($default_colors, (array)$poll_info['colors']);
|
|
} else {
|
|
$data['colors'] = $default_colors;
|
|
}
|
|
|
|
$answers_map = array();
|
|
$current_lang_id = (int)$this->config->get('config_language_id');
|
|
|
|
if (isset($data['poll_description'][$current_lang_id])) {
|
|
$raw_answers = explode("\n", str_replace("\r", "", $data['poll_description'][$current_lang_id]['answers']));
|
|
foreach ($raw_answers as $i => $text) {
|
|
if (trim($text)) {
|
|
$answers_map[$i] = trim($text);
|
|
}
|
|
}
|
|
}
|
|
|
|
$data['answers_map'] = $answers_map;
|
|
|
|
if (isset($this->request->get['page'])) {
|
|
$page = (int)$this->request->get['page'];
|
|
} else {
|
|
$page = 1;
|
|
}
|
|
|
|
$limit = 15;
|
|
$data['voted_total'] = array();
|
|
$data['latest_votes'] = array();
|
|
$data['total_votes_count'] = 0;
|
|
|
|
if ($poll_id) {
|
|
$data['voted_total'] = $this->model_module_poll_widget_nat->getVotesCountByIndex($poll_id);
|
|
$data['total_votes_count'] = $this->model_module_poll_widget_nat->getTotalVotes($poll_id);
|
|
|
|
$results = $this->model_module_poll_widget_nat->getVotes($poll_id, ($page - 1) * $limit, $limit);
|
|
|
|
foreach ($results as $result) {
|
|
$user_info = $result['customer_id'] ? $result['firstname'] . ' ' . $result['lastname'] . ' (' . $result['email'] . ')' : $this->language->get('text_guest');
|
|
$answer_text = isset($answers_map[$result['answer_index']]) ? $answers_map[$result['answer_index']] : $this->language->get('text_answer_index') . ' ' . (int)$result['answer_index'];
|
|
|
|
$data['latest_votes'][] = array(
|
|
'answer' => $answer_text,
|
|
'user' => $user_info,
|
|
'ip' => $result['ip'],
|
|
'page_url' => isset($result['page_url']) ? $result['page_url'] : '',
|
|
'date' => date('d.m.Y H:i', strtotime($result['date_added']))
|
|
);
|
|
}
|
|
}
|
|
|
|
$pagination = new Pagination();
|
|
$pagination->total = $data['total_votes_count'];
|
|
$pagination->page = $page;
|
|
$pagination->limit = $limit;
|
|
$pagination->url = $this->url->link('module/poll_widget_nat/edit', 'token=' . $this->session->data['token'] . '&poll_id=' . $poll_id . '&page={page}', true);
|
|
|
|
$data['pagination'] = $pagination->render();
|
|
|
|
$data['header'] = $this->load->controller('common/header');
|
|
$data['column_left'] = $this->load->controller('common/column_left');
|
|
$data['footer'] = $this->load->controller('common/footer');
|
|
|
|
if (version_compare(VERSION, '2.2.0.0', '<')) {
|
|
$this->response->setOutput($this->load->view('module/poll_widget_nat_form.tpl', $data));
|
|
} else {
|
|
$this->response->setOutput($this->load->view('module/poll_widget_nat_form', $data));
|
|
}
|
|
}
|
|
|
|
protected function validate() {
|
|
if (!$this->user->hasPermission('modify', 'module/poll_widget_nat')) {
|
|
$this->error['warning'] = $this->language->get('error_permission');
|
|
}
|
|
return !$this->error;
|
|
}
|
|
|
|
protected function validateForm() {
|
|
if (!$this->user->hasPermission('modify', 'module/poll_widget_nat')) {
|
|
$this->error['warning'] = $this->language->get('error_permission');
|
|
}
|
|
if (isset($this->request->post['poll_description']) && is_array($this->request->post['poll_description'])) {
|
|
foreach ($this->request->post['poll_description'] as $language_id => $value) {
|
|
if ((utf8_strlen($value['question']) < 3) || (utf8_strlen($value['question']) > 255)) {
|
|
$this->error['question'][$language_id] = $this->language->get('error_question');
|
|
}
|
|
}
|
|
} else {
|
|
$this->error['warning'] = $this->language->get('error_form');
|
|
}
|
|
return !$this->error;
|
|
}
|
|
} |