langs = $DLang->getList(); $this->DBObj($id); if (!is_array($user)) { $user = null; } $this->_user = $user; //$this->search = SiteMap::getObj('Search/Search.php'); //$this->search->app = 'poll'; } function exists($poll_id) { $poll_id = (int)$poll_id; $sql = "select 1 from {$this->tableNameDB} where id = {$poll_id} and enabled = 'y'"; return $this->db->queryOne($sql); } function setEnabledOnly($val = null) { if ($val !== null) { $this->enabled_only = (bool)$val; } return $this->enabled_only; } function getQuestions($id = 0, $lang = '') { $data = array(); $id = (int)$id; if (!$id) { $id = (int)$this->id; } if ($lang == '') { $lang = CURR_LANG; } $Question = SiteMap::getObj('Poll/Question/PollQuestion.php', $id); if ($Question->checkPoll()) { $Question->setEnabledOnly(true); $data = $Question->getList4Grid(array('def_order_by' => 'sorter')); $data = $data['list']; if (!is_array($data) || empty($data)) $data = array(); foreach ($data as $k => $v) { $data[$k]['answers'] = $Question->getAnswers($v['id'], $lang); } } return $data; } function getFromDB($id, $field = false) { $res = parent::getFromDB($id); if (is_array($res) && !empty($res)) { $sql = "select lang, title from {$this->tableNameDB}Data where id_poll = " . (int)$res['id']; $res['title'] = $this->db->queryAll($sql, null, MDB_FETCHMODE_DEFAULT, true); $sql = "select lang, description from {$this->tableNameDB}Data where id_poll = " . (int)$res['id']; $res['description'] = $this->db->queryAll($sql, null, MDB_FETCHMODE_DEFAULT, true); } $this->arrayParseTag2Url(&$res); return $this->getSubElem($res, $field); } function prepareFormDate($date) { return sprintf("%04d-%02d-%02d", $date['Y'], $date['M'], $date['d']); } function initFromForm(&$from) { $files = array(); //$files['image'] = $from->getElementValue('image'); $data = $from->exportValues(); $data['date_start'] = $this->prepareFormDate($data['date_start']); $data['date_end'] = $this->prepareFormDate($data['date_end']); $this->arrayParseUrl2Tag($data); $this->initFromArray($data, $files); return true; } function insert($data = null, $files = null) { if (!is_null($data)) { $this->initFromArray($data, $files); } $this->_initTable(); $data = $this->_data; unset($data['id']); $data['sorter'] = $this->getSorter(); $this->id = $this->table->insert($data); foreach ($this->langs as $lng) { $id = (int)$this->id; $lang = mysql_real_escape_string($lng['name']); $title = mysql_real_escape_string($this->_data['title'][$lng['name']]); $description = mysql_real_escape_string($this->_data['description'][$lng['name']]); $sql = "insert into {$this->tableNameDB}Data (id_poll, lang, title, description) values ({$id}, '{$lang}', '{$title}', '{$description}')"; $this->db->query($sql); } $this->handleFiles(); //$this->_cacheAll(); return $this->id; } function _initFile() { parent::_initFile(); $this->file->typeStoring = FILE_DB_TS_FS; return true; } function update($data = null, $files = null) { parent::update($data, $files); foreach ($this->langs as $lng) { $id = (int)$this->id; $lang = mysql_real_escape_string($lng['name']); $title = mysql_real_escape_string($this->_data['title'][$lng['name']]); $description = mysql_real_escape_string($this->_data['description'][$lng['name']]); $sql = "replace into {$this->tableNameDB}Data (id_poll, lang, title, description) values ({$id}, '{$lang}', '{$title}', '{$description}')"; $this->db->query($sql); } //$this->_cacheAll(); return true; } function delete() { //$this->_files = array('image' => array('del' => 1)); $this->deleteQuestions(); $sql = "delete from {$this->tableNameDB}Data where id_poll = " . (int)$this->id; $this->db->query($sql); parent::delete(); //$this->_cacheAll(); return true; } function deleteQuestions() { $Q = SiteMap::getObj('Poll/Question/PollQuestion.php', $this->id); $qs = $Q->getList4Grid(); $qs = $qs['list']; if (is_array($qs)) { foreach ($qs as $i) { $Q->id = (int)$i['id']; $Q->delete(); } } return true; } function _getWhat4Grid($opt = array()) { return parent::_getWhat4Grid($opt) . ", {$this->tableName}Data.title, concat('" . _('Edit') . " ( ', count({$this->tableName}Question.id), ' ) ') as questions_num_title, {$this->tableName}Data.description, concat(date_format({$this->tableName}.date_start, '%d/%m/%y'), ' - ', date_format({$this->tableName}.date_end, '%d/%m/%y')) as term, if ({$this->tableName}.date_start <= current_date() and {$this->tableName}.date_end >= current_date(), 'y', 'n') as active"; } function _getJoin4Grid($opt = array()) { return "join {$this->tableNameDB}Data as {$this->tableName}Data on {$this->tableName}.id = {$this->tableName}Data.id_poll and {$this->tableName}Data.lang = '" . CURR_LANG . "' left join {$this->tableNameDB}Question as {$this->tableName}Question on {$this->tableName}.id = {$this->tableName}Question.id_poll "; } function _getGroup4Grid($opt = array()) { return "group by {$this->tableName}.id"; } function _getHaving4Grid($opt = array()) { $having = ''; if ($this->enabled_only) { $having = "having count({$this->tableName}Question.id) > 0"; } return $having; } function _getWhere4Grid($opt = array()) { $where = parent::_getWhere4Grid($opt); if ($this->enabled_only) { $where .= " and {$this->tableName}.enabled = 'y'"; } return $where; } function reorder($list) { $this->_initTable(); foreach ((array)$list as $sorter => $id) { $this->table->update(array('sorter' => $sorter), 'id = ' . intval($id)); } return true; } function getList4Select($lang = '') { $lang = (string)$lang; if ($lang == '') { $lang = CURR_LANG; } $lang = mysql_real_escape_string($lang); $sql = "select {$this->tableName}.id, {$this->tableName}Data.title from {$this->tableNameDB} as {$this->tableName} join {$this->tableNameDB}Data as {$this->tableName}Data on {$this->tableName}.id = {$this->tableName}Data.id_poll and {$this->tableName}Data.lang = '{$lang}' left join {$this->tableNameDB}Question as {$this->tableName}Question on {$this->tableName}.id = {$this->tableName}Question.id_poll where {$this->tableName}.enabled = 'y' group by {$this->tableName}.id having count({$this->tableName}Question.id) > 0 order by {$this->tableName}.sorter "; $res = $this->db->queryAll($sql, null, MDB_FETCHMODE_DEFAULT, true); return (array)$res; } function changeOption($opt) { parent::changeOption($opt); //$this->_cacheAll(); return true; } function isAllowVote($id_poll = 0) { $res = false; if ((int)$this->_user['id']) { $id_poll = (int)$id_poll; if (!$id_poll) { $id_poll = (int)$this->id; } if ($id_poll) { $sql = "select 1 from {$this->tableNameDB} where date_start <= current_date() and date_end >= current_date() and id = {$id_poll}"; $res = (bool)$this->db->queryOne($sql); //return $res; // залишив для тестування !!!!!!!!!!!!!! if ($res) { //$sql = "select id from {$this->tableNameDB}QuestionVote where id_poll = {$id_poll} and remote_addr = '" . mysql_real_escape_string($_SERVER['REMOTE_ADDR']) . "' limit 1"; $sql = "select id from {$this->tableNameDB}QuestionVote where id_poll = {$id_poll} and id_individual = " . (int)$this->_user['id'] . " limit 1"; $res = !(bool)$this->db->queryOne($sql); } } } return $res; } function getTotalVotes($id_poll) { $sql = "select count(*) from {$this->tableNameDB}QuestionVote where id_poll = " . (int)$id_poll; return $this->db->queryOne($sql); } function getResults($id_poll = 0) { $res = array(); $id_poll = (int)$id_poll; if (!$id_poll) { $id_poll = (int)$this->id; } if ($id_poll) { $res['questions'] = $this->getQuestions($id_poll); if (is_array($res['questions']) && !empty($res['questions'])) { foreach ($res['questions'] as $k => $v) { $sql = "select id_poll_question_answer, count(*) from {$this->tableNameDB}QuestionVote where id_poll_question = " . (int)$v['id'] . " group by id_poll_question_answer "; $ans = $this->db->queryAll($sql, null, MDB_FETCHMODE_DEFAULT, true); $total = array_sum($ans); foreach ($v['answers'] as $id_answer => $title) { $res['questions'][$k]['answers'][$id_answer] = array( 'title' => $title, 'votes' => (int)$ans[$id_answer], 'percent' => round((int)$ans[$id_answer] * 100 / $total), ); } } } $res['total'] = $this->getTotalVotes($id_poll); } return $res; } function vote($answer) { if ($this->isAllowVote($answer['id_poll'])) { if (is_array($answer['question'])) { $voteTable = new DBTable($this->tableName . 'QuestionVote'); foreach ($answer['question'] as $id_poll_question => $id_poll_question_answer) { $data = array( 'id_poll' => $answer['id_poll'], 'id_poll_question' => (int)$id_poll_question, 'id_poll_question_answer' => (int)$id_poll_question_answer, 'remote_addr' => $_SERVER['REMOTE_ADDR'], 'id_individual' => (int)$this->_user['id'], ); $voteTable->insert($data); } } } return true; } function getPageAlias() { $DocObj = SiteMap::getObj('CMS/Doc/DocObj.php', 0); $pages = $DocObj->getList4Grid(array('addonWhere' => array("{$DocObj->tableName}.page_type" => 'poll'), 'ignore_parent' => true)); $page_alias = $DocObj->_getPath2Doc($pages['list'][0]['id']); return (string)$page_alias; } //function _cacheAll() //{ // $this->db->query("DELETE FROM {$this->search->tableNameDB} WHERE `app` = '{$this->search->app}'"); // // $list = $this->getList4Grid(array('addonWhere' => "and {$this->tableName}.enabled = 'y'")); // $page_alias = $this->getPageAlias(); // // foreach ($list['list'] as $page) { // $this->_cacheItem($page['id'], "{$page_alias}/{$page['id']}", $this->appName . ' :: '); // } // // return true; //} // //function _cacheItem($id, $url, $title = '') //{ // $data = $this->getFromDB($id); // foreach ($this->langs as $lang) { // $arr = array( // 'lang' => $lang['name'], // 'url' => ($lang['name'] == CURR_LANG ? '' : $lang['name'] . '/') . $url, // 'title' => $title . $data['title'][$lang['name']], // 'data' => trim(strip_tags($data['description'][$lang['name']])) . ' ', // ); // $this->search->insert($arr); // } // // return 1; //} }