OSDN Git Service

プロフィールのデフォルト入力機能追加
[trpgtools-onweb/cake-frame.git] / app / controllers / default_profiles_controller.php
1 <?php
2 /*
3  * PHP version 5
4  *
5  * @copyright Copyright 2010, Cake. (http://trpgtools-onweb.sourceforge.jp/)
6  * @category Controller
7  * @package  TRPG Data Bank
8  * @version  beta
9  * @author   Cake <cake_67@users.sourceforge.jp>
10  * @license  http://www.opensource.org/licenses/mit-license.php The MIT License
11  * @link     http://trpgtools-onweb.sourceforge.jp/
12  */
13
14 class DefaultProfilesController extends AppController {
15
16         var $name = 'DefaultProfiles';
17
18         var $helpers = array(
19                 'Profiledisp',
20                 'Select',
21         );
22
23         var $disableTokenActions = array();
24
25         /* メソッド */
26
27         function beforeFilter() {
28                 parent::beforeFilter();
29
30                 // Cache
31                 $this->cacheAction = array(
32                 );
33
34                 // 認証なしアクセス可
35         }
36
37         function beforeRender()
38         {
39                 parent::beforeRender();
40
41                 $this->set_public_flag4view();
42         }
43
44         /* アクションメソッド */
45         function admin_edit($profile_id = null) {
46                 if (!$profile_id ) {
47                         $this->Session->setFlash(__('Invalid data.', true));
48                         $this->redirect(array('controller' => 'systems', 'action'=>'admin_index'));
49                 }
50
51                 $profile = $this->get_profile4profile_id($profile_id);
52                 if (empty($profile)) {
53                         $this->Session->setFlash(__('Invalid Profile', true));
54                         $this->redirect(array('controller' => 'systems', 'action'=>'admin_index'));
55                 }
56
57                 // 「その他」システム設定
58                 $this->checkEditOtherSystem($profile['Profile']['system_id']);
59
60                 if (!empty($this->data)) {
61                         $this->post_data = $this->data;
62                         $this->data = array();
63
64                         $allow_blank = false;
65                         if ($profile['Profile']['profile_type'] == 'checkbox') {
66                                 $allow_blank = true;
67                         }
68                         $this->data['CharactersHasProfile'] = $this->_set_new_characters_has_profile($this->post_data['CharactersHasProfile'], $profile_id, array(), $allow_blank);
69
70                         foreach ($this->data['CharactersHasProfile'] as $k => $v) {
71                                 $this->data['DefaultProfile'][$k]['profile_id'] = $profile_id;
72                                 if (!empty($v['profile_select_id'])) {
73                                         $this->data['DefaultProfile'][$k]['profile_select_id'] = $v['profile_select_id'];
74                                 }
75                                 if (!empty($v['profile_table_id'])) {
76                                         $this->data['DefaultProfile'][$k]['profile_table_id'] = $v['profile_table_id'];
77                                 }
78                                 if (!empty($v['profile_table_static_id'])) {
79                                         $this->data['DefaultProfile'][$k]['profile_table_static_id'] = $v['profile_table_static_id'];
80                                 }
81
82                                 if (!isset($v['value'])) {
83                                         $this->data['DefaultProfile'][$k]['value'] = '';
84                                 } else {
85                                         $this->data['DefaultProfile'][$k]['value'] = $v['value'];
86                                 }
87
88                                 if (!isset($v['public_flag'])) {
89                                         $this->data['DefaultProfile'][$k]['public_flag'] = 'public';
90                                 } else {
91                                         $this->data['DefaultProfile'][$k]['public_flag'] = $v['public_flag'];
92                                 }
93                         }
94                         unset($this->data["CharactersHasProfile"]);
95
96 //                      $this->data['DefaultProfile']['__Token'] = $this->post_data['DefaultProfile']['__Token'];
97
98                         /* validate */
99                         if ($this->DefaultProfile->saveAll($this->data['DefaultProfile'], array('validate' => 'only'))) {
100
101                                 // 現在のdefault_profiles削除
102                                 $this->DefaultProfile->deleteAll(array(
103                                         'DefaultProfile.profile_id' => $profile_id
104                                 ));
105
106                                 /* データ保存 */
107                                 $this->DefaultProfile->create();
108                                 if ($this->DefaultProfile->saveAll($this->data['DefaultProfile'], array(
109                                         'validate' => false,
110                                          'fieldList' => $this->DefaultProfile->fields['edit'], 
111                                 ))) {
112                                         $this->Session->setFlash(__('The default profile has been saved', true));
113 //                                      $this->redirect(array('controller' => 'systems', 'action' => 'view', $profile['Profile']['system_id']));
114                                         $this->redirect(array('action' => 'edit', $profile_id));
115                                 }       
116
117                                 $this->Session->setFlash(__('The default profile could not be saved. Please, try again.', true));
118                                 $this->data = $this->post_data;
119                         }
120                 }
121
122                 $this->data['System'] = $profile['System'];
123                 unset($profile['System']);
124
125                 $profile['Profile']['CharactersHasProfile'] = $profile['DefaultProfile'];
126
127                 $profile['Profile']['ProfileSelect'] = $profile['ProfileSelect'];
128                 unset($profile['ProfileSelect']);
129
130                 $profile['Profile']['ProfileTable'] = $profile['ProfileTable'];
131                 unset($profile['ProfileTable']);
132
133                 $this->data['System']['Profile'][0] = $profile['Profile'];
134
135                 $this->set('form_nums', count($profile['Profile']['CharactersHasProfile']) + 3);
136
137                 $this->set('title_for_layout', " - ". sprintf(__('DefaultProfile for %s %s', true), $this->data['System']['name'], $this->data['System']['Profile'][0]['name']));
138
139         }
140
141         function admin_delete($id = null) {
142                 if (!$id) {
143                         $this->Session->setFlash(__('Invalid id for default profile', true));
144                         $this->redirect(array('action'=>'index'));
145                 }
146                 if ($this->DefaultProfile->delete($id)) {
147                         $this->Session->setFlash(__('Default profile deleted', true));
148                         $this->redirect(array('action'=>'index'));
149                 }
150                 $this->Session->setFlash(__('Default profile was not deleted', true));
151                 $this->redirect(array('action' => 'index'));
152         }
153
154
155         function get_profile4profile_id($profile_id)
156         {
157                 if (!$profile_id) {
158                         $this->Session->setFlash(__('Invalid ProfileTable.', true));
159                         $this->redirect(array('controller' => 'systems', 'action'=>'admin_index'));
160                 }
161
162                 $profile = $this->DefaultProfile->Profile->find('first', array(
163                         'conditions' => array(
164                                 'Profile.id' => $profile_id,
165                         ),
166                         'contain' => array(
167                                 'System',
168                                 'DefaultProfile',
169                                 'ProfileSelect',
170                                 'ProfileTable' => array(
171                                         'ProfileTableStatic',
172                                 ),
173                         ),
174                 ));
175
176                 if (!empty($profile['ProfileSelect'])) {
177                         foreach ($profile['ProfileSelect'] as $k => $v) {
178
179                                 if (isset($v['sort_order'])  && !empty($v['sort_order'])) {
180                                         $sort_order[$k] = $v['sort_order'];
181                                 } else {
182                                         $sort_order[$k] = 0;
183                                 }
184                         }
185                         if (!empty($sort_order)) {
186                                 $profile['ProfileSelect'] = $this->sort4sort_order($profile['ProfileSelect'], $sort_order);
187                         }
188                 }
189                 if (!empty($profile['ProfileTable'])) {
190                         foreach ($profile['ProfileTable'] as $k => $v) {
191
192                                 if (isset($v['sort_order'])  && !empty($v['sort_order'])) {
193                                         $sort_order[$k] = $v['sort_order'];
194                                 } else {
195                                         $sort_order[$k] = 0;
196                                 }
197                         }
198                         if (!empty($sort_order)) {
199                                 $profile['ProfileTable'] = $this->sort4sort_order($profile['ProfileTable'], $sort_order);
200                         }
201                 }
202
203                 return $profile;
204         }
205 }
206