rodrigobertin
7/13/2015 - 1:46 AM

Social Counter

Social Counter

/**
 * Class Social Counter and share buttons
 */
class SocialCounter
{
	public $url;
	private $urlFace;
	private $urlTwitter;
	private $urlGoogle;
	public $countLikesFace;
	public $countShareFace;
	public $countTwitter;
	public $countGoogle;
	public $render;


	/**
	 * Contar
	 * @return string
	 */
	public function contar()
	{
		//paso la url con el iden del vendedor
		$this->url = urlencode(dameURL() . '&vend_id=' . base64_encode($_SESSION['id']));

		//set
		$this->urlFace = 'https://api.facebook.com/restserver.php?method=links.getStats&urls=' . $this->url . '/&format=json';
		$this->urlTwitter = 'http://urls.api.twitter.com/1/urls/count.json?url=' . $this->url;
		$this->urlGoogle = 'https://plusone.google.com/_/+1/fastbutton?url=' . $this->url;

		//facebook data
		$fql = "SELECT url, normalized_url, share_count, like_count, comment_count, ";
		$fql .= "total_count, commentsbox_count, comments_fbid, click_count FROM ";
		$fql .= "link_stat WHERE url = '" . $this->url . "'";
		$apifql = "https://api.facebook.com/method/fql.query?format=json&query=" . urlencode($fql);
		$json = file_get_contents($apifql);
		$contFace = json_decode($json);

		//twitter data
		$dataTwitter = file_get_contents($this->urlTwitter);
		$contTwitter = json_decode($dataTwitter, true);

		//google Counter
		$dataGoogle = file_get_contents('https://plusone.google.com/_/+1/fastbutton?url=' . $this->url);
		$countGoogle = preg_match('/window\.__SSR = {c: ([\d]+)/', $dataGoogle, $matches);

		//shares facebooks
		$this->countShareFace = $contFace[0]->share_count;
		//count twitter
		$this->countTwitter = $contTwitter['count'];
		//count google
		$this->countGoogle = $countGoogle;
	}

	/**
	 * Compartir en redes sociales
	 *
	 * @param $imagen
	 * @param $titulo
	 * @param $descripcion
	 * @param $tipo
	 *
	 * @return string
	 */
	public function BotonesCompartir($titulo, $counter = true, $urlShare = '', $descripcion = '')
	{
		if ($urlShare == '') {
			$urlShare = $this->url;
		} else {
			$urlShare = urlencode($urlShare);
		}
		//compartir en face
		$redes = array('face', 'twitter', 'google', 'mail');


		for ($c = 0; $c < count($redes); $c++):
			$tipo = $redes[$c];

			if ($tipo == 'face' || $tipo == 'facebook') {
				$enlace = 'http://facebook.com/sharer/sharer.php?s=100&p[url]=' . $urlShare;
				$icon = 'facebook';
				$color = '#344a90';
				$text = ' Compartir';

				if ($counter == true) {
					$d = $this->countShareFace;
				} else {
					$d = '';
				}
			}

			//compartir en twitter
			if ($tipo == 'twitter') {
				$enlace = 'https://twitter.com/share?url=' . $urlShare . ';hashtags=' . $titulo;
				$icon = 'twitter';
				$color = '#1c9cf0';
				$text = ' Tweet';

				if ($counter == true) {
					$d = $this->countTwitter;
				} else {
					$d = '';
				}

			}

			//compartir google plus
			if ($tipo == 'google') {
				$enlace = 'https://plus.google.com/share?url=' . $urlShare;
				$icon = 'google-plus';
				$color = '#D43D2F';
				$text = ' Share';

				if ($counter == true) {
					$d = $this->countGoogle;
				} else {
					$d = '';
				}

			}

			if ($tipo == 'mail') {
				$enlace = 'mailto:?&subject=' . $titulo . '&body=' . $urlShare . ' - ' . $descripcion;
				$icon = 'envelope';
				$color = 'blue';
				$text = 'E-mail';
			}

			//accion on click
			$on = 'onclick="window.open(this.href, \'Compartir\',\'left=20,top=20,width=500,height=500,toolbar=0,resizable=0\'); return false;"';
			//para mail no
			if ($tipo == 'mail') {
				$on = '';
			}

			//estio del boton
			$style = "background:" . $color . ";";
			$style .= 'padding:.3em .9em;';
			$style .= 'color:#fff !important;';
			$style .= 'margin:.5em .1em;';
			$style .= 'display:inline-block;';

			//estilo del Icono
			$styleI = 'color:white !important;z-index:3;';
			$styleI .= 'font-size:1.8em !important';

			//crear el boton
			$print = '' .
				'<a href="' . $enlace . '" ' . $on . ' style="' . $style . '" >' .
				'<i style="' . $styleI . '" class="fa fa-' . $icon . '-square"></i> ' .
				'<span style="vertical-align:4px;padding-right:3px;" >' . $text . '  ' . $d . ' ' .
				'</span>' .
				'</a>';

			echo $print;

			//////////
		endfor;
	}

}