BxDolStudioUtils.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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_MODULE_ICON_DEFAULT', 'lightbulb-o');
  13. define('BX_DOL_STUDIO_VISIBLE_ALL', 'all');
  14. define('BX_DOL_STUDIO_VISIBLE_SELECTED', 'selected');
  15. class BxDolStudioUtils extends BxDol
  16. {
  17. function __construct()
  18. {
  19. parent::__construct();
  20. }
  21. public static function getSystemName($sValue)
  22. {
  23. return str_replace(' ', '_', strtolower($sValue));
  24. }
  25. public static function getClassName($sValue)
  26. {
  27. return bx_gen_method_name($sValue);
  28. }
  29. public static function getIconDefault($sType = '')
  30. {
  31. $sIcon = '';
  32. switch ($sType) {
  33. case BX_DOL_MODULE_TYPE_MODULE:
  34. $sIcon = 'cube';
  35. break;
  36. case BX_DOL_MODULE_TYPE_LANGUAGE:
  37. $sIcon = 'language';
  38. break;
  39. case BX_DOL_MODULE_TYPE_TEMPLATE:
  40. $sIcon = 'adjust';
  41. break;
  42. default:
  43. $sIcon = BX_DOL_STUDIO_MODULE_ICON_DEFAULT;
  44. }
  45. return $sIcon;
  46. }
  47. public static function getModuleIcon($mixedModule, $sType = 'menu', $bReturnAsUrl = true)
  48. {
  49. $aType2Prefix = array('menu' => 'mi', 'page' => 'pi', 'store' => 'si');
  50. if(!is_array($mixedModule)) {
  51. bx_import('BxDolModuleQuery');
  52. $aModule = BxDolModuleQuery::getInstance()->getModuleByName($mixedModule);
  53. }
  54. else
  55. $aModule = $mixedModule;
  56. $sDefaultIcon = self::getIconDefault(isset($aModule['type']) ? $aModule['type'] : '');
  57. if(empty($aModule))
  58. return $sDefaultIcon;
  59. $sModuleIcon = '';
  60. if(isset($aModule['path']))
  61. $sModuleIcon = $aModule['name'] . '@modules/' . $aModule['path'] . '|std-' . $aType2Prefix[$sType] . '.png';
  62. else if(isset($aModule['dir']))
  63. $sModuleIcon = $aModule['name'] . '@modules/' . $aModule['dir'] . '|std-' . $aType2Prefix[$sType] . '.png';
  64. $sModuleIconUrl = BxDolStudioTemplate::getInstance()->getIconUrl($sModuleIcon);
  65. return !empty($sModuleIcon) && !empty($sModuleIconUrl) ? ($bReturnAsUrl ? $sModuleIconUrl : $sModuleIcon) : $sDefaultIcon;
  66. }
  67. public static function getModuleTitle($sName)
  68. {
  69. $sPrefix = '_adm_txt_module_';
  70. if(in_array($sName, array(BX_DOL_STUDIO_MODULE_SYSTEM, BX_DOL_STUDIO_MODULE_CUSTOM)))
  71. return _t($sPrefix . $sName);
  72. bx_import('BxDolModuleQuery');
  73. $aModule = BxDolModuleQuery::getInstance()->getModuleByName($sName);
  74. if(!empty($aModule))
  75. return $aModule['title'];
  76. return _t($sPrefix . strtolower($sName));
  77. }
  78. public static function getModules($bShowCustom = true, $bShowSystem = true)
  79. {
  80. $aResult = array();
  81. if($bShowSystem)
  82. $aResult[BX_DOL_STUDIO_MODULE_SYSTEM] = self::getModuleTitle(BX_DOL_STUDIO_MODULE_SYSTEM);
  83. if($bShowCustom)
  84. $aResult[BX_DOL_STUDIO_MODULE_CUSTOM] = self::getModuleTitle(BX_DOL_STUDIO_MODULE_CUSTOM);
  85. bx_import('BxDolModuleQuery');
  86. $aModules = BxDolModuleQuery::getInstance()->getModulesBy(array('type' => 'modules', 'active' => 1));
  87. foreach($aModules as $aModule)
  88. $aResult[$aModule['name']] = $aModule['title'];
  89. return $aResult;
  90. }
  91. public static function getVisibilityTitle($iValue)
  92. {
  93. $iCount = self::getVisibilityCount($iValue);
  94. $sResult = "";
  95. switch($iCount) {
  96. case 1:
  97. bx_import('BxDolAcl');
  98. $aLevel = BxDolAcl::getInstance()->getMembershipInfo(log($iValue, 2) + 1);
  99. $sResult = _t($aLevel['name']);
  100. break;
  101. case 0:
  102. $sResult = _t('_adm_prm_txt_visibility_items_nobody');
  103. break;
  104. case -1:
  105. $sResult = _t('_adm_prm_txt_visibility_items_anyone');
  106. break;
  107. default:
  108. $sResult = _t('_adm_prm_txt_visibility_items_n_user_levels', $iCount);
  109. }
  110. return $sResult;
  111. }
  112. public static function getVisibilityCount($iValue)
  113. {
  114. $iResult = 0;
  115. $iValue = (int)$iValue;
  116. if($iValue == 0)
  117. return $iResult;
  118. else if($iValue == BX_DOL_INT_MAX)
  119. return -1;
  120. bx_import('BxDolAcl');
  121. $aLevels = BxDolAcl::getInstance()->getMemberships(false, true);
  122. $iIndex = 1;
  123. do {
  124. if(array_key_exists($iIndex++, $aLevels))
  125. $iResult += $iValue & 1;
  126. } while(($iValue = $iValue >> 1) != 0);
  127. return $iResult;
  128. }
  129. public static function getVisibilityValue($sVisibleFor, $aVisibleForLevels)
  130. {
  131. if($sVisibleFor == BX_DOL_STUDIO_VISIBLE_ALL)
  132. return BX_DOL_INT_MAX;
  133. $iVisibleFor = 0;
  134. if(is_array($aVisibleForLevels))
  135. foreach($aVisibleForLevels as $iLevelId)
  136. $iVisibleFor += pow(2, (int)$iLevelId - 1);
  137. return $iVisibleFor;
  138. }
  139. public static function getVisibilityValues($iValue, &$aValuesAll, &$aValuesSelected)
  140. {
  141. bx_import('BxDolAcl');
  142. $aLevels = BxDolAcl::getInstance()->getMemberships(false, true);
  143. foreach($aLevels as $iKey => $sValue) {
  144. if(((int)$iValue & pow(2, (int)$iKey - 1)) != 0)
  145. $aValuesSelected[] = $iKey;
  146. $aValuesAll[$iKey] = _t($sValue);
  147. }
  148. }
  149. public static function addInArray($aInput, $sKey, $aValues)
  150. {
  151. reset($aInput);
  152. $iInput = count($aInput);
  153. for($i = 0; $i < $iInput; $i++, next($aInput))
  154. if(key($aInput) == $sKey)
  155. break;
  156. $aOutput = array_slice($aInput, 0, $i + 1);
  157. $aOutput = array_merge($aOutput, $aValues);
  158. $aOutput = array_merge($aOutput, array_slice($aInput, $i + 1));
  159. return $aOutput;
  160. }
  161. }
  162. /** @} */