<?php

/*
	Googlyzer
	Contact: Bill Edgar (bill.edgar@oaktondata.com)
	http://www.oaktondata.com/googlyzer
	
	Copyright (c) 2011-2012, Oakton Data LLC
	All rights reserved.

	Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
	    
	    * Redistributions of source code must retain the above copyright
	      notice, this list of conditions and the following disclaimer.
	    * Redistributions in binary form must reproduce the above copyright
	      notice, this list of conditions and the following disclaimer in the
	      documentation and/or other materials provided with the distribution.
	    * Neither the name of the Oakton Data LLC nor the
	      names of its contributors may be used to endorse or promote products
	      derived from this software without specific prior written permission.

	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
	ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
	WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
	DISCLAIMED. IN NO EVENT SHALL Oakton Data LLC BE LIABLE FOR ANY
	DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
	(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
	LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
	ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
	(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
	SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
	
	This is the googlyzer_Chart PHP class file.
	
 */

if ( !class_exists( 'googlyzer_ChartObj' ) ) {

	/** googlyzer_Chart class
	 *
	 * PHP class for creation and manipulation of chart objects.
	 * @author wmsedgar
	 *
	 */
	class googlyzer_ChartObj {
		/* public variables */

		/* private variables */
		private $composite = 			false;
		private $elementId = 			null; // must be set in constructor
		private $fillColor = 			'#33CCFF';
		private $height = 				null; // must be set in constructor
		private $lineColor = 			'#000000'; // must be set in constructor (line only)
		private $maxSpotColor = 		'#7D7D7D'; // #CCCC00 was default
		private $minSpotColor = 		'#7D7D7D'; // #CCCC00 was default
		private $sliceColors = 			array(); // must be set in constructor (pie only)
		private $spotColor = 			'#7D7D7D'; // #CCCC00 was default
		private $jQuerySparklineTags = 	'';
		private $type =					null; // must be set in constructor (line or pie)
		private $width = 				null; // must be set in constructor (line only)
		private $values = 				array();
		
		/**
		 * 
		 * old style constructor for backwards PHP compatibility
		 */
		public function googlyzer_ChartObj( $args ) {
			$this->__construct( $args );
		}
		
		/**
		 * 
		 * public constructor
		 * 
		 * @param array $args
		 */
		public function __construct( $args ) {
			extract( $args );
			// set chart object properties
			foreach ( $args as $key => $value ) {
				$this->$key = $value;
			}
			// set fill color based on composite setting
			if	( $this->composite ) $this->fillColor = false;
			// build sparkline tag options string specific to chart type
			$this->buildTags();
		}
		
		/*
		* private functions
		*/
		
		private function sparklineScriptStart( $elementId ) {
			return "jQuery( '." . $elementId . "_sparkline' ).sparkline( ";
		}
		
		/*
		 * public functions
		*/
		
		public function buildTags() {
			$tags = '';
			switch ( $this->type ) {
				case 'line':
					$tags .= "type:'line', ";
					if ( $this->composite ) {
						$tags .= "composite:true, fillColor:false, ";
					} elseif ( $this->fillColor ) {
						$tags .= "fillColor:'" . $this->fillColor . "', ";
					} else {
						$tags .= "fillColor:false, ";
					}
					$tags .= "height:'" . $this->height . "', " .
							"lineColor:'" . $this->lineColor . "', " .
							"maxSpotColor:'" . $this->maxSpotColor . "', " .
							"minSpotColor:'" . $this->minSpotColor . "', " .
							"spotColor:'" . $this->spotColor . "', " .
							"width:'" . $this->width . "'";
					break;
				case 'pie':
					$tags .= "type:'pie', height:'" . $this->height . "', " .
							"sliceColors:[ '" . join( "','", $this->sliceColors ) . "' ]";
					break;
			}
			$this->jQuerySparklineTags = $tags;
		}
		
		/**
		 * 
		 * public method setProperties to set object attributes using associative array.
		 * 
		 * @param array $properties
		 */
		public function setProperties( $properties ) {
			foreach ( $properties as $key => $value ) {
				$this->$key = $value;
			}
		}
		
		public function getSparklineScript() {
			// output sparkline script
			$script = $this->sparklineScriptStart( $this->elementId );
			$script .= "[" . $this->values . "], { " . $this->jQuerySparklineTags . " } );";
			return $script;	
		}
		
		public function printTable() {
			// output table
		}
		
	} // end class googlyzer_Chart
	
} // end if class exists

?>