PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/app/protected/modules/activities/views/LatestActivtiesForPortletView.php

https://bitbucket.org/ddonthula/zurmofeb
PHP | 326 lines | 238 code | 31 blank | 57 comment | 12 complexity | 5820edc3ff5a23bd65d372a92c5494a1 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. * Base class used for wrapping a latest activity view.
  28. */
  29. abstract class LatestActivtiesForPortletView extends ConfigurableMetadataView
  30. implements PortletViewInterface,
  31. UserPersistentSettingsCleanupForPortletInterface
  32. {
  33. /**
  34. * Portlet parameters passed in from the portlet.
  35. * @var array
  36. */
  37. protected $params;
  38. protected $controllerId;
  39. protected $moduleId;
  40. protected $model;
  41. protected $uniqueLayoutId;
  42. protected $viewData;
  43. protected static $persistantUserPortletConfigs = array(
  44. 'rollup',
  45. 'ownedByFilter',
  46. 'filteredByModelName'
  47. );
  48. abstract protected function getLatestActivitiesViewClassName();
  49. /**
  50. * Some extra assertions are made to ensure this view is used in a way that it supports.
  51. */
  52. public function __construct($viewData, $params, $uniqueLayoutId)
  53. {
  54. assert('is_array($viewData) || $viewData == null');
  55. assert('isset($params["relationModuleId"])');
  56. assert('isset($params["relationModel"])');
  57. assert('isset($params["portletId"])');
  58. assert('is_string($uniqueLayoutId)');
  59. $this->moduleId = $params['relationModuleId'];
  60. $this->viewData = $viewData;
  61. $this->params = $params;
  62. $this->uniqueLayoutId = $uniqueLayoutId;
  63. }
  64. public static function getDefaultMetadata()
  65. {
  66. $metadata = array(
  67. 'perUser' => array(
  68. 'title' => "eval:Zurmo::t('ActivitiesModule', 'Latest Activities')",
  69. ),
  70. );
  71. return $metadata;
  72. }
  73. public function getTitle()
  74. {
  75. $title = Zurmo::t('ActivitiesModule', 'Latest Activities');
  76. return $title;
  77. }
  78. public function renderContent()
  79. {
  80. $content = $this->renderActionContent();
  81. $content .= $this->renderLatestActivitiesContent();
  82. return $content;
  83. }
  84. protected function renderLatestActivitiesContent()
  85. {
  86. $mashableModelClassNamesAndDisplayLabels = LatestActivitiesUtil::
  87. getMashableModelDataForCurrentUser(
  88. static::includeHavingRelatedItemsWhenRenderingMashableModels());
  89. if (count($mashableModelClassNamesAndDisplayLabels) > 0)
  90. {
  91. $uniquePageId = get_called_class();
  92. $latestActivitiesConfigurationForm = $this->makeLatestActivitiesConfigurationForm();
  93. $latestActivitiesConfigurationForm->mashableModelClassNamesAndDisplayLabels =
  94. $mashableModelClassNamesAndDisplayLabels;
  95. $this->resolveLatestActivitiesConfigFormFromRequest($latestActivitiesConfigurationForm);
  96. $latestActivitiesViewClassName = $this->getLatestActivitiesViewClassName();
  97. $dataProvider = $this->getDataProvider($uniquePageId, $latestActivitiesConfigurationForm);
  98. $latestView = new $latestActivitiesViewClassName($dataProvider,
  99. $latestActivitiesConfigurationForm,
  100. 'default', 'activities',
  101. $this->getPortletDetailsUrl(),
  102. $this->getNonAjaxRedirectUrl(),
  103. $uniquePageId,
  104. $this->params,
  105. get_class(Yii::app()->findModule($this->moduleId)));
  106. return $latestView->render();
  107. }
  108. }
  109. protected function resolveLatestActivitiesConfigFormFromRequest(&$latestActivitiesConfigurationForm)
  110. {
  111. $excludeFromRestore = array();
  112. if (isset($_GET[get_class($latestActivitiesConfigurationForm)]))
  113. {
  114. $latestActivitiesConfigurationForm->setAttributes($_GET[get_class($latestActivitiesConfigurationForm)]);
  115. $excludeFromRestore = $this->saveUserSettingsFromConfigForm($latestActivitiesConfigurationForm);
  116. }
  117. $this->restoreUserSettingsToConfigFrom($latestActivitiesConfigurationForm, $excludeFromRestore);
  118. }
  119. protected function saveUserSettingsFromConfigForm(&$latestActivitiesConfigurationForm)
  120. {
  121. $savedConfigs = array();
  122. foreach (static::$persistantUserPortletConfigs as $persistantUserConfigItem)
  123. {
  124. if ($latestActivitiesConfigurationForm->$persistantUserConfigItem !==
  125. LatestActivitiesUtil::getPersistentConfigForCurrentUserByPortletIdAndKey($this->params['portletId'],
  126. $persistantUserConfigItem))
  127. {
  128. LatestActivitiesUtil::setPersistentConfigForCurrentUserByPortletIdAndKey($this->params['portletId'],
  129. $persistantUserConfigItem,
  130. $latestActivitiesConfigurationForm->$persistantUserConfigItem
  131. );
  132. $savedConfigs[] = $persistantUserConfigItem;
  133. }
  134. }
  135. return $savedConfigs;
  136. }
  137. protected function restoreUserSettingsToConfigFrom(&$latestActivitiesConfigurationForm, $excludeFromRestore)
  138. {
  139. foreach (static::$persistantUserPortletConfigs as $persistantUserConfigItem)
  140. {
  141. if (in_array($persistantUserConfigItem, $excludeFromRestore))
  142. {
  143. continue;
  144. }
  145. $persistantUserConfigItemValue = LatestActivitiesUtil::getPersistentConfigForCurrentUserByPortletIdAndKey(
  146. $this->params['portletId'],
  147. $persistantUserConfigItem);
  148. if (isset($persistantUserConfigItemValue))
  149. {
  150. $latestActivitiesConfigurationForm->$persistantUserConfigItem = $persistantUserConfigItemValue;
  151. }
  152. }
  153. return $latestActivitiesConfigurationForm;
  154. }
  155. protected static function includeHavingRelatedItemsWhenRenderingMashableModels()
  156. {
  157. return false;
  158. }
  159. protected function makeLatestActivitiesConfigurationForm()
  160. {
  161. return new LatestActivitiesConfigurationForm();
  162. }
  163. /**
  164. * After a portlet action is completed, the portlet must be refreshed. This is the url to correctly
  165. * refresh the portlet content.
  166. */
  167. protected function getPortletDetailsUrl()
  168. {
  169. return Yii::app()->createUrl('/' . $this->moduleId . '/defaultPortlet/details',
  170. array_merge($_GET, array( 'portletId' =>
  171. $this->params['portletId'],
  172. 'uniqueLayoutId' => $this->uniqueLayoutId)));
  173. }
  174. /**
  175. * Url to go to after an action is completed. Typically returns user to either a model's detail view or
  176. * the home page dashboard.
  177. */
  178. protected function getNonAjaxRedirectUrl()
  179. {
  180. return Yii::app()->createUrl('/' . $this->moduleId . '/default/details',
  181. array( 'id' => $this->params['relationModel']->id));
  182. }
  183. protected function getDataProvider($uniquePageId, $form)
  184. {
  185. assert('is_string($uniquePageId)');
  186. assert('$form instanceOf LatestActivitiesConfigurationForm');
  187. $pageSize = Yii::app()->pagination->resolveActiveForCurrentUserByType('subListPageSize');
  188. $filteredMashableModelClassNames = LatestActivitiesUtil::resolveMashableModelClassNamesByFilteredBy(
  189. array_keys($form->mashableModelClassNamesAndDisplayLabels),
  190. $form->filteredByModelName);
  191. $relationItemId = (int)$this->params['relationModel']->getClassId('Item');
  192. if ($form->rollup)
  193. {
  194. $relationItemsIds = ModelRollUpUtil::getItemIdsByModelAndUser($this->params['relationModel'],
  195. Yii::app()->user->userModel);
  196. }
  197. else
  198. {
  199. $relationItemsIds = array($relationItemId);
  200. }
  201. $modelClassNamesAndSearchAttributeData = // Not Coding Standard
  202. LatestActivitiesUtil::
  203. getSearchAttributesDataByModelClassNamesAndRelatedItemIds($filteredMashableModelClassNames,
  204. $relationItemsIds, $form->ownedByFilter);
  205. $modelClassNamesAndSortAttributes = // Not Coding Standard
  206. LatestActivitiesUtil::getSortAttributesByMashableModelClassNames($filteredMashableModelClassNames);
  207. return new RedBeanModelsDataProvider($uniquePageId, $modelClassNamesAndSortAttributes,
  208. true, $modelClassNamesAndSearchAttributeData,
  209. array('pagination' => array('pageSize' => $pageSize)));
  210. }
  211. public static function canUserConfigure()
  212. {
  213. return false;
  214. }
  215. /**
  216. * What kind of PortletRules this view follows
  217. * @return PortletRulesType as string.
  218. */
  219. public static function getPortletRulesType()
  220. {
  221. return 'ModelDetails';
  222. }
  223. /**
  224. * The view's module class name.
  225. */
  226. public static function getModuleClassName()
  227. {
  228. return 'ActivitiesModule';
  229. }
  230. protected function renderActionContent()
  231. {
  232. $actionElementContent = $this->renderActionElementMenu(Zurmo::t('ActivitiesModule', 'Create'));
  233. $content = null;
  234. if ($actionElementContent != null)
  235. {
  236. $content .= '<div class="view-toolbar-container toolbar-mbmenu clearfix"><div class="view-toolbar">';
  237. $content .= $actionElementContent;
  238. $content .= '</div></div>';
  239. }
  240. return $content;
  241. }
  242. /**
  243. * Expects there to be a modelClassName parameter passed through the elementInformation that can be used
  244. * to compare the Rights security on based on the actionType of the element.
  245. * (non-PHPdoc)
  246. * @see MetadataView::shouldRenderToolBarElement()
  247. */
  248. protected function shouldRenderToolBarElement($element, $elementInformation)
  249. {
  250. assert('$element instanceof ActionElement');
  251. assert('is_array($elementInformation)');
  252. if (!parent::shouldRenderToolBarElement($element, $elementInformation))
  253. {
  254. return false;
  255. }
  256. if (isset($elementInformation['modelClassName']))
  257. {
  258. $modelClassName = $elementInformation['modelClassName'];
  259. //Todo: figure out how to not need to new up a new model.
  260. return ActionSecurityUtil::canCurrentUserPerformAction( $element->getActionType(),
  261. new $modelClassName(false));
  262. }
  263. return true;
  264. }
  265. protected static function resolvePropertyName($attribute)
  266. {
  267. return 'has' . ucfirst($attribute) . 'Switch';
  268. }
  269. public static function hasRollupSwitch()
  270. {
  271. return false;
  272. }
  273. ///*
  274. public static function hasOwnedByFilterSwitch()
  275. {
  276. return true;
  277. }
  278. public static function hasFilteredByModelNameSwitch()
  279. {
  280. return true;
  281. }
  282. public static function processBeforeDelete($portletId)
  283. {
  284. foreach (static::$persistantUserPortletConfigs as $persistantUserConfigItem)
  285. {
  286. $property = static::resolvePropertyName($persistantUserConfigItem);
  287. if (method_exists(get_called_class(), $property) && static::$property())
  288. {
  289. LatestActivitiesUtil::setPersistentConfigForCurrentUserByPortletIdAndKey(
  290. $portletId, $persistantUserConfigItem, null);
  291. }
  292. }
  293. }
  294. }
  295. ?>