-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRoomDeleteRelatedTable.php
More file actions
245 lines (213 loc) · 6.79 KB
/
RoomDeleteRelatedTable.php
File metadata and controls
245 lines (213 loc) · 6.79 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<?php
/**
* ルーム削除時に関連して削除するテーブル Model
*
* @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('RoomsAppModel', 'Rooms.Model');
App::uses('RoomsLibForeignConditionsParser', 'Rooms.Lib');
/**
* ルーム削除時に関連して削除するテーブル Model
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @package NetCommons\Rooms\Model
*/
class RoomDeleteRelatedTable extends RoomsAppModel {
/**
* ルームIDを基にルーム削除関連データを追加
*
* @param int|string $roomId ルームID
* @return void
*/
public function insertByRoomId($roomId) {
$this->loadModels([
'Room' => 'Rooms.Room',
'Page' => 'Pages.Page',
'Frame' => 'Frames.Frame',
'Block' => 'Blocks.Block',
]);
if ($this->__existTableValue($this->Room->table, 'id', $roomId)) {
return;
}
$targetTables = [
//$this->Room->table,
$this->Page->table,
$this->Frame->table,
$this->Block->table,
];
//トランザクションBegin
$this->begin();
try {
$table = $this->Room->table;
$this->__execInsertQuery(
$roomId, 'id', $roomId, $table, 'id',
RoomsLibForeignConditionsParser::getForeignCondition($table)
);
foreach ($targetTables as $table) {
$foreignConditions = RoomsLibForeignConditionsParser::getForeignCondition($table);
foreach ($foreignConditions as $field => $condition) {
$this->__execInsertQuery(
$roomId, 'room_id', $roomId, $table, $field, [$field => $condition]
);
}
}
//トランザクションCommit
$this->commit();
} catch (Exception $ex) {
//トランザクションRollback
$this->rollback($ex);
}
}
/**
* プライベートルームIDを基にルーム削除関連データを追加
*
* @param int|string $userId ユーザID
* @param int|string $roomId プライベートルームID
* @return void
*/
public function insertUser($userId, $roomId) {
$this->loadModels([
'User' => 'Users.User',
]);
$table = $this->User->table;
$this->insert(
$roomId, $table, 'id', $userId,
RoomsLibForeignConditionsParser::getForeignCondition($table)
);
}
/**
* ルーム削除関連データを追加
*
* insertByRoomId()やinsertUser()に含まれないものを追加する時に使用する
*
* @param int|string $roomId ルームID
* @param string $tableName テーブル名
* @param string $fieldName カラム名
* @param string|int $value 値
* @param array $foreignConditions 外部キーリスト
* @return void
*/
public function insert($roomId, $tableName, $fieldName, $value, $foreignConditions) {
//既に登録されているかチェック
if ($this->__existTableValue($tableName, $fieldName, $value)) {
return;
}
//トランザクションBegin
$this->begin();
try {
$this->__execInsertQuery(
$roomId, $fieldName, $value, $tableName, $fieldName, $foreignConditions
);
//トランザクションCommit
$this->commit();
} catch (Exception $ex) {
//トランザクションRollback
$this->rollback($ex);
}
}
/**
* 対象テーブルの値が存在するか否か
*
* @param string $tableName 対象テーブル名
* @param string $fieldName 対象カラム名
* @param string $value 対象の値
*
* @return bool
*/
private function __existTableValue($tableName, $fieldName, $value) {
$findTableName = $this->tablePrefix . $this->table;
$result = $this->query(
"SELECT COUNT(*) count_num FROM {$findTableName} AS {$this->alias}" .
" WHERE {$this->alias}.delete_table_name = :tableName" .
" AND {$this->alias}.field_name = :fieldName" .
" AND {$this->alias}.value = :value",
['tableName' => $tableName, 'fieldName' => $fieldName, 'value' => $value]
);
return $result[0][0]['count_num'] > 0;
}
/**
* ルーム削除に関する情報を追加する
*
* @param int|string $roomId ルームID
* @param string $findField SELECTで取得するカラム名
* @param string $findValue SELECTで取得する値
* @param string $targetTableName 対象テーブル名
* @param string $targetFieldName 対象カラム名
* @param array $foreignConditions 外部キー情報
*
* @return void
*/
private function __execInsertQuery(
$roomId, $findField, $findValue, $targetTableName, $targetFieldName, $foreignConditions) {
$db = $this->getDataSource();
$loginUserId = Current::read('User.id', '0');
$targetAlias = Inflector::classify($targetTableName);
$fullTargetTableName = $this->tablePrefix . $targetTableName;
$values = [
'room_id' => $db->value($roomId, 'string'),
'delete_table_name' => $db->value($targetTableName, 'string'),
'field_name' => $db->value($targetFieldName, 'string'),
'value' => $this->escapeField($targetFieldName, $targetAlias),
'foreign_field_conditions' =>
$db->value(RoomsLibForeignConditionsParser::convertDbValue($foreignConditions), 'string'),
'created' => $db->value(date('Y-m-d H:i:s'), 'string'),
'created_user' => $db->value($loginUserId, 'string'),
'modified' => $db->value(date('Y-m-d H:i:s'), 'string'),
'modified_user' => $db->value($loginUserId, 'string'),
];
$sql = 'INSERT INTO ' . $this->tablePrefix . $this->table .
' (' . implode(', ', array_keys($values)) . ')' .
' SELECT ' . implode(', ', $values) .
' FROM ' . $fullTargetTableName . ' AS ' . $targetAlias .
' WHERE ' . $this->escapeField($findField, $targetAlias) .
' = ' . $db->value($findValue, 'string');
//登録処理
return $this->query($sql);
}
/**
* 開始時間の更新
*
* @param int|string $id ID
* @return bool
*/
public function updateStartTime($id) {
$this->__updateField($id, 'start_time', gmdate('Y-m-d H:i:s'));
}
/**
* 終了日時の更新
*
* @param int|string $id ID
* @return bool
*/
public function updateEndTime($id) {
$this->__updateField($id, 'end_time', gmdate('Y-m-d H:i:s'));
}
/**
* 更新処理
*
* @param int|string $id ID
* @param string $name カラム名
* @param string $value 値
* @return bool|array See Model::save() False on failure or an array of model data on success.
* @see Model::save()
* @link https://book.cakephp.org/2.0/en/models/saving-your-data.html#model-savefield-string-fieldname-string-fieldvalue-validate-false
*/
private function __updateField($id, $name, $value) {
//トランザクションBegin
$this->begin();
try {
$this->create(false);
$options = ['validate' => false, 'fieldList' => [$name]];
$this->save([$this->alias => [$this->primaryKey => $id, $name => $value]], $options);
//トランザクションCommit
$this->commit();
} catch (Exception $ex) {
//トランザクションRollback
$this->rollback($ex);
}
}
}