Permalink
Browse files

The extension

  • Loading branch information...
1 parent c502499 commit c6a37a80f97e9404d64075771e5e354a762515fe @alejofonseca committed Feb 2, 2012
Showing with 125 additions and 0 deletions.
  1. +68 −0 GPluginBase.php
  2. +57 −0 GPlusoneButton.php
View
68 GPluginBase.php
@@ -0,0 +1,68 @@
+<?php
+/**
+ * Registration class file.
+ *
+ * @author Alejandro Fonseca <alejandrofonseca - AT - gmx - DOT - com>
+ * @link https://github.com/alejofonseca/yii-google-plusone
+ * @license http://www.gnu.org/licenses/gpl.html GNU General Public License, version 3
+ *
+ */
+
+/**
+ * This is the base class for the plusone google button plugin. Collects
+ * the parameters for the div tag and echo the button.
+ *
+ */
+abstract class GPluginBase extends CWidget
+{
+ /**
+ * Grabs public properties of the child class for passing to the plugin creator.
+ * @return array Associative array
+ */
+ protected function getParams()
+ {
+ $ref = new ReflectionObject($this);
+ $props = $ref->getProperties(ReflectionProperty::IS_PUBLIC);
+
+ $params = array();
+ foreach ($props as $k => $v) {
+ $name = $v->name;
+ if ($this->$name !== null && !is_array($this->$name)) {
+ if (is_bool($this->$name)) {
+ $value = ($this->$name === true) ? 'true' : 'false';
+ }
+ else {
+ $value = $this->$name;
+ }
+ $params[$name] = $value;
+ }
+ }
+ return $params;
+ }
+
+ /**
+ * Elaborates the proper name for the div tag.
+ *
+ * @param $name 'plusone'
+ * @param $params the parameters for the Plusone Plugin
+ * @return void
+ */
+ protected function renderTag($name, $params){
+ $this->makeHtml5Tag('g-'.$name,$params);
+ }
+
+ /**
+ * @param $class the name of the Plusone Plugin
+ * @param $params the parameters for the Plusone Plugin
+ * @return void
+ */
+ protected function makeHtml5Tag($class, $params) {
+ $newParams = array();
+ foreach($params as $key=>$data) {
+ $newParams["data-".$key] = $data;
+ }
+ $newParams['class'] = $class;
+ echo CHtml::openTag('div', $newParams), CHtml::closeTag('div');
+ }
+}
+?>
View
57 GPlusoneButton.php
@@ -0,0 +1,57 @@
+<?php
+/**
+ * ActivityFeed class file.
+ *
+ * @author Alejandro Fonseca <alejandrofonseca - AT - gmx - DOT - com>
+ * @link https://github.com/alejofonseca/yii-google-plusone
+ * @license http://www.gnu.org/licenses/gpl.html GNU General Public License, version 3
+ *
+ */
+
+require_once 'GPluginBase.php';
+
+/**
+ * The Plusone button lets visitors recommend your content on Google Search and
+ * share it on Google+
+ *
+ * @see http://www.google.com/webmasters/+1/button/
+ */
+
+class GplusoneButton extends GPluginBase
+{
+ /**
+ * @var string This attribute explicitly defines the +1 target URL.
+ */
+ public $href;
+
+ /**
+ * @var integer If annotation is set to "inline", the width in pixels
+ * to use for the button and its inline annotation.
+ */
+ public $width;
+
+ /**
+ * @var string The annotation to display next to the button.
+ */
+ public $annotation;
+
+ /**
+ * @var string The language to use.
+ */
+ public $lang;
+
+ public function run()
+ {
+ parent::run();
+ $params = $this->getParams();
+ $this->renderTag('plusone', $params);
+ $script = "window.___gcfg = {lang: '".$params['lang']."'};
+ (function() {
+ var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
+ po.src = 'https://apis.google.com/js/plusone.js';
+ var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
+ })();";
+ Yii::app()->getClientScript()->registerScript('fb-async-callback', $script, CClientScript::POS_END);
+ }
+}
+?>

0 comments on commit c6a37a8

Please sign in to comment.