-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTinydbBlocksController.php
More file actions
214 lines (196 loc) · 4.9 KB
/
TinydbBlocksController.php
File metadata and controls
214 lines (196 loc) · 4.9 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php
/**
* TinydbBlocks 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('TinydbAppController', 'Tinydb.Controller');
/**
* TinydbBlocks Controller
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @package NetCommons\Tinydb\Controller
*
* @property Tinydb $Tinydb
*/
abstract class TinydbBlocksController extends TinydbAppController {
/**
* layout
*
* @var array
*/
public $layout = 'NetCommons.setting';
/**
* use models
*
* @var array
*/
public $uses = array(
'Tinydb.TinydbFrameSetting',
'Blocks.Block',
'Tinydb.TinydbItem',
);
/**
* use components
*
* @var array
*/
public $components = array(
'NetCommons.Permission' => array(
//アクセスの権限
'allow' => array(
'index,add,edit,delete' => 'block_editable',
),
),
'Paginator',
'Categories.CategoryEdit',
'Tinydb.TinydbBlockTabSetting',
);
/**
* use helpers
* @var array
*/
public $helpers = array(
'Blocks.BlockForm',
//'Blocks.BlockTabs' => array(
// 'mainTabs' => array(
// 'block_index' => array('url' => array('controller' => 'tinydb_blocks')),
// 'frame_settings' => array('url' => array('controller' => 'tinydb_frame_settings')),
// ),
// 'blockTabs' => array(
// 'block_settings' => array('url' => array('controller' => 'tinydb_blocks')),
// 'mail_settings',
// 'role_permissions' => array('url' => array('controller' => 'tinydb_block_role_permissions')),
// ),
//),
'Blocks.BlockIndex',
//'Blocks.Block',
'Likes.Like',
);
/**
* beforeFilter
*
* @return void
*/
public function beforeFilter() {
parent::beforeFilter();
//CategoryEditComponentの削除
if ($this->params['action'] === 'index') {
$this->Components->unload('Categories.CategoryEdit');
}
}
/**
* index
*
* @return void
*/
public function index() {
$currentDbType = \NetCommons\Tinydb\Lib\CurrentDbType::instance();
if ($currentDbType->isSingleDb()) {
// シングルDBモード
// 既にDBがあれば、editへ
$tinydb = $this->Tinydb->find('first', [
'conditions' => [
'Tinydb.db_type' => $currentDbType->getDbTypeKey(),
'Block.room_id' => Current::read('Room.id')
],
'recursive' => 0,
]);
if ($tinydb) {
$this->redirect(
[
'controller' => $currentDbType->getDbTypeKey() . '_blocks',
'action' => 'edit',
$tinydb['Tinydb']['block_id'],
'?' => [
'frame_id' => Current::read('Frame.id')
]
]
);
return;
}
// DBがなければ追加へ
$this->redirect(
[
'controller' => $currentDbType->getDbTypeKey() . '_blocks',
'action' => 'add',
'?' => [
'frame_id' => Current::read('Frame.id')
]
]
);
return;
}
$this->Paginator->settings = array(
'Tinydb' => $this->Tinydb->getBlockIndexSettings()
);
$tinydb = $this->Paginator->paginate('Tinydb');
if (! $tinydb) {
$this->view = 'Blocks.Blocks/not_found';
return;
}
$this->set('tinydb', $tinydb);
$this->request->data['Frame'] = Current::read('Frame');
}
/**
* add
*
* @return void
*/
public function add() {
$this->view = 'edit';
if ($this->request->is('post')) {
//登録処理
if ($this->Tinydb->saveTinydb($this->data)) {
$this->NetCommons->setFlashNotification('保存しました。', ['class' => 'success']);
$this->redirect(NetCommonsUrl::backToIndexUrl('default_setting_action'));
}
$this->NetCommons->handleValidationError($this->Tinydb->validationErrors);
} else {
//表示処理(初期データセット)
$this->request->data = $this->Tinydb->createTinydb();
$this->request->data += $this->TinydbFrameSetting->getTinydbFrameSetting();
$this->request->data['Frame'] = Current::read('Frame');
}
}
/**
* edit
*
* @return void
*/
public function edit() {
if ($this->request->is('put')) {
//登録処理
if ($this->Tinydb->saveTinydb($this->data)) {
$this->NetCommons->setFlashNotification('保存しました。', ['class' => 'success']);
$this->redirect(NetCommonsUrl::backToIndexUrl('default_setting_action'));
}
$this->NetCommons->handleValidationError($this->Tinydb->validationErrors);
} else {
//表示処理(初期データセット)
if (! $tinydb = $this->Tinydb->getTinydb()) {
return $this->throwBadRequest();
}
$this->request->data += $tinydb;
$this->request->data += $this->TinydbFrameSetting->getTinydbFrameSetting();
$this->request->data['Frame'] = Current::read('Frame');
}
}
/**
* delete
*
* @return void
*/
public function delete() {
if ($this->request->is('delete')) {
if ($this->Tinydb->deleteTinydb($this->data)) {
return $this->redirect(NetCommonsUrl::backToIndexUrl('default_setting_action'));
}
}
return $this->throwBadRequest();
}
}