PageRenderTime 28ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/app/protected/modules/activities/utils/LatestActivitiesUtil.php

https://bitbucket.org/ddonthula/zurmounl
PHP | 196 lines | 115 code | 10 blank | 71 comment | 9 complexity | c8013721b6e4145204ca1cbaa878d072 MD5 | raw file
Possible License(s): GPL-3.0, BSD-3-Clause, LGPL-3.0, LGPL-2.1, BSD-2-Clause, GPL-2.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 for working with latest activity views.
  28. */
  29. class LatestActivitiesUtil
  30. {
  31. const CONFIG_MODULE_NAME = 'ActivitiesModule';
  32. /**
  33. * Based on the current user, return model class names and thier display labels. Only include models
  34. * that the user has a right to access its corresponding module, as well as only models that implement the
  35. * MashableActivityInterface.
  36. * @param $includeHavingRelatedItems - if the returning data should include models that are
  37. * mashable but are connected via activityItems. An example is accounts, a mission is not conneted to an account
  38. * so if this setting is false, accounts would not be returned. Home/User are always returned.
  39. * @return array of model class names and display labels.
  40. */
  41. public static function getMashableModelDataForCurrentUser($includeHavingRelatedItems = true)
  42. {
  43. //todo: cache results to improve performance if needed.
  44. $mashableModelClassNames = array();
  45. $modules = Module::getModuleObjects();
  46. foreach ($modules as $module)
  47. {
  48. $modelClassNames = $module::getModelClassNames();
  49. foreach ($modelClassNames as $modelClassName)
  50. {
  51. $classToEvaluate = new ReflectionClass($modelClassName);
  52. if ($classToEvaluate->implementsInterface('MashableActivityInterface') &&
  53. !$classToEvaluate->isAbstract())
  54. {
  55. if (RightsUtil::canUserAccessModule(get_class($module), Yii::app()->user->userModel))
  56. {
  57. if (!$includeHavingRelatedItems && !$modelClassName::hasRelatedItems())
  58. {
  59. continue;
  60. }
  61. $mashableModelClassNames[$modelClassName] =
  62. $modelClassName::getModelLabelByTypeAndLanguage('Plural');
  63. }
  64. }
  65. }
  66. }
  67. return $mashableModelClassNames;
  68. }
  69. /**
  70. * Given an array of modelClassNames and relationItemIds build an array of searchAttributeData that
  71. * can be used by the RedBeanModelsDataProvider to produce a union query of data.
  72. * @param array $modelClassNames
  73. * @param array $relationItemIds
  74. * @return array $modelClassNamesAndSearchAttributeData
  75. */
  76. public static function getSearchAttributesDataByModelClassNamesAndRelatedItemIds($modelClassNames,
  77. $relationItemIds,
  78. $ownedByFilter)
  79. {
  80. assert('is_array($modelClassNames)');
  81. assert('is_array($relationItemIds)');
  82. assert('$ownedByFilter == LatestActivitiesConfigurationForm::OWNED_BY_FILTER_ALL ||
  83. $ownedByFilter == LatestActivitiesConfigurationForm::OWNED_BY_FILTER_USER ||
  84. is_int($ownedByFilter)');
  85. $modelClassNamesAndSearchAttributeData = array();
  86. foreach ($modelClassNames as $modelClassName)
  87. {
  88. $mashableActivityRules = // Not Coding Standard
  89. MashableActivityRulesFactory::createMashableActivityRulesByModel($modelClassName);
  90. if (count($relationItemIds) > 1)
  91. {
  92. $searchAttributesData = // Not Coding Standard
  93. $mashableActivityRules->resolveSearchAttributesDataByRelatedItemIds($relationItemIds);
  94. }
  95. elseif (count($relationItemIds) == 1)
  96. {
  97. $searchAttributesData = // Not Coding Standard
  98. $mashableActivityRules->resolveSearchAttributesDataByRelatedItemId($relationItemIds[0]);
  99. }
  100. else
  101. {
  102. $searchAttributesData = array();
  103. $searchAttributesData['clauses'] = array();
  104. $searchAttributesData['structure'] = null;
  105. $searchAttributesData = // Not Coding Standard
  106. $mashableActivityRules->resolveSearchAttributeDataForLatestActivities($searchAttributesData);
  107. }
  108. $mashableActivityRules->resolveSearchAttributesDataByOwnedByFilter($searchAttributesData, $ownedByFilter);
  109. $modelClassNamesAndSearchAttributeData[] = array($modelClassName => $searchAttributesData);
  110. }
  111. return $modelClassNamesAndSearchAttributeData;
  112. }
  113. /**
  114. * Given an array of modelClassNames build an array of sortAttributeData that
  115. * can be used by the RedBeanModelsDataProvider to produce a union query of data.
  116. * @param array $modelClassNames
  117. * @return array $modelClassNamesAndSortAttributes
  118. */
  119. public static function getSortAttributesByMashableModelClassNames($modelClassNames)
  120. {
  121. $modelClassNamesAndSortAttributes = array();
  122. foreach ($modelClassNames as $modelClassName)
  123. {
  124. $mashableActivityRules = // Not Coding Standard
  125. MashableActivityRulesFactory::createMashableActivityRulesByModel($modelClassName);
  126. $modelClassNamesAndSortAttributes[$modelClassName] =
  127. $mashableActivityRules->getLatestActivitiesOrderByAttributeName();
  128. }
  129. return $modelClassNamesAndSortAttributes;
  130. }
  131. /**
  132. * Given an array of $mashableModelClassNames, filter out and return that array based on the $filteredByModelName
  133. * value. If $filteredByModelName is set to LatestActivitiesConfigurationForm::FILTERED_BY_ALL then the array
  134. * will be returned as it was passed in, otherwise filter the array to a specific model.
  135. * @param array $mashableModelClassNames
  136. * @param string $filteredByModelName
  137. * @return array of filtered $mashableModelClassNames
  138. */
  139. public static function resolveMashableModelClassNamesByFilteredBy($mashableModelClassNames, $filteredByModelName)
  140. {
  141. assert('is_array($mashableModelClassNames)');
  142. assert('is_string($filteredByModelName) || $filteredByModelName == null');
  143. foreach ($mashableModelClassNames as $index => $modelClassName)
  144. {
  145. if ( $filteredByModelName != LatestActivitiesConfigurationForm::FILTERED_BY_ALL &&
  146. $filteredByModelName != $modelClassName)
  147. {
  148. unset($mashableModelClassNames[$index]);
  149. }
  150. }
  151. return array_values($mashableModelClassNames);
  152. }
  153. /**
  154. * Set a persistent config value for current user against portletId and keyName.
  155. * @param $portletId integer Id of the portlet to set value against
  156. * @param $keyName string Name of the key that should be set
  157. * @param $value string|integer|boolean Value that should be assigned to keyName config
  158. */
  159. public static function setPersistentConfigForCurrentUserByPortletIdAndKey($portletId, $keyName, $value)
  160. {
  161. assert('is_int($portletId)');
  162. $keyName = static::resolveKeyNameByPortletId($portletId, $keyName);
  163. ZurmoConfigurationUtil::setForCurrentUserByModuleName(static::CONFIG_MODULE_NAME, $keyName, $value);
  164. Yii::app()->user->setState($keyName, $value);
  165. }
  166. /**
  167. * Get a persistent config value for current user against portletId and keyName.
  168. * @param $portletId integer Id of the portlet to get value against
  169. * @param $keyName string Name of the key that should be returned
  170. * @param bool $returnBoolean bool Force return value to be boolean (explicit type casting)
  171. * @return bool|null|string
  172. */
  173. public static function getPersistentConfigForCurrentUserByPortletIdAndKey($portletId, $keyName, $returnBoolean = false)
  174. {
  175. assert('is_int($portletId)');
  176. $keyName = static::resolveKeyNameByPortletId($portletId, $keyName);
  177. $value = ZurmoConfigurationUtil::getForCurrentUserByModuleName(static::CONFIG_MODULE_NAME, $keyName);
  178. $value = ($returnBoolean)? (boolean) $value: $value;
  179. return $value;
  180. }
  181. protected static function resolveKeyNameByPortletId($portletId, $keyName)
  182. {
  183. return $portletId . '_' . $keyName;
  184. }
  185. }
  186. ?>