<?php

class Intense_Table extends Intense_Shortcode {

	function __construct() {
		parent::__construct();

		global $intense_visions_options;

		$this->title = __( 'Table', 'intense' );
		$this->description = __( 'Adds a styled table with column headers and data using the table shortcode. Choose to add data manually or to import it from a CSV file.
			<br><br>
			There are three different table types: standard, condensed, and striped. You can also choose to show table borders. The header background and font colors can be customized.', 'intense' );
		$this->category = __( 'Layout', 'intense' );
		$this->show_preview = true;
	}

	function shortcode( $atts, $content = null ) {
		global $intense_visions_options;
		extract( shortcode_atts( $this->get_shortcode_defaults( null, $atts ), $atts ) );

		$random = rand();

		if ( !empty( $id ) ) {
			$css_selector = '#' . $id;
		} else if ( !empty( $class ) ) {
				$css_selector = '.' . $class;
		} else {
			$css_selector = "#table_$random";
		}

		if ( empty( $id ) ) $id = 'table_' . $random;

		$table_style = '';
		$heading_style = '';

		$heading_background_color = intense_get_plugin_color( $heading_background_color );
		$heading_font_color = intense_get_plugin_color( $heading_font_color );

		if ( !empty( $heading_background_color ) ) $heading_style .= 'background: ' . $heading_background_color . ' !important;';
		if ( !empty( $heading_font_color ) ) $heading_style .= 'color: ' . $heading_font_color . ' !important;';

		$columns = explode( ',', $columns );
		$data = explode( ',', $data );
		$total = count( $columns );

		if ( is_numeric( $file ) ) {
			$url = wp_get_attachment_url( $file );
		} else if ( !empty( $file ) ) {
			$url = $file;
		}

		if ( isset( $url ) ) {
			$csv = file_get_contents( $url );

			$csv = $this->csv_to_array( $csv, $delimiter, $skip_empty_lines, $trim_fields );

			$columns = array();
			$data = array();

			foreach ( $csv as $row => $fields ) {
				if ( $row == 0 ) {
					$columns = array_merge( $columns, $fields );
					$total = count( $columns );
				} else {
					//fill in so you have the same number of fields as columns
					if ( count( $fields ) < $total ) {
						for ( $i=0; $i <= $total - count( $fields ) ; $i++ ) {
							$fields[] = '';
						}
					}

					$data = array_merge( $data, $fields );
				}
			}
		}

		if ( !empty( $heading_style ) ) {
			$table_style .= $css_selector . ' th {' . $heading_style . '}';
		}

		if ( $type == 'striped' ) {
			$table_style .= $css_selector . " tbody>tr:nth-child(odd)>td, " . $css_selector . " tbody>tr:nth-child(odd)>th {
							background-color: " . intense_get_plugin_color( "boxed" ) . " !important;
						}";
		}

		$output = '';

		if ( $type == 'striped' || !empty( $heading_style ) ) {
			$output = "<style type='text/css'>";
		}

		$output .= $table_style;

		if ( !empty( $table_style ) ) {
			$output .= '</style>';
		}

		$output .= "<table id='$id' class='intense table table-$type " . ( !empty( $class ) ? ' ' . $class : '' ) . " " . ( $bordered ? "table-bordered " : "" ) . ( is_rtl() ? 'rtl ' : '' ) . "'><thead><tr>";

		foreach ( $columns as $col ) {
			$output .= '<th>' . trim( $col ) . '</th>';
		}

		$output .= '</tr></thead><tbody><tr>';
		$counter = 1;

		foreach ( $data as $datum ) {
			$output .= '<td>' . $datum . '</td>';

			if ( $counter % $total == 0 ) {
				$output .= '</tr><tr>';
			}

			$counter++;
		}

		$output = substr( $output, 0, -4 );

		$output .= '</tbody></table>';

		if ( isset( $atts['get_css'] ) && $atts['get_css'] ) {
			return array(
				'css' => $table_style,
				'base_classes' => 'intense table',
				'extra_classes' => "table-$type " . ( !empty( $class ) ? ' ' . $class : '' ) . " " . ( $bordered ? "table-bordered " : "" ) . ( is_rtl() ? 'rtl ' : '' )
			);
		}

		return $output;
	}

	/**
	 * Borrowed and modified from http://php.net/str_getcsv comment by Jay Williams
	 *
	 * @link http://gist.github.com/385876
	 */

	function csv_to_array( $csv='', $delimiter=',', $skip_empty_lines = true, $trim_fields = true ) {
		$data = array();

		$lines = str_getcsv( $csv, "\n" ); //parse the lines

		foreach ( $lines as $line ) {
			$row = str_getcsv( $line, $delimiter ); //parse the row

			$is_empty = false;
			$value_list = '';

			foreach ( $row as $key => $value ) {
				if ( $trim_fields ) {
					$row[ $key ] = trim( $value );
					$value = $row[ $key ];
				}

				$value_list .= $value;
			}

			if ( !empty( $value_list ) || !$skip_empty_lines ) {
				$data[] = $row;
			}
		}

		return $data;
	}

}
