[eluser]zyzzzz[/eluser]
Lets see what everyone elses coding standards look like, how they comment, and how they structure thier controllers. Perhaps we can learn something. Obviously, remove any sensitive information :-) Note: If your controller is absolutely massive, maybe post a different one.
Code.php
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Code extends Controller
{
function __construct()
{
parent::Controller();
}
function index($code_name)
{
$this->load->model('code_model');
$this->load->library('tank_auth');
$this->load->library('form_validation');
// If the code url is not valid, show error message.
$sql = 'SELECT * FROM codes WHERE url_name = ?';
$bindings = array($code_name);
$listing = $this->db->query($sql, $bindings)->row_array();
if (empty($listing))
{
// Show error
return;
}
// If we have feedback, lets post it.
if ($this->tank_auth->is_logged_in() && $this->input->post('feedback'))
{
$this->form_validation->set_rules('feedback', 'Feedback', 'trim|required|xss_clean');
if ($this->form_validation->run())
{
// Insert the data into the feedback table
$sql = 'INSERT INTO feedback(code_id, user_id, feedback) VALUES (?, ?, ?)';
$bindings = array ($listing['id'], $this->tank_auth->get_user_id(), set_value('feedback'));
$this->db->query($sql, $bindings);
}
}
// If we have a rating, lets rate it.
if ($rating = $this->input->post('rating'))
{
if (is_numeric($rating) && $rating >= 1 && $rating <= 5)
{
// Get the number of votes that this IP address has on this code.
$sql = 'SELECT * FROM ratings WHERE INET_NTOA(ip) = ? AND code_id = ?';
$bindings = array ($_SERVER['REMOTE_ADDR'], $listing['id']);
$query = $this->db->query($sql, $bindings);
// We should only let them vote if they haven't already voted.
if ($query->num_rows() == 0)
{
$sql = 'INSERT INTO ratings (code_id, ip, rating) VALUES (?, ?, ?)';
$bindings = array ($listing['id'], $_SERVER['REMOTE_ADDR'], $rating);
$this->db->query($sql, $bindings);
}
}
}
// Add one to the number of views that this code has.
$sql = "UPDATE codes SET views = views + 1 WHERE id = {$listing['id']}";
$this->db->query($sql);
// Retrieve language, category, tag and user arrays and add them to the listing.
$sql = "SELECT * FROM languages WHERE id = {$listing['language_id']}";
$listing['language'] = $this->db->query($sql)->row_array();
unset($listing['language_id']);
$sql = "SELECT * FROM categories WHERE id = {$listing['category_id']}";
$listing['category'] = $this->db->query($sql)->row_array();
unset($listing['language_id']);
$sql = "SELECT username FROM users WHERE id = {$listing['user_id']}";
$listing['user'] = $this->db->query($sql)->row_array();
unset($listing['user_id']);
// Retrieve rating average and rating number and add it to listing
$sql = "SELECT COUNT(rating) as rating_count, SUM(rating) / COUNT(rating) as average FROM ratings WHERE code_id = {$listing['id']}";
$listing['rating'] = $this->db->query($sql)->row_array();
// Retrieve the tags that this listing has
$listing['tags'] = $this->code_model->get_tags($listing['id']);
//echo '<pre>', print_r($listing, true), '</pre>';
//return;
// Render the partial view.
$partial_data = array ('listing' => $listing);
// If the user is logged in, lets add it to the partial data
if ($this->tank_auth->is_logged_in())
{
$sql = 'SELECT * FROM users WHERE id = ?';
$bindings = array($this->tank_auth->get_user_id());
$partial_data['user'] = $this->db->query($sql, $bindings)->row_array();
}
// Get a list of feedback and add it to the partial data
$sql = "SELECT feedback.*, users.username
FROM feedback
INNER JOIN users
ON feedback.user_id = users.id
WHERE feedback.code_id = {$listing['id']}
ORDER BY feedback.time_posted ASC";
$partial_data['feedback'] = $this->db->query($sql)->result_array();
$partial_rendered = $this->load->view('pages/partial/code/index', $partial_data, TRUE);
// Render the layout
$layout_data = array(
'content' => $partial_rendered
);
$this->load->view('pages/layout', $layout_data);
}
function _remap($code_name)
{
$this->index($code_name);
}
}