Chapter 4. Creating new posts

We're going to create a new page called add. Let's start by creating the view (app/views/add.phtml).

			
<h1>New post</h1>
<form action="" method="post">
	<dl>
		<dt><label for="title">Title:</label></dt>
		<dd><input type="text" name="title" /></dd>
		<dt><label for="content">Content:</label></dt>
		<dd><textarea name="content" rows="10" cols="100"></textarea></dd>
		<dt></dt>
		<dd><input type="submit" /></dd>
	</dl>
</form>
		

As you can see, this is a very simple HTML form. We now need to handle the form's data. This will take place in the action file. ().

Our action should only be executed when there's POST data. Atomik allows you to create action files for specific HTTP methods. To do so, suffix the action name with a dot followed by the HTTP method in lower case. Our action file will thus be named app/actions/add.post.php.

The first thing we need to do is filter the data. This is always an important step when dealing with POST data for security reasons. We're going to use Atomik::filter().

This method works in two ways: it can filter a scalar value or it can filter an entire array. We're obviously going to use the later as we're going to filter the $_POST array.

To filter an array, the method needs a rule. The rule is an array listing the allowed keys in the input data. For each keys, we can use a filter and define if it's required. The default filter is to sanitize strings (FILTER_SANITIZE_STRING) and we'll use that one. We're only going to set fields as required.

			
$rule = array(
	'title' => array('required' => true),
	'content' => array('required' => true)
);
		

Now we can filter the data using this rule. If the validation fail, the method will return false. It will in this case generate some error messages stored in app/filters/messages. We can then use Atomik::flash() to store them.

			
if (($data = Atomik::filter($_POST, $rule)) === false) {
	Atomik::flash(A('app/filters/messages'), 'error');
	return;
}
		

You can note that we use the A() function which is an alias to Atomik::get().

Now that our data as been validated we're going to insert them in the database. Wel'll use the Atomik_Db::insert() method.

			
$data['publish_date'] = date('Y-m-d h:i:s');
Atomik_Db::insert('posts', $data);
		

Note

Using date() could result in an error if the timezone is not set in php.ini. This can be resolved at runtime by calling the date_default_timezone_set() function.

Note that we define the publish_date before inserting the data.

Finally, we'll add a flash message announcing that the operation has been successful. We'll also redirect the user to the index page.

			
Atomik::flash('Post successfully added!', 'success');
Atomik::redirect('index');
		

Below is the complete action

			
<?php

$rule = array(
	'title' => array('required' => true),
	'content' => array('required' => true)
);

if (($data = Atomik::filter($_POST, $rule)) === false) {
	Atomik::flash(A('app/filters/messages'), 'error');
	return;
}

$data['publish_date'] = date('Y-m-d h:i:s');
Atomik_Db::insert('posts', $data);

Atomik::flash('Post successfully added!', 'success');
Atomik::redirect('index');