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){
64 $content .= '<tr '.$GLOBALS['HTML']->boxGetAltRowStyle($i).'><td WIDTH="99%">';
65 if ($image = $item->get_link(0, 'image')) {
66 //hack to display twitter avatar
67 $content .= '<img src="'. $hp->purify($image, CODENDI_PURIFIER_CONVERT_HTML) .'" style="float:left; margin-right:1em;" />';
69 $content .= '<a href="'. $item->get_link() .'">'. $item->get_title() .'</a>'; //Trust SimplePie for purifying
70 if ($item->get_date()) {
71 $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>';
73 $content .= '</td></tr>';
75 $content .= '</table>';
82 function hasPreferences() {
85 function getPreferences() {
86 $hp = Codendi_HTMLPurifier::instance();
88 $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>';
89 $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>';
93 function getInstallPreferences() {
96 $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 function cloneContent($id, $owner_id, $owner_type) {
101 $sql = "INSERT INTO widget_rss (owner_id, owner_type, title, url)
102 SELECT $1, $2, title, url
104 WHERE owner_id = $3 AND owner_type = $4";
105 $res = db_query_params($sql,array($owner_id,$owner_type,$this->owner_id,$this->owner_type));
106 return db_insertid($res,'widget_rss','id');
108 function loadContent($id) {
109 $sql = "SELECT * FROM widget_rss WHERE owner_id = $1 AND owner_type = $2 AND id = $3";
110 $res = db_query_params($sql,array($this->owner_id,$this->owner_type,$id));
111 if ($res && db_numrows($res)) {
112 $data = db_fetch_array($res);
113 $this->rss_title = $data['title'];
114 $this->rss_url = $data['url'];
115 $this->content_id = $id;
118 function create(&$request) {
120 $vUrl = new Valid_String('url');
121 $vUrl->setErrorMessage("Can't add empty rss url");
123 if($request->validInArray('rss', $vUrl)) {
124 $rss = $request->get('rss');
125 $vTitle = new Valid_String('title');
127 if (!$request->validInArray('rss', $vTitle)) {
128 if (function_exists('idn_to_utf8()')) {
129 require_once 'simplepie/simplepie.inc';
132 require_once 'common/rss/simplepie.inc';
134 if (!is_dir(forge_get_config('data_path') .'/rss')) {
135 mkdir(forge_get_config('data_path') .'/rss');
137 $rss_reader = new SimplePie($rss['url'], forge_get_config('data_path') .'/rss', null, forge_get_config('sys_proxy'));
138 $rss['title'] = $rss_reader->get_title();
140 $sql = 'INSERT INTO widget_rss (owner_id, owner_type, title, url) VALUES ($1,$2,$3,$4)';
141 $res = db_query_params($sql,array($this->owner_id,$this->owner_type,$rss['title'],$rss['url']));
142 $content_id = db_insertid($res, 'widget_rss', 'id');
146 function updatePreferences(&$request) {
148 $vContentId = new Valid_UInt('content_id');
149 $vContentId->required();
150 if (($rss = $request->get('rss')) && $request->valid($vContentId)) {
151 $vUrl = new Valid_String('url');
152 if($request->validInArray('rss', $vUrl)) {
158 $vTitle = new Valid_String('title');
159 if($request->validInArray('rss', $vTitle)) {
160 $title = $rss['title'] ;
165 if ($url || $title) {
166 $sql = "UPDATE widget_rss SET title=$1 , url=$2 WHERE owner_id =$3 AND owner_type = $4 AND id = $5";
167 $res = db_query_params($sql,array($title,$url,$this->owner_id,$this->owner_type,(int)$request->get('content_id')));
173 function destroy($id) {
174 $sql = 'DELETE FROM widget_rss WHERE id = $1 AND owner_id = $2 AND owner_type = $3';
175 db_query_params($sql,array($id,$this->owner_id,$this->owner_type));
177 function isUnique() {
180 function _date_ago($from_time, $to_time, $include_seconds = false) {
181 $distance_in_minutes = round((abs($to_time - $from_time))/60);
182 $distance_in_seconds = round(abs($to_time - $from_time));
184 if ($distance_in_minutes <= 1) {
185 return ($distance_in_minutes == 0) ? _('less than 1 minute') : _('1 minute');
186 } elseif ($distance_in_minutes <= 44) {
187 return vsprintf(_('%s minutes ago'), $distance_in_minutes);
188 } elseif ($distance_in_minutes <= 89) {
189 return _('About one hour') ;
190 } elseif ($distance_in_minutes <= 1439) {
191 return vsprintf(_('about %s hours'), round($distance_in_minutes/60));
192 } elseif ($distance_in_minutes <= 2879) {
193 return _('About one day') ;
194 } elseif ($distance_in_minutes <= 43199) {
195 return vsprintf(_('%s days ago'), round($distance_in_minutes/1440));
196 } elseif ($distance_in_minutes <= 86399) {
197 return _('About one month') ;
198 } elseif ($distance_in_minutes <= 525959) {
199 return vsprintf(_('%s months ago'), round($distance_in_minutes/43200));
200 } elseif ($distance_in_minutes <= 1051919) {
201 return _('About one year') ;
203 return vsprintf(_('over %s years'), round($distance_in_minutes/525960));