OSDN Git Service

Document追加
[trpgtools-onweb/cake-frame.git] / app / controllers / character_sheets_controller.php
1 <?php
2 class CharacterSheetsController extends AppController {
3
4         var $name = 'CharacterSheets';
5         var $helpers = array(
6                 'Select',
7         );
8
9         // pagenate設定
10         var $paginate = array(
11                 'conditions' => array(
12                 ),
13                 'fields' => array(
14                 ),
15                 'recursive' => 1,
16                 'contain' => array(
17                         'System',
18                 ),
19                 'limit' => 20,
20                 'order' => array(
21                         'CharacterSheet.system_id' => 'asc',
22                         'CharacterSheet.sort_order' => 'asc',
23                 ),
24         );
25
26         var $disableTokenActions = array();
27
28
29         /* メソッド */
30
31         function beforeFilter() {
32
33                 parent::beforeFilter();
34
35                 // 認証なしアクセス可
36         }
37
38         function beforeRender()
39         {
40                 parent::beforeRender();
41
42                 $this->set_public_flag4view();
43         }
44
45         /* アクションメソッド */
46
47         function admin_index($system_id = null) {
48                 // $system_id指定
49                 if (!empty($system_id)) {
50                         // System情報取得
51                         $this_system = $this->CharacterSheet->System->find('first', array(
52                                 'conditions' => array(
53                                         'System.id' => $system_id,
54                                 ),
55                                 'recursive' => -1,
56                         ));
57
58                         if (!isset($this_system['System'])) {
59                                 $this->Session->setFlash(__('Invalid System.', true));
60                                 $this->redirect(array('controller' => 'character_sheets', 'action' => 'index'));
61                         }
62                         $this->set('this_system', $this_system);
63
64                         // character_sheets絞込み
65                         $this->paginate['conditions']['CharacterSheet.system_id'] = $system_id;
66                 }
67
68                 $characterSheets = $this->paginate();
69
70                 $this->set('characterSheets', $characterSheets);
71
72                 $this->pageTitle .= " - ". __('List of All CharacterSheets', true);
73         }
74
75         function admin_view($id = null) {
76                 if (!$id) {
77                         $this->Session->setFlash(__('Invalid CharacterSheet.', true));
78                         $this->redirect(array('action'=>'index'));
79                 }
80
81                 $characterSheet = $this->CharacterSheet->find('first', array(
82                         'conditions' => array('CharacterSheet.id' => $id),
83                         'fields' => '',
84                         'recursive' => 2,
85                         'contain' => array(
86                                 'System' => array(
87                                         'Character',
88                                 ),
89                         )
90                 ));
91                 if (empty($characterSheet)) {
92                         $this->Session->setFlash(__('Invalid CharacterSheet.', true));
93                         $this->redirect(array('action'=>'index'));
94                 }
95                 $this->set('characterSheets', $characterSheet);
96
97                 // キャラシビュー表示
98                 $skin_dir = $this->getSkinDir($characterSheet['CharacterSheet']['key_name']);
99                 $view_link = null;
100                 if (is_readable($skin_dir. 'view.ctp') && !empty($characterSheet['System']['Character'])) {
101                         $view_link = Router::url(array(
102                                 'controller' => 'characters',
103                                 'action' => 'view',
104                                 'prefix' => Configure::read('Routing.admin'),
105                                 $characterSheet['System']['Character'][0]['id'],
106                                 'mode:'. $characterSheet['CharacterSheet']['key_name']), true
107                         );
108                 }
109                 $this->set('view_link', $view_link);
110
111
112                 $this->pageTitle .= " - ". __('CharacterSheets', true);
113         }
114
115         function admin_add($system_id = null) {
116                 $this_system = array();
117                 if (!empty($system_id)) {
118                         if (!empty($this->data)) {
119                                 $this->data['CharacterSheet']['system_id'] = $system_id;
120                         }
121                         $this_system = $this->CharacterSheet->System->read(null, $system_id);
122                 }
123                 $this->set('this_system', $this_system);
124
125                 if (!empty($this->data)) {
126                         // Dir作れなかった場合のロールバック用
127                         $dataSource = $this->CharacterSheet->getDataSource();
128                         $dataSource->begin($this);
129
130                         $this->CharacterSheet->create();
131                         if ($this->CharacterSheet->save($this->data, array('fieldList' => $this->CharacterSheet->fields['add']))) {
132
133                                 $new_dir = $this->getSkinDir($this->data['CharacterSheet']['key_name']);
134                                 if (@mkdir($new_dir, 0777, true)) {
135                                         $this->Session->setFlash(__('The CharacterSheet has been saved', true));
136                                         $dataSource->commit($this);
137                                 } else {
138                                         $this->Session->setFlash(__('The new directory  has not been saved.', true));
139                                         $dataSource->rollback($this);
140                                 }
141
142                                 $this->redirect(array('action'=>'index', $system_id));
143                         } else {
144                                 $this->Session->setFlash(__('The data could not be saved. Please, try again.', true));
145                         }
146                 }
147
148                 $systems = $this->_get_systems(null);
149                 $this->set(compact('systems'));
150
151                 $this->pageTitle .= " - ". __('Add CharacterSheet', true);
152         }
153
154         function admin_edit($id = null) {
155                 if (!$id && empty($this->data)) {
156                         $this->Session->setFlash(__('Invalid CharacterSheet', true));
157                         $this->redirect(array('action'=>'index'));
158                 }
159                 if (!empty($this->data)) {
160                         if ($this->CharacterSheet->save($this->data, array('fieldList' => $this->CharacterSheet->fields['edit']))) {
161                                 $this->Session->setFlash(__('The CharacterSheet has been saved', true));
162                         } else {
163                                 $this->Session->setFlash(__('The data could not be saved. Please, try again.', true));
164                         }
165                 }
166
167                 $this->redirect(array('action'=>'view', $id));
168         }
169
170         function admin_delete($id = null) {
171                 if (!$id) {
172                         $this->Session->setFlash(__('Invalid id for CharacterSheet', true));
173                         $this->redirect(array('action'=>'index'));
174                 }
175
176                 $characterSheet = $this->CharacterSheet->read(null, $id);
177                 if (empty($characterSheet)) {
178                         $this->Session->setFlash(__('Invalid CharacterSheet.', true));
179                         $this->redirect(array('action'=>'index'));
180                 }
181
182                 if ($this->CharacterSheet->del($id)) {
183                         $dir = $this->getSkinDir($characterSheet['CharacterSheet']['key_name']);
184
185                         @unlink($dir. 'view.ctp');
186                         @unlink($dir. 'admin_view.ctp');
187                         @rmdir($dir);
188                         rmdir($this->getSkinDir($characterSheet['CharacterSheet']['key_name'], false));
189
190                         $this->Session->setFlash(__('CharacterSheet deleted', true));
191                         $this->redirect(array('action'=>'index'));
192                 }
193
194                 $this->pageTitle .= " - ". __('Delete CharacterSheet', true);
195         }
196
197         function admin_upload($id = null) {
198                 if (!$id) {
199                         $this->Session->setFlash(__('Invalid id for CharacterSheet', true));
200                         $this->redirect(array('action'=>'index'));
201                 }
202
203                 $characterSheet = $this->CharacterSheet->read(null, $id);
204                 if (empty($characterSheet)) {
205                         $this->Session->setFlash(__('Invalid CharacterSheet.', true));
206                         $this->redirect(array('action'=>'index'));
207                 }
208
209                 $skin_dir = $this->getSkinDir($characterSheet['CharacterSheet']['key_name']);
210                 $tmpfile = $skin_dir. 'view.tmp';
211                 if (!is_dir($skin_dir)) {
212                         if (!@mkdir($skin_dir, 0777, true)) {
213                         $this->Session->setFlash(__('Invalid CharacterSheet.', true));
214                         }
215                 }
216
217                 if (!empty($this->data['CharacterSheet']['file'])) {
218                         if (empty($this->data['CharacterSheet']['file']['error'])) {
219                                 if (is_uploaded_file($this->data['CharacterSheet']['file']['tmp_name']) && move_uploaded_file($this->data['CharacterSheet']['file']['tmp_name'], $tmpfile)) {
220                                         $upfile = file_get_contents($tmpfile);
221
222                                         /* スキン変換 */
223                                         // Character
224                                         $upfile = $this->_charcterData($upfile);
225
226                                         // Profiles
227                                         // 繰り返し系
228                                         $upfile = $this->_key_name2multiValue($upfile);
229                                         $upfile = $this->_tablekeyname_keyname2tr($upfile);
230                                         // StaticTable
231                                         $upfile = $this->_staticTable_tablekeyname_keyname2value($upfile);
232                                         // key_name単純変換
233                                         $upfile = $this->_key_name2name($upfile);
234                                         $upfile = $this->_key_name2value($upfile);
235
236
237                                         // 出力
238                                         $this->_writeSkinFile($skin_dir. 'view.ctp', $upfile);
239                                         $this->_writeSkinFile($skin_dir. 'admin_view.ctp', $upfile);
240                                         @unlink($tmpfile);
241
242                                         $this->Session->setFlash(__('The new CharacterSheet has been saved.', true));
243                                 } else {
244                                         $this->Session->setFlash(__('The file is invalid.', true));
245                                 }
246                         } else {
247                                 if ($this->data['CharacterSheet']['file']['error'] == UPLOAD_ERR_NO_FILE) {
248                                         $this->Session->setFlash(__('Please upload.', true));
249                                 } else {
250                                         $this->Session->setFlash(sprintf(__('An error occured while transferring the file (ErrorNo:%s)', true), $this->data['CharacterSheet']['file']['error']));
251                                 }
252                         }
253                 }
254
255                 $this->redirect(array('action'=>'view', $id));
256         }
257
258
259         function getSkinDir($dir_name, $isLower = true)
260         {
261                 $skin_dir = APP. 'views'. DS. 'themed'. DS. $dir_name. DS;
262                 if ($isLower) {
263                         $skin_dir .= 'characters'. DS;
264                 }
265                 return  $skin_dir;
266         }
267
268         function _writeSkinFile($file_name, $data)
269         {
270                 $fp = fopen($file_name, "w+");
271                 @fwrite($fp, $data);
272                 @fclose($fp);
273         }
274
275         /* スキン変換 */
276         function __changeSkin($data, $replacement, $pattern)
277         {
278                 if (empty($data) || empty($pattern)) {
279                         return $data;
280                 }
281
282                 if (!empty($replacement)) {
283                         $replacement = '<?php '.$replacement. '; ?>';
284                 }
285
286                 return preg_replace($pattern, $replacement, $data);
287         }
288
289         // Character
290         function _charcterData($data)
291         {
292                 $data = $this->__charcterData($data, 'name');
293                 $data = $this->__charcterData($data, 'notes');
294                 $data = $this->__charcterData($data, 'modified');
295                 $data = $this->__charcterData($data, 'userName');
296
297                 $data = $this->__charcterData($data, 'mainPicture');
298                 $data = $this->__charcterData($data, 'fullPicture');
299
300                 return $data;
301         }
302         function __charcterData($data, $key_name)
303         {
304                 switch($key_name) {
305                         case 'name':
306                                 $pattern = '/{C:name}/';
307                                 $replacement = '\$character[\'Character\'][\'name\']';
308                                 break;
309                         case 'notes':
310                                 $pattern = '/{C:notes}/';
311                                 $replacement = '\$character[\'Character\'][\'notes\']';
312                                 break;
313                         case 'modified':
314                                 $pattern = '/{C:modified:?(.*)?}/';
315                                 $format = 'Y/m/d H:i';
316
317                                 if (preg_match($pattern, $data, $matches)) {
318                                         if (!empty($matches[1])) {
319                                                 if ($matches[1] == 'short') {
320                                                         $replacement = '\$time->niceshort(\$character[\'Character\'][\'modified\'], array(\'format\' => \''. $format. '\'))';
321                                                 } else {
322                                                         $format = $matches[1];
323                                                 }
324                                         }
325                                 }
326
327                                 if (empty($replacement)) {
328                                         $replacement = '\$time->format("'. $format. '", \$character[\'Character\'][\'modified\'])';
329                                 }
330
331                                 break;
332                         case 'userName':
333                                 $pattern = '/{C:userName}/';
334                                 $replacement = '\$character[\'User\'][\'name\']';
335                                 break;
336
337
338                         case 'mainPicture':
339                         case 'fullPicture':
340                                 if ($key_name == 'mainPicture') {
341                                         $element = 'character_picture_image';
342                                         $pattern = '/{C:mainPicture:?(.*)}/';
343                                         $filename = 'main_picture';
344                                 } elseif ($key_name == 'fullPicture') {
345                                         $element = 'character_picture_full';
346                                         $pattern = '/{C:fullPicture:?(.*)}/';
347                                         $filename = 'full_length';
348                                 } else {
349                                         return $data;
350                                         break;
351                                 }
352
353                                 if (preg_match($pattern, $data, $matches)) {
354                                         if (empty($matches[1])) {
355                                                 if ($key_name == 'fullPicture') {
356                                                         $size = 'fullPicture';
357                                                 } else {
358                                                         $size = 'l';
359                                                 }
360                                                 $class = '';
361                                         } else {
362                                                 $arr = explode(":", $matches[1]);
363                                                 if (isset($arr[1])) {
364                                                         $size = $arr[0];
365                                                         $class = $arr[1];
366                                                 } else {
367                                                         $size = $matches[1];
368                                                         $class = '';
369                                                 }
370                                         }
371                                 } else {
372                                         return $data;
373                                         break;
374                                 }
375
376                                 $replacement = '\$this->renderElement(\''. $element. '\', array(\'basename\' => \$character[\'Character\'][\''. $filename. '\'], \'options\' => array(\'previewVersion\' => \''. $size. '\', \'class\' => \''. $class. '\', \'nodata\' => \'image\')));';
377                                 break;
378                         default:
379                                 return $data;
380                                 break;
381                 }
382
383                 return $this->__changeSkin($data, "echo ".$replacement, $pattern);
384         }
385
386         // Profiles
387         // 単純表示
388         function _key_name2value($data)
389         {
390                 $pattern = '/{([_a-z0-9]+)}/';
391                 $replacement = '\$characterSheet->profileValue4key_name(\$character["System"]["Profile"], "${1}")';
392
393                 return $this->__changeSkin($data, $replacement, $pattern);
394         }
395         function _key_name2name($data)
396         {
397                 $pattern = '/{([_a-z0-9]+):title}/';
398                 $replacement = '\$characterSheet->profileName4key_name(\$character["System"]["Profile"], "${1}")';
399
400                 return $this->__changeSkin($data, $replacement, $pattern);
401         }
402
403         // StaticTable
404         function _staticTable_tablekeyname_keyname2value($data)
405         {
406                 $pattern = '/{ST:([_a-z0-9]+):([_a-z0-9]+):([0-9]+)}/';
407                 $replacement = '\$characterSheet->staticTable4row_tablekey_name(\$character["System"]["Profile"], "${1}", "${2}", ${3})';
408
409                 return $this->__changeSkin($data, $replacement, $pattern);
410         }
411
412         // 繰り返し表示
413         function _key_name2multiValue($data)
414         {
415                 $pattern = '/{M}(.*){([_a-z0-9]+)}(.*){\/M}/';
416                 $replacement = '\$characterSheet->multiInput4key_name(\$character["System"]["Profile"], "${2}", \'${1}%s${3}\')';
417
418                 return $this->__changeSkin($data, $replacement, $pattern);
419         }
420
421         // テーブル
422         // 繰り返し表示
423         function _tablekeyname_keyname2tr($data)
424         {
425                 $pattern = '/{T:([_a-z0-9]+)}(.*){\/T}/';
426
427                 return preg_replace_callback($pattern, array($this, '__tablePattern2key_names'), $data);
428         }
429
430         function __tablePattern2key_names($matches)
431         {
432                 $tablekey_name = $matches[1];
433                 $key_names_parts = $matches[2];
434
435                 $pattern = '/{([_0-9a-z]+)}/';
436
437                 preg_match_all($pattern, $key_names_parts, $key_matches);
438                 $keys = $key_matches[1];
439
440                 $keys_str = 'array(';
441                 foreach ($keys as $k => $v) {
442                         if ($k != 0) {
443                                 $keys_str .= ', ';
444                         }
445                         $keys_str .= '"'. $v. '"';
446                 }
447                 $keys_str .= ')';
448
449                 $replacement = preg_replace($pattern, '%s', $key_names_parts);
450                 $replacement = '$characterSheet->table4tablekey_name($character["System"]["Profile"], "'.$tablekey_name.'", '. $keys_str. ', \''. $replacement. '\')';
451                 $replacement = '<?php '.$replacement. '; ?>';
452
453                 return $replacement;
454         }
455 }
456