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