]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/SugarFeed/Dashlets/SugarFeedDashlet/SugarFeedDashlet.php
Release 6.1.4
[Github/sugarcrm.git] / modules / SugarFeed / Dashlets / SugarFeedDashlet / SugarFeedDashlet.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
40  * Description:  Defines the English language pack for the base application.
41  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
42  * All Rights Reserved.
43  * Contributor(s): ______________________________________..
44  ********************************************************************************/
45
46 require_once('include/Dashlets/DashletGeneric.php');
47
48
49 class SugarFeedDashlet extends DashletGeneric { 
50 var $displayRows = 15;
51
52 var $categories;
53
54 var $selectedCategories = array();
55
56     function SugarFeedDashlet($id, $def = null) {
57                 global $current_user, $app_strings, $app_list_strings;
58                 require('modules/SugarFeed/metadata/dashletviewdefs.php');
59                 $this->myItemsOnly = false;
60         parent::DashletGeneric($id, $def);
61                 $this->myItemsOnly = false;
62                 $this->isConfigurable = true;
63                 $this->hasScript = true;
64         // Add in some default categories.
65         $this->categories['ALL'] = translate('LBL_ALL','SugarFeed');
66         // Need to get the rest of the active SugarFeed modules
67         $module_list = SugarFeed::getActiveFeedModules();
68         // Translate the category names
69         if ( ! is_array($module_list) ) { $module_list = array(); }
70         foreach ( $module_list as $module ) {
71             if ( $module == 'UserFeed' ) {
72                 // Fake module, need to translate specially
73                 $this->categories[$module] = translate('LBL_USER_FEED','SugarFeed');
74             } else {
75                 $this->categories[$module] = $app_list_strings['moduleList'][$module];
76             }
77         }
78
79         if(empty($def['title'])) $this->title = translate('LBL_HOMEPAGE_TITLE', 'SugarFeed');
80                 if(!empty($def['rows']))$this->displayRows = $def['rows'];
81                 if(!empty($def['categories']))$this->selectedCategories = $def['categories'];
82         $this->searchFields = $dashletData['SugarFeedDashlet']['searchFields'];
83         $this->columns = $dashletData['SugarFeedDashlet']['columns'];
84                 $catCount = count($this->categories);
85                 ACLController::filterModuleList($this->categories, false);
86                 if(count($this->categories) < $catCount){
87                         if(!empty($this->selectedCategories)){
88                                 ACLController::filterModuleList($this->selectedCategories, true);
89                         }else{
90                                 $this->selectedCategories = array_keys($this->categories);
91                                 unset($this->selectedCategories[0]);
92                         }
93                 }
94         $this->seedBean = new SugarFeed();
95     }
96         
97         function process($lvsParams = array()) {
98         global $current_user;
99
100         $currentSearchFields = array();
101         $configureView = true; // configure view or regular view
102         $query = false;
103         $whereArray = array();
104         $lvsParams['massupdate'] = false;
105
106         // apply filters
107         if(isset($this->filters) || $this->myItemsOnly) {
108             $whereArray = $this->buildWhere();
109         }
110
111         $this->lvs->export = false;
112         $this->lvs->multiSelect = false;
113                 $this->lvs->quickViewLinks = false;
114         // columns
115     foreach($this->columns as $name => $val) {
116                 if(!empty($val['default']) && $val['default']) {
117                     $displayColumns[strtoupper($name)] = $val;
118                     $displayColumns[strtoupper($name)]['label'] = trim($displayColumns[strtoupper($name)]['label'], ':');
119                 }
120             }
121
122         $this->lvs->displayColumns = $displayColumns;
123
124         $this->lvs->lvd->setVariableName($this->seedBean->object_name, array());
125    
126         $lvsParams['overrideOrder'] = true;
127         $lvsParams['orderBy'] = 'date_entered';
128         $lvsParams['sortOrder'] = 'DESC';
129                 
130         // Get the real module list
131         if (empty($this->selectedCategories)){
132             $mod_list = $this->categories;
133         } else {
134             $mod_list = array_flip($this->selectedCategories);//27949, here the key of $this->selectedCategories is not module name, the value is module name, so array_flip it.
135         }
136
137         $admin_modules = array();
138         $owner_modules = array();
139         $regular_modules = array();
140         foreach($mod_list as $module => $ignore) {
141                         // Handle the UserFeed differently
142                         if ( $module == 'UserFeed') {
143                                 $regular_modules[] = 'UserFeed';
144                                 continue;
145                         }
146                         if (ACLAction::getUserAccessLevel($current_user->id,$module,'view') <= ACL_ALLOW_NONE ) {
147                                 // Not enough access to view any records, don't add it to any lists
148                                 continue;
149                         }
150                         if ( ACLAction::getUserAccessLevel($current_user->id,$module,'view') == ACL_ALLOW_OWNER ) {
151                                 $owner_modules[] = $module;
152             } else {
153                 $regular_modules[] = $module;
154             }
155         }
156
157         if(!empty($this->displayTpl))
158         {
159                 //MFH BUG #14296
160             $where = '';
161             if(!empty($whereArray)){
162                 $where = '(' . implode(') AND (', $whereArray) . ')';
163             }            
164
165                         $module_limiter = " sugarfeed.related_module in ('" . implode("','", $regular_modules) . "')";
166
167             if ( count($owner_modules) > 0
168                                 ) {
169                 $module_limiter = " ((sugarfeed.related_module IN ('".implode("','", $regular_modules)."') "
170                                         .") ";
171                                 if ( count($owner_modules) > 0 ) {
172                                         $module_limiter .= "OR (sugarfeed.related_module IN('".implode("','", $owner_modules)."') AND sugarfeed.assigned_user_id = '".$current_user->id."' "
173                                                 .") ";
174                                 }
175                                 $module_limiter .= ")";
176             }
177                         if(!empty($where)) { $where .= ' AND '; }
178                         $where .= $module_limiter;
179
180             $this->lvs->setup($this->seedBean, $this->displayTpl, $where , $lvsParams, 0, $this->displayRows, 
181                               array('name', 
182                                     'description', 
183                                     'date_entered', 
184                                     'created_by', 
185                                     'link_url', 
186                                     'link_type'));
187
188             foreach($this->lvs->data['data'] as $row => $data) {
189                 $this->lvs->data['data'][$row]['CREATED_BY'] = get_assigned_user_name($data['CREATED_BY']);
190                 $this->lvs->data['data'][$row]['NAME'] = str_replace("{this.CREATED_BY}",$this->lvs->data['data'][$row]['CREATED_BY'],$data['NAME']);
191             }
192
193             // assign a baseURL w/ the action set as DisplayDashlet
194             foreach($this->lvs->data['pageData']['urls'] as $type => $url) {
195                 // awu Replacing action=DisplayDashlet with action=DynamicAction&DynamicAction=DisplayDashlet
196                 if($type == 'orderBy')
197                     $this->lvs->data['pageData']['urls'][$type] = preg_replace('/(action=.*&)/Ui', 'action=DynamicAction&DynamicAction=displayDashlet&', $url);
198                 else
199                     $this->lvs->data['pageData']['urls'][$type] = preg_replace('/(action=.*&)/Ui', 'action=DynamicAction&DynamicAction=displayDashlet&', $url) . '&sugar_body_only=1&id=' . $this->id;
200             }
201
202             $this->lvs->ss->assign('dashletId', $this->id);
203
204         }
205     }
206         
207           function deleteUserFeed() {
208         if(!empty($_REQUEST['record'])) {
209                         $feed = new SugarFeed();
210                         $feed->retrieve($_REQUEST['record']);
211                         if(is_admin($GLOBALS['current_user']) || $feed->created_by == $GLOBALS['current_user']->id){ 
212                 $feed->mark_deleted($_REQUEST['record']);
213
214                         }
215         }
216     }
217          function pushUserFeed() {
218         if(!empty($_REQUEST['text'])) {
219                         $text = htmlspecialchars($_REQUEST['text']);
220                         //allow for bold and italic user tags
221                         $text = preg_replace('/&amp;lt;(\/*[bi])&amp;gt;/i','<$1>', $text);
222             SugarFeed::pushFeed($text, 'UserFeed', $GLOBALS['current_user']->id, 
223                                                                 $GLOBALS['current_user']->id,
224                                 $_REQUEST['link_type'], $_REQUEST['link_url']
225                                 );
226         }
227        
228     }
229           function displayOptions() {
230         global $app_strings;
231         $ss = new Sugar_Smarty();
232         $ss->assign('titleLBL', translate('LBL_TITLE', 'SugarFeed'));
233                 $ss->assign('categoriesLBL', translate('LBL_CATEGORIES', 'SugarFeed'));
234         $ss->assign('rowsLBL', translate('LBL_ROWS', 'SugarFeed'));
235         $ss->assign('saveLBL', $app_strings['LBL_SAVE_BUTTON_LABEL']);
236         $ss->assign('title', $this->title);
237                 $ss->assign('categories', $this->categories);
238         if ( empty($this->selectedCategories) ) {
239             $this->selectedCategories['ALL'] = 'ALL';
240         }
241                 $ss->assign('selectedCategories', $this->selectedCategories);
242         $ss->assign('rows', $this->displayRows);
243         $ss->assign('id', $this->id);
244
245         return  $ss->fetch('modules/SugarFeed/Dashlets/SugarFeedDashlet/Options.tpl');
246     }  
247         
248         /**
249          * creats the values
250          * @return 
251          * @param $req Object
252          */
253           function saveOptions($req) {
254         global $sugar_config, $timedate, $current_user, $theme;
255         $options = array();
256         $options['title'] = $_REQUEST['title'];
257                 $rows = intval($_REQUEST['rows']);
258         if($rows <= 0) {
259             $rows = 15;         
260         }
261                 if($rows > 100){
262                         $rows = 100;
263                 }
264         $options['rows'] = $rows;
265                 $options['categories'] = $_REQUEST['categories'];
266                 foreach($options['categories'] as $cat){
267                         if($cat == 'ALL'){
268                                 unset($options['categories']);
269                         }
270                 }
271         return $options;
272     }
273         
274       
275       function sugarFeedDisplayScript() {
276           // Forces the quicksearch to reload anytime the dashlet gets refreshed
277           return '<script type="text/javascript">
278 enableQS(false);
279 </script>';
280       }
281         /**
282          * 
283          * @return javascript including QuickSearch for SugarFeeds
284          */
285          function displayScript() {
286                 require_once('include/QuickSearchDefaults.php');
287
288         $ss = new Sugar_Smarty();
289         $ss->assign('saving', translate('LBL_SAVING', 'SugarFeed'));
290         $ss->assign('saved', translate('LBL_SAVED', 'SugarFeed'));
291         $ss->assign('id', $this->id);
292         
293         $str = $ss->fetch('modules/SugarFeed/Dashlets/SugarFeedDashlet/SugarFeedScript.tpl');
294         return $str; // return parent::display for title and such
295     }
296         
297         /**
298          * 
299          * @return the fully rendered dashlet
300          */
301         function display(){
302                 
303                 $listview = parent::display();
304                 $GLOBALS['current_sugarfeed'] = $this;
305                 $listview = preg_replace_callback('/\{([^\}]+)\.([^\}]+)\}/', create_function(
306             '$matches',
307             'if($matches[1] == "this"){$var = $matches[2]; return $GLOBALS[\'current_sugarfeed\']->$var;}else{return translate($matches[2], $matches[1]);}'
308         ),$listview);
309                 $listview = preg_replace('/\[(\w+)\:([\w\-\d]*)\:([^\]]*)\]/', '<a href="index.php?module=$1&action=DetailView&record=$2"><img src="themes/default/images/$1.gif" border=0>$3</a>', $listview);
310         
311                 return $listview.'</div>';
312         }
313         
314         
315         /**
316          * 
317          * @return the title and the user post form
318          * @param $text Object
319          */
320         function getHeader($text='') {
321                 return parent::getHeader($text) . $this->getPostForm().$this->getDisabledWarning().$this->sugarFeedDisplayScript().'<div class="sugarFeedDashlet">';    
322         }
323         
324         
325         /**
326          * 
327          * @return a warning message if the sugar feed system is not enabled currently
328          */
329         function getDisabledWarning(){
330         /* Check to see if the sugar feed system is enabled */
331         if ( ! $this->shouldDisplay() ) {
332             // The Sugar Feeds are disabled, populate the warning message
333             return translate('LBL_DASHLET_DISABLED','SugarFeed');
334         } else {
335             return '';
336         }
337     }
338
339         /**
340          * 
341          * @return the form for users posting custom messages to the feed stream
342          */
343         function getPostForm(){
344         global $current_user;
345
346         if ( empty($this->categories['UserFeed']) ) {
347             // The user feed system isn't enabled, don't let them post notes
348             return '';
349         }
350                 $user_name = ucfirst($GLOBALS['current_user']->user_name);
351                 $moreimg = SugarThemeRegistry::current()->getImage('advanced_search' , 'onclick="toggleDisplay(\'more_' . $this->id . '\'); toggleDisplay(\'more_img_'.$this->id.'\'); toggleDisplay(\'less_img_'.$this->id.'\');"');
352                 $lessimg = SugarThemeRegistry::current()->getImage('basic_search' , 'onclick="toggleDisplay(\'more_' . $this->id . '\'); toggleDisplay(\'more_img_'.$this->id.'\'); toggleDisplay(\'less_img_'.$this->id.'\');"');
353                 $ss = new Sugar_Smarty();
354                 $ss->assign('LBL_TO', translate('LBL_TO', 'SugarFeed'));
355                 $ss->assign('LBL_POST', translate('LBL_POST', 'SugarFeed'));
356                 $ss->assign('LBL_SELECT', translate('LBL_SELECT', 'SugarFeed'));
357                 $ss->assign('LBL_IS', translate('LBL_IS', 'SugarFeed'));
358                 $ss->assign('id', $this->id);
359                 $ss->assign('more_img', $moreimg);
360                 $ss->assign('less_img', $lessimg);
361         if($current_user->getPreference('use_real_names') == 'on'){
362             $ss->assign('user_name', $current_user->full_name);
363         }
364         else {
365             $ss->assign('user_name', $user_name);
366         }
367         $linkTypesIn = SugarFeed::getLinkTypes();
368         $linkTypes = array();
369         foreach ( $linkTypesIn as $key => $value ) {
370             $linkTypes[$key] = translate('LBL_LINK_TYPE_'.$value,'SugarFeed');
371         }
372                 $ss->assign('link_types', $linkTypes);
373                 return $ss->fetch('modules/SugarFeed/Dashlets/SugarFeedDashlet/UserPostForm.tpl');
374         
375         }
376     
377     // This is called from the include/MySugar/DashletsDialog/DashletsDialog.php and determines if we should display the SugarFeed dashlet as an option or not
378     static function shouldDisplay() {
379         
380         $admin = new Administration();
381         $admin->retrieveSettings();
382
383         if ( !isset($admin->settings['sugarfeed_enabled']) || $admin->settings['sugarfeed_enabled'] != '1' ) {
384             return false;
385         } else {
386             return true;
387         }
388     }
389 }