BxDolStudioUtils.php 5.1 KB

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