-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathUserAttributesController.php
More file actions
192 lines (169 loc) · 4.87 KB
/
UserAttributesController.php
File metadata and controls
192 lines (169 loc) · 4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
/**
* UserAttributes Controller
*
* @author Noriko Arai <arai@nii.ac.jp>
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @copyright Copyright 2014, NetCommons Project
*/
App::uses('UserAttributesAppController', 'UserAttributes.Controller');
/**
* UserAttributes Controller
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @package NetCommons\UserAttributes\Controller
*/
class UserAttributesController extends UserAttributesAppController {
/**
* use model
*
* @var array
*/
public $uses = array(
'UserAttributes.UserAttribute',
'UserAttributes.UserAttributeChoice',
'UserAttributes.UserAttributeSetting',
);
/**
* use component
*
* @var array
*/
public $components = array(
'ControlPanel.ControlPanelLayout',
'M17n.SwitchLanguage' => array(
'fields' => array(
'UserAttribute.name', 'UserAttribute.description',
)
),
'UserAttributes.UserAttributeLayout',
'DataTypes.DataTypeForm',
);
/**
* use helpers
*
* @var array
*/
public $helpers = array(
'UserAttributes.UserAttribute',
'UserAttributes.UserAttributeLayout',
);
/**
* index
*
* @return void
*/
public function index() {
$this->DataTypeForm->dataTypes = null;
}
/**
* add
*
* @param int $row 段
* @param int $col 列
* @return void
*/
public function add($row, $col) {
$this->view = 'edit';
if ($this->request->is('post')) {
//不要パラメータ除去
unset($this->request->data['save'], $this->request->data['active_lang_id']);
//他言語が入力されていない場合、表示されている言語データをセット
$this->SwitchLanguage->setM17nRequestValue();
//登録処理
$row = $this->request->data['UserAttributeSetting']['row'];
$col = $this->request->data['UserAttributeSetting']['col'];
$weight = $this->UserAttributeSetting->getMaxWeight($row, $col) + 1;
$this->request->data['UserAttributeSetting']['weight'] = $weight;
$result = $this->UserAttributeChoice->validateRequestData($this->request->data);
if ($result === false) {
$this->throwBadRequest();
return;
}
$this->request->data['UserAttributeChoice'] = $result;
if ($this->UserAttribute->saveUserAttribute($this->request->data)) {
//正常の場合
$this->redirect('/user_attributes/user_attributes/index/');
return;
}
$this->NetCommons->handleValidationError($this->UserAttribute->validationErrors);
} else {
//初期値セット
$this->request->data['UserAttribute'] = array();
foreach (array_keys($this->viewVars['languages']) as $langId) {
$index = count($this->request->data['UserAttribute']);
$userAttribute = $this->UserAttribute->create(array(
'id' => null,
'language_id' => $langId,
));
$this->request->data['UserAttribute'][$index] = $userAttribute['UserAttribute'];
}
$this->request->data = Hash::merge($this->request->data,
$this->UserAttributeSetting->create(array(
'data_type_key' => 'text',
'row' => $row,
'col' => $col,
))
);
}
$this->DataTypeForm->dataTypes = $this->UserAttributeSetting->addDataTypes;
}
/**
* edit
*
* @param string $key user_attributes.key
* @return void
*/
public function edit($key = null) {
if ($this->request->is('put')) {
//不要パラメータ除去
unset($this->request->data['save'], $this->request->data['active_lang_id']);
$result = $this->UserAttributeChoice->validateRequestData($this->request->data);
if ($result === false) {
$this->throwBadRequest();
return;
}
$this->request->data['UserAttributeChoice'] = $result;
//他言語が入力されていない場合、表示されている言語データをセット
$this->SwitchLanguage->setM17nRequestValue();
//登録処理
if ($this->UserAttribute->saveUserAttribute($this->request->data)) {
//正常の場合
$this->redirect('/user_attributes/user_attributes/index/');
return;
}
$this->NetCommons->handleValidationError($this->UserAttribute->validationErrors);
} else {
//既存データ取得
$this->request->data = $this->UserAttribute->getUserAttribute($key);
if (! $this->request->data) {
$this->throwBadRequest();
return;
}
}
if ($this->request->data['UserAttributeSetting']['is_system']) {
$this->DataTypeForm->dataTypes = $this->UserAttributeSetting->editDataTypes;
} else {
$this->DataTypeForm->dataTypes = $this->UserAttributeSetting->addDataTypes;
}
}
/**
* delete
*
* @return void
*/
public function delete() {
if (! $this->request->is('delete')) {
$this->throwBadRequest();
return;
}
$key = Hash::get($this->data, 'UserAttributeSetting.user_attribute_key');
if (! $this->UserAttribute->deleteUserAttribute($key)) {
$this->throwBadRequest();
return;
}
$this->redirect('/user_attributes/user_attributes/index/');
}
}