自作のフォームメールプラグインの管理画面に設定メニューを表示するコードの後半です。フォームの編集、コピー、新規作成、初期化、項目追加、並び替え、変更を保存の各処理の実行コードです。
更新者・作成者は、氏名表示です。
コード
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
class Hk_Form_Mail_Admin_2 { private $form_message = ''; // 管理画面メッセージ // 管理画面メイン2 フォーム新規作成・編集 function admin_main_2( $form_no, $admin_next ) { global $hk_form_mail; if ( $admin_next == '新規作成' ) { // データベースからフォーム実在数の読み込み $form_exist = $hk_form_mail->db_read_index(); // フォーム番号、フォーム存在数(仮) ++$form_exist; $form_no = $form_exist; // フォーム初期設定 $form_data = $this->form_data_default(); // フォーム設定入力画面表示 $this->form_message = ''; $this->show_form_setting( $form_no, $form_data ); return; } if ( $admin_next == '初期化' ) { // POSTされたデータの取り込み $form_data = $this->admin_form_post_data(); // フォーム付帯情報の退避 $creator = $form_data['creator']; $updater = $form_data['updater']; $create_day = $form_data['create_day']; $update_day = $form_data['update_day']; // フォーム初期設定 $form_data = $this->form_data_default(); // フォーム付帯情報の戻し $form_data['creator'] = $creator; $form_data['updater'] = $updater; $form_data['create_day'] = $create_day; $form_data['update_day'] = $update_day; // フォーム設定入力画面表示 $this->form_message = '初期化しました'; $this->show_form_setting( $form_no, $form_data ); return; } if ( $admin_next == '編集' ) { // データベースからフォームデータの読み込み $form_data = $hk_form_mail->db_read( $form_no ); // フォームデータが空の場合、既削除フォームの再度作成 if ( $form_data == '' ) // フォーム初期設定 $form_data = $this->form_data_default(); // フォーム設定入力画面表示 $this->form_message = ''; $this->show_form_setting( $form_no, $form_data ); return; } if ( $admin_next == 'コピー' ) { // データベースからフォームデータの読み込み $form_data = $hk_form_mail->db_read( $form_no ); // フォーム付帯情報のクリア $form_data['creator'] = ''; $form_data['updater'] = ''; $form_data['create_day'] = ''; $form_data['update_day'] = ''; // データベースからフォーム実在数の読み込み $form_exist = $hk_form_mail->db_read_index(); // フォーム番号、フォーム存在数(仮) ++$form_exist; $form_no = $form_exist; // フォーム設定入力画面表示 $this->form_message = '新しいフォームにコピーしました'; $this->show_form_setting( $form_no, $form_data ); return; } if ( $admin_next == '項目追加' ) { // POSTされたデータの取り込み $form_data = $this->admin_form_post_data(); // POSTされたデータのエラーチェック $this->admin_form_post_check( $form_data ); if ( $this->form_message ) { $this->show_form_setting( $form_no, $form_data ); return; } // 項目並べ替え $form_data = $this->form_reorder( $form_data ); // 項目数 $label_exist = $form_data['label_exist']; ++$label_exist; $form_data['label_exist'] = $label_exist; // 追加項目のデータクリア $form_data[ $label_exist ]['order'] = $label_exist; $form_data[ $label_exist ]['label'] = ''; $form_data[ $label_exist ]['need'] = ''; $form_data[ $label_exist ]['type'] = 'text'; // 設定入力画面表示 $this->form_message = '項目欄を追加しました'; $this->show_form_setting( $form_no, $form_data ); return; } if ( $admin_next == '並び替え' ) { // POSTされたデータの取り込み $form_data = $this->admin_form_post_data(); // POSTされたデータのエラーチェック $this->admin_form_post_check( $form_data ); if ( $this->form_message ) { $this->show_form_setting( $form_no, $form_data ); return; } // 項目並べ替え $form_data = $this->form_reorder( $form_data ); // 設定入力画面表示 $this->form_message = '並び替えました'; $this->show_form_setting( $form_no, $form_data ); return; } if ( $admin_next == '変更を保存' ) { // POSTされたデータの取り込み $form_data = $this->admin_form_post_data(); // POSTされたデータのエラーチェック $this->admin_form_post_check( $form_data ); if ( $this->form_message ) { $this->show_form_setting( $form_no, $form_data ); return; } // 項目並べ替え $form_data = $this->form_reorder( $form_data ); // ログイン者ID $current_user_id = get_current_user_id(); // 日付 date_default_timezone_set('Asia/Tokyo'); $today = date("Y-m-d"); // 作成者、作成日 $creator = $form_data['creator']; if ( $creator == '' ) { $form_data['creator'] = $current_user_id; $form_data['create_day'] = $today; } // 更新者、更新日 $form_data['updater'] = $current_user_id; $form_data['update_day'] = $today; // データベースからフォーム実在数の読み込み $form_exist = $hk_form_mail->db_read_index(); // form_no が form_exist より大きければ新規 if ( $form_no > $form_exist ) { $form_exist = $form_no; // データベース更新 $hk_form_mail->db_write_index( $form_exist ); } // データベース更新 $hk_form_mail->db_write( $form_no, $form_data ); // 更新メッセージ → フォーム選択画面表示 $this->form_message = '変更を保存しました'; $this->show_form_setting( $form_no, $form_data ); return; } return; } // フォームデータのデフォルト設定 // 戻り値:$form_data private function form_data_default() { $site_name = hk_func_site_name(); $str_reg = $site_name.'から下記のメッセージが送信されました。'."\n"; $str_reg .= ' (このメールは自動送信でお送りしています。)'; $form_data = array( 'form_name' => '設定', 'label_exist' => 6, 'title' => '【'.$site_name.'】問い合わせ', 'to' => get_bloginfo('admin_email'), 'cc' => '', 'bcc' => '', 'from' => get_bloginfo('admin_email'), 'lead' => $str_reg, 'creator' => '', 'updater' => '', 'create_day' => '', 'update_day' => '', 1 => array( 'order' => 1, 'label' => 'お名前', 'need' => 'yes', 'type' => 'text' ), 2 => array( 'order' => 2, 'label' => '会社名', 'need' => '', 'type' => 'text' ), 3 => array( 'order' => 3, 'label' => 'メールアドレス', 'need' => 'yes', 'type' => 'email' ), 4 => array( 'order' => 4, 'label' => '電話番号', 'need' => '', 'type' => 'tel' ), 5 => array( 'order' => 5, 'label' => '件名', 'need' => '', 'type' => 'text' ), 6 => array( 'order' => 6, 'label' => 'お問い合わせの内容', 'need' => 'yes', 'type' => 'textarea' ), ); return $form_data; } // POSTされたデータの取り込み // 戻り値:$form_data private function admin_form_post_data() { // 初期化 $form_data = array(); // フォーム名称 $form_data['form_name'] = hk_func_post_read( 'form_name', 1 ); // 項目数 $form_data['label_exist'] = hk_func_post_read( 'label_exist', 1 ); // メール項目 to,cc,bcc は連続した改行を改行一つに置き換え $form_data['title'] = hk_func_post_read( 'title', 1 ); $to = hk_func_post_read( 'to', 2 ); $form_data['to'] = preg_replace( '/\n+/', "\n", $to ); $cc = hk_func_post_read( 'cc', 2 ); $form_data['cc'] = preg_replace( '/\n+/', "\n", $cc ); $bcc = hk_func_post_read( 'bcc', 2 ); $form_data['bcc'] = preg_replace( '/\n+/', "\n", $bcc ); $form_data['from'] = hk_func_post_read( 'from', 1 ); $form_data['lead'] = hk_func_post_read( 'lead', 2 ); // フォーム項目 for ( $i=1; $i<=$form_data['label_exist']; $i++ ) { $str_reg1 = 'order_'.$i; $form_data[ $i ]['order'] = hk_func_post_read( $str_reg1, 1 ); $str_reg1 = 'label_'.$i; $form_data[ $i ]['label'] = hk_func_post_read( $str_reg1, 1 ); $str_reg1 = 'need_'.$i; $form_data[ $i ]['need'] = hk_func_post_read( $str_reg1, 1 ); $str_reg1 = 'type_'.$i; $form_data[ $i ]['type'] = hk_func_post_read( $str_reg1, 1 ); } // 付帯情報 $form_data['creator'] = hk_func_post_read( 'creator', 1 ); $form_data['updater'] = hk_func_post_read( 'updater', 1 ); $form_data['create_day'] = hk_func_post_read( 'create_day', 1 ); $form_data['update_day'] = hk_func_post_read( 'update_day', 1 ); return $form_data; } // POSTされたデータのチェック // 戻り値:なし private function admin_form_post_check( $form_data ) { // 入力エラーチェックメッセージ初期化 $this->form_message = ''; // フォーム名称 if ( $form_data['form_name'] == '' ) $this->form_message .= 'エラー:フォーム名称が入力されていません'."\n"; // 項目数 $label_exist = $form_data['label_exist']; // 存在する項目数のカウント $label_exist_new = 0; // フォーム設定 for ( $i=1; $i<=$label_exist; $i++ ) { // 順番 $reg1 = $form_data[ $i ]['order']; if ( $reg1 != '' ) { ++$label_exist_new; // 数字だけで構成されているか if ( ! preg_match( '/^[0-9]+$/', $reg1 ) ) $this->form_message .= 'エラー:NO.'.$i.' フォームの順番に数値以外が入力されています'."\n"; // 項目名 if ( $form_data[ $i ]['label'] == '' ) $this->form_message .= 'エラー:NO.'.$i.' フォームの項目名が入力されていません'."\n"; } } if ( $label_exist_new == 0 ) $this->form_message .= 'エラー:フォーム項目が一つも入力されていません'."\n"; // メール送信設定 if ( $form_data['title'] == '' ) $this->form_message .= 'エラー:タイトルが入力されていません'."\n"; if ( $form_data['to'] == '' ) $this->form_message .= 'エラー:TO が入力されていません'."\n"; else if ( $this->address_check( $form_data['to'] ) === false ) $this->form_message .= 'エラー:TO の書式に誤りがあります'."\n"; if (( $form_data['cc'] ) && ( $this->address_check( $form_data['cc'] ) === false )) $this->form_message .= 'エラー:CC の書式に誤りがあります'."\n"; if (( $form_data['bcc'] ) && ( $this->address_check( $form_data['bcc'] ) === false )) $this->form_message .= 'エラー:BCC の書式に誤りがあります'."\n"; if ( $form_data['from'] == '' ) $this->form_message .= 'エラー:FROM が入力されていません'."\n"; else if ( filter_var( $form_data['from'], FILTER_VALIDATE_EMAIL ) === false ) $this->form_message .= 'エラー:FROM の書式に誤りがあります'."\n"; if ( $form_data['lead'] == '') $this->form_message .= 'エラー:メール冒頭文が入力されていません'."\n"; // エラーメッセージ整形 if ( $this->form_message ) $this->form_message = trim( $this->form_message ); return; } // メールアドレスのチェック 改行区切りによる複数入力用 // 戻り値:true、false private function address_check( $string ) { // 改行で分割 $address_array = explode( "\n", $string ); // チェック foreach( $address_array as $address ) { if ( filter_var( $address, FILTER_VALIDATE_EMAIL ) === false ) return false; } unset( $address ); return true; } // 並び替え 順番が空欄の場合、削除 // 戻り値:並び替え後の$form_data private function form_reorder( $form_data ) { // 項目数 $label_exist = $form_data['label_exist']; // データを並び替え用にコピー $order = array(); $label = array(); $need = array(); $type = array(); for ( $i=1; $i<=$label_exist; $i++ ) { if ( $form_data[ $i ]['order'] === '' ) continue; $order[ $i ] = $form_data[ $i ]['order']; $label[ $i ] = $form_data[ $i ]['label']; $need[ $i ] = $form_data[ $i ]['need']; $type[ $i ] = $form_data[ $i ]['type']; } // 並び替え asort( $order ); $i = 0; foreach( $order as $key => $value ) { $i++; $form_data[ $i ]['order'] = $i; $form_data[ $i ]['label'] = $label[ $key ]; $form_data[ $i ]['need'] = $need[ $key ]; $form_data[ $i ]['type'] = $type[ $key ]; } unset( $key ); unset( $value ); // 項目数書き換え $form_data['label_exist'] = $i; return $form_data; } // フォーム設定入力画面表示 private function show_form_setting( $form_no, $form_data ) { echo "\n\n".'<div class="hk_plugin_admin">'."\n"; echo ' <h1>フォームメール 設定</h1>'."\n"; if ( $this->form_message ) { $this->form_message = trim( $this->form_message ); $this->form_message = nl2br( $this->form_message ); echo ' <div class="hk_message">'."\n"; echo $this->form_message."\n"; echo ' </div>'."\n"; } // HTML特殊文字変換 $form_data = $this->admin_form_data_to_html( $form_data ); // ショートコード $short_code = '[hk-form-mail form_no='.$form_no.']'; // フォーム表示開始 echo ' <form method="post">'."\n"; echo ' <input type="hidden" name="form_no" value="'.$form_no.'" />'."\n"; echo ' <input type="hidden" name="label_exist" value="'.$form_data['label_exist'].'" />'."\n"; echo ' <input type="hidden" name="creator" value="'.$form_data['creator'].'" />'."\n"; echo ' <input type="hidden" name="updater" value="'.$form_data['updater'].'" />'."\n"; echo ' <input type="hidden" name="create_day" value="'.$form_data['create_day'].'" />'."\n"; echo ' <input type="hidden" name="update_day" value="'.$form_data['update_day'].'" />'."\n"; echo ' <div class="hk_chap">'."\n"; echo ' <h3>フォーム基本情報</h3>'."\n"; echo ' <p> フォーム名称を編集できます。</p>'."\n"; echo ' <p> 【使用方法】ショートコードをページの本文に貼り付けてください。</p>'."\n"; echo '<table><tbody>'."\n"; echo '<tr>'."\n"; echo ' <th>フォーム番号</th>'."\n"; echo ' <td>NO.'.$form_no.'</td>'."\n"; echo '</tr>'."\n"; echo '<tr>'."\n"; echo ' <th>フォーム名称</th>'."\n"; echo ' <td><input type="text" name="form_name" size="40" value="'.$form_data['form_name'].'" /></td>'."\n"; echo '</tr>'."\n"; echo '<tr>'."\n"; echo ' <th>ショートコード</th>'."\n"; echo ' <td>'.$short_code.'</td>'."\n"; echo '</tr>'."\n"; echo '<tr>'."\n"; echo ' <th>更新者・更新日</th>'."\n"; $user_obj = get_userdata( $form_data['updater'] ); if ( $user_obj ) $user_name = $user_obj->last_name.' '.$user_obj->first_name; else $user_name = '------'; echo ' <td>'.$user_name.' '.$form_data['update_day'].'</td>'."\n"; echo '</tr>'."\n"; echo '<tr>'."\n"; echo ' <th>作成者・作成日</th>'."\n"; $user_obj = get_userdata( $form_data['creator'] ); if ( $user_obj ) $user_name = $user_obj->last_name.' '.$user_obj->first_name; else $user_name = '------'; echo ' <td>'.$user_name.' '.$form_data['create_day'].'</td>'."\n"; echo '</tr>'."\n"; echo '</tbody></table>'."\n"; echo ' </div>'."\n"; echo ' <div class="hk_chap">'."\n"; echo ' <h3>フォーム設定</h3>'."\n"; echo ' <p> 項目の順番は、メール入力ページの項目順及びメール送信の項目順になります。<br /> 順番を空欄にすると削除されます。</p>'."\n"; echo '<table><tbody>'."\n"; echo '<tr>'."\n"; echo ' <th>順番</th>'."\n"; echo ' <th>項目名</th>'."\n"; echo ' <th>必須</th>'."\n"; echo ' <th>入力タイプ</th>'."\n"; echo '</tr>'."\n"; for ( $i=1; $i<=$form_data['label_exist']; $i++ ) { echo '<tr>'."\n"; // 順番 echo ' <td><input type="text" name="order_'.$i.'" size="3" value="'.$form_data[ $i ]['order'].'" /></td>'."\n"; // 項目名 echo ' <td><input type="text" name="label_'.$i.'" size="30" value="'.$form_data[ $i ]['label'].'" /></td>'."\n"; // 必須 echo ' <td><input type="checkbox" name="need_'.$i.'" value="yes"'; if ( $form_data[ $i ]['need'] == 'yes' ) echo ' checked="checked"'; echo ' /> 必須</td>'."\n"; // 入力タイプ echo ' <td><select size="1" name="type_'.$i.'">'."\n"; $type_option = array( 'text' => '一行テキスト', 'email' => 'メールアドレス', 'tel' => '電話番号', 'url' => 'URL', 'textarea' => '複数行テキスト', ); foreach( $type_option as $key => $value ) { echo ' <option value="'.$key.'"'; if ( $key == $form_data[ $i ]['type'] ) echo ' selected="selected"'; echo '>'.$value.'</option>'."\n"; } unset( $key ); unset( $value ); echo ' </select></td>'."\n"; echo '</tr>'."\n"; } echo '</tbody></table>'."\n"; echo ' <div class="hk_sec1">'."\n"; echo ' <input type="submit" name="admin_next" value="項目追加" /> '."\n"; echo ' <input type="submit" name="admin_next" value="並び替え" />'."\n"; echo ' </div>'."\n"; echo ' </div>'."\n"; echo ' <div class="hk_chap">'."\n"; echo ' <h3>メール送信設定</h3>'."\n"; echo ' <p> TO、CC、BCC にアドレスを複数設定する場合、アドレスを改行で区切ってください。</p>'."\n"; echo '<table><tbody>'."\n"; echo '<tr>'."\n"; echo ' <th>タイトル</th>'."\n"; echo ' <td><input type="text" name="title" size="60" value="'.$form_data['title'].'" /></td>'."\n"; echo '</tr>'."\n"; echo '<tr>'."\n"; echo ' <th>TO</th>'."\n"; echo ' <td><textarea cols="60" name="to" rows="4">'.$form_data['to'].'</textarea></td>'."\n"; echo '</tr>'."\n"; echo '<tr>'."\n"; echo ' <th>CC</th>'."\n"; echo ' <td><textarea cols="60" name="cc" rows="4">'.$form_data['cc'].'</textarea></td>'."\n"; echo '</tr>'."\n"; echo '<tr>'."\n"; echo ' <th>BCC</th>'."\n"; echo ' <td><textarea cols="60" name="bcc" rows="4">'.$form_data['bcc'].'</textarea></td>'."\n"; echo '</tr>'."\n"; echo '<tr>'."\n"; echo ' <th>FROM</th>'."\n"; echo ' <td><input type="email" name="from" size="60" value="'.$form_data['from'].'" /></td>'."\n"; echo '</tr>'."\n"; echo '<tr>'."\n"; echo ' <th>メール冒頭文</th>'."\n"; echo ' <td><textarea cols="60" name="lead" rows="4">'.$form_data['lead'].'</textarea></td>'."\n"; echo '</tr>'."\n"; echo '</tbody></table>'."\n"; echo ' </div>'."\n"; // SUBMIT echo ' <div class="hk_sec1">'."\n"; echo ' <input type="submit" name="admin_next" value="変更を保存" /> '."\n"; echo ' <input type="submit" name="admin_next" value="初期化" />(デフォルトに戻す) '."\n"; echo ' <input type="submit" name="admin_next" value="フォーム選択" />'."\n"; echo ' </div>'."\n"; echo ' </form>'."\n"; echo '</div>'."\n"; return; } // HTML特殊文字変換 // 戻り値:変換後の$form_data private function admin_form_data_to_html( $form_data ) { $form_data['form_name'] = htmlspecialchars( $form_data['form_name'] ); $form_data['title'] = htmlspecialchars( $form_data['title'] ); $form_data['to'] = htmlspecialchars( $form_data['to'] ); $form_data['cc'] = htmlspecialchars( $form_data['cc'] ); $form_data['bcc'] = htmlspecialchars( $form_data['bcc'] ); $form_data['from'] = htmlspecialchars( $form_data['from'] ); $form_data['lead'] = htmlspecialchars( $form_data['lead'] ); $label_exist = $form_data['label_exist']; for ( $i=1; $i<=$label_exist; $i++ ) { $form_data[ $i ]['order'] = htmlspecialchars( $form_data[ $i ]['order'] ); $form_data[ $i ]['label'] = htmlspecialchars( $form_data[ $i ]['label'] ); } return $form_data; } }
説明
下記関数に関しては“共通関数”をご覧ください。
hk_func_site_name
hk_func_post_read
このプログラムをお使いになる場合は、お使いになる方の自己責任でお願いします。