]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Home/Dashlets/RSSDashlet/RSSDashlet.php
Release 6.1.4
[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 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         parent::Dashlet($id); // call parent constructor
71          
72         $this->isConfigurable = true; // dashlet is configurable
73         $this->hasScript = false;  // dashlet has javascript attached to it
74     }
75
76     /**
77      * Displays the dashlet
78      * 
79      * @return string html to display dashlet
80      */
81     public function display() 
82     {
83         $ss = new Sugar_Smarty();
84         $ss->assign('saving', $this->dashletStrings['LBL_SAVING']);
85         $ss->assign('saved', $this->dashletStrings['LBL_SAVED']);
86         $ss->assign('id', $this->id);
87         $ss->assign('height', $this->height);
88         $ss->assign('rss_output', $this->getRSSOutput($this->url)); 
89         $str = $ss->fetch('modules/Home/Dashlets/RSSDashlet/RSSDashlet.tpl');
90         return parent::display($this->dashletStrings['LBL_DBLCLICK_HELP']) . $str; // return parent::display for title and such
91     }
92     
93     /**
94      * Displays the configuration form for the dashlet
95      * 
96      * @return string html to display form
97      */
98     public function displayOptions() {
99         global $app_strings, $sugar_version, $sugar_config;
100         
101         $ss = new Sugar_Smarty();
102         $ss->assign('titleLbl', $this->dashletStrings['LBL_CONFIGURE_TITLE']);
103         $ss->assign('heightLbl', $this->dashletStrings['LBL_CONFIGURE_HEIGHT']);
104         $ss->assign('rssUrlLbl', $this->dashletStrings['LBL_CONFIGURE_RSSURL']);
105         $ss->assign('saveLbl', $app_strings['LBL_SAVE_BUTTON_LABEL']);
106         $ss->assign('title', $this->title);
107         $ss->assign('height', $this->height);
108         $ss->assign('url', $this->url);
109         $ss->assign('id', $this->id);
110         
111         return parent::displayOptions() . $ss->fetch('modules/Home/Dashlets/RSSDashlet/RSSDashletOptions.tpl');
112     }  
113
114     /**
115      * called to filter out $_REQUEST object when the user submits the configure dropdown
116      * 
117      * @param array $req $_REQUEST
118      * @return array filtered options to save
119      */  
120     public function saveOptions(
121         array $req
122         ) 
123     {
124         $options = array();
125         $options['title'] = $req['title'];
126         $options['url'] = $req['url'];
127         $options['height'] = $req['height'];
128          
129         return $options;
130     }
131     
132     protected function getRSSOutput(
133         $url
134         )
135     {
136         // suppress XML errors
137         libxml_use_internal_errors(true);
138         $rssdoc = simplexml_load_file($url);
139         // return back the error message if the loading wasn't successful
140         if (!$rssdoc)
141             return $this->dashletStrings['ERR_LOADING_FEED'];
142         
143         $output = "<table class='edit view'>";
144         if ( isset($rssdoc->channel) ) {
145             foreach ( $rssdoc->channel as $channel ) {
146                 if ( isset($channel->item ) ) {
147                     foreach ( $channel->item as $item ) {
148                         $output .= <<<EOHTML
149 <tr>
150 <td>
151     <h3><a href="{$item->link}" target="_child">{$item->title}</a></h3>
152     {$item->description}
153 </td>
154 </tr>
155 EOHTML;
156                     }
157                 }
158             }
159         }
160         else {
161             foreach ( $rssdoc->entry as $entry ) {
162                 $output .= <<<EOHTML
163 <tr>
164 <td>
165     <h3><a href="{$entry->link}" target="_child">{$entry->title}</a></h3>
166     {$entry->summary}
167 </td>
168 </tr>
169 EOHTML;
170             }
171         }
172         $output .= "</table>";
173         
174         return $output;
175     }
176 }