id_poll = (int)$id_poll; $DLang = new DLang(); $this->langs = $DLang->getList(); $this->DBObj($id); } function setEnabledOnly($val = null) { if ($val !== null) { $this->enabled_only = (bool)$val; } return $this->enabled_only; } function checkPoll() { $this->id_poll = (int)$this->id_poll; $sql = "select 1 from {$this->db->tblPoll} where id = {$this->id_poll}"; return $this->db->queryOne($sql); } function getPollName($id = null, $lang = '') { $id = (int)$id; if (!$id) { $id = (int)$this->id_poll; } if ($lang == '') { $lang = CURR_LANG; } $lang = mysql_real_escape_string($lang); $sql = "select pq.title from {$this->db->tblPoll} as p join {$this->db->tblPoll}Data as pq on p.id = pq.id_poll and pq.lang = '{$lang}' where id = {$id}"; return $this->db->queryOne($sql); } 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_question = " . (int)$res['id']; $res['title'] = $this->db->queryAll($sql, null, MDB_FETCHMODE_DEFAULT, true); $res['answers'] = $this->getAnswers($res['id']); } $this->arrayParseTag2Url(&$res); return $this->getSubElem($res, $field); } function getAnswers($id_question, $lang = '') { $id_question = (int)$id_question; $data = array(); $sql = "select sorter, id from {$this->tableNameDB}Answer where id_poll_question = {$id_question} order by sorter"; $ans = $this->db->queryAll($sql, null, MDB_FETCHMODE_DEFAULT, true); if (!MDB2::isError($ans) && !empty($ans)) { foreach ($ans as $ans_id) { $sql = "select lang, title from {$this->tableNameDB}AnswerData where id_poll_question_answer = " . (int)$ans_id; $res = $this->db->queryAll($sql); if (!MDB2::isError($ans) && !empty($ans)) { foreach ($res as $ans_data) { $data[$ans_data['lang']][$ans_id] = $ans_data['title']; } } } } if (!empty($data) && $lang != '') { $data = $data[$lang]; } return $data; } function initFromForm(&$from) { $files = array(); //$files['image'] = $from->getElementValue('image'); $data = $from->exportValues(); $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('id_poll = ' . $this->id_poll); $data['id_poll'] = $this->id_poll; $this->id = $this->table->insert($data); if ($this->id) { $id = (int)$this->id; foreach ($this->langs as $lng) { $lang = mysql_real_escape_string($lng['name']); $title = mysql_real_escape_string($this->_data['title'][$lng['name']]); $sql = "insert into {$this->tableNameDB}Data (id_poll_question, lang, title) values ({$id}, '{$lang}', '{$title}')"; $this->db->query($sql); } $this->insertAnswers(); } $this->handleFiles(); return $this->id; } function _initTable() { if (!is_object($this->table)) { require_once('class/DB/DBTable.php'); $this->table = new DBTable($this->tableName); $this->tableAnswer = new DBTable('PollQuestionAnswer'); } return true; } function insertAnswers($data = null) { if (!is_null($data)) { $this->initFromArray($data, $files); } $this->_initTable(); $data = $this->_data['answers']; $keys = array_keys($data[$this->langs[0]['name']]); if (is_array($keys) && !empty($keys)) { foreach ($keys as $sorter => $key) { $id_poll_question_answer = (int)$this->tableAnswer->insert(array('sorter' => $sorter, 'id_poll_question' => $this->id)); if ($id_poll_question_answer) { foreach ($this->langs as $lng) { $lang = mysql_real_escape_string($lng['name']); $title = mysql_real_escape_string($data[$lng['name']][$key]); $sql = "insert into {$this->tableNameDB}AnswerData (id_poll_question_answer, lang, title) values ({$id_poll_question_answer}, '{$lang}', '{$title}')"; $this->db->query($sql); } } } } return true; } 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']]); $sql = "replace into {$this->tableNameDB}Data (id_poll_question, lang, title) values ({$id}, '{$lang}', '{$title}')"; $this->db->query($sql); } $this->updateAnswers(); return true; } function updateAnswers($data = null) { if (!is_null($data)) { $this->initFromArray($data, $files); } $this->_initTable(); $data = $this->_data['answers']; $keys = array_keys($data[$this->langs[0]['name']]); // âèçíà÷àºì ÿê³ â³äïîâ³ä³ òðåáà âèäàëèòè $sql = "select id from {$this->tableNameDB}Answer where id_poll_question = {$this->id} order by sorter"; $ans = $this->db->queryCol($sql); if (MDB2::isError($ans) || empty($ans)) { $ans = array(); } $ans_to_del = array_diff($ans, $keys); if (!empty($ans_to_del)) { $this->deleteAnswers($this->id, $ans_to_del); } if (is_array($keys) && !empty($keys)) { foreach ($keys as $sorter => $key) { $key = (int)$key; if ($key <= 0) { // insert äîäàëàñü íîâà â³äïîâ³äü $id_poll_question_answer = (int)$this->tableAnswer->insert(array('sorter' => $sorter, 'id_poll_question' => $this->id)); if ($id_poll_question_answer) { foreach ($this->langs as $lng) { $lang = mysql_real_escape_string($lng['name']); $title = mysql_real_escape_string($data[$lng['name']][$key]); $sql = "insert into {$this->tableNameDB}AnswerData (id_poll_question_answer, lang, title) values ({$id_poll_question_answer}, '{$lang}', '{$title}')"; $this->db->query($sql); } } } else { // update çì³íà ³ñíóþ÷î¿ $this->tableAnswer->update(array('sorter' => $sorter), "id = {$key}"); foreach ($this->langs as $lng) { $lang = mysql_real_escape_string($lng['name']); $title = mysql_real_escape_string($data[$lng['name']][$key]); $sql = "replace into {$this->tableNameDB}AnswerData (id_poll_question_answer, lang, title) values ({$key}, '{$lang}', '{$title}')"; $this->db->query($sql); } } } } return true; } function delete() { //$this->_files = array('image' => array('del' => 1)); $this->deleteAnswers(); $sql = "delete from {$this->tableNameDB}Data where id_poll_question = " . (int)$this->id; $this->db->query($sql); return parent::delete(); } function deleteAnswers($id = 0, $ans_to_del = array()) { $id = (int)$id; if (!$id) { $id = (int)$this->id; } if (!is_array($ans_to_del) || empty($ans_to_del)) { $ans_to_del = array(); } if (empty($ans_to_del)) { $sql = "select id from {$this->tableNameDB}Answer where id_poll_question = {$id}"; $ans_to_del = $this->db->queryCol($sql); if (MDB2::isError($ans_to_del) || !is_array($ans_to_del)) { $ans_to_del = array(); } } foreach ($ans_to_del as $id_answer) { $id_answer = (int)$id_answer; $sql = "delete from {$this->tableNameDB}Answer where id = {$id_answer}"; $this->db->query($sql); $sql = "delete from {$this->tableNameDB}AnswerData where id_poll_question_answer = {$id_answer}"; $this->db->query($sql); } return true; } function _getWhat4Grid($opt = array()) { return parent::_getWhat4Grid($opt) . ", {$this->tableName}Data.title"; } function _getJoin4Grid($opt = array()) { return "join {$this->tableNameDB}Data as {$this->tableName}Data on {$this->tableName}.id = {$this->tableName}Data.id_poll_question and {$this->tableName}Data.lang = '" . CURR_LANG . "'"; } function _getGroup4Grid($opt = array()) { return ""; } function _getWhere4Grid($opt = array()) { $where = parent::_getWhere4Grid($opt); if ($this->enabled_only) { $where .= " and {$this->tableName}.enabled = 'y'"; } $where .= " and {$this->tableName}.id_poll = {$this->id_poll}"; return $where; } function _getOrder4Grid($opt = array()) { $order = parent::_getOrder4Grid($opt); if ($order == '' && $opt['def_order_by'] != '') { $order = "order by {$opt['def_order_by']}"; } return $order; } function reorder($list) { $this->_initTable(); foreach ((array)$list as $sorter => $id) { $this->table->update(array('sorter' => $sorter), 'id = ' . intval($id)); } return true; } }