All Data Structures Functions Variables Pages
PushDashboardUtil.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 
41  {
42  const GROUP_PREFIX = 'Group_';
43 
44  const USER_PREFIX = 'User_';
45 
46  const HOME_DASHBOARD = 'HomeDashboard';
47 
48  const DETAILS_AND_RELATIONS_VIEW = 'DetailsAndRelationsView';
49 
54  public static function canCurrentUserPushDashboardOrLayout()
55  {
56  if (RightsUtil::doesUserHaveAllowByRightName('ZurmoModule', ZurmoModule::RIGHT_PUSH_DASHBOARD_OR_LAYOUT,
57  Yii::app()->user->userModel))
58  {
59  return true;
60  }
61  return false;
62  }
63 
69  public static function pushDashboardToUsers(Dashboard $dashboard, $groupsAndUsers)
70  {
71  $processedUsers = array();
72  foreach ($groupsAndUsers['groups'] as $groupId)
73  {
74  $group = Group::getById(intval($groupId));
75  $usersInGroup = $group->getUsersExceptSystemUsers();
76  foreach ($usersInGroup as $user)
77  {
78  if (!in_array($user->id, $processedUsers))
79  {
80  $processedUsers[] = $user->id;
81  $userDashboard = self::resolveDefaultDashboardByUser($dashboard, $user);
82  self::pushUserHomeDashboardPortlets($user, $userDashboard, $dashboard);
83  }
84  }
85  }
86  foreach ($groupsAndUsers['users'] as $userId)
87  {
88  $user = User::getById(intval($userId));
89  if (!in_array($user->id, $processedUsers))
90  {
91  $processedUsers[] = $user->id;
92  $userDashboard = self::resolveDefaultDashboardByUser($dashboard, $user);
93  self::pushUserHomeDashboardPortlets($user, $userDashboard, $dashboard);
94  }
95  }
96  }
97 
104  public static function pushUserHomeDashboardPortlets(User $user, Dashboard $userDashboard, Dashboard $pushedDashboard)
105  {
106  $userDashboardPortletsLayoutId = self::HOME_DASHBOARD . $userDashboard->layoutId;
107  $userDashboardPortlets = Portlet::getByLayoutIdAndUserSortedById($userDashboardPortletsLayoutId,
108  $user->id);
109  $pushedDashboardPortletsLayoutId = self::HOME_DASHBOARD . $pushedDashboard->layoutId;
110  $pushedDashboardPortlets = Portlet::getByLayoutIdAndUserSortedById($pushedDashboardPortletsLayoutId,
111  Yii::app()->user->userModel->id);
112  foreach ($userDashboardPortlets as $portlet)
113  {
114  $portlet->delete();
115  unset($portlet);
116  }
117  foreach ($pushedDashboardPortlets as $pushedDashboardPortlet)
118  {
119  $portlet = new Portlet();
120  $portlet->column = $pushedDashboardPortlet->column;
121  $portlet->position = $pushedDashboardPortlet->position;
122  $portlet->layoutId = $userDashboardPortletsLayoutId;
123  $portlet->collapsed = $pushedDashboardPortlet->collapsed;
124  $portlet->viewType = $pushedDashboardPortlet->viewType;
125  $portlet->serializedViewData = $pushedDashboardPortlet->serializedViewData;
126  $portlet->user = $user;
127  $portlet->save();
128  }
129  }
130 
139  public static function resolveDefaultDashboardByUser(Dashboard $dashboard, User $user)
140  {
141  $userDefaultDashboards = Dashboard::getDefaultDashboardsByUser($user);
142  if (count($userDefaultDashboards) == 0)
143  {
144  $userDashboard = Dashboard::setDefaultDashboardForUser($user);
145  }
146  else
147  {
148  $userDashboard = $userDefaultDashboards[0];
149  }
150  $userDashboard->setTreatCurrentUserAsOwnerForPermissions(true);
151  $userDashboard->name = $dashboard->name;
152  $userDashboard->layoutType = $dashboard->layoutType;
153  $saved = $userDashboard->save();
154  if (!$saved)
155  {
156  throw new FailedToSaveModelException();
157  }
158  return $userDashboard;
159  }
160 
166  public static function resolveGroupsAndUsersFromPost($postData)
167  {
168  $groupIds = array();
169  $userIds = array();
170  $groupAndUserIds = array_filter(explode(',', $postData['GroupsAndUsers']['ids'])); // Not Coding Standard
171  foreach ($groupAndUserIds as $identifier)
172  {
173  if (strpos($identifier, self::GROUP_PREFIX) !== false)
174  {
175  $groupIds[] = intval(substr($identifier,
176  strpos($identifier, self::GROUP_PREFIX) + strlen(self::GROUP_PREFIX), strlen($identifier)));
177  }
178  elseif (strpos($identifier, self::USER_PREFIX) !== false)
179  {
180  $userIds[] = intval(substr($identifier,
181  strpos($identifier, self::USER_PREFIX) + strlen(self::USER_PREFIX), strlen($identifier)));
182  }
183  }
184  $groupsAndUsers = array();
185  $groupsAndUsers['groups'] = array_filter($groupIds);
186  $groupsAndUsers['users'] = array_filter($userIds);
187  return $groupsAndUsers;
188  }
189 
195  public static function pushLayoutToUsers($model, $groupsAndUsers)
196  {
197  $processedUsers = array();
198  foreach ($groupsAndUsers['groups'] as $groupId)
199  {
200  $group = Group::getById(intval($groupId));
201  $usersInGroup = $group->getUsersExceptSystemUsers();
202  foreach ($usersInGroup as $user)
203  {
204  if (!in_array($user->id, $processedUsers))
205  {
206  $processedUsers[] = $user->id;
207  self::pushDetailsAndRelationsViewPortlets($user, $model);
208  }
209  }
210  }
211  foreach ($groupsAndUsers['users'] as $userId)
212  {
213  $user = User::getById(intval($userId));
214  if (!in_array($user->id, $processedUsers))
215  {
216  $processedUsers[] = $user->id;
217  self::pushDetailsAndRelationsViewPortlets($user, $model);
218  }
219  }
220  }
221 
222  public static function pushDetailsAndRelationsViewPortlets(User $user, $model)
223  {
224  $layoutIdPrefix = get_class($model);
225  $layoutId = $layoutIdPrefix . self::DETAILS_AND_RELATIONS_VIEW;
226  $userLayoutPortlets = Portlet::getByLayoutIdAndUserSortedById($layoutId, $user->id);
227  $pushedLayoutPortlets = Portlet::getByLayoutIdAndUserSortedById($layoutId, Yii::app()->user->userModel->id);
228  foreach ($userLayoutPortlets as $portlet)
229  {
230  $portlet->delete();
231  unset($portlet);
232  }
233  foreach ($pushedLayoutPortlets as $pushedLayoutPortlet)
234  {
235  $portlet = new Portlet();
236  $portlet->column = $pushedLayoutPortlet->column;
237  $portlet->position = $pushedLayoutPortlet->position;
238  $portlet->layoutId = $layoutId;
239  $portlet->collapsed = $pushedLayoutPortlet->collapsed;
240  $portlet->viewType = $pushedLayoutPortlet->viewType;
241  $portlet->serializedViewData = $pushedLayoutPortlet->serializedViewData;
242  $portlet->user = $user;
243  $portlet->save();
244  }
245  }
246  }
247 ?>
static resolveGroupsAndUsersFromPost($postData)
Definition: User.php:37
static doesUserHaveAllowByRightName($moduleClassName, $rightName, $user)
Definition: RightsUtil.php:121
static pushLayoutToUsers($model, $groupsAndUsers)
static setDefaultDashboardForUser($user)
Definition: Dashboard.php:154
static resolveDefaultDashboardByUser(Dashboard $dashboard, User $user)
static canCurrentUserPushDashboardOrLayout()
static pushDashboardToUsers(Dashboard $dashboard, $groupsAndUsers)
static getById($id, $modelClassName=null)
static pushUserHomeDashboardPortlets(User $user, Dashboard $userDashboard, Dashboard $pushedDashboard)
Generated on Fri Jun 5 2020 07:10:32