--- /dev/null
+<?php
+ class TasksController extends AppController {
+ public $helpers = array('Html', 'Form');
+ public $components = array('RequestHandler');
+
+ public function index() {
+ $tasks = $this->Task->find('all');
+ $this->set(array(
+ 'tasks' => $tasks,
+ '_serialize' => array('tasks')
+ ));
+ }
+
+ public function add($patch_id = null) {
+ App::import('Controller', 'Patches');
+ $patchesController = new PatchesController;
+
+ // FORM IS SENT
+ if ($this->request->is('POST')) {
+ $this->Task->create();
+ $this->Task->value('name', $this->request->data['Task']['name']);
+ if ($this->Task->save($this->request->data)) {
+
+ // GETTING PATCH
+ $patch_id = $this->request->data['Task']['patch'];
+ if(isset($patch_id)) {
+ $patch = $patchesController->Patch->findById($patch_id);
+ $this->set('patch', $patch);
+
+ // ADDING TASK TO PATCH
+ $tasks = array();
+ if($patch['Patch']['tasks']) {
+ $tasks = explode('|', $patch['Patch']['tasks']);
+ }
+ $tasks[] = $this->Task->id;
+ $patchesController->Patch->read(null, $patch_id);
+ $patchesController->Patch->set('tasks', implode("|", $tasks));
+ $patchesController->Patch->save();
+ }
+ $this->Session->setFlash(__("La tâche a été enregistrée."));
+ if(isset($patch_id)) {
+ $this->redirect(array('controller' => 'patches', 'action' => 'index'));
+ } else {
+ $this->redirect(array('action' => 'index'));
+ }
+ }
+ $this->Session->setFlash(__("Enregistrement de la tâche impossible."));
+
+ // FORM IS NOT SENT OR ACCESS VIA WRONG METHOD
+ } else {
+
+ // GETTING PATCH IF ITS ID IS SET IN REQUEST
+ if(isset($patch_id)) {
+ $patch = $patchesController->Patch->findById($patch_id);
+ $this->set('patch', $patch);
+ }
+ }
+ }
+ }