BxDolStudioUtils.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup DolphinStudio Dolphin Studio
  7. * @{
  8. */
  9. defined('BX_DOL') or die('hack attempt');
  10. bx_import('BxDol');
  11. define('BX_DOL_STUDIO_MODULE_SYSTEM', 'system');
  12. define('BX_DOL_STUDIO_MODULE_CUSTOM', 'custom');
  13. define('BX_DOL_STUDIO_VISIBLE_ALL', 'all');
  14. define('BX_DOL_STUDIO_VISIBLE_SELECTED', 'selected');
  15. class BxDolStudioUtils extends BxDol {
  16. function __construct() {
  17. parent::BxDol();
  18. }
  19. public static function getSystemName($sValue) {
  20. return str_replace(' ', '_', strtolower($sValue));
  21. }
  22. public static function getClassName($sValue) {
  23. return str_replace(' ', '', ucwords(str_replace('_', ' ', $sValue)));
  24. }
  25. public static function getModuleIcon($sName, $sType = 'menu') {
  26. $aType2Prefix = array('menu' => 'mi', 'page' => 'pi');
  27. $oTemplate = BxDolStudioTemplate::getInstance();
  28. $sDefaultIcon = $oTemplate->getIconUrl($aType2Prefix[$sType] . '-mod-empty.png');
  29. bx_import('BxDolModuleQuery');
  30. $aModule = BxDolModuleQuery::getInstance()->getModuleByName($sName);
  31. if(empty($aModule))
  32. return $sDefaultIcon;
  33. $sModuleIcon = isset($aModule['path']) ? $oTemplate->getIconUrl($aModule['name'] . '@modules/' . $aModule['path'] . '|std-' . $aType2Prefix[$sType] . '.png') : '';
  34. return !empty($sModuleIcon) ? $sModuleIcon : $sDefaultIcon;
  35. }
  36. public static function getModuleTitle($sName) {
  37. $sPrefix = '_adm_txt_module_';
  38. if(in_array($sName, array(BX_DOL_STUDIO_MODULE_SYSTEM, BX_DOL_STUDIO_MODULE_CUSTOM)))
  39. return _t($sPrefix . $sName);
  40. bx_import('BxDolModuleQuery');
  41. $aModule = BxDolModuleQuery::getInstance()->getModuleByName($sName);
  42. if(!empty($aModule))
  43. return $aModule['title'];
  44. return _t($sPrefix . strtolower($sName));
  45. }
  46. public static function getModules($bShowCustom = true, $bShowSystem = true) {
  47. $aResult = array();
  48. if($bShowSystem)
  49. $aResult[BX_DOL_STUDIO_MODULE_SYSTEM] = self::getModuleTitle(BX_DOL_STUDIO_MODULE_SYSTEM);
  50. if($bShowCustom)
  51. $aResult[BX_DOL_STUDIO_MODULE_CUSTOM] = self::getModuleTitle(BX_DOL_STUDIO_MODULE_CUSTOM);
  52. bx_import('BxDolModuleQuery');
  53. $aModules = BxDolModuleQuery::getInstance()->getModulesBy(array('type' => 'modules', 'active' => 1));
  54. foreach($aModules as $aModule)
  55. $aResult[$aModule['name']] = $aModule['title'];
  56. return $aResult;
  57. }
  58. public static function getVisibilityTitle($iValue) {
  59. $iCount = self::getVisibilityCount($iValue);
  60. $sResult = "";
  61. switch($iCount) {
  62. case 1:
  63. bx_import('BxDolAcl');
  64. $aLevel = BxDolAcl::getInstance()->getMembershipInfo(log($iValue, 2) + 1);
  65. $sResult = _t($aLevel['name']);
  66. break;
  67. case 0:
  68. $sResult = _t('_adm_prm_txt_visibility_items_nobody');
  69. break;
  70. case -1:
  71. $sResult = _t('_adm_prm_txt_visibility_items_anyone');
  72. break;
  73. default:
  74. $sResult = _t('_adm_prm_txt_visibility_items_n_user_levels', $iCount);
  75. }
  76. return $sResult;
  77. }
  78. public static function getVisibilityCount($iValue) {
  79. $iResult = 0;
  80. $iValue = (int)$iValue;
  81. if($iValue == 0)
  82. return $iResult;
  83. else if($iValue == BX_DOL_INT_MAX)
  84. return -1;
  85. bx_import('BxDolAcl');
  86. $aLevels = BxDolAcl::getInstance()->getMemberships(false, true);
  87. $iIndex = 1;
  88. do {
  89. if(array_key_exists($iIndex++, $aLevels))
  90. $iResult += $iValue & 1;
  91. } while(($iValue = $iValue >> 1) != 0);
  92. return $iResult;
  93. }
  94. public static function getVisibilityValue($sVisibleFor, $aVisibleForLevels) {
  95. if($sVisibleFor == BX_DOL_STUDIO_VISIBLE_ALL)
  96. return BX_DOL_INT_MAX;
  97. $iVisibleFor = 0;
  98. if(is_array($aVisibleForLevels))
  99. foreach($aVisibleForLevels as $iLevelId)
  100. $iVisibleFor += pow(2, (int)$iLevelId - 1);
  101. return $iVisibleFor;
  102. }
  103. public static function getVisibilityValues($iValue, &$aValuesAll, &$aValuesSelected) {
  104. bx_import('BxDolAcl');
  105. $aLevels = BxDolAcl::getInstance()->getMemberships(false, true);
  106. foreach($aLevels as $iKey => $sValue) {
  107. if(((int)$iValue & pow(2, (int)$iKey - 1)) != 0)
  108. $aValuesSelected[] = $iKey;
  109. $aValuesAll[$iKey] = _t($sValue);
  110. }
  111. }
  112. public static function addInArray($aInput, $sKey, $aValues) {
  113. reset($aInput);
  114. $iInput = count($aInput);
  115. for($i = 0; $i < $iInput; $i++, next($aInput))
  116. if(key($aInput) == $sKey)
  117. break;
  118. $aOutput = array_slice($aInput, 0, $i + 1);
  119. $aOutput = array_merge($aOutput, $aValues);
  120. $aOutput = array_merge($aOutput, array_slice($aInput, $i + 1));
  121. return $aOutput;
  122. }
  123. }
  124. /** @} */