<?php
if ((strpos($_SERVER['HTTP_HOST'],'pilotwebs.com') !== false)||(strpos($_SERVER['HTTP_HOST'],'restaurant-online.biz') !== false)) {
	//site is being viewed in design mode, not at final vanity domain
		$mode = "design";
	} else {
		$mode = "live";
	}
//include ('../custom/site_options.php');

	
if ($_SESSION['FirstName'] == "") {
	echo 'You are not authorized to view this widget. <br><br>You may <a href="index.php">log in here.</a>';
	exit;
}
	//include_once 'ckeditor/ckeditor.php' ;
	//require_once 'ckfinder/ckfinder.php' ;
?>
<!--<script type="text/javascript" src="tinymce/jscripts/tiny_mce/tiny_mce.js"></script>-->
<script type="text/javascript" src="js/jscolor/jscolor.js"></script>


<script type="text/javascript" src="ckeditor/ckeditor.js"></script>
<script type="text/javascript" src="ckfinder/ckfinder_v1.js"></script>


<?php
 //require_once ('mysql_connect.php');
$action          = $_GET["a"];
$editwidget          = $_POST["widget"];
if(empty($editwidget)){
$editwidget          = $_GET["key"];
}
$widgettype = $_POST["widgettype"];

if (!empty($_POST["chkDelete"])) {
     $query = "delete from widgets WHERE recordKey = '" . $_GET['key'] ."'";
     mysql_query($query);
     echo "<font color=red>Widget deleted successfully.</font>";
     unset($action);
}
	//echo 'Action = ' . $action; (for testing)
if ($action == "add") {         
    $query = "Insert into widgets (title) values('" . mysql_real_escape_string($_POST["newwidget"]) . "')";
    mysql_query($query);
	
    echo "<font color=red>Widget added successfully.</font>";
	$editwidget=mysql_insert_id();
}


                      //******************make any updates
if ($action == "update") {
		$query = "UPDATE widgets SET title = '" . mysql_real_escape_string($_POST['title']) . "',  body = '" . mysql_real_escape_string($_POST['widgetbody']) . "',  color = '"
    . strtoupper($_POST['color']) . "'";
	//echo $query;
    if(!empty($_POST['chkActive'])){
      $query .= ", active ='1'";
      }else{
        //if($_POST['widgetname']!=="HOME"){  //can only turn to inactive if not the main home widget
            $query .= ", active ='0'";
      //}
    }
	 if(!empty($_POST['chknoborder'])){
      $query .= ", noborder ='1'";
      }else{
        //if($_POST['widgetname']!=="HOME"){  //can only turn to inactive if not the main home widget
            $query .= ", noborder ='0'";
      //}
    }
	 if($_POST['widget_padding']=='1'){
      $query .= ", no_padding ='1'";
      }else{
        //if($_POST['widgetname']!=="HOME"){  //can only turn to inactive if not the main home widget
            $query .= ", no_padding ='0'";
      //}
    }
	 if(!empty($_POST['chknocolor'])){
      $query .= ", nocolor ='1'";
      }else{
        //if($_POST['widgetname']!=="HOME"){  //can only turn to inactive if not the main home widget
            $query .= ", nocolor ='0'";
      //}
    }
			if(!empty($_POST['formnumber'])){
				$query .= ", formNumber ='".$_POST['formnumber'] ."'";
			}else{
				$query .= ", formNumber ='0'";
			}
		$query = $query . " WHERE recordkey = '" . $_GET['key'] ."'" ;
		//echo $query;
		mysql_query($query) or die(mysql_error());   //execute query

		//confirmation message that update worked...
		echo "<font color=red>widget content updated successfully.</font>";

	}	 //*******************end of update query

		

	echo '<div align="left"><form action="' . $_SERVER['PHP_SELF'] . '?a=add&admin=widgets" method="post">Add new widget: <br><input type="text" name="newwidget" size=38>
	<input type="submit" name="save" id="save-button" value="Add widget">
	</form>';
	
	
 //create selector to choose page to edit
 echo '<form action="' . $_SERVER['PHP_SELF'] . '?a=select&admin=widgets" method="post">';
 echo'Or choose widget to edit: <br><select name="widget">';
 $pages="Select recordkey, title from widgets";
 $pg=mysql_query($pages);
  while($pnt=mysql_fetch_array($pg)){
   echo '<option value='. $pnt[recordkey];
   if($pnt[recordkey]==$editwidget){echo ' selected="selected"';}
   echo'>Widget#'.$pnt[recordkey];
   if (!empty($pnt[title])){echo " - " .$pnt[title];} else { echo " - Untitled";} 
   echo '</option>';
}

mysql_close;
echo '</select> <input id="save-button" type="submit" name="select" value="Edit"></form></div><br>';
//end of selector

if(!empty($editwidget)){	//determine which record to display
	$query = "SELECT * from widgets where recordkey=$editwidget";
	$rt=mysql_query($query);          // query executed 
	//echo mysql_error();                    // if any error is there that will be printed to the screen
} else{
	$query= "Select min(recordkey) as recordkey from widgets";
	$rt=mysql_query($query);          // query executed
	while($nt=mysql_fetch_array($rt)){
		$editwidget=$nt[recordkey];
	}
	mysql_close;
	if(empty($editwidget)){$editwidget="0";}
	$query = "SELECT * from widgets where recordkey=$editwidget";
	$rt=mysql_query($query);          // query executed 
}		
//echo "query=" . $query;		
echo '<div align="left" style="margin-left:5px;"><table border=0 cellpadding=0 cellspacing=4>';
if(mysql_num_rows($rt)!=0){
	while($nt=mysql_fetch_array($rt)){
		if(!empty($nt[body])){
			if($mode=="live"){
				$root_qry = "Select root_folder from storePrefs";
				$rst=mysql_query($root_qry);
				while($rnt=mysql_fetch_array($rst)){
					$root_folder = $rnt[root_folder];
				}
				if(!empty($root_folder)){
					$root_folder = "/" . $root_folder;
					$body = str_replace($root_folder, "", $nt[body]);
				}
			}else{
				$body = $nt[body];
			}
		}
		
		$clean_body = stripslashes($body);
		//echo "mode=" . $mode; //for testing
		//echo "<br>root_folder=" . $root_folder; //for testing
		
		if(($mode=="design")&&(!empty($root_folder))){
			$clean_body = str_replace($root_folder."/custom/", "/custom/", $clean_body);
			$clean_body = str_replace("/".$root_folder, "", $clean_body);
			$clean_body = str_replace("///", "/", $clean_body);
			$clean_body = str_replace("//", "/", $clean_body);
			$clean_body = str_replace("/custom/", "/". $root_folder."/custom/", $clean_body);
			//add root to any internal links if in design mode
			//$body = str_replace('href=\"/', 'href=\"http://'.$root_url.'/', $body);
		}	
		echo '<tr><td><input type="hidden" name="recordkey" value="' . $nt[recordkey] . '"><form action="' . $_SERVER['PHP_SELF'] . '?a=update&admin=widgets&key=' . $nt[recordkey] . '" method="post">';
		echo '<tr><td><div align=left>Widget name: </div><td valign="top"><div align=left><input type="text" name="title" value="' .  stripslashes($nt[title]) . '"></div>';
		echo '<tr><td colspan="2"><div align=left><a href="#" data-tooltip="Add your content in this wysiwyg.">Content:</a> </div></td></tr>
		<tr><td colspan="2" valign="top"><div align=left style="width:390px;"><textarea name="widgetbody"  class="ckeditor" id="widgetbody" cols=30>' . $clean_body . '</textarea></div></td></tr>';
		echo '<tr><td><div align=left><a href="#" data-tooltip="This will change the default background color of this widget">Background color:</a> </div><td valign="top"><div align=left><input name="color" class="color" value="' . $nt[color] . '"> <i><font color="red">(click to select from color chart)</font></i></div>';

		//add selector to choose padding option for this widget
			echo'<tr><td><div align=left><a href="#" data-tooltip="This removes all the padding from this widget - use if you have an image for a widget">Padding:</a> <td valign="top"><div align=left><select name="widget_padding"><option value="0"> - Standard - </option>';
			echo '<option value="1"';
			if($nt[no_padding]=='1'){echo ' selected="selected"';}
			echo'>No Padding</option>';
			echo '</select>';
		
		echo '<tr><td valign="top"><div align=left><a href="#" data-tooltip="If checked, this widget will be available to add to your pages.">Active:</a> </div><td valign="top"><div align=left><input type="checkbox" name="chkActive"';
		if(!empty($nt[active])) {
		  echo ' checked=checked';
		}
		echo '"></div>';

		echo '<tr><td valign="top"><div align=left><a href="#" data-tooltip="If checked, removes border from this widget.">Hide border:</a> </div><td valign="top"><div align=left><input type="checkbox" name="chknoborder"';
		if(!empty($nt[noborder])) {
		  echo ' checked=checked';
		}
		echo '"></div>';
		
		echo '<tr><td valign="top"><div align=left><a href="#" data-tooltip="If checked, removes background color for this widget.">Hide back color:</a> </div><td valign="top"><div align=left><input type="checkbox" name="chknocolor"';
		if(!empty($nt[nocolor])) {
		  echo ' checked=checked';
		}
		echo '"></div><br>';
	   //add form selector to include a form in this widget
			echo'<tr><td><div align=left>Include form: <td valign="top"><div align=left><select name="formnumber"><option value=""> - Select a form - </option>';
				$forms="Select recordKey, formName from formData where formID=0 AND formName>''";
				$frm=mysql_query($forms);
				while($fnt=mysql_fetch_array($frm)){
					echo '<option value='. $fnt[recordKey];
					if($fnt[recordKey]==$nt[formNumber]){echo ' selected="selected"';}
					echo'>' .$fnt[formName] . '</option>';
				}

				mysql_close;
				echo '</select>';
	  
		echo '<tr><td><td valign=top><div align=left><input type="submit" id="save-button" name="save" value="Save"></div>';
		

		//display delete checkbox if widget is not a locked, built-in widget
		if(empty($nt[locked])) {
		echo '<tr><td valign="top"><div align=left>Delete: </div><td valign="top"><div align=left><input type="checkbox" name="chkDelete"><font color=red> <i><br>Be sure you mean to delete this widget if you check this box<br> before saving. You will not be prompted again.</i></font></div>';
	  }
	  
	  echo '</form>';
	//echo '<tr><td colspan=3 height=3 bgcolor=black>';
	//}
	}
}
echo '</table><br><br></div>';

	

	

?>
<script type="text/javascript" >
CKEDITOR.replace( 'widgetbody',
{
	filebrowserBrowseUrl : 'ckfinder/ckfinder.html',
	filebrowserImageBrowseUrl : 'ckfinder/ckfinder.html?type=Images',
	filebrowserFlashBrowseUrl : 'ckfinder/ckfinder.html?type=Flash',
	filebrowserUploadUrl : 'ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Files',
	filebrowserImageUploadUrl : 'ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Images',
	filebrowserFlashUploadUrl : 'ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Flash',
	toolbarGroups: [
		{ name: 'clipboard',   groups: [ 'clipboard', 'undo' ] },
		{ name: 'editing',     groups: [ 'find', 'selection', 'spellchecker' ] },
		{ name: 'links' },
		{ name: 'insert' },
		
		{ name: 'others' },
		'/',
		{ name: 'basicstyles', groups: [ 'basicstyles', 'cleanup' ] },
		{ name: 'paragraph',   groups: [ 'list', 'indent', 'blocks', 'align' ] },
		{ name: 'styles' },
		{ name: 'colors' },
		{ name: 'mode' },

	],
    uiColor: '#499ED7'
});
</script>
