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 $alias = $this->reporter->db->convert($alias, 'IFNULL', array(0));
139 // for a field with type='currency' conversion of values into a user-preferred currency
140 if ($layout_def['type'] == 'currency' && strpos($layout_def['name'], '_usdoll') === false) {
141 $currency = $this->reporter->currency_obj;
142 $currency_alias = isset($layout_def['currency_alias'])
143 ? $layout_def['currency_alias'] : $currency->table_name;
144 $query = $this->reporter->db->convert($currency_alias.".conversion_rate", "IFNULL", array(1));
145 $alias = "{$layout_def['group_function']}($alias/{$query})*{$currency->conversion_rate}";
147 $alias = "{$layout_def['group_function']}($alias)";
151 $reportAlias[$alias] = $layout_def;
155 function querySelect(&$layout_def)
157 return $this->_get_column_select($layout_def)." ".$this->_get_column_alias($layout_def)."\n";
160 function queryGroupBy($layout_def)
162 return $this->_get_column_select($layout_def)." \n";
166 function queryOrderBy($layout_def)
168 if(!empty($this->reporter->all_fields[$layout_def['column_key']])) $field_def = $this->reporter->all_fields[$layout_def['column_key']];
170 if (!empty($layout_def['group_function']))
172 $order_by = $this->_get_column_alias($layout_def);
174 elseif (!empty($field_def['sort_on']))
176 $order_by = $layout_def['table_alias'].".".$field_def['sort_on'];
177 if(!empty($field_def['sort_on2']))
178 $order_by .= ', ' . $layout_def['table_alias'].".".$field_def['sort_on2'];
181 $order_by = $this->_get_column_alias($layout_def)." \n";
184 if ( empty($layout_def['sort_dir']) || $layout_def['sort_dir'] == 'a')
186 return $order_by." ASC";
188 return $order_by." DESC";
193 function queryFilter($layout_def)
195 $method_name = "queryFilter".$layout_def['qualifier_name'];
196 return $this->$method_name($layout_def);
199 function displayHeaderCell($layout_def)
201 global $start_link_wrapper,$end_link_wrapper;
204 // don't show sort links if name isn't defined
205 $no_sort = $this->layout_manager->getAttribute('no_sort');
206 if(empty($layout_def['name']) || ! empty($no_sort) || ! empty($layout_def['no_sort']))
208 return $layout_def['label'];
214 if ( ! empty($layout_def['table_key']) && ! empty($layout_def['name']) ) {
215 if (! empty($layout_def['group_function']) && $layout_def['group_function'] == 'count') {
216 $sort_by = $layout_def['table_key'].":".'count';
218 $sort_by = $layout_def['table_key'].":".$layout_def['name'];
219 if ( ! empty($layout_def['column_function'])) {
220 $sort_by .= ':'.$layout_def['column_function'];
221 } else if ( ! empty($layout_def['group_function']) ) {
222 $sort_by .= ':'.$layout_def['group_function'];
226 return $this->displayHeaderCellPlain($layout_def);
229 $start = empty($start_link_wrapper) ? '': $start_link_wrapper;
230 $end = empty($end_link_wrapper) ? '': $end_link_wrapper;
232 // unable to retrieve the vardef here, exclude columns of type clob/text from being sortable
234 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'))) {
235 $header_cell = "<a class=\"listViewThLinkS1\" href=\"".$start.$sort_by.$end."\">";
236 $header_cell .= $this->displayHeaderCellPlain($layout_def);
237 $header_cell .= ListView::getArrowUpDownStart(isset($layout_def['sort']) ? $layout_def['sort'] : '');
238 $header_cell .= ListView::getArrowUpDownEnd(isset($layout_def['sort']) ? $layout_def['sort'] : '');
239 $header_cell .= "</a>";
243 return $this->displayHeaderCellPlain($layout_def);
246 function query($layout_def)
248 $obj = $this->getSubClass($layout_def);
250 $context = $this->layout_manager->getAttribute('context');
251 $func_name = 'query'.$context;
253 if ( ! empty($context) && method_exists($obj,$func_name))
255 return $obj->$func_name($layout_def);
262 function _get_column_alias($layout_def)
264 $alias_arr = array();
266 if (!empty($layout_def['table_key']) && $layout_def['table_key'] == 'self' && !empty($layout_def['name']) && $layout_def['name'] == 'id')
272 // this comment is being added to trigger the upgrade package
273 if ( ! empty($layout_def['group_function']) && $layout_def['group_function']=='count')
275 return $layout_def['table_alias'] . '__count';
278 if ( ! empty($layout_def['table_alias']))
280 array_push($alias_arr,$layout_def['table_alias']);
283 if ( ! empty($layout_def['group_function']) && $layout_def['group_function'] != 'weighted_amount' && $layout_def['group_function'] != 'weighted_sum')
285 array_push($alias_arr,$layout_def['group_function']);
286 } else if ( ! empty($layout_def['column_function']))
288 array_push($alias_arr,$layout_def['column_function']);
289 } else if ( ! empty($layout_def['qualifier']))
291 array_push($alias_arr,$layout_def['qualifier']);
294 if ( ! empty($layout_def['name']))
296 array_push($alias_arr,$layout_def['name']);
299 global $used_aliases, $alias_map;
301 $alias = strtolower(implode("_",$alias_arr));
303 $short_alias = $this->getTruncatedColumnAlias($alias);
305 if ( empty($used_aliases[$short_alias]))
307 $alias_map[$alias] = $short_alias;
308 $used_aliases[$short_alias] = 1;
310 } else if ( ! empty($alias_map[$alias]) )
312 return $alias_map[$alias];
314 $alias_map[$alias] = $short_alias.'_'.$used_aliases[$short_alias];
315 $used_aliases[$short_alias]++;
316 return $alias_map[$alias];
320 function queryFilterEmpty($layout_def)
322 $column = $this->_get_column_select($layout_def);
323 return "($column IS NULL OR $column = ".$this->reporter->db->emptyValue($layout_def['type']).")";
326 function queryFilterIs($layout_def)
328 return '( '.$this->_get_column_select($layout_def)."='".$GLOBALS['db']->quote($layout_def['input_name0'])."')\n";
331 function queryFilteris_not($layout_def)
333 return '( '.$this->_get_column_select($layout_def)."<>'".$GLOBALS['db']->quote($layout_def['input_name0'])."')\n";
336 function queryFilterNot_Empty($layout_def)
338 $column = $this->_get_column_select($layout_def);
339 return "($column IS NOT NULL AND $column <> ".$this->reporter->db->emptyValue($layout_def['type']).")";