<?php
/*  Copyright 2014-2015 Scott Paterson

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

/*
Autor: asanch92
shortcode: [c2em_qr size="240" page="control-acreditacions"]
Motra un codi QR amb el contingut home_url( '/' )+control-acreditacions+/?dni=...
El parametre get_param es posa per tal que hi pugui haver diferents usuaris passant el codi i la pagina per rebre'ls no es solapi la informacio
*/
//global $get_param;

add_shortcode ('c2em_qr','c2em_qr_php');


/*
Crear el codi de barres de tots els DNI
*/
function c2em_qr_php($atts){
	include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
	if ( is_plugin_active( 'qrcode-wprhe/qrcode_wprhe.php' ) ){
		//Recuparem l'atribut del shortcode, el formulari on es troben els DNIs
		$var = shortcode_atts( array(
			'size' => '240',
			'page' => '',
			//'get_param' => '',
		), $atts );
	

		$size = $var['size'];
		$page = $var['page'];
		$get_param = 'dni';//$var['get_param'];
		$content='';
		//Per que el wp envii correctament pel metode get:
		add_filter( 'query_vars', 'add_query_vars_filter' );

		//Mirem el nom d'usuari actual, que ha de ser el dni i el posem com a parametre la url
		//$url_actual = home_url( add_query_arg( array() ) )."?dni=".$current_user->user_login;
		$current_user = wp_get_current_user();
		//$url =  home_url( '/' ).$page."/?".$get_param."=".$current_user->user_login;
		$url = add_query_arg( $get_param, $current_user->user_login, home_url($page)); // www.example.com/page/?dni=...
		$content= do_shortcode("[qrcode content='".$url."' size='".$size."']");
		$content.=$url;
	}
	else{
		$content="No es troba activat el plugin qrcode_wprhe";
	}
//	echo get_query_var( 'dni' ); 

	return $content;
}

/*function add_query_vars_filter( $vars ){
  	$vars[] = $get_param;
 	return $vars;
}

add_filter( 'query_vars', 'add_query_vars_filter' );*/

?>
