forked from bcit-ci/CodeIgniter
-
Notifications
You must be signed in to change notification settings - Fork 0
Share helper
Derek Jones edited this page Jul 5, 2012
·
7 revisions
CodeIgniter helper for generate share url and buttons (Twitter, Facebook, Buzz, VKontakte)
Download with example from http://mrak7.com/download/CI/share-helper.zip
p.s. sorry for wiki parser, [removed] some code ;-)
<?php
/**
* CodeIgniter helper for generate share url and buttons (Twitter, Facebook, Buzz, VKontakte)
*
* @package CodeIgniter
* @subpackage Helpers
* @category Helpers
* @version 1.0
* @author Ibragimov Renat <info@mrak7.com> www.mrak7.com
*/
if( !function_exists('share_check') ){
/**
* Check type of share and return $URL or FALSE
*
* @param string $type type of share
* @return string|bool
*/
function share_check( $type='' ){
$url = array(
'twitter' => 'http://twitter.com/share',
'facebook' => 'http://facebook.com/sharer.php',
'buzz' => 'http://www.google.com/buzz/post',
'vkontakte' => 'http://vkontakte.ru/share.php',
);
return (isset($url[$type])) ? $url[$type] : FALSE;
}
}
if( !function_exists('share_url') ){
/**
* Generate url for share at some social networks
*
* @param string $type type of share
* @param array $args parameters for share
* @return string
*/
function share_url( $type='', $args=array() ){
$url = share_check( $type );
if( $url === FALSE ){
log_message( 'debug', 'Please check your type share_url('.$type.')' );
return "#ERROR-check_share_url_type";
}
$params = array();
if( $type == 'twitter' ){
foreach( explode(' ', 'url via text related count lang counturl') as $v ){
if( isset($args[$v]) ) $params[$v] = $args[$v];
}
}elseif( $type == 'facebook' ){
$params['u'] = $args['url'];
$params['t'] = $args['text'];
}elseif( $type == 'buzz'){
$params['url'] = $args['url'];
$params['imageurl'] = $args['image'];
$params['message'] = $args['text'];
}elseif( $type == 'vkontakte'){
$params['url'] = $args['url'];
}
$param = '';
foreach( $params as $k=>$v ) $param .= '&'.$k.'='.urlencode($v);
return $url.'?'.trim($param, '&');
}
}
if( !function_exists('share_button') ){
/**
* Generate buttons for share at some social networks
*
* @param string $type type of share
* @param array $args parameters for share
* @return string
*/
function share_button( $type='', $args=array() ){
$url = share_check( $type );
if( $url === FALSE ){
log_message( 'debug', 'Please check your type share_button('.$type.')' );
return "#ERROR-check_share_button_type";
}
$params = array();
$param = '';
if( $type == 'twitter'){
if( isset($args['iframe']) ){
$url = share_url( $type, $args );
list($url, $param) = explode('?', $url);
$button = <<<DOT
<iframe allowtransparency="true" frameborder="0" scrolling="no" style="width:130px; height:50px;"
src="http://platform.twitter.com/widgets/tweet_button.html?{$param}"></iframe>
DOT;
}else{
foreach( explode(' ', 'url via text related count lang counturl') as $v ){
if( isset($args[$v]) ) $params[] = 'data-'.$v.'="'.$args[$v].'"';
}
$param = implode( ' ', $params );
$button = <<<DOT
<a href="http://twitter.com/share" class="twitter-share-button">Tweet</a>
[removed][removed]
DOT;
}
}elseif( $type == 'facebook' ){
if( !isset($args['type']) ) $args['type'] = 'button_count';
if( isset($args['fb']) ){
$params = array( 'type'=>'type', 'href'=>'url', 'class'=>'class' );
foreach( $params as $k=>$v ){
if( isset($args[$v]) ) $param .= $k.'="'.$args[$v].'"';
}
$button = "<fb:share-button {$param}></fb:share-button>";
}else{
$params = array( 'type'=>'type', 'share_url'=>'url' );
foreach( $params as $k=>$v ){
if( isset($args[$v]) ) $param .= $k.'="'.$args[$v].'"';
}
if( !isset($args['button_text']) ) $args['button_text'] = 'Share to Facebook';
$button = <<<DOT
<a name="fb_share">{$args['button_text']}</a>
[removed][removed]
DOT;
}
}elseif( $type == 'buzz' ){
$params = array( 'button-style'=>'type', 'local'=>'lang', 'url'=>'url', 'imageurl'=>'image');
foreach( $params as $k=>$v ){
if( isset($args[$v]) ) $param .= ' data-'.$k.'="'.$args[$v].'"';
}
if( !isset($args['title']) ) $args['title'] = 'Share to Google Buzz';
$button = <<<DOT
<a title="{$args['title']}" class="google-buzz-button" href="http://www.google.com/buzz/post"></a>
[removed][removed]
DOT;
}elseif( $type == 'vkontakte' ){
$url = isset($args['url']) ? '{url: "'.$args['url'].'"}' : 'false';
foreach( explode(' ', 'type text') as $v ){
if( isset($args[$v]) ) $param[] = $k.': "'.urlencode($args[$v]).'"';
}
$param = implode( ', ', $params );
if( !empty($param) ) $param = ', {'.$param.'}';
$button = <<<DOT
[removed][removed]
[removed][removed](VK.Share.button({$url}{$param}));[removed]
DOT;
}
return $button;
}
}
?>
<?
// in Controller
$url = 'http://rockufa.ru';
$text = 'Best Rock Portal Ever';
$image = 'http://rockufa.ru/forum/Smileys/rockufa/qut.gif';
?>
<!-- this is View -->
<p>
<a >$url, 'text'=>$text, 'via'=>'mpak666'))?>">Share to Twitter</a>
<a >$url, 'text'=>$text))?>">Share to Facebook</a>
<a >$url, 'image'=>$image, 'text'=>$text))?>">Share to Google Buzz</a>
<a >$url))?>">Share to Vkontakte</a>
</p>
<p>
<?=share_button('twitter', array('url'=>$url, 'text'=>$text, 'via'=>'mpak666', 'type'=>'iframe'))?>
<?=share_button('facebook', array('url'=>$url, 'text'=>$text))?>
<?=share_button('buzz', array('url'=>$url, 'image'=>$image, 'text'=>$text))?>
<?=share_button('vkontakte',array('url'=>$url))?>
</p>