1<?php
2	/**************************************************************************\
3	* phpGroupWare - News                                                      *
4	* http://www.phpgroupware.org                                              *
5	* --------------------------------------------                             *
6	*  This program is free software; you can redistribute it and/or modify it *
7	*  under the terms of the GNU General Public License as published by the   *
8	*  Free Software Foundation; either version 2 of the License, or (at your  *
9	*  option) any later version.                                              *
10	* --------------------------------------------                             *
11	* This program was sponsered by Golden Glair productions                   *
12	* http://www.goldenglair.com                                               *
13	\**************************************************************************/
14
15	/* $Id: class.sonews.inc.php 21076 2010-03-25 22:53:13Z Caeies $ */
16
17	class sonews
18	{
19		var $db;
20
21		function sonews()
22		{
23			$this->db       = $GLOBALS['phpgw']->db;
24		}
25
26
27		function get_newslist($cat_id, $start, $order,$sort,$limit=0,$activeonly,&$total)
28		{
29			if ($order)
30			{
31				$ordermethod = ' ORDER BY ' . $this->db->db_addslashes($order) . ' ' . $this->db->db_addslashes($sort);
32			}
33			else
34			{
35				$ordermethod = ' ORDER BY news_date DESC';
36			}
37
38			if (is_array($cat_id))
39			{
40				$filter = "IN (" . implode(',',$cat_id) . ')';
41			}
42			else
43			{
44				$filter = "=" . intval($cat_id);
45			}
46
47			$sql = 'SELECT * FROM phpgw_news WHERE news_cat ' . $filter;
48			if ($activeonly)
49			{
50				$now = time();
51				$sql .= " AND news_begin<=$now AND news_end>=$now";
52			}
53			$sql .= $ordermethod;
54
55			$this->db->query($sql,__LINE__,__FILE__);
56			$total = $this->db->num_rows();
57			$this->db->limit_query($sql,$start,__LINE__,__FILE__,$limit);
58
59			$news = array();
60
61			while ($this->db->next_record())
62			{
63				$news[$this->db->f('news_id')] = array(
64					'subject'	=> htmlentities($this->db->f('news_subject', True)),
65					'submittedby'	=> $this->db->f('news_submittedby'),
66					'date'		=> $this->db->f('news_date'),
67					'id'		=> $this->db->f('news_id'),
68					'begin'		=> $this->db->f('news_begin'),
69					'end'		=> $this->db->f('news_end'),
70					'teaser'	=> htmlentities($this->db->f('news_teaser', True)),
71					'content'	=> $this->db->f('news_content',True),
72					'is_html'	=> ($this->db->f('is_html') ? True : False),
73				);
74			}
75			return $news;
76		}
77
78		function get_all_public_news($limit=5)
79		{
80			$now = time();
81			$this->db->limit_query("SELECT * FROM phpgw_news WHERE news_begin<=$now AND news_end>=$now ORDER BY news_date DESC",0,__LINE__,__FILE__,$limit);
82
83			$news = array();
84
85			while ($this->db->next_record())
86			{
87				$news[$this->db->f('news_id')] = array(
88					'subject'	=> $this->db->f('news_subject', True),
89					'submittedby'	=> $this->db->f('news_submittedby'),
90					'date'		=> $this->db->f('news_date'),
91					'id'		=> $this->db->f('news_id'),
92					'teaser'	=> $this->db->f('news_teaser', True),
93					'content'	=> $this->db->f('news_content', True),
94					'is_html'	=> ($this->db->f('is_html') ? True : False),
95				);
96			}
97			return $news;
98		}
99
100		function add($news)
101		{
102			$sql  = 'INSERT INTO phpgw_news '
103				. ' (news_date, news_submittedby, news_content, news_subject, news_begin,'
104				. ' news_end,news_teaser,news_cat,is_html) '
105				. 'VALUES (' . intval($news['date'])  . ','
106				. $GLOBALS['phpgw_info']['user']['account_id'] . ','
107				. "'" . $this->db->db_addslashes($news['content']) ."',"
108				. "'" . $this->db->db_addslashes($news['subject']) ."',"
109				. intval($news['begin']) . ','
110				. intval($news['end']) . ','
111				. "'" . $this->db->db_addslashes($news['teaser']) . "',"
112				. intval($news['category']) . ',' . intval($news['is_html']) .')';
113
114			$this->db->query($sql, __LINE__, __FILE__);
115
116			return $this->db->get_last_insert_id('phpgw_news', 'news_id');
117		}
118
119		function edit($news)
120		{
121			//echo '<br />SO-save:<pre>'; print_r($news); echo '</pre>';
122			$this->db->query("UPDATE phpgw_news SET "
123				. "news_content='" . $this->db->db_addslashes($news['content']) . "',"
124				. "news_subject='" . $this->db->db_addslashes($news['subject']) . "', "
125				. "news_teaser='" . $this->db->db_addslashes($news['teaser']) . "', "
126				. 'news_begin=' . intval($news['begin']) . ', '
127				. 'news_end=' . intval($news['end']) . ', '
128				. 'news_cat=' . intval($news['category']) . ', '
129				. 'is_html=' . ($news['is_html'] ? 1 : 0) .' '
130				. 'WHERE news_id=' . intval($news['id']),__LINE__,__FILE__);
131		}
132
133		function delete($news_id)
134		{
135			$this->db->query('DELETE FROM phpgw_news WHERE news_id=' . intval($news_id) ,__LINE__,__FILE__);
136		}
137
138		function get_news($news_id)
139		{
140			$this->db->query('SELECT * FROM phpgw_news WHERE news_id=' . intval($news_id),__LINE__,__FILE__);
141			$this->db->next_record();
142
143			$item = array(
144				'id'		=> $this->db->f('news_id'),
145				'date'		=> $this->db->f('news_date'),
146				'subject'	=> $this->db->f('news_subject', True),
147				'submittedby'	=> $this->db->f('news_submittedby'),
148				'teaser'	=> $this->db->f('news_teaser', True),
149				'content'	=> $this->db->f('news_content', True),
150				'begin'		=> $this->db->f('news_begin'),
151				'end' 		=> $this->db->f('news_end'),
152				'category'	=> $this->db->f('news_cat'),
153				'is_html'	=> ($this->db->f('is_html') ? True : False),
154			);
155			//echo '<pre>'; print_r($item); echo '</pre>';
156			return $item;
157		}
158
159// 		function getlist($order,$sort,$cat_id)
160// 		{
161// 			if ($order)
162// 			{
163// 				$ordermethod = ' ORDER BY ' . $this->db->db_addslashes($order) . ' ' . $this->db->db_addslashes($sort);
164// 			}
165// 			else
166// 			{
167// 				$ordermethod = ' ORDER BY news_date DESC';
168// 			}
169
170// 			$this->db->query('SELECT * FROM phpgw_news WHERE news_cat=' . intval($cat_id) . $ordermethod,__LINE__,__FILE__);
171// 			while ($this->db->next_record())
172// 			{
173// 				$items[] = array(
174// 					'id'          => $this->db->f('news_id'),
175// 					'date'        => $this->db->f('news_date'),
176// 					'subject'     => $this->db->f('news_subject'),
177// 					'submittedby' => $this->db->f('news_submittedby'),
178// 					'content'     => $this->db->f('news_content'),
179// 					'status'      => $this->db->f('news_status'),
180// 					'cat'         => $this->db->f('news_cat')
181// 				);
182// 			}
183// 			return $items;
184// 		}
185
186	}
187