BxDolStudioUtils.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 getWidgetIcon($mixedWidget)
  47. {
  48. if(!is_array($mixedWidget))
  49. $mixedWidget = BxDolStudioWidgetsQuery::getInstance()->getWidgets(array('type' => 'by_id', 'value' => (int)$mixedWidget));
  50. $oTemplate = BxDolStudioTemplate::getInstance();
  51. $sUrl = $oTemplate->getModuleIconUrl($mixedWidget['module'] . '.svg');
  52. if(empty($sUrl))
  53. $sUrl = $oTemplate->getIconUrl($mixedWidget['icon']);
  54. if(empty($sUrl) && ($aModule = BxDolModuleQuery::getInstance()->getModuleByName($mixedWidget['module'])) && is_array($aModule))
  55. $sUrl = BxDolStudioUtils::getIconDefault($aModule['type']);
  56. return $sUrl;
  57. }
  58. public static function getModuleIcon($mixedModule, $sType = 'menu', $bReturnAsUrl = true)
  59. {
  60. $aType2Prefix = array('menu' => 'mi', 'page' => 'pi', 'store' => 'si');
  61. if(!is_array($mixedModule))
  62. $aModule = BxDolModuleQuery::getInstance()->getModuleByName($mixedModule);
  63. else
  64. $aModule = $mixedModule;
  65. $sDefaultIcon = self::getIconDefault(isset($aModule['type']) ? $aModule['type'] : '');
  66. if(empty($aModule))
  67. return $sDefaultIcon;
  68. $sModuleIcon = '';
  69. if(isset($aModule['path']))
  70. $sModuleIcon = $aModule['name'] . '@modules/' . $aModule['path'] . '|std-' . $aType2Prefix[$sType] . '.png';
  71. else if(isset($aModule['dir']))
  72. $sModuleIcon = $aModule['name'] . '@modules/' . $aModule['dir'] . '|std-' . $aType2Prefix[$sType] . '.png';
  73. $sModuleIconUrl = BxDolStudioTemplate::getInstance()->getIconUrl($sModuleIcon);
  74. if(empty($sModuleIconUrl)) {
  75. if(isset($aModule['path']))
  76. $sModuleIcon = $aModule['name'] . '@modules/' . $aModule['path'] . '|std-icon.svg';
  77. else if(isset($aModule['dir']))
  78. $sModuleIcon = $aModule['name'] . '@modules/' . $aModule['dir'] . '|std-icon.svg';
  79. $sModuleIconUrl = BxDolStudioTemplate::getInstance()->getIconUrl($sModuleIcon);
  80. }
  81. return !empty($sModuleIcon) && !empty($sModuleIconUrl) ? ($bReturnAsUrl ? $sModuleIconUrl : $sModuleIcon) : $sDefaultIcon;
  82. }
  83. public static function getModuleImage($mixedModule, $sName, $bReturnAsUrl = true)
  84. {
  85. $aModule = is_array($mixedModule) ? $mixedModule : BxDolModuleQuery::getInstance()->getModuleByName($mixedModule);
  86. if(empty($aModule))
  87. return '';
  88. $sModuleImage = '';
  89. if(isset($aModule['path']))
  90. $sModuleImage = $aModule['name'] . '@modules/' . $aModule['path'] . '|' . $sName;
  91. else if(isset($aModule['dir']))
  92. $sModuleImage = $aModule['name'] . '@modules/' . $aModule['dir'] . '|' . $sName;
  93. $sModuleImageUrl = BxDolStudioTemplate::getInstance()->getImageUrl($sModuleImage);
  94. return !empty($sModuleImage) && !empty($sModuleImageUrl) ? ($bReturnAsUrl ? $sModuleImageUrl : $sModuleImage) : '';
  95. }
  96. public static function getModuleTitle($sName)
  97. {
  98. $sTitle = '_adm_txt_module_' . strtolower($sName);
  99. if(in_array($sName, array(BX_DOL_STUDIO_MODULE_SYSTEM, BX_DOL_STUDIO_MODULE_CUSTOM)))
  100. return _t($sTitle);
  101. $aModule = BxDolModuleQuery::getInstance()->getModuleByName($sName);
  102. if(empty($aModule) || !is_array($aModule))
  103. return _t($sTitle);
  104. $sTitle = '_' . $aModule['name'];
  105. $$sTitle = _t($sTitle);
  106. if(strcmp($sTitle, $$sTitle) !== 0)
  107. return $$sTitle;
  108. return $aModule['title'];
  109. }
  110. public static function getModules($bShowCustom = true, $bShowSystem = true)
  111. {
  112. $aResult = array();
  113. if($bShowSystem)
  114. $aResult[BX_DOL_STUDIO_MODULE_SYSTEM] = self::getModuleTitle(BX_DOL_STUDIO_MODULE_SYSTEM);
  115. if($bShowCustom)
  116. $aResult[BX_DOL_STUDIO_MODULE_CUSTOM] = self::getModuleTitle(BX_DOL_STUDIO_MODULE_CUSTOM);
  117. $aModules = BxDolModuleQuery::getInstance()->getModulesBy(array('type' => 'type', 'value' => array(BX_DOL_MODULE_TYPE_MODULE, BX_DOL_MODULE_TYPE_TEMPLATE), 'active' => 1));
  118. foreach($aModules as $aModule)
  119. $aResult[$aModule['name']] = $aModule['title'];
  120. return $aResult;
  121. }
  122. public static function getVisibilityTitle($iValue)
  123. {
  124. $iCount = self::getVisibilityCount($iValue);
  125. $sResult = "";
  126. switch($iCount) {
  127. case 1:
  128. $aLevel = BxDolAcl::getInstance()->getMembershipInfo(log($iValue, 2) + 1);
  129. $sResult = _t($aLevel['name']);
  130. break;
  131. case 0:
  132. $sResult = _t('_adm_prm_txt_visibility_items_nobody');
  133. break;
  134. case -1:
  135. $sResult = _t('_adm_prm_txt_visibility_items_anyone');
  136. break;
  137. default:
  138. $sResult = _t('_adm_prm_txt_visibility_items_n_user_levels', $iCount);
  139. }
  140. return $sResult;
  141. }
  142. public static function getVisibilityCount(&$iValue)
  143. {
  144. $iValue = (int)$iValue;
  145. $iResult = 0;
  146. $iCheck = $iValue;
  147. if($iCheck == 0)
  148. return $iResult;
  149. else if($iCheck == BX_DOL_INT_MAX)
  150. return -1;
  151. $aLevels = BxDolAcl::getInstance()->getMemberships(false, true);
  152. $iIndex = 1;
  153. do {
  154. if($iCheck & 1) {
  155. if(array_key_exists($iIndex, $aLevels))
  156. $iResult += 1;
  157. else
  158. $iValue = $iValue ^ (1 << ($iIndex - 1));
  159. }
  160. $iIndex += 1;
  161. } while(($iCheck = $iCheck >> 1) != 0);
  162. return $iResult;
  163. }
  164. public static function getVisibilityValue($sVisibleFor, $aVisibleForLevels)
  165. {
  166. if($sVisibleFor == BX_DOL_STUDIO_VISIBLE_ALL)
  167. return BX_DOL_INT_MAX;
  168. $iVisibleFor = 0;
  169. if(is_array($aVisibleForLevels))
  170. foreach($aVisibleForLevels as $iLevelId)
  171. $iVisibleFor += pow(2, (int)$iLevelId - 1);
  172. return $iVisibleFor;
  173. }
  174. public static function getVisibilityValues($iValue, &$aValuesAll, &$aValuesSelected)
  175. {
  176. if(!is_array($aValuesAll))
  177. $aValuesAll = array();
  178. if(!is_array($aValuesSelected))
  179. $aValuesSelected = array();
  180. $aLevels = BxDolAcl::getInstance()->getMemberships(false, true);
  181. foreach($aLevels as $iKey => $sValue) {
  182. if(((int)$iValue & pow(2, (int)$iKey - 1)) != 0)
  183. $aValuesSelected[] = $iKey;
  184. $aValuesAll[$iKey] = _t($sValue);
  185. }
  186. }
  187. public static function addInArray($aInput, $sKey, $aValues, $bAddAfter = true)
  188. {
  189. reset($aInput);
  190. $iInput = count($aInput);
  191. for($i = 0; $i < $iInput; $i++, next($aInput))
  192. if(key($aInput) == $sKey)
  193. break;
  194. if($bAddAfter)
  195. $i += 1;
  196. $aOutput = array_slice($aInput, 0, $i);
  197. $aOutput = array_merge($aOutput, $aValues);
  198. $aOutput = array_merge($aOutput, array_slice($aInput, $i));
  199. return $aOutput;
  200. }
  201. }
  202. /** @} */