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 if (function_exists('idn_to_utf8()')) {
44 function idn_to_utf8($param) {
45 return idn_to_unicode($param);
48 require_once('common/rss/simplepie.inc');
49 if (!is_dir(forge_get_config('sys_var_path') .'/rss')) {
50 mkdir(forge_get_config('sys_var_path') .'/rss');
52 $rss = new SimplePie($this->rss_url, forge_get_config('sys_var_path') .'/rss', null, forge_get_config('sys_proxy'));
54 $items = array_slice($rss->get_items(), 0, $max_items);
55 $content .= '<table class="fullwidth">';
57 foreach($items as $item){
59 $class="bgcolor-white";
62 $class="bgcolor-grey";
67 $content .= '<tr class="'. $class .'"><td WIDTH="99%">';
68 if ($image = $item->get_link(0, 'image')) {
69 //hack to display twitter avatar
70 $content .= '<img src="'. $hp->purify($image, CODENDI_PURIFIER_CONVERT_HTML) .'" style="float:left; margin-right:1em;" />';
72 $content .= '<a href="'. $item->get_link() .'">'. $item->get_title() .'</a>'; //Trust SimplePie for purifying
73 if ($item->get_date()) {
74 $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>';
76 $content .= '</td></tr>';
78 $content .= '</table>';
85 function hasPreferences() {
88 function getPreferences() {
89 $hp = Codendi_HTMLPurifier::instance();
91 $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>';
92 $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>';
96 function getInstallPreferences() {
99 $prefs .= '<tr><td>Url:</td><td><input type="text" class="textfield_medium" name="rss[url]" value="'. _("http://search.twitter.com/search.atom?q=fusionforge&show_user=1") .'" /></td></tr>';
100 $prefs .= '</table>';
103 function cloneContent($id, $owner_id, $owner_type) {
104 $sql = "INSERT INTO widget_rss (owner_id, owner_type, title, url)
105 SELECT $1, $2, title, url
107 WHERE owner_id = $3 AND owner_type = $4";
108 $res = db_query_params($sql,array($owner_id,$owner_type,$this->owner_id,$this->owner_type));
109 return db_insertid($res,'widget_rss','id');
111 function loadContent($id) {
112 $sql = "SELECT * FROM widget_rss WHERE owner_id = $1 AND owner_type = $2 AND id = $3";
113 $res = db_query_params($sql,array($this->owner_id,$this->owner_type,$id));
114 if ($res && db_numrows($res)) {
115 $data = db_fetch_array($res);
116 $this->rss_title = $data['title'];
117 $this->rss_url = $data['url'];
118 $this->content_id = $id;
121 function create(&$request) {
123 $vUrl = new Valid_String('url');
124 $vUrl->setErrorMessage("Can't add empty rss url");
126 if($request->validInArray('rss', $vUrl)) {
127 $rss = $request->get('rss');
128 $vTitle = new Valid_String('title');
130 if (!$request->validInArray('rss', $vTitle)) {
131 if (function_exists('idn_to_utf8()')) {
132 require_once('simplepie/simplepie.inc');
135 require_once('common/rss/simplepie.inc');
137 if (!is_dir(forge_get_config('sys_var_path') .'/rss')) {
138 mkdir(forge_get_config('sys_var_path') .'/rss');
140 $rss_reader = new SimplePie($rss['url'], forge_get_config('sys_var_path') .'/rss', null, forge_get_config('sys_proxy'));
141 $rss['title'] = $rss_reader->get_title();
143 $sql = 'INSERT INTO widget_rss (owner_id, owner_type, title, url) VALUES ($1,$2,$3,$4)';
144 $res = db_query_params($sql,array($this->owner_id,$this->owner_type,$rss['title'],$rss['url']));
145 $content_id = db_insertid($res, 'widget_rss', 'id');
149 function updatePreferences(&$request) {
151 $vContentId = new Valid_UInt('content_id');
152 $vContentId->required();
153 if (($rss = $request->get('rss')) && $request->valid($vContentId)) {
154 $vUrl = new Valid_String('url');
155 if($request->validInArray('rss', $vUrl)) {
161 $vTitle = new Valid_String('title');
162 if($request->validInArray('rss', $vTitle)) {
163 $title = $rss['title'] ;
168 if ($url || $title) {
169 $sql = "UPDATE widget_rss SET title=$1 , url=$2 WHERE owner_id =$3 AND owner_type = $4 AND id = $5";
170 $res = db_query_params($sql,array($title,$url,$this->owner_id,$this->owner_type,(int)$request->get('content_id')));
176 function destroy($id) {
177 $sql = 'DELETE FROM widget_rss WHERE id = $1 AND owner_id = $2 AND owner_type = $3';
178 db_query_params($sql,array($id,$this->owner_id,$this->owner_type));
180 function isUnique() {
183 function _date_ago($from_time, $to_time, $include_seconds = false) {
184 $distance_in_minutes = round((abs($to_time - $from_time))/60);
185 $distance_in_seconds = round(abs($to_time - $from_time));
187 if ($distance_in_minutes <= 1) {
188 return ($distance_in_minutes == 0) ? _('less than 1 minute') : _('1 minute');
189 } else if ($distance_in_minutes <= 44) {
190 return vsprintf(_('%s minutes ago'), $distance_in_minutes);
191 } else if ($distance_in_minutes <= 89) {
192 return _('About one hour') ;
193 } else if ($distance_in_minutes <= 1439) {
194 return vsprintf(_('about %s hours'), round($distance_in_minutes/60));
195 } else if ($distance_in_minutes <= 2879) {
196 return _('About one day') ;
197 } else if ($distance_in_minutes <= 43199) {
198 return vsprintf(_('%s days ago'), round($distance_in_minutes/1440));
199 } else if ($distance_in_minutes <= 86399) {
200 return _('About one month') ;
201 } else if ($distance_in_minutes <= 525959) {
202 return vsprintf(_('%s months ago'), round($distance_in_minutes/43200));
203 } else if ($distance_in_minutes <= 1051919) {
204 return _('About one year') ;
206 return vsprintf(_('over %s years'), round($distance_in_minutes/525960));