5 * @copyright Copyright 2010, Cake. (http://trpgtools-onweb.sourceforge.jp/)
7 * @package TRPG Data Bank
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/
14 class Character extends AppModel {
16 var $name = 'Character';
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'),
41 'secret_notes' => array(
48 // other 'system_id', 'deleted''deleted_date'
51 var $validate = array(
58 'validSystemId' => array(
59 'rule' => array('validSystemId', true),
62 'sort_order' => array(
69 'validStatus' => array(
70 'rule' => 'validStatus',
74 'public_flag' => array(
75 'publicFlag' => array(
76 'rule' => 'publicFlag',
81 'validUserId' => array(
82 'rule' => 'validUserId',
87 //The Associations below have been created with all possible keys, those that are not needed can be removed
88 var $belongsTo = array(
90 'className' => 'System',
91 'foreignKey' => 'system_id',
92 'conditions' => array(
93 'System.public_flag' => 'public',
104 'className' => 'User',
105 'foreignKey' => 'user_id',
114 var $hasMany = array(
116 'CharacterPicture' => array(
117 'className' => 'CharacterPicture',
118 'foreignKey' => 'character_id',
119 'dependent' => false,
120 'conditions' => array(
121 'CharacterPicture.public_flag' => 'public',
124 'CharacterPicture.id',
125 'CharacterPicture.public_flag',
126 'CharacterPicture.created',
128 'order' => array('CharacterPicture.id' => 'asc'),
135 'CharactersHasProfile' => array(
136 'className' => 'CharactersHasProfile',
137 'foreignKey' => 'character_id',
138 'dependent' => false,
141 'order' => array('CharactersHasProfile.id' => 'asc'),
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',
157 'CharacterProfileArchive.id',
158 'CharacterProfileArchive.title',
159 'CharacterProfileArchive.public_flag',
160 'CharacterProfileArchive.disp_date',
163 'CharacterProfileArchive.disp_date' => 'desc',
173 /* validation Rule */
174 function validStatus($data)
176 if (isset($data["status"]) && in_array($data["status"], $this->status)) {
183 /* check PublicFlag Setting */
184 function publicFlagDefault($data)
186 if (isset($data["public_flag_default"]) && in_array($data["public_flag_default"], $this->public_flag)) {
194 function beforeSave($options = array())
196 if (empty($this->data['Character']['sort_order'])) {
197 $this->data['Character']['sort_order'] = 0;
200 $result = parent::beforeSave($options);
201 if ($result === false) {
206 if (!empty($this->data['Character']['notes'])) {
207 $this->data['Character']['notes'] = str_replace(array("\n\r", '\n', "\r"), '<br />', $this->data['Character']['notes']);
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']);
216 function afterSave($created) {
217 $this->deleteCache4Charcter();
219 return parent::afterSave($created);
221 function afterDelete() {
222 $this->deleteCache4Charcter();
224 return parent::afterDelete();
228 function get_character($id, $conditions = array(), $isAdmin = false, $contain = array())
230 $conditions = array_merge(
232 'Character.id' => $id,
233 'Character.deleted' => 0,
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']);
245 $contain = array_merge(array(
247 'CharacterPicture' => array(
254 'ProfileTable' => array(
255 'ProfileTableStatic',
257 'CharactersHasProfile',
260 'CharacterProfileArchive',
263 $character = $this->find('first', array(
264 'conditions' => $conditions,
265 'contain' => $contain,
272 function set_profiles2view($orig_character, $isArchive = false)
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'];
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'];
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']);
300 $new_profile[$profile['key_name']]['CharactersHasProfile'] = null;
303 $new_profile[$profile['key_name']]['CharactersHasProfile'] = $profile['CharactersHasProfile'];
307 array_multisort($sort_order, SORT_ASC, $new_profile);
309 $orig_character['System']['Profile'] = $new_profile;
312 $character['Character'] = $orig_character['Character'];
313 if ($isArchive === true) {
314 // beforeSerialize($character['Character']['name']);
315 // beforeSerialize($character['Character']['notes']);
318 if (isset($orig_character['CharacterPicture'])) {
319 $character['CharacterPicture'] = $orig_character['CharacterPicture'];
321 if (isset($orig_character['CharacterProfileArchive']) && $isArchive !== true) {
322 $character['CharacterProfileArchive'] = $orig_character['CharacterProfileArchive'];
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']);
332 $character['System'] = $orig_character['System'];
335 if (isset($orig_character['User'])) {
336 $character['User'] = $orig_character['User'];
342 function _set_blank4static_table($value, $profile_id, $profile_table_id)
345 'profile_id' => $profile_id,
346 'profile_table_id' => $profile_table_id,
348 'public_flag' => 'public',
352 function saveCharacterProfile($id, $params = array())
358 if (isset($params['CharacterProfileArchive'])) {
359 $params = array($params['CharacterProfileArchive']);
364 $archive['CharacterProfileArchive']['character_id'] = $id;
366 $character = $this->get_character($id, array(), true);
368 $newData = $this->set_profiles2view($character, true);
369 array_walk_recursive($newData, 'beforeSerialize');
371 $archive['CharacterProfileArchive']['value'] = serialize($newData);
373 $archive['CharacterProfileArchive']['title'] = $params[0]['title'];
374 $archive['CharacterProfileArchive']['memo'] = $params[0]['memo'];
376 if (isset($params['disp_date'])) {
377 $archive['CharacterProfileArchive']['disp_date'] = $params['disp_date'];
379 $archive['CharacterProfileArchive']['disp_date'] = date('Y-m-d H:i:s');
382 if ($character['Character']['public_flag'] != 'public') {
383 $archive['CharacterProfileArchive']['public_flag'] = $character['Character']['public_flag'];
385 $archive['CharacterProfileArchive']['public_flag'] = $params[0]['public_flag'];
388 $this->CharacterProfileArchive->create();
389 if ($this->CharacterProfileArchive->save($archive, array('fieldList' => $this->CharacterProfileArchive->fields['add']))) {
397 function paginateCount($conditions = null, $recursive = 0, $extra = array()) {
398 if ($this->checkSearchProfile($conditions)) {
399 unset($conditions['profile_search']);
401 $character_ids = $this->_getCharactersByCharacterProfiles($conditions);
403 return count($character_ids);
405 // 元のpaginateCount(controller.phpより)
406 $parameters = compact('conditions');
407 if ($recursive != $this->recursive) {
408 $parameters['recursive'] = $recursive;
410 $count = $this->find('count', array_merge($parameters, $extra));
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']);
419 $character_ids = $this->_getCharactersByCharacterProfiles($conditions);
421 if (empty($character_ids)) {
426 'Character.id' => array_values($character_ids),
431 'conditions' => $conditions,
437 if (isset($extra['contain'])) {
438 $params['contain'] = $extra['contain'];
441 $results = $this->find('all', $params);
446 function checkSearchProfile($conditions = array())
448 if (isset($conditions['profile_search'])) {
455 function _getCharactersByCharacterProfiles($conditions = null)
457 static $character_ids;
458 static $prev_conditions;
460 if (!empty($character_ids) && $prev_conditions == $conditions) {
461 return $character_ids;
464 $prev_conditions = $conditions;
466 $this->CharactersHasProfile->unbindModel(
467 array('belongsTo' => array(
471 'ProfileTableStatic',
474 return $character_ids = $this->CharactersHasProfile->find('list', array(
475 'conditions' => $conditions,
476 'fields' => array('CharactersHasProfile.character_id'),
477 'group' => ('CharactersHasProfile.character_id'),
483 function deleteCache4Charcter()
486 $this->deleteCacheMyData();
488 $this->deleteCacheCharacter($this->id, false);
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
495 $this->deleteCacheHome();
499 $this->deleteCache4ProfileArchivesIndex($this->id);
503 function beforeSerialize(&$item)
505 $item = preg_replace('/\\\n/', "<br />", $item);
506 $item = addslashes($item);