BxQuoteOfDayModule.php 7.5 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 QuoteOfTheDay Quote of the Day
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. define('BX_QOD_SOURCE_INTERNAL', 'internal');
  12. define('BX_QOD_SOURCE_RSS', 'rss');
  13. define('BX_QOD_SELECTION_MODE_BY_RANDOM', 'random');
  14. define('BX_QOD_SELECTION_MODE_BY_ORDER', 'order');
  15. define('BX_QOD_LIFETIME_IN_SECONDS', 86400);
  16. class BxQuoteOfDayModule extends BxDolModule
  17. {
  18. function __construct(&$aModule)
  19. {
  20. parent::__construct($aModule);
  21. }
  22. /**
  23. * Service methods
  24. */
  25. /**
  26. * @page service Service Calls
  27. * @section bx_quoteofday Quote of the Day
  28. * @subsection bx_quoteofday-other Other
  29. * @subsubsection bx_quoteofday-get_sources get_sources
  30. *
  31. * @code bx_srv('bx_quoteofday', 'get_sources', [...]); @endcode
  32. *
  33. * Get list of avaliable sources types for studio settings (rss feed or internal list)
  34. *
  35. * @return an array with avaliable sources.
  36. *
  37. * @see BxQuoteOfDayModule::serviceGetSources
  38. */
  39. /**
  40. * @ref bx_quoteofday-get_sources "get_sources"
  41. */
  42. public function serviceGetSources()
  43. {
  44. $aResult = array();
  45. $aChoices = array(BX_QOD_SOURCE_INTERNAL, BX_QOD_SOURCE_RSS);
  46. foreach($aChoices as $sChoice)
  47. $aResult[$sChoice] = _t('_bx_quoteofday_source_' . $sChoice);
  48. return $aResult;
  49. }
  50. /**
  51. * @page service Service Calls
  52. * @section bx_quoteofday Quote of the Day
  53. * @subsection bx_quoteofday-other Other
  54. * @subsubsection bx_quoteofday-get_selection_mode selection_mode
  55. *
  56. * @code bx_srv('bx_quoteofday', 'get_selection_mode', [...]); @endcode
  57. *
  58. * Get list of avaliable selection mode(randome mode or ordered mode) for studio settings
  59. *
  60. * @return an array with selection modes.
  61. *
  62. * @see BxQuoteOfDayModule::serviceGetSelectionMode
  63. */
  64. /**
  65. * @ref bx_quoteofday-get_selection_mode "get_selection_mode"
  66. */
  67. public function serviceGetSelectionMode()
  68. {
  69. $aResult = array();
  70. $aChoices = array(BX_QOD_SELECTION_MODE_BY_RANDOM, BX_QOD_SELECTION_MODE_BY_ORDER);
  71. foreach($aChoices as $sChoice)
  72. $aResult[$sChoice] = _t('_bx_quoteofday_selection_mode_by_' . $sChoice);
  73. return $aResult;
  74. }
  75. /**
  76. * @page service Service Calls
  77. * @section bx_quoteofday Quote of the Day
  78. * @subsection bx_quoteofday-other Other
  79. * @subsubsection bx_quoteofday-get_menu_addon_manage_tools get_menu_addon_manage_tools
  80. *
  81. * @code bx_srv('bx_quoteofday', 'get_menu_addon_manage_tools', [...]); @endcode
  82. *
  83. * Get count of hidden items in internal data for dashbord
  84. *
  85. * @return digit - count of hidden item .
  86. *
  87. * @see BxQuoteOfDayModule::serviceGetMenuAddonManageTools
  88. */
  89. /**
  90. * @ref bx_quoteofday-get_menu_addon_manage_tools "get_menu_addon_manage_tools"
  91. */
  92. public function serviceGetMenuAddonManageTools()
  93. {
  94. return $this->_oDb->getHiddenItemsCount();
  95. }
  96. /**
  97. * @page service Service Calls
  98. * @section bx_quoteofday Quote of the Day
  99. * @subsection bx_quoteofday-other Other
  100. * @subsubsection bx_quoteofday-get_quote
  101. *
  102. * @code bx_srv('bx_quoteofday', 'get_quote', [...]); @endcode
  103. *
  104. * Get current Quote of the Day
  105. *
  106. * @return string - current Quote of the Day from system cache .
  107. *
  108. * @see BxQuoteOfDayModule::serviceGetQuote
  109. */
  110. /**
  111. * @ref bx_quoteofday-get_quote "get_quote"
  112. */
  113. public function serviceGetQuote()
  114. {
  115. $sTextFromCache = $this->getQuoteFromCache();
  116. if ($sTextFromCache == null || $sTextFromCache == "")
  117. {
  118. $sTextFromCache = $this->defineAndSetQuote();
  119. }
  120. return $sTextFromCache;
  121. }
  122. /**
  123. * @page service Service Calls
  124. * @section bx_quoteofday Quote of the Day
  125. * @subsection bx_quoteofday-other Other
  126. * @subsubsection bx_quoteofday-set_quotes_manage
  127. *
  128. * @code bx_srv('bx_quoteofday', 'set_quotes_manage', [...]); @endcode
  129. *
  130. * Return Grid from internal quotes management
  131. *
  132. * @return string - code of grid quotes management.
  133. *
  134. * @see BxQuoteOfDayModule::serviceGetQuotesManage
  135. */
  136. /**
  137. * @ref bx_quoteofday-set_quotes_manage "set_quotes_manage"
  138. */
  139. public function serviceGetQuotesManage()
  140. {
  141. $this->_oTemplate->addJs('jquery.form.min.js');
  142. $oGrid = BxDolGrid::getObjectInstance($this->_oConfig->CNF['OBJECT_GRID']);
  143. if(!$oGrid)
  144. return '';
  145. return $oGrid->getCode();
  146. }
  147. public function defineAndSetQuote()
  148. {
  149. $sRssUrl = getParam('bx_quoteofday_rss_url');
  150. $iRssMaxItems = intval(getParam('bx_quoteofday_rss_max_items'));
  151. $aSources = explode(',', getParam('bx_quoteofday_source'));
  152. $sSelectionType = getParam('bx_quoteofday_selection_mode');
  153. $aData = array();
  154. //##### Get data from internal set #####
  155. if (in_array(BX_QOD_SOURCE_INTERNAL, $aSources)) {
  156. $aData = array_merge($aData, $this->getInternalData());
  157. }
  158. //##### Get data from rss #####
  159. if (in_array(BX_QOD_SOURCE_RSS, $aSources) && $sRssUrl != "" && $iRssMaxItems > 0)
  160. $aData = array_merge($aData, $this->getRssData($sRssUrl, $iRssMaxItems));
  161. $sTextToChache = "";
  162. $iIndex = -1;
  163. if (count($aData) > 0){
  164. if ($sSelectionType == BX_QOD_SELECTION_MODE_BY_RANDOM) {
  165. $iIndex = rand(0, count($aData) - 1);
  166. }
  167. else {
  168. $iDayOfYear = date('z');
  169. $iIndex = $iDayOfYear % count($aData);
  170. }
  171. if ($iIndex > -1) {
  172. $sTextToChache = $aData[$iIndex];
  173. $this->putQuoteToCache($sTextToChache);
  174. }
  175. }
  176. return $sTextToChache;
  177. }
  178. public function putQuoteToCache($quoteText)
  179. {
  180. $oCachObject = $this->_oDb->getDbCacheObject();
  181. $oCachObject->setData($this->_oConfig->CNF['CACHEKEY'], $quoteText, BX_QOD_LIFETIME_IN_SECONDS);
  182. }
  183. public function getQuoteFromCache()
  184. {
  185. $oCachObject = $this->_oDb->getDbCacheObject();
  186. return $oCachObject->getData($this->_oConfig->CNF['CACHEKEY'], BX_QOD_LIFETIME_IN_SECONDS);
  187. }
  188. public function removeQuoteFromCache()
  189. {
  190. $oCachObject = $this->_oDb->getDbCacheObject();
  191. $oCachObject->delData($this->_oConfig->CNF['CACHEKEY']);
  192. }
  193. private function getInternalData()
  194. {
  195. return $this->_oDb->getData();
  196. }
  197. private function getRssData($sRssUrl, $iRssMaxItems)
  198. {
  199. $aTmpRv = array();
  200. $oXmlParser = BxDolXmlParser::getInstance();
  201. $sXmlContent = bx_file_get_contents($sRssUrl);
  202. $aTmp = $oXmlParser->getTags($sXmlContent, 'description');
  203. if (is_array($aTmp)) {
  204. $iC = 0;
  205. foreach ($aTmp as $aValue) {
  206. if ($iC == $iRssMaxItems) break;
  207. if (isset($aValue['value']) && $aValue['level'] == 4 && trim(strip_tags($aValue['value'])) != "") {
  208. array_push($aTmpRv, $aValue['value']);
  209. $iC++;
  210. }
  211. }
  212. }
  213. return $aTmpRv;
  214. }
  215. }
  216. /** @} */