BxBaseMenuSocialSharing.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 UnaBaseView UNA Base Representation Classes
  7. * @{
  8. */
  9. /**
  10. * Menu for content social sharing
  11. */
  12. class BxBaseMenuSocialSharing extends BxTemplMenu
  13. {
  14. protected $_aNeedEncode = array(
  15. 'url' => 'url_encoded',
  16. 'title' => 'title_encoded'
  17. );
  18. public function __construct ($aObject, $oTemplate)
  19. {
  20. parent::__construct ($aObject, $oTemplate);
  21. $sLang = BxDolLanguages::getInstance()->getCurrentLanguage();
  22. $this->addMarkers(array (
  23. 'lang' => $sLang,
  24. 'locale' => $this->_getLocaleFacebook($sLang),
  25. ));
  26. }
  27. public function addMarkers($a)
  28. {
  29. if(!empty($this->_aNeedEncode) && is_array($this->_aNeedEncode))
  30. foreach($this->_aNeedEncode as $sKey1 => $sKey2)
  31. if(array_key_exists($sKey1, $a))
  32. $a[$sKey2] = rawurlencode($a[$sKey1]);
  33. return parent::addMarkers($a);
  34. }
  35. public function getCode()
  36. {
  37. if (!isset($this->_aObject['menu_items']))
  38. $this->_aObject['menu_items'] = $this->getMenuItemsRaw();
  39. // alert
  40. $sOverrideOutput = null;
  41. $oAlert = new BxDolAlerts('system', 'social_sharing_display', '', '', array (
  42. 'markers' => &$this->_aMarkers,
  43. 'buttons' => &$this->_aObject['menu_items'],
  44. 'override_output' => &$sOverrideOutput,
  45. ));
  46. $oAlert->alert();
  47. // return custom code if there is one
  48. if ($sOverrideOutput)
  49. return $sOverrideOutput;
  50. //return empty string of there is no buttons
  51. if (empty($this->_aObject['menu_items']))
  52. return '';
  53. return parent::getCode();
  54. }
  55. /**
  56. * Get most facebook locale for provided language code.
  57. * @param $sLang lang code
  58. * @return locale string or empty string if no lacale is found
  59. */
  60. protected function _getLocaleFacebook ($sLang)
  61. {
  62. $aLocales = $this->_getLocalesFacebook();
  63. if (!$aLocales || !isset($aLocales[$sLang]))
  64. return '';
  65. return $aLocales[$sLang];
  66. }
  67. /**
  68. * Get facebook locales
  69. * @return locales array, lang is array key and locale is array value
  70. */
  71. protected function _getLocalesFacebook ()
  72. {
  73. $oCache = $this->_oQuery->getDbCacheObject();
  74. $sCacheKey = $this->_oQuery->genDbCacheKey('sys_social_sharing_locales_fb');
  75. $aData = $oCache->getData($sCacheKey);
  76. if (null === $aData) {
  77. $sXML = bx_file_get_contents ('http://www.facebook.com/translations/FacebookLocales.xml');
  78. if (!$sXML)
  79. return false;
  80. $xmlLocates = new SimpleXMLElement($sXML);
  81. $aData = array ();
  82. foreach ($xmlLocates->locale as $xmlLocale) {
  83. $sLocale = (string)($xmlLocale->codes->code->standard->representation);
  84. list ($sLang,) = explode('_', $sLocale);
  85. if (!isset($aData[$sLang]))
  86. $aData[$sLang] = $sLocale;
  87. }
  88. $oCache->setData ($sCacheKey, $aData);
  89. }
  90. return $aData;
  91. }
  92. }
  93. /** @} */