Location: PHPKode > projects > ChiCoMaS - Chilek CMS > chicomas/modules/structure/deletepage_operation.php
<?
function RecursivePageSelect($pageid, $recursivecount){
	$content = null;

	$obj_page = new CPage();
	$obj_pagearray = new CPageArray();
	$obj_pageengine = new CPageEngine();

	$obj_content = new CContent();
	$obj_contentarray = new CContentArray();
	$obj_contentengine = new CContentEngine();


	$obj_pagearray = $obj_pageengine->GetChildPages($pageid, -1);
	$num_rows = $obj_pagearray->count();

	if ($num_rows!=0){
		$recursivecount++;
		for ( $counter = 1; $counter <= $recursivecount; $counter++)
			$braket = $braket."|--";
		while( $obj_page = $obj_pagearray->get() ){
			$thispageid=$obj_page->page_id;
			$thisactive=$obj_page->active;
			$obj_content = $obj_contentengine->GetContent(ActiveLanguage(), $thispageid, "name");
			$thisname = $obj_content->context;
			if ($thisactive==0)
				$activepage="style=\"background-color:gray;color:white\"";
			else
				$activepage="";

			$content .= "<option value=\"$thispageid\" $activepage>";
			$content .= "$braket";
			$content .= "$thisname</option>";;
			$content .= RecursivePageSelect($thispageid, $recursivecount);
		}
	}
	else{
		$content = null;
		$recursivecount--;
		$braket = null;
	}

	return $content;
}

function RecursivePageList($pageid, $recursivecount){
	$content = null;

	$obj_page = new CPage();
	$obj_pagearray = new CPageArray();
	$obj_pageengine = new CPageEngine();

	$obj_content = new CContent();
	$obj_contentarray = new CContentArray();
	$obj_contentengine = new CContentEngine();


	$obj_pagearray = $obj_pageengine->GetChildPages($pageid, -1);
	$num_rows = $obj_pagearray->count();

	if ($num_rows!=0){
		$recursivecount++;
		for ( $counter = 1; $counter <= $recursivecount; $counter++)
			$braket = $braket."|-- ";
		while( $obj_page = $obj_pagearray->get() ){
			$thispageid=$obj_page->page_id;
			$obj_content = $obj_contentengine->GetContent(ActiveLanguage(), $thispageid, "name");
			$thisname = $obj_content->context;
			$content .= "<b style=\"color:red\">$braket $thisname</b><br />";
			//echo "thisname(id):$thisname($thispageid)<br>";
			$content .= RecursivePageList($thispageid, $recursivecount);
		}
	}
	else{
		$content = null;
		$recursivecount--;
		$braket = null;
	}

	return $content;
}

function RecursivePageDelete($pageid, $recursivecount){
	$content = null;

	$obj_page = new CPage();
	$obj_pagearray = new CPageArray();
	$obj_pageengine = new CPageEngine();

	$obj_content = new CContent();
	$obj_contentarray = new CContentArray();
	$obj_contentengine = new CContentEngine();

	$obj_pagearray = $obj_pageengine->GetChildPages($pageid, -1);
	$num_rows = $obj_pagearray->count();

	if ($num_rows!=0){
		$recursivecount++;
		for ( $counter = 1; $counter <= $recursivecount; $counter++)
			$braket = $braket."|-- ";
		while( $obj_page = $obj_pagearray->get() ){
			$thispageid=$obj_page->page_id;
			$thisalias = $obj_page->alias;

			$obj_content = $obj_contentengine->GetContent(ActiveLanguage(), $thispageid, "name");
			$thisname = $obj_content->context;

			/* Step1 - remove the content(s) subsections from the DB */
			if (!$obj_contentengine->DeleteContent($thispageid))
				echo "Delete error on Content Engine<br>";

			/* Step2 - remove the pages(s) subsections from the DB */
			if (!$obj_pageengine->DeletePage($thispageid))
				echo "Delete error on Page Engine<br>";
	
			/* Step3 - remove the files, images and directory */
			/* subsection(s) directory images and files */	
			$imagedir="./sections/$thispageid/images";
		
			$dh=opendir($imagedir);
			while ($file=readdir($dh)){
				if ($file!="." && $file!="..")
					unlink("$imagedir/$file");
			}
			closedir($dh);
			rmdir($imagedir);
			
			$filedir="./sections/$thispageid/files";
			$dh=opendir($filedir);
			while ($file=readdir($dh)){
				if ($file!="." && $file!="..")
					unlink("$filedir/$file");
			}
			closedir($dh);
			rmdir($filedir);
	
			$rootdir="./sections/$thispageid";
			$dh=opendir($rootdir);
			while ($file=readdir($dh)){
				if ($file!="." && $file!="..")
					unlink("$rootdir/$file");
			}
			closedir($dh);
			rmdir($rootdir);

			if ($thisalias!=""){
				$file = "./.htaccess";
				$file_cont=file_get_contents($file);
				$file_cont_new=str_replace("\n Redirect 301 /$thisalias http://www.cilekyazilim.com/index.php?pageid=$thispageid","",$file_cont);
				$handle=fopen($file,"w");
				fwrite($handle, $file_cont_new);
				fclose($handle);
			}

			$content .= RecursivePageDelete($thispageid, $recursivecount);
		}
	}
	else{
		$content = null;
		$recursivecount--;
		$braket = null;
	}

	return $content;
}

function DeletePageSelect(){
	$content = null;

	$obj_page = new CPage();
	$obj_pagearray = new CPageArray();
	$obj_pageengine = new CPageEngine();

	$obj_subpage = new CPage();
	$obj_subpagearray = new CPageArray();
	$obj_subpageengine = new CPageEngine();

	$obj_content = new CContent();
	$obj_contentarray = new CContentArray();
	$obj_contentengine = new CContentEngine();

	global $recursivecount;

	if (!isset($f)) $f="structure";
	if (!isset($sf)) $sf="delete";

	$recursivecount = 0;
	$obj_pagearray = $obj_pageengine->GetParentPages(-1);
	$num_rows = $obj_pagearray->count();

	if ($num_rows!=0){
		$content = "
		<center>
		<form action='".basename($PHP_SELF)."?f=structure&sf=delete' method='post'>
		<input type=\"hidden\" name=\"operation\" value=\"deletepageconfirm\" />
		<fieldset>
		<img style=\"float:right;\" src=\"./images/icon48/delete.png\" width=\"48\" height=\"48\" border=\"0\">
		<legend>"._CP_PA_DELETE_PAGE_."</legend>
			<table cellpadding=\"2\" cellspacing=\"2\" border=\"0\">
			<tr>
				<td align=\"left\">"._CP_PA_DP_SELECT_PAGE_."</td>
				<td align=\"left\">
					<select name=\"pageid\">
					";
					while( $obj_page = $obj_pagearray->get() ){
						$thisrefid = $obj_page->page_id;
						$thisactive = $obj_page->active;
						$obj_content = $obj_contentengine->GetContent(ActiveLanguage(), $thisrefid, "name");
						$thisname = $obj_content->context;
						if ($thisrefid==$refid)
							$selected="selected=\"selected\"";
						else
							$selected="";
		
						if ($thisactive==0)
							$activepage="style=\"background-color:gray;color:white\"";
						else
							$activepage="";
		
						$content .= "<option $selected $activepage value=\"$thisrefid\">$thisname</option>";
		
						$recursivecount = 0;
						$content .= RecursivePageSelect($thisrefid, $recursivecount);
					}
					$content .= "
					</select>
				</td>
			<tr>
				<td colspan=\"2\" align=\"center\">
					<br>";
					$content .= "<input type=\"submit\" value=\""._CMN_SELECT_."\" style=\"float:center;\"/>";
					$content .= go_back();
					$content .= "
				</td>
			</tr>";
			$content .= "
			</table>
			<br>
		</fieldset>
		</form>
		</center>";
	}
	else{
		$content = "
		<div class=note>
			<h3>"._CMN_NOTE_."</h3>
			<p>";
			$content .= "<img src=\"./images/icon48/warning.png\" alt=\"\" width=\"48\" height=\"48\" border=\"0\">";
			$content .= "<br><br>";
			$content .= _CP_NO_PAGE_FOUND_;
			$content .= "<br><br>";
			$content .= go_back();
			$content .= "
			</p>
		</div>
		";
	}

	return $content;
}

function DeletePageConfirm($pageid){
	$content = null;

	$obj_page = new CPage();
	$obj_pagearray = new CPageArray();
	$obj_pageengine = new CPageEngine();

	$obj_subpage = new CPage();
	$obj_subpagearray = new CPageArray();
	$obj_subpageengine = new CPageEngine();

	$obj_content = new CContent();
	$obj_contentarray = new CContentArray();
	$obj_contentengine = new CContentEngine();

	global $recursivecount;

	if (!isset($f)) $f="structure";
	if (!isset($sf)) $sf="delete";

	$content = "
	<center>
	<form action='".basename($PHP_SELF)."?f=structure&sf=delete' method='post'>
	<input type=\"hidden\" name=\"pageid\" value=\"$pageid\" />
	<input type=\"hidden\" name=\"operation\" value=\"deletepage\" />
	<fieldset>
	<img style=\"float:right;\" src=\"./images/icon48/delete.png\" width=\"48\" height=\"48\" border=\"0\">
	<legend>"._CP_PA_DELETE_PAGE_."</legend>
		<table cellpadding=\"2\" cellspacing=\"2\" border=\"0\">
		<tr>
			<td align=\"left\">
				"._CP_PA_DP_ARE_YOU_SURE_."<br><br>"._CP_PA_DP_SELECT_PAGE_."
			</td>
		</tr>
		<tr>
			<td align=\"left\">";
				$obj_page = $obj_pageengine->GetPage($pageid);
				$refid = $obj_page->ref_id;
	
				$obj_content = $obj_contentengine->GetContent(ActiveLanguage(), $pageid, "name");
				$name = $obj_content->context;
				$content .= "<b>$name</b><br />";
				$recursivecount = 0;
				$content .= RecursivePageList($pageid, $recursivecount);
				$content .= "
			</td>
		</tr>
		<tr>
			<td align=\"center\">
				<br>
				<input type=\"submit\" value=\""._CP_PA_DP_I_AM_SURE_."\" />
				".go_back()."
			</td>
		</tr>
		</table>
		<br>
	</fieldset>
	</form>
	</center>";

	return $content;
}

function DeletePage($request){
	$content = null;

	$obj_page = new CPage();
	$obj_pagearray = new CPageArray();
	$obj_pageengine = new CPageEngine();

	$obj_subpage = new CPage();
	$obj_subpagearray = new CPageArray();
	$obj_subpageengine = new CPageEngine();

	$obj_content = new CContent();
	$obj_contentarray = new CContentArray();
	$obj_contentengine = new CContentEngine();

	global $recursivecount;

	if (!isset($f)) $f="structure";
	if (!isset($sf)) $sf="delete";

	$pageid = $request['pageid'];

	/* Step0 - read the Alias from the DB, to remove the re-direction from the .htaccess file */
	$obj_page = $obj_pageengine->GetPage($pageid);
	$alias = $obj_page->alias;

	$content .= RecursivePageDelete($pageid, $recursivecount);

	/* Step4 - remove the content(s) sections from the DB */
	if (!$obj_contentengine->DeleteContent($pageid))
				echo "Delete error on Content Engine<br>";
	
	/* Step5 - remove the section(s) from the DB */
	if (!$obj_pageengine->DeletePage($pageid))
		echo "Delete error on Page Engine<br>";
	
	/* ... and the subsections */
	if (!$obj_pageengine->DeletePageByRef($pageid))
		echo "Delete error on Page Engine<br>";
	
	
	/* Step6 - remove the directory */
	$imagedir="./sections/$pageid/images";
	
	$dh=opendir($imagedir);
	while ($file=readdir($dh)){
		if ($file!="." && $file!="..")
			unlink("$imagedir/$file");
	}
	closedir($dh);
	rmdir($imagedir);
	
	$filedir="./sections/$pageid/files";
	$dh=opendir($filedir);
	while ($file=readdir($dh)){
		if ($file!="." && $file!="..")
			unlink("$filedir/$file");
	}
	closedir($dh);
	rmdir($filedir);
	
	$rootdir="./sections/$pageid";
	$dh=opendir($rootdir);
	while ($file=readdir($dh)){
		if ($file!="." && $file!="..")
			unlink("$rootdir/$file");
	}
	closedir($dh);
	rmdir($rootdir);
	
	
	/* Step7 - remove the redirection from the .htaccess file */
	if ($alias!=""){
		$file = "./.htaccess";
		$file_cont=file_get_contents($file);
		$file_cont_new=str_replace("\n Redirect 301 /$alias $thisurl/index.php?pageid=$pageid","",$file_cont);
		$handle=fopen($file,"w");
		fwrite($handle, $file_cont_new);
		fclose($handle);
	}
	
	$content .= "
	<div class=note>
		<h3>"._CMN_NOTE_."</h3>
		<p>";
		if (!$failed){
			$content .= "<img src=\"./images/icon48/info.png\" alt=\"\" width=\"48\" height=\"48\" border=\"0\">";
			$content .= "<br><br>";
			$content .= _CP_PA_DP_COMPLETED_;
		}
		else{
			$content .= _CP_PA_DP_ERROR_."
			<center>";
			$content .= "<img src=\"./images/icon48/warning.png\" alt=\"\" width=\"48\" height=\"48\" border=\"0\">";
			$content .= "<br><br>";
			$content .= go_back();
		}
		$content .= "
		</p>
	</div>
	";

	return $content;
}

function DeletePage_Operation($request){
	$content = null;

	$pageid = $request['pageid'];
	$operation = $request['operation'];

	switch ($operation){
	default:
	case 'deletepageselect':
		$content = DeletePageSelect();
	break;
	case 'deletepageconfirm':
		$content = DeletePageConfirm($pageid);
	break;
	case 'deletepage':
		$content = DeletePage($request);
	break;
	}

	return $content;
}


?>
Return current item: ChiCoMaS - Chilek CMS