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.bonews.inc.php 21076 2010-03-25 22:53:13Z Caeies $ */
16
17	class bonews
18	{
19		var $sonews;
20		var $acl;
21		var $start = 0;
22		var $query = '';
23		var $sort  = '';
24		var $cat_id;
25		var $total = 0;
26		var $debug;
27		var $use_session = False;
28		var $unixtimestampmax;
29		var $dateformat;
30
31		function bonews($session=False)
32		{
33			$this->acl = CreateObject('news_admin.boacl');
34			$this->sonews = CreateObject('news_admin.sonews');
35			$this->accounts = $GLOBALS['phpgw']->accounts->get_list();
36			$this->debug = False;
37			if($session)
38			{
39				$this->read_sessiondata();
40				$this->use_session = True;
41				foreach(array('start','query','sort','order','cat_id') as $var)
42				{
43					$this->$var = get_var($var, array('POST', 'GET'), '');
44				}
45
46				$this->cat_id = $this->cat_id ? $this->cat_id : 'all';
47				$this->save_sessiondata();
48			}
49			$this->catbo = createobject('phpgwapi.categories','','news_admin');
50			$this->cats = $this->catbo->return_array('all',0,False,'','','cat_name',True);
51			//change this around 19 Jan 2038 03:14:07 GMT
52			$this->unixtimestampmax = 2147483647;
53			$this->dateformat = $GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat'];
54		}
55
56		function save_sessiondata()
57		{
58			$data = array(
59				'start' => $this->start,
60				'query' => $this->query,
61				'sort'  => $this->sort,
62				'order' => $this->order,
63				'cat_id' => $this->cat_id,
64			);
65			if($this->debug) { echo '<br>Save:'; _debug_array($data); }
66			$GLOBALS['phpgw']->session->appsession('session_data','news_admin',$data);
67		}
68
69		function read_sessiondata()
70		{
71			$data = $GLOBALS['phpgw']->session->appsession('session_data','news_admin');
72			if($this->debug) { echo '<br>Read:'; _debug_array($data); }
73
74			$this->start  = $data['start'];
75			$this->query  = $data['query'];
76			$this->sort   = $data['sort'];
77			$this->order  = $data['order'];
78			$this->cat_id = $data['cat_id'];
79		}
80
81		function get_newslist($cat_id, $start=0, $order='',$sort='',$limit=0,$activeonly=False)
82		{
83			$cats = False;
84			if ($cat_id == 'all')
85			{
86				if(! ($this->cats && is_array($this->cats)) )
87				{
88					$this->cats = array();
89				}
90				foreach($this->cats as $cat)
91				{
92					if ($this->acl->is_readable($cat['id']))
93					{
94						$cats[] = $cat['id'];
95					}
96				}
97			}
98			elseif($this->acl->is_readable($cat_id))
99			{
100				$cats = $cat_id;
101			}
102
103			if($cats)
104			{
105				$news = $this->sonews->get_newslist($cats, $start,$order,$sort,$limit,$activeonly,$this->total);
106				foreach($news as $id => $item)
107				{
108					$news[$id]['content'] = ($item['is_html'] ? $item['content'] : nl2br(htmlentities($item['content'])));
109				}
110				return $news;
111			}
112			else
113			{
114				return array();
115			}
116		}
117
118		function get_all_public_news($limit = 5)
119		{
120			$news = $this->sonews->get_all_public_news($limit);
121			foreach($news as $id => $item)
122			{
123				$news[$id]['content'] = ($item['is_html'] ? $item['content'] : nl2br(htmlentities($item['content'])));
124			}
125			return $news;
126		}
127
128		function delete($news_id)
129		{
130			$this->sonews->delete($news_id);
131		}
132
133		function add($news)
134		{
135			return $this->acl->is_writeable($news['category']) ?
136				$this->sonews->add($news) :
137				false;
138		}
139
140		function edit($news)
141		{
142			$oldnews = $this->sonews->get_news($news['id']);
143			return ($this->acl->is_writeable($oldnews['category']) &&
144					$this->acl->is_writeable($news['category'])) ?
145				$this->sonews->edit($news) :
146				False;
147		}
148
149		function get_visibility(&$news)
150		{
151			$now = time();
152
153			if ($news['end'] < $now)
154			{
155				return lang('Never');
156			}
157			else
158			{
159				if ($news['begin'] < $now)
160				{
161					if ($news['end'] == $this->unixtimestampmax)
162					{
163						return lang('Always');
164					}
165					else
166					{
167						return lang('until') . date($this->dateformat,$news['end']);
168					}
169				}
170				else
171				{
172					if ($news['end'] == $this->unixtimestampmax)
173					{
174						return lang('from') . date($this->dateformat,$news['begin']);
175
176					}
177					else
178					{
179						return lang('from') . ' ' . date($this->dateformat,$news['begin']) . ' ' .
180							lang('until') . ' ' . date($this->dateformat,$news['end']);
181					}
182				}
183			}
184		}
185
186		//return the selectboxes with calculated defaults, and change begin and end by sideaffect
187		function get_options(&$news)
188		{
189			$now = time();
190			//always is default
191			if (!isset($news['begin']))
192			{
193				//these are only displayed values not necessarily the ones that will get stored
194				$news['begin'] = $now;
195				$news['end'] = $now;
196				$from = 1;
197				$until = 1;
198			}
199			//if enddate is in the past set option to never
200			elseif ($news['end'] < $now)
201			{
202				$news['begin'] = $now;
203				$news['end'] = $now;
204				$from = 0;
205				$until = 1;
206			}
207			else
208			{
209				if ($news['begin'] < $now)
210				{
211					$news['begin'] = $now;
212					if ($news['end'] == $this->unixtimestampmax)
213					{
214						$news['end'] = $now;
215						$from = 1;
216						$until = 1;
217					}
218					else
219					{
220						$from = 0.5;
221						$until = 0.5;
222					}
223				}
224				else
225				{
226					if ($news['end'] == $this->unixtimestampmax)
227					{
228						$news['end'] = $now;
229						$from = 0.5;
230						$until = 1;
231					}
232					else
233					{
234						$from = 0.5;
235						$until = 0.5;
236					}
237				}
238			}
239			$options['from'] = '<option value="1"' . (($from == 1) ? ' selected="selected"' : '') . '>' . lang('Always') . '</option>';
240			$options['from'] .= '<option value="0"' . (($from == 0) ? ' selected="selected"' : '') . '>' . lang('Never') . '</option>';
241			$options['from'] .= '<option value="0.5"' . (($from == 0.5) ? ' selected="selected"' : '') . '>' . lang('From') . '</option>';
242			$options['until'] = '<option value="1"' . (($until == 1) ? ' selected="selected"' : '') . '>' . lang('Always') . '</option>';
243			$options['until'] .= '<option value="0.5"' . (($until == 0.5) ? ' selected="selected"' : '') . '>' . lang('until') . '</option>';
244			return $options;
245		}
246
247		//set the begin and end dates
248		function set_dates($from,$until,&$news)
249		{
250			switch($from)
251			{
252				//always
253				case 1:
254					$news['begin'] = $news['date'];
255					$news['end'] = $this->unixtimestampmax;
256					break;
257				//never
258				case 0:
259					$news['begin'] = 0;
260					$news['end'] = 0;
261					break;
262				default:
263					$news['begin'] = mktime(0,0,0,intval($news['begin_m']), intval($news['begin_d']), intval($news['begin_y']));
264					switch($until)
265					{
266						case 1:
267							$news['end'] = $this->unixtimestampmax;
268							break;
269						default:
270							$news['end'] = mktime(0,0,0,intval($news['end_m']), intval($news['end_d']), intval($news['end_y']));
271					}
272			}
273		}
274
275// 		function format_fields($fields)
276// 		{
277// 			$cat = createobject('phpgwapi.categories','news_admin');
278
279// 			$item = array(
280// 				'id'          => $fields['id'],
281// 				'date'        => $GLOBALS['phpgw']->common->show_date($fields['date']),
282// 				'subject'     => $GLOBALS['phpgw']->strip_html($fields['subject']),
283// 				'submittedby' => $fields['submittedby'],
284// 				'content'     => $fields['content'],
285// 				'status'      => lang($fields['status']),
286// 				'cat'         => $cat->id2name($fields['cat'])
287// 			);
288// 			return $item;
289// 		}
290
291		function get_news($news_id)
292		{
293			$news = $this->sonews->get_news($news_id);
294			//echo '<br />BO:<br />'; print_r($news); echo '</pre>';
295			if ($this->acl->is_readable($news['category']))
296			{
297				$this->total = 1;
298				$news['content'] = ($news['is_html'] ? $news['content']: htmlentities($news['content']));
299				//echo '<br />BO2:<br />'; print_r($news); echo '</pre>';
300				return $news;
301			}
302			else
303			{
304				 return False;
305			}
306		}
307	}
308?>
309