Passed
Branch master (44bfae)
by giu
03:41
created

OrganizationsController::edit()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 10
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 16
ccs 0
cts 15
cp 0
crap 12
rs 9.9332
1
<?php
2
namespace App\Controller;
3
4
use App\Controller\AppController;
5
6
/**
7
 * Organizations Controller
8
 *
9
 * @property \App\Model\Table\OrganizationsTable $Organizations
10
 *
11
 * @method \App\Model\Entity\Organization[]|\Cake\Datasource\ResultSetInterface paginate($object = null, array $settings = [])
12
 */
13
class OrganizationsController extends AppController
14
{
15
16
    /**
17
     * Index method
18
     *
19
     * @return \Cake\Http\Response|void
20
     */
21
    public function index()
22
    {
23
        $this->paginate = [
24
            'contain' => ['Users']
25
        ];
26
        $organizations = $this->paginate($this->Organizations);
27
28
        $this->set(compact('organizations'));
29
    }
30
31
    /**
32
     * View method
33
     *
34
     * @param string|null $id Organization id.
35
     * @return \Cake\Http\Response|void
36
     * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
37
     */
38
    public function view($id = null)
39
    {
40
        $organization = $this->Organizations->get($id, [
41
            'contain' => ['Users']
42
        ]);
43
44
        $this->set('organization', $organization);
45
    }
46
47
    /**
48
     * Add method
49
     *
50
     * @return \Cake\Http\Response|null Redirects on successful add, renders view otherwise.
51
     */
52
    public function add()
53
    {
54
        $organization = $this->Organizations->newEntity();
55
        if ($this->request->is('post')) {
56
            $organization = $this->Organizations->patchEntity($organization, $this->request->getData());
57
            if ($this->Organizations->save($organization)) {
58
                $this->Flash->success(__('The organization has been saved.'));
59
60
                return $this->redirect(['action' => 'index']);
61
            }
62
            $this->Flash->error(__('The organization could not be saved. Please, try again.'));
63
        }
64
        $users = $this->Organizations->Users->find('list', ['limit' => 200]);
65
        $this->set(compact('organization', 'users'));
66
    }
67
68
    /**
69
     * Edit method
70
     *
71
     * @param string|null $id Organization id.
72
     * @return \Cake\Http\Response|null Redirects on successful edit, renders view otherwise.
73
     * @throws \Cake\Network\Exception\NotFoundException When record not found.
74
     */
75
    public function edit($id = null)
76
    {
77
        $organization = $this->Organizations->get($id, [
78
            'contain' => []
79
        ]);
80
        if ($this->request->is(['patch', 'post', 'put'])) {
81
            $organization = $this->Organizations->patchEntity($organization, $this->request->getData());
82
            if ($this->Organizations->save($organization)) {
83
                $this->Flash->success(__('The organization has been saved.'));
84
85
                return $this->redirect(['action' => 'index']);
86
            }
87
            $this->Flash->error(__('The organization could not be saved. Please, try again.'));
88
        }
89
        $users = $this->Organizations->Users->find('list', ['limit' => 200]);
90
        $this->set(compact('organization', 'users'));
91
    }
92
93
    /**
94
     * Delete method
95
     *
96
     * @param string|null $id Organization id.
97
     * @return \Cake\Http\Response|null Redirects to index.
98
     * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
99
     */
100
    public function delete($id = null)
101
    {
102
        $this->request->allowMethod(['post', 'delete']);
103
        $organization = $this->Organizations->get($id);
104
        if ($this->Organizations->delete($organization)) {
105
            $this->Flash->success(__('The organization has been deleted.'));
106
        } else {
107
            $this->Flash->error(__('The organization could not be deleted. Please, try again.'));
108
        }
109
110
        return $this->redirect(['action' => 'index']);
111
    }
112
}
113