Location: PHPKode > scripts > NabiCI > CI/website_builder.php
<?php
require_once realpath(dirname(__FILE__))."/".('../include/easy_file.php');
require_once realpath(dirname(__FILE__))."/".('../include/folders.php');

/**
 * class WebsiteBuilder is used to create the web pages, one to ovierview the result of the Loop with possibility 
 * to get more details with klicking at links and to run a Task separately, another with same structure adapt to running Loop which 
 * will be updated several times till finishing Loop pages for the Task details and for their results
 *
 * @author Derya Oez
 * @version 1.0
 * @package NabidooCI
 */


Class WebsiteBuilder
{
	/**
	 * This is the complete source code of the 'result page' or 'current page'
	 * 
	 * @access private
	 * @var String
	 */
	
	private $sourceCodeWebsite;
	
	/**
	 * The type of a website can be "Result" or "Current" depending on which type of page should be created for the Loop
	 * 
	 * @access private
	 * @var String
	 */
	
	private $websiteType;
	
	/**
	 * This variable contains an Object of Loop and it is used to get needed information with using the the defined methods of Loop
	 * 
	 * @access private
	 * @var Object of Loop
	 */
	
	private $loopObject;
	
	/**
	 * The revision number is returned after a project is checked out
	 * 
	 * @access private
	 * @var String
	 */
	
	private $revision= "Revision not yet detected";
	
	/**
	 * PHP-code for embedding in HTML-Code to calculate the expiration time
	 * 
	 * @access private
	 * @var String
	 */
	
	private $checkLimit;
	
	/**
	 * the end part of the HTML-Code for the web page
	 * 
	 * @access private
	 * @var String
	 */
	
	private $footer; 
	
	/**
	 * the end time of a Loop
	 * 
	 * @access private
	 * @var Integer
	 */
	
	private $loopEnd; 
	
	/**
	 * The part of the HTML-Code for create a Link to the Homepage of the Nabidoo project
	 * 
	 * @access private
	 * @var String
	 */
	
	private $linkNab="<a href=http://www.nabidoo.de target=_blank>made with NabiCI by Nabidoo</a>";
	
	/**
	 * The part of the HTML-Code for giving an overview of all Task
	 * 
	 * @access private
	 * @var String
	 */
	
	private $taskMsg;  

	/**
	 * function __construct(Loop $loop,$websiteType)
	 * initialize to create folder and web pages
	 *
	 * @access public
	 * @param Object $loop
	 * Object of Loop
	 * @param String $websiteType
	 * "Current" or "Result"
	 * necessary to differentiate between creating wep page for running and already finished Loop
	 */

	public function __construct(Loop $loop,$websiteType)
	{
		$this->loopObject = $loop;
		$this->websiteType = $websiteType;
		$this->createFolder();
		$this->startBuildWebsite();
	}

	
	/**
	 * function startBuildWebsite() prepares the first part for creating a web page, therefore is needed some values like
	 * revision number,end time of the Loop, all names of the Task, code for check the expiration time of the web page, title
	 * and use the method to finish the building of the website 
	 * 
	 * @access public
	 */


	public function startBuildWebsite()
	{
		// if the Task is "checkout" get the revision number,
		$this->checkRevisionNr();
		//get the end time of the Loop
		$this->loopEnd = $this->loopObject->getEndTime();
		//get all names of the Tasks of the Continuous integration Loop
		$this->taskMsg = " <br><tr><th>Overview All Tasks<br></th><td>".$this->taskInformation()."</td></tr>";
		//in case of finishing Loop
		if ($this->websiteType=="Result")
		{
			//$backRefreshLink: link to the 'currentpage' & link for refresh the web page
			$backRefreshLink = "<a href=\"../Current/Current.php\">Current Loop</a><br><br>
					            <a href=\"<? echo \$_SERVER[\"PHP_SELF\"] ?>\">Refresh</a>";
			//$this->checkLimit: checking if web page is up to date
			$this->checkLimit = "<?php
									\$timeout = false;
									\$expirationTime  =".$this->loopEnd."+".$this->loopObject->getDeadline()."; // Endzeit + Deadline								  							   
										   if(time()>\$expirationTime)
							 			   { 
								 			     \$timeout = true;
																						  
												 \$seconds = time() - \$expirationTime;  
					    										
												 \$days = floor(\$seconds/60/60/24);  
												 \$hours = \$seconds/60/60%24;  
												 \$mins = \$seconds/60%60;  
												 \$secs = \$seconds%60;  
															    
												 \$duration='';  
												 if(\$days>0) \$duration= \$duration.\$days.' d ';  
												 if(\$hours>0) \$duration= \$duration.\$hours.' h ';  
												 if(\$mins>0) \$duration= \$duration.\$mins.' min ';  
												 if(\$secs>0) \$duration= \$duration.\$secs.' s ';  
																    
												 \$duration = trim(\$duration);  
												 if(\$duration==null) \$duration = '0 s'; 				 							
				 						  }	
								 ?>";	
			/*$expiration_info: contains the part of code, where expiration time be wrote out
			 if the web page is not up to date (timeout), it will signalize with a cell in red color*/
			$expiration_info = "<tr><th>Expiration Time</th><td><?echo date('j-M-Y, H:i:s',\$expirationTime)?></td></tr>
						    	<? if (\$timeout){ ?>
						    	<tr><th style=\"background-color:red\">Expired since</th><td><?echo \$duration?></td></tr>
						    	<? } ?>";
			//$loop_title_info: depends on the status of the Loop process and gives a suitable title
			$loop_title_info = "Last Completed Loop ";
				
		}
		// if the Loop is not finish or in other words it should be created the 'currentpage'
		else{
			//$backRefreshLink: link to the 'resultpage' & link for refresh the web page
			$backRefreshLink = "<a href=\"../Result/Result.php\">Back to the Resultpage</a><br><br>
				 	    		<a href=\"<? echo \$_SERVER[\"PHP_SELF\"] ?>\">Refresh</a>";
			//$this->checkLimit: checking if 'currentpage' is up to date is not necessary, so it will never be time out
			$this->checkLimit= "<?php \$timeout = false;?>";
			//$expiration_info: String is empty because no time out
			$expiration_info = "";
			//$loop_title_info: depends on the status of the Loop process and gives a suitable title
			$loop_title_info = "Running Loop ";
				
		}
	 $this->finishBuildWebsite($backRefreshLink,$expiration_info,$loop_title_info);
	}

	/**
	 * function finishBuildWebsite()
	 *
	 * @access private
	 * @param String $backRefreshLink 
	 * Links to set at the web page
	 * @param String $expiration_info
	 * PHP code for embedding in HTML-Code to signalize the expiration of the validity of the web page
	 * @param String $loop_title_info 
	 * title at the web page
	 */

	private function finishBuildWebsite($backRefreshLink,$expiration_info,$loop_title_info)
	{
		//$header: contains the first part of source code for the web page with embedding php code [$this->checkLimit]
		$header = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
							<html xmlns=\"http://www.w3.org/1999/xhtml\">
							<head>
							<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8889-1\" />
							<title>NabiCI Continuous Integration Result Page</title>
							<link rel=\"stylesheet\" type=\"text/css\" href=\"../../css/styles.css\">
							</head>
							".$this->checkLimit.
							"<body>";

		//$footer: contains the end part of the html-code
		$this->footer = "	</body>
							</html>";

		//in case of the current loop is finished it will be created a new web page to give this information
		if ($this->websiteType=="Current" and $this->loopEnd)
		{
			//html Code for the 'currentpage' - no loop is running
			$this->sourceCodeWebsite=$header."<h1 align=center> Sorry, no loop is running at the moment ...</h1>
											 <table width = 100%>
												<tr class=undecorated>
												 <td><div align=\"left\">".$backRefreshLink."</td>
												 <td><div align=\"right\">".$this->linkNab."</td>
											  	</tr>
											 </table>".$this->footer;
		}
		//creating web pages for running or finish Loop
		else{
				$this->sourceCodeWebsite = 	$header.
											$this->statusBar().
											"<table width = 100%>
											   <tr class=undecorated>
											 	<td width=50%><div align=\"left\">".$backRefreshLink."</td>
											 	<td width=50%><div align=\"right\">".$this->linkNab."</td>
											   </tr>
											 </table>
											  
											<table>
											<colgroup>
												<col width=20%></col>
												<col width=80%></col>
											</colgroup>
												<tr class=undecorated>
												 <td></td>
												 <td align=center><h1>".$loop_title_info." of Build ".$this->revision."</h1></td>
												</tr>
												<tr class=\"undecorated\" valign=top>
												 <td>
													<table>
													  <tr><th>Loop </th><td>".$loopName= $this->loopObject->getName()."</td></tr>
													  <tr><th> Begin</th><td>".date('j-M-Y, H:i:s',$this->loopObject->getStartTime())."</td></tr>
													  <tr><th>End</th> <td>".$this->beautydate($this->loopEnd)."</td></tr>
													  <tr><th>Duration</th><td>".$this->loopObject->getTimeDuration()."</td></tr>
													  ".$expiration_info.$this->taskMsg."
													</table>
												 </td>
												 <td>
													 <table align=\"center\">
													  <tr>
													      <th>Task</td>
													      <th>Begin</td>
													      <th>End</td>
													      <th>Duration</td>
													      <th>Result</td>
													      <th>Run Task</td>  
												    </tr>";
			$this->tasksTableRows();
			$this->sourceCodeWebsite=$this->sourceCodeWebsite."</table></td></tr>
																	</table>"
																	.$this->footer;
		}
		//create the file 'Result.php' or 'Current.php' and save it
		$filename="ci_web_pages/".$this->websiteType."/".$this->websiteType.".php";
		easy_file::write($filename,$this->sourceCodeWebsite);
	}
	
	/**
	 * function tasksTableRows() is used 
	 * to generate the table rows, to prepare and to create the -Task detail page- and -Task result page-,
	 * all in depending on the number of the Tasks
	 *
	 * @access private
	 */

	private function tasksTableRows()
	{
		$row=""; //table row
		$i = 0; 
		
		foreach ($this->loopObject->getTasks() as $task)
		{
			$taskName=$task->getName(); //Task name
			$taskStartTime=$task->getStartTime(); //Task start time
			$taskEndTime=$task->getEndTime(); // Task end time
			$taskTimeDuration=$task->getTimeDuration(); //Task time duration
			$taskMode=$task->getTaskMode(); //Task mode
			$expResult=$task->getExpResult(); // Task expected result
			$command=$task->getCommand(); //Task command
			
			//path to the web page of task details
			$taskDetailsLink= $this->websiteType."/Task_Details/".$i."_details_of_".$taskName.".php";
			//path to the web page of task result
			$taskResultLink = $this->websiteType."/Task_Result/".$i."_result_of_".$taskName.".php";
			

			//table row contains Task information which is given out at the 'result page' or 'current page'
			$row = $row."<tr>
						    <td><a href=../$taskDetailsLink>".$taskName."</a></td>
						    <td>".$this->beautydate($taskStartTime)."</td>
						    <td>".$this->beautydate($taskEndTime)."</td>
						    <td>".$taskTimeDuration."</td>
						    <td><a href=../$taskResultLink>".$task->getMessage()."</a></td>
						    <td><a href=\"../../CI/Task/Task.php?loopStatus=".$this->websiteType."&taskName=".$taskName."&taskMode=".$taskMode."&taskExpRes=".$expResult."&command=".$command."\" target=\"_blank\" rel=\"nofollow\">".$taskName."</a></td>
						</tr>
					  ";
			
			//check if Task is successful or not
			$successTemp="";
			if($task->getSuccessful()==true)
			{	//String in green color
				$successTemp = "<font color=green>success</font>";
			}else{
				//String in red color
				$successTemp = "<font color=red>failed</font>";
			}
			//prepare source code for presenting the -details of a Task- in a table
			$taskDetails = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
							<html xmlns=\"http://www.w3.org/1999/xhtml\">
							<head>
							<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8889-1\" />
							<title>NabiCI Continuous Integration Task Attribute Page</title>
							<link rel=\"stylesheet\" type=\"text/css\" href=\"../../../css/styles.css\">
							</head>
							<body>
							<table width=100%  border=\"0\" >
							 <tr class=undecorated>
							  <td width=20%><div align=\"left\"><p><a href=\"../".$this->websiteType.".php\">back to the ".$this->websiteType."page</a></p></div></td>
							  <td width=60%><div align=\"center\"><h1>Details On Task ".$taskName."</h1></div></td>
							  <td width=20%><div align=\"right\">".$this->linkNab."</td>
							</tr>
							</table>
							<div align=\"center\">
							  <table border=\"0\">
							    <tr>
							      <th align=left>Task Attributes</th>
							      <th>Task Attribute Values</th>
							    </tr align=left>
							    <tr>
							      <td align=left>Begin</td>
							      <td align=left width=\"205\">".date('j-M-Y, H:i:s',$taskStartTime)."</td>
							    </tr>
							    <tr>
							      <td align=left>End</td>
							      <td align=left>".date('j-M-Y, H:i:s',$taskEndTime)."</td>
							    </tr>
							    <tr>
							      <td align=left>Duration</td>
							      <td align=left>".$taskTimeDuration."</td>
							    </tr>
							    <tr>
							      <td align=left>Task Result</td>
							      <td align=left>".$successTemp."</td>
							    </tr>
							    <tr>
							      <td align=left>Task Mode</td>
							      <td align=left>".$taskMode."</td>
							    </tr>
							    <tr>
							      <td align=left>Command</td>
							      <td align=left>".$command."</td>
							    </tr>
							    <tr>
							      <td align=left>Expected Result</td>
							      <td align=left>".$expResult."</td>
							    </tr>
							  </table>
							 </div>
						  ".$this->footer;
			
			//prepare source code for presenting the -result of a Task-
			$resultHeader= "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
							<html xmlns=\"http://www.w3.org/1999/xhtml\">
							<head>
							<meta http-equiv=\"Content-Type\" content=\"text/html; charset=windows-1250\" />
							<title>NabiCI Continuous Integration Task Result Page</title>
							<link rel=\"stylesheet\" type=\"text/css\" href=\"../../../css/styles.css\">
							</head>
							<body>";

			//$task_result_file: path to the 'task result file'
			$task_result_file = $i."_result_of_".$taskName.".html";
			//$taskRes: complete code for -task result web page- the result will be presented in a frame
			$taskRes = $resultHeader."
									 <table width=100%  border=\"0\" >
									    <tr class=undecorated>
											<td width=20%><div align=\"left\"><a href=\"../".$this->websiteType.".php\"> back to the ".$this->websiteType."page</a></div></td>
											<td width=60%><h1 align=center> This is the result of task ".$taskName."</h1></div></td>
											<td width=20%><div align=\"right\">".$this->linkNab."</td>
										</tr>
								   	 </table>
										<div align=\"center\">
										<table width=98% border=\"0\">
										<tr>
										   <iframe src=\"".$task_result_file."\"  name=\"notSingle_result\" width=\"100%\" height=\"500\"></iframe>
									 	   </iframe>
										</tr>"
										.$this->footer;
													
			$taskResultHtmlFileName = "ci_web_pages/".$this->websiteType."/Task_Result/".$task_result_file;
			//create the html-file contains the -result of the task- and save it
			easy_file::write($taskResultHtmlFileName,$task->getResult());
			
			$taskResultPhpFileName = "ci_web_pages/".$taskResultLink;
			//create the php-file contains the html-code of the Task result web page and save it
			easy_file::write($taskResultPhpFileName,$taskRes);
			
			$taskDetailsFileName = "ci_web_pages/".$taskDetailsLink;
			//create the php-file contains the -details of the task- and save it
			easy_file::write($taskDetailsFileName,$taskDetails);
			

			$i++;
		}
		$this->sourceCodeWebsite = $this->sourceCodeWebsite.$row;
	}
	
	/**
	 * function beautydate($inp) is used to check, if the time is fixed,
	 * the return value makes the presentation at the web page user-friendly
	 * 
	 * @access private
	 * @param Integer $inp 
	 * unformatted time in seconds
	 * @return String
	 */
	
	private function beautydate($inp){
		if ($inp==0){
			return "running";
		}else{
			return date('j-M-Y, H:i:s',$inp);
		}
	}
	
	/**
	 * function createFolder() is used to create specific folder to save the created web pages into it,
	 * addition to this, the content of the already exist folders will be deleted before new web pages will be created 
	 * for the new running Loop.
	 * 
	 * @access private
	 */

	private function createFolder()
	{	
		$topFolder = "../ci_web_pages/";
		$subFolderCurrent = $topFolder."Current/";
		$subFolderResult = $topFolder."Result/";
		
		//$folderArray: the folder to create
		$folderArray=array( 
		realpath(dirname(__FILE__))."/".$topFolder,
		realpath(dirname(__FILE__))."/".$subFolderCurrent,
		realpath(dirname(__FILE__))."/".$subFolderCurrent.'Task_Details',
		realpath(dirname(__FILE__))."/".$subFolderCurrent.'Task_Result',
		realpath(dirname(__FILE__))."/".$subFolderCurrent.'Separate_Task_Result',
		realpath(dirname(__FILE__))."/".$subFolderResult,
		realpath(dirname(__FILE__))."/".$subFolderResult.'Task_Details',
		realpath(dirname(__FILE__))."/".$subFolderResult.'Task_Result',
		realpath(dirname(__FILE__))."/".$subFolderResult.'Separate_Task_Result');

		foreach ($folderArray as $folder)
		{
		 //if the folder does not exist
			if(!file_exists($folder))
			{	//create this folder
				easy_folder::create($folder);
			}
		}
		//depending on the type of the website, the content of the folders will be deleted
		if ($this->websiteType=='Current')
		{
			$loopCurrentFolder=array( 
			realpath(dirname(__FILE__))."/".$subFolderCurrent.'Task_Details',
			realpath(dirname(__FILE__))."/".$subFolderCurrent.'Task_Result',
			realpath(dirname(__FILE__))."/".$subFolderCurrent.'Separate_Task_Result');

			$this->deleteFolderContent($loopCurrentFolder);
		}else
		{
			$loopResultFolder=array( 
			realpath(dirname(__FILE__))."/".$subFolderResult.'Task_Details',
			realpath(dirname(__FILE__))."/".$subFolderResult.'Task_Result',
			realpath(dirname(__FILE__))."/".$subFolderResult.'Separate_Task_Result');

			$this->deleteFolderContent($loopResultFolder);
		}
	}
	
	/**
	 * function deleteFolderContent($loopFolder) is used to delete the content of the folder
	 * 
	 * @access private
	 * @param Array $loopFolder
	 * folder to delete the content
	 */

	private function deleteFolderContent($loopFolder)
	{
		$folderArray=array();
		$folderArray=$loopFolder;

		foreach ($folderArray as $folder)
		{
			//$file: all files in the folder as an array
			$file=scandir($folder,1);

			//delete the files one after another
			for ($i=0;$i<count($file);$i++)
			{
				//	if the value is not . or .. 
				if ( $file[$i] != "." && $file[$i] != "..")
					
				{	//delete this file
					easy_file::delete($folder.'/'.$file[$i]);
					// echo of the deleted file
					echo $folder."/".$file[$i]." deleted \n"."<br>";
				}
			}
		}
	}
	
	/**
	 * function statusBar() is used to check wether the Loop is successful, depending on this, 
	 * the method returns the part of a source code, which will present a bar at the web page to show the status of the Loop,
	 * the source code include also a possibility to check if time out or not and set depending on it the color and the message
	 * 
	 * @access private
	 * @return String 
	 * code for the bar
	 * 
	 */

	private function statusBar()
	{	
		//define the 'error color'
		$error_color = "red";
		//define the 'no error color'
		$no_error_color = "green";
		//define the 'error message'
		$error_message = "<a href=#NOK style=color:white;text-decoration:none>ERROR - ERROR - ERROR - ERROR</a>";
		//define the 'no error message'
		$no_error_message = "No Error";
		//$color_code: code to check the time and set the right color
		$color_code = "<?if (\$timeout){echo \"$error_color\";}else{echo \"$no_error_color\";}?>";
		//$msg_code: code to check the time and set the right message
		$msg_code = "<?if (\$timeout){echo \"$error_message\";}else{echo \"$no_error_message\";}?>";
		//$success_status_bar: code to show the 'success bar'
		$success_status_bar = "<h2 style=\"background-color:".$color_code.";color: white; text-align: center;\">$msg_code</h2>";
		//$no_success_status_bar: code to show the 'no success bar'
		$no_success_status_bar = "<h2 style=\"background-color:".$error_color." ; color: white; text-align: center;\">$error_message</h2>";
		
		//checking the status of the Loop and returns the suitable code for embedding in the web page
		if ($this->loopObject->getSuccessful()==true)
		{
			$statusBar = $success_status_bar;
		}else{
			$statusBar = $no_success_status_bar;
		}
		return $statusBar;
	}
	
	/**
	 * function checkRevisionNr() is used to set the revision number of a project which is checked out
	 *  
	 * @access private
	 */

	private function checkRevisionNr()
	{
		foreach ($this->loopObject->getTasks() as $task)
		{
			if($task->getName()== checkout_task)
			{
				$this->revision = $task->getRevision();
			}
		}
	}
	
	/**
	 * function taskInformation() is used to get a list of all tasks names of the running Loop
	 *   
	 * @access private
	 * @return String 
	 * all task names
	 */

	private function taskInformation()
	{
		$taskname="";
		$taskNames= $this->loopObject->getAllTaskNames();
		$tasks = explode(" ",$taskNames);
		$taskname=implode("<br>",$tasks);
		return $taskname;
	}

}
?>
Return current item: NabiCI