<?php

/*
Plugin Name: C2EM grafic estadístic
Description: Veure un grafic estadístic segons el camp  de les dades d'un formulari . <p> Exemple <code>[c2em_visualizer id="538"   form_name="Aportacions" field_name="tipus"] </code> en una pagina per veure el contingut. Ha d'estar instal·lar el plugin visualizer i els atributs són els mateixos a excepció de <code>form_name</code> i <code>field_name</code></p>
Author: Arnau Sanchez 
License: GPL2
Version: 1.0.2
*/


//Ha d'estar el plugin visualizer i cfdb
if ( is_plugin_active( 'visualizer/index.php' ) 
	&&  is_plugin_active( 'contact-form-7-to-database-extension/contact-form-7-db.php' )  ) {

	add_shortcode( 'c2em_visualizer', 'c2em_visualizer_html' ); 

	function c2em_visualizer_html($atts) {

		//Recuparem l'atribut del shortcode
		//Els 4 primers son els propis del shorcode [visualizer]
		//Nom del formulari per veure les dades
		//Camp per veure les estadistiques
		$var = shortcode_atts( array(
			'id'     => false, // chart id (creat amb visualizer des del backend)
			'class'  => false, // chart class
			'series' => false, // series filter hook
			'data'   => false, // data filter hook
			'settings'   => false, // data filter hook
		    'form_name' => 'nom formulari',
			'field_name' => 'nom camp',
		), $atts );

		// Sino hi ha id o no existeix, retornem amb un avis
		if ( !$atts['id'] || !( $chart = get_post( $atts['id'] ) ) || $chart->post_type != Visualizer_Plugin::CPT_VISUALIZER ) {
			return 'Upps, no hi ha cap grafic al Visualizer Library. Comprova que l\'id és l\'adequat. ';
		}
		//Nom del filtre creat mes endavant per personalizar les series i les dades
		$series="mycustom_series_filter";
		$data="mycustom_data_filter";
		$content= do_shortcode( '[visualizer id="'.$var['id'].'" series="'.$series.'" data="'.$data.'"]');


		//Desem les dades per recuperar-les mes tard
		update_option('c2em_visualizer_opt', $var);
		return $content;
		
	}

	//Costumitzem la serie del grafic, sera Nom:numero
	add_filter( 'mycustom_series_filter', 'myplugin_filter_chart_series', 10, 3 );
	function myplugin_filter_chart_series( $series, $chart_id, $type ) {
		$series = array(
		// the first series
		array(
			'label' => 'Classes1', // the label of the series
			'type' => 'string', // the type of the series
		),
		array(
			'label' => 'Classes2', // the label of the series
			'type' => 'number', // the type of the series
		),
		);
		return $series;
	}
	
	add_filter( 'mycustom_data_filter', 'myplugin_filter_chart_data', 10, 3 );
	function myplugin_filter_chart_data( $data, $chart_id, $type ) {
		//Recuparem els atributs desats
		$var = get_option('c2em_visualizer_opt');
		//Recupareme el nom de la taula que crea CF7DB per veure les dades del formulari desitjat
		require_once (ABSPATH . '/wp-content/plugins/contact-form-7-to-database-extension/CF7DBPlugin.php');
		$plugin = new CF7DBPlugin;
		$tableName = $plugin->getSubmitsTableName();
		//Recuparem el contingut a mostrar en el grafic
		$field_count_values=c2em_field_count_value($var, $tableName);
		/*
		$data = array(
		// the first row of data
		array(
			$field_count_value[0][0], // the value of the first series
			2,
			 // the value of the second series
			 // the value of the third series
		),
		// the second row of data
		array(
			$field_count_value[1][0], // the value of the first series
			1,
			 // the value of the second series
			 // the value of the third series
		),
		array(
			$field_count_value[2][0], // the value of the first series
			2,
			 // the value of the second series
			 // the value of the third series
		),
		array(
			$field_count_value[3][0], // the value of the first series
			2,
			 // the value of the second series
			 // the value of the third series
		),
		);*/
		$i=0;
		foreach ( $field_count_values as $field_count_value ){
			$data[$i]=array(
					$field_count_value[0], // the value of the first series
					 intval($field_count_value[1]),
					 // the value of the second series
					 // the value of the third series
					);
			$i+=1;
		}
		//Redimensio del array, sino posa el que hi ha per defecte
		array_splice($data, $i);
		return $data;
	}

	//contingut a mostrar en el grafic
	function c2em_field_count_value($var, $tableName){
		global $wpdb;
	
		//Recuparem quants hi ha segons dintre el camp field_name
		$field_count_values =$wpdb->get_results( 
							$wpdb->prepare("select  `field_value`, count(*) as count_value 
											from `$tableName`
											where `form_name` = '%s' and `field_name` =  '%s' 
											group by `field_value`", $var['form_name'], $var['field_name']),
						ARRAY_N);	

		return $field_count_values;
	}


}
