Hi,
I’m relatively new to OOP and whilst I’m beginning to get to grips with the various design descisions out there, I’m still unsure as to whether I’m doing it right, so I just wondered whether you kind folks would mind taking a brief look over this snippet of code and make any suggestions as to how I can seperate everything out further.
I’m aware that there are various programming patterns out there which further aid the seperation of concerns such as MVC, but as it stands in this example I’m not using any kind of programming pattern (except for a strategy pattern for the error checking.)
Many thanks in advance!
<?php
// Classes are autloaded within here
require_once("inc/config.php");
/* Create object, check user data/module exists */
$moduleEdit = new Admin_Edit($db, $_GET['mid']);
$errorClass = new Misc_Error();
if (!$core->user->getIsAdmin() || !$moduleEdit->moduleExistsById($moduleEdit->getModuleId())) {
header("Location: " . PATH . "/dashboard/");
}
/* If action is set, update module */
if (isset($_POST['action'])) {
$act = $_POST['action'];
if ($act == "edit"){
/* Basic strategy pattern validate fields */
$val['title'] = new Validate_Title($_POST['module_title']);
$val['table'] = new Validate_TableEdit($db, $moduleEdit, $_POST['module_db_name']);
$val['key'] = new Validate_KeyEdit($db, $moduleEdit, $_POST['module_primarykey']);
/* Set errors in errorClass which is then output further down the page amongst html */
foreach($val as $validate) {
if (!$validate->isValid()) {
while ($error = $validate->getError()) {
$errorClass->setError($error);
}
}
}
if (!$errorClass->isError()) {
$moduleEdit->editModule(
$_POST['module_title'],
$_POST['module_db_name'],
$_POST['module_primarykey']);
}
/* More code to go here to show module has been sucessfully edited */
}
}
/* Continue to load module if form is loaded first time */
$moduleEdit->setModuleProp($moduleEdit->getModuleId());
if (!$moduleEdit->getModulePropSet()) {
header("Location: " . PATH . "/dashboard/");
}
require("inc/header.php");
?>
<div id="formcon">
<?php
if ($errorClass->isError()) {
//Argument is ID for the UL output, ie: <ul id="x">
$errorClass->outputError("quick_module_err");
}
?>
<form action="" method="post" id="regform">
<ul id="form1"><li class="heading"><h2>Details</h2></li>
<li class="req clearfix">
<?php $var = "module_title"; $val = (isset($_POST[$var])) ? $_POST[$var] : $moduleEdit->{$var}; ?>
<label for="<?php echo $var; ?>" class="form_label">Title</label>
<input type="text" id="<?php echo $var; ?>" name="<?php echo $var; ?>" value="<? if (isset($val)) echo $val; ?>" />
</li>
<li class="req clearfix">
<?php $var = "module_db_name"; $val = (isset($_POST[$var])) ? $_POST[$var] : $moduleEdit->{$var}; ?>
<label for="<?php echo $var; ?>" class="form_label">Table Name</label>
<input type="text" id="<?php echo $var; ?>" name="<?php echo $var; ?>" value="<? if (isset($val)) echo $val; ?>" />
</li>
<li class="req clearfix">
<?php $var = "module_primarykey"; $val = (isset($_POST[$var])) ? $_POST[$var] : $moduleEdit->{$var}; ?>
<label for="<?php echo $var; ?>" class="form_label">Primary Key</label>
<input type="text" id="<?php echo $var; ?>" name="<?php echo $var; ?>" value="<? if (isset($val)) echo $val; ?>" style="width: 60px;" />
</li>
<li><label class="form_label"> </label><input type="submit" id="submit" name="submit" value="Submit" /></li>
</ul>
<input type="hidden" id="action" name="action" value="edit" />
</form>
</div>
<?php require("inc/footer.php"); ?>