EnduroDave
EnduroDave

Reputation: 1033

Joining two models/controllers to allow two views on one page

I have installed this simple-task-board codeigniter app from github. The app has 3 main pages

  1. a dashboard which displays links to the projects
  2. a project view which displays links to the tasks
  3. a task view with information on the individual task

The project details are set up in their own models and controllers as are the tasks. As it stands the project view shows a list of tasks but in order to add a new task it has to pass you to another view loaded from the tasks MVC.

My question is what is the best way to get the 'add tasks' form on the project view. I am relatively new to MVC but I know that I should not be trying to what I am asking with the current set up i.e. I should not be trying to load a view from one controller in another. Would the best strategy be to try and join the two separate MVCs together or should I give up on trying to do this and rewrite the whole thing from scratch?

I did try using a library to pass the controller class from one MVC to the other but ended up with many database errors. I am assuming this was not a good plan?


Update - extra code added:

public function tasks($project_id)
{
    $this->load->helper('stb_date');
    $this->load->helper('tasks');

    // Check permission
    if(!$this->usercontrol->has_permission('project', 'tasks'))
        redirect('dashboard');

    // Load tasks
    $this->load->model('task_model');
    $tasks = $this->task_model->get($project_id);

    foreach ($tasks as $task) {
        if ($task['status'] == 0) {
            $data['stories'][] = $task;
        } elseif ($task['status'] == 1) {
            $data['tasks'][] = $task;
        } elseif ($task['status'] == 2) {
            $data['tests'][] = $task;
        } elseif ($task['status'] == 3) {
            $data['done'][] = $task;
        }
    }

    // Load project info
    $this->load->model('project_model');
    $project = $this->project_model->get($project_id);

    $this->title = "Project: {$project['name']}";
    $this->menu = 'dashboard|edit_project|new_task';

    $data['project_id']    = $project_id;

    $data['current_user'] = $this->session->userdata('user');

    $db_users = $this->project_model->get_related_users($project_id);
    $users = array();
    foreach ($db_users as $user) {
        $users[$user['id']] = $user;
    }
    $data['users'] = $users;

    // Load text helper to be used in the view
    $this->load->helper('text');

    // Load View
    $this->load->view('task_board', $data);


    //new code added here
    $task_data['parent_id']      = 0;
    $task_data['title']          = '';
    $task_data['description']    = '';
    $task_data['priority']       = '2';
    $task_data['due_date']     = '';

    $project = '1';
    $task_data['project_id'] = $project;
    $task_data['users']      = $this->task_model->get_related_users($project);
    $task_data['user_id']    = $this->session->userdata('user');
    $task_data['tasks']      = $this->task_model->get_hierarchy($project);

    if($this->error)
        $task_data['error'] = $this->error;
    $task_data['task_data'] = $this->load->view('task_add',$task_data,true);
}

Upvotes: 1

Views: 138

Answers (1)

Hans
Hans

Reputation: 3523

I use CI quite a lot and at the moment and somewhat like it but please note if you are just starting with MVC. The company that used to maintain it is looking for a new owner: http://ellislab.com/blog/entry/ellislab-seeking-new-owner-for-codeigniter.

As for your question, this view looks like it's the one with the form you are wanting to copy/move: https://github.com/oscardias/Simple-Task-Board/blob/master/application/views/task_add.php

That's all pretty easy, but lines 91-93 have some hidden ids you likely won't have on the project view page. You don't get it until you choose a view. So you'll probably want to rewrite it slightly to include a drop-down list of projects or something to add the task to.

I would probably call that task_add.php view in the projects page so you don't have two copies. If the $project_id exists, show the hidden field, otherwise the dropdown of projects.

There isn't really a hard association between views and controllers or models. Typically the controller is the middle man, controlling the data that the view gets (and that is usually provided by a model).

Upvotes: 1

Related Questions