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