mircobabini
10/31/2018 - 9:55 AM

glue-gf-repeater-in-excel.php

<?php
// init the glue on the form (10), field (101)
$Glue = new GF_Glue_Repeater_In_Excel( 10, 101 );

// generic glue (not so generic. it's specific for this "brands" case)
class GF_Glue_Repeater_In_Excel{
	public $form_id     = -1;

	public $field_id    = -1;
	public $field_index = -1;

	public $all_brands = array();
	public $structure = array();

	public function __construct( $form_id, $field_id ){
		$this->form_id = $form_id;
		$this->field_id = $field_id;

		add_filter( "gfexcel_output_rows_{$this->form_id}", array( $this, 'handle_rows' ), 10, 2 );
		add_filter( "gfexcel_output_columns_{$this->form_id}", array( $this, 'handle_columns' ), 10, 2 );
	}

	public function get_field_index_in_results(){
		if( $this->field_index === -1 ){
			return -1;
		}

		// becase of the first three standard fields (ID, IP, ...)
		return $this->field_index + 3;
	}
	public function structure_add( $row_index, $brand, $percentage ){
		$brand = trim( $brand );

		if( ! isset( $this->structure[ $row_index ] ) ){
			$this->structure[ $row_index ] = array();
		}

		$this->structure[ $row_index ][ $brand ] = $percentage;

		if( ! in_array( $brand, $this->all_brands ) ){
			$this->all_brands[] = $brand;
		}
	}
	public function handle_rows( $rows, $form_id ){
		// ottengo il form
		$form = GFAPI::get_form( $form_id );

		// cerco l'indice del campo (ID fornito)
		$__excel_column_index = 0;
		foreach( $form['fields'] as $field_index => $field ){
			// wont be in excel-columns, skip
			if( $field->type === 'section' || $field->type === 'fileupload' ){
				continue;
			}

			if( $field->id === $this->field_id ){
				$this->field_index = $__excel_column_index;
				break;
			}

			$__excel_column_index++;
		}

		// se il campo non è stato trovato, non posso procedere
		if( $this->field_index === -1 ){
			wp_die( "Errore nel cercare di trasferire il campo repeater in Excel. Field index non trovato." );
		}

		// create the structure
		foreach( $rows as $row_index => $row ){
			$entry_id = reset( $row )->getValue();
			$entry = GFAPI::get_entry( $entry_id );

			$data = unserialize( $entry[ $this->field_id ] );
			foreach( $data as $brand_data ){
				$brand_data = array_values( $brand_data );

				switch( sizeof( $brand_data ) ){
					case 2: // brand, percentage
						$this->structure_add( $row_index, $brand_data[0][0], $brand_data[1][0] );
						break;
					case 3: // _, percentage, brand
						$this->structure_add( $row_index, $brand_data[2][0], $brand_data[1][0] );
						break;
				}
			}
		}

		// sort brands
		sort( $this->all_brands );

		// inject rows
		foreach( $rows as $row_index => $row ){
			// remove repeater (starter) in row-result
			unset( $rows[ $row_index ][ $this->get_field_index_in_results() ] );

			// prepare data to inject
			$data_to_inject = array();
			if( isset( $this->structure[ $row_index ] ) ){
				foreach( $this->all_brands as $brand ){
					if( isset( $this->structure[ $row_index ][ $brand ] ) ){
						$data_to_inject[] = new GFExcel\Values\StringValue( $this->structure[ $row_index ][ $brand ] );
					}else{
						$data_to_inject[] = new GFExcel\Values\StringValue( '0' );
					}
				}
			}else{
				// this row doesnt have the repeater field in results
				foreach( $this->all_brands as $brand ){
					$data_to_inject[] = new GFExcel\Values\StringValue( '0' );
				}

			}

			// insert: https://stackoverflow.com/a/3797526/1160173
			array_splice( $rows[ $row_index ], $this->get_field_index_in_results(), 0, $data_to_inject );
		}

		return $rows;
	}
	public function handle_columns( $columns, $form_id ){
		$form = GFAPI::get_form( $form_id );

		// remove repeater (starter) in titles
		unset( $columns[ $this->get_field_index_in_results() ] );

		// prepare data to inject
		$data_to_inject = array();
		foreach( $this->all_brands as $brand ){
			$data_to_inject[] = new GFExcel\Values\StringValue( $brand );
		}

		// insert: https://stackoverflow.com/a/3797526/1160173
		array_splice( $columns, $this->get_field_index_in_results(), 0, $data_to_inject );

		return $columns;
	}
}