2 class CharacterSheetsController extends AppController {
4 var $name = 'CharacterSheets';
10 var $paginate = array(
11 'conditions' => array(
21 'CharacterSheet.system_id' => 'asc',
22 'CharacterSheet.sort_order' => 'asc',
26 var $disableTokenActions = array();
31 function beforeFilter() {
33 parent::beforeFilter();
38 function beforeRender()
40 parent::beforeRender();
42 $this->set_public_flag4view();
47 function admin_index($system_id = null) {
49 if (!empty($system_id)) {
51 $this_system = $this->CharacterSheet->System->find('first', array(
52 'conditions' => array(
53 'System.id' => $system_id,
58 if (!isset($this_system['System'])) {
59 $this->Session->setFlash(__('Invalid System.', true));
60 $this->redirect(array('controller' => 'character_sheets', 'action' => 'index'));
62 $this->set('this_system', $this_system);
64 // character_sheets絞込み
65 $this->paginate['conditions']['CharacterSheet.system_id'] = $system_id;
68 $characterSheets = $this->paginate();
70 $this->set('characterSheets', $characterSheets);
72 $this->pageTitle .= " - ". __('List of All CharacterSheets', true);
75 function admin_view($id = null) {
77 $this->Session->setFlash(__('Invalid CharacterSheet.', true));
78 $this->redirect(array('action'=>'index'));
81 $characterSheet = $this->CharacterSheet->find('first', array(
82 'conditions' => array('CharacterSheet.id' => $id),
91 if (empty($characterSheet)) {
92 $this->Session->setFlash(__('Invalid CharacterSheet.', true));
93 $this->redirect(array('action'=>'index'));
95 $this->set('characterSheets', $characterSheet);
98 $skin_dir = $this->getSkinDir($characterSheet['CharacterSheet']['key_name']);
100 if (is_readable($skin_dir. 'view.ctp') && !empty($characterSheet['System']['Character'])) {
101 $view_link = Router::url(array(
102 'controller' => 'characters',
104 'prefix' => Configure::read('Routing.admin'),
105 $characterSheet['System']['Character'][0]['id'],
106 'mode:'. $characterSheet['CharacterSheet']['key_name']), true
109 $this->set('view_link', $view_link);
112 $this->pageTitle .= " - ". __('CharacterSheets', true);
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;
121 $this_system = $this->CharacterSheet->System->read(null, $system_id);
123 $this->set('this_system', $this_system);
125 if (!empty($this->data)) {
126 // Dir作れなかった場合のロールバック用
127 $dataSource = $this->CharacterSheet->getDataSource();
128 $dataSource->begin($this);
130 $this->CharacterSheet->create();
131 if ($this->CharacterSheet->save($this->data, array('fieldList' => $this->CharacterSheet->fields['add']))) {
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);
138 $this->Session->setFlash(__('The new directory has not been saved.', true));
139 $dataSource->rollback($this);
142 $this->redirect(array('action'=>'index', $system_id));
144 $this->Session->setFlash(__('The data could not be saved. Please, try again.', true));
148 $systems = $this->_get_systems(null);
149 $this->set(compact('systems'));
151 $this->pageTitle .= " - ". __('Add CharacterSheet', true);
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'));
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));
163 $this->Session->setFlash(__('The data could not be saved. Please, try again.', true));
167 $this->redirect(array('action'=>'view', $id));
170 function admin_delete($id = null) {
172 $this->Session->setFlash(__('Invalid id for CharacterSheet', true));
173 $this->redirect(array('action'=>'index'));
176 $characterSheet = $this->CharacterSheet->read(null, $id);
177 if (empty($characterSheet)) {
178 $this->Session->setFlash(__('Invalid CharacterSheet.', true));
179 $this->redirect(array('action'=>'index'));
182 if ($this->CharacterSheet->del($id)) {
183 $dir = $this->getSkinDir($characterSheet['CharacterSheet']['key_name']);
185 @unlink($dir. 'view.ctp');
186 @unlink($dir. 'admin_view.ctp');
188 rmdir($this->getSkinDir($characterSheet['CharacterSheet']['key_name'], false));
190 $this->Session->setFlash(__('CharacterSheet deleted', true));
191 $this->redirect(array('action'=>'index'));
194 $this->pageTitle .= " - ". __('Delete CharacterSheet', true);
197 function admin_upload($id = null) {
199 $this->Session->setFlash(__('Invalid id for CharacterSheet', true));
200 $this->redirect(array('action'=>'index'));
203 $characterSheet = $this->CharacterSheet->read(null, $id);
204 if (empty($characterSheet)) {
205 $this->Session->setFlash(__('Invalid CharacterSheet.', true));
206 $this->redirect(array('action'=>'index'));
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));
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);
224 $upfile = $this->_charcterData($upfile);
228 $upfile = $this->_key_name2multiValue($upfile);
229 $upfile = $this->_tablekeyname_keyname2tr($upfile);
231 $upfile = $this->_staticTable_tablekeyname_keyname2value($upfile);
233 $upfile = $this->_key_name2name($upfile);
234 $upfile = $this->_key_name2value($upfile);
238 $this->_writeSkinFile($skin_dir. 'view.ctp', $upfile);
239 $this->_writeSkinFile($skin_dir. 'admin_view.ctp', $upfile);
242 $this->Session->setFlash(__('The new CharacterSheet has been saved.', true));
244 $this->Session->setFlash(__('The file is invalid.', true));
247 if ($this->data['CharacterSheet']['file']['error'] == UPLOAD_ERR_NO_FILE) {
248 $this->Session->setFlash(__('Please upload.', true));
250 $this->Session->setFlash(sprintf(__('An error occured while transferring the file (ErrorNo:%s)', true), $this->data['CharacterSheet']['file']['error']));
255 $this->redirect(array('action'=>'view', $id));
259 function getSkinDir($dir_name, $isLower = true)
261 $skin_dir = APP. 'views'. DS. 'themed'. DS. $dir_name. DS;
263 $skin_dir .= 'characters'. DS;
268 function _writeSkinFile($file_name, $data)
270 $fp = fopen($file_name, "w+");
276 function __changeSkin($data, $replacement, $pattern)
278 if (empty($data) || empty($pattern)) {
282 if (!empty($replacement)) {
283 $replacement = '<?php '.$replacement. '; ?>';
286 return preg_replace($pattern, $replacement, $data);
290 function _charcterData($data)
292 $data = $this->__charcterData($data, 'name');
293 $data = $this->__charcterData($data, 'notes');
294 $data = $this->__charcterData($data, 'modified');
295 $data = $this->__charcterData($data, 'userName');
297 $data = $this->__charcterData($data, 'mainPicture');
298 $data = $this->__charcterData($data, 'fullPicture');
302 function __charcterData($data, $key_name)
306 $pattern = '/{C:name}/';
307 $replacement = '\$character[\'Character\'][\'name\']';
310 $pattern = '/{C:notes}/';
311 $replacement = '\$character[\'Character\'][\'notes\']';
314 $pattern = '/{C:modified:?(.*)?}/';
315 $format = 'Y/m/d H:i';
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. '\'))';
322 $format = $matches[1];
327 if (empty($replacement)) {
328 $replacement = '\$time->format("'. $format. '", \$character[\'Character\'][\'modified\'])';
333 $pattern = '/{C:userName}/';
334 $replacement = '\$character[\'User\'][\'name\']';
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';
353 if (preg_match($pattern, $data, $matches)) {
354 if (empty($matches[1])) {
355 if ($key_name == 'fullPicture') {
356 $size = 'fullPicture';
362 $arr = explode(":", $matches[1]);
363 if (isset($arr[1])) {
376 $replacement = '\$this->renderElement(\''. $element. '\', array(\'basename\' => \$character[\'Character\'][\''. $filename. '\'], \'options\' => array(\'previewVersion\' => \''. $size. '\', \'class\' => \''. $class. '\', \'nodata\' => \'image\')));';
383 return $this->__changeSkin($data, "echo ".$replacement, $pattern);
388 function _key_name2value($data)
390 $pattern = '/{([_a-z0-9]+)}/';
391 $replacement = '\$characterSheet->profileValue4key_name(\$character["System"]["Profile"], "${1}")';
393 return $this->__changeSkin($data, $replacement, $pattern);
395 function _key_name2name($data)
397 $pattern = '/{([_a-z0-9]+):title}/';
398 $replacement = '\$characterSheet->profileName4key_name(\$character["System"]["Profile"], "${1}")';
400 return $this->__changeSkin($data, $replacement, $pattern);
404 function _staticTable_tablekeyname_keyname2value($data)
406 $pattern = '/{ST:([_a-z0-9]+):([_a-z0-9]+):([0-9]+)}/';
407 $replacement = '\$characterSheet->staticTable4row_tablekey_name(\$character["System"]["Profile"], "${1}", "${2}", ${3})';
409 return $this->__changeSkin($data, $replacement, $pattern);
413 function _key_name2multiValue($data)
415 $pattern = '/{M}(.*){([_a-z0-9]+)}(.*){\/M}/';
416 $replacement = '\$characterSheet->multiInput4key_name(\$character["System"]["Profile"], "${2}", \'${1}%s${3}\')';
418 return $this->__changeSkin($data, $replacement, $pattern);
423 function _tablekeyname_keyname2tr($data)
425 $pattern = '/{T:([_a-z0-9]+)}(.*){\/T}/';
427 return preg_replace_callback($pattern, array($this, '__tablePattern2key_names'), $data);
430 function __tablePattern2key_names($matches)
432 $tablekey_name = $matches[1];
433 $key_names_parts = $matches[2];
435 $pattern = '/{([_0-9a-z]+)}/';
437 preg_match_all($pattern, $key_names_parts, $key_matches);
438 $keys = $key_matches[1];
440 $keys_str = 'array(';
441 foreach ($keys as $k => $v) {
445 $keys_str .= '"'. $v. '"';
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. '; ?>';