3 * Generic RSS Widget Class
5 * Copyright (c) Xerox Corporation, Codendi Team, 2001-2009. All rights reserved
6 * Copyright 2012, Franck Villaume - TrivialDev
7 * http://fusionforge.org
9 * This file is a part of Fusionforge.
11 * Fusionforge is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * Fusionforge is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with Fusionforge. If not, see <http://www.gnu.org/licenses/>.
25 require_once('Widget.class.php');
32 /* abstract */ class Widget_Rss extends Widget {
35 function Widget_Rss($id, $owner_id, $owner_type) {
37 $this->setOwner($owner_id, $owner_type);
40 $hp = Codendi_HTMLPurifier::instance();
41 return $this->rss_title ? $hp->purify($this->rss_title, CODENDI_PURIFIER_CONVERT_HTML) : 'RSS Reader';
43 function getContent() {
44 $hp = Codendi_HTMLPurifier::instance();
47 if (function_exists('idn_to_utf8()')) {
48 function idn_to_utf8($param) {
49 return idn_to_unicode($param);
52 require_once('common/rss/simplepie.inc');
53 if (!is_dir(forge_get_config('data_path') .'/rss')) {
54 mkdir(forge_get_config('data_path') .'/rss');
56 $rss = new SimplePie($this->rss_url, forge_get_config('data_path') .'/rss', null, forge_get_config('sys_proxy'));
58 $items = array_slice($rss->get_items(), 0, $max_items);
59 $content .= '<table class="fullwidth">';
61 foreach($items as $item){
63 $class="bgcolor-white";
66 $class="bgcolor-grey";
71 $content .= '<tr class="'. $class .'"><td WIDTH="99%">';
72 if ($image = $item->get_link(0, 'image')) {
73 //hack to display twitter avatar
74 $content .= '<img src="'. $hp->purify($image, CODENDI_PURIFIER_CONVERT_HTML) .'" style="float:left; margin-right:1em;" />';
76 $content .= '<a href="'. $item->get_link() .'">'. $item->get_title() .'</a>'; //Trust SimplePie for purifying
77 if ($item->get_date()) {
78 $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>';
80 $content .= '</td></tr>';
82 $content .= '</table>';
89 function hasPreferences() {
92 function getPreferences() {
93 $hp = Codendi_HTMLPurifier::instance();
95 $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>';
96 $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>';
100 function getInstallPreferences() {
103 $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>';
104 $prefs .= '</table>';
107 function cloneContent($id, $owner_id, $owner_type) {
108 $sql = "INSERT INTO widget_rss (owner_id, owner_type, title, url)
109 SELECT $1, $2, title, url
111 WHERE owner_id = $3 AND owner_type = $4";
112 $res = db_query_params($sql,array($owner_id,$owner_type,$this->owner_id,$this->owner_type));
113 return db_insertid($res,'widget_rss','id');
115 function loadContent($id) {
116 $sql = "SELECT * FROM widget_rss WHERE owner_id = $1 AND owner_type = $2 AND id = $3";
117 $res = db_query_params($sql,array($this->owner_id,$this->owner_type,$id));
118 if ($res && db_numrows($res)) {
119 $data = db_fetch_array($res);
120 $this->rss_title = $data['title'];
121 $this->rss_url = $data['url'];
122 $this->content_id = $id;
125 function create(&$request) {
127 $vUrl = new Valid_String('url');
128 $vUrl->setErrorMessage("Can't add empty rss url");
130 if($request->validInArray('rss', $vUrl)) {
131 $rss = $request->get('rss');
132 $vTitle = new Valid_String('title');
134 if (!$request->validInArray('rss', $vTitle)) {
135 if (function_exists('idn_to_utf8()')) {
136 require_once('simplepie/simplepie.inc');
139 require_once('common/rss/simplepie.inc');
141 if (!is_dir(forge_get_config('data_path') .'/rss')) {
142 mkdir(forge_get_config('data_path') .'/rss');
144 $rss_reader = new SimplePie($rss['url'], forge_get_config('data_path') .'/rss', null, forge_get_config('sys_proxy'));
145 $rss['title'] = $rss_reader->get_title();
147 $sql = 'INSERT INTO widget_rss (owner_id, owner_type, title, url) VALUES ($1,$2,$3,$4)';
148 $res = db_query_params($sql,array($this->owner_id,$this->owner_type,$rss['title'],$rss['url']));
149 $content_id = db_insertid($res, 'widget_rss', 'id');
153 function updatePreferences(&$request) {
155 $vContentId = new Valid_UInt('content_id');
156 $vContentId->required();
157 if (($rss = $request->get('rss')) && $request->valid($vContentId)) {
158 $vUrl = new Valid_String('url');
159 if($request->validInArray('rss', $vUrl)) {
165 $vTitle = new Valid_String('title');
166 if($request->validInArray('rss', $vTitle)) {
167 $title = $rss['title'] ;
172 if ($url || $title) {
173 $sql = "UPDATE widget_rss SET title=$1 , url=$2 WHERE owner_id =$3 AND owner_type = $4 AND id = $5";
174 $res = db_query_params($sql,array($title,$url,$this->owner_id,$this->owner_type,(int)$request->get('content_id')));
180 function destroy($id) {
181 $sql = 'DELETE FROM widget_rss WHERE id = $1 AND owner_id = $2 AND owner_type = $3';
182 db_query_params($sql,array($id,$this->owner_id,$this->owner_type));
184 function isUnique() {
187 function _date_ago($from_time, $to_time, $include_seconds = false) {
188 $distance_in_minutes = round((abs($to_time - $from_time))/60);
189 $distance_in_seconds = round(abs($to_time - $from_time));
191 if ($distance_in_minutes <= 1) {
192 return ($distance_in_minutes == 0) ? _('less than 1 minute') : _('1 minute');
193 } else if ($distance_in_minutes <= 44) {
194 return vsprintf(_('%s minutes ago'), $distance_in_minutes);
195 } else if ($distance_in_minutes <= 89) {
196 return _('About one hour') ;
197 } else if ($distance_in_minutes <= 1439) {
198 return vsprintf(_('about %s hours'), round($distance_in_minutes/60));
199 } else if ($distance_in_minutes <= 2879) {
200 return _('About one day') ;
201 } else if ($distance_in_minutes <= 43199) {
202 return vsprintf(_('%s days ago'), round($distance_in_minutes/1440));
203 } else if ($distance_in_minutes <= 86399) {
204 return _('About one month') ;
205 } else if ($distance_in_minutes <= 525959) {
206 return vsprintf(_('%s months ago'), round($distance_in_minutes/43200));
207 } else if ($distance_in_minutes <= 1051919) {
208 return _('About one year') ;
210 return vsprintf(_('over %s years'), round($distance_in_minutes/525960));