<?php import('libs/plugins/file.php'); import('libs/plugins/ui.php'); if ($_SERVER['REQUEST_METHOD'] === 'POST') { // ワンタイムトークン if ((empty($_POST['view']) || $_POST['view'] !== 'preview') && !token('check')) { error('不正なアクセスです。'); } // 入力データを整理 $post = array('member' => normalize_members(array('id' => isset($_POST['id']) ? $_POST['id'] : '', 'class_id' => isset($_POST['class_id']) ? $_POST['class_id'] : '', 'name' => isset($_POST['name']) ? $_POST['name'] : '', 'name_kana' => isset($_POST['name_kana']) ? $_POST['name_kana'] : '', 'grade' => isset($_POST['grade']) ? $_POST['grade'] : '', 'birthday' => isset($_POST['birthday']) ? $_POST['birthday'] : '', 'email' => isset($_POST['email']) ? $_POST['email'] : '', 'tel' => isset($_POST['tel']) ? $_POST['tel'] : '', 'memo' => isset($_POST['memo']) ? $_POST['memo'] : '', 'public' => isset($_POST['public']) ? $_POST['public'] : '', 'category_sets' => isset($_POST['category_sets']) ? $_POST['category_sets'] : array()))); if (isset($_POST['view']) && $_POST['view'] === 'preview') { // プレビュー $_view['member'] = $post['member']; } else { // 入力データを検証&登録 $warnings = validate_members($post['member']); if (isset($_POST['_type']) && $_POST['_type'] === 'json') { if (empty($warnings)) { ok(); } else { warning($warnings); } } else { if (empty($warnings)) { $_SESSION['post']['member'] = $post['member']; // フォワード forward('/admin/member_post'); } else { $_view['member'] = $post['member']; $_view['warnings'] = $warnings;
/** * 名簿をインポート * * @param string $filename * * @return array */ function service_member_import($filename) { if ($fp = fopen($filename, 'r')) { $options = array('grades' => array_flip($GLOBALS['config']['options']['member']['grades']), 'publics' => array_flip($GLOBALS['config']['options']['member']['publics'])); if ($_POST['operation'] === 'replace') { // 元データ削除 $resource = db_delete(array('delete_from' => DATABASE_PREFIX . 'members')); if (!$resource) { error('データを削除できません。'); } $resource = db_delete(array('delete_from' => DATABASE_PREFIX . 'category_sets')); if (!$resource) { error('データを削除できません。'); } } // CSVファイルの一行目を無視 $dummy = file_getcsv($fp); // CSVファイル読み込み $all_warnings = array(); $i = 1; while ($line = file_getcsv($fp)) { list($id, $created, $modified, $deleted, $class_id, $name, $name_kana, $grade, $birthday, $email, $tel, $memo, $image_01, $image_02, $public, $dummy, $category_sets) = $line; // 入力データを整理 $post = array('member' => normalize_members(array('id' => mb_convert_encoding($id, 'UTF-8', 'SJIS-WIN'), 'created' => mb_convert_encoding($created, 'UTF-8', 'SJIS-WIN'), 'modified' => mb_convert_encoding($modified, 'UTF-8', 'SJIS-WIN'), 'deleted' => mb_convert_encoding($deleted, 'UTF-8', 'SJIS-WIN'), 'class_id' => mb_convert_encoding($class_id, 'UTF-8', 'SJIS-WIN'), 'name' => mb_convert_encoding($name, 'UTF-8', 'SJIS-WIN'), 'name_kana' => mb_convert_encoding($name_kana, 'UTF-8', 'SJIS-WIN'), 'grade' => $options['grades'][mb_convert_encoding($grade, 'UTF-8', 'SJIS-WIN')], 'birthday' => mb_convert_encoding($birthday, 'UTF-8', 'SJIS-WIN'), 'email' => mb_convert_encoding($email, 'UTF-8', 'SJIS-WIN'), 'tel' => mb_convert_encoding($tel, 'UTF-8', 'SJIS-WIN'), 'memo' => mb_convert_encoding($memo, 'UTF-8', 'SJIS-WIN'), 'image_01' => mb_convert_encoding($image_01, 'UTF-8', 'SJIS-WIN'), 'image_02' => mb_convert_encoding($image_02, 'UTF-8', 'SJIS-WIN'), 'public' => $options['publics'][mb_convert_encoding($public, 'UTF-8', 'SJIS-WIN')]))); // 入力データを検証&登録 $warnings = validate_members($post['member']); if (empty($warnings)) { if ($_POST['operation'] === 'update') { // データ編集 $resource = db_update(array('update' => DATABASE_PREFIX . 'members', 'set' => array('created' => $post['member']['created'], 'modified' => $post['member']['modified'], 'deleted' => $post['member']['deleted'], 'class_id' => $post['member']['class_id'], 'name' => $post['member']['name'], 'name_kana' => $post['member']['name_kana'], 'grade' => $post['member']['grade'], 'birthday' => $post['member']['birthday'], 'email' => $post['member']['email'], 'tel' => $post['member']['tel'], 'memo' => $post['member']['memo'], 'image_01' => $post['member']['image_01'], 'image_02' => $post['member']['image_02'], 'public' => $post['member']['public']), 'where' => array('id = :id', array('id' => $post['member']['id'])))); if (!$resource) { error('データを編集できません。'); } } else { // データ登録 $resource = db_insert(array('insert_into' => DATABASE_PREFIX . 'members', 'values' => array('id' => $post['member']['id'], 'created' => $post['member']['created'], 'modified' => $post['member']['modified'], 'deleted' => $post['member']['deleted'], 'class_id' => $post['member']['class_id'], 'name' => $post['member']['name'], 'name_kana' => $post['member']['name_kana'], 'grade' => $post['member']['grade'], 'birthday' => $post['member']['birthday'], 'email' => $post['member']['email'], 'tel' => $post['member']['tel'], 'memo' => $post['member']['memo'], 'image_01' => $post['member']['image_01'], 'image_02' => $post['member']['image_02'], 'public' => $post['member']['public']))); if (!$resource) { error('データを登録できません。'); } } if ($category_sets) { // 分類を登録 $category_sets = explode(',', $category_sets); foreach ($category_sets as $category_id) { $resource = db_insert(array('insert_into' => DATABASE_PREFIX . 'category_sets', 'values' => array('category_id' => $category_id, 'member_id' => $id))); if (!$resource) { return $resource; } } } } else { foreach ($warnings as $warning) { $all_warnings[] = '[' . $i . '行目] ' . $warning; } } $i++; } fclose($fp); if (empty($all_warnings)) { return array(); } else { return $all_warnings; } } else { return array('ファイルを読み込めません。'); } }