]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/generic/SugarWidgets/SugarWidgetReportField.php
Release 6.3.0
[Github/sugarcrm.git] / include / generic / SugarWidgets / SugarWidgetReportField.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
40
41 require_once('include/generic/SugarWidgets/SugarWidgetField.php');
42
43 $used_aliases = array();
44 $alias_map = array();
45
46 class SugarWidgetReportField extends SugarWidgetField
47 {
48         function SugarWidgetReportField(&$layout_manager) {
49         parent::SugarWidgetField($layout_manager);
50     }
51
52         function  getSubClass($layout_def)
53         {
54                 if (! empty($layout_def['type'])) {
55                         if ($layout_def['type'] == 'time') {
56                                 $layout_def['widget_class'] = 'Fielddate';
57                         } 
58                         else {
59                                 $layout_def['widget_class'] = 'Field'.$layout_def['type'];
60                         }
61                         return $this->layout_manager->getClassFromWidgetDef($layout_def);
62                 } 
63                 else {
64                         return $this;
65                 }
66         }
67
68         function display($layout_def)
69         {
70         $obj = $this->getSubClass($layout_def);
71
72         $context = $this->layout_manager->getAttribute('context');//_ppd($context);
73         $func_name = 'display'.$context;
74
75         if ( ! empty($context) && method_exists($obj,$func_name)){
76                 return  $obj->$func_name($layout_def);
77         } 
78         else {
79                 return 'display not found:'.$func_name;
80         }
81     }
82
83     function _get_column_select_special($layout_def)
84     {
85         $alias = '';
86         if ( ! empty($layout_def['table_alias'])) {
87             $alias = $layout_def['table_alias'];
88         }
89
90         $reporter = $this->layout_manager->getAttribute("reporter");
91
92         if ($layout_def['name'] == 'weighted_sum' ) {
93             return "SUM( ".$alias.".probability * ".$alias.".amount_usdollar * 0.01) ";
94                 }
95                 if ($layout_def['name'] == 'weighted_amount' ) {
96                         return "AVG(".$alias.".probability * ".$alias.".amount_usdollar * 0.01) ";
97                 }
98         }
99
100         function _get_column_select($layout_def) 
101         {
102             global $reportAlias;
103             
104             if (!isset($reportAlias)) {
105                 $reportAlias = array();
106             }
107                 $alias = '';
108                 $endalias = '';
109
110                 if ( ! empty($layout_def['group_function']) ) {
111                     if ($layout_def['name'] == 'weighted_sum' || $layout_def['name'] == 'weighted_amount') {
112                         $alias = $this->_get_column_select_special($layout_def);
113                         $reportAlias[$alias] = $layout_def;
114                         return $alias;
115                     }
116
117                     $reporter = $this->layout_manager->getAttribute('reporter');
118                     $alias .= $layout_def['group_function']."(";
119                     $endalias = ')';
120                 }
121                 if ( ! empty($layout_def['table_alias'])) {
122                     $alias .= $layout_def['table_alias'].".".$layout_def['name'];
123                 }
124                 elseif (! empty($layout_def['name'])) {
125                     $alias = $layout_def['name'];
126                 } 
127                 else {
128                     $alias .= "*";
129                 }
130                 $alias .= $endalias;
131
132                 $reportAlias[$alias] = $layout_def;
133         
134                 return $alias;
135     }
136
137     function querySelect(&$layout_def)
138     {
139         return $this->_get_column_select($layout_def)." ".$this->_get_column_alias($layout_def)."\n";
140     }
141
142     function queryGroupBy($layout_def)
143     {
144         return $this->_get_column_select($layout_def)." \n";
145     }
146
147     function queryOrderBy($layout_def)
148     {
149         $reporter = $this->layout_manager->getAttribute('reporter');
150         if(!empty($reporter->all_fields[$layout_def['column_key']])) {
151             $field_def = $reporter->all_fields[$layout_def['column_key']];
152         }
153         
154         if ( ! empty( $field_def['sort_on'])) {
155             $order_by = $layout_def['table_alias'].".".$field_def['sort_on'];
156             if(!empty($field_def['sort_on2'])) {
157                 $order_by .= ', ' . $layout_def['table_alias'].".".$field_def['sort_on2'];
158             }
159         }
160         else {
161             $order_by = $this->_get_column_alias($layout_def)." \n";
162         }
163
164         if ( empty($layout_def['sort_dir']) || $layout_def['sort_dir'] == 'a') {
165             return $order_by." ASC";
166         } 
167         
168         return $order_by." DESC";
169     }
170
171     function queryFilter($layout_def) {
172         $method_name = "queryFilter".$layout_def['qualifier_name'];
173         
174         return $this->$method_name($layout_def);
175     }
176
177         function displayHeaderCell($layout_def)
178         {
179             global $start_link_wrapper,$end_link_wrapper;
180
181             // don't show sort links if name isn't defined
182             $no_sort = $this->layout_manager->getAttribute('no_sort');
183             $fieldList = array(
184                 'description', 
185                 'account_description', 
186                 'lead_source_description', 
187                 'status_description', 
188                 'to_addrs', 
189                 'cc_addrs', 
190                 'bcc_addrs', 
191                 'work_log', 
192                 'objective', 
193                 'resolution',
194                 );
195         if ( ! empty($layout_def['table_key']) && ! empty($layout_def['name']) && empty($no_sort) 
196                 && empty($layout_def['no_sort']) && (!in_array($layout_def['name'], $fieldList))) {
197             $sort_by = 'count';
198             if (empty($layout_def['group_function']) || !($layout_def['group_function'] == 'count')) {
199                 $sort_by = $layout_def['table_key'].":".$layout_def['name'];
200                 if ( ! empty($layout_def['column_function'])) {
201                     $sort_by .= ':'.$layout_def['column_function'];
202                 } 
203                 elseif ( ! empty($layout_def['group_function']) ) {
204                     $sort_by .= ':'.$layout_def['group_function'];
205                 }
206             }
207
208             $start = empty($start_link_wrapper) ? '': $start_link_wrapper;
209             $end = empty($end_link_wrapper) ? '': $end_link_wrapper;
210
211             // unable to retrieve the vardef here, exclude columns of type clob/text from being sortable
212             $header_cell = "<a class=\"listViewThLinkS1\" href=\"".$start.$sort_by.$end."\">";
213             $header_cell .= $this->displayHeaderCellPlain($layout_def);
214             $header_cell .= ListView::getArrowUpDownStart(isset($layout_def['sort']) ? $layout_def['sort'] : '');
215             $header_cell .= ListView::getArrowUpDownEnd(isset($layout_def['sort']) ? $layout_def['sort'] : '');
216             $header_cell .= "</a>";
217                 
218             return $header_cell;
219         }
220                 
221         return $this->displayHeaderCellPlain($layout_def);
222     }
223
224         function query($layout_def)
225         {
226         $obj = $this->getSubClass($layout_def);
227
228         $context = $this->layout_manager->getAttribute('context');
229         $func_name = 'query'.$context;
230
231         if ( ! empty($context) && method_exists($obj,$func_name)) {
232             return $obj->$func_name($layout_def);
233         }
234         
235         return '';
236         }
237
238         function _get_column_alias($layout_def)
239         {
240         $alias_arr = array();
241
242         if ($layout_def['table_key'] == 'self' && !empty($layout_def['name']) && $layout_def['name'] == 'id') {
243             return 'primaryid';
244         }
245
246         if ( ! empty($layout_def['group_function']) && $layout_def['group_function']=='count') {
247             return 'count';
248         }
249
250         if ( ! empty($layout_def['table_alias'])) {
251             array_push($alias_arr,$layout_def['table_alias']);
252         }
253
254         if ( ! empty($layout_def['group_function']) && $layout_def['group_function'] != 'weighted_amount' && $layout_def['group_function'] != 'weighted_sum') {
255             array_push($alias_arr,$layout_def['group_function']);
256         } 
257         elseif ( ! empty($layout_def['column_function'])) {
258             array_push($alias_arr,$layout_def['column_function']);
259         } 
260         elseif ( ! empty($layout_def['qualifier'])) {
261             array_push($alias_arr,$layout_def['qualifier']);
262         }
263
264         if ( ! empty($layout_def['name'])) {
265             array_push($alias_arr,$layout_def['name']);
266         }
267
268         global $used_aliases, $alias_map;
269
270         $alias = strtolower(implode("_",$alias_arr));
271         
272         $short_alias = $this->getTruncatedColumnAlias($alias);
273
274         if ( empty($used_aliases[$short_alias])) {
275             $alias_map[$alias] = $short_alias;
276             $used_aliases[$short_alias] = 1;
277             return $short_alias;
278         } 
279         elseif ( ! empty($alias_map[$alias]) ) {
280                         return $alias_map[$alias];
281         } 
282
283         $alias_map[$alias] = $short_alias.'_'.$used_aliases[$short_alias];
284         $used_aliases[$short_alias]++;
285         return $alias_map[$alias];
286     }
287
288     function queryFilterEmpty(&$layout_def)
289     {
290         $reporter = $this->layout_manager->getAttribute("reporter");
291         if( $reporter->db->dbType == 'mssql' && $layout_def['type'] == 'currency') {
292             return '( '.$this->_get_column_select($layout_def).' IS NULL OR '.$this->_get_column_select($layout_def)."=0 )\n";
293         }
294         
295         return '( '.$this->_get_column_select($layout_def).' IS NULL OR '.$this->_get_column_select($layout_def)."='' )\n";
296     }
297
298     function queryFilterIs(&$layout_def)
299     {
300         return '( '.$this->_get_column_select($layout_def)."='".$GLOBALS['db']->quote($layout_def['input_name0'])."')\n";
301     }
302
303     function queryFilteris_not(&$layout_def)
304     {
305         return '( '.$this->_get_column_select($layout_def)."<>'".$GLOBALS['db']->quote($layout_def['input_name0'])."')\n";
306     }
307  
308     function queryFilterNot_Empty(&$layout_def)
309     {
310         $reporter = $this->layout_manager->getAttribute("reporter");
311         if( $reporter->db->dbType == 'mssql' && $layout_def['type'] == 'currency') {
312             return '( '.$this->_get_column_select($layout_def).' IS NOT NULL AND '.$this->_get_column_select($layout_def)."<>0 )\n";
313         }
314
315         return '( '.$this->_get_column_select($layout_def).' IS NOT NULL AND '.$this->_get_column_select($layout_def)."<>'' )\n";
316     }
317 }