Location: PHPKode > projects > ChiCoMaS - Chilek CMS > chicomas/modules/structure/addnewpage_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 AddNewPageForm(){

	$content = null;

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

	$obj_pagetype = new CPageType();
	$obj_pagetypearray = new CPageTypeArray();
	$obj_pagetypeengine = new CPageTypeEngine();

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

	global $recursivecount;

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

	if (!isset($action)) $action="0";
	if (!isset($alias)) $alias="";
	if (!isset($name)) $name="";
	if (!isset($refid)) $refid="0";
	if (!isset($thisdescription)) $thisdescription="";
	if (!isset($thiskeywords)) $thiskeywords="";
	if (!isset($pagetypeid)) $pagetypeid="";

	$content = "
	<center>
	<form action='".basename($PHP_SELF)."?f=structure&sf=add' method='post'>
	<input type=\"hidden\" name=\"operation\" value=\"addnewpage\" />
	<fieldset>
	<img style=\"float:right;\" src=\"./images/icon48/new.png\" width=\"48\" height=\"48\" border=\"0\">
	<legend>"._CP_PA_NEW_PAGE_."</legend>
		<table cellpadding=\"2\" cellspacing=\"2\" border=\"0\">
		<tr>
			<td><label for=\"name\">"._CP_PA_NP_NAME_."</label></td>
			<td><input type=\"text\" name=\"name\" value=\"$name\" /></td>
		</tr>
		<tr>
			<td><label for=\"alias\">"._CP_PA_NP_ALIAS_."</label></td>
			<td><input type=\"text\" name=\"alias\" value=\"$alias\" /></td>
		</tr>
		<tr>
			<td><label for=\"thisdescription\">"._CP_PA_NP_DEFINITION_."</label></td>
			<td><input type=\"text\" name=\"thisdescription\" value=\"$thisdescription\" /></td>
		</tr>
		<tr>
			<td><label for=\"thiskeywords\">"._CP_PA_NP_KEYWORDS_."</label></td>
			<td><input type=\"text\" name=\"thiskeywords\" value=\"$thiskeywords\" /></td>
		</tr>
		<tr>
			<td><label for=\"refid\">"._CP_PA_NP_PARENT_OR_CHILD_."</label></td>
			<td>
				<select name=\"refid\" style=\"float:left;\">
					<option value=\"0\">"._CP_PA_NP_TOP_LEVEL_."</option>
					";
		
					$recursivecount = 0;
					$obj_pagearray = $obj_pageengine->GetParentPages(-1);
					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>

		<tr>
			<td><label for=\"active\">"._CP_PA_NP_STATUS_."</label></td>
			<td>
				<select name=\"active\" style=\"float:left;\">
					<option value=\"0\" style=\"background-color:gray;color:white\">"._CP_PA_NP_NOT_ACTIVE_."</option>
					<option value=\"1\">"._CP_PA_NP_ACTIVE_."</option>
				</select>
			</td>
		</tr>

		<tr>
			<td><label for=\"menuitem\">"._CP_PA_NP_VIEW_ON_MAINMENU_."</label></td>
			<td>
				<select name=\"menuitem\" style=\"float:left;\">
					<option value=\"0\">"._CMN_NO_."</option>
					<option value=\"1\">"._CMN_YES_."</option>
				</select>
			</td>
		</tr>

		<tr>
			<td><label for=\"topmenuitem\">"._CP_PA_NP_VIEW_ON_TOPMENU_."</label></td>
			<td>
				<select name=\"topmenuitem\" style=\"float:left;\">
					<option value=\"0\">"._CMN_NO_."</option>
					<option value=\"1\">"._CMN_YES_."</option>
				</select>
			</td>
		</tr>

		<tr>
			<td><label for=\"pagetypeid\">"._CP_PA_NP_TYPE_."</label></td>
			<td>
				<select name=\"pagetypeid\" style=\"float:left;\">";
						$obj_pagetypearray = $obj_pagetypeengine->GetPageTypes(1);
						while( $obj_pagetype = $obj_pagetypearray->get() ){
							$thispagetypeid = $obj_pagetype->page_type_id;
							$thispagetype = $obj_pagetype->page_type;
							$content .= "<option value=\"$thispagetypeid\">$thispagetype</option>";
						}
				$content .= "
				</select>
			</td>
		</tr>

		<tr>
			<td colspan=\"2\" align=\"center\">
				<br>";
				$content .= "<input type=\"submit\" value=\""._CMN_ADD_."\" style=\"float:center;\"/>";
				$content .= go_back();
				$content .= "
			</td>
		</tr>
		</table>
		<br>
	</fieldset>
	</form>
	</center>";

	return $content;
}

function AddNewPage($request){

	$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_language = new CLanguage();
	$obj_languagearray = new CLanguageArray();
	$obj_languageengine = new CLanguageEngine();

	$my_validator = new validator();

	$name = $request['name'];

	if (!$my_validator->validate_fields("name")) {
		//echo $my_validator->error;
		$failed = true;
	}
	else {
	
		$obj_page->ref_id = $request['refid'];
		$obj_page->name_old = $request['name'];
		$obj_page->description = $request['thisdescription'];
		$obj_page->keywords = $request['thiskeywords'];
		$obj_page->alias = $request['alias'];
		$obj_page->page_type_id = $request['pagetypeid'];
		$obj_page->menu_item = $request['menuitem'];
		$obj_page->topmenu_item = $request['topmenuitem'];
		$obj_page->active = $request['active'];
	
		$pageid = $obj_pageengine->InsertPage($obj_page);
	
		$obj_languagearray = $obj_languageengine->GetLanguages(0);
		$num_rows = $obj_languagearray->count;
	
		if ($num_rows!=0){
			while( $obj_language = $obj_languagearray->get() ){
				/* insert the values into the DB and read out its PageId */
				$obj_content->page_id = $pageid;
				$obj_content->lang = $obj_language->lang;
				$obj_content->context = $name;
				$content_id = $obj_contentengine->InsertContent($obj_content, "name");
	
				/* insert the values into the DB and read out its PageId */
				$obj_content->page_id = $pageid;
				$obj_content->lang = $obj_language->lang;
				$obj_content->context = ":)";
				$content_id = $obj_contentengine->InsertContent($obj_content, "content");
			}
		}
	
		/* Create the corresponding directories - root, images and files */
		$rootdir="./sections/$pageid";
		if (!file_exists($rootdir)){
			mkdir($rootdir, 0775);
			chmod($rootdir, 0775);
		}
	
		$imagedir="./sections/$pageid/images";
		if (!file_exists($imagedir)){
			mkdir($imagedir, 0775);
			chmod($imagedir, 0775);
		}
	
		$filedir="./sections/$pageid/files";
		if (!file_exists($filedir)){
			mkdir($filedir, 0775);
			chmod($filedir, 0775);
		}
	
		/* Addition of the re-direction to the .htaccess file, if necessary */
		if ($alias!=""){
			$handle=fopen("./.htaccess","a");
			fwrite($handle,"\n Redirect 301 /$alias $thisurl/index.php?pageid=$pageid");
	
			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_NP_COMPLETED_;
		}
		else{
			$content .= _CP_PA_NP_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 AddNewPage_Operation($request){

	$operation = $request['operation'];

	switch ($operation){
	default:
	case 'addnewpageform':
		$content = AddNewPageForm();
	break;
	case 'addnewpage':
		$content = AddNewPage($request);
	break;
	}
	return $content;
}
?>
Return current item: ChiCoMaS - Chilek CMS