<?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_AccountFeed PHP class file.
	
 */

if ( !class_exists( 'googlyzer_AccountFeed' ) ) {

	/** googlyzer_AccountFeed class
	 *
	 * PHP class for processing Google Analytics account feed using curl.
	 * @author wmsedgar
	 *
	 */
	class googlyzer_AccountFeed extends googlyzer_GoogleFeed {
		/* public variables */
		
		/* private variables */
		private $accountList = array();
		/*
		 * private methods
		 */
		
		private function parseAccountList() {
			
			$accountDOM = new DOMDocument();
			$accountDOM->loadXML( $this->curl_response );
			$accountEntries = $accountDOM->getElementsByTagName( 'entry' );
			
			$i = 0;
			$accountProfiles = array();
			foreach( $accountEntries as $account ){
			
				$this->accountList[$i] = array();
			
				$accountTitle = $account->getElementsByTagName( 'title' );
				$this->accountList[$i]['title'] = $accountTitle->item( 0 )->nodeValue;
			
				$accountEntryId = $account->getElementsByTagName( 'id' );
				$this->accountList[$i]['entryid'] = $accountEntryId->item( 0 )->nodeValue;
			
				$accountProperties = $account->getElementsByTagName( 'property' );
				foreach( $accountProperties as $property ){
					if ( strcmp( $property->getAttribute( 'name' ), 'ga:accountId' ) == 0 ){ 
						$this->accountList[$i]["accountId"] = $property->getAttribute( 'value' );
					}
					if ( strcmp( $property->getAttribute( 'name' ), 'ga:accountName' ) == 0 ){
						$this->accountList[$i]["accountName"] = $property->getAttribute( 'value' );
					}
					if ( strcmp( $property->getAttribute( 'name' ), 'ga:profileId' ) == 0 ){
						$this->accountList[$i]["profileId"] = $property->getAttribute( 'value' );
					}
					if ( strcmp( $property->getAttribute( 'name' ), 'ga:webPropertyId' ) == 0 ){
						$this->accountList[$i]["webPropertyId"] = $property->getAttribute( 'value' );
					}
				}
			
				$accountTableId = $account->getElementsByTagName( 'tableId' );
				$this->accountList[$i]['tableId'] = $accountTableId->item( 0 )->nodeValue;
			
				$i++;
			}
		}
		
		/**
		* Returns associative array with ga:tableId => accountTitle
		*
		*/
		private function getProfileList(){
			$profileList = array();
			foreach ( $this->accountList as $account ){
				$profileList[$account['tableId']] =  $account['title'];
			}
			return $profileList;
		}
		
		/*
		 * public methods
		 */
		
		public function getAccountList() {
			return $this->accountList;
		}
		
		/**
		 * 
		 * method to handle feed for accounts
		 * 
		 * @return array $profiles
		 * 
		 */
		public function handleFeed() {
			// parse account list, then return profiles
			$accounts = $this->parseAccountList();
			return $this->getProfileList();
		}
	}
}
?>