55502f40dc8b7c769880b10874abc9d0

User profile page. Want to show either debates started by the user, or debates in which the user has replied to... depending on parameters in the URL. Load a different view depending on what debates to show. I then pass the url of the view to the view, and then show it there (I know, confusing). If there are no debates to show, I show a message depending on what view should have been sent.

It seems very inefficient, but I cant find a better way.

$debates = ORM::factory('debate')->with('user')->with('category');
		
		// what debates are we showing?
		$view   = $this->input->get('view');
		$view   = strtolower($view);
		$limits = Kohana::config('app/user.limits');
		
		switch($view)
		{
			case 'debates':
				$debates->where('debates.user_id', $id);
				$view  = 'user/user_debate_list';
				$limit = $limits['debates'];
				break;
				
			case 'replies':
				$debates->join('replies', 'replies.debate_id', 'debates.id', 'LEFT')
					->where('replies.user_id', $id)
					->groupby('debates.id');
				$view  = 'debate/debate_list';
				$limit = $limits['replies'];
				break;
				
			default:
				$debates->where('debates.user_id', $id);
				$view  = 'user/user_debate_list';
				$limit = $limits['debates'];
				break;
		}

                $this->template->title = $user->username;
		$this->template->body  = View::factory('user/profile')
			->set('user', 	 	$user)
			->set('debates', 	$debates->find_all($limit, $offset))
			->set('view', 	 	$view);




        <!-- Debate List -->
	<?php if (count($debates) > 0): ?>
		
		<?php View::factory($view)->set('debates', $debates)->render(TRUE); ?>
		
	<?php elseif ($view == 'user/user_debate_list'): ?>
	
		<h3><?php echo $user->username; ?> has not created any debates</h3>
	
	<?php elseif ($view == 'debate/debate_list'): ?>
	
		<h3><?php echo $user->username; ?> has not replied to any debates</h3>
	
	<?php endif; ?>
	<!-- End Debate List -->

Refactorings

No refactoring yet !

C7402070919f7a3c76fec48132ec8bd2

Alix Axel

May 3, 2010, May 03, 2010 00:33, permalink

No rating. Login to rate!

The switch() for debates is redundant.

<?php

switch($view)
{
	case 'replies':
		$debates->join('replies', 'replies.debate_id', 'debates.id', 'LEFT')
			->where('replies.user_id', $id)
			->groupby('debates.id');
		$view  = 'debate/debate_list';
		$limit = $limits['replies'];
		break;
		
	default: // or case 'debates':
		$debates->where('debates.user_id', $id);
		$view  = 'user/user_debate_list';
		$limit = $limits['debates'];
		break;
}

?>

Your refactoring





Format Copy from initial code

or Cancel