Skip to content

Instantly share code, notes, and snippets.

@codersatx
Created May 20, 2011 13:17
Show Gist options
  • Save codersatx/982867 to your computer and use it in GitHub Desktop.
Save codersatx/982867 to your computer and use it in GitHub Desktop.
The project controller class for a Kohana based cms.
<?php defined('SYSPATH') OR die('No direct access allowed.');
/**
* Project Controller
*
* Handles the data traffic for our projects.
*
* @package Project_Controller
* @category Controllers
* @author Alex Garcia
* @copyright Copyright (c) 2010, KGBTexas
*/
class Project_Controller extends Controller{
public $project;
public $user;
public $status;
public $task;
/**
* Creates our object
*
* @return object Project
*/
public function __construct()
{
parent::__construct();
$this->project = new Project_Model();
$this->user = new User_Model();
$this->status = new Status_Model();
$this->task = new Task_Model();
}
//-----------------------------------------------------------------------
/**
* Default method.
*
* We redirect this to our view method since we will use it to filter
* what items are displayed.
*
* @return void
*/
public function index()
{
$this->view();
}
//-----------------------------------------------------------------------
/**
* View a table with a list of projects.
*
* @return object Template : Returns our view object with the content.
*/
public function view()
{
//Let's load our model objects.
$user = $this->user;
$status = $this->status;
//Let's load our view object.
$form = new View('project/form');
//Let's define the properties for our view object
$form->function = 'create';
$form->status = $status->status_dropdown(0,TRUE);
$form->account_exec = $user->user_dropdown(0,TRUE);
//Lets create our Opportunity Model Object
$project = $this->project;
//Our section title
$this->template->section = "Projects";
//Lets setup our breadcrumb list
$breadcrumb = html_list::render(array(
'<a href="/">Dashboard</a>',
$this->path_seperator,
'Projects',
));
//Lets pass our breadcrumb to our template
$this->template->breadcrumb = $breadcrumb;
//Lets create our add icon
$icon_add = asset::render('img','icon_add.png',array('id'=>'icon_add'));
$this->template->content = html::div($icon_add .
html::anchor('/project/create','Create A New Project',array('id'=>'create_project_link')),array('id'=>'create_opp_div'));
$this->template->content .= html::div($form,array('id'=>'form_wrapper'));
//To the content above let's add the table of opportunities.
$this->template->content .= $project->get_table();
//Finally let's render our template.
$this->template->render(TRUE);
}
//-----------------------------------------------------------------------
/**
* Sets up a form so we can create a new project.
*
* @return object template : Returns our view object with the content.
*/
public function create()
{
//Let's load our model objects.
$user = $this->user;
$status = $this->status;
//Let's load our view object.
$form = new View('project/form');
//Let's define the properties for our view object
$form->function = 'create';
$form->status = $status->status_dropdown(0,TRUE);
$form->account_exec = $user->user_dropdown(0,TRUE);
//Lets setup our breadcrumb list
$breadcrumb = html_list::render(array(
'<a href="/">Dashboard</a>',
$this->path_seperator,
'<a href="/project">Projects</a>',
$this->path_seperator,
'New Project'));
//Lets pass our breadcrumb to our template
$this->template->breadcrumb = $breadcrumb;
$this->template->section = "Create Project";
$this->template->content = $form;
//Finally let's render our template.
$this->template->render(TRUE);
}
//-----------------------------------------------------------------------
/**
* Passes the data from the view to the model to insert a new record.
*
* @return string 'OK' : This is because we are outputting to an ajax function.
*/
public function insert()
{
//Let's create our model object.
$project = $this->project;
//Let's get our post array.
$post = $this->input->post();
//Let's loop through the array to remove the items we don't want.
$garbage = array('button','button_x','button_y','redirect');
foreach($post as $key=>$value)
{
if ( ! in_array($key, $garbage))
{
$data[$key] = $value;
}
}
//Let's call the insert method of our data object and pass to it our
//data for this insert.
$result = $project->insert($data);
//If the method returns true let's echo OK since we are outputting to an ajax function.
if($result==TRUE)
{
echo 'OK';
}
}
//-----------------------------------------------------------------------
/**
* Sets up our form so we can edit the details
*
* @param int $project_id : The id of the project we want to edit.
* @return object template : Returns our view object with the content.
*/
public function edit($project_id)
{
//Let's load our data oibjects.
$user = $this->user;
$status = $this->status;
$project = $this->project->detail($project_id);
//Let's load our view object.
$form = new View('project/form');
//Let's define our form properties.
$form->function = 'edit';
$form->project_id = $project->project_id;
$form->status = $status->status_dropdown($project->status_id,TRUE);
$form->account_exec = $user->user_dropdown($project->account_exec, TRUE);
$form->job_number = $project->job_number;
$form->project_name = $project->project_name;
$form->project_details = str_replace('<br/>',"\n",$project->project_details);
//Let's create our breadcrumb list.
$breadcrumb = html_list::render(array(
'<a href="/">Dashboard</a>',
$this->path_seperator,
'<a href="/project">Projects</a>',
$this->path_seperator,
$project->project_name
));
//Let's pass our breadcrumb to our template.
$this->template->breadcrumb = $breadcrumb;
//Let's set the section title for our template.
$this->template->section = "Edit Project";
//Let's set the content property for our template.
$this->template->content = $form;
//Finally let's render the template.
$this->template->render(TRUE);
}
//-----------------------------------------------------------------------
/**
* Handles the update for a project
*
* @param int $project_id : The id of the project we want to update.
* @return string 'OK' : Used by our ajax success function.
*/
public function update($project_id)
{
$post = $this->input->post();
$garbage = array('button','button_x','button_y','redirect');
foreach($post as $key=>$value)
{
if ( ! in_array($key, $garbage))
{
$data[$key] = $value;
}
}
$project = $this->project;
$result = $project->update($project_id,$data);
if ($result==TRUE)
{
echo 'OK';
}
}
//-----------------------------------------------------------------------
/**
* Gets a table of tasks for a project.
*
* @param int $project_id : The id of the project we want to display the projects for.
* @return object $template : Loads our view object with the content needed.
*/
public function tasks($project_id=NULL)
{
$task = $this->task;
$project = $this->project;
$user = $this->user;
$status = $this->status;
//Let's load our view object.
$form = new View('task/form');
//Let's define the properties for our view object
$form->function = 'create';
$form->status = $status->status_dropdown(0,TRUE);
$form->user_drop_down = $user->user_dropdown(0,FALSE);
$form->account_exec = $user->user_dropdown(0,TRUE);
$form->percentage_drop_down = form::dropdown('percentage_complete',array('
0'=>'Not Started',
'10'=>'10 % complete',
'20'=>'20 % complete',
'30'=>'30 % complete',
'40'=>'40 % complete',
'50'=>'50 % complete',
'60'=>'60 % complete',
'70'=>'70 % complete',
'80'=>'80 % complete',
'90'=>'90 % complete',
'100'=>'100 % complete',
),'','standard');
$form->project_id = $project_id;
if($project_id!=NULL)
{
$project_details = $project->detail($project_id);
$list = '<ul id="project_details">';
$list .= '<li><h4>Last Updated</h4>'. date('m-d-Y h:i:s',strtotime($project->detail($project_id)->last_updated)) .'</li>';
$list .= '<li><h4>Status</h4>'. $status->status_title($project->detail($project_id)->status_id).'</li>';
$list .= '<li><h4>Project Name</h4>'. $project->detail($project_id)->project_name .'</li>';
$list .= '<li><h4>Project Details</h4>'. $project->detail($project_id)->project_details .'</li>';
$list .= '<li><h4>Account Exec</h4>'. $user->user_name($project->detail($project_id)->account_exec) .'</li>';
$list .= '</ul>';
$link_show_details = '<a href="#" id="show_project_details">Toggle Details</a>';
$project_name = $project->detail($project_id)->project_name;
}
else
{
$project_name = 'All ';
$link_show_details = '';
$list = '';
}
//Lets setup our breadcrumb list
$breadcrumb = html_list::render(array(
'<a href="/">Dashboard</a>',
$this->path_seperator,
'<a href="/project">Projects</a>',
$this->path_seperator,
$project_name .' Tasks',
));
//Lets pass our breadcrumb to our template
$this->template->breadcrumb = $breadcrumb;
//Lets create our add icon
$icon_add = asset::render('img','icon_add.png',array('id'=>'icon_add'));
$this->template->section = $project_name . ' Tasks';
$this->template->content = html::div($icon_add .
html::anchor('/task/create/'. $project_id,
'Create A New Task',array('id'=>'create_task_link')) .
$link_show_details, array('id'=>'create_opp_div'));
$this->template->content .= html::div($form,array('id'=>'form_wrapper'));
$this->template->content .= $list;
//To the content above let's add the table of opportunities.
$this->template->content .= $task->get_table($project_id);
//Finally let's render our template.
$this->template->render(TRUE);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment