OSDN Git Service

6f72f358a624b79c5cbd9b4f824c2912aa177b98
[trpgtools-onweb/cake-frame.git] / app / models / character.php
1 <?php
2 /*
3  * PHP version 5
4  *
5  * @copyright Copyright 2010, Cake. (http://trpgtools-onweb.sourceforge.jp/)
6  * @category Model
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 Character extends AppModel {
15
16         var $name = 'Character';
17
18         var $status = array(
19                 'active',
20                 'inactive',
21         );
22
23         var $status2 = array(
24         );
25
26         var $fields = array(
27                 'add' => array('name', 'system_id', 'sort_order', 'status', 'public_flag', 'user_id', 'notes', 'secret_notes'),
28                 'edit' => array('name', 'sort_order', 'status', 'public_flag', 'notes', 'secret_notes'),
29                 'escape' => array(
30                         'name' => array(
31                                 'html' => true,
32                                 'tags' => true,
33                                 'all' => true,
34                         ),
35                         'notes' => array(
36                                 'html' => true,
37                                 'tags' => true,
38                                 'images' => true,
39                                 'sctipts' => true,
40                         ),
41                         'secret_notes' => array(
42                                 'html' => true,
43                                 'tags' => true,
44                                 'images' => true,
45                                 'sctipts' => true,
46                         ),
47                 ),
48                 // other 'system_id', 'deleted''deleted_date' 
49         );
50
51         var $validate = array(
52                 'name' => array(
53                         'notEmpty' => array(
54                                 'rule' => 'notEmpty',
55                         ),
56                 ),
57                 'system_id' => array(
58                         'validSystemId' => array(
59                                 'rule' => array('validSystemId', true),
60                         ),
61                 ),
62                 'sort_order' => array(
63                         'numeric' => array(
64                                 'rule' => 'numeric',
65                                 'allowEmpty' => true,
66                         ),
67                 ),
68                 'status' => array(
69                         'validStatus' => array(
70                                 'rule' => 'validStatus',
71                                 'allowEmpty' => true,
72                         ),
73                 ),
74                 'public_flag' => array(
75                         'publicFlag' => array(
76                                 'rule' => 'publicFlag',
77                                 'allowEmpty' => true,
78                         ),
79                 ),
80                 'user_id' => array(
81                         'validUserId' => array(
82                                 'rule' => 'validUserId',
83                         ),
84                 ),
85         );
86
87         //The Associations below have been created with all possible keys, those that are not needed can be removed
88         var $belongsTo = array(
89                 'System' => array(
90                         'className' => 'System',
91                         'foreignKey' => 'system_id',
92                         'conditions' => array(
93                                 'System.public_flag' => 'public',
94                         ),
95                         'fields' => array(
96                                 'System.id', 
97                                 'System.name', 
98                                 'System.public_flag',
99                                 'System.set_npc',
100                                 'System.copyright',
101                         ),
102                 ),
103                 'User' => array(
104                         'className' => 'User',
105                         'foreignKey' => 'user_id',
106                         'conditions' => '',
107                         'fields' => array(
108                                 'User.id', 
109                                 'User.name'
110                         ),
111                 )
112         );
113
114         var $hasMany = array(
115                 // キャライメージ
116                 'CharacterPicture' => array(
117                         'className' => 'CharacterPicture',
118                         'foreignKey' => 'character_id',
119                         'dependent' => false,
120                         'conditions' => array(
121                                 'CharacterPicture.public_flag' => 'public',
122                         ),
123                         'fields' => array(
124                                 'CharacterPicture.id',
125                                 'CharacterPicture.public_flag',
126                                 'CharacterPicture.created',
127                         ),
128                         'order' => array('CharacterPicture.id' => 'asc'),
129                         'limit' => '',
130                         'offset' => '',
131                         'finderQuery' => '',
132                         'deleteQuery' => '',
133                         'insertQuery' => ''
134                 ),
135                 'CharactersHasProfile' => array(
136                         'className' => 'CharactersHasProfile',
137                         'foreignKey' => 'character_id',
138                         'dependent' => false,
139                         'conditions' => '',
140                         'fields' => '',
141                         'order' => array('CharactersHasProfile.id' => 'asc'),
142                         'limit' => '',
143                         'offset' => '',
144                         'finderQuery' => '',
145                         'deleteQuery' => '',
146                         'insertQuery' => ''
147                 ),
148                 'CharacterProfileArchive' => array(
149                         'className' => 'CharacterProfileArchive',
150                         'foreignKey' => 'character_id',
151                         'dependent' => false,
152                         'conditions' => array(
153                                 'CharacterProfileArchive.deleted' => '0',
154                                 'CharacterProfileArchive.public_flag' => 'public',
155                         ),
156                         'fields' => array(
157                                 'CharacterProfileArchive.id',
158                                 'CharacterProfileArchive.title',
159                                 'CharacterProfileArchive.public_flag',
160                                 'CharacterProfileArchive.disp_date',
161                         ),
162                         'order' => array(
163                                 'CharacterProfileArchive.disp_date' => 'desc',
164                         ),
165                         'limit' => '5',
166                         'offset' => '',
167                         'exclusive' => '',
168                         'finderQuery' => '',
169                         'counterQuery' => ''
170                 ),
171         );
172
173         /* validation Rule */
174         function validStatus($data) 
175         {
176                 if (isset($data["status"]) && in_array($data["status"], $this->status)) {
177                         return true;
178                 } else {
179                         return false;
180                 }
181         }
182
183         /* check PublicFlag Setting */
184         function publicFlagDefault($data)
185         {
186                 if (isset($data["public_flag_default"]) && in_array($data["public_flag_default"], $this->public_flag)) {
187                         return true;
188                 } else {
189                         return false;
190                 }
191         }
192
193         /* コールバックメソッド */
194         function beforeSave($options = array())
195         {
196                 if (empty($this->data['Character']['sort_order'])) {
197                         $this->data['Character']['sort_order'] = 0;
198                 }
199
200                 $result = parent::beforeSave($options);
201                 if ($result === false) {
202                         return $result;
203                 }
204
205                 // textarea
206                 if (!empty($this->data['Character']['notes'])) {
207                         $this->data['Character']['notes'] = str_replace(array("\n\r", '\n', "\r"), '<br />', $this->data['Character']['notes']);
208                 }
209                 if (!empty($this->data['Character']['secret_notes'])) {
210                         $this->data['Character']['secret_notes'] = str_replace(array("\n\r", '\n', "\r"), '<br />', $this->data['Character']['secret_notes']);
211                 }
212
213                 return $result;
214         }
215
216         function afterSave($created) {
217                 $this->deleteCache4Charcter();
218
219                 return parent::afterSave($created);
220         }
221         function afterDelete() {
222                 $this->deleteCache4Charcter();
223
224                 return parent::afterDelete();
225         }
226
227         /* 共通関数 */
228         function get_character($id, $conditions = array(), $isAdmin = false, $contain = array())
229         {
230                 $conditions = array_merge(
231                         array(
232                                 'Character.id' => $id,
233                                 'Character.deleted' => 0,
234                         ), $conditions
235                 );
236
237                 $this->System->Profile->hasMany['CharactersHasProfile']['conditions']['CharactersHasProfile.character_id'] = $id;
238                 unset($this->belongsTo['System']['conditions']['System.public_flag']);
239                 unset($this->hasMany['CharacterPicture']['conditions']['CharacterPicture.public_flag']);
240                 if ($isAdmin === true) {
241                         unset($this->System->hasMany['CharacterSheet']['conditions']['CharacterSheet.public_flag']);
242                         unset($this->hasMany['CharacterProfileArchive']['conditions']['CharacterProfileArchive.public_flag']);
243                 }
244
245                 $contain = array_merge(array(
246                                 'User',
247                                 'CharacterPicture' => array(
248                                         'Attachment',
249                                 ),
250                                 'System' => array(
251                                         'CharacterSheet',
252                                         'Profile' => array(
253                                                 'ProfileSelect',
254                                                 'ProfileTable' => array(
255                                                         'ProfileTableStatic',
256                                                 ),
257                                                 'CharactersHasProfile',
258                                         ),
259                                 ),
260                                 'CharacterProfileArchive',
261                         ), $contain
262                 );
263                 $character = $this->find('first', array(
264                         'conditions' => $conditions,
265                         'contain' => $contain,
266                         'recursive' => 4,
267                 ));
268
269                 return $character;
270         }
271
272         function set_profiles2view($orig_character, $isArchive = false)
273         {
274                 if (!empty($orig_character['System']['Profile'])) {
275                         $new_profile = array();
276                         $sort_order = array();
277                         foreach ($orig_character['System']['Profile'] as $k => $profile) {
278                                 if (isset($profile['sort_order'])  && !empty($profile)) {
279                                         $sort_order[$k] = $profile['sort_order'];
280                                 } else {
281                                         $sort_order[$k] = 0;
282                                 }
283
284                                 $new_profile[$profile['key_name']]['id'] = $profile['id'];
285                                 $new_profile[$profile['key_name']]['name'] = $profile['name'];
286                                 $new_profile[$profile['key_name']]['profile_type'] = $profile['profile_type'];
287                                 if (!empty($profile['ProfileTable'])) {
288                                         $new_profile[$profile['key_name']]['ProfileTable'] = $profile['ProfileTable'];
289                                 }
290                                 if ($profile['profile_type'] == 's-table' && empty($profile['CharactersHasProfile'])) {
291                                         if (isset($profile['ProfileTable'][0]['ProfileTableStatic'])) {
292                                                 foreach ($profile['ProfileTable'][0]['ProfileTableStatic'] as $k2 => $v2) {
293                                                         $new_profile[$profile['key_name']]['CharactersHasProfile'][] = $this->_set_blank4static_table($profile['ProfileTable'][0]['ProfileTableStatic'][$k2]['title'], $profile['ProfileTable'][0]['profile_id'], $profile['ProfileTable'][0]['id']);
294                                                         for ($i=1;$i<count($profile['ProfileTable']);$i++) {
295                                                                 $new_profile[$profile['key_name']]['CharactersHasProfile'][] = $this->_set_blank4static_table(null, $profile['ProfileTable'][$i]['profile_id'], $profile['ProfileTable'][$i]['id']);
296
297                                                         }
298                                                 }
299                                         } else {
300                                                 $new_profile[$profile['key_name']]['CharactersHasProfile'] = null;
301                                         }
302                                 } else {
303                                         $new_profile[$profile['key_name']]['CharactersHasProfile'] = $profile['CharactersHasProfile'];
304                                 }
305                         }
306
307                         array_multisort($sort_order, SORT_ASC, $new_profile);
308
309                         $orig_character['System']['Profile'] = $new_profile;
310                 }
311
312                 $character['Character'] = $orig_character['Character'];
313                 if ($isArchive === true) {
314 //                      beforeSerialize($character['Character']['name']);
315 //                      beforeSerialize($character['Character']['notes']);
316                 }
317
318                 if (isset($orig_character['CharacterPicture'])) {
319                         $character['CharacterPicture'] = $orig_character['CharacterPicture'];
320                 }
321                 if (isset($orig_character['CharacterProfileArchive']) && $isArchive !== true) {
322                         $character['CharacterProfileArchive'] = $orig_character['CharacterProfileArchive'];
323                 }
324                 if (isset($orig_character['System'])) {
325                         if ($isArchive === true) {
326                                 unset($orig_character['System']['CharacterSheet']);
327                                 unset($orig_character['System']['copyright']);
328                                 unset($orig_character['System']['detail']);
329                                 unset($orig_character['System']['ad']);
330                         }
331
332                         $character['System'] = $orig_character['System'];
333                 }
334
335                 if (isset($orig_character['User'])) {
336                         $character['User'] = $orig_character['User'];
337                 }
338
339                 return $character;
340         }
341
342         function _set_blank4static_table($value, $profile_id, $profile_table_id)
343         {
344                 return array(
345                         'profile_id' => $profile_id,
346                         'profile_table_id' => $profile_table_id,
347                         'value' => $value,
348                         'public_flag' => 'public',
349                 );
350         }
351
352         function saveCharacterProfile($id, $params = array())
353         {
354                 if (empty($id)) {
355                         return false;
356                 }
357
358                 if (isset($params['CharacterProfileArchive'])) {
359                         $params = array($params['CharacterProfileArchive']);
360                 } else {
361                         $params = array();
362                 }
363
364                 $archive['CharacterProfileArchive']['character_id'] = $id;
365
366                 $character = $this->get_character($id, array(), true);
367
368                 $newData = $this->set_profiles2view($character, true);
369                 array_walk_recursive($newData, 'beforeSerialize');
370
371                 $archive['CharacterProfileArchive']['value'] = serialize($newData);
372
373                 $archive['CharacterProfileArchive']['title'] = $params[0]['title'];
374                 $archive['CharacterProfileArchive']['memo'] = $params[0]['memo'];
375
376                 if (isset($params['disp_date'])) {
377                         $archive['CharacterProfileArchive']['disp_date'] = $params['disp_date'];
378                 } else {
379                         $archive['CharacterProfileArchive']['disp_date'] = date('Y-m-d H:i:s');
380                 }
381
382                 if ($character['Character']['public_flag'] != 'public') {
383                         $archive['CharacterProfileArchive']['public_flag'] = $character['Character']['public_flag'];
384                 } else {
385                         $archive['CharacterProfileArchive']['public_flag'] = $params[0]['public_flag'];
386                 }
387
388                 $this->CharacterProfileArchive->create();
389                 if ($this->CharacterProfileArchive->save($archive, array('fieldList' => $this->CharacterProfileArchive->fields['add']))) {
390                         return true;
391                 } else {
392                         return false;
393                 }
394         }
395
396         /* Paginate */
397         function paginateCount($conditions = null, $recursive = 0, $extra = array()) {
398                 if ($this->checkSearchProfile($conditions)) {
399                         unset($conditions['profile_search']);
400
401                         $character_ids = $this->_getCharactersByCharacterProfiles($conditions);
402
403                         return count($character_ids);
404                 } else {
405                         // 元のpaginateCount(controller.phpより)
406                         $parameters = compact('conditions');
407                         if ($recursive != $this->recursive) {
408                                 $parameters['recursive'] = $recursive;
409                         }
410                         $count = $this->find('count', array_merge($parameters, $extra));
411                         return $count;
412                 }
413         }
414
415         function paginate($conditions = array(), $fields = null, $order = null, $limit = 20, $page = 1, $recursive = -1, $extra = array()) {
416                 if ($this->checkSearchProfile($conditions)) {
417                         unset($conditions['profile_search']);
418
419                         $character_ids =  $this->_getCharactersByCharacterProfiles($conditions);
420
421                         if (empty($character_ids)) {
422                                 return array();
423                         }
424
425                         $conditions = array(
426                                 'Character.id' => array_values($character_ids),
427                         );
428                 }
429
430                 $params = array(
431                         'conditions' => $conditions,
432                         'fields' => $fields,
433                         'order' => $order,
434                         'limit' => $limit,
435                         'page' => $page,
436                 );
437                 if (isset($extra['contain'])) {
438                         $params['contain'] = $extra['contain'];
439                 }
440
441                 $results = $this->find('all', $params);
442
443                 return $results;
444         }
445
446         function checkSearchProfile($conditions = array())
447         {
448                 if (isset($conditions['profile_search'])) {
449                         return true;
450                 } else {
451                         return false;
452                 }
453         }
454
455         function _getCharactersByCharacterProfiles($conditions = null)
456         {
457                 static $character_ids;
458                 static $prev_conditions;
459
460                 if (!empty($character_ids) && $prev_conditions == $conditions)  {
461                         return $character_ids;
462                 }
463
464                 $prev_conditions = $conditions;
465
466                 $this->CharactersHasProfile->unbindModel(
467                         array('belongsTo' => array(
468                                 'Profile',
469                                 'ProfileSelect',
470                                 'ProfileTable',
471                                 'ProfileTableStatic',
472                         )
473                 ));
474                 return $character_ids = $this->CharactersHasProfile->find('list', array(
475                         'conditions' => $conditions,
476                         'fields' => array('CharactersHasProfile.character_id'),
477                         'group' => ('CharactersHasProfile.character_id'),
478                         'recursive' => 1,
479                 ));
480         }
481
482         /* キャッシュ削除 */
483         function deleteCache4Charcter()
484         {
485                 // 自セッション関連
486                 $this->deleteCacheMyData();
487
488                 $this->deleteCacheCharacter($this->id, false);
489
490                 if (isset($this->data['clearCache'])) {
491                         $this->deleteCacheHome($this->data['clearCache']['user_id']);
492                         $this->deleteCacheUser($this->data['clearCache']['user_id']);
493                         $this->deleteCacheSystem($this->data['clearCache']['system_id'], false); // System一覧の更新遅れを修正する場合true
494                 } else {
495                         $this->deleteCacheHome();
496                 }
497
498                 // ProfileArchive
499                 $this->deleteCache4ProfileArchivesIndex($this->id);
500         }
501 }
502
503 function beforeSerialize(&$item)
504 {
505         $item = preg_replace('/\\\n/', "<br />", $item);
506         $item = addslashes($item);
507
508         return $item;
509 }