]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Home/Dashlets/RSSDashlet/RSSDashlet.php
Release 6.5.0
[Github/sugarcrm.git] / modules / Home / Dashlets / RSSDashlet / RSSDashlet.php
1 <?php
2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4  * SugarCRM Community Edition is a customer relationship management program developed by
5  * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
6  * 
7  * This program is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU Affero General Public License version 3 as published by the
9  * Free Software Foundation with the addition of the following permission added
10  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13  * 
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
17  * details.
18  * 
19  * You should have received a copy of the GNU Affero General Public License along with
20  * this program; if not, see http://www.gnu.org/licenses or write to the Free
21  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22  * 02110-1301 USA.
23  * 
24  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26  * 
27  * The interactive user interfaces in modified source and object code versions
28  * of this program must display Appropriate Legal Notices, as required under
29  * Section 5 of the GNU Affero General Public License version 3.
30  * 
31  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32  * these Appropriate Legal Notices must retain the display of the "Powered by
33  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34  * technical reasons, the Appropriate Legal Notices must display the words
35  * "Powered by SugarCRM".
36  ********************************************************************************/
37
38
39 require_once('include/Dashlets/Dashlet.php');
40 require_once('include/Sugar_Smarty.php');
41
42 class RSSDashlet extends Dashlet
43 {
44     protected $url = 'http://www.sugarcrm.com/crm/aggregator/rss/1';
45     protected $height = '200'; // height of the pad
46     protected $images_dir = 'modules/Home/Dashlets/RSSDashlet/images';
47
48     /**
49      * Constructor
50      *
51      * @global string current language
52      * @param guid $id id for the current dashlet (assigned from Home module)
53      * @param array $def options saved for this dashlet
54      */
55     public function __construct($id, $def)
56     {
57         $this->loadLanguage('RSSDashlet', 'modules/Home/Dashlets/'); // load the language strings here
58
59         if(!empty($def['height'])) // set a default height if none is set
60             $this->height = $def['height'];
61
62         if(!empty($def['url']))
63             $this->url = $def['url'];
64
65         if(!empty($def['title']))
66             $this->title = $def['title'];
67         else
68             $this->title = $this->dashletStrings['LBL_TITLE'];
69
70         if(isset($def['autoRefresh'])) $this->autoRefresh = $def['autoRefresh'];
71
72         parent::Dashlet($id); // call parent constructor
73
74         $this->isConfigurable = true; // dashlet is configurable
75         $this->hasScript = false;  // dashlet has javascript attached to it
76     }
77
78     /**
79      * Displays the dashlet
80      *
81      * @return string html to display dashlet
82      */
83     public function display()
84     {
85         $ss = new Sugar_Smarty();
86         $ss->assign('saving', $this->dashletStrings['LBL_SAVING']);
87         $ss->assign('saved', $this->dashletStrings['LBL_SAVED']);
88         $ss->assign('id', $this->id);
89         $ss->assign('height', $this->height);
90         $ss->assign('rss_output', $this->getRSSOutput($this->url));
91         $str = $ss->fetch('modules/Home/Dashlets/RSSDashlet/RSSDashlet.tpl');
92         return parent::display($this->dashletStrings['LBL_DBLCLICK_HELP']) . $str; // return parent::display for title and such
93     }
94
95     /**
96      * Displays the configuration form for the dashlet
97      *
98      * @return string html to display form
99      */
100     public function displayOptions() {
101         global $app_strings, $sugar_version, $sugar_config;
102
103         $ss = new Sugar_Smarty();
104         $ss->assign('titleLbl', $this->dashletStrings['LBL_CONFIGURE_TITLE']);
105         $ss->assign('heightLbl', $this->dashletStrings['LBL_CONFIGURE_HEIGHT']);
106         $ss->assign('rssUrlLbl', $this->dashletStrings['LBL_CONFIGURE_RSSURL']);
107         $ss->assign('saveLbl', $app_strings['LBL_SAVE_BUTTON_LABEL']);
108         $ss->assign('clearLbl', $app_strings['LBL_CLEAR_BUTTON_LABEL']);
109         $ss->assign('title', $this->title);
110         $ss->assign('height', $this->height);
111         $ss->assign('url', $this->url);
112         $ss->assign('id', $this->id);
113         if($this->isAutoRefreshable()) {
114                 $ss->assign('isRefreshable', true);
115                         $ss->assign('autoRefresh', $GLOBALS['app_strings']['LBL_DASHLET_CONFIGURE_AUTOREFRESH']);
116                         $ss->assign('autoRefreshOptions', $this->getAutoRefreshOptions());
117                         $ss->assign('autoRefreshSelect', $this->autoRefresh);
118                 }
119
120         return parent::displayOptions() . $ss->fetch('modules/Home/Dashlets/RSSDashlet/RSSDashletOptions.tpl');
121     }
122
123     /**
124      * called to filter out $_REQUEST object when the user submits the configure dropdown
125      *
126      * @param array $req $_REQUEST
127      * @return array filtered options to save
128      */
129     public function saveOptions(
130         array $req
131         )
132     {
133         $options = array();
134         $options['title'] = $req['title'];
135         $options['url'] = $req['url'];
136         $options['height'] = $req['height'];
137         $options['autoRefresh'] = empty($req['autoRefresh']) ? '0' : $req['autoRefresh'];
138
139         return $options;
140     }
141
142     protected function getRSSOutput(
143         $url
144         )
145     {
146         // suppress XML errors
147         libxml_use_internal_errors(true);
148         $rssdoc = simplexml_load_file($url);
149         // return back the error message if the loading wasn't successful
150         if (!$rssdoc)
151             return $this->dashletStrings['ERR_LOADING_FEED'];
152
153         $output = "<table class='edit view'>";
154         if ( isset($rssdoc->channel) ) {
155             foreach ( $rssdoc->channel as $channel ) {
156                 if ( isset($channel->item ) ) {
157                     foreach ( $channel->item as $item ) {
158                         $output .= <<<EOHTML
159 <tr>
160 <td>
161     <h3><a href="{$item->link}" target="_child">{$item->title}</a></h3>
162     {$item->description}
163 </td>
164 </tr>
165 EOHTML;
166                     }
167                 }
168             }
169         }
170         else {
171             foreach ( $rssdoc->entry as $entry ) {
172                 $link = trim($entry->link);
173                 if ( empty($link) ) {
174                     $link = $entry->link[0]['href'];
175                 }
176                 $output .= <<<EOHTML
177 <tr>
178 <td>
179     <h3><a href="{$link}" target="_child">{$entry->title}</a></h3>
180     {$entry->summary}
181 </td>
182 </tr>
183 EOHTML;
184             }
185         }
186         $output .= "</table>";
187
188         return $output;
189     }
190 }