PageRenderTime 61ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

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

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