All Data Structures Functions Variables Pages
Report.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 
46  class Report extends CComponent
47  {
51  const TYPE_ROWS_AND_COLUMNS = 'RowsAndColumns';
52 
57  const TYPE_SUMMATION = 'Summation';
58 
63  const TYPE_MATRIX = 'Matrix';
64 
71 
77 
83 
88  private $description;
89 
94  private $explicitReadWriteModelPermissions;
95 
100  private $id;
101 
106  private $moduleClassName;
107 
112  private $name;
113 
118  private $owner;
119 
124  private $type;
125 
131  private $filtersStructure;
132 
137  private $filters = array();
138 
143  private $orderBys = array();
144 
149  private $displayAttributes = array();
150 
155  private $drillDownDisplayAttributes = array();
156 
161  private $groupBys = array();
162 
166  private $chart;
167 
173  private $currencyConversionType;
174 
180  private $spotConversionCurrencyCode;
181 
185  public static function getTypeDropDownArray()
186  {
187  return array(self::TYPE_ROWS_AND_COLUMNS => Zurmo::t('ReportsModule', 'Rows and Columns'),
188  self::TYPE_SUMMATION => Zurmo::t('ReportsModule', 'Summation'),
189  self::TYPE_MATRIX => Zurmo::t('ReportsModule', 'Matrix'));
190  }
191 
198  {
199  $moduleClassNamesAndLabels = array();
200  foreach (self::getReportableModulesClassNamesCurrentUserHasAccessTo() as $moduleClassName)
201  {
202  if ($moduleClassName::getStateMetadataAdapterClassName() != null)
203  {
204  $reportRules = ReportRules::makeByModuleClassName($moduleClassName);
205  $label = $reportRules->getVariableStateModuleLabel(Yii::app()->user->userModel);
206  }
207  else
208  {
209  $label = $moduleClassName::getModuleLabelByTypeAndLanguage('Plural');
210  }
211  if ($label != null)
212  {
213  $moduleClassNamesAndLabels[$moduleClassName] = $label;
214  }
215  }
216  return $moduleClassNamesAndLabels;
217  }
218 
223  {
224  $moduleClassNames = array();
225  $modules = Module::getModuleObjects();
226  foreach ($modules as $module)
227  {
228  if ($module::isReportable())
229  {
231  {
232  $moduleClassNames[] = get_class($module);
233  }
234  }
235  }
236  return $moduleClassNames;
237  }
238 
239  public function __toString()
240  {
241  if (trim($this->name) == '')
242  {
243  return Zurmo::t('Core', '(Unnamed)');
244  }
245  return $this->name;
246  }
247 
257  {
258  if (!ReportSecurityUtil::canCurrentUserCanAccessModule($this->moduleClassName))
259  {
260  return false;
261  }
262  if (!ReportSecurityUtil::canCurrentUserAccessAllComponents($this->displayAttributes))
263  {
264  return false;
265  }
267  {
268  return false;
269  }
271  {
272  return false;
273  }
275  {
276  return false;
277  }
278  if (!ReportSecurityUtil::canCurrentUserAccessAllComponents($this->drillDownDisplayAttributes))
279  {
280  return false;
281  }
282  return true;
283  }
284 
288  public function getModuleClassName()
289  {
290  return $this->moduleClassName;
291  }
292 
296  public function setModuleClassName($moduleClassName)
297  {
298  assert('is_string($moduleClassName)');
299  $this->moduleClassName = $moduleClassName;
300  }
301 
305  public function getDescription()
306  {
307  return $this->description;
308  }
309 
313  public function setDescription($description)
314  {
315  assert('is_string($description) || $description == null');
316  $this->description = $description;
317  }
318 
322  public function setFiltersStructure($filtersStructure)
323  {
324  assert('is_string($filtersStructure)');
325  $this->filtersStructure = $filtersStructure;
326  }
327 
331  public function getFiltersStructure()
332  {
333  return $this->filtersStructure;
334  }
335 
339  public function getId()
340  {
341  return $this->id;
342  }
343 
347  public function setId($id)
348  {
349  assert('is_int($id)');
350  $this->id = $id;
351  }
352 
356  public function getName()
357  {
358  return $this->name;
359  }
360 
364  public function setName($name)
365  {
366  assert('is_string($name)');
367  $this->name = $name;
368  }
369 
373  public function getType()
374  {
375  return $this->type;
376  }
377 
381  public function setType($type)
382  {
383  assert('$type == self::TYPE_ROWS_AND_COLUMNS || $type == self::TYPE_SUMMATION || $type == self::TYPE_MATRIX');
384  $this->type = $type;
385  }
386 
390  public function getCurrencyConversionType()
391  {
392  return $this->currencyConversionType;
393  }
394 
398  public function setCurrencyConversionType($currencyConversionType)
399  {
400  assert('is_int($currencyConversionType)');
401  $this->currencyConversionType = $currencyConversionType;
402  }
403 
408  {
409  return $this->spotConversionCurrencyCode;
410  }
411 
415  public function setSpotConversionCurrencyCode($spotConversionCurrencyCode)
416  {
417  assert('is_string($spotConversionCurrencyCode)');
418  $this->spotConversionCurrencyCode = $spotConversionCurrencyCode;
419  }
420 
424  public function getFromBaseToSpotRate()
425  {
426  return 1 / Yii::app()->currencyHelper->getConversionRateToBase($this->spotConversionCurrencyCode);
427  }
428 
432  public function isNew()
433  {
434  if ($this->id > 0)
435  {
436  return false;
437  }
438  return true;
439  }
440 
444  public function getOwner()
445  {
446  if ($this->owner == null)
447  {
448  $this->owner = Yii::app()->user->userModel;
449  }
450  return $this->owner;
451  }
452 
456  public function setOwner(User $owner)
457  {
458  $this->owner = $owner;
459  }
460 
464  public function getFilters()
465  {
466  return $this->filters;
467  }
468 
472  public function addFilter(FilterForReportForm $filter)
473  {
474  $this->filters[] = $filter;
475  }
476 
480  public function removeAllFilters()
481  {
482  $this->filters = array();
483  }
484 
488  public function removeRuntimeFilters()
489  {
490  $filters = array();
491  foreach ($this->filters as $filter)
492  {
493  if (!$filter->availableAtRunTime)
494  {
495  $filters[] = $filter;
496  }
497  }
498  $this->filters = $filters;
499  }
500 
504  public function getGroupBys()
505  {
506  return $this->groupBys;
507  }
508 
512  public function addGroupBy(GroupByForReportForm $groupBy)
513  {
514  $this->groupBys[] = $groupBy;
515  }
516 
520  public function removeAllGroupBys()
521  {
522  $this->groupBys = array();
523  }
524 
528  public function getOrderBys()
529  {
530  return $this->orderBys;
531  }
532 
536  public function addOrderBy(OrderByForReportForm $orderBy)
537  {
538  $this->orderBys[] = $orderBy;
539  }
540 
544  public function removeAllOrderBys()
545  {
546  $this->orderBys = array();
547  }
548 
552  public function getDisplayAttributes()
553  {
554  return $this->displayAttributes;
555  }
556 
560  public function addDisplayAttribute(DisplayAttributeForReportForm $displayAttribute)
561  {
562  $this->displayAttributes[] = $displayAttribute;
563  }
564 
568  public function removeAllDisplayAttributes()
569  {
570  $this->displayAttributes = array();
571  }
572 
577  {
578  return $this->drillDownDisplayAttributes;
579  }
580 
584  public function addDrillDownDisplayAttribute(DrillDownDisplayAttributeForReportForm $drillDownDisplayAttribute)
585  {
586  $this->drillDownDisplayAttributes[] = $drillDownDisplayAttribute;
587  }
588 
593  {
594  $this->drillDownDisplayAttributes = array();
595  }
596 
600  public function getChart()
601  {
602  if ($this->chart == null)
603  {
604  $this->chart = new ChartForReportForm();
605  }
606  return $this->chart;
607  }
608 
612  public function setChart(ChartForReportForm $chart)
613  {
614  $this->chart = $chart;
615  }
616 
621  public function hasChart()
622  {
623  if ($this->getChart()->type == null)
624  {
625  return false;
626  }
627  return true;
628  }
629 
634  {
635  if ($this->explicitReadWriteModelPermissions == null)
636  {
637  $this->explicitReadWriteModelPermissions = new ExplicitReadWriteModelPermissions();
638  }
639  return $this->explicitReadWriteModelPermissions;
640  }
641 
646  public function setExplicitReadWriteModelPermissions(ExplicitReadWriteModelPermissions $explicitReadWriteModelPermissions)
647  {
648  $this->explicitReadWriteModelPermissions = $explicitReadWriteModelPermissions;
649  }
650 
655  public function hasRuntimeFilters()
656  {
657  foreach ($this->getFilters() as $filter)
658  {
659  if ($filter->availableAtRunTime)
660  {
661  return true;
662  }
663  }
664  return false;
665  }
666 
673  public function getDisplayAttributeIndex($attribute)
674  {
675  foreach ($this->displayAttributes as $key => $displayAttribute)
676  {
677  if ($attribute == $displayAttribute->attributeIndexOrDerivedType)
678  {
679  return $key;
680  }
681  }
682  return null;
683  }
684 
692  public function getDisplayAttributeByAttribute($attribute)
693  {
694  foreach ($this->getDisplayAttributes() as $displayAttribute)
695  {
696  if ($attribute == $displayAttribute->attributeIndexOrDerivedType)
697  {
698  return $displayAttribute;
699  }
700  }
701  throw new NotFoundException();
702  }
703 
710  public function resolveGroupBysAsFilters(Array $getData)
711  {
712  $newStartingStructurePosition = count($this->filters) + 1;
713  $structure = null;
714  foreach ($this->getGroupBys() as $groupBy)
715  {
716  $index = ReportResultsRowData::resolveDataParamKeyForDrillDown($groupBy->attributeIndexOrDerivedType);
717  if (isset($getData[$index]))
718  {
719  $value = $getData[$index];
720  }
721  else
722  {
723  $value = null;
724  }
725  $filter = new FilterForReportForm($groupBy->getModuleClassName(),
726  $groupBy->getModelClassName(),
727  $this->type);
728  $filter->attributeIndexOrDerivedType = $groupBy->attributeIndexOrDerivedType;
729  self::resolveGroupByAsFilterValue($value, $filter);
730 
731  $this->addFilter($filter);
732  if ($structure != null)
733  {
734  $structure .= ' AND ';
735  }
736  $structure .= $newStartingStructurePosition;
737  $newStartingStructurePosition++;
738  }
739  $structure = '(' . $structure . ')';
740  if ($this->filtersStructure != null)
741  {
742  $this->filtersStructure = '(' . $this->filtersStructure . ')';
743  $this->filtersStructure .= ' AND ';
744  }
745  $this->filtersStructure .= $structure;
746  }
747 
754  protected static function resolveGroupByAsFilterValue($value, FilterForReportForm $filter)
755  {
756  if ($value !== null)
757  {
758  $filter->operator = OperatorRules::TYPE_EQUALS;
759  $filter->value = $value;
760  }
761  else
762  {
763  $filter->operator = OperatorRules::TYPE_IS_NULL;
764  }
765  }
766  }
767 ?>
static getReportableModulesClassNamesCurrentUserHasAccessTo()
Definition: Report.php:222
getDescription()
Definition: Report.php:305
const CURRENCY_CONVERSION_TYPE_SPOT
Definition: Report.php:82
removeAllFilters()
Definition: Report.php:480
addDrillDownDisplayAttribute(DrillDownDisplayAttributeForReportForm $drillDownDisplayAttribute)
Definition: Report.php:584
static resolveDataParamKeyForDrillDown($attributeIndexOrDerivedType)
setOwner(User $owner)
Definition: Report.php:456
isNew()
Definition: Report.php:432
const CURRENCY_CONVERSION_TYPE_BASE
Definition: Report.php:76
addDisplayAttribute(DisplayAttributeForReportForm $displayAttribute)
Definition: Report.php:560
static resolveGroupByAsFilterValue($value, FilterForReportForm $filter)
Definition: Report.php:754
Definition: User.php:37
static getReportableModulesAndLabelsForCurrentUser()
Definition: Report.php:197
getType()
Definition: Report.php:373
setCurrencyConversionType($currencyConversionType)
Definition: Report.php:398
getFromBaseToSpotRate()
Definition: Report.php:424
getGroupBys()
Definition: Report.php:504
getChart()
Definition: Report.php:600
setName($name)
Definition: Report.php:364
resolveGroupBysAsFilters(Array $getData)
Definition: Report.php:710
setDescription($description)
Definition: Report.php:313
static getModuleObjects()
Definition: Module.php:51
setChart(ChartForReportForm $chart)
Definition: Report.php:612
setSpotConversionCurrencyCode($spotConversionCurrencyCode)
Definition: Report.php:415
removeAllOrderBys()
Definition: Report.php:544
getSpotConversionCurrencyCode()
Definition: Report.php:407
const TYPE_SUMMATION
Definition: Report.php:57
getCurrencyConversionType()
Definition: Report.php:390
getDisplayAttributes()
Definition: Report.php:552
const CURRENCY_CONVERSION_TYPE_ACTUAL
Definition: Report.php:70
setFiltersStructure($filtersStructure)
Definition: Report.php:322
getFilters()
Definition: Report.php:464
getFiltersStructure()
Definition: Report.php:331
removeAllGroupBys()
Definition: Report.php:520
removeRuntimeFilters()
Definition: Report.php:488
removeAllDisplayAttributes()
Definition: Report.php:568
hasRuntimeFilters()
Definition: Report.php:655
addFilter(FilterForReportForm $filter)
Definition: Report.php:472
getDisplayAttributeByAttribute($attribute)
Definition: Report.php:692
const TYPE_MATRIX
Definition: Report.php:63
static canCurrentUserCanAccessModule($moduleClassName)
getDisplayAttributeIndex($attribute)
Definition: Report.php:673
hasChart()
Definition: Report.php:621
getModuleClassName()
Definition: Report.php:288
setId($id)
Definition: Report.php:347
getOrderBys()
Definition: Report.php:528
getName()
Definition: Report.php:356
getExplicitReadWriteModelPermissions()
Definition: Report.php:633
static canCurrentUserAccessAllComponents(Array $componentForms)
getId()
Definition: Report.php:339
static makeByModuleClassName($moduleClassName)
const TYPE_ROWS_AND_COLUMNS
Definition: Report.php:51
addOrderBy(OrderByForReportForm $orderBy)
Definition: Report.php:536
getDrillDownDisplayAttributes()
Definition: Report.php:576
addGroupBy(GroupByForReportForm $groupBy)
Definition: Report.php:512
getOwner()
Definition: Report.php:444
removeAllDrillDownDisplayAttributes()
Definition: Report.php:592
setModuleClassName($moduleClassName)
Definition: Report.php:296
setExplicitReadWriteModelPermissions(ExplicitReadWriteModelPermissions $explicitReadWriteModelPermissions)
Definition: Report.php:646
static getTypeDropDownArray()
Definition: Report.php:185
setType($type)
Definition: Report.php:381
canCurrentUserProperlyRenderResults()
Definition: Report.php:256
Generated on Thu Apr 9 2020 07:10:43