BxDolStudioUtils.php 6.9 KB

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