All Data Structures Functions Variables Pages
MenuUtil.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 
40  class MenuUtil
41  {
42  const MENU_VIEW_ITEMS = 'MenuViewItems';
43  const ADMIN_MENU_VIEW_ITEMS = 'AdminMenuViewItems';
44 
45  public static function resolveByCacheAndGetVisibleAndOrderedTabMenuByCurrentUser()
46  {
47  $user = Yii::app()->user->userModel;
48  return self::resolveByCacheAndGetVisibleAndOrderedTabMenuByUser($user);
49  }
50 
56  {
57  assert('$user instanceof User && $user != null');
58  try
59  {
60  // not using default value to save cpu cycles on requests that follow the first exception.
61  $items = GeneralCache::getEntry(self::getMenuViewItemsCacheIdentifier());
62  }
63  catch (NotFoundException $e)
64  {
65  $items = self::getVisibleAndOrderedTabMenuByUser($user);
66  GeneralCache::cacheEntry(self::getMenuViewItemsCacheIdentifier(), $items);
67  }
68  static::resolveTabMenuForDynamicLabelContent($items);
69  return $items;
70  }
71 
72  public static function resolveByCacheAndGetVisibleAndOrderedAdminTabMenuByCurrentUser()
73  {
74  $user = Yii::app()->user->userModel;
75  return self::resolveByCacheAndGetVisibleAndOrderedAdminTabMenuByUser($user);
76  }
77 
83  {
84  assert('$user instanceof User && $user != null');
85  try
86  {
87  // not using default value to save cpu cycles on requests that follow the first exception.
88  $items = GeneralCache::getEntry(self::getAdminMenuViewItemsCacheIdentifier());
89  }
90  catch (NotFoundException $e)
91  {
92  $items = self::getVisibleAndOrderedAdminTabMenuByUser($user);
93  GeneralCache::cacheEntry(self::getAdminMenuViewItemsCacheIdentifier(), $items);
94  }
95  return $items;
96  }
97 
102  protected static function getMenuViewItemsCacheIdentifier()
103  {
104  return self::getMenuViewItemsCacheIdentifierByUser(Yii::app()->user->userModel);
105  }
106 
111  public static function getMenuViewItemsCacheIdentifierByUser($user)
112  {
113  return self::MENU_VIEW_ITEMS . $user->id . Yii::app()->language;
114  }
115 
120  protected static function getAdminMenuViewItemsCacheIdentifier()
121  {
122  return self::getAdminMenuViewItemsCacheIdentifierByUser(Yii::app()->user->userModel);
123  }
124 
129  public static function getAdminMenuViewItemsCacheIdentifierByUser($user)
130  {
131  assert('$user instanceof User && $user != null');
132  return self::ADMIN_MENU_VIEW_ITEMS . $user->id . Yii::app()->language;
133  }
134 
138  public static function forgetCacheEntryForTabMenuByUser($user)
139  {
140  $identifier = self::getMenuViewItemsCacheIdentifierByUser($user);
141  GeneralCache::forgetEntry($identifier);
142  }
143 
152  {
153  return self::getVisibleAndOrderedTabMenuByUser(Yii::app()->user->userModel);
154  }
155 
160  public static function getVisibleAndOrderedTabMenuByUser($user)
161  {
162  assert('$user instanceof User && $user != null');
163  $moduleMenuItemsInOrder = array();
164  $tabMenuItems = self::getCustomVisibleAndOrderedTabMenuItemsByUser($user);
165  $orderedModules = self::getModuleOrderingForTabMenuByUser($user);
166  $modules = Module::getModuleObjects();
167  foreach ($modules as $moduleId => $module)
168  {
169  $moduleMenuItems = self::getAccessibleModuleTabMenuByUser(get_class($module), $user);
170  if ($module->isEnabled() && count($moduleMenuItems) > 0)
171  {
172  if (($order = array_search($module->getName(), $orderedModules)) !== false)
173  {
174  $moduleMenuItemsInOrder[$order] = self::resolveMenuItemsForLanguageLocalization(
175  $moduleMenuItems, get_class($module));
176  $moduleMenuItemsInOrder[$order][0]['moduleId'] = $moduleId;
177  $moduleMenuItemsInOrder[$order][0]['itemOptions'] = array('id' => $moduleId);
178  }
179  }
180  }
181  ksort($moduleMenuItemsInOrder);
182  foreach ($moduleMenuItemsInOrder as $menuItems)
183  {
184  foreach ($menuItems as $item)
185  {
186  $tabMenuItems[$item['moduleId']] = $item;
187  }
188  }
189  foreach ($tabMenuItems as $key => $menuItem)
190  {
191  if (!is_array($menuItem))
192  {
193  unset($tabMenuItems[$key]);
194  }
195  }
196  return $tabMenuItems;
197  }
198 
203  public static function getCustomVisibleAndOrderedTabMenuItemsByUser($user)
204  {
205  $tabMenuItems = array();
206  if (!null == ZurmoConfigurationUtil::getByUserAndModuleName($user, 'ZurmoModule', 'VisibleAndOrderedTabMenuItems'))
207  {
208  $customOrderedTabMenuItems = unserialize(ZurmoConfigurationUtil::getByUserAndModuleName(
209  $user, 'ZurmoModule', 'VisibleAndOrderedTabMenuItems'));
210  foreach ($customOrderedTabMenuItems as $moduleId)
211  {
212  $tabMenuItems[$moduleId] = "";
213  }
214  }
215  return $tabMenuItems;
216  }
217 
226  {
227  return self::getVisibleAndOrderedAdminTabMenuByUser(Yii::app()->user->userModel);
228  }
229 
234  public static function getVisibleAndOrderedAdminTabMenuByUser($user)
235  {
236  assert('$user instanceof User && $user != null');
237  $moduleMenuItemsInOrder = array();
238  $tabMenuItems = array();
239  $orderedModules = self::getModuleOrderingForAdminTabMenuByUser($user);
240  $modules = Module::getModuleObjects();
241  foreach ($modules as $moduleId => $module)
242  {
243  $moduleMenuItems = self::getAccessibleModuleAdminTabMenuByUser(get_class($module), $user);
244  if ($module->isEnabled() && count($moduleMenuItems) > 0)
245  {
246  if (($order = array_search($module->getName(), $orderedModules)) !== false)
247  {
248  $moduleMenuItemsInOrder[$order] = self::resolveMenuItemsForLanguageLocalization(
249  $moduleMenuItems, get_class($module));
250  $moduleMenuItemsInOrder[$order][0]['moduleId'] = $moduleId;
251  }
252  }
253  }
254  ksort($moduleMenuItemsInOrder);
255  foreach ($moduleMenuItemsInOrder as $menuItems)
256  {
257  foreach ($menuItems as $itemKey => $item)
258  {
259  $tabMenuItems[] = $item;
260  }
261  }
262  return $tabMenuItems;
263  }
264 
270  {
271  $user = Yii::app()->user->userModel;
272  return self::getAccessibleShortcutsCreateMenuByUser($user);
273  }
274 
279  public static function getAccessibleShortcutsCreateMenuByUser($user)
280  {
281  assert('$user instanceof User && $user != null');
282  $modules = Module::getModuleObjects();
283  $createMenuItems = array('label' => Zurmo::t('Core', 'Create'),
284  'url' => null,
285  'items' => array());
286  foreach ($modules as $module)
287  {
288  $metadata = $module::getShortCutsCreateMenuItems();
289  $menuItems = self::resolveModuleMenuForAccess(get_class($module), $metadata, $user);
290  $menuItems = self::resolveMenuItemsForLanguageLocalization($menuItems, get_class($module));
291  if (!empty($menuItems))
292  {
293  $createMenuItems['items'] = array_merge($createMenuItems['items'],
294  self::resolveMenuItemsForLanguageLocalization
295  ($menuItems, get_class($module)));
296  }
297  }
298  if (empty($createMenuItems['items']))
299  {
300  return array();
301  }
302  return $createMenuItems;
303  }
304 
309  public static function getAccessibleConfigureMenuByCurrentUser($moduleClassName)
310  {
311  assert('is_string($moduleClassName)');
312  $user = Yii::app()->user->userModel;
313  return self::getAccessibleConfigureMenuByUser($moduleClassName, $user);
314  }
315 
321  public static function getAccessibleConfigureMenuByUser($moduleClassName, $user)
322  {
323  assert('is_string($moduleClassName)');
324  assert('$user instanceof User && $user != null');
325  $metadata = $moduleClassName::getConfigureMenuItems();
326  $menuItems = self::resolveModuleMenuForAccess($moduleClassName, $metadata, $user);
327  return self::resolveMenuItemsForLanguageLocalization($menuItems,
328  $moduleClassName,
329  array('titleLabel', 'descriptionLabel'));
330  }
331 
336  public static function getAccessibleConfigureSubMenuByCurrentUser($moduleClassName)
337  {
338  assert('is_string($moduleClassName)');
339  $user = Yii::app()->user->userModel;
340  return self::getAccessibleConfigureSubMenuByUser($moduleClassName, $user);
341  }
342 
348  public static function getAccessibleConfigureSubMenuByUser($moduleClassName, $user)
349  {
350  assert('is_string($moduleClassName)');
351  assert('$user instanceof User && $user != null');
352  $metadata = $moduleClassName::getConfigureSubMenuItems();
353  $menuItems = self::resolveModuleMenuForAccess($moduleClassName, $metadata, $user);
354  return self::resolveMenuItemsForLanguageLocalization($menuItems,
355  $moduleClassName,
356  array('titleLabel', 'descriptionLabel'));
357  }
358 
359  public static function getOrderedAccessibleHeaderMenuForCurrentUser()
360  {
361  $user = Yii::app()->user->userModel;
362  return self::getOrderedAccessibleHeaderMenuForUser($user);
363  }
364 
369  public static function getOrderedAccessibleHeaderMenuForUser($user)
370  {
371  assert('$user instanceof User && $user != null');
372  $headerMenuItems = static::getAccessibleHeaderMenuForUser($user);
373  usort($headerMenuItems, "static::orderHeaderMenuItems");
374  return $headerMenuItems;
375  }
376 
381  protected static function getAccessibleHeaderMenuForCurrentUser()
382  {
383  $user = Yii::app()->user->userModel;
384  return self::getAccessibleHeaderMenuForUser($user);
385  }
386 
387  protected static function getAccessibleHeaderMenuForUser($user)
388  {
389  assert('$user instanceof User && $user != null');
390  $modules = Module::getModuleObjects();
391  $headerMenuItems = array();
392  foreach ($modules as $module)
393  {
394  $metadata = $module::getMetadata();
395  if (!empty($metadata['global']['headerMenuItems']))
396  {
397  $menuItems = self::resolveModuleMenuForAccess(get_class($module),
398  $metadata['global']['headerMenuItems'],
399  $user);
400  $headerMenuItems = array_merge($headerMenuItems,
401  self::resolveMenuItemsForLanguageLocalization
402  ($menuItems, get_class($module)));
403  }
404  }
405  return $headerMenuItems;
406  }
407 
408  protected static function orderHeaderMenuItems($a, $b)
409  {
410  if (!isset($a['order']))
411  {
412  $aOrder = 1;
413  }
414  else
415  {
416  $aOrder = $a['order'];
417  }
418  if (!isset($b['order']))
419  {
420  $bOrder = 1;
421  }
422  else
423  {
424  $bOrder = $b['order'];
425  }
426  return $aOrder - $bOrder;
427  }
428 
434  {
435  $user = Yii::app()->user->userModel;
436  return self::getAccessibleOrderedUserHeaderMenuForUser($user);
437  }
438 
443  public static function getAccessibleOrderedUserHeaderMenuForUser($user)
444  {
445  assert('$user instanceof User && $user != null');
446  $modules = Module::getModuleObjects();
447  $headerMenuItems = array();
448  foreach ($modules as $module)
449  {
450  $metadata = $module::getMetadata();
451  if (!empty($metadata['global']['userHeaderMenuItems']))
452  {
453  $menuItems = self::resolveModuleMenuForAccess(get_class($module),
454  $metadata['global']['userHeaderMenuItems'],
455  $user);
456  $headerMenuItems = array_merge($headerMenuItems,
457  self::resolveMenuItemsForLanguageLocalization
458  ($menuItems, get_class($module)));
459  }
460  }
461  $orderedHeaderMenuItems = array();
462  foreach ($headerMenuItems as $item)
463  {
464  if (isset($item['order']))
465  {
466  $orderedHeaderMenuItems[$item['order']] = $item;
467  }
468  else
469  {
470  $orderedHeaderMenuItems[] = $item;
471  }
472  }
473  ksort($orderedHeaderMenuItems);
474  return $orderedHeaderMenuItems;
475  }
476 
481  public static function getAccessibleModuleTabMenuByUser($moduleClassName, $user)
482  {
483  assert('$user instanceof User && $user != null');
484  assert('is_string($moduleClassName)');
485  if (null == $user)
486  {
487  $user = Yii::app()->user->userModel;
488  }
489  if (RightsUtil::canUserAccessModule($moduleClassName, $user))
490  {
491  $metadata = $moduleClassName::getTabMenuItems($user);
492  if (!empty($metadata))
493  {
494  return self::resolveModuleMenuForAccess($moduleClassName, $metadata, $user);
495  }
496  }
497  return array();
498  }
499 
504  public static function getAccessibleModuleAdminTabMenuByUser($moduleClassName, $user)
505  {
506  assert('$user instanceof User && $user != null');
507  assert('is_string($moduleClassName)');
508  if (null == $user)
509  {
510  $user = Yii::app()->user->userModel;
511  }
512  if (RightsUtil::canUserAccessModule($moduleClassName, $user))
513  {
514  $metadata = $moduleClassName::getAdminTabMenuItems($user);
515  if (!empty($metadata))
516  {
517  return self::resolveModuleMenuForAccess($moduleClassName, $metadata, $user);
518  }
519  }
520  return array();
521  }
522 
526  protected static function resolveModuleMenuForAccess($moduleClassName, array $menu, $user = null)
527  {
528  assert('is_string($moduleClassName)');
529  assert('$user == null || $user instanceof User');
530  $resolvedMenu = array();
531  foreach ($menu as $index => $menuItem)
532  {
533  if (self::doesUserHaveRightToViewMenuItem($moduleClassName, $menuItem, $user))
534  {
535  if (!empty($menuItem['items']))
536  {
537  $resolvedNestedItems = self::resolveModuleMenuForAccess($moduleClassName,
538  $menuItem['items'],
539  $user);
540  if (count($resolvedNestedItems) > 0)
541  {
542  $menuItem['items'] = $resolvedNestedItems;
543  }
544  else
545  {
546  unset($menuItem['items']);
547  }
548  }
549  $resolvedMenu[] = $menuItem;
550  }
551  }
552  return $resolvedMenu;
553  }
554 
558  protected static function doesUserHaveRightToViewMenuItem($moduleClassName, $item, $user)
559  {
560  assert('$user == null || $user instanceof User');
561  if ( $user == null ||
562  !isset($item['right']) ||
563  Right::ALLOW == $user->getEffectiveRight($moduleClassName , $item['right']))
564  {
565  return true;
566  }
567  return false;
568  }
569 
574  protected static function getModuleOrderingForTabMenuByUser($user)
575  {
576  assert('$user instanceof User');
577  $metadata = ZurmoModule::getMetadata();
578  if (isset($metadata['global']['tabMenuItemsModuleOrdering']))
579  {
580  assert('is_array($metadata["global"]["tabMenuItemsModuleOrdering"])');
581  $orderedModules = $metadata['global']['tabMenuItemsModuleOrdering'];
582  }
583  else
584  {
585  throw new NotSupportedException();
586  }
587  return $orderedModules;
588  }
589 
594  protected static function getModuleOrderingForAdminTabMenuByUser($user)
595  {
596  assert('$user instanceof User');
597  $metadata = ZurmoModule::getMetadata();
598  if (isset($metadata['global']['adminTabMenuItemsModuleOrdering']))
599  {
600  assert('is_array($metadata["global"]["adminTabMenuItemsModuleOrdering"])');
601  $orderedModules = $metadata['global']['adminTabMenuItemsModuleOrdering'];
602  }
603  else
604  {
605  throw new NotSupportedException();
606  }
607  return $orderedModules;
608  }
609 
616  protected static function resolveMenuItemsForLanguageLocalization( $menuItems,
617  $moduleClassName,
618  $labelElements = array('label'),
619  $urlElements = array('url'),
620  $ajaxLinkOptionsElements = array('ajaxLinkOptions'))
621  {
622  assert('is_array($menuItems)');
623  assert('is_string($moduleClassName)');
624  $translationParams = LabelUtil::getTranslationParamsForAllModules();
625  foreach ($menuItems as $itemKey => $item)
626  {
627  foreach ($labelElements as $labelElement)
628  {
629  MetadataUtil::resolveEvaluateSubString($menuItems[$itemKey][$labelElement], 'translationParams', $translationParams);
630  }
631  foreach ($urlElements as $urlElement)
632  {
633  MetadataUtil::resolveEvaluateSubString($menuItems[$itemKey][$urlElement]);
634  }
635  if (isset($item['items']))
636  {
637  $menuItems[$itemKey]['items'] = self::resolveMenuItemsForLanguageLocalization($item['items'],
638  $moduleClassName,
639  $labelElements);
640  }
641  foreach ($ajaxLinkOptionsElements as $ajaxLinkOptionsElement)
642  {
643  if (isset($menuItems[$itemKey][$ajaxLinkOptionsElement]))
644  {
645  $substring = $menuItems[$itemKey][$ajaxLinkOptionsElement];
646  if (is_string($substring))
647  {
648  $options = array();
649  eval("\$options = $substring;");
650  $menuItems[$itemKey][$ajaxLinkOptionsElement] = $options;
651  }
652  }
653  }
654  }
655  return $menuItems;
656  }
657 
658  protected static function resolveTabMenuForDynamicLabelContent(& $items)
659  {
660  foreach ($items as $key => $item)
661  {
662  if (isset($items[$key]['dynamicLabelContent']))
663  {
664  MetadataUtil::resolveEvaluateSubString($items[$key]['dynamicLabelContent']);
665  if (isset($items[$key]['items']))
666  {
667  static::resolveTabMenuForDynamicLabelContent($items[$key]['items']);
668  }
669  }
670  }
671  }
672  }
673 ?>
static getAdminMenuViewItemsCacheIdentifier()
Definition: MenuUtil.php:120
static getMetadata(User $user=null)
Definition: Module.php:502
static getAccessibleModuleAdminTabMenuByUser($moduleClassName, $user)
Definition: MenuUtil.php:504
static forgetCacheEntryForTabMenuByUser($user)
Definition: MenuUtil.php:138
static getModuleOrderingForAdminTabMenuByUser($user)
Definition: MenuUtil.php:594
static resolveByCacheAndGetVisibleAndOrderedTabMenuByUser($user)
Definition: MenuUtil.php:55
static getTranslationParamsForAllModules()
Definition: LabelUtil.php:75
static getVisibleAndOrderedAdminTabMenuByUser($user)
Definition: MenuUtil.php:234
static resolveModuleMenuForAccess($moduleClassName, array $menu, $user=null)
Definition: MenuUtil.php:526
static getVisibleAndOrderedTabMenuByCurrentUser()
Definition: MenuUtil.php:151
static getModuleObjects()
Definition: Module.php:51
static getAccessibleConfigureMenuByUser($moduleClassName, $user)
Definition: MenuUtil.php:321
static getMenuViewItemsCacheIdentifier()
Definition: MenuUtil.php:102
static doesUserHaveRightToViewMenuItem($moduleClassName, $item, $user)
Definition: MenuUtil.php:558
static getAccessibleConfigureMenuByCurrentUser($moduleClassName)
Definition: MenuUtil.php:309
static getAccessibleOrderedUserHeaderMenuForUser($user)
Definition: MenuUtil.php:443
static getAccessibleConfigureSubMenuByCurrentUser($moduleClassName)
Definition: MenuUtil.php:336
static getOrderedAccessibleHeaderMenuForUser($user)
Definition: MenuUtil.php:369
static getMenuViewItemsCacheIdentifierByUser($user)
Definition: MenuUtil.php:111
static resolveEvaluateSubString(&$subString, $resolveVariableName=null, $params=null, $defaultValue=null)
static getAdminMenuViewItemsCacheIdentifierByUser($user)
Definition: MenuUtil.php:129
static canUserAccessModule($moduleClassName, $user)
Definition: RightsUtil.php:103
static getByUserAndModuleName($user, $moduleName, $key, $cache=true)
static getAccessibleConfigureSubMenuByUser($moduleClassName, $user)
Definition: MenuUtil.php:348
static getVisibleAndOrderedTabMenuByUser($user)
Definition: MenuUtil.php:160
static getModuleOrderingForTabMenuByUser($user)
Definition: MenuUtil.php:574
static resolveMenuItemsForLanguageLocalization($menuItems, $moduleClassName, $labelElements=array('label'), $urlElements=array('url'), $ajaxLinkOptionsElements=array('ajaxLinkOptions'))
Definition: MenuUtil.php:616
static getAccessibleModuleTabMenuByUser($moduleClassName, $user)
Definition: MenuUtil.php:481
static getAccessibleOrderedUserHeaderMenuForCurrentUser()
Definition: MenuUtil.php:433
static getCustomVisibleAndOrderedTabMenuItemsByUser($user)
Definition: MenuUtil.php:203
static getAccessibleHeaderMenuForCurrentUser()
Definition: MenuUtil.php:381
static resolveByCacheAndGetVisibleAndOrderedAdminTabMenuByUser($user)
Definition: MenuUtil.php:82
static getVisibleAndOrderedAdminTabMenuByCurrentUser()
Definition: MenuUtil.php:225
static getAccessibleShortcutsCreateMenuByUser($user)
Definition: MenuUtil.php:279
static getAccessibleShortcutsCreateMenuByCurrentUser()
Definition: MenuUtil.php:269
Generated on Thu May 28 2020 07:10:36