BxDolSocialSharing.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup TridentCore Trident Core
  7. * @{
  8. */
  9. /**
  10. * @page objects
  11. * @section social_sharing Social Sharing Buttons
  12. * @ref BxDolSocialSharing
  13. */
  14. /**
  15. * Social sharing buttons for any content
  16. *
  17. * It displays sharing buttons from popular social networks, like facebook, twitter, gogole plus, etc.
  18. *
  19. *
  20. * @section example Example of usage:
  21. *
  22. * @code
  23. * echo BxTemplSocialSharing::getInstance()->getCode($iId, $sModuleName, $sUrl, $sTitle);
  24. * @endcode
  25. *
  26. *
  27. * @section alerts Alerts:
  28. *
  29. * Type/unit: system
  30. * Action: social_sharing_display
  31. * Options:
  32. * buttons - reference to buttons array
  33. * markers - reference to variables for replacement
  34. * override_output - override output string
  35. *
  36. */
  37. class BxDolSocialSharing extends BxDol implements iBxDolSingleton
  38. {
  39. protected $_oQuery;
  40. protected $_aSocialButtons = array (); // active social buttons array
  41. /**
  42. * Constructor
  43. */
  44. protected function __construct()
  45. {
  46. parent::__construct();
  47. $this->_oQuery = new BxDolSocialSharingQuery();
  48. $this->_aSocialButtons = $this->_oQuery->getActiveButtons();
  49. }
  50. /**
  51. * Get object instance
  52. * @param $sObject object name
  53. * @return object instance
  54. */
  55. static public function getInstance()
  56. {
  57. if (isset($GLOBALS['bxDolClasses']['BxDolSocialSharing']))
  58. return $GLOBALS['bxDolClasses']['BxDolSocialSharing'];
  59. $o = new BxTemplSocialSharing();
  60. return ($GLOBALS['bxDolClasses']['BxDolSocialSharing'] = $o);
  61. }
  62. public function getCode ($sContentId, $sModuleName, $sUrl, $sTitle, $aCustomVars = false)
  63. {
  64. // overrided in template class
  65. }
  66. /**
  67. * Replace provided markers in string.
  68. * @param $s - string to replace markers in
  69. * @param $a - markers array
  70. * @return string with replaces markers
  71. */
  72. protected function _replaceMarkers ($mixed, $aMarkers)
  73. {
  74. if (empty($mixed) || empty($aMarkers) || !is_array($aMarkers))
  75. return $mixed;
  76. return bx_replace_markers($mixed, $aMarkers);
  77. }
  78. /**
  79. * Get most facebook locale for provided language code.
  80. * @param $sLang lang code
  81. * @return locale string or empty string if no lacale is found
  82. */
  83. protected function _getLocaleFacebook ($sLang)
  84. {
  85. $aLocales = $this->_getLocalesFacebook();
  86. if (!$aLocales || !isset($aLocales[$sLang]))
  87. return '';
  88. return $aLocales[$sLang];
  89. }
  90. /**
  91. * Get facebook locales
  92. * @return locales array, lang is array key and locale is array value
  93. */
  94. protected function _getLocalesFacebook ()
  95. {
  96. $oCache = $this->_oQuery->getDbCacheObject();
  97. $sCacheKey = $this->_oQuery->genDbCacheKey('sys_social_sharing_locales_fb');
  98. $aData = $oCache->getData($sCacheKey);
  99. if (null === $aData) {
  100. $sXML = bx_file_get_contents ('http://www.facebook.com/translations/FacebookLocales.xml');
  101. if (!$sXML)
  102. return false;
  103. $xmlLocates = new SimpleXMLElement($sXML);
  104. $aData = array ();
  105. foreach ($xmlLocates->locale as $xmlLocale) {
  106. $sLocale = (string)($xmlLocale->codes->code->standard->representation);
  107. list ($sLang,) = explode('_', $sLocale);
  108. if (!isset($aData[$sLang]))
  109. $aData[$sLang] = $sLocale;
  110. }
  111. $oCache->setData ($sCacheKey, $aData);
  112. }
  113. return $aData;
  114. }
  115. }
  116. /** @} */