include $_SERVER['DOCUMENT_ROOT'].'/classes/PHPExcel/PHPExcel.php';
include $_SERVER['DOCUMENT_ROOT'].'/classes/PHPExcel/PHPExcel/Reader/Excel2007.php';

$errors = array();

if ($_POST['acceptanduploaddata'] == 1)
{
		
		$q = "TRUNCATE TABLE Collection_400";
		$res = $dbh->query($q);
				
		$parentIDs = array();
		
		foreach ($_POST['importRow'] as $k => $v)
		{
				
				
				$addArray = array();
				
				$addArray['Collection_400_1'] = $_POST['PageName'][$v];
				$addArray['Collection_400_4'] = ucwords(strtolower($_POST['PageName'][$v]));
				$addArray['Collection_400_3'] = $_POST['pageActTemplate'][$v];
				
				$addArray['Collection_400_2'] = NULL;
				
				if (intval($_POST['ParentPageID'][$v]) > 0)
				{
							$addArray['Collection_400_2'] = $parentIDs[$_POST['ParentPageID'][$v]];
				}
				
				$ThisSitePageID = $api->insertCollectionRow_(400, $addArray , '' , '');
				
				$parentIDs[$v] = $ThisSitePageID;
		
		}//end foreach
		
		
		header('Location: /'.ADMIN_APP_DIR.'/admin.php?module_id=1&action=update&id=400');
		die('');
}

//we check to see if we have something to upload
if ($_POST['uploadexcelfile'] == 1)
{
		$error = $_FILES["excelfile"]["error"];
		
		
		
		if ($error == UPLOAD_ERR_OK ) {
		
					$fileName = $_FILES["excelfile"]["tmp_name"];
					
					$inputFileType = PHPExcel_IOFactory::identify($fileName);
					$Level = 0;
					$Counter = 0;
					$Stack = array();
					
					$q = "SELECT * FROM Collection_403 WHERE 1";			
					$res= $dbh->query($q);
					
					$Templates = array();
					while ($row = $res->fetchRow(MDB2_FETCHMDOE_ASSOC))
					{
							$Templates[$row['Collection_403_4']] = $row['Collection_403_ID'];
					}//end while
					
					
					if ($inputFileType == 'Excel2007')
					{
				
								$objReader = new PHPExcel_Reader_Excel2007();
								$objReader->setReadDataOnly(true);
								$objPHPExcel = $objReader->load($fileName);
								
								//$locatedSheet = $objPHPExcel->getSheetByName('ContentSheet');
								
								//get the first sheet and start looping through the results
								$ImportedData = array();
								
								$objWorksheet 		= $objPHPExcel->getActiveSheet();
								$highestRow 		= $objWorksheet->getHighestRow(); // e.g. 10
								$highestColumn 		= $objWorksheet->getHighestColumn(); // e.g 'F'
								$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn); // e.g. 5
								
								
								
								//we skpi the first row
								$firstRow = 2;
								
								for ($row = $firstRow; $row <= $highestRow; ++$row) {
			 
					 					$TheLevel = 0;
										$CellValue = '';
										while ($TheLevel <= $highestColumnIndex)
										{
											$CellValue = $objWorksheet->getCellByColumnAndRow($TheLevel, $row)->getValue();
											if ($CellValue != '')
											{
													break;
											}
											$TheLevel++;
										}
										
																			
										
										if ($CellValue != '' )
										{
											
																			
											$rowArray = array();
											$rowArray['PageName'] = $CellValue;
											
											//we need to search one more cell further to see if we have the type of page stored
											$tmpTheLevel = $TheLevel + 1;
											$PageType = $objWorksheet->getCellByColumnAndRow($tmpTheLevel, $row)->getValue();
											
											if ($PageType == '')
											{
													//we need to assign manually the level
													if ($TheLevel == 0)
													{
															$PageType = 'HOME';
													}
													else
													{
															if ($TheLevel == 1)
															{
																	$PageType = 'LANDER';
															}
															else
															{
																	$PageType = 'CONTENT';
															}
													}
											}//end if finding the page type
											
											$rowArray['Template'] = $PageType;
											
											if ($TheLevel == 1 || $TheLevel == 0)
											{
													$rowArray['ParentPageName'] = '';
													$TheLevel = 1;
											}
											else
											{
													$tmpTheLevel = $TheLevel - 1;
													
													$rowArray['ParentPage'] = $Stack[$tmpTheLevel];
													$rowArray['ParentPageName'] = $ImportedData[$Stack[$tmpTheLevel]]['PageName'];
											
											}//end if the level is 1 or 2
										
																					
										$ImportedData[$Counter] = $rowArray;
										
										//the next row can only have this item as the parent
										$Stack[$TheLevel] = $Counter;
										
										$Counter++;
										
										
										
										
										}//end if cell value is empty and we need to skip the row
										
								}//end loop through		
								
								
								
								if (count($ImportedData) == 0)
								{
									$errors[] = "There is nothing to import. Please check the excel file.";
								}
								else
								{
										foreach ($ImportedData as $k => $v)
										{
												//we need to check if we have a template
												$q = "SELECT * FROM Collection_403 WHERE Collection_403_4 = '". addslashes($v['Template']) . "'";
												$res = $dbh->query($q);
												
												if ($res->numRows() == 0)
												{
														$ImportedData[$k]['Errors'][] = 'This item does not have a valid template. The file should be edited and reuploaded.';
												}
											
										}
										
										$smarty->assign('ImportedData'		, $ImportedData);
										$smarty->assign('Templates'			, $Templates);
										
								}//end if we have something to import		
								
								
								
					}
					else
					{
							$errors[] = "The uploaded file type has to be in Excel2007 format.";
					}			//end if file type is not good
					
					
					
		
		
		}
		else
		{
				$errors[] = "There was a problem with the upload. Please try again.";
		
		}//end if upload error

}



$smarty->assign('errors',$errors);

$page = array (
			"PageTitle"    => "Initial Content Upload"
 		);
$smarty->assign("page",$page);
$smarty->display(ADMIN_APP_DIR.'/site-map-builder-macro.html');