--- /dev/null
+<?php
+ class PatchesController extends AppController {
+ public $helpers = array('Html', 'Form');
+ public $components = array('RequestHandler');
+
+ public function index() {
+
+ // GETTING TASK CONTROLLER
+ App::import('Controller', 'Tasks');
+ $tasksController = new TasksController;
+
+ // GETTING ALL PATCHES
+ $patches = $this->Patch->find('all');
+
+ // GETTING TASKS FOR EACH PATCH
+ $tasksArray = array();
+ foreach($patches as $patch) {
+
+ // GETTING TASKS ID
+ if($patch['Patch']['tasks']) {
+ $tasks_id = explode("|", $patch['Patch']['tasks']);
+ $tasks = array();
+ foreach($tasks_id as $task_id) {
+ $tasks[] = $tasksController->Task->findById($task_id);
+ }
+
+ // ADDIND TASKS TO ARRAY
+ $tasksArray[$patch['Patch']['id']] = $tasks;
+ }
+ }
+
+ // SENDING DATA TO VIEW
+ $this->set(array(
+ 'tasksArray' => $tasksArray,
+ '_serialize' => array('tasksArray')
+ ));
+ $this->set(array(
+ 'patches' => $patches,
+ '_serialize' => array('patches')
+ ));
+ }
+
+ public function add() {
+ if ($this->request->is('POST')) {
+ $this->Patch->create();
+ if ($this->Patch->save($this->request->data)) {
+ $this->Session->setFlash(__("Le patch a été enregistré."));
+ $this->redirect(array('action' => 'index'));
+ }
+ $this->Session->setFlash(__("Enregistrement du patch impossible."));
+ }
+ }
+
+ public function order_task($id = null, $order = null, $direction = null) {
+ if (!$id) {
+ throw new NotFoundException(__('Patch introuvable'));
+ }
+ $patch = $this->Patch->findById($id);
+ if (!$patch) {
+ throw new NotFoundException(__('Patch introuvable'));
+ }
+ $this->Patch->id = $id;
+
+ // GETTING TASKS ID
+ $tasks_id = explode("|", $patch['Patch']['tasks']);
+
+ // CHANGING PLACES
+ $newTasks_id = array();
+ $key = $order-1;
+ if($direction == "up") {
+ $newkey = $key-1;
+ } else if($direction == "down") {
+ $newkey = $order;
+ }
+ foreach($tasks_id as $task_id) {
+ $newTasks_id[] = $task_id;
+ }
+ if(isset($newkey)) {
+ $newTasks_id[$newkey] = $tasks_id[$key];
+ $newTasks_id[$key] = $tasks_id[$newkey];
+ }
+
+ // SAVING CHANGES
+ $this->Patch->value('tasks', implode("|", $newTasks_id));
+ $this->Patch->save();
+ $this->redirect(array('action' => 'index'));
+ }
+ }