Преглед изворни кода

#30 - services support in social buttons

Alex Trofimov пре 13 година
родитељ
комит
e6a3c07179

+ 11 - 5
inc/classes/BxDolSocialSharing.php

@@ -79,14 +79,20 @@ class BxDolSocialSharing extends BxDol implements iBxDolSingleton
      * @param $a - markers array
      * @return string with replaces markers
      */ 
-    protected function _replaceMarkers ($s, $a) {
-        if (empty($s) || empty($a) || !is_array($a))
+    protected function _replaceMarkers ($mixed, $aMarkers) {
+        if (empty($mixed) || empty($aMarkers) || !is_array($aMarkers))
             return $s;
 
-        foreach ($a as $sKey => $sValue)
-            $s = str_replace('{' . $sKey . '}', $sValue, $s);
 
-        return $s;
+        if (is_array($mixed)) {
+            foreach ($mixed as $sKey => $sValue)
+                $mixed[$sKey] = $this->_replaceMarkers ($sValue, $aMarkers);
+        } else {
+            foreach ($aMarkers as $sKey => $sValue)
+                $mixed = str_replace('{' . $sKey . '}', $sValue, $mixed);
+        }
+        
+        return $mixed;
     }
 
     /**

+ 8 - 5
install/sql/v70.sql

@@ -53,22 +53,25 @@ INSERT INTO `sys_objects_editor` (`object`, `title`, `skin`, `override_class_nam
 ('sys_tinymce', 'TinyMCE', 'default', 'BxTemplEditorTinyMCE', ''),
 ('sys_cleditor', 'CLEditor', '', 'BxTemplEditorCLEditor', '');
 
+
 -- --------------------------------------------------------
 
+
 CREATE TABLE IF NOT EXISTS `sys_objects_social_sharing` (
   `id` int(11) NOT NULL AUTO_INCREMENT,
   `object` varchar(32) NOT NULL,
+  `type` enum('html','service') NOT NULL,
   `content` text NOT NULL,
   `order` int(11) NOT NULL,
   `active` tinyint(4) NOT NULL,
   PRIMARY KEY (`id`)
 ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
 
-INSERT INTO `sys_objects_social_sharing` (`object`, `content`, `order`, `active`) VALUES
-('facebook', '<iframe src="//www.facebook.com/plugins/like.php?href={url_encoded}&amp;send=false&amp;layout=button_count&amp;width=450&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font&amp;height=21&amp;locale={locale}" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:100%; height:21px;" allowTransparency="true"></iframe>', 1, 1),
-('googleplus', '<div style="height:21px;">\r\n<div class="g-plusone" data-size="medium" data-href="{url}"></div>\r\n<script type="text/javascript">\r\n  window.___gcfg = {lang: ''{lang}''};\r\n  (function() {\r\n    var po = document.createElement(''script''); po.type = ''text/javascript''; po.async = true;\r\n    po.src = ''https://apis.google.com/js/plusone.js'';\r\n    var s = document.getElementsByTagName(''script'')[0]; s.parentNode.insertBefore(po, s);\r\n  })();\r\n</script>\r\n</div>', 2, 1),
-('twitter', '<iframe allowtransparency="true" frameborder="0" scrolling="no" src="//platform.twitter.com/widgets/tweet_button.html?url={url_encoded}&amp;text={title_encoded}&amp;size=medium&amp;count=horizontal&amp;lang={lang}" style="width:100%;height:21px;"></iframe>', 3, 1),
-('pinterest', '<a href="http://pinterest.com/pin/create/button/?url={url_encoded}&media={img_url_encoded}&description={title_encoded}" class="pin-it-button" count-layout="horizontal"><img border="0" src="//assets.pinterest.com/images/PinExt.png" title="Pin It" /></a>\r\n\r\n<script type="text/javascript" src="//assets.pinterest.com/js/pinit.js"></script>', 4, 1);
+INSERT INTO `sys_objects_social_sharing` (`object`, `type`, `content`, `order`, `active`) VALUES
+('facebook', 'html', '<iframe src="//www.facebook.com/plugins/like.php?href={url_encoded}&amp;send=false&amp;layout=button_count&amp;width=450&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font&amp;height=21&amp;locale={locale}" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:100%; height:21px;" allowTransparency="true"></iframe>', 1, 1),
+('googleplus', 'html', '<div style="height:21px;">\r\n<div class="g-plusone" data-size="medium" data-href="{url}"></div>\r\n<script type="text/javascript">\r\n  window.___gcfg = {lang: ''{lang}''};\r\n  (function() {\r\n    var po = document.createElement(''script''); po.type = ''text/javascript''; po.async = true;\r\n    po.src = ''https://apis.google.com/js/plusone.js'';\r\n    var s = document.getElementsByTagName(''script'')[0]; s.parentNode.insertBefore(po, s);\r\n  })();\r\n</script>\r\n</div>', 2, 1),
+('twitter', 'html', '<iframe allowtransparency="true" frameborder="0" scrolling="no" src="//platform.twitter.com/widgets/tweet_button.html?url={url_encoded}&amp;text={title_encoded}&amp;size=medium&amp;count=horizontal&amp;lang={lang}" style="width:100%;height:21px;"></iframe>', 3, 1),
+('pinterest', 'html', '<a href="http://pinterest.com/pin/create/button/?url={url_encoded}&media={img_url_encoded}&description={title_encoded}" class="pin-it-button" count-layout="horizontal"><img border="0" src="//assets.pinterest.com/images/PinExt.png" title="Pin It" /></a>\r\n\r\n<script type="text/javascript" src="//assets.pinterest.com/js/pinit.js"></script>', 4, 1);
 
 
 -- --------------------------------------------------------

+ 1 - 1
modules/boonex/notes/classes/BxNotesModule.php

@@ -139,7 +139,7 @@ class BxNotesModule extends BxDolModule {
         }
 
         bx_import('BxTemplSocialSharing');
-        return BxTemplSocialSharing::getInstance()->getCode(BX_DOL_URL_ROOT . $sUrl, $aContentInfo['title'], $aCustomParams);
+        return BxTemplSocialSharing::getInstance()->getCode($iContentId, 'bx_notes', BX_DOL_URL_ROOT . $sUrl, $aContentInfo['title'], $aCustomParams);
     }
 
     public function serviceEntityComments ($iContentId = 0) {

+ 18 - 3
template/scripts/BxBaseSocialSharing.php

@@ -24,12 +24,14 @@ class BxBaseSocialSharing extends BxDolSocialSharing
         $this->_oTemplate = $oTemplate ? $oTemplate : BxDolTemplate::getInstance();
 	}
 
-    public function getCode ($sUrl, $sTitle, $aCustomVars = false) {
+    public function getCode ($sContentId, $sModuleName, $sUrl, $sTitle, $aCustomVars = false) {
 
         // define markers for replacments
         bx_import('BxDolLanguages');
         $sLang = BxDolLanguages::getInstance()->getCurrentLanguage();
         $aMarkers = array (
+            'id' => $sContentId,
+            'module' => $sModuleName,
             'url' => $sUrl,
             'url_encoded' => rawurlencode($sUrl),
             'lang' => $sLang, 
@@ -62,8 +64,21 @@ class BxBaseSocialSharing extends BxDolSocialSharing
         // prepare buttons
         $aButtons = array();
         foreach ($this->_aSocialButtons as $aButton) {
-            $sButton = $this->_replaceMarkers($aButton['content'], $aMarkers);
-            if (preg_match('/{[A-Za-z0-9_]+}/', $sButton)) // if not all markers are replaced skip it
+
+            switch ($aButton['type']) {
+                case 'html':
+                    $sButton = $this->_replaceMarkers($aButton['content'], $aMarkers);
+                    break;
+                case 'service':
+                    $a = @unserialize($aButton['content']);
+                    if (false === $a || !is_array($a))
+                        break;
+                    $a = $this->_replaceMarkers($a, $aMarkers);
+                    $sButton = BxDolService::call($a['module'], $a['method'], isset($a['params']) ? $a['params'] : array(), isset($a['class']) ? $a['class'] : 'Module');
+                    break;
+            }
+            
+            if (!isset($sButton) || preg_match('/{[A-Za-z0-9_]+}/', $sButton)) // if not all markers are replaced skip it
                 continue;
             $aButtons[] = array ('button' => $sButton);
         }