<?php
class SITImportManagerFunctions {

    function finalizeImport($ISITDatabaseController, $exportPath, $returnArray) {

        return $returnArray;
    }

	/**
	 * 
	 */
	function sampleFunction($headerDataArray, $rowDataArray, $currentHeader, $currentValue, $ISITDatabaseController) {
	
// For database checks use		
// 		$resultArray = $ISITDatabaseController->readQuery("SELECT ...");
// 		foreach ($resultArray as $key => $resultRow) {
//			
// 		}
		
		return $currentValue . " edited";
	}
	
	
	/**
	 * Prepares category titles for import
	 * - Source: Category title
	 * - Target: oxcategories.oxtitle
	 */
	function checkCategoryTitle($headerDataArray, $rowDataArray, $currentHeader, $currentValue, $ISITDatabaseController) {
		
	}
	
	
	/**
	 * Prepares category titles for import
	 * - Source: category title or category id
	 * - Target: oxcategories.oxid
	 */
	function checkCategoryId($headerDataArray, $rowDataArray, $currentHeader, $currentValue, $ISITDatabaseController) {
		
		$categoryIdArray = array();
		
		// Get current category titles and create ids
		$categoryTitle = $this->checkCategoryTitle($headerDataArray, $rowDataArray, $currentHeader, $currentValue, $ISITDatabaseController);
		$categoryTitleArray = explode("|", $categoryTitle);
		foreach($categoryTitleArray as $key => $categoryTitle) {
			$categoryIdArray[] = md5($categoryTitle);
		}
	
		// TODO Get variant article id from row data
		$variantArticleId = $rowDataArray[$this->indexOfColumn($headerDataArray, "", $ISITDatabaseController)];
		
		// Create parent article id
		$parentArticleId = $this->createParentArticleId($headerDataArray, $rowDataArray, $currentHeader, $variantArticleId, $ISITDatabaseController);
		
		// Clear old category mappings
		$this->helperFunctionClearArticle2CategoryMapping($parentArticleId, $ISITDatabaseController, $rowDataArray);
		
		// Create article category mapping for parent
		foreach ($categoryIdArray as $key => $categoryId) {
			
			$this->helperFunctionCreateArticle2CategoryMapping($parentArticleId, $categoryId, $ISITDatabaseController);
		}
		
		return implode("|", $categoryIdArray);
	}
	
	
	/**
	 * Prepares attribute titles for import
	 * - Source: attribute title or attribute id
	 * - Target: oxattribute.oxid
	 */
	function checkAttributeId($headerDataArray, $rowDataArray, $currentHeader, $currentValue, $ISITDatabaseController) {
		
		// Get current attribute titles and create ids
		$attributeTitle = $currentHeader;
		$attributeId    = md5($attributeTitle);
	
		// TODO Get variant article id from row data
		$variantArticleId = $rowDataArray[$this->indexOfColumn($headerDataArray, "", $ISITDatabaseController)];
		
		// Create parent article id
		$parentArticleId = $this->createParentArticleId($headerDataArray, $rowDataArray, $currentHeader, $variantArticleId, $ISITDatabaseController);
		
		// Clear old categoty mappings
		$this->helperFunctionCreateArticle2AttributeMapping($parentArticleId, $attributeId, $currentValue, $ISITDatabaseController);
		
		return $attributeId;
	}
	
	
	/**
	 * Creates parent article from variant - if needed
	 * - Source: Any import column
	 * - Target: Same as source
	 */
	function checkExistingParentArticle($headerDataArray, $rowDataArray, $currentHeader, $currentValue, $ISITDatabaseController) {
		
		// TODO Get variant article id from row data
		$variantArticleId = $rowDataArray[$this->indexOfColumn($headerDataArray, "", $ISITDatabaseController)];
		
		// Create parent article id
		$parentArticleId = $this->createParentArticleId($headerDataArray, $rowDataArray, $currentHeader, $variantArticleId, $ISITDatabaseController);
		
		// Check existing parent article
		$resultArray = $ISITDatabaseController->readQuery("SELECT * FROM oxarticles WHERE oxid = '" . $ISITDatabaseController->escapeInputString($parentArticleId) . "'");
		if ( empty($resultArray) ) {
			
			// TODO Prepare article data from variant
			$articleTitle = $rowDataArray[$this->indexOfColumn($headerDataArray, "", $ISITDatabaseController)];
			
			// Create parent article
			$this->helperFunctionCreateArticle($parentArticleId, $articleTitle, $ISITDatabaseController);
		}
		
		return $currentValue;
	}
	
	
/** Helper functions */
	
	function indexOfColumn($headerDataArray, $columnName, $ISITDatabaseController) {
		
		$index = 0;
		foreach ($headerDataArray as $key => $currentColumn) {

			if ($currentColumn == $columnName) {
				return $index;
			}
			
			$index++;
		}
		
		return -1;
	}
	
	/**
	 * Creates parentid from variant
	 * - Source: "variant id" or "variant articlenumber"
	 * - Target: oxarticles.oxparentid
	 */
	function createParentArticleId($headerDataArray, $rowDataArray, $currentHeader, $currentValue, $ISITDatabaseController) {
		
		return substr($currentValue, 0, -4);
	}
	
	/**
	 * @param $articleId
	 * @param $attributeId
	 * @param $ISITDatabaseController
	 */
	function helperFunctionCreateArticle2AttributeMapping($articleId, $attributeId, $attributeValue, $ISITDatabaseController) {
		
		$oxid = md5(uniqid(mt_rand(), true));
		
		// Create article 
		$ISITDatabaseController->writeQuery(
			"REPLACE INTO " .
				"oxobject2attribute " .
			"(oxid, oxobjectid, oxattrid, oxvalue) " .  
			"VALUES " . 
				"('" . $oxid . "', '" . $articleId . "', '" . $attributeId . "', '" . $attributeValue . "')"
		);
	}
	
	/**
	 * @param $articleId
	 * @param $ISITDatabaseController
	 */
	function helperFunctionClearArticle2CategoryMapping($articleId, $ISITDatabaseController, $rowDataArray) {
		
		// Create article 
		$ISITDatabaseController->writeQuery(
			"DELETE FROM " .
				"oxobject2category " .
			"WHERE " . 
				"oxobjectid = '" . $articleId . "'"
		);
	}
	
	/**
	 * @param $articleId
	 * @param $categoryId
	 * @param $ISITDatabaseController
	 */
	function helperFunctionCreateArticle2CategoryMapping($articleId, $categoryId, $ISITDatabaseController) {
		
		$oxid = md5(uniqid(mt_rand(), true));
		
		// Create article 
		$ISITDatabaseController->writeQuery(
			"REPLACE INTO " .
				"oxobject2category " .
			"(oxid, oxobjectid, oxcatnid) " .  
			"VALUES " . 
				"('" . $oxid . "', '" . $articleId . "', '" . $categoryId . "')"
		);
	}
	
	/**
	 * Enhance with additional article data if needed
	 * 
	 * @param $articleId
	 * @param $articleTitle
	 */
	function helperFunctionCreateArticle($parentArticleId, $articleTitle, $ISITDatabaseController) {
		
		// Create article 
		$ISITDatabaseController->writeQuery(
			"REPLACE INTO " .
				"oxarticles " .
			"(oxid, oxtitle) " .  
			"VALUES " . 
				"('" . $parentArticleId . "', '" . $articleTitle . "')"
		);
		// Create article extends
		$ISITDatabaseController->writeQuery(
			"REPLACE INTO " .
				"oxartextends " .
			"(oxid) " .  
			"VALUES " . 
				"('" . $parentArticleId . "')"
		);
	}
}
?>