3 * Generic RSS Widget Class
5 * Copyright (c) Xerox Corporation, Codendi Team, 2001-2009. All rights reserved
6 * Copyright 2012,2014, 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() {
45 $hp = Codendi_HTMLPurifier::instance();
48 if (function_exists('idn_to_utf8()')) {
49 function idn_to_utf8($param) {
50 return idn_to_unicode($param);
53 require_once 'common/rss/simplepie.inc';
54 if (!is_dir(forge_get_config('data_path') .'/rss')) {
55 if (!mkdir(forge_get_config('data_path') .'/rss')) {
56 $content .= $HTML->error_msg(_('Cannot create backend directory. Contact forge administrator.'));
59 $rss = new SimplePie($this->rss_url, forge_get_config('data_path') .'/rss', null, forge_get_config('sys_proxy'));
61 $items = array_slice($rss->get_items(), 0, $max_items);
62 $content .= '<table class="fullwidth">';
64 foreach($items as $item){
67 $content .= '<tr '.$HTML->boxGetAltRowStyle($i).'><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 /* Do not trust SimplePie for purifying. */
73 $content .= html_e('a', array(
74 'href' => util_unconvert_htmlspecialchars($item->get_link()),
75 ), util_html_secure($item->get_title()));
76 if ($item->get_date()) {
77 $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>';
79 $content .= '</td></tr>';
81 $content .= '</table>';
88 function hasPreferences() {
91 function getPreferences() {
92 $hp = Codendi_HTMLPurifier::instance();
96 $prefs .= _('Title')._(':');
99 $prefs .= '<input type="text" class="textfield_medium" name="rss[title]" value="'. $hp->purify($this->rss_title, CODENDI_PURIFIER_CONVERT_HTML) .'" /></td></tr>';
102 $prefs .= 'URL'._(':');
105 $prefs .= '<input type="url" class="textfield_medium" name="rss[url]" value="'. $hp->purify($this->rss_url, CODENDI_PURIFIER_CONVERT_HTML) .'" /></td></tr>';
106 $prefs .= '</table>';
109 function getInstallPreferences() {
113 $prefs .= 'URL'._(':');
116 $prefs .= '<input type="url" class="textfield_medium" name="rss[url]" value="http://search.twitter.com/search.atom?q=alcatel-lucent&show_user=1" />';
119 $prefs .= '</table>';
122 function cloneContent($id, $owner_id, $owner_type) {
123 $sql = "INSERT INTO widget_rss (owner_id, owner_type, title, url)
124 SELECT $1, $2, title, url
126 WHERE owner_id = $3 AND owner_type = $4";
127 $res = db_query_params($sql,array($owner_id,$owner_type,$this->owner_id,$this->owner_type));
128 return db_insertid($res,'widget_rss','id');
130 function loadContent($id) {
131 $sql = "SELECT * FROM widget_rss WHERE owner_id = $1 AND owner_type = $2 AND id = $3";
132 $res = db_query_params($sql,array($this->owner_id,$this->owner_type,$id));
133 if ($res && db_numrows($res)) {
134 $data = db_fetch_array($res);
135 $this->rss_title = $data['title'];
136 $this->rss_url = $data['url'];
137 $this->content_id = $id;
140 function create(&$request) {
142 $vUrl = new Valid_String('url');
143 $vUrl->setErrorMessage(_("Cannot add empty RSS URL"));
145 if($request->validInArray('rss', $vUrl)) {
146 $rss = $request->get('rss');
147 $vTitle = new Valid_String('title');
149 if (!$request->validInArray('rss', $vTitle)) {
150 if (function_exists('idn_to_utf8()')) {
151 require_once 'simplepie/simplepie.inc';
154 require_once 'common/rss/simplepie.inc';
156 if (!is_dir(forge_get_config('data_path') .'/rss')) {
157 mkdir(forge_get_config('data_path') .'/rss');
159 $rss_reader = new SimplePie($rss['url'], forge_get_config('data_path') .'/rss', null, forge_get_config('sys_proxy'));
160 $rss['title'] = $rss_reader->get_title();
162 $sql = 'INSERT INTO widget_rss (owner_id, owner_type, title, url) VALUES ($1,$2,$3,$4)';
163 $res = db_query_params($sql,array($this->owner_id,$this->owner_type,$rss['title'],$rss['url']));
164 $content_id = db_insertid($res, 'widget_rss', 'id');
168 function updatePreferences(&$request) {
170 $vContentId = new Valid_UInt('content_id');
171 $vContentId->required();
172 if (($rss = $request->get('rss')) && $request->valid($vContentId)) {
173 $vUrl = new Valid_String('url');
174 if($request->validInArray('rss', $vUrl)) {
180 $vTitle = new Valid_String('title');
181 if($request->validInArray('rss', $vTitle)) {
182 $title = $rss['title'] ;
187 if ($url || $title) {
188 $sql = "UPDATE widget_rss SET title=$1 , url=$2 WHERE owner_id =$3 AND owner_type = $4 AND id = $5";
189 db_query_params($sql,array($title,$url,$this->owner_id,$this->owner_type,(int)$request->get('content_id')));
195 function destroy($id) {
196 $sql = 'DELETE FROM widget_rss WHERE id = $1 AND owner_id = $2 AND owner_type = $3';
197 db_query_params($sql,array($id,$this->owner_id,$this->owner_type));
199 function isUnique() {
202 function _date_ago($from_time, $to_time) {
203 $distance_in_minutes = round((abs($to_time - $from_time))/60);
205 if ($distance_in_minutes <= 1) {
206 return ($distance_in_minutes == 0) ? _('less than 1 minute') : _('1 minute');
207 } elseif ($distance_in_minutes <= 44) {
208 return sprintf(_('%d minutes ago'), $distance_in_minutes);
209 } elseif ($distance_in_minutes <= 89) {
210 return _('About one hour') ;
211 } elseif ($distance_in_minutes <= 1439) {
212 return sprintf(_('about %s hours'), round($distance_in_minutes/60));
213 } elseif ($distance_in_minutes <= 2879) {
214 return _('About one day') ;
215 } elseif ($distance_in_minutes <= 43199) {
216 return sprintf(_('%s days ago'), round($distance_in_minutes/1440));
217 } elseif ($distance_in_minutes <= 86399) {
218 return _('About one month') ;
219 } elseif ($distance_in_minutes <= 525959) {
220 return sprintf(_('%s months ago'), round($distance_in_minutes/43200));
221 } elseif ($distance_in_minutes <= 1051919) {
222 return _('About one year') ;
224 return sprintf(_('over %s years'), round($distance_in_minutes/525960));