Tutorial 1

From Genitura

Jump to: navigation, search

Contents

How to create a user object

Creating the user table

Connect to your database using your favorite SQL tool and execute the following query:

CREATE TABLE user COLUMN (
id INT,
login VARCHAR(255),
passwd VARCHAR(255))
)

Creating the model

In the model folder, create a new file named user_class.php. In that file, you must create a PHP array that contains a key for each column of the previously created table. It is possible to add default values directly in the array, but if the object is loaded from the database the default values will not be used. Since we want a user schema, we do not need default values:

require_once 'model.php';

class user extends Model {
	var $data = array (
		'id' => '',
		'login' => '',
		'passwd' => '',
		);
}

Creating the form

All the *_form classes must have a constructor method and a validate() method. The constructor method can initialize the following variables:

  • name (the name of the form)
  • method (get or post)
  • action (the target file of the form)
  • fields (list of fields in the form)

Every fields entry must have the "fields" variable format:

'field_name' => array ('class'=>'Class', 'type'=>'Type', 'value'=>'default value')

The validate method is in charge of defining the form's validation rules. It returns true if the validation passed, false if the validation failed.

In the case of multilingual applications, the set_lang($lang) method can be used to initialize labels and error messages.

In the form folder, create a new file named user_form.php:

require_once 'form_manager.php';

class user_form extends form_manager {

	// constructor
	function user_form ($lang='en') {
		// form name
		$this->name = 'addUser';
		//method
		$this->method = 'post';

		$this->fields = Array (
			'id'		=> array('class'=>'Input', 'type'=>'hidden', 'value'=>$id),
			'email'		=> array('class'=>'Input', 'type'=>'text'),
			'passwd'	=> array('class'=>'Input', 'type'=>'password'),
			'save_login'	=> array('class'=>'Input', 'type'=>'submit', 'value'=>'Save')
			);

		$this->set_lang($lang);
	}

	function set_lang($lang) {
		if ($lang == 'en') {

			$this->title = "Connection info";

			$this->err = array (
				"Email cannot be empty.",
				"Invalid email address.",
				"Please enter a password.",
				"This email address is already in use.",
			);

			$this->labels = array (
				'email'	=> "Email address",
				'passwd'=> "Password",
			);
		}
	}

	function validate() {

		if ($this->fields['email']['value'] == '') {
			$this->error['email'] .= $this->err[0];
		}
		else if (!eregi("^[_a-z0-9-]+)*@[a-z0-9]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $this->fields['email']['value'])) {
				$this->error['email'] .= $this->err[1];
			}

		if ($_POST['passwd']=='' && $this->fields['id']['value']=='') {
			$this->['passwd'] .= $this->err[2];
		}

		$dbh = new SQL_Class();

		$sql = $dbh->execute('SELECT COUNT(*) FROM user
			WHERE id != "'.$this->fields['id']['value'].'"
			AND email = "'.$this->fields['email']['value'].'" AND status = "active"');

		list($count) = $sql->fetch_array();

		if ($count > 0) {
			$this->error['glob'] .= $this->err[3];
		}

		return !$this->error;
	}
}

Creating the controller

The controller is in charge of determining the data to present to the user depending on the GET and POST parameters of the HTTP request. If needed, it must initialize models, create forms, save the new data and redirect to the requested view.

In the project's root directory, create a new file named user.php:

require_once 'model/user_class.php';
require_once 'form/user_form.php';

$user = new user;

if (isset($_POST['Save'])) {
        $form = new user_form($POST['id']);
        $form->load_post();
        if ($form->validate()) {
                $user->array_to_object($form->to_array());
                $user->save();
                header('Location: ?r=user&id='.$user->get('id'));
        }
        include '../view/form.php';
}
else if (isset($_POST['Delete'])) {
        $user->delete($_POST['id']);
        header('Location: ?r=user');
}
else if (isset($_GET['id'])) {
        $form = new user_form($_GET['id']);
        $user->load($_GET['id']);
        $form->load_object($user);
        include '../view/form.php';
}
else {
        $list = $user->all();
        $table_content = array();
        $link = array('name' => '?r=user&id=');
        $table_head = array (
                'name' => "Name",
                'email' => "Email",
                'company' => "Company",
        );
        foreach($list as $user) {
                $line = array();
                foreach ($table_head as $key=>$cell) {
                        $line[$key] = $user->get($key);
                }
                $table_content[$user->get('id')] = $line;
        }
        include '../view/list.php';
}

Creating views

All views are built in the same, generic way and are meant to be instanciated by multiple controllers. HTML elements that are common to multiple views can be placed in seperate files so they can be included by views that want to utilize them.

In the view folder, create a file named form.php:

<?php include 'header.html'; ?>
        <?=$form->title?>
        <?=$form->get_html()?>
<?php include 'footer.html'; ?>

Create a file named list.php:

<?php include 'header.html'; ?>
        <table class="longlist">
                <tr>
                        <?php foreach($table_head as $title) { ?>
                <th><?=$title?></th>
                        <?php } ?>
                </tr>
                <?php foreach($table_content as $id=>$line) { ?>
                        <tr class="">
                <?php foreach($table_head as $cell=>$title) { ?>
                        <td>
                                <?php if (isset($link[$cell])) { ?>
                                        <a href="<?=$link[$cell]?><?=$id?>"><?=$line[$cell]?></a>
                                <?php } else { ?>
                                        <?=$line[$cell]?>
                                <?php } ?>
                        </td>
                <?php } ?>
                        </tr>
                <?php } ?>
        </table>
<?php include 'footer.html'; ?>
Personal tools