55502f40dc8b7c769880b10874abc9d0

Code for a view debate page where a user would view a live debate. The logic goes like this...

If the user is logged in, and the user is not the creator of the debate Then check if the user already replied to the debate.
If the user already replied to the debate, then don't show the reply form. If the user already replied to the debate, but wants to edit it and clicked on the edit button then check for the reply id in the URL and show the reply form (filled in with existing data).

If the form is not being shown, save the reason why (using an integer) and pass that int to the view. Do a switch in the view to determine what message to show the user.

public function view($id = 0)
	{
		$debate = ORM::factory('debate')->with('user')->with('category')->find($id);
		
		if ($debate->loaded == FALSE)
		{
			url::redirect();
		}
		
		// series of tests to show an add reply form
		if ($this->logged_in)
		{
			// is the viewer the creator?
			if ($this->user->id != $debate->user->id)
			{
				// has the user already replied?
				if (ORM::factory('reply')
					->where(array('debate_id' => $id, 'user_id' => $this->user->id))
					->count_all() == 0)
				{
					$form = $errors = array
					(
						'body' 		=> '',
						'choice_id' => '',
						'add'		=> ''
					);
					
					if ($post = $this->input->post())
					{
						$reply = ORM::factory('reply');
						
						// validate and insert the reply
						if ($reply->add($post, TRUE))
						{
							url::redirect(url::current());
						}
						
						$form   = arr::overwrite($form,   $post->as_array());
						$errors = arr::overwrite($errors, $post->errors('reply_errors'));
					}
				}
				// editing a reply?
				else if (($rid = (int) $this->input->get('edit'))
						AND ($reply = ORM::factory('reply')
									->where(array('debate_id' => $id, 'user_id' => $this->user->id))
									->find($rid)))
				{
					$form = $errors = array
					(
						'body' 		=> '',
						'choice_id' => '',
						'add'		=> ''
					);
					
					// autocomplete the form
					$form = arr::overwrite($form, $reply->as_array());
					
					if ($post = $this->input->post())
					{
						// validate and insert the reply
						if ($reply->edit($post, TRUE))
						{
							url::redirect(url::current());
						}
						
						$form   = arr::overwrite($form,   $post->as_array());
						$errors = arr::overwrite($errors, $post->errors('reply_errors'));
					}
				}
				else
				{
					// user already replied
					$reason = 3;
				}
			}
			else
			{
				// user started the debate
				$reason = 2;
			}
		}
		else
		{
			// user is not logged in.
			$reason = 1;
		}
		
		$limits = Kohana::config('app/debate.limits');
		$page 	= (int) $this->input->get('page', 1);
		$offset = ($page > 0) ? ($page - 1) * $limits['replies'] : 0;

		$replies = ORM::factory('reply')->with('user')->with('choice')->where('replies.debate_id', $id);

		$this->template->title  = $debate->topic;
		$this->template->debate = $debate;
		$this->template->body   = View::factory('debate/view')
			->set('debate',  	$debate)
			->set('replies', 	$replies->find_all($limits['replies'], $offset))
			->set('pagination', Pagination::factory(array
				(
					'style'			 => 'digg',
					'items_per_page' => $limits['replies'],
					'query_string'   => 'page',
					'auto_hide'		 => TRUE,
					'total_items' 	 => $total = $replies->count_last_query()
				))
			)
			->set('total', $total);
		
		// are we showing the add reply form?
		if (isset($form, $errors))
		{
			$this->template->body->add_reply_form = View::factory('reply/add_reply_form')
				->set('debate', $debate)
				->set('form', 	$form)
				->set('errors', $errors);
		}
		else
		{
			$this->template->body->reason = $reason;
		}
	}



	<!-- Add Reply Form -->
	<?php if (isset($add_reply_form)): ?>

		<?php echo $add_reply_form;	?>

	<?php else: ?>

		<?php
			switch ($reason)
			{
				case 1 :
					// not logged in, show a message
					$message = 'Add your ' . html::anchor('login?url=' . url::current(TRUE), '<b>vote</b>') . ' to this discussion';
					break;
					
				case 2 :
					// started the debate. dont show a message for that.
					$message = NULL;
					break;
					
				case 3:
					// already replied, show a message
					$message = 'You have already replied to this debate';
					break;
					
				default:
					// unknown reason. dont show a message
					$message = NULL;
					break;
			}
		?>
		
		<?php echo app::show_message($message, 'h2'); ?>

	<?php endif; ?>
	<!-- End Add Reply Form -->

Refactorings

No refactoring yet !

8a908aeb24a6d16f6dd7efb24a1b7fdf

Wordpress Themes

April 30, 2010, April 30, 2010 18:21, permalink

No rating. Login to rate!

Genial dispatch and this fill someone in on helped me alot in my college assignement. Say thank you you on your information.

C9128cfe2527d00da315dba0782f3a1e

pojo

June 2, 2010, June 02, 2010 00:16, permalink

No rating. Login to rate!

Nice one mate!!! Might use it at http://w3mentor.com

772ca5ee4ecc89e7449241c6b091cc3a

Drailacit

January 26, 2012, January 26, 2012 23:59, permalink

No rating. Login to rate!

This piece of writing concerning how to embed a YouTube video code is truly useful for fresh internet access people. Fastidious work, keep it up.

10a25ceef1d2cad8998d8ac9c203624f

agencja interaktywna

January 31, 2012, January 31, 2012 14:34, permalink

No rating. Login to rate!

welcome http://www.pozycjonowaniezak47.pl allowable vulgar in place of partnership from you as every

Your refactoring





Format Copy from initial code

or Cancel