-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCalendarFrameSettingSelectRoom.php
More file actions
216 lines (206 loc) · 5.37 KB
/
CalendarFrameSettingSelectRoom.php
File metadata and controls
216 lines (206 loc) · 5.37 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
<?php
/**
* CalendarFrameSettingSelectRoom Model
*
* @property Room $Room
* @property CalendarFrameSetting $CalendarFrameSetting
*
* @author Noriko Arai <arai@nii.ac.jp>
* @author AllCreator Co., Ltd. <info@allcreator.net>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @copyright Copyright 2014, NetCommons Project
*/
App::uses('CalendarsAppModel', 'Calendars.Model');
/**
* CalendarFrameSettingSelectRoom Model
*
* @author AllCreator Co., Ltd. <info@allcreator.net>
* @package NetCommons\Calendars\Model
*/
class CalendarFrameSettingSelectRoom extends CalendarsAppModel {
/**
* use behaviors
*
* @var array
*/
public $actsAs = array(
'NetCommons.OriginalKey',
'NetCommons.Trackable',
//'Workflow.WorkflowComment',
//'Workflow.Workflow',
'Calendars.CalendarValidate',
'Calendars.CalendarApp', //baseビヘイビア
'Calendars.CalendarInsertPlan', //Insert用
'Calendars.CalendarUpdatePlan', //Update用
'Calendars.CalendarDeletePlan', //Delete用
);
/**
* belongsTo associations
*
* @var array
*/
public $belongsTo = array(
'CalendarFrameSetting' => array(
'className' => 'Calendars.CalendarFrameSetting',
'foreignKey' => 'calendar_frame_setting_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Room' => array(
'className' => 'Rooms.Room',
'foreignKey' => 'room_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
);
/**
* Validation rules
*
* @var array
*/
public $validate = array(
);
/**
* Called during validation operations, before validation. Please note that custom
* validation rules can be defined in $validate.
*
* @param array $options Options passed from Model::save().
* @return bool True if validate operation should continue, false to abort
* @link http://book.cakephp.org/2.0/en/models/callback-methods.html#beforevalidate
* @see Model::save()
*/
public function beforeValidate($options = array()) {
$roomIds = $this->getReadableRoomIds();
$this->validate = array_merge($this->validate, array(
'calendar_frame_setting_id' => array(
'rule1' => array(
'rule' => array('numeric'),
'required' => true,
'message' => __d('net_commons', 'Invalid request.'),
),
),
'room_id' => array(
'rule1' => array(
'rule' => array('numeric'),
'required' => true,
'message' => __d('net_commons', 'Invalid request.'),
),
'rule2' => array(
'rule' => array('inList', $roomIds),
'message' => __d('net_commons', 'Invalid request.'),
)
),
));
return parent::beforeValidate($options);
}
/**
* getSelectRooms
*
* @param int $settingId frame setting id
* @return array select Rooms
*/
public function getSelectRooms($settingId) {
$this->Room = ClassRegistry::init('Rooms.Room', true);
$roomIds = $this->getReadableRoomIds();
$selectRoom = $this->Room->find('all', array(
'fields' => array(
'Room.id',
'CalendarFrameSettingSelectRoom.room_id',
'CalendarFrameSettingSelectRoom.calendar_frame_setting_id'
),
'recursive' => -1,
'joins' => array(
array('table' => 'calendar_frame_setting_select_rooms',
'alias' => 'CalendarFrameSettingSelectRoom',
'type' => 'LEFT',
'conditions' => array(
'Room.id = CalendarFrameSettingSelectRoom.room_id',
'calendar_frame_setting_id' => $settingId,
)
)
),
'conditions' => array(
'Room.id' => $roomIds
),
'order' => array('Room.id ASC')
));
if (! $selectRoom) {
return array();
}
$selectRoomArr = [];
foreach ($selectRoom as $item) {
$selectRoomArr[$item['Room']['id']] = $item['CalendarFrameSettingSelectRoom'];
}
return $selectRoomArr;
}
/**
* validateFrameSettingSelectRoom
*
* @param array $data validate data
* @return bool
*/
public function validateCalendarFrameSettingSelectRoom($data) {
foreach ($data['CalendarFrameSettingSelectRoom'] as $selectRoom) {
if ($selectRoom['room_id'] == '') {
continue;
}
$this->create();
$this->set($selectRoom);
if (! $this->validates()) {
return false;
}
}
return true;
}
/**
* saveFrameSettingSelectRoom
*
* @param array $data save data
* @return mixed On success Model::$data if its not empty or true, false on failure
* @throws InternalErrorException
*/
public function saveCalendarFrameSettingSelectRoom($data) {
$settingId = $data['CalendarFrameSetting']['id'];
$ret = array();
//トランザクションBegin
$this->begin();
try {
foreach ($data['CalendarFrameSettingSelectRoom'] as $roomId => $selectRoom) {
$condition = array(
'CalendarFrameSettingSelectRoom.calendar_frame_setting_id' => $settingId,
'CalendarFrameSettingSelectRoom.room_id' => $roomId,
);
$orgData = $this->find('first', array(
'conditions' => $condition
));
if (empty($selectRoom['room_id'])) {
$this->deleteAll($condition, false);
} else {
if (! $orgData) {
$this->create();
$this->set($selectRoom);
if (! $this->validates()) {
return false;
}
$saveData = $this->save($selectRoom, false);
if (! $saveData) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
$ret[] = $saveData;
} else {
$ret[] = $orgData;
}
}
}
$this->commit();
} catch (Exception $ex) {
CakeLog::error($ex);
$this->rollback();
throw $ex;
}
return $ret;
}
}