BxDolStudioUtils.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaStudio UNA 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', 'far lightbulb');
  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. $sTitle = '_adm_txt_module_' . strtolower($sName);
  87. if(in_array($sName, array(BX_DOL_STUDIO_MODULE_SYSTEM, BX_DOL_STUDIO_MODULE_CUSTOM)))
  88. return _t($sTitle);
  89. $aModule = BxDolModuleQuery::getInstance()->getModuleByName($sName);
  90. if(empty($aModule) || !is_array($aModule))
  91. return _t($sTitle);
  92. $sTitle = '_' . $aModule['name'];
  93. $$sTitle = _t($sTitle);
  94. if(strcmp($sTitle, $$sTitle) !== 0)
  95. return $$sTitle;
  96. return $aModule['title'];
  97. }
  98. public static function getModules($bShowCustom = true, $bShowSystem = true)
  99. {
  100. $aResult = array();
  101. if($bShowSystem)
  102. $aResult[BX_DOL_STUDIO_MODULE_SYSTEM] = self::getModuleTitle(BX_DOL_STUDIO_MODULE_SYSTEM);
  103. if($bShowCustom)
  104. $aResult[BX_DOL_STUDIO_MODULE_CUSTOM] = self::getModuleTitle(BX_DOL_STUDIO_MODULE_CUSTOM);
  105. $aModules = BxDolModuleQuery::getInstance()->getModulesBy(array('type' => 'type', 'value' => array(BX_DOL_MODULE_TYPE_MODULE, BX_DOL_MODULE_TYPE_TEMPLATE), 'active' => 1));
  106. foreach($aModules as $aModule)
  107. $aResult[$aModule['name']] = $aModule['title'];
  108. return $aResult;
  109. }
  110. public static function getVisibilityTitle($iValue)
  111. {
  112. $iCount = self::getVisibilityCount($iValue);
  113. $sResult = "";
  114. switch($iCount) {
  115. case 1:
  116. $aLevel = BxDolAcl::getInstance()->getMembershipInfo(log($iValue, 2) + 1);
  117. $sResult = _t($aLevel['name']);
  118. break;
  119. case 0:
  120. $sResult = _t('_adm_prm_txt_visibility_items_nobody');
  121. break;
  122. case -1:
  123. $sResult = _t('_adm_prm_txt_visibility_items_anyone');
  124. break;
  125. default:
  126. $sResult = _t('_adm_prm_txt_visibility_items_n_user_levels', $iCount);
  127. }
  128. return $sResult;
  129. }
  130. public static function getVisibilityCount(&$iValue)
  131. {
  132. $iValue = (int)$iValue;
  133. $iResult = 0;
  134. $iCheck = $iValue;
  135. if($iCheck == 0)
  136. return $iResult;
  137. else if($iCheck == BX_DOL_INT_MAX)
  138. return -1;
  139. $aLevels = BxDolAcl::getInstance()->getMemberships(false, true);
  140. $iIndex = 1;
  141. do {
  142. if($iCheck & 1) {
  143. if(array_key_exists($iIndex, $aLevels))
  144. $iResult += 1;
  145. else
  146. $iValue = $iValue ^ (1 << ($iIndex - 1));
  147. }
  148. $iIndex += 1;
  149. } while(($iCheck = $iCheck >> 1) != 0);
  150. return $iResult;
  151. }
  152. public static function getVisibilityValue($sVisibleFor, $aVisibleForLevels)
  153. {
  154. if($sVisibleFor == BX_DOL_STUDIO_VISIBLE_ALL)
  155. return BX_DOL_INT_MAX;
  156. $iVisibleFor = 0;
  157. if(is_array($aVisibleForLevels))
  158. foreach($aVisibleForLevels as $iLevelId)
  159. $iVisibleFor += pow(2, (int)$iLevelId - 1);
  160. return $iVisibleFor;
  161. }
  162. public static function getVisibilityValues($iValue, &$aValuesAll, &$aValuesSelected)
  163. {
  164. if(!is_array($aValuesAll))
  165. $aValuesAll = array();
  166. if(!is_array($aValuesSelected))
  167. $aValuesSelected = array();
  168. $aLevels = BxDolAcl::getInstance()->getMemberships(false, true);
  169. foreach($aLevels as $iKey => $sValue) {
  170. if(((int)$iValue & pow(2, (int)$iKey - 1)) != 0)
  171. $aValuesSelected[] = $iKey;
  172. $aValuesAll[$iKey] = _t($sValue);
  173. }
  174. }
  175. public static function addInArray($aInput, $sKey, $aValues, $bAddAfter = true)
  176. {
  177. reset($aInput);
  178. $iInput = count($aInput);
  179. for($i = 0; $i < $iInput; $i++, next($aInput))
  180. if(key($aInput) == $sKey)
  181. break;
  182. if($bAddAfter)
  183. $i += 1;
  184. $aOutput = array_slice($aInput, 0, $i);
  185. $aOutput = array_merge($aOutput, $aValues);
  186. $aOutput = array_merge($aOutput, array_slice($aInput, $i));
  187. return $aOutput;
  188. }
  189. }
  190. /** @} */