Report abuse


			
template->pagetitle = 'Create a new job listing';
		$this->template->content = new View('content/post');

		// Initialize all form fields
		$form = array
		(
			'company'	=> '',
			'location'	=> '',
			'email'		=> '',
			'description'	=> '',
		);

		// Form not submitted
		if ( ! $_POST)
		{
			// Intialize (empty) form fields in view
			$this->template->content->set('job', (object) $form);
		}

		// Form submitted
		else
		{
			// Set validation rules
			$post = Validation::factory($_POST)
				->pre_filter('trim')
				->add_rules('company', 'required', 'length[1,100]')
				->add_rules('location', 'required', 'length[1,100]')
				->add_rules('email', 'required', 'email', 'length[1,100]')
				->add_rules('description', 'required', 'length[1,5000]');

			// Run validation (filters and rules)
			$validate = $post->validate();

			// Overwrite initialized form values
			$form = array_merge($form, $post->as_array());

			// Errors
			if ( ! $validate)
			{
				// Show error messages
				$this->template->content->formerrors = $post->errors();

				// Repopulate the form
				$this->template->content->set('job', (object) $form);
			}

			// No errors
			else
			{
				// Load job ORM model
				$this->job = new Job_Model;

				// Load form values into ORM
				foreach ($form as $field => $value)
				{
					$this->job->$field = $value;
				}

				// Save the job
				$this->job->save();

				// Send confirmation mail
				email::send(/*...*/);

				// Redirect to homepage
				$this->session->set_flash('flash', 'Check mail for confirmation link.');
				url::redirect();
			}
		}
	}
}