PageRenderTime 126ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/app/protected/modules/reports/adapters/SavedReportToReportAdapter.php

https://bitbucket.org/ddonthula/zurmofeb
PHP | 185 lines | 129 code | 8 blank | 48 comment | 9 complexity | 2dfbad79b6013d15f0ed821cb3514b0e MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, BSD-2-Clause, GPL-3.0, BSD-3-Clause, LGPL-3.0
  1. <?php
  2. /*********************************************************************************
  3. * Zurmo is a customer relationship management program developed by
  4. * Zurmo, Inc. Copyright (C) 2012 Zurmo Inc.
  5. *
  6. * Zurmo is free software; you can redistribute it and/or modify it under
  7. * the terms of the GNU 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 General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU 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 113 McHenry Road Suite 207,
  24. * Buffalo Grove, IL 60089, USA. or at email address contact@zurmo.com.
  25. ********************************************************************************/
  26. /**
  27. * Helper class to adapt a SavedReport object to a Report object
  28. */
  29. class SavedReportToReportAdapter
  30. {
  31. /**
  32. * @param $savedReport
  33. * @return Report
  34. */
  35. public static function makeReportBySavedReport($savedReport)
  36. {
  37. $report = new Report();
  38. if($savedReport->id > 0)
  39. {
  40. $report->setId((int)$savedReport->id);
  41. }
  42. $report->setDescription($savedReport->description);
  43. $report->setModuleClassName($savedReport->moduleClassName);
  44. $report->setName($savedReport->name);
  45. $report->setOwner($savedReport->owner);
  46. $report->setType($savedReport->type);
  47. $explicitReadWriteModelPermissions = ExplicitReadWriteModelPermissionsUtil::makeBySecurableItem($savedReport);
  48. $report->setExplicitReadWriteModelPermissions($explicitReadWriteModelPermissions);
  49. if($savedReport->serializedData != null)
  50. {
  51. $unserializedData = unserialize($savedReport->serializedData);
  52. if(isset($unserializedData['filtersStructure']))
  53. {
  54. $report->setFiltersStructure($unserializedData['filtersStructure']);
  55. }
  56. if(isset($unserializedData['currencyConversionType']))
  57. {
  58. $report->setCurrencyConversionType((int)$unserializedData['currencyConversionType']);
  59. }
  60. if(isset($unserializedData['spotConversionCurrencyCode']))
  61. {
  62. $report->setSpotConversionCurrencyCode($unserializedData['spotConversionCurrencyCode']);
  63. }
  64. self::makeComponentFormAndPopulateReportFromData(
  65. $unserializedData[ComponentForReportForm::TYPE_FILTERS], $report, 'Filter');
  66. self::makeComponentFormAndPopulateReportFromData(
  67. $unserializedData[ComponentForReportForm::TYPE_ORDER_BYS], $report, 'OrderBy');
  68. self::makeComponentFormAndPopulateReportFromData(
  69. $unserializedData[ComponentForReportForm::TYPE_GROUP_BYS], $report, 'GroupBy');
  70. self::makeComponentFormAndPopulateReportFromData(
  71. $unserializedData[ComponentForReportForm::TYPE_DISPLAY_ATTRIBUTES],
  72. $report, 'DisplayAttribute');
  73. self::makeComponentFormAndPopulateReportFromData(
  74. $unserializedData[ComponentForReportForm::TYPE_DRILL_DOWN_DISPLAY_ATTRIBUTES],
  75. $report, 'DrillDownDisplayAttribute');
  76. if(isset($unserializedData['chart']))
  77. {
  78. $moduleClassName = $report->getModuleClassName();
  79. $modelClassName = $moduleClassName::getPrimaryModelName();
  80. $adapter = ModelRelationsAndAttributesToSummationReportAdapter::
  81. make($moduleClassName, $modelClassName, $report->getType());
  82. $chart = new ChartForReportForm(
  83. ReportUtil::makeDataAndLabelsForSeriesOrRange(
  84. $adapter->getAttributesForChartSeries($report->getGroupBys(),
  85. $report->getDisplayAttributes())),
  86. ReportUtil::makeDataAndLabelsForSeriesOrRange(
  87. $adapter->getAttributesForChartRange($report->getDisplayAttributes())));
  88. $chart->setAttributes($unserializedData['chart']);
  89. $report->setChart($chart);
  90. }
  91. }
  92. return $report;
  93. }
  94. /**
  95. * @param Report $report
  96. * @param SavedReport$savedReport
  97. */
  98. public static function resolveReportToSavedReport(Report $report, SavedReport $savedReport)
  99. {
  100. $savedReport->description = $report->getDescription();
  101. $savedReport->moduleClassName = $report->getModuleClassName();
  102. $savedReport->name = $report->getName();
  103. $savedReport->owner = $report->getOwner();
  104. $savedReport->type = $report->getType();
  105. $data = array();
  106. $data['filtersStructure'] = $report->getFiltersStructure();
  107. $data['currencyConversionType'] = $report->getCurrencyConversionType();
  108. $data['spotConversionCurrencyCode'] = $report->getSpotConversionCurrencyCode();
  109. $data[ComponentForReportForm::TYPE_FILTERS] =
  110. self::makeArrayFromComponentFormsAttributesData($report->getFilters());
  111. $data[ComponentForReportForm::TYPE_ORDER_BYS] =
  112. self::makeArrayFromComponentFormsAttributesData($report->getOrderBys());
  113. $data[ComponentForReportForm::TYPE_GROUP_BYS] =
  114. self::makeArrayFromComponentFormsAttributesData($report->getGroupBys());
  115. $data[ComponentForReportForm::TYPE_DISPLAY_ATTRIBUTES] =
  116. self::makeArrayFromComponentFormsAttributesData($report->getDisplayAttributes());
  117. $data[ComponentForReportForm::TYPE_DRILL_DOWN_DISPLAY_ATTRIBUTES] =
  118. self::makeArrayFromComponentFormsAttributesData($report->getDrillDownDisplayAttributes());
  119. if($report->getChart()->type != null)
  120. {
  121. $data['chart'] = self::makeArrayFromChartForReportFormAttributesData($report->getChart());
  122. }
  123. $savedReport->serializedData = serialize($data);
  124. }
  125. /**
  126. * @param ChartForReportForm $chartForReportForm
  127. * @return array
  128. */
  129. protected static function makeArrayFromChartForReportFormAttributesData(ChartForReportForm $chartForReportForm)
  130. {
  131. $data = array();
  132. foreach($chartForReportForm->getAttributes() as $attribute => $value)
  133. {
  134. $data[$attribute] = $value;
  135. }
  136. return $data;
  137. }
  138. /**
  139. * @param array $componentFormsData
  140. * @return array
  141. */
  142. protected static function makeArrayFromComponentFormsAttributesData(Array $componentFormsData)
  143. {
  144. $data = array();
  145. foreach($componentFormsData as $key => $componentForm)
  146. {
  147. foreach($componentForm->getAttributes() as $attribute => $value)
  148. {
  149. $data[$key][$attribute] = $value;
  150. }
  151. }
  152. return $data;
  153. }
  154. /**
  155. * @param array $componentFormsData
  156. * @param Report $report
  157. * @param null|string $componentPrefix
  158. */
  159. protected static function makeComponentFormAndPopulateReportFromData($componentFormsData, Report $report, $componentPrefix)
  160. {
  161. $moduleClassName = $report->getModuleClassName();
  162. $addMethodName = 'add' . $componentPrefix;
  163. $componentClassName = $componentPrefix . 'ForReportForm';
  164. $rowKey = 0;
  165. foreach($componentFormsData as $componentFormData)
  166. {
  167. $component = new $componentClassName($moduleClassName,
  168. $moduleClassName::getPrimaryModelName(),
  169. $report->getType(),
  170. $rowKey);
  171. $component->setAttributes($componentFormData);
  172. $report->{$addMethodName}($component);
  173. $rowKey ++;
  174. }
  175. }
  176. }
  177. ?>