What's the best way to 'architecturally clean up this file? Sorry for pasting the code. It's not really the code that you have to read, just the structure.
I read in some previous database information to populate the form if the person is editing a database entry or otherwise I don't really do anything
I create a new form and start adding elements and their corresponding attributes. Fun stuff.
Then if they pressed sumbmit, I check to see if its an update or a new insertion and do the corresponding database actions to either insert or update the database using data access objects.
If they pressed submit but there was an error somewhere, it redisplays the form with all the fields populated as they last had them
If they didn't press submit, it simply displays the form either empty or filled with data.
It's kind of 1, 2, 3 algorithm but at step 1 and 3, I choose different strategies depending on the data. Should I just make a Strategy class and make 3 sub classes with 2 methods in them or is there a 'cleaner' and more maintainable way to clean this up using OO.
Here is the code, as it will help you out describe the actions above. It's working code.
<?php
$path = getenv("document_root");
require_once( $path . '/lib/presentation/DatasourceAdminTemplate.class.php' );
require_once( $path . '/lib/presentation/form/FormEngine.class.php' );
require_once( $path . '/lib/data/WebsiteSectionFactory.class.php' );
function displayPage( $formEngine, $form, $pageTitle, $errorMessage = '' ) {
$page = new DatasourceAdminTemplate( $pageTitle );
$page->displayHeader();
$formEngine->displayJavaScript();
echo $errorMessage;
$form->display();
$page->displayFooter();
}
// set the form as posted if the submit button was pressed.
if( isset( $submit ) && $submit == ' Submit ' ) {
$isPosted = 'yes';
} else {
$isPosted = 'no';
}
if( isset( $wssid ) && isset( $wsid ) && $wssid != '' && $wsid != '' ) {
$factory = new WebsiteSectionFactory( $dbConnection );
$websiteSection = $factory->findByPrimaryKey( $wsid );
$section = $websiteSection->findByPrimaryKey( $wssid );
if( $isPosted == 'no' ) {
$name = $section->name;
$text = $section->text;
$url = $section->url;
$ordering = $section->ordering;
}
$pageTitle = 'Editing Section: ' . $section->name;
} else {
$pageTitle = 'Creating New Website Section';
}
// CREATE FORM
$formEngine = new FormEngine();
$form = $formEngine->create( "form", $pageTitle, $PHP_SELF, 'post', true );
$form->addElement( new FormHeader( 'General Information' ) );
// CREATE NAME FIELD
$nameField = new TextField( 'name', $name, 'Name', true );
$nameField->addConstraint( ALPHA_REG, "You did not supply a proper name for this section." );
$form->addElement( $nameField );
// CREATE TEXT AREA
$textField = new TextArea( 'text', $text, 'Text', true,
array(
"rows" => 10,
"cols" => 40,
)
);
$textField->addConstraint( ALPHANUMERIC_REG, "You did not supply any text to be displayed on the website." );
$form->addElement( $textField );
// CREATE URL FIELD
$urlField = new TextField( 'url', $url, 'Url', true );
$urlField->addConstraint( ALPHA_REG, "You did not supply a Url for the destination page when someone clicks the menu." );
$form->addElement( $urlField );
// CREATE ORDERING FIELD
$orderingField = new TextField( 'ordering', $ordering, 'Ordering', true );
$orderingField->addConstraint( NUMERIC_REG, "You did not supply a number to represent the ordering of the menus. The lower number represents higher priority." );
$form->addElement( $orderingField );
// CREATE HIDDEN RELATED WEBSITE SECTION FIELD
$form->addElement( new HiddenField( 'wsid', $wsid ) );
$form->addElement( new HiddenField( 'wssid', $wssid ) );
$form->addElement( new HiddenField( 'pageName', $pageName ) );
$form->addElement( new HiddenField( 'isPosted', $isPosted ) );
// FIELDS FOR UPLOADING NEW MENU GRAPHICS
$form->addElement( new FormHeader( 'Menu Graphics' ) );
$form->addElement( new FileBrowser( 'offGraphic', 'Normal Graphic', false ) );
$form->addElement( new FileBrowser( 'onGraphic', 'Hover Graphic', false ) );
// ADD BUTTONS
$form->addButton( new SubmitButton( 'submit', 'Submit' ) );
$form->addButton( new ResetButton( 'reset', 'Reset' ) );
// PROCESS FORM
if( isset( $submit ) && $submit == ' Submit ' ) {
if( $form->validate() ) {
$factory = new WebsiteSectionFactory( $dbConnection );
$websiteSection = $factory->findByPrimaryKey( $wsid );
// if this is a previous sub section
if( isset( $wssid ) && $wssid != '' ) {
$section = $websiteSection->findByPrimaryKey( $wssid );
$section->set( $name, $text, $url, $ordering );
// or create a new subsection
} else {
$section = $websiteSection->create( $name, $text, $url, $ordering );
}
$section->setOffGraphic( $uploadoffGraphic, $offGraphic,
$offGraphic_name, $offGraphic_type );
$section->setOnGraphic( $uploadonGraphic, $onGraphic,
$onGraphic_name, $onGraphic_type );
$section->commit();
header( "location: /admin/$pageName" );
} else {
$errorMessage = '<br>';
$errorMessage .= 'There appears to be some errors with the form:<br>';
$errorMessage .= $form->getErrorMessage();
displayPage( $formEngine, $form, $pageTitle, $errorMessage );
}
} else {
displayPage( $formEngine, $form, $pageTitle );
}
?>