BxQuoteofdayModule.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) Vendor
  4. *
  5. * @defgroup Quote of the Day module
  6. * @ingroup VendorModules
  7. *
  8. * @{
  9. */
  10. define('BX_QOD_SOURCE_INTERNAL', 'internal');
  11. define('BX_QOD_SOURCE_RSS', 'rss');
  12. bx_import('BxDolModule');
  13. class BxQuoteofdayModule extends BxBaseModGeneralModule
  14. {
  15. function __construct(&$aModule)
  16. {
  17. parent::__construct($aModule);
  18. }
  19. // ====== SERVICE METHODS
  20. public function serviceGetSources()
  21. {
  22. //RL-TO-IMPROVE GET SOURCES FROM DB AND MULTISELECT
  23. $aResult = array();
  24. $aChoices = array(BX_QOD_SOURCE_INTERNAL, BX_QOD_SOURCE_RSS);
  25. foreach($aChoices as $sChoice)
  26. $aResult[$sChoice]=_t('_bx_quoteofday_source_'. $sChoice);
  27. return $aResult;
  28. }
  29. public function serviceGetMenuAddonManageTools()
  30. {
  31. return $this->_oDb->getHiddenItemsCount();
  32. }
  33. //##### get current Quote #####
  34. /*public function actionGetQuote()
  35. {
  36. echo $this->serviceGetQuote();
  37. }
  38. */
  39. public function serviceGetQuote()
  40. {
  41. $oCachObject = $this->_oDb->getDbCacheObject();
  42. return $oCachObject->getData($this->_oConfig->CNF['CACHEKEY']);
  43. }
  44. //##### Set current Quote from cron #####
  45. /*public function actionSetQuote()
  46. {
  47. echo $this->serviceSetQuote();
  48. }*/
  49. public function serviceSetQuote()
  50. {
  51. $sRssUrl = getParam('bx_quoteofday_rss_url');
  52. $iRssMaxItems = intval(getParam('bx_quoteofday_rss_max_items'));
  53. $aSources=explode(',',getParam('bx_quoteofday_source'));
  54. $aData=array();
  55. //##### Get data from internal set #####
  56. if (in_array(BX_QOD_SOURCE_INTERNAL,$aSources))
  57. {
  58. $aData=array_merge($aData,$this->getInternalData());
  59. }
  60. //##### Get data from rss #####
  61. if (in_array(BX_QOD_SOURCE_RSS,$aSources) && $sRssUrl!="" && $iRssMaxItems>0)
  62. $aData = array_merge($aData,$this->getRssData($sRssUrl,$iRssMaxItems));
  63. $iDayOfYear = date('z');
  64. //$iDayOfYear=rand(1,365);
  65. $iIndex = $iDayOfYear % count($aData);
  66. $this->PutQuoteToCache($aData[$iIndex]);
  67. }
  68. public function PutQuoteToCache($quoteText)
  69. {
  70. $oCachObject = $this->_oDb->getDbCacheObject();
  71. $oCachObject->setData($this->_oConfig->CNF['CACHEKEY'],$quoteText);
  72. }
  73. private function getInternalData()
  74. {
  75. return $this->_oDb->getData();
  76. }
  77. private function getRssData($sRssUrl,$iRssMaxItems)
  78. {
  79. $oTmpRv=array();
  80. $oXmlParser = BxDolXmlParser::getInstance();
  81. $sXmlContent = file_get_contents($sRssUrl);
  82. $oTmp=$oXmlParser->getTags($sXmlContent, 'description');
  83. if (is_array($oTmp))
  84. {
  85. $iC=0;
  86. while (list($key,$value ) = each($oTmp)) {
  87. if ($iC==$iRssMaxItems) break;
  88. if (isset($value['value']) && $value['level']==4 && trim(strip_tags($value['value'])) != "")
  89. {
  90. array_push($oTmpRv,$value['value']);
  91. $iC++;
  92. }
  93. }
  94. }
  95. return $oTmpRv;
  96. }
  97. public function serviceGetQuotesManage()
  98. {
  99. $oCheckAllowedViewValue=$this->checkAcl();
  100. if ($oCheckAllowedViewValue !== CHECK_ACTION_RESULT_ALLOWED)
  101. return MsgBox($oCheckAllowedViewValue);
  102. $this->_oTemplate->addJs('jquery.form.min.js');
  103. $oGrid = BxDolGrid::getObjectInstance($this->_oConfig->CNF['OBJECT_GRID']);
  104. if(!$oGrid)
  105. return '';
  106. return $oGrid->getCode();
  107. }
  108. /**
  109. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden. So make sure to make strict(===) checking.
  110. */
  111. public function checkAcl ($isPerformAction = false)
  112. {
  113. // check ACL
  114. $aCheck = checkActionModule($this->_iProfileId, 'manage entries', $this->getName(), $isPerformAction);
  115. if ($aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  116. return $aCheck[CHECK_ACTION_MESSAGE];
  117. return CHECK_ACTION_RESULT_ALLOWED;
  118. }
  119. }
  120. /** @} */