All Data Structures Functions Variables Pages
ReportDataProviderToAmChartMakerAdapter.php
1 <?php
2  /*********************************************************************************
3  * Zurmo is a customer relationship management program developed by
4  * Zurmo, Inc. Copyright (C) 2017 Zurmo Inc.
5  *
6  * Zurmo is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License version 3 as published by the
8  * Free Software Foundation with the addition of the following permission added
9  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10  * IN WHICH THE COPYRIGHT IS OWNED BY ZURMO, ZURMO DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  *
13  * Zurmo is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Affero General Public License along with
19  * this program; if not, see http://www.gnu.org/licenses or write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  *
23  * You can contact Zurmo, Inc. with a mailing address at 27 North Wacker Drive
24  * Suite 370 Chicago, IL 60606. or at email address contact@zurmo.com.
25  *
26  * The interactive user interfaces in original and modified versions
27  * of this program must display Appropriate Legal Notices, as required under
28  * Section 5 of the GNU Affero General Public License version 3.
29  *
30  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31  * these Appropriate Legal Notices must retain the display of the Zurmo
32  * logo and Zurmo copyright notice. If the display of the logo is not reasonably
33  * feasible for technical reasons, the Appropriate Legal Notices must display the words
34  * "Copyright Zurmo Inc. 2017. All rights reserved".
35  ********************************************************************************/
36 
41  {
42  const FIRST_SERIES_VALUE = 'FirstSeriesValue';
43 
44  const FIRST_SERIES_DISPLAY_LABEL = 'FirstSeriesDisplayLabel';
45 
46  const FIRST_RANGE_DISPLAY_LABEL = 'FirstRangeDisplayLabel';
47 
48  const FIRST_SERIES_FORMATTED_VALUE = 'FirstSeriesFormattedValue';
49 
50  const SECOND_SERIES_VALUE = 'SecondSeriesValue';
51 
52  const SECOND_SERIES_DISPLAY_LABEL = 'SecondSeriesDisplayLabel';
53 
54  const SECOND_SERIES_FORMATTED_VALUE = 'SecondSeriesFormattedValue';
55 
59  protected $report;
60 
64  protected $data;
65 
69  protected $secondSeriesValueData = array();
70 
74  protected $secondSeriesDisplayLabels = array();
75 
79  protected $secondSeriesValueCount;
80 
84  protected $formattedData;
85 
90  public static function resolveFirstSeriesValueName($key)
91  {
92  assert('is_int($key)');
93  return self::FIRST_SERIES_VALUE . $key;
94  }
95 
100  public static function resolveFirstSeriesDisplayLabelName($key)
101  {
102  assert('is_int($key)');
103  return self::FIRST_SERIES_DISPLAY_LABEL . $key;
104  }
105 
110  public static function resolveFirstRangeDisplayLabelName($key)
111  {
112  assert('is_int($key)');
113  return self::FIRST_RANGE_DISPLAY_LABEL . $key;
114  }
115 
120  public static function resolveFirstSeriesFormattedValueName($key)
121  {
122  assert('is_int($key)');
123  return self::FIRST_SERIES_FORMATTED_VALUE . $key;
124  }
125 
130  public static function resolveSecondSeriesValueName($key)
131  {
132  assert('is_int($key)');
133  return self::SECOND_SERIES_VALUE . $key;
134  }
135 
140  public static function resolveSecondSeriesDisplayLabelName($key)
141  {
142  assert('is_int($key)');
143  return self::SECOND_SERIES_DISPLAY_LABEL . $key;
144  }
145 
150  public static function resolveSecondSeriesFormattedValueName($key)
151  {
152  assert('is_int($key)');
153  return self::SECOND_SERIES_FORMATTED_VALUE . $key;
154  }
155 
163  public function __construct(Report $report, Array $data, Array $secondSeriesValueData = array(),
164  Array $secondSeriesDisplayLabels = array(),
165  $secondSeriesValueCount = null)
166  {
167  assert('is_int($secondSeriesValueCount) || $secondSeriesValueCount == null');
168  $this->report = $report;
169  $this->data = $data;
170  $this->secondSeriesValueData = $secondSeriesValueData;
171  $this->secondSeriesDisplayLabels = $secondSeriesDisplayLabels;
172  $this->secondSeriesValueCount = $secondSeriesValueCount;
173  }
174 
178  public function getType()
179  {
180  return $this->report->getChart()->type;
181  }
182 
186  public function getData()
187  {
188  if ($this->formattedData == null)
189  {
190  $this->formattedData = $this->formatData($this->data);
191  }
192  return $this->formattedData;
193  }
194 
198  public function getSecondSeriesValueCount()
199  {
200  return $this->secondSeriesValueCount;
201  }
202 
206  public function isStacked()
207  {
208  return ChartRules::isStacked($this->getType());
209  }
210 
215  public function getSecondSeriesDisplayLabelByKey($key)
216  {
217  assert('is_int($key)');
218  return $this->secondSeriesDisplayLabels[$key];
219  }
220 
225  protected function formatData($data)
226  {
227  if (!$this->isStacked())
228  {
229  return $data;
230  }
231  foreach ($this->secondSeriesValueData as $secondSeriesKey)
232  {
233  foreach ($data as $firstSeriesDataKey => $firstSeriesData)
234  {
235  if (isset($firstSeriesData[self::resolveFirstSeriesValueName($secondSeriesKey)]) &&
236  !isset($firstSeriesData[self::resolveFirstSeriesFormattedValueName($secondSeriesKey)]))
237  {
238  $value = $firstSeriesData[self::resolveFirstSeriesValueName($secondSeriesKey)];
239  $displayAttribute = $this->report->getDisplayAttributeByAttribute($this->report->getChart()->firstRange);
240  $data[$firstSeriesDataKey][self::resolveFirstSeriesFormattedValueName($secondSeriesKey)] =
241  $this->formatValue($displayAttribute, $value);
242  }
243  if (isset($firstSeriesData[self::resolveSecondSeriesValueName($secondSeriesKey)]) &&
244  !isset($firstSeriesData[self::resolveSecondSeriesFormattedValueName($secondSeriesKey)]))
245  {
246  $value = $firstSeriesData[self::resolveSecondSeriesValueName($secondSeriesKey)];
247  $displayAttribute = $this->report->getDisplayAttributeByAttribute($this->report->getChart()->secondRange);
248  $data[$firstSeriesDataKey][self::resolveSecondSeriesFormattedValueName($secondSeriesKey)] =
249  $this->formatValue($displayAttribute, $value);
250  }
251  }
252  }
253  return $data;
254  }
255 
263  protected function formatValue(DisplayAttributeForReportForm $displayAttribute, $value)
264  {
265  if ($displayAttribute->isATypeOfCurrencyValue())
266  {
267  if ($this->report->getCurrencyConversionType() == Report::CURRENCY_CONVERSION_TYPE_ACTUAL)
268  {
269  return Yii::app()->numberFormatter->formatDecimal((float)$value);
270  }
271  elseif ($this->report->getCurrencyConversionType() == Report::CURRENCY_CONVERSION_TYPE_BASE)
272  {
273  return Yii::app()->numberFormatter->formatCurrency((float)$value, Yii::app()->currencyHelper->getBaseCode());
274  }
275  elseif ($this->report->getCurrencyConversionType() == Report::CURRENCY_CONVERSION_TYPE_SPOT)
276  {
277  return Yii::app()->numberFormatter->formatCurrency((float)$value * $this->report->getFromBaseToSpotRate(),
278  $this->report->getSpotConversionCurrencyCode());
279  }
280  else
281  {
282  throw new NotSupportedException();
283  }
284  }
285  elseif ($displayAttribute->getDisplayElementType() == 'Decimal')
286  {
287  return Yii::app()->numberFormatter->formatDecimal((float)$value);
288  }
289  elseif ($displayAttribute->getDisplayElementType() == 'Integer')
290  {
291  return Yii::app()->numberFormatter->formatDecimal((int)$value);
292  }
293  elseif ($displayAttribute->getDisplayElementType() == 'Date')
294  {
296  }
297  elseif ($displayAttribute->getDisplayElementType() == 'DateTime')
298  {
299  return DateTimeUtil::convertDbFormattedDateTimeToLocaleFormattedDisplay($value);
300  }
301  else
302  {
303  return $value;
304  }
305  }
306  }
307 ?>
__construct(Report $report, Array $data, Array $secondSeriesValueData=array(), Array $secondSeriesDisplayLabels=array(), $secondSeriesValueCount=null)
const CURRENCY_CONVERSION_TYPE_SPOT
Definition: Report.php:82
formatValue(DisplayAttributeForReportForm $displayAttribute, $value)
const CURRENCY_CONVERSION_TYPE_BASE
Definition: Report.php:76
const CURRENCY_CONVERSION_TYPE_ACTUAL
Definition: Report.php:70
static resolveValueForDateLocaleFormattedDisplay($date, $displayFormat=null)
Generated on Tue Jan 28 2020 07:10:19