BxDolSounds.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 UnaCore UNA Core
  7. * @{
  8. */
  9. /**
  10. * Interface sounds settings (Studio > Settings > Global > Sounds) for Cuelume's cues: where they play, the sound set
  11. * and volume, the level people start at, and a set (or silence) for each kind of action.
  12. *
  13. * The site and Studio read it in BxBaseFunctions::getInjHeadSounds; apps get the same config with every page
  14. * (BxBasePage::getPageAPI), so playing a sound never waits on a request.
  15. */
  16. class BxDolSounds extends BxDolFactory implements iBxDolSingleton
  17. {
  18. public const PLATFORMS = ['site', 'studio', 'neo_web', 'ios', 'android'];
  19. /** What each person can pick for themselves; the option sets where they start. */
  20. public const LEVELS = ['full', 'quiet', 'off'];
  21. /** Cuelume's themes: the same cues in another material. */
  22. public const SETS = ['default', 'mech', 'bubble', 'press'];
  23. public const VOLUMES = [100, 75, 50, 25];
  24. /** Kinds of action that can have their own set, or no sound. */
  25. public const GROUPS = ['buttons', 'choices', 'reactions', 'menus', 'navigation', 'results', 'notifications'];
  26. public const GROUP_OFF = 'off';
  27. protected function __construct()
  28. {
  29. parent::__construct();
  30. }
  31. /**
  32. * Prevent cloning the instance
  33. */
  34. public function __clone()
  35. {
  36. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  37. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  38. }
  39. /**
  40. * Get singleton instance of the class
  41. */
  42. public static function getInstance()
  43. {
  44. if(!isset($GLOBALS['bxDolClasses'][__CLASS__]))
  45. $GLOBALS['bxDolClasses'][__CLASS__] = new BxDolSounds();
  46. return $GLOBALS['bxDolClasses'][__CLASS__];
  47. }
  48. public function isOn($sPlatform)
  49. {
  50. return in_array($sPlatform, $this->getPlatforms(), true);
  51. }
  52. public function getPlatforms()
  53. {
  54. return array_values(array_intersect(self::PLATFORMS, explode(',', (string)getParam('sys_sounds_platforms'))));
  55. }
  56. /**
  57. * The config every client applies: `level` is where people start until they pick their own, `volume` is 0-1,
  58. * `groups` lists only the kinds of action that differ from `set` (a set name or 'off').
  59. */
  60. public function getConfig()
  61. {
  62. $sLevel = getParam('sys_sounds_level');
  63. $sSet = getParam('sys_sounds_set');
  64. $iVolume = (int)getParam('sys_sounds_volume');
  65. $aGroups = [];
  66. foreach(self::GROUPS as $sGroup) {
  67. $sGroupSet = getParam('sys_sounds_' . $sGroup);
  68. if($sGroupSet == self::GROUP_OFF || (in_array($sGroupSet, self::SETS, true) && $sGroupSet != $sSet))
  69. $aGroups[$sGroup] = $sGroupSet;
  70. }
  71. return [
  72. 'platforms' => $this->getPlatforms(),
  73. 'level' => in_array($sLevel, self::LEVELS, true) ? $sLevel : self::LEVELS[0],
  74. 'set' => in_array($sSet, self::SETS, true) ? $sSet : self::SETS[0],
  75. 'volume' => (in_array($iVolume, self::VOLUMES, true) ? $iVolume : self::VOLUMES[0]) / 100,
  76. 'groups' => (object)$aGroups,
  77. ];
  78. }
  79. }
  80. /** @} */