id = (int) $id; return $this; } function addClick($id) { $data = array( 'id_announcement' => (int)$id, 'remote_addr' => $_SERVER['REMOTE_ADDR'], 'http_referer' => $_SERVER['HTTP_REFERER'], 'http_user_agent' => $_SERVER['HTTP_USER_AGENT'], 'added' => date("Y-m-d H:i:s"), ); $sql = "replace into {$this->tableNameDB}Click set "; foreach ($data as $k => $v) { $sql .= "{$k} = " . $this->db->quote($v) . ', '; } $sql = substr($sql, 0, -2); $this->db->query($sql); return true; } function getClick($id) { $res = array(); $id = (int)$id; $anc = $this->getFromDB($id); if (is_array($anc) && !empty($anc)) { $sql = "select count(*) from {$this->tableNameDB}Click where id_announcement = {$id}"; $res['total'] = (int)$this->db->queryOne($sql); if ($res['total'] > 0) { $sql = "select count(*) from {$this->tableNameDB}Click where id_announcement = {$id} and added like '" . date('Y-m-d') . "%'"; $res['today'] = (int)$this->db->queryOne($sql); $sql = "select count(*) from {$this->tableNameDB}Click where id_announcement = {$id} and added >= '" . $anc['changed'] . "%'"; $res['last_edit'] = (int)$this->db->queryOne($sql); } } return $res; } function getTypes($short = false) { if ($short) { $title = 'short_title'; } else { $title = 'title'; } $sql = "select id, {$title} from {$this->tableNameDB}Type where 1 order by sorter"; $res = $this->db->queryAll($sql, null, MDB_FETCHMODE_DEFAULT, true); if ($this->isDBError($res) || !is_array($res)) { $res = array(); } return $res; } function getCategories($short = false, $homeOnly = false) { if ($short) { $title = 'short_title'; } else { $title = 'title'; } $sql = "select id, {$title} from {$this->tableNameDB}Category where 1 order by sorter"; $res = $this->db->queryAll($sql, null, MDB_FETCHMODE_DEFAULT, true); if ($this->isDBError($res) || !is_array($res)) { $res = array(); } if ($homeOnly) { $tmp = array(); foreach ($res as $k => $v) { if (in_array($k, array(1, 2 ,3))) { $tmp[$k] = $v; } } $res = $tmp; } return $res; } function getEvents($includeEvent = null) { $event = SiteMap::getObj('Event/Event.php'); if (empty($this->eventsList)) { $start_date = $this->db->quote(date('Y-m-d H:i:s', mktime(0, 0, 0, (int)date('n') - 3))); $end_date = $this->db->quote(date('Y-m-d H:i:s', mktime(0, 0, 0, (int)date('n') + 3) + 86400)); $this->eventsList = $event->getList4Select("eventdate >= {$start_date} and eventdate <= {$end_date}"); } if (null !== $includeEvent) { $includeEvent = (int) $includeEvent; if (!array_key_exists($includeEvent, $this->eventsList)) { $title = $event->getFromDB($includeEvent, 'name'); if (!empty($title)) { $this->eventsList[$includeEvent] = $title; } } } return $this->eventsList; } function getHomeSorter() { return $this->getSorter('show_on_home = \'y\'', 'home_sorter'); } function initFromForm(&$form) { $files = array(); $files['image'] = $form->getElementValue('image'); $data = $form->exportValues(); $this->arrayParseUrl2Tag($data); $this->initFromArray($data, $files); return true; } function _initFile() { if (!is_object($this->file)) { require_once('class/DB/ImageDB.php'); $this->file = new ImageDB(FILE_DB_TS_FS); } return true; } function _initTable() { parent::_initTable(); if (!is_object($this->tableType)) { $this->tableType = new DBTable("{$this->tableName}2Type"); } return true; } function delete() { $sql = "delete from {$this->tableNameDB}Click where id_announcement = " . (int) $this->id; $this->db->query($sql); $this->_deleteReplies(); $this->_deleteTypes(); $this->_files = array( 'image' => array('del' => 1), ); parent::delete(); } function reorder($list, $field_name = 'sorter') { $this->_initTable(); foreach ((array)$list as $sorter => $id) { $this->table->update(array($field_name => $sorter), 'id = ' . intval($id)); } return true; } function getHome() { $data = array(); $sql = "select id from {$this->tableNameDB} where show_on_home = 'y' and id_category in (1, 2, 3) order by home_sorter"; $res = $this->db->queryCol($sql); if ($this->isDBError($res) || !is_array($res)) { $res = array(); } foreach ($res as $id) { $announcement = $this->getFromDB($id); $data[$announcement['id_category']][] = $this->getFromDB($id); } foreach ($data as &$announcements) { $this->_setAnnouncementCategory($announcements); } return $data; } function _setAnnouncementCategory(&$arr) { if (is_array($arr) && !empty($arr)) { $anc_categories = $this->getCategories(true); foreach ($arr as &$v) { //if ($v['id_category'] == 3) { // Event // if (!is_object($event)) $event = SiteMap::getObj('Event/Event.php'); // $ev = $event->getList4Grid(array('addonWhere' => 'and Event.id = ' . (int)$v['id_event'])); // $v['announcement_category_title'] = $ev['list'][0]['title']; //} else { $v['announcement_category_title'] = $anc_categories[$v['id_category']]; //} $v['id_type'] = $this->_getAnnouncementTypes($v['id']); } } return true; } function getList4Grid($opt = array()) { $list = parent::getList4Grid($opt); $this->_setAnnouncementCategory($list['list']); return $list; } function getList4SiteMap($opt = array()) { $list = array(); $posts = parent::getList4Grid($opt); $posts = $posts['list']; if (!empty($posts)) { foreach ($posts as $post) { $list[] = array( 'alias' => 'blog/' . $post['alias'], 'title' => $post['title'], 'block_cnt' => 1, 'changed' => $post['changed'], 'sub' => array(), ); } } return $list; } function getPolls() { $poll = SiteMap::getObj('Poll/Poll.php'); $polls = $poll->getList4Select(); return $polls; } function getIdByAlias($alias) { $sql = "select id from {$this->tableNameDB} where alias = " . $this->db->quote($alias) . " limit 1"; $id = (int) $this->db->queryOne($sql); return $id; } function prepareAlias($alias) { $alias = trim($alias); $alias = preg_replace("/[^a-zA-Z0-9 _-]+/", '', $alias); $alias = preg_replace("/\s+/", '-', $alias); $alias = preg_replace("/-{2,}/", '-', $alias); return $alias; } function generateUniqAlias($alias) { $id = (int) $this->id; $sql = "select 1 from {$this->tableNameDB} where alias = " . $this->db->quote($alias); if ($id) { $sql .= " and id != {$id}"; } if ($this->db->queryOne($sql)) { $sql = "select count(*) from {$this->tableNameDB} where alias like '" . $this->db->escape($alias) . "%'"; if ($id) { $sql .= " and id != {$id}"; } $i = (int) $this->db->queryOne($sql); $orig = $alias; do { $i++; $alias = $orig . '-' . $i; $sql = "select 1 from {$this->tableNameDB} where alias = " . $this->db->quote($alias); if ($id) { $sql .= " and id != {$id}"; } } while ($this->db->queryOne($sql)); } return $alias; } function insert($data = null, $files = null) { if (!is_null($data)) { $this->initFromArray($data, $files); } $this->_data['alias'] = trim($this->_data['alias']); if (empty($this->_data['alias'])) { $this->_data['alias'] = trim($this->_data['title']); } $this->_data['alias'] = $this->prepareAlias($this->_data['alias']); $this->_data['alias'] = $this->generateUniqAlias($this->_data['alias']); $this->id = parent::insert($data, $files); if (!$this->isDBError($this->id)) { if (!empty($this->_data['event_image']) && $this->_files['image']['del'] != 1 && $this->_files['image']['error'] != 0) { $this->_initFile(); $fileData = $this->file->getArr($this->_data['event_image']); if (!empty($fileData)) { $tmpName = tempnam(sys_get_temp_dir(), 'php'); if (copy(WWW_PATH . FILE_DB_FS_DIR . '/' . $fileData['name'], $tmpName)) { $image = array( 'name' => $fileData['title'], 'type' => $fileData['type'], 'size' => $fileData['size'], 'tmp_name' => $tmpName, 'error' => 0, 'del' => 0, ); $this->handleFile('image', $image); } } } $this->_insertTypes(); } return $this->id; } function insertImport($data = null, $files = null) { if (!is_null($data)) { $this->initFromArray($data, $files); } $this->_data['alias'] = trim($this->_data['title']); $this->_data['alias'] = $this->prepareAlias($this->_data['alias']); $this->_data['alias'] = $this->generateUniqAlias($this->_data['alias']); $this->_initTable(); $data = $this->_data; $fields = $this->getSimpleFieldList(); if (isset($fields['sorter'])) { $data['sorter'] = $this->getSorter(); } $this->table->insert($data); $this->id = $this->_data['id']; $this->handleFiles(); $this->_insertTypes(); return $this->id; } function update($data = null, $files = null) { if (!is_null($data)) { $this->initFromArray($data, $files); } $this->_data['alias'] = trim($this->_data['alias']); if (empty($this->_data['alias'])) { $this->_data['alias'] = trim($this->_data['title']); } $this->_data['alias'] = $this->prepareAlias($this->_data['alias']); $this->_data['alias'] = $this->generateUniqAlias($this->_data['alias']); parent::update($data, $files); $this->_deleteTypes(); $this->_insertTypes(); return true; } function _deleteTypes() { $this->_initTable(); return $this->tableType->delete("id_announcement = " . (int) $this->id); } function _deleteReplies() { $reply = SiteMap::getObj('Announcement/Reply/Reply.php', $this->id); $list = $reply->getList4Grid(); $list = $list['list']; if (is_array($list) && !empty($list)) { foreach ($list as $v) { $reply->id = (int) $v['id']; $reply->delete(); } } } function _insertTypes() { $types = $this->_data['id_type']; if (is_array($types) && !empty($types)) { $this->_initTable(); foreach ($types as $id_type) { $data = array('id_announcement' => $this->id, 'id_type' => $id_type); $this->tableType->insert($data); } } return true; } function _getAnnouncementTypes($id) { $id = (int) $id; $sql = "select id_type as idx, id_type from {$this->tableNameDB}2Type where id_announcement = {$id}"; $types = $this->db->queryAll($sql, null, MDB2_FETCHMODE_DEFAULT, true); if ($this->isDBError($types)) { $types = array(); } return $types; } function getFromDB($id, $field = false) { $data = parent::getFromDB($id); if (!empty($data)) { $data['id_type'] = $this->_getAnnouncementTypes($data['id']); } //$this->arrayParseTag2Url(&$data); return $this->getSubElem($data, $field); } function setFilter($filter) { $this->_filterQuery = (array) $filter; return $this; } function getFilter($field = null) { if (null === $field) { return $this->_filterQuery; } else if (array_key_exists($field, $this->_filterQuery)) { return $this->_filterQuery[$field]; } return null; } function updateFilter($filter) { if (!is_array($this->_filterQuery)) { $this->_filterQuery = array(); } $this->_filterQuery = array_merge($this->_filterQuery, (array)$filter); return $this; } function _filterSql() { $where = (string) DBObj_Filter::perseQr($this->db, $this->_filterFields, $this->_filterQuery['query']); $id_category = (int) $this->_filterQuery['id_category']; if ($id_category) { $where .= " AND {$this->tableName}.id_category = {$id_category}"; } $archives = $this->_filterQuery['archives']; if (!empty($archives)) { $where .= " AND {$this->tableName}.added LIKE '" . $this->db->escape($archives) . "%'"; } $excludeCategories = $this->_filterQuery['excludeCategories']; if (!empty($excludeCategories) && is_array($excludeCategories)) { foreach ($excludeCategories as &$v) { $v = (int) $v; } $where .= " AND {$this->tableName}.id_category NOT IN (" . implode(', ', $excludeCategories) . ")"; } return $where; } function _getWhat4Grid($opt = array()) { $what = parent::_getWhat4Grid($opt); $what .= ", (SELECT COUNT(ReplayTotal.id) FROM {$this->tableNameDB}Reply as ReplayTotal WHERE {$this->tableName}.id = ReplayTotal.id_announcement) AS comments_total"; $what .= ", (SELECT COUNT(ReplayDisapproved.id) FROM {$this->tableNameDB}Reply as ReplayDisapproved WHERE {$this->tableName}.id = ReplayDisapproved.id_announcement AND ReplayDisapproved.approved = 'n') AS comments_disapproved"; return $what; } function getSorter($where = '1', $field = 'sorter', $table_name_db = '') { if ($table_name_db == '') { $table_name_db = $this->tableNameDB; } $fields = $this->getSimpleFieldList($table_name_db); if (!isset($fields[$field])) { return false; } $sql = "update {$table_name_db} set {$field} = {$field} + 1 where " . $where; $this->db->query($sql); return 0; } function getArchive() { $sql = "select distinct date_format(added, '%Y-%m-01') as y from {$this->tableNameDB} where 1 order by y desc"; $list = $this->db->queryCol($sql); if ($this->isDBError($list)) { $list = array(); } return $list; } }