Skip to content

Commit

Permalink
Fix #19: Implement GithubXPlugin
Browse files Browse the repository at this point in the history
  • Loading branch information
kartik-v committed Feb 5, 2015
1 parent 75192ff commit 4b2fdbd
Show file tree
Hide file tree
Showing 26 changed files with 649 additions and 122 deletions.
26 changes: 0 additions & 26 deletions .gitattributes

This file was deleted.

1 change: 0 additions & 1 deletion .gitignore

This file was deleted.

1 change: 1 addition & 0 deletions CHANGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ version 1.3.0
- Ability to add additional javascript **before** sending the ga data.
- Ability to add additional javascript **after** sending the ga data.
- (enh #18): Separate Github star and watch buttons as per v2.0.
- (enh #19): Implement GithubXPlugin based on [github buttons](https://github.com/ntkme/github-buttons).

version 1.2.0
=============
Expand Down
4 changes: 2 additions & 2 deletions Disqus.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ public function init()
parent::init();
$config = $this->setConfig('disqus');
if ($this->credits === null) {
$this->credits = Html::a(Yii::t('social', 'comments powered by Disqus'), 'http://disqus.com/?ref_noscript');
$this->credits = Html::a(Yii::t('kvsocial', 'comments powered by Disqus'), 'http://disqus.com/?ref_noscript');
}
$this->noscript = Yii::t('social', 'Please enable JavaScript to view the {pluginLink}.', ['pluginLink' => $this->credits]);
$this->noscript = Yii::t('kvsocial', 'Please enable JavaScript to view the {pluginLink}.', ['pluginLink' => $this->credits]);
if (empty($this->settings['shortname'])) {
throw new InvalidConfigException("Disqus 'shortname' has not been set in settings.");
}
Expand Down
2 changes: 1 addition & 1 deletion FacebookPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function init()
throw new InvalidConfigException("The Facebook 'appId' has not been set.");
}
if (!isset($this->noscript)) {
$this->noscript = Yii::t('social', 'Please enable JavaScript on your browser to view the Facebook {pluginName} plugin correctly on this site.', ['pluginName' => Yii::t('social', str_replace('fb-', '', $this->type))]
$this->noscript = Yii::t('kvsocial', 'Please enable JavaScript on your browser to view the Facebook {pluginName} plugin correctly on this site.', ['pluginName' => Yii::t('kvsocial', str_replace('fb-', '', $this->type))]
);
}
$this->registerAssets();
Expand Down
2 changes: 1 addition & 1 deletion GithubPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function init()
throw new InvalidConfigException("The GitHub button 'type' has not been set.");
}
if (!isset($this->noscript)) {
$this->noscript = Yii::t('social', 'Please enable JavaScript on your browser to view the Facebook {pluginName} plugin correctly on this site.', ['pluginName' => Yii::t('social', str_replace('fb-', '', $this->type))]
$this->noscript = Yii::t('kvsocial', 'Please enable JavaScript on your browser to view the Facebook {pluginName} plugin correctly on this site.', ['pluginName' => Yii::t('kvsocial', str_replace('fb-', '', $this->type))]
);
}
if (!empty($this->settings['count']) && ($this->settings['count'] || $this->settings['count'] == 'true')) {
Expand Down
217 changes: 217 additions & 0 deletions GithubXPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
<?php

/**
* @copyright Copyright &copy; Kartik Visweswaran, Krajee.com, 2014
* @package yii2-social
* @version 1.3.0
*/

namespace kartik\social;

use Yii;
use yii\base\InvalidConfigException;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\helpers\Url;

/**
* Widget to render various Github buttons. Based on the
* [unofficial Github Buttons](https://buttons.github.io/).
*
* Usage:
* ```
* echo GithubXPlugin::widget([
* 'type' => GithubPlugin::WATCH,
* 'user' => 'GITHUB_USER',
* 'repo' => 'GITHUB_REPO',
* 'settings' => ['data-style'=>'mega']
* ]);
* ```
*
* @see https://github.com/ntkme/github-buttons
*
* @author Kartik Visweswaran <[email protected]>
* @since 1.0
*/
class GithubXPlugin extends Widget
{
const WATCH = 'watch';
const STAR = 'star';
const FORK = 'fork';
const ISSUE = 'issue';
const DOWNLOAD = 'download';
const FOLLOW = 'follow';

/**
* @var string the type of button. One of 'watch', 'fork', 'follow'. This is mandatory.
*/
public $type;

/**
* @var string the Github user name that owns the repo. This is mandatory.
*/
public $user;

/**
* @var string the Github repository name. This is mandatory for all buttons except FOLLOW.
*/
public $repo;

/**
* @var bool whether to show the count. Defaults to `true`.
*/
public $showCount = true;

/**
* @var string the button label to display. If not set it will be autogenerated based on
* the button type.
*/
public $label;

/**
* @var array the social plugin settings. The following attributes are recognized:
* - href: controls the size of the button one of `default` or `mega`.
* - data-style: controls the size of the button one of `default` or `mega`.
* - data-icon: string, the octicon for the button. It will be autogenerated if not set.
* All available icons can be found at [Octicons](https://octicons.github.com/).
* - data-count-href: GitHub link for the count. It defaults to `href` value (generated from
* `repo` and `user` name). Relative url will be relative to `href` value. It will
* be autogenerated if not set.
* - data-count-api: string, GitHub API endpoint for the count. It will be autogenerated if
* not set.
*/
public $settings = [];

/**
* @var string the HTML attributes for the button
*/
public $options = [];

/**
* @var array the valid plugins
*/
protected $validPlugins = [
self::WATCH,
self::STAR,
self::FORK,
self::ISSUE,
self::DOWNLOAD,
self::FOLLOW
];

/**
* Initialize the widget
*
* @throws InvalidConfigException
*/
public function init()
{
parent::init();
$this->setConfig('githubX');
if (empty($this->user)) {
throw new InvalidConfigException("The GitHub 'user' must be set.");
}
if (empty($this->repo) && $this->type !== self::FOLLOW) {
throw new InvalidConfigException("The GitHub 'repository' has not been set.");
}
if (empty($this->type)) {
throw new InvalidConfigException("The GitHub button 'type' has not been set.");
}
if (!isset($this->noscript)) {
$this->noscript = Yii::t('kvsocial',
'Please enable JavaScript on your browser to view the Facebook {pluginName} plugin correctly on this site.',
['pluginName' => Yii::t('kvsocial', str_replace('fb-', '', $this->type))]
);
}
}

/**
* @inheritdoc
*/
public function run()
{
Html::addCssClass($this->options, 'github-button');
Html::addCssStyle($this->options, 'padding:0 5px');
echo $this->renderButton();
$view = $this->getView();
$view->registerJsFile('https://buttons.github.io/buttons.js', [
'id' => 'github-bjs',
'async' => true,
'defer' => true
]);
}

/**
* Gets the default button configurations settings
*
* @return array
*/
public function getDefaultSetting()
{
$repodir = $this->user . '/' . $this->repo;
$href = "https://github.com/{$repodir}";
$api = "/repos/{$repodir}#";
switch ($this->type) {
case self::WATCH:
return [
"href" => $href,
"data-icon" => "octicon-eye",
"data-count-api" => "{$api}subscribers_count",
"data-count-href" => "/{$repodir}/watchers",
"label" => Yii::t('kvsocial', 'Watch')
];
case self::STAR:
return [
"href" => $href,
"data-icon" => "octicon-star",
"data-count-api" => "{$api}stargazers_count",
"data-count-href" => "/{$repodir}/stargazers",
"label" => Yii::t('kvsocial', 'Star')
];
case self::FORK:
return [
"href" => $href . '/fork',
"data-icon" => "octicon-git-branch",
"data-count-api" => "{$api}forks_count",
"data-count-href" => "{$href}/{$repodir}/network",
"label" => Yii::t('kvsocial', 'Fork')
];
case self::ISSUE:
return [
"href" => $href . "/issues",
"data-icon" => "octicon-issue-opened",
"data-count-api" => "{$api}open_issues_count",
"label" => Yii::t('kvsocial', 'Issue')
];
case self::DOWNLOAD:
return [
"href" => "https://github.com/{$this->user}",
"data-icon" => "octicon-cloud-download",
"label" => Yii::t('kvsocial', 'Download')
];
case self::FOLLOW:
return [
"href" => $href . "/archive/master.zip",
"data-icon" => "octicon-mark-github",
"data-count-api" => "/users/{$this->user}#followers",
"data-count-href" => "/{$this->user}/followers",
"label" => Yii::t('kvsocial', 'Follow') . " @{$this->user}"
];
default:
return [];
}
}

/**
* Renders the button
*
* @return string
*/
public function renderButton()
{
$setting = $this->getDefaultSetting();
$label = empty($this->label) ? ArrayHelper::remove($setting, 'label', '') : $this->label;
$setting += $this->settings + $this->options;
return Html::tag('a', $label, $setting);
}
}
8 changes: 4 additions & 4 deletions GooglePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public function init()
throw new InvalidConfigException("The Google 'communityId' must be set for the community badge.");
}
if (!isset($this->noscript)) {
$this->noscript = Yii::t('social', 'Please enable JavaScript on your browser to view the Google {pluginName} plugin correctly on this site.', ['pluginName' => Yii::t('social', str_replace('ga-', '', $this->type))]
$this->noscript = Yii::t('kvsocial', 'Please enable JavaScript on your browser to view the Google {pluginName} plugin correctly on this site.', ['pluginName' => Yii::t('kvsocial', str_replace('ga-', '', $this->type))]
);
}
$this->registerAssets();
Expand Down Expand Up @@ -147,9 +147,9 @@ protected function registerAssets()
{
$view = $this->getView();
$view->registerJsFile('https://apis.google.com/js/platform.js', [
'position'=>View::POS_HEAD,
'async'=>true,
'defer'=>true
'position' => View::POS_HEAD,
'async' => true,
'defer' => true
]);
$view->registerJs("\nwindow.___gcfg={lang:'{$this->language}'};\n", View::POS_HEAD);
}
Expand Down
17 changes: 15 additions & 2 deletions Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,7 @@ class Module extends \yii\base\Module

/**
* @var array the github buttons api configuration. You can setup these keys:
* - type: string the Twitter Screen Name. This is mandatory for
* follow, mention, and hashtag buttons.
* - type: string the Github button type.
* - settings: array the configuration for the GitHub buttons widget
* - options: array the HTML attributes for the GitHub buttons iframe container.
* - noscript: string/boolean text to be displayed if browser does not support
Expand All @@ -106,6 +105,20 @@ class Module extends \yii\base\Module
*/
public $github = [];

/**
* @var array the github extended alternative buttons api configuration. You can setup these keys:
* - type: string the Github button type.
* - user: string the Github user name.
* - repo: string the Github repo name.
* - settings: array the configuration for the GitHub buttons widget
* - options: array the HTML attributes for the GitHub buttons iframe container.
* - noscript: string/boolean text to be displayed if browser does not support
* javascript. If set to false will not displayed.
* - noscriptOptions: array HTML attributes for the noscript message container.
* Defaults to ['class' => 'alert alert-danger'].
*/
public $githubX = [];

/**
* @var FacebookSession the Facebook session object
*/
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ Module that enables access to social plugins for Yii Framework 2.0. It includes
- Star Button
- Fork Button
- Follow Button
- GitHubX
- Watch Button
- Star Button
- Fork Button
- Issue Button
- Download Button
- Follow Button

## Installation

Expand Down
2 changes: 1 addition & 1 deletion TwitterPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function init()
throw new InvalidConfigException("The Twitter 'content' must be set for displaying 'embedded tweets'.");
}
if (!isset($this->noscript)) {
$this->noscript = Yii::t('social', 'Please enable JavaScript on your browser to view the Twitter {pluginName} plugin correctly on this site.', ['pluginName' => Yii::t('social', str_replace('twitter-', '', $this->type))]
$this->noscript = Yii::t('kvsocial', 'Please enable JavaScript on your browser to view the Twitter {pluginName} plugin correctly on this site.', ['pluginName' => Yii::t('kvsocial', str_replace('twitter-', '', $this->type))]
);
}
$this->registerAssets();
Expand Down
8 changes: 4 additions & 4 deletions Widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,16 @@ public function init()
if ($this->validPlugins !== false && !in_array($this->type, $this->validPlugins)) {
throw new InvalidConfigException("Invalid plugin type 'type'.");
}
Yii::setAlias('@social', dirname(__FILE__));
Yii::setAlias('@kvsocial', dirname(__FILE__));
if (empty($this->i18n)) {
$this->i18n = [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '@social/messages',
'basePath' => '@kvsocial/messages',
'forceTranslation' => true
];
}
Yii::$app->i18n->translations['social'] = $this->i18n;
$this->noscript = Yii::t('social', 'You must enable Javascript on your browser for the site to work optimally and display sections completely.');
Yii::$app->i18n->translations['kvsocial'] = $this->i18n;
$this->noscript = Yii::t('kvsocial', 'You must enable Javascript on your browser for the site to work optimally and display sections completely.');
}

/**
Expand Down
Loading

0 comments on commit 4b2fdbd

Please sign in to comment.