BxDolCover.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. * Cover.
  11. *
  12. * It displays cover area for different pages, usually it have background image, text and some buttons on it.
  13. *
  14. * @section example Example of usage
  15. *
  16. * Setting cover:
  17. *
  18. * @code
  19. * $oCover = BxDolCover::getInstance(); // get object instance
  20. * if ($oCover) // check if Cover is available for using
  21. * $oCover->set (array( // set cover with default template
  22. * 'title' => 'Hello World!',
  23. * ));
  24. * @endcode
  25. *
  26. */
  27. class BxDolCover extends BxDolFactory implements iBxDolSingleton
  28. {
  29. protected $_aOptions = array();
  30. protected $_sTemplateName = 'cover.html';
  31. protected $_sTemplateNameEmpty = 'cover_empty.html';
  32. protected $_sCoverImageUrl = false;
  33. protected $_aOptiondDefault = array();
  34. protected function __construct()
  35. {
  36. if (isset($GLOBALS['bxDolClasses']['BxDolCover']))
  37. trigger_error ('Multiple instances are not allowed for the BxDolCover class.', E_USER_ERROR);
  38. parent::__construct();
  39. $this->_aOptiondDefault = array(
  40. 'class' => '',
  41. 'title' => '',
  42. 'actions' => '',
  43. 'bx_if:image' => array (
  44. 'condition' => false,
  45. 'content' => array(),
  46. ),
  47. 'bx_if:icon' => array (
  48. 'condition' => false,
  49. 'content' => array(),
  50. ),
  51. 'bx_if:bg' => array (
  52. 'condition' => false,
  53. 'content' => array(),
  54. ),
  55. 'bx_if:logged_in' => array(
  56. 'condition' => isLogged(),
  57. 'content' => array(),
  58. ),
  59. );
  60. }
  61. /**
  62. * Prevent cloning the instance
  63. */
  64. public function __clone()
  65. {
  66. if (isset($GLOBALS['bxDolClasses']['BxDolCover']))
  67. trigger_error('Clone is not allowed for the BxDolCover class.', E_USER_ERROR);
  68. }
  69. /**
  70. * Get Cover object instance
  71. * @return object instance or false on error
  72. */
  73. public static function getInstance($oTemplate = false)
  74. {
  75. if (isset($GLOBALS['bxDolClasses']['BxDolCover']))
  76. return $GLOBALS['bxDolClasses']['BxDolCover'];
  77. $o = new BxTemplCover($oTemplate);
  78. return ($GLOBALS['bxDolClasses']['BxDolCover'] = $o);
  79. }
  80. /**
  81. * Get cover image URL by descriptive array.
  82. * @param mixed $mixedImage - image descriptive array.
  83. * @return string with image URL or false on error.
  84. */
  85. public static function getCoverImageUrl($mixedImage)
  86. {
  87. $bObject = !empty($mixedImage['object']);
  88. $bTranscoder = !empty($mixedImage['transcoder']);
  89. if(!is_array($mixedImage) || !isset($mixedImage['id']) || (!$bObject && !$bTranscoder))
  90. return false;
  91. if($bObject)
  92. $o = BxDolStorage::getObjectInstance($mixedImage['object']);
  93. else if($bTranscoder)
  94. $o = BxDolTranscoder::getObjectInstance($mixedImage['transcoder']);
  95. if(!$o)
  96. return false;
  97. return $o->getFileUrlById($mixedImage['id']);
  98. }
  99. /**
  100. * Determine whether cover is already set for the page or not.
  101. */
  102. public function isCover()
  103. {
  104. return !empty($this->_sCoverImageUrl) || !empty($this->_aOptions);
  105. }
  106. /**
  107. * Set custom cover options
  108. * @param $aOptions - cover options, default template supports the following: title. For more info @see template/cover.html
  109. * @param $sTemplateName - optional template name
  110. */
  111. public function set ($aOptions, $sTemplateName = false)
  112. {
  113. $this->_aOptions = $aOptions;
  114. if (false !== $sTemplateName)
  115. $this->_sTemplateName = $sTemplateName;
  116. bx_alert('system', 'set_cover', 0, false, array('options' => &$this->_aOptions, 'template_name' => &$this->_sTemplateName, 'cover' => $this));
  117. }
  118. /**
  119. * Set cover image url for default cover template
  120. * @param $mixedCoverImageUrl - cover image url, or array with file id and storage or transcoder object:
  121. * @code
  122. * array('id' => 12, 'object' => 'sample');
  123. * // or
  124. * array('id' => 21, 'transcoder' => 'sample');
  125. * @endcode
  126. */
  127. public function setCoverImageUrl ($mixedCoverImage)
  128. {
  129. if (is_string($mixedCoverImage))
  130. $this->_sCoverImageUrl = $mixedCoverImage;
  131. else
  132. $this->_sCoverImageUrl = self::getCoverImageUrl($mixedCoverImage);
  133. if(!$this->_sCoverImageUrl)
  134. return false;
  135. bx_alert('system', 'set_cover_image', 0, false, array('cover_image' => &$this->_sCoverImageUrl, 'cover' => $this));
  136. return true;
  137. }
  138. /**
  139. * Set cover area class
  140. */
  141. public function setCoverClass ($sClass)
  142. {
  143. $this->_aOptiondDefault['class'] = $sClass;
  144. }
  145. }
  146. /** @} */