source: companies/serpro/news_admin/inc/class.bonews.inc.php @ 903

Revision 903, 8.5 KB checked in by niltonneto, 15 years ago (diff)

Importacao inicial do Expresso do Serpro

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