OSDN Git Service

キャラクター一覧のページネーション取得方法の共通化
authorCake <cake_67@users.sourceforge.jp>
Wed, 24 Mar 2010 07:57:27 +0000 (16:57 +0900)
committerCake <cake_67@users.sourceforge.jp>
Wed, 24 Mar 2010 07:57:27 +0000 (16:57 +0900)
app/controllers/app_controller.php
app/controllers/characters_controller.php
app/controllers/users_controller.php
app/views/characters/admin_index.ctp
app/views/characters/index.ctp

index 9240f32..4689938 100644 (file)
@@ -47,10 +47,40 @@ class AppController extends Controller
                'Token'
        );
 
+       /* Characters抽出条件 */
        var $conditions = array(
                'Character.public_flag' => 'public',
                'Character.deleted' => 0
        );
+       var $fields = array(
+               'Character.id',
+               'Character.system_id',
+               'Character.user_id',
+               'Character.name',
+               'Character.main_picture',
+               'Character.sort_order',
+               'Character.status',
+               'Character.public_flag',
+               'Character.modified',
+       );
+       var $contain = array();
+       var $recursive = -1;
+       var $order = array(
+               'Character.modified' => 'DESC',
+       );
+       var $paginate = array(
+               'Character' => array(
+                       'limit' => 20,
+                       'conditions' => array(
+                               'Character.public_flag' => 'public',
+                               'Character.deleted' => 0
+                       ),
+                       'recursive' => -1,
+                       'order' => array(
+                               'Character.modified' => 'DESC',
+                       )
+               ),
+       );
 
        /* ACL */
        // 追加アクション用 crudMap
@@ -210,56 +240,50 @@ class AppController extends Controller
        }
 
        /* キャラクター一覧取得 */
-       function _get_characters4user_id($user_id = null, $conditions = array(), $limit = 5, $fields = array(), $contain = array(), $order = array())
+       /* 条件セット */
+       function _set_conditions_characters4user_id($user_id = null, $conditions = array(), $fields = array(), $contain = array(), $order = array())
        {
-               if (!isset($this->Character)) {
-                       $this->Character = CorePlus::set_model('Character');
-               }
-
                if (!empty($user_id)) {
-                       $conditions = array_merge($this->conditions, 
+                       $conditions = array_merge((array)$conditions, 
                                array(
                                        'Character.user_id' => $user_id,
-                               ),
-                               $conditions
+                               )
                        );
-                       if ($user_id == $this->user_id) {
-                               unset($conditions['Character.public_flag']);
-                       }
-               }
 
-               if (!is_array($contain)) {
-                       $contain = array($contain);
                }
+               $conditions = array_merge($this->conditions, (array)$conditions);
 
-               return $this->_get_characters($conditions, $limit, $fields, $contain, $order);
-       }
-
-       function _get_characters($conditions = array(), $limit = 5, $fields = array(), $contain = array(), $order = array())
-       {
-               if (empty($conditions)) {
-                       $conditions = $this->conditions;
+               if ((isset($conditions['isAdmin']))|| (!empty($user_id) && ($user_id == $this->user_id))) {
+                       unset($conditions['Character.public_flag']);
+                       unset($conditions['isAdmin']);
                }
 
-               if (empty($fields)) {
-                       $fields = '';
-               }
+               $fields = array_merge($this->fields, (array)$fields);
 
-               if (!is_array($contain)) {
-                       $contain = array('');
-               }
+
+               $contain = array_merge($this->contain, (array)$contain);
                if (empty($contain)) {
                        $recursive = -1;
                } else {
                        $recursive = Set::countDim($contain);
                }
-               if (empty($order)) {
-                       $order = array('Character.modified' => 'DESC');
+
+               $order = array_merge($this->order, (array)$order);
+
+               return array($conditions, $fields, $contain, $recursive, $order);
+       }
+
+       function _get_characters_list4user_id($user_id = null, $conditions = array(), $limit = 5, $fields = array(), $contain = array(), $order = array())
+       {
+               list($conditions, $fields, $contain, $recursive, $order) = $this->_set_conditions_characters4user_id($user_id, $conditions, $fields, $contain, $order);
+
+               if (!isset($this->Character)) {
+                       $this->Character = CorePlus::set_model('Character');
                }
 
                return $this->Character->find('all', array(
                        'conditions' => $conditions,
-                       'limit' => 5,
+                       'limit' => $limit,
                        'fields' => $fields,
                        'recursive' => $recursive,
                        'contain' => $contain,
@@ -267,6 +291,28 @@ class AppController extends Controller
                ));
        }
 
+       function _get_characters_page4user_id($user_id = null, $conditions = array(), $limit = 20, $fields = array(), $contain = array(), $order = array(), $page = 1)
+       {
+               list($conditions, $fields, $contain, $recursive, $order) = $this->_set_conditions_characters4user_id($user_id, $conditions, $fields, $contain, $order);
+
+               if (!isset($this->Character)) {
+                       $this->Character = CorePlus::set_model('Character');
+               }
+
+               $this->paginate['Character'] = array(
+                       'limit' => $limit,
+                       'conditions' => $conditions,
+                       'fields' => $fields,
+                       'contain' => $contain,
+                       'recursive' => $recursive,
+                       'order' => $order,
+                       'page' => $page,
+               );
+
+               return $this->paginate('Character');
+       }
+
+
        /* キャラクター情報取得 */
        function _get_character4character_id($character_id, $user_id = null, $public_flag = null, $is_deleted = 0)
        {
index cb09cdd..3ba742b 100644 (file)
@@ -16,36 +16,6 @@ class CharactersController extends AppController {
        );
 
        var $disableTokenActions = array();
-
-       var $paginate = array(
-               'Character' => array(
-                       'limit' => 20,
-                       'conditions' => array(
-                               'Character.public_flag' => 'public',
-                               'Character.deleted' => 0,
-                       ),
-                       'fields' => array(
-                               'Character.id',
-                               'Character.system_id',
-                               'Character.user_id',
-                               'Character.name',
-                               'Character.main_picture',
-                               'Character.sort_order',
-                               'Character.status',
-                               'Character.public_flag',
-                               'Character.modified',
-                       ),
-                       'recursive' => 1,
-                       'contain' => array(
-                               'System',
-                               'User',
-                       ),
-                       'order' => array(
-                               'Character.modified' => 'desc',
-                       ),
-               ),
-       );
-
        var $post_data = array();
 
        /* メソッド */
@@ -380,9 +350,7 @@ class CharactersController extends AppController {
 
 
        function admin_index($id = null) {
-               unset($this->paginate['Character']['conditions']['Character.public_flag']);
-
-               $this->_index($id);
+               $this->_index($id, array('isAdmin' => true));
        }
 
        function admin_view($id = null) {
@@ -417,34 +385,40 @@ class CharactersController extends AppController {
 
 
        /* 共通化アクションメソッド */
-       function _index($id) {
+       function _index($id, $conditions = array(), $limit = 20, $fields = array(), $contain = array(), $order = array(), $page = 1) {
+               $contain = array_merge(array('System'), (array)$contain);
+
                $isOwner = false;
                $user = array();
+
                if (!empty($id)) {
                        $user = $this->Character->User->find('first', array(
+                               'conditions' => array('User.id' => $id),
                                'recursive' => -1,
                                'fields' => array(
                                        'User.id',
                                        'User.name',
                                ),
                        ));
+
                        if (empty($user)) {
                        $this->Session->setFlash(__('Invalid Id.', true));
                        $this->redirect(array('action'=>'index'));
                        }
+               } else {
+                       $contain = array_merge($contain, array('User'));
                }
                $this->set('userSet', $user);
 
                if (!empty($user)) {
-                       $this->paginate['Character']['conditions']['Character.user_id'] = $id;
-
                        if ($user['User']['id'] == $this->user_id) {
-                               unset($this->paginate['Character']['conditions']['Character.public_flag']);
                                $isOwner = true;
                        }
                }
 
-               $this->set('characters', $this->HtmlEscape->nl_unescape($this->paginate('Character')));
+                $characters =  $this->HtmlEscape->nl_unescape($this->_get_characters_page4user_id($id, $conditions, $limit, $fields, $contain, $order, $page));
+
+               $this->set('characters', $characters);
 
                $this->set('isOwner', false);
                $this->set('isOwner_userSet', $isOwner);
index 3a28f61..a2aba58 100644 (file)
@@ -358,7 +358,7 @@ class UsersController extends ModuleController {
                $target_user_characters = array();
                if (!empty($id)) {
                        // characters
-                       $target_user_characters = $this->_get_characters4user_id($id, array(), 5, array(), array('System'));
+                       $target_user_characters = $this->_get_characters_list4user_id($id, array(), 5, array(), array('System'));
                }
                $this->set('target_user_characters', $target_user_characters);
 
@@ -366,7 +366,7 @@ class UsersController extends ModuleController {
                $public_characters = array();
                if (empty($id) || ($id == $this->user_id)) {
                        // characters
-                       $public_characters = $this->_get_characters4user_id(null, array(), 5, array(), array('System', 'User'));
+                       $public_characters = $this->_get_characters_list4user_id(null, array(), 5, array(), array('System', 'User'));
                }
                $this->set('public_characters', $public_characters);
 
index 964d9b4..53a7b15 100644 (file)
@@ -1,7 +1,9 @@
 <div class="characters index">
-<h2><?php if (!empty($userSet)): ?><?php echo $html->link($characters[0]['User']['name'], array('controller' => 'users', 'action' => 'view', $characters[0]['User']['id']), array(), false, false) ?><?php __("'s "); ?><?php endif; ?><?php __('Characters');?></h2>
+<h2><?php if (!empty($userSet)): ?><?php echo $html->link($userSet['User']['name'], array('controller' => 'users', 'action' => 'view', $userSet['User']['id']), array(), false, false) ?><?php __("'s "); ?><?php else: ?><?php __('All ') ?><?php endif; ?><?php __('Characters');?></h2>
 <p>
 <?php
+$paginator->options(array('url' => $this->passedArgs));
+
 echo $paginator->counter(array(
 'format' => __('Page %page% of %pages%, showing %current% records out of %count% total, starting on record %start%, ending on %end%', true)
 ));
@@ -74,6 +76,6 @@ echo $paginator->counter(array(
 
 <div class="paging">
 <?php echo $paginator->prev('<< '.__('previous', true), array(), null, array('class'=>'disabled'));?>
- | <?php echo $paginator->numbers();?>
+ | <?php echo $paginator->numbers();?> | 
 <?php echo $paginator->next(__('next', true).' >>', array(), null, array('class' => 'disabled'));?>
 </div>
index 97c2b47..94b3621 100644 (file)
@@ -1,7 +1,9 @@
 <div class="characters index">
-<h2><?php if (!empty($userSet)): ?><?php echo $html->link($characters[0]['User']['name'], array('controller' => 'users', 'action' => 'view', $characters[0]['User']['id']), array(), false, false) ?><?php __("'s "); ?><?php endif; ?><?php __('Characters');?></h2>
+<h2><?php if (!empty($userSet)): ?><?php echo $html->link($userSet['User']['name'], array('controller' => 'users', 'action' => 'view', $userSet['User']['id']), array(), false, false) ?><?php __("'s "); ?><?php else: ?><?php __('All ') ?><?php endif; ?><?php __('Characters');?></h2>
 <p>
 <?php
+$paginator->options(array('url' => $this->passedArgs));
+
 echo $paginator->counter(array(
 'format' => __('Page %page% of %pages%, showing %current% records out of %count% total, starting on record %start%, ending on %end%', true)
 ));
@@ -14,6 +16,7 @@ echo $paginator->counter(array(
 <?php if (empty($userSet)): ?>
        <th><?php echo $paginator->sort('user_id');?></th>
 <?php endif; ?>
+       <th><?php echo $paginator->sort('modified');?></th>
        <th><?php echo $paginator->sort('status');?></th>
 </th>
 </tr>
@@ -57,6 +60,9 @@ foreach ($characters as $character):
 </td>
 <?php endif; ?>
 <td>
+       <?php echo $time->niceShort($character['Character']['modified'], array('format' => 'Y/m/d H:i')); ?>
+</td>
+<td>
        <?php echo $select->get_i18n_status($character['Character']['status'], $status); ?>
 </td>
 </tr>
@@ -66,7 +72,7 @@ foreach ($characters as $character):
 
 <div class="paging">
 <?php echo $paginator->prev('<< '.__('previous', true), array(), null, array('class'=>'disabled'));?>
- | <?php echo $paginator->numbers();?>
+ | <?php echo $paginator->numbers();?> | 
 <?php echo $paginator->next(__('next', true).' >>', array(), null, array('class' => 'disabled'));?>
 </div>