OSDN Git Service

Document追加
[trpgtools-onweb/cake-frame.git] / app / controllers / systems_controller.php
1 <?php
2 class SystemsController extends AppController {
3
4         var $name = 'Systems';
5         var $helpers = array(
6                 'Select',
7                 'Profiledisp',
8         );
9
10         // listView用のpagenate設定
11         var $paginate = array(
12                 'conditions' => array(
13                 ),
14                 'fields' => array(
15                         'System.id',
16                         'System.name',
17                         'System.sort_order',
18                         'System.set_npc',
19                         'System.public_flag',
20                 ),
21                 'recursive' => 1,
22                 'contain' => array(
23                         'Attachment',
24                         'Character',
25                 ),
26                 'limit' => 10,
27                 'order' => array(
28 //                      'System.sort_order' => 'asc'
29                 ),
30         );
31
32         var $disableTokenActions = array(
33         );
34
35         function beforeFilter() {
36
37                 parent::beforeFilter();
38
39                 // Cache
40                 $this->cacheAction = array(
41                         'index' => Configure::read('Cache.expireLong'),
42                         'view/' => Configure::read('Cache.expire'),
43                 );
44
45                 // 認証なしアクセス可
46                 $this->AuthPlus->allow('index');
47                 $this->AuthPlus->allow('view');
48         }
49
50         function beforeRender()
51         {
52                 parent::beforeRender();
53
54                         $this->set_public_flag4view();
55         }
56
57
58         function index() {
59                 // SingleSystem
60                 if ($this->site_configs['System.singleSystem']['value']) {
61                         $this->Session->setFlash(__('Invalid URL.', true));
62                         $this->redirect(array('controller'=>'users', 'action'=>'index'));
63                 }
64
65                 $this->paginate['conditions']['public_flag'] = 'public';
66                 $this->System->hasMany['Character']['limit'] = 8;
67                 $this->_index();
68         }
69
70         function view($id = null) {
71                 $this->System->hasMany['Character']['limit'] = 60;
72                 $this->_view($id);
73         }
74
75         function admin_index() {
76                 unset($this->System->hasMany['Character']['conditions']['Character.public_flag']);
77                 $this->_index();
78         }
79
80         function admin_view($id = null) {
81                 $this->System->hasMany['Character']['limit'] = 30;
82                 unset($this->System->hasMany['CharacterSheet']['conditions']['CharacterSheet.public_flag']);
83                 unset($this->System->hasMany['Character']['conditions']['Character.public_flag']);
84                 $this->_view($id);
85         }
86
87         function admin_add() {
88                 if (!empty($this->data)) {
89                         if (empty($this->data['Attachment'][0]['file']['tmp_name'])) {
90                                 unset($this->data['Attachment']);
91                         }
92
93                         $this->System->create();
94                         if ($this->System->saveAll($this->data, array('fieldList' => array_merge($this->System->fields['add'], $this->System->fields['image'])))) {
95                                 $this->Session->setFlash(sprintf(__('%s has been saved', true), $this->data['System']['name']));
96                                 $this->redirect(array('action'=>'index'));
97                         } else {
98                                 $this->Session->setFlash(__('The data could not be saved. Please, try again.', true));
99                         }
100                 }
101
102                 $this->pageTitle .= " - ". __('Add New System', true);
103         }
104
105         function admin_edit($id = null) {
106                 if (!$id && empty($this->data)) {
107                         $this->Session->setFlash(__('Inview System', true));
108                         $this->redirect(array('action'=>'index'));
109                 }
110
111                 $system = $this->System->find('first', array(
112                         'conditions' => array('System.id' => $id),
113                         'fields' => '',
114                         'recursive' => 1,
115                         'contain' => array(
116                                 'Attachment',
117                         ),
118                 ));
119                 $system = $this->_restore_html_system($system, true);
120
121                 if (!empty($this->data)) {
122                         $this->data['System']['id'] = $id;
123
124                         if ($this->System->saveAll($this->data, array('fieldList' => array_merge($this->System->fields['edit'], $this->System->fields['image'])))) {
125                                 $this->Session->setFlash(sprintf(__('%s has been saved', true), $this->data['System']['name']));
126
127                                 if ($this->data['System']['public_flag'] != 'public') {
128                                         $this->System->Character->updateAll(array('Character.public_flag' => "'private'"), array('Character.system_id' => $id));
129                                 }
130
131                                 $this->redirect(array('action'=>'view', $id));
132                         } else {
133                                 $this->Session->setFlash(__('The data could not be saved. Please, try again.', true));
134                                 $this->data['System']['id'] = $id;
135                                 if (isset($this->data['Attachment'][1])) {
136                                         list($alternative, $delete) = array($this->data['Attachment'][1]['alternative'], $this->data['Attachment'][1]['delete']);
137                                 }
138                                 $this->data['Attachment'] = $system['Attachment'];
139                         if (isset($alternative)) {
140                                         $this->data['Attachment'][0]['alternative'] = $alternative;
141                                         $this->data['Attachment'][0]['delete'] = $delete;
142                                 }
143
144                         }
145                 }
146                 if (empty($this->data)) {
147                         $this->data = $system;
148                 }
149
150                 $this->pageTitle .= " - ". __('Edit System', true);
151         }
152
153         function admin_delete($id = null) {
154                 // SingleSystem
155                 if ($this->site_configs['System.singleSystem']['value']) {
156                         $this->Session->setFlash(__('Invalid URL.', true));
157                         $this->redirect(array('controller'=>'site_configs', 'action'=>'admin_edit'));
158                 }
159
160                 if (!$id) {
161                         $this->Session->setFlash(__('Invalid id for System', true));
162                         $this->redirect(array('action'=>'index'));
163                 }
164                 if (isset($this->data['System']['confirm']) && $this->data['System']['confirm'] == 'yes') {
165                         if ($this->System->del($id)) {
166                                 $this->Session->setFlash(__('System deleted', true));
167                                 $this->redirect(array('action'=>'index'));
168                         }
169                 }
170
171                 $this->pageTitle .= " - ". __('Delete System', true);
172
173                 $this->admin_view($id);
174         }
175
176         /* 共通メソッド */
177         function _index($limit = null)
178         {
179                 if ($limit) {
180                         $this->paginate['limit'] = $limit;
181                 }
182                 $systems = $this->paginate();
183
184                 if (!empty($systems)) {
185                         $sort_order = array();
186                         foreach ($systems as $k => $v) {
187                                 $systems[$k] = $this->_restore_html_system($v);
188
189                                 if (isset($v['System']['sort_order'])  && !empty($v['System']['sort_order'])) {
190                                         $sort_order[$k] = $v['System']['sort_order'];
191                                 } else {
192                                         $sort_order[$k] = 0;
193                                 }
194
195                                 // 全キャラクター数取得
196                                 if (empty($v['Character'])) {
197                                         $systems[$k]['System']['character_num'] = 0;
198                                         continue;
199                                 }
200
201                                 $systems[$k]['System']['character_num'] = $this->System->Character->find('count', array(
202                                         'conditions' => array_merge($this->System->hasMany['Character']['conditions'],
203                                                 array('Character.system_id' => $v['System']['id'])),
204                                         'recursive' => -1,
205                                 ));
206                         }
207
208                         $systems = $this->sort4sort_order($systems, $sort_order);
209
210                         $systems = $this->HtmlEscape->nl_unescape($systems);
211                 }
212
213                 $this->set('systems', $systems);
214
215                 $this->pageTitle .= " - ". __('List of All Systems', true);
216         }
217
218         function _view($id = null, $conditions = array())
219         {
220                 if (!$id) {
221                         $this->Session->setFlash(__('Invalid System.', true));
222                         $this->redirect(array('action'=>'index'));
223                 }
224
225                 // 抽出
226                 $conditions['System.id'] = $id;
227                 if ($this->isAdmin) {
228                         $this->System->hasMany['Profile']['fields'] = '';
229                         $contain = array(
230                                 'Character',
231                                 'CharacterSheet',
232                                 'Profile' => array(
233                                         'ProfileSelect',
234                                         'ProfileTable' => array(
235                                                 'ProfileTableStatic',
236                                         ),
237                                 ),
238                                 'Attachment',
239                         );
240                         $recursive = 3;
241                 } else {
242                         $contain = array(
243                                 'Character',
244                                 'Attachment',
245                         );
246                         $recursive = 1;
247                 }
248
249                 $system = $this->System->find('first', array(
250                         'conditions' => $conditions,
251                         'contain' => $contain,
252                         'recursive' => $recursive,
253                 ));
254
255                 if (!isset($system['System']) || empty($system['System'])) {
256                         $this->Session->setFlash(__('No System.', true));
257                         $this->redirect(array('action'=>'index'));
258                 }
259                 $system = $this->_restore_html_system($system, true);
260
261                 // 全キャラクター数取得
262                 if (empty($system['Character'])) {
263                         $system['System']['character_num'] = 0;
264                 }
265
266                 $system['System']['character_num'] = $this->System->Character->find('count', array(
267                         'conditions' => array_merge($this->System->hasMany['Character']['conditions'],
268                                 array('Character.system_id' => $system['System']['id'])),
269                         'recursive' => -1,
270                 ));
271
272                 $this->set('system', $this->HtmlEscape->nl2br_escaped($system));
273                 $this->set('profile_types', $this->System->Profile->profile_type);
274
275                 $this->pageTitle .= " - ". $system['System']['name'];
276         }
277
278 }
279