-
Notifications
You must be signed in to change notification settings - Fork 26
Share helper
CodeIgniter helper for generate share url and buttons (Twitter, Facebook, Buzz, VKontakte)
[h3]Source[/h3] [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 [email protected] 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 Tweet [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 {$args['button_text']} [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 [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;[removed] DOT; } return $button; } } ?> [/code]
[h3]Example[/h3] [code] <? // in Controller $url = 'http://rockufa.ru'; $text = 'Best Rock Portal Ever'; $image = 'http://rockufa.ru/forum/Smileys/rockufa/qut.gif'; ?>
<!-- this is View -->
$url, 'text'=>$text, 'via'=>'mpak666'))?>">Share to Twitter $url, 'text'=>$text))?>">Share to Facebook $url, 'image'=>$image, 'text'=>$text))?>">Share to Google Buzz $url))?>">Share to Vkontakte
<?=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))?>
[/code]