]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Home/Dashlets/RSSDashlet/RSSDashlet.php
Release 6.2.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-2011 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('title', $this->title);
109         $ss->assign('height', $this->height);
110         $ss->assign('url', $this->url);
111         $ss->assign('id', $this->id);
112         if($this->isAutoRefreshable()) {
113                 $ss->assign('isRefreshable', true);
114                         $ss->assign('autoRefresh', $GLOBALS['app_strings']['LBL_DASHLET_CONFIGURE_AUTOREFRESH']);
115                         $ss->assign('autoRefreshOptions', $this->getAutoRefreshOptions());
116                         $ss->assign('autoRefreshSelect', $this->autoRefresh);
117                 }
118         
119         return parent::displayOptions() . $ss->fetch('modules/Home/Dashlets/RSSDashlet/RSSDashletOptions.tpl');
120     }  
121
122     /**
123      * called to filter out $_REQUEST object when the user submits the configure dropdown
124      * 
125      * @param array $req $_REQUEST
126      * @return array filtered options to save
127      */  
128     public function saveOptions(
129         array $req
130         ) 
131     {
132         $options = array();
133         $options['title'] = $req['title'];
134         $options['url'] = $req['url'];
135         $options['height'] = $req['height'];
136         $options['autoRefresh'] = empty($req['autoRefresh']) ? '0' : $req['autoRefresh'];
137         
138         return $options;
139     }
140     
141     protected function getRSSOutput(
142         $url
143         )
144     {
145         // suppress XML errors
146         libxml_use_internal_errors(true);
147         $rssdoc = simplexml_load_file($url);
148         // return back the error message if the loading wasn't successful
149         if (!$rssdoc)
150             return $this->dashletStrings['ERR_LOADING_FEED'];
151         
152         $output = "<table class='edit view'>";
153         if ( isset($rssdoc->channel) ) {
154             foreach ( $rssdoc->channel as $channel ) {
155                 if ( isset($channel->item ) ) {
156                     foreach ( $channel->item as $item ) {
157                         $output .= <<<EOHTML
158 <tr>
159 <td>
160     <h3><a href="{$item->link}" target="_child">{$item->title}</a></h3>
161     {$item->description}
162 </td>
163 </tr>
164 EOHTML;
165                     }
166                 }
167             }
168         }
169         else {
170             foreach ( $rssdoc->entry as $entry ) {
171                 $output .= <<<EOHTML
172 <tr>
173 <td>
174     <h3><a href="{$entry->link}" target="_child">{$entry->title}</a></h3>
175     {$entry->summary}
176 </td>
177 </tr>
178 EOHTML;
179             }
180         }
181         $output .= "</table>";
182         
183         return $output;
184     }
185 }