3 * Copyright (c) Xerox Corporation, Codendi Team, 2001-2009. All rights reserved
5 * This file is a part of Codendi.
7 * Codendi is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * Codendi is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Codendi. If not, see <http://www.gnu.org/licenses/>.
21 require_once('Widget.class.php');
28 /* abstract */ class Widget_Rss extends Widget {
31 function Widget_Rss($id, $owner_id, $owner_type) {
33 $this->setOwner($owner_id, $owner_type);
36 $hp = Codendi_HTMLPurifier::instance();
37 return $this->rss_title ? $hp->purify($this->rss_title, CODENDI_PURIFIER_CONVERT_HTML) : 'RSS Reader';
39 function getContent() {
40 $hp = Codendi_HTMLPurifier::instance();
43 require_once('simplepie.inc');
44 if (!is_dir($GLOBALS['sys_var_path'] .'/rss')) {
45 mkdir($GLOBALS['sys_var_path'] .'/rss');
47 $rss =& new SimplePie($this->rss_url, $GLOBALS['sys_var_path'] .'/rss', null, $GLOBALS['sys_proxy']);
49 $items = array_slice($rss->get_items(), 0, $max_items);
50 $content .= '<table width="100%">';
52 foreach($items as $item){
54 $class="boxitemalt bgcolor-white";
56 $class="boxitem bgcolor-grey";
60 $content .= '<tr class="'. $class .'"><td WIDTH="99%">';
61 if ($image = $item->get_link(0, 'image')) {
62 //hack to display twitter avatar
63 $content .= '<img src="'. $hp->purify($image, CODENDI_PURIFIER_CONVERT_HTML) .'" style="float:left; margin-right:1em;" />';
65 $content .= '<a href="'. $item->get_link() .'">'. $item->get_title() .'</a>'; //Trust SimplePie for purifying
66 if ($item->get_date()) {
67 $content .= '<span style="color:#999;" title="'. date(_("Y-m-d H:i"), $item->get_date('U')) .'"> - '. $this->_date_ago($item->get_date('U'),time()) .'</span>';
69 $content .= '</td></tr>';
71 $content .= '</table>';
78 function hasPreferences() {
81 function getPreferences() {
82 $hp = Codendi_HTMLPurifier::instance();
84 $prefs .= '<table><tr><td>Title:</td><td><input type="text" class="textfield_medium" name="rss[title]" value="'. $hp->purify($this->rss_title, CODENDI_PURIFIER_CONVERT_HTML) .'" /></td></tr>';
85 $prefs .= '<tr><td>Url:</td><td><input type="text" class="textfield_medium" name="rss[url]" value="'. $hp->purify($this->rss_url, CODENDI_PURIFIER_CONVERT_HTML) .'" /></td></tr>';
89 function getInstallPreferences() {
92 $prefs .= '<tr><td>Url:</td><td><input type="text" class="textfield_medium" name="rss[url]" value="'. _("http://search.twitter.com/search.atom?q=codendi&show_user=1") .'" /></td></tr>';
96 function cloneContent($id, $owner_id, $owner_type) {
97 $sql = "INSERT INTO widget_rss (owner_id, owner_type, title, url)
98 SELECT $1, $2, title, url
100 WHERE owner_id = $3 AND owner_type = $4";
101 $res = db_query_params($sql,array($owner_id,$owner_type,$this->owner_id,$this->owner_type));
102 return db_insertid($res,'widget_rss','id');
104 function loadContent($id) {
105 $sql = "SELECT * FROM widget_rss WHERE owner_id = $1 AND owner_type = $2 AND id = $3";
106 $res = db_query_params($sql,array($this->owner_id,$this->owner_type,$id));
107 if ($res && db_numrows($res)) {
108 $data = db_fetch_array($res);
109 $this->rss_title = $data['title'];
110 $this->rss_url = $data['url'];
111 $this->content_id = $id;
114 function create(&$request) {
116 $vUrl = new Valid_String('url');
117 $vUrl->setErrorMessage("Can't add empty rss url");
119 if($request->validInArray('rss', $vUrl)) {
120 $rss = $request->get('rss');
121 $vTitle = new Valid_String('title');
123 if (!$request->validInArray('rss', $vTitle)) {
124 require_once('common/rss/libs/SimplePie/simplepie.inc');
125 if (!is_dir($GLOBALS['sys_var_path'] .'/rss')) {
126 mkdir($GLOBALS['sys_var_path'] .'/rss');
128 $rss_reader =& new SimplePie($rss['url'], $GLOBALS['sys_var_path'] .'/rss', null, 'http://p-goodway:3128');
129 $rss['title'] = $rss_reader->get_title();
131 $sql = 'INSERT INTO widget_rss (owner_id, owner_type, title, url) VALUES ($1,$2,$3,$4)';
132 $res = db_query_params($sql,array($this->owner_id,$this->owner_type,$rss['title'],$rss['url']));
133 $content_id = db_insertid($res, 'widget_rss', 'id');
137 function updatePreferences(&$request) {
139 $vContentId = new Valid_UInt('content_id');
140 $vContentId->required();
141 if (($rss = $request->get('rss')) && $request->valid($vContentId)) {
142 $vUrl = new Valid_String('url');
143 if($request->validInArray('rss', $vUrl)) {
149 $vTitle = new Valid_String('title');
150 if($request->validInArray('rss', $vTitle)) {
151 $title = $rss['title'] ;
156 if ($url || $title) {
157 $sql = "UPDATE widget_rss SET title=$1 , url=$2 WHERE owner_id =$3 AND owner_type = $4 AND id = $5";
158 $res = db_query_params($sql,array($title,$url,$this->owner_id,$this->owner_type,(int)$request->get('content_id')));
164 function destroy($id) {
165 $sql = 'DELETE FROM widget_rss WHERE id = $1 AND owner_id = $2AND owner_type = $3';
166 db_query_params($sql,array($id,$this->owner_id,$this->owner_type));
168 function isUnique() {
171 function _date_ago($from_time, $to_time, $include_seconds = false) {
172 $distance_in_minutes = round((abs($to_time - $from_time))/60);
173 $distance_in_seconds = round(abs($to_time - $from_time));
175 if ($distance_in_minutes <= 1) {
176 return ($distance_in_minutes == 0) ? _('less than 1 minute') : _('1 minute');
177 } else if ($distance_in_minutes <= 44) {
178 return vsprintf(_('%s minutes ago'), $distance_in_minutes);
179 } else if ($distance_in_minutes <= 89) {
180 return _('About one hour') ;
181 } else if ($distance_in_minutes <= 1439) {
182 return vsprintf(_('about %s hours'), round($distance_in_minutes/60));
183 } else if ($distance_in_minutes <= 2879) {
184 return _('About one day') ;
185 } else if ($distance_in_minutes <= 43199) {
186 return vsprintf(_('%s days ago'), round($distance_in_minutes/1440));
187 } else if ($distance_in_minutes <= 86399) {
188 return _('About one month') ;
189 } else if ($distance_in_minutes <= 525959) {
190 return vsprintf(_('%s months ago'), round($distance_in_minutes/43200));
191 } else if ($distance_in_minutes <= 1051919) {
192 return _('About one year') ;
194 return vsprintf(_('over %s years'), round($distance_in_minutes/525960));