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 if (!mkdir(forge_get_config('data_path') .'/rss')) {
55 $content .= '<p class="error_msg" >'._('Cannot create backend directory. Contact forge administrator.').'</p>';
58 $rss = new SimplePie($this->rss_url, forge_get_config('data_path') .'/rss', null, forge_get_config('sys_proxy'));
60 $items = array_slice($rss->get_items(), 0, $max_items);
61 $content .= '<table class="fullwidth">';
63 foreach($items as $item){
66 $content .= '<tr '.$GLOBALS['HTML']->boxGetAltRowStyle($i).'><td WIDTH="99%">';
67 if ($image = $item->get_link(0, 'image')) {
68 //hack to display twitter avatar
69 $content .= '<img src="'. $hp->purify($image, CODENDI_PURIFIER_CONVERT_HTML) .'" style="float:left; margin-right:1em;" />';
71 /* Do not trust SimplePie for purifying. */
72 $content .= html_e('a', array(
73 'href' => util_unconvert_htmlspecialchars($item->get_link()),
74 ), util_html_secure($item->get_title()));
75 if ($item->get_date()) {
76 $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>';
78 $content .= '</td></tr>';
80 $content .= '</table>';
87 function hasPreferences() {
90 function getPreferences() {
91 $hp = Codendi_HTMLPurifier::instance();
93 $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>';
94 $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>';
98 function getInstallPreferences() {
101 $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>';
102 $prefs .= '</table>';
105 function cloneContent($id, $owner_id, $owner_type) {
106 $sql = "INSERT INTO widget_rss (owner_id, owner_type, title, url)
107 SELECT $1, $2, title, url
109 WHERE owner_id = $3 AND owner_type = $4";
110 $res = db_query_params($sql,array($owner_id,$owner_type,$this->owner_id,$this->owner_type));
111 return db_insertid($res,'widget_rss','id');
113 function loadContent($id) {
114 $sql = "SELECT * FROM widget_rss WHERE owner_id = $1 AND owner_type = $2 AND id = $3";
115 $res = db_query_params($sql,array($this->owner_id,$this->owner_type,$id));
116 if ($res && db_numrows($res)) {
117 $data = db_fetch_array($res);
118 $this->rss_title = $data['title'];
119 $this->rss_url = $data['url'];
120 $this->content_id = $id;
123 function create(&$request) {
125 $vUrl = new Valid_String('url');
126 $vUrl->setErrorMessage("Can't add empty rss url");
128 if($request->validInArray('rss', $vUrl)) {
129 $rss = $request->get('rss');
130 $vTitle = new Valid_String('title');
132 if (!$request->validInArray('rss', $vTitle)) {
133 if (function_exists('idn_to_utf8()')) {
134 require_once 'simplepie/simplepie.inc';
137 require_once 'common/rss/simplepie.inc';
139 if (!is_dir(forge_get_config('data_path') .'/rss')) {
140 mkdir(forge_get_config('data_path') .'/rss');
142 $rss_reader = new SimplePie($rss['url'], forge_get_config('data_path') .'/rss', null, forge_get_config('sys_proxy'));
143 $rss['title'] = $rss_reader->get_title();
145 $sql = 'INSERT INTO widget_rss (owner_id, owner_type, title, url) VALUES ($1,$2,$3,$4)';
146 $res = db_query_params($sql,array($this->owner_id,$this->owner_type,$rss['title'],$rss['url']));
147 $content_id = db_insertid($res, 'widget_rss', 'id');
151 function updatePreferences(&$request) {
153 $vContentId = new Valid_UInt('content_id');
154 $vContentId->required();
155 if (($rss = $request->get('rss')) && $request->valid($vContentId)) {
156 $vUrl = new Valid_String('url');
157 if($request->validInArray('rss', $vUrl)) {
163 $vTitle = new Valid_String('title');
164 if($request->validInArray('rss', $vTitle)) {
165 $title = $rss['title'] ;
170 if ($url || $title) {
171 $sql = "UPDATE widget_rss SET title=$1 , url=$2 WHERE owner_id =$3 AND owner_type = $4 AND id = $5";
172 $res = db_query_params($sql,array($title,$url,$this->owner_id,$this->owner_type,(int)$request->get('content_id')));
178 function destroy($id) {
179 $sql = 'DELETE FROM widget_rss WHERE id = $1 AND owner_id = $2 AND owner_type = $3';
180 db_query_params($sql,array($id,$this->owner_id,$this->owner_type));
182 function isUnique() {
185 function _date_ago($from_time, $to_time, $include_seconds = false) {
186 $distance_in_minutes = round((abs($to_time - $from_time))/60);
187 $distance_in_seconds = round(abs($to_time - $from_time));
189 if ($distance_in_minutes <= 1) {
190 return ($distance_in_minutes == 0) ? _('less than 1 minute') : _('1 minute');
191 } elseif ($distance_in_minutes <= 44) {
192 return vsprintf(_('%s minutes ago'), $distance_in_minutes);
193 } elseif ($distance_in_minutes <= 89) {
194 return _('About one hour') ;
195 } elseif ($distance_in_minutes <= 1439) {
196 return vsprintf(_('about %s hours'), round($distance_in_minutes/60));
197 } elseif ($distance_in_minutes <= 2879) {
198 return _('About one day') ;
199 } elseif ($distance_in_minutes <= 43199) {
200 return vsprintf(_('%s days ago'), round($distance_in_minutes/1440));
201 } elseif ($distance_in_minutes <= 86399) {
202 return _('About one month') ;
203 } elseif ($distance_in_minutes <= 525959) {
204 return vsprintf(_('%s months ago'), round($distance_in_minutes/43200));
205 } elseif ($distance_in_minutes <= 1051919) {
206 return _('About one year') ;
208 return vsprintf(_('over %s years'), round($distance_in_minutes/525960));