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-2013 SugarCRM Inc.
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.
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
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
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.
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.
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 ********************************************************************************/
43 $used_aliases = array();
46 class SugarWidgetReportField extends SugarWidgetField
49 * Layout manager reporter attribute
54 function SugarWidgetReportField(&$layout_manager) {
55 parent::SugarWidgetField($layout_manager);
56 $this->reporter = $this->layout_manager->getAttribute("reporter");
59 function getSubClass($layout_def)
61 if (! empty($layout_def['type']))
64 if ($layout_def['type'] == 'time') {
65 $layout_def['widget_class'] = 'Fielddate';
67 $layout_def['widget_class'] = 'Field'.$layout_def['type'];
69 return $this->layout_manager->getClassFromWidgetDef($layout_def);
76 function display($layout_def)
78 $obj = $this->getSubClass($layout_def);
80 $context = $this->layout_manager->getAttribute('context');//_ppd($context);
81 $func_name = 'display'.$context;
84 if ( ! empty($context) && method_exists($obj,$func_name))
86 return $obj->$func_name($layout_def);
89 return 'display not found:'.$func_name;
93 function _get_column_select_special($layout_def)
96 if ( ! empty($layout_def['table_alias']))
98 $alias = $layout_def['table_alias'];
101 if ($layout_def['name'] == 'weighted_sum' )
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)));
106 if ($layout_def['name'] == 'weighted_amount' )
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)));
113 function _get_column_select($layout_def)
116 if (!isset($reportAlias)) {
117 $reportAlias = array();
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'];
128 if ( ! empty($layout_def['group_function']) )
130 if ($layout_def['name'] == 'weighted_sum' || $layout_def['name'] == 'weighted_amount')
132 $alias = $this->_get_column_select_special($layout_def);
133 $reportAlias[$alias] = $layout_def;
137 // Use IFNULL only if it's not AVG aggregate
138 // because it adds NULL rows to the count when it should not, thus getting wrong result
139 if ($layout_def['group_function'] != 'avg') {
140 $alias = $this->reporter->db->convert($alias, 'IFNULL', array(0));
143 // for a field with type='currency' conversion of values into a user-preferred currency
144 if ($layout_def['type'] == 'currency' && strpos($layout_def['name'], '_usdoll') === false) {
145 $currency = $this->reporter->currency_obj;
146 $currency_alias = isset($layout_def['currency_alias'])
147 ? $layout_def['currency_alias'] : $currency->table_name;
148 $query = $this->reporter->db->convert($currency_alias.".conversion_rate", "IFNULL", array(1));
149 // We need to use convert() for AVG because of Oracle
150 if ($layout_def['group_function'] != 'avg') {
151 $alias = "{$layout_def['group_function']}($alias/{$query})*{$currency->conversion_rate}";
153 $alias = $this->reporter->db->convert("$alias/$query", "AVG") . " * {$currency->conversion_rate}";
156 // We need to use convert() for AVG because of Oracle
157 if ($layout_def['group_function'] != 'avg') {
158 $alias = "{$layout_def['group_function']}($alias)";
160 $alias = $this->reporter->db->convert($alias, "AVG");
166 $reportAlias[$alias] = $layout_def;
170 function querySelect(&$layout_def)
172 return $this->_get_column_select($layout_def)." ".$this->_get_column_alias($layout_def)."\n";
175 function queryGroupBy($layout_def)
177 return $this->_get_column_select($layout_def)." \n";
181 function queryOrderBy($layout_def)
183 $field_def = array();
184 if(!empty($this->reporter->all_fields[$layout_def['column_key']])) $field_def = $this->reporter->all_fields[$layout_def['column_key']];
186 if (!empty($layout_def['group_function']))
188 $order_by = $this->_get_column_alias($layout_def);
190 elseif (!empty($field_def['sort_on']))
192 $order_by = $layout_def['table_alias'].".".$field_def['sort_on'];
193 if(!empty($field_def['sort_on2']))
194 $order_by .= ', ' . $layout_def['table_alias'].".".$field_def['sort_on2'];
197 $order_by = $this->_get_column_alias($layout_def)." \n";
200 //use sugar db function convert on order by string to convert to varchar. This is mainly for db's
201 //that do not allow sorting on clob/text fields
202 if ($this->reporter->db->isTextType($this->reporter->db->getFieldType($field_def))) {
203 $order_by = $this->reporter->db->convert($order_by,'text2char', array(10000)); // array(10000) is for db2 only
206 if ( empty($layout_def['sort_dir']) || $layout_def['sort_dir'] == 'a')
208 return $order_by." ASC";
210 return $order_by." DESC";
215 function queryFilter($layout_def)
217 $method_name = "queryFilter".$layout_def['qualifier_name'];
218 return $this->$method_name($layout_def);
221 function displayHeaderCell($layout_def)
223 global $start_link_wrapper,$end_link_wrapper;
226 // don't show sort links if name isn't defined
227 $no_sort = $this->layout_manager->getAttribute('no_sort');
228 if(empty($layout_def['name']) || ! empty($no_sort) || ! empty($layout_def['no_sort']))
230 return $layout_def['label'];
236 if ( ! empty($layout_def['table_key']) && ! empty($layout_def['name']) ) {
237 if (! empty($layout_def['group_function']) && $layout_def['group_function'] == 'count') {
238 $sort_by = $layout_def['table_key'].":".'count';
240 $sort_by = $layout_def['table_key'].":".$layout_def['name'];
241 if ( ! empty($layout_def['column_function'])) {
242 $sort_by .= ':'.$layout_def['column_function'];
243 } else if ( ! empty($layout_def['group_function']) ) {
244 $sort_by .= ':'.$layout_def['group_function'];
248 return $this->displayHeaderCellPlain($layout_def);
251 $start = empty($start_link_wrapper) ? '': $start_link_wrapper;
252 $end = empty($end_link_wrapper) ? '': $end_link_wrapper;
254 // unable to retrieve the vardef here, exclude columns of type clob/text from being sortable
256 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'))) {
257 $header_cell = "<a class=\"listViewThLinkS1\" href=\"".$start.$sort_by.$end."\">";
258 $header_cell .= $this->displayHeaderCellPlain($layout_def);
259 $header_cell .= ListView::getArrowUpDownStart(isset($layout_def['sort']) ? $layout_def['sort'] : '');
260 $header_cell .= ListView::getArrowUpDownEnd(isset($layout_def['sort']) ? $layout_def['sort'] : '');
261 $header_cell .= "</a>";
265 return $this->displayHeaderCellPlain($layout_def);
268 function query($layout_def)
270 $obj = $this->getSubClass($layout_def);
272 $context = $this->layout_manager->getAttribute('context');
273 $func_name = 'query'.$context;
275 if ( ! empty($context) && method_exists($obj,$func_name))
277 return $obj->$func_name($layout_def);
284 function _get_column_alias($layout_def)
286 $alias_arr = array();
288 if (!empty($layout_def['table_key']) && $layout_def['table_key'] == 'self' && !empty($layout_def['name']) && $layout_def['name'] == 'id')
294 // this comment is being added to trigger the upgrade package
295 if ( ! empty($layout_def['group_function']) && $layout_def['group_function']=='count')
297 return $layout_def['table_alias'] . '__count';
300 if ( ! empty($layout_def['table_alias']))
302 array_push($alias_arr,$layout_def['table_alias']);
305 if ( ! empty($layout_def['group_function']) && $layout_def['group_function'] != 'weighted_amount' && $layout_def['group_function'] != 'weighted_sum')
307 array_push($alias_arr,$layout_def['group_function']);
308 } else if ( ! empty($layout_def['column_function']))
310 array_push($alias_arr,$layout_def['column_function']);
311 } else if ( ! empty($layout_def['qualifier']))
313 array_push($alias_arr,$layout_def['qualifier']);
316 if ( ! empty($layout_def['name']))
318 array_push($alias_arr,$layout_def['name']);
321 global $used_aliases, $alias_map;
323 $alias = strtolower(implode("_",$alias_arr));
325 $short_alias = $this->getTruncatedColumnAlias($alias);
327 if ( empty($used_aliases[$short_alias]))
329 $alias_map[$alias] = $short_alias;
330 $used_aliases[$short_alias] = 1;
332 } else if ( ! empty($alias_map[$alias]) )
334 return $alias_map[$alias];
336 $alias_map[$alias] = $short_alias.'_'.$used_aliases[$short_alias];
337 $used_aliases[$short_alias]++;
338 return $alias_map[$alias];
342 function queryFilterEmpty($layout_def)
344 $column = $this->_get_column_select($layout_def);
345 return "($column IS NULL OR $column = ".$this->reporter->db->emptyValue($layout_def['type']).")";
348 function queryFilterIs($layout_def)
350 return '( '.$this->_get_column_select($layout_def)."='".$GLOBALS['db']->quote($layout_def['input_name0'])."')\n";
353 function queryFilteris_not($layout_def)
355 return '( '.$this->_get_column_select($layout_def)."<>'".$GLOBALS['db']->quote($layout_def['input_name0'])."')\n";
358 function queryFilterNot_Empty($layout_def)
360 /** @var $db DBManager */
361 $db = $this->reporter->db;
362 $column = $this->_get_column_select($layout_def);
363 return "(coalesce(" . $db->convert($column, "length") . ",0) > 0)\n";