-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRoomDeleteRelatedTable.php
More file actions
170 lines (148 loc) · 4.32 KB
/
RoomDeleteRelatedTable.php
File metadata and controls
170 lines (148 loc) · 4.32 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
<?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');
/**
* ルーム削除時に関連して削除するテーブル Model
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @package NetCommons\Rooms\Model
*/
class RoomDeleteRelatedTable extends RoomsAppModel {
/**
* 削除
*
* @var array
*/
private $__defaultConditions = [
'rooms' => [
'id' => ['*.room_id'],
],
'blocks' => [
'id' => ['*.block_id'],
'key' => ['*.block_key'],
],
'pages' => [
'id' => ['*.page_id'],
],
'users' => [
'id' => [
'*.user_id',
'calendar_event_share_users.share_user',
'groups.created_user',
'reservation_event_share_users.share_user'
],
],
'frames' => [
'id' => ['*.frame_id'],
'key' => ['*.frame_key'],
],
];
/**
* ルーム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',
]);
$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', $this->__defaultConditions[$table]
);
foreach ($targetTables as $table) {
$this->__execInsertQuery(
$roomId, 'room_id', $roomId, $table, 'id', $this->__defaultConditions[$table]
);
}
//トランザクション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',
]);
//トランザクションBegin
$this->begin();
try {
$table = $this->User->table;
$this->__execInsertQuery(
$roomId, 'id', $userId, $table, 'id', $this->__defaultConditions[$table]
);
//トランザクションCommit
$this->commit();
} catch (Exception $ex) {
//トランザクションRollback
$this->rollback($ex);
}
}
/**
* ルーム削除に関する情報を追加する
*
* @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(json_encode($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) . ' = ' . $findValue;
//登録処理
return $this->query($sql);
}
}