You Are Here : /var/www/virtual/tvarditsa.org/classes/ |
Current File : /var/www/virtual/tvarditsa.org/classes/news.class.php |
<?php /** * VSite Framework for PHP * * This product includes PHP, freely available from * <{@link http://www.php.net/ http://www.php.net/}> * * @package VSite_Framework * @subpackage Application * @copyright Copyright (c) 2003-2004, Valentin Sheiretsky * @license http://www.vsite.org/license.txt BSD-style license */ if (!defined("_VSITE_NEWS_CLASS_")) { /** * @ignore */ define("_VSITE_NEWS_CLASS_", 1); if (!defined("_PAGER_CLASS_")) { include (dirname(__FILE__) . '/../classes/pager.class.php'); } /** * News Class * * @version 2.0.2 * @author Valentin Sheiretsky <valio@vsite.org> * @package VSite_Framework */ class News extends Pager { /**#@+ * @access private */ var $_cfg = NULL; var $_cfg_News = array ( 'table' => 'news', 'fields' => array ( 'id' => 'id', 'category' => 'cat', 'headline' => 'title', 'content' => 'story', 'comments' => '', 'counter' => '', 'posted' => 'posted', ), 'strip_sent_delim' => '.?!', 'strip_word_delim' => ',;)}]+-=&', 'strip_more' => '...', 'strip_chars' => 1800, 'strip_sentences' => 6, 'image_path' => 'images/news/', 'highlight_prefix' => '<strong style="color: #FF0000">', 'highlight_sufix' => '</strong>', ); /**#@-*/ /** * Constructor */ function __construct($cfname = 'news') { $c = new VsiteConf(); $this->_cfg = $c->join_conf ($this->_cfg, $this->_cfg_News); $this->_cfg = $c->read_conf ($cfname, $this->_cfg); parent::__construct(); } /** * @ignore */ function News ($cfname = 'news') { $this->__construct($cfname); } /** * Get News Data */ function GetNews ($id = 0, $fields = '*') { $where = ''; if ($id > 0) { $where = $this->_cfg['fields']['id'].' = '.intval($id); } if ($this->_where != '') { if ($where == '') $where = $this->_where; else $where .= ' AND ( '.$this->_where.' )'; } $this->set_condition($where); $this->set_records(1); $rs = $this->create ($this->_cfg['table'], $fields, TRUE); if ($rs['total'] > 0) return $rs['data'][0]; return FALSE; } /** * Get News Data */ function ListNews ($fields = '*', $no_count = false) { return $this->create ($this->_cfg['table'], $fields, $no_count); } /** * Update News * * @param array news data */ function UpdateNews ($data) { if (!isset($data[$this->_cfg['fields']['id']])) return FALSE; $this->set_condition( $this->_cfg['fields']['id'].' = '.intval($data['id']) ); return $this->update ($this->_cfg['table'], $data); } /** * Delete News * * @param int news id */ function DeleteNews ($id) { $this->set_condition ($this->_cfg['fields']['id'].' = '.intval($id)); return $this->delete ($this->_cfg['table']); } /** * Generate Search Query * * @param string search request * @param string DB cell * @param boolean require all words, or any word * @param boolean require all cells, or any cell */ function SearchQuery ( $request = '', $thecell = '', $multi_req_all = true, $multi_cell_all = false ) { $query = ""; $flag = 1; $request = $this->ValidateSearch ($request); if ($request == "") return "1=1"; if ($multi_req_all) $oper = ' AND '; else $oper = ' OR '; if ($multi_cell_all) $oper_cell = ' AND '; else $oper_cell = ' OR '; $SearchArray = explode(' ', $request); while (list ($key, $val) = each ($SearchArray)) { if ($flag) $flag = 0; else $query .= $oper; if ($val[0] == '-') { $like = " NOT LIKE "; $val = substr ($val, 1); } else { $like = " LIKE "; } if (is_array($thecell)) { $subquery = ''; foreach ($thecell as $cell) { if ($subquery != '') $subquery .= $oper_cell; $subquery .= $cell.$like.$this->qstr("%".$val."%"); } $query .= '( '.$subquery.' )'; } else $query .= $thecell.$like.$this->qstr("%".$val."%"); } if ($flag) return "1=1"; return $query; } /** * Validate Search * * @param string request */ function ValidateSearch ($request) { $newrequest = strtr( $request, array("\"" => "", "\'" => "", "-" => " ") ); $query = ""; $request = ""; $flag = 1; $SearchArray = explode(' ', $newrequest); while (list ($key, $val) = each ($SearchArray)) { if (($val = trim($val)) == "") continue; if (strlen($val) < 3) continue; if ($request != "") $request .= " "; $request .= $val; } return $request; } /** * High Light Words * * @param string text * @param string words to highlight */ function HighLightWords ($text, $words = '') { if ($words == '') return $text; $WordsArray = explode(' ', $words); while (list ($key, $val) = each ($WordsArray)) { if (($val = trim($val)) == "") continue; $pattern = '/('. quotemeta($val) .')/i'; $replacement = quotemeta($this->_cfg['highlight_prefix']). '\\1'.quotemeta($this->_cfg['highlight_sufix']); $text = preg_replace($pattern, $replacement, $text); } return $text; } /** * StripStory * * @param string news story to strip * @param int max sentences * @param int max chars */ function StripStory ($story, $n = NULL, $maxc = NULL) { if ($n == NULL) $n = $this->_cfg['strip_sentences']; if ($maxc == NULL) $maxc = $this->_cfg['strip_chars']; $story_len = strlen($story); // ignore html tags $replaced_story = preg_replace( "/(<[^>]*>)/e", "str_pad(' ', strlen('\\1'))", $story ); // ignore urls $replaced_story = preg_replace( "/((http|ftp|mailto):\/\/[\.\-\w^\s]*[\s^\w\.\-])/e", "str_pad(' ', strlen('\\1'))", $replaced_story ); $sentences = preg_split( '/['.quotemeta($this->_cfg['strip_sent_delim']).']/', $replaced_story ); $p = 0; $counter = -1; $s_counter = 0; while (++$counter < sizeof($sentences)) { if (trim($sentences[$counter]) == '') { $p += strlen($sentences[$counter]) + 1; continue; } $s_counter++; $p += strlen($sentences[$counter]) + 1; if ($s_counter >= $n) break; if ($p > $maxc) break; } if ($p > $maxc) { return preg_replace( '/(.*)([\s'.quotemeta ( $this->_cfg['strip_sent_delim'].$this->_cfg['strip_word_delim'] ).']+[^\s'.quotemeta ( $this->_cfg['strip_sent_delim'].$this->_cfg['strip_word_delim'] ).']*)$/', "\\1", substr($story, 0, $maxc) ).$this->_cfg['strip_more']; } if (($p + 5) > $story_len) return $story; return substr($story, 0, $p - 1).$this->_cfg['strip_more']; } /** * ShowPicture * * @param string picture name * @param int alignment */ function ShowPicture ($pic_name, $a = 'left') { $root_path = $this->_cfg['image_path']; if (function_exists('GetImageSize')) { if ($pic_size = @GetImageSize($root_path.$pic_name)) $pic_src = '<img src="'.$root_path.$pic_name.'" '.$pic_size[3]. ' border="1" alt="" style="border-color: #000000;">'; else return ''; } else $pic_src = '<img src="'.$root_path.$pic_name. '" border="0" border="1" alt="" style="border-color: #000000;">'; return '<table border="0" cellspacing="0" cellpadding="3" align="'.$a. '"><tr><td valign="top">'.$pic_src.'</td></tr></table>'; } /** * CountYears */ function CountYears ($where = NULL) { $this->set_offset (0); $this->set_records (-1); if (!$where) $where = '1'; $this->set_condition ($where.' GROUP BY postedyear'); $this->set_order ('postedyear DESC'); return $this->create ( $this->_cfg['table'], 'YEAR('.$this->_cfg['fields']['posted'].') as postedyear', true ); } /** * CountMonths */ function CountMonths ($year, $where = NULL) { $this->set_offset (0); $this->set_records (-1); if (!$where) $where = '1'; $this->set_condition ( $where.' AND YEAR('.$this->_cfg['fields']['posted'].') = '.intval($year). ' GROUP BY postedmonth' ); $this->set_order ('postedmonth DESC'); return $this->create ( $this->_cfg['table'], 'MONTH('.$this->_cfg['fields']['posted'].') as postedmonth', true ); } /** * CountDays */ function CountDays ($year, $month, $where = NULL) { $this->set_offset (0); $this->set_records (-1); if (!$where) $where = '1'; $this->set_condition ( $where.' AND YEAR('.$this->_cfg['fields']['posted'].') = '.intval($year). ' AND MONTH('.$this->_cfg['fields']['posted'].') = '.intval($month). ' GROUP BY postedday' ); $this->set_order ('postedday DESC'); return $this->create ( $this->_cfg['table'], 'DAYOFMONTH('.$this->_cfg['fields']['posted'].') as postedday', true ); } /** * increment news counter */ function IncNewsCounter ($id) { $field = trim($this->_cfg['fields']['counter']); if ($field == "") return; $sql = 'UPDATE '.$this->_cfg['table'].' SET '; $sql .= '`'.$field.'` = `'.$field.'` + 1 WHERE `id` = '.intval($id); return $this->Execute($sql); } /** * increment news comments */ function IncNewsComments ($id) { $field = trim($this->_cfg['fields']['comments']); if ($field == "") return; $sql = 'UPDATE '.$this->_cfg['table'].' SET '; $sql .= '`'.$field.'` = `'.$field.'` + 1 WHERE `id` = '.intval($id); return $this->Execute($sql); } /** * Destructor */ function __destruct() { parent::__destruct(); } } } // END NEWS_CLASS ?>