OSDN Git Service

プロフィールのデフォルト入力機能追加
[trpgtools-onweb/cake-frame.git] / app / views / helpers / profiledisp.php
index f01e73c..bca6eea 100644 (file)
@@ -123,11 +123,13 @@ class ProfiledispHelper extends Helper {
                        $profile['CharactersHasProfile'][0]['value'] = null;
                }
 
-               foreach ($profile['CharactersHasProfile'] as $v) {
-                       $this->_get_profile_tag($profile['id'], $v['value'], false, $options);
+               foreach ($profile['CharactersHasProfile'] as $k => $v) {
+                       $disp_profile = $this->_get_profile_tag($profile['id'], $v['value'], false, $options);
 
                        if ($disp_public) {
-                               echo $this->dispPublicFlag4Profile($this->character_has_profile_num, $v, false)."<br>\n";
+                               echo $this->dispPublicFlag4Profile($disp_profile, $this->character_has_profile_num, $v, false)."<br>\n";
+                       } else {
+                               echo $disp_profile;
                        }
 
                        $this->character_has_profile_num++;
@@ -135,10 +137,12 @@ class ProfiledispHelper extends Helper {
 
                if ($this->profile_type['is_multi']) {
                        for ($i=0;$i<$form_num;$i++) {
-                               $this->_get_profile_tag($profile['id'], null, false, $options);
+                               $disp_profile =  $this->_get_profile_tag($profile['id'], null, false, $options);
 
                                if ($disp_public) {
-                                       echo $this->dispPublicFlag4Profile($this->character_has_profile_num, $profile, $isMulti)."<br>\n";
+                                       echo $this->dispPublicFlag4Profile($disp_profile, $this->character_has_profile_num, $profile, $isMulti)."<br>\n";
+                               } else {
+                                       echo $disp_profile;
                                }
 
                                $this->character_has_profile_num++;
@@ -146,7 +150,7 @@ class ProfiledispHelper extends Helper {
 
                        if ($disp_add_link) {
                                echo $this->Html->div('editLink', $this->Html->link(
-                                       __('Edit Data', true),
+                                       __('Edit', true),
                                        array(
                                                'action' => 'add_milti_profiles',
                                                $options['character_id'].
@@ -161,7 +165,7 @@ class ProfiledispHelper extends Helper {
        function _get_profile_tag($profile_id, $value = null, $label = false, $options = array())
        {
                if (empty($profile_id)) {
-                       echo null;
+                       return null;
                }
 
                $name1 = sprintf('CharactersHasProfile.%s.value', $this->character_has_profile_num);
@@ -186,21 +190,19 @@ class ProfiledispHelper extends Helper {
                        ));
                }
 
-               echo $profile_tag;
-
-               return ;
+               return $profile_tag;
        }
 
-       function dispPublicFlag4Profile($profile_num, $profile = null, $isMulti = false)
+       function dispPublicFlag4Profile($profile, $profile_num, $public_flag = null, $isMulti = false)
        {
                $profilePublicName = sprintf('CharactersHasProfile.%d.public_flag', $profile_num);
 
                if (is_array($profile) && isset($profile['CharactersHasProfile'][0]['public_flag'])) {
                        $selected_public_flag = $profile['CharactersHasProfile'][0]['public_flag'];
-               } elseif(is_array($profile) && isset($profile['public_flag'])) {
-                       $selected_public_flag = $profile['public_flag'];
-               } elseif (is_string($profile)) {
-                       $selected_public_flag = $profile;
+               } elseif(is_array($public_flag) && isset($public_flag['public_flag'])) {
+                       $selected_public_flag = $public_flag['public_flag'];
+               } elseif (is_string($public_flag)) {
+                       $selected_public_flag = $public_flag;
                } else {
                        $selected_public_flag = null;
                }
@@ -214,7 +216,16 @@ class ProfiledispHelper extends Helper {
                        $publicSelect['div'] = 'profilePublic profilePublicMulti';
                }
 
-               return $this->Select->create_publicflag_select($this->public_flags, $profilePublicName, $publicSelect);
+               if (!empty($profile)) {
+                       $publicSelect['before'] = $profile;
+               }
+
+               $public_select = $this->Select->create_publicflag_select($this->public_flags, $profilePublicName, $publicSelect);
+               if (!empty($profile)) {
+                       $public_select .= '<WBR>';
+               }
+
+               return $public_select;
        }
 
        /* profile_selectsの設定を出力 */
@@ -241,10 +252,12 @@ class ProfiledispHelper extends Helper {
                                'type' => 'hidden',
                                'value' => $profile_selects[0]['id'],
                        ));
-               echo $profile_select_tag;
+               $profile = $profile_select_tag;
 
                if ($disp_public) {
-                       echo $this->dispPublicFlag4Profile($this->character_has_profile_num, $options['public_flag']);
+                       echo $this->dispPublicFlag4Profile($profile, $this->character_has_profile_num, $options['public_flag']);
+               } else {
+                       echo $profile;
                }
 
                $this->character_has_profile_num++;
@@ -252,13 +265,13 @@ class ProfiledispHelper extends Helper {
 
 
        /* profile_selectsの設定をプルダウンで出力 */
-       function disp_select4profile_selects($profile_selects, $characters_has_profile = array(), $options = array(), $label = false, $legend = false, $disp_public = false)
+       function disp_select4profile_selects($profile_selects, $characters_has_profile = array(), $options = array(), $label = false, $legend = false, $disp_public = false, $isProfileDefault = false)
        {
                if (empty($profile_selects)) {
                        return null;
                }
 
-               $selected = $this->set_form_value($profile_selects, $characters_has_profile);
+               $selected = $this->set_form_value($profile_selects, $characters_has_profile, $isProfileDefault);
 
                $public_flag = null;
                if (isset($characters_has_profile[0]['public_flag'])) {
@@ -315,7 +328,7 @@ class ProfiledispHelper extends Helper {
        }
 
        /* profile_selectsの設定をチェックボックスで出力 */
-       function disp_checkbox4profile_selects($profile_selects, $characters_has_profile = array(), $label = false, $legend = false, $disp_public = false)
+       function disp_checkbox4profile_selects($profile_selects, $characters_has_profile = array(), $label = false, $legend = false, $disp_public = false, $isProfileDefault = false)
        {
                if (empty($profile_selects)) {
                        return null;
@@ -327,7 +340,7 @@ class ProfiledispHelper extends Helper {
                        }
 
                        $profile_select[0] = $v;
-                       $has_profile[0] = $this->get_selected_profile($v, $characters_has_profile);
+                       $has_profile[0] = $this->get_selected_profile($v, $characters_has_profile, $isProfileDefault);
                        $this->disp_select4profile_selects(
                                $profile_select, 
                                $has_profile,
@@ -338,7 +351,7 @@ class ProfiledispHelper extends Helper {
                                        'div' => array(
                                                'class' => 'input checkbox inline'
                                        ),
-                               ), $label, $legend, $disp_public
+                               ), $label, $legend, $disp_public, $isProfileDefault
                        );
                }
        }
@@ -416,7 +429,7 @@ class ProfiledispHelper extends Helper {
        }
 
        /* characters_has_profileからtable td出力 */
-       function disp_tds4characters_has_profiles($characters_has_profile, $type = 'view', $publicOnly = true, $profile_type = null, $oddTrOptions = null, $evenTrOptions = null, $useCount = false, $continueOddEven = true)
+       function disp_tds4characters_has_profiles($characters_has_profile, $type = 'view', $publicOnly = true, $profile_type = null, $oddTrOptions = null, $evenTrOptions = null, $useCount = false, $continueOddEven = true, $isProfileDefault = false)
        {
                $i = 1;
                $profile_num = 0;
@@ -437,7 +450,6 @@ class ProfiledispHelper extends Helper {
                        if (!CorePlus::is_valid($v, 'profile_table_id')) {
                                $profile_table_ids = array_keys(Set::combine($characters_has_profile, '{n}.profile_table_id'));
 
-
                                if (!empty($prof_table_ids)) {
                                        $v['profile_table_id'] = current(array_slice($prof_table_ids, $profile_num+1, 1));
                                        $v['value'] = null;
@@ -461,9 +473,9 @@ class ProfiledispHelper extends Helper {
 
                foreach($profiles as $k => $v) {
                        if ($type == 'form') {
-                               $tds = $this->disp_form4profile_tables($v, $publicOnly, $oddTrOptions, $evenTrOptions, true);
+                               $tds = $this->disp_form4profile_tables($v, $publicOnly, $oddTrOptions, $evenTrOptions, true, true, $isProfileDefault);
                        } else {
-                                       $tds = $this->disp_value4profile_tables($v, $publicOnly, $profile_type, null, null, true);
+                               $tds = $this->disp_value4profile_tables($v, $publicOnly, $profile_type, null, null, true, $isProfileDefault);
                        }
                }
                $this->character_has_profile_cols = 0;
@@ -479,18 +491,18 @@ class ProfiledispHelper extends Helper {
        }
 
        /* characters_has_profileからtdフォーム出力 */
-       function disp_form_td4characters_has_profiles($characters_has_profile, $disp_public = false, $profile_type = null)
+       function disp_form_td4characters_has_profiles($characters_has_profile, $disp_public = false, $profile_type = null, $isProfileDefault = false)
        {
                if ($disp_public === true) {
                        $publicOnly = false;
                } else {
                        $publicOnly = true;
                }
-               $this->disp_tds4characters_has_profiles($characters_has_profile, 'form', $publicOnly, $profile_type, null, null, true);
+               $this->disp_tds4characters_has_profiles($characters_has_profile, 'form', $publicOnly, $profile_type, null, null, true, true, $isProfileDefault);
        }
 
        /* profile_tablesの設定からView表示を出力 */
-       function disp_value4profile_tables($profile_tables, $publicOnly = true, $profile_type = null, $oddTrOptions = null, $evenTrOptions = null, $useCount = false, $continueOddEven = true)
+       function disp_value4profile_tables($profile_tables, $publicOnly = true, $profile_type = null, $oddTrOptions = null, $evenTrOptions = null, $useCount = false, $continueOddEven = true, $isProfileDefault = false)
        {
                if (empty($profile_tables)) {
                        return null;
@@ -517,12 +529,13 @@ class ProfiledispHelper extends Helper {
        }
 
        /* profile_tablesの設定から入力フォームを出力 */
-       function disp_form4profile_tables($profile_tables, $publicOnly = true, $oddTrOptions = null, $evenTrOptions = null, $useCount = false, $continueOddEven = true)
+       function disp_form4profile_tables($profile_tables, $publicOnly = true, $oddTrOptions = null, $evenTrOptions = null, $useCount = false, $continueOddEven = true, $isProfileDefault = false)
        {
                if (empty($profile_tables)) {
                        return null;
                }
                $first = reset($profile_tables);
+
                if (isset($first['ProfileTableStatic'])) {
                        $statics = $first['ProfileTableStatic'];
                }
@@ -547,7 +560,7 @@ class ProfiledispHelper extends Helper {
                                $v['value'] = null;
                        }
 
-                       if ($i == 0 && (isset($statics[$this->character_has_profile_cols]) || isset($v['profile_table_static_id']))) {
+                       if ($i == 0 && (isset($statics[$this->character_has_profile_cols]) || (isset($v['profile_table_static_id']) && !empty($v['profile_table_static_id'])))) {
                                $value = isset($v['profile_table_static_id']) ? $v['value']: $statics[$this->character_has_profile_cols]['title']; 
                                $form = '<span class="staticHead">'.$value.'</span>'.
                                        $this->Form->input('CharactersHasProfile.'.$this->character_has_profile_num.'.value', array(
@@ -558,6 +571,7 @@ class ProfiledispHelper extends Helper {
                                $form = $this->Form->input('CharactersHasProfile.'.$this->character_has_profile_num.'.value', array(
                                        'type' => 'text',
                                        'label' => false,
+                                       'class' => 'longInput',
                                        'value' => $v['value'],
                                ));
                        }
@@ -589,7 +603,12 @@ class ProfiledispHelper extends Helper {
                }
 
                if ($publicOnly === false) {
-                       $forms[] = $this->dispPublicFlag4Profile($first_num, $first, false);
+                       if ($isProfileDefault && isset($last["public_flag"])) {
+                               $last = end($profile_tables);
+                               $forms[] = $this->dispPublicFlag4Profile(null, $first_num, $last["public_flag"], false);
+                       } else {
+                               $forms[] = $this->dispPublicFlag4Profile(null, $first_num, $first, false);
+                       }
                }
 
                $this->character_has_profile_cols++;
@@ -646,10 +665,15 @@ class ProfiledispHelper extends Helper {
        /* profile_typeの設定に応じてフォーム表示
         * set_profile_type4view($profile)チェック済み前提。
         */
-       function disp_forms4profile_type($profile, $form_num = 2, $table_blank = 2, $disp_add_link = false, $character_id = null, $disp_public = false, $options = array())
+       function disp_forms4profile_type($profile, $form_num = 2, $table_blank = 2, $disp_add_link = false, $character_id = null, $disp_public = false, $options = array(), $isProfileDefault = false)
        {
-               if (!isset($profile['CharactersHasProfile'])) {
-                       $profile['CharactersHasProfile'] = array();
+               if (!isset($profile['CharactersHasProfile']) || empty($profile['CharactersHasProfile'])) {
+                       if (isset($profile['DefaultProfile']) && !empty($profile['DefaultProfile'])) {
+                               $profile['CharactersHasProfile'] = $profile['DefaultProfile'];
+                               $form_num = $form_num - count($profile['DefaultProfile']) +1;
+                       } else {
+                               $profile['CharactersHasProfile'] = array();
+                       }
                }
 
                if ($this->profile_type['is_select'] || $this->profile_type['is_radio'] || $this->profile_type['is_checkbox']) {
@@ -658,7 +682,7 @@ class ProfiledispHelper extends Helper {
                        } elseif ($this->profile_type['is_radio']) {
                                $this->disp_radio4profile_selects($profile['ProfileSelect'], $profile['CharactersHasProfile'], false, false, $disp_public);
                        } elseif ($this->profile_type['is_checkbox']) {
-                               $this->disp_checkbox4profile_selects($profile['ProfileSelect'], $profile['CharactersHasProfile'],  false, false, $disp_public);
+                               $this->disp_checkbox4profile_selects($profile['ProfileSelect'], $profile['CharactersHasProfile'],  false, false, $disp_public, $isProfileDefault);
                        }
                } elseif ($this->profile_type['is_table']) {
                        if ($disp_public == true) {
@@ -676,7 +700,7 @@ class ProfiledispHelper extends Helper {
                        $this->disp_th4profile_tables($profile['ProfileTable'], $tableId, $disp_public);
                        echo '<tbody>';
                        if (!empty($profile['CharactersHasProfile'])) {
-                               $this->disp_form_td4characters_has_profiles($profile['CharactersHasProfile'], $disp_public);
+                               $this->disp_form_td4characters_has_profiles($profile['CharactersHasProfile'], $disp_public, null, $isProfileDefault);
 
                        }
                        if (intval($form_num) > 0 
@@ -694,7 +718,7 @@ class ProfiledispHelper extends Helper {
                        echo '</table>';
                        if ($disp_add_link) {
                                $link = $this->Html->link(
-                                       __('Edit Data', true),
+                                       __('Add Column', true),
                                        array(
                                                'action' => 'add_milti_profiles',
                                                $character_id.
@@ -735,7 +759,7 @@ class ProfiledispHelper extends Helper {
                {
                        echo $this->Html->tag(
                                'div',
-                               __(' required', true),
+                               __(' required', true),
                                array(
                                        'class' => 'required'
                                )
@@ -788,7 +812,7 @@ class ProfiledispHelper extends Helper {
 
                        if ($this->profile_type['is_multi'] && $disp_add_link) {
                                echo $this->Html->div('editLink', $this->Html->link(
-                                       __('Edit Data', true),
+                                       __('Add Column', true),
                                        array(
                                                'action' => 'add_milti_profiles',
                                                $character_id.
@@ -815,14 +839,14 @@ class ProfiledispHelper extends Helper {
                        echo '</table>';
                        if ($disp_add_link) {
                                $link = $this->Html->link(
-                                       __('Edit Data', true),
+                                       __('Edit', true),
                                        array(
                                                'action' => 'add_milti_profiles',
                                                $character_id.
                                                '/profile_id:'.$profile['id']
                                        )
                                );
-                               if (!empty($profile['CharactersHasProfile'][0]['character_id'])) {
+                               if (isset($profile['CharactersHasProfile'][0]) && !empty($profile['CharactersHasProfile'][0]['character_id'])) {
                                        $link .= " ".$this->Html->link(
                                                __('Clear', true),
                                                array(
@@ -844,15 +868,16 @@ class ProfiledispHelper extends Helper {
        }
 
        /* フォームに値入力 */
-       function set_form_value($profile_selects, $characters_has_profile)
+       function set_form_value($profile_selects, $characters_has_profile, $isProfileDefault = false)
        {
                $this->form_id++;
 
                if ($this->profile_type['is_select'] || $this->profile_type['is_radio'] || $this->profile_type['is_checkbox']) {
-                       return $this->set_form_value_select($profile_selects, $characters_has_profile);
+                       return $this->set_form_value_select($profile_selects, $characters_has_profile, $isProfileDefault);
                } elseif ($this->profile_type['is_table']) {
                        return $this->set_form_value_table($characters_has_profile);
                } else {
+//                     return $profile_selects;
                        return $this->set_form_value_profile($profile_selects, $characters_has_profile);
                }
 
@@ -860,14 +885,18 @@ class ProfiledispHelper extends Helper {
 
        /* profile_select */
        /* profile_select_id->profile_idで抽出 */
-       function set_form_value_select($profile_selects, $characters_has_profile)
+       function set_form_value_select($profile_selects, $characters_has_profile, $isProfileDefault = false)
        {
-               if (isset($characters_has_profile[0]['id'])) {
+               if (isset($characters_has_profile[0]['value'])) {
                        $selected = array(
                                'value' => $characters_has_profile[0]['value'],
-                               'public_flag' => $characters_has_profile[0]['public_flag'],
-                               'link_value' => $characters_has_profile[0]['link_value'],
                        );
+                       if (isset($characters_has_profile[0]['public_flag'])) {
+                               $selected['public_flag'] = $characters_has_profile[0]['public_flag'];
+                       }
+                       if (isset($characters_has_profile[0]['link_value'])) {
+                               $selected['link_value'] = $characters_has_profile[0]['link_value'];
+                       }
                } else {
                        $selected = array(
                                'value' => '',
@@ -884,14 +913,18 @@ class ProfiledispHelper extends Helper {
        }
 
        /* postからprofile_select_id->profile_idで抽出 */
-       function get_selected_profile($profile_select, $characters_has_profile)
+       function get_selected_profile($profile_select, $characters_has_profile, $isProfileDefault = false)
        {
                $profile = array(
                        'profile_select_id' => $profile_select['id'],
                        'profile_id' => $profile_select['profile_id'],
-                       'value' => $profile_select['value'],
                );
 
+               if (!empty($isProfileDefault)) {
+               } else {
+                       $profile['value'] = $profile_select['value'];
+               }
+
                foreach ($characters_has_profile as $v) {
                        if (Set::contains($v, $profile)) {
                                return $v;
@@ -931,5 +964,173 @@ class ProfiledispHelper extends Helper {
                );
 ;
        }
+
+       /* ProfileTableを横並びに表示(行単位) */
+       function dispTableBySideways($profile, $key_name = 'title', $option = array())
+       {
+               $option = array_merge(array(
+                       'header' => null,
+                       'type' => 'text',
+                       'tabindex' => array(
+                               'start' => 0,
+                               'gap' => 0,
+                       ),
+                       'add_column' => array(),
+                       'form_options' => array(),
+                       'tr_options' => array(),
+                       'td_options' => array(),
+               ), $option);
+
+               if (!isset($profile['ProfileTable']) || empty($profile['ProfileTable'])) {
+                       if (isset($option['add_column']) && empty($option['add_column'])) {
+                               return null;
+                       }
+               }
+
+               if (!empty($option['add_column'])) {
+                       $add_profile = array(
+                               'id' => 0,
+                               'profile_id' => $profile['Profile']['id'],
+                               'title' => '',
+                               'tablekey_name' => '',
+                               'width' => '',
+                               'sort_order' => '',
+                               'ProfileTableStatic' => array(),
+                       );
+                       if (isset($option['add_column']['data']) && isset($add_profile[$key_name])) {
+                               $add_profile[$key_name] = $option['add_column']['data'];
+                       }
+                       if (isset($option['add_column']['place']) && $option['add_column']['place'] == 'top') {
+                               array_unshift($profile['ProfileTable'], $add_profile);
+                       } else {
+                               array_push($profile['ProfileTable'], $add_profile);
+                       }
+               }
+
+               $rows = count($profile['ProfileTable']);
+               if (!empty($option['header'])) {
+                       $tds = $this->Html->tag('th', $option['header'], array('style' => 'width:65px;'));
+               } else {
+                       $tds = null;
+               }
+
+               for ($i=0; $i<$rows;$i++) {
+                       if (!isset($profile['ProfileTable'][$i][$key_name])) {
+                               $profile['ProfileTable'][$i][$key_name] = null;
+                       }
+
+                       if ($option['type'] == 'form') {
+                               $tabindex = $option['tabindex']['start'] + $option['tabindex']['gap'] * $i;
+                               $form_options = array_merge(
+                                       array(
+                                               'type' => 'text',
+                                               'value' => $profile['ProfileTable'][$i][$key_name],
+                                               'label' => false,
+                                               'tabindex' => $tabindex,
+                                       ),
+                                       $option['form_options']
+                               );
+
+                               $content = $this->Form->input(
+                                       'ProfileTable.'.$profile['ProfileTable'][$i]['id'].'.'.$key_name,
+                                       $form_options
+                               );
+                               $option['td_options'] = array_merge($option['td_options'], array('escape' => false));
+                       } else {
+                               if (isset($option['add_column']['place']) &&
+                                       (($option['add_column']['place'] == 'top' && $i == 0) || 
+                                       ($option['add_column']['place'] != 'top' && $i == $rows - 1))) {
+                                       $content = __('Add', true);
+                               } else {
+                                       $content = $profile['ProfileTable'][$i][$key_name];
+                               }
+                       }
+
+                       $tds .= $this->Html->tag('td', $content, $option['td_options']);
+               }
+
+               return $this->Html->tag('tr', $tds, $option['tr_options']);
+       }
+
+
+       /* 一定の文字数で複数行に折り返すtruncate
+        * マルチバイト対応
+        * URLやタグはスキップしない
+        * @param array $options is for truncate()
+        */
+       function multi_truncate($text, $length = 20, $rows = 3, $options = array())
+       {
+               if (empty($text)) {
+                       return $text;
+               }
+
+               $rows = (int)$rows;
+               if (!($rows > 0)) {
+                       $rows = 1;
+               }
+
+               // 置換
+               $replace_pairs = array(
+                       // 改行
+                       '<br>' => "\n",
+                       '<br />' => "\n",
+                       // スペースの連続
+                       "\s\s" => "\s",
+                       ' ' => '',
+               );
+               $text = strtr($text, $replace_pairs);
+
+               // Sanitize戻し
+               $Sanitize = CorePlus::set_behavoir('SanitizePlus');
+               $text = $Sanitize->restore_html($this->model(), $text);
+
+               $text = trim($text);
+
+               if (mb_strlen($text) < $length) {
+                       return $text;
+               }
+
+               // 複数行対応
+               $result = array();
+               $last_row = '';
+               for ($i = 1; $i <= $rows; $i++) {
+                       if ($i > 1) {
+                               if (!$text) {
+                                       break;
+                               }
+
+                               // 前行の分を切り取り
+                               if (!empty($last_row)) {
+                                       $text = substr_replace($text, '', 0, strlen($last_row));
+                               }
+                       }
+                       if ($i == $rows) {
+                               $text_len = mb_strlen($text);
+
+                               if ($text_len < $length) {
+                                       $result[$i] = $text;
+                               } else {
+                                       $result[$i] = $this->Text->truncate($text, mb_strlen(mb_substr($text, 0, $length)), $options);
+                               }
+
+                               break;
+                       } else {
+                               $result[$i] = mb_substr($text, 0, $length);
+                       }
+
+                       $last_row = $result[$i];
+                       $result[$i] = strip_tags($result[$i]);
+
+
+                       // タグをエスケープ
+                       $result[$i] = $Sanitize->escapeTags($this->model(), $result[$i]);
+               }
+
+               $text = implode("<br />\n", $result);
+
+               return $text;
+       }
+
+
 }