Version 27.1 by Super Admin on 2026/03/12 12:20

Show last authors
1 {{velocity}}
2 ## 新しい学校ページ作成フォーム(学校マスターデータ検索方式)
3
4 #set($currentUser = $xcontext.user)
5 #if($currentUser == 'XWiki.XWikiGuest')
6 {{error}}学校ページの作成にはログインが必要です。[[ログイン>>path:/bin/login/XWiki/XWikiLogin]]してください。{{/error}}
7 #stop
8 #end
9
10 #if($request.action && $request.action == 'create' && $services.csrf.isTokenValid($request.form_token))
11 ## フォーム送信後の処理
12 #set($schoolCode = $request.schoolCode)
13 #set($schoolName = $request.schoolName)
14
15 ## バリデーション: 学校コードが13文字の文科省形式であること(例: D112310000377)
16 ## 形式: 学校種(C1/C2/D1/D2) + 都道府県(2桁) + 設置区分(1桁) + 学校番号(7桁) + 検査数字(1桁)
17 #if(!$schoolCode || $schoolCode.length() != 13 || !$schoolCode.matches('^[CD][12]\d{11}$'))
18 {{error}}学校コードが不正です。学校名検索から選択してください。{{/error}}
19 #stop
20 #end
21
22 #set($targetPage = "Schools.${schoolCode}.WebHome")
23
24 #if($xwiki.exists($targetPage))
25 {{error}}この学校のページは既に存在します。[[既存のページを開く>>$targetPage]]{{/error}}
26 #else
27 ## ページをプログラム的に作成
28 #set($newDoc = $xwiki.getDocument($targetPage))
29 ## SchoolTemplateのコンテンツを参照として設定(テンプレートの include)
30 ## author="target" により、SchoolTemplateの作者(superadmin)の権限でVelocityを実行
31 ## これがないと学校ページの作者(一般ユーザー)のscript権限が必要になりエラーになる
32 $newDoc.setContent('{{include reference="SeitokaiCode.SchoolTemplate" author="target" /}}')
33 $newDoc.setTitle($schoolName)
34 $newDoc.setParent('Schools.WebHome')
35 ## SchoolClassオブジェクトを追加して初期値を設定
36 #set($objNum = $newDoc.createNewObject('SeitokaiCode.SchoolClass'))
37 #set($newObj = $newDoc.getObject('SeitokaiCode.SchoolClass', $objNum))
38 $newObj.set('schoolCode', $schoolCode)
39 $newObj.set('schoolName', $schoolName)
40 ## 現在の年度を初期値として設定(4月始まり)
41 #set($now = $datetool.date)
42 #set($currentYear = $datetool.format('yyyy', $now))
43 #set($currentMonth = $mathtool.toInteger($datetool.format('MM', $now)))
44 #if($currentMonth >= 4)
45 #set($fiscalYear = $currentYear)
46 #else
47 #set($fiscalYear = $mathtool.sub($mathtool.toInteger($currentYear), 1).toString())
48 #end
49 $newObj.set('fiscalYear', $fiscalYear)
50 $newDoc.saveWithProgrammingRights('学校ページを新規作成')
51 ## 作成後にビューにリダイレクト
52 $response.sendRedirect($xwiki.getURL($targetPage, 'view'))
53 #end
54 #else
55
56 = 新しい学校ページを作成 =
57
58 {{info}}
59 学校名で検索してページを作成します。1校で複数のページを持つことはできません。文科省の学校コード毎に学校ページを作成することができます。
60 {{/info}}
61
62 {{html clean="false"}}
63 <div class="school-create-form">
64 <form method="post" id="createSchoolForm">
65 <input type="hidden" name="action" value="create" />
66 <input type="hidden" name="form_token" value="$services.csrf.getToken()" />
67 <input type="hidden" name="schoolCode" id="schoolCode" />
68 <input type="hidden" name="schoolName" id="schoolName" />
69
70 <div class="form-group">
71 <label class="form-label">学校を検索 <span class="required-mark">*</span></label>
72 <div class="search-input-wrap">
73 <input type="text" id="schoolSearchInput" class="form-input search-input-padded"
74 placeholder="学校名で検索(例: 日比谷、渋谷、北野...)"
75 autocomplete="off" />
76 <span class="search-input-icon"><svg class="ico" viewBox="0 0 24 24"><circle cx="11" cy="11" r="8"/><line x1="21" y1="21" x2="16.65" y2="16.65"/></svg></span>
77 <ul id="schoolSearchResults" class="search-results-list"></ul>
78 </div>
79 <div class="form-hint">文科省の学校マスターデータから候補を検索します。</div>
80 </div>
81
82 <!-- 選択結果 -->
83 <div id="selectedSchoolArea" style="display:none;" class="form-group">
84 <div class="selected-school-card">
85 <div class="selected-school-layout">
86 <div>
87 <div class="selected-school-label">選択された学校</div>
88 <div id="selectedSchoolName" class="selected-school-name"></div>
89 <div id="selectedSchoolInfo" class="selected-school-info"></div>
90 <div id="selectedSchoolCode" class="selected-school-code"></div>
91 </div>
92 <button type="button" onclick="clearSelection()" class="btn-change-school">変更</button>
93 </div>
94 </div>
95 </div>
96
97 <!-- 重複チェック結果 -->
98 <div id="duplicateWarning" class="duplicate-warning" style="display:none;">
99 <strong><svg class="ico" viewBox="0 0 24 24"><path d="M10.29 3.86L1.82 18a2 2 0 001.71 3h16.94a2 2 0 001.71-3L13.71 3.86a2 2 0 00-3.42 0z"/><line x1="12" y1="9" x2="12" y2="13"/><line x1="12" y1="17" x2="12.01" y2="17"/></svg> この学校のページは既に存在します</strong>
100 <div style="margin-top:6px;"><a id="duplicateLink" href="#">既存のページを開く →</a></div>
101 </div>
102
103 <!-- 作成ボタン -->
104 <div id="submitArea" style="display:none;">
105 <div class="create-ready-box">
106 <svg class="ico" viewBox="0 0 24 24" stroke-width="2.5"><path d="M20 6L9 17l-5-5"/></svg> この学校のページはまだ作成されていません。
107 </div>
108 <button type="submit" class="btn-save btn-full-width">ページを作成</button>
109 </div>
110 </form>
111
112 <!-- 未選択時ヒント -->
113 <div id="searchHint" class="search-hint-box">
114 <div class="search-hint-icon"><svg class="ico" viewBox="0 0 24 24"><circle cx="11" cy="11" r="8"/><line x1="21" y1="21" x2="16.65" y2="16.65"/></svg></div>
115 <div class="search-hint-text">上の検索欄から学校名を入力してください</div>
116 </div>
117 </div>
118
119 <script>
120 (function() {
121 // XSSエスケープ関数
122 function escapeHtml(s) {
123 var div = document.createElement('div');
124 div.appendChild(document.createTextNode(s));
125 return div.innerHTML;
126 }
127
128 // 学校マスターデータをJSON APIから取得
129 var schools = [];
130 var schoolsLoaded = false;
131
132 // schools.json をロード(XWiki添付ファイルとして配置)
133 // 配置先: SeitokaiCode.SchoolMasterData の添付ファイル
134 fetch('/rest/wikis/xwiki/spaces/SeitokaiCode/pages/SchoolMasterData/attachments/schools.json')
135 .then(function(r) { return r.json(); })
136 .then(function(data) { schools = data; schoolsLoaded = true; })
137 .catch(function() {
138 console.warn('学校マスターデータの読み込みに失敗しました。SeitokaiCode.SchoolMasterData に schools.json が添付されているか確認してください。');
139 });
140
141 var input = document.getElementById('schoolSearchInput');
142 var resultsList = document.getElementById('schoolSearchResults');
143 var debounceTimer = null;
144
145 input.addEventListener('input', function() {
146 clearTimeout(debounceTimer);
147 debounceTimer = setTimeout(function() { searchSchools(input.value); }, 200);
148 });
149
150 function searchSchools(query) {
151 if (!query || query.length < 1 || !schoolsLoaded) {
152 resultsList.style.display = 'none';
153 return;
154 }
155 var q = query.toLowerCase();
156 var results = schools.filter(function(s) {
157 return s.name.toLowerCase().indexOf(q) >= 0 ||
158 s.pref.indexOf(q) >= 0 ||
159 s.city.indexOf(q) >= 0 ||
160 s.code.indexOf(q) >= 0;
161 }).slice(0, 10);
162
163 resultsList.innerHTML = '';
164 if (results.length === 0) {
165 resultsList.innerHTML = '<li class="search-result-empty">該当する学校が見つかりません</li>';
166 } else {
167 results.forEach(function(s) {
168 var li = document.createElement('li');
169 li.className = 'search-result-item';
170 li.innerHTML =
171 '<div class="search-result-name">' + escapeHtml(s.name) + '</div>' +
172 '<div class="search-result-info">' +
173 escapeHtml(s.pref) + ' ' + escapeHtml(s.city) + ' ・ ' + escapeHtml(s.type) + '(' + escapeHtml(s.est) + ')' +
174 '</div>' +
175 '<div class="search-result-code">' + escapeHtml(s.code) + '</div>';
176 li.onclick = function() { selectSchool(s); };
177 resultsList.appendChild(li);
178 });
179 }
180 resultsList.style.display = 'block';
181 }
182
183 window.selectSchool = function(school) {
184 // hidden フィールドにセット
185 document.getElementById('schoolCode').value = school.code;
186 document.getElementById('schoolName').value = school.name;
187
188 // UI更新
189 input.style.display = 'none';
190 resultsList.style.display = 'none';
191 document.getElementById('searchHint').style.display = 'none';
192
193 document.getElementById('selectedSchoolName').textContent = school.name;
194 document.getElementById('selectedSchoolInfo').textContent =
195 school.pref + ' ' + school.city + ' ・ ' + school.type + '(' + school.est + ')';
196 document.getElementById('selectedSchoolCode').textContent = '学校コード: ' + school.code;
197 document.getElementById('selectedSchoolArea').style.display = 'block';
198
199 // 重複チェック(XWikiにページが存在するか確認)
200 var targetPage = 'Schools.' + school.code + '.WebHome';
201 fetch('/rest/wikis/xwiki/spaces/Schools/spaces/' + school.code + '/pages/WebHome')
202 .then(function(r) {
203 if (r.ok) {
204 // 既存ページあり
205 document.getElementById('duplicateWarning').style.display = 'block';
206 document.getElementById('duplicateLink').href =
207 '/bin/Schools/' + school.code + '/';
208 document.getElementById('submitArea').style.display = 'none';
209 } else {
210 // 新規作成OK
211 document.getElementById('duplicateWarning').style.display = 'none';
212 document.getElementById('submitArea').style.display = 'block';
213 }
214 })
215 .catch(function() {
216 // APIエラー時は作成を許可(サーバー側で重複チェックもある)
217 document.getElementById('submitArea').style.display = 'block';
218 });
219 };
220
221 window.clearSelection = function() {
222 document.getElementById('schoolCode').value = '';
223 document.getElementById('schoolName').value = '';
224 document.getElementById('selectedSchoolArea').style.display = 'none';
225 document.getElementById('duplicateWarning').style.display = 'none';
226 document.getElementById('submitArea').style.display = 'none';
227 document.getElementById('searchHint').style.display = '';
228 input.style.display = '';
229 input.value = '';
230 input.focus();
231 };
232
233 // キーボードナビゲーション
234 var selectedIdx = -1;
235 input.addEventListener('keydown', function(e) {
236 var items = resultsList.querySelectorAll('li');
237 if (e.key === 'ArrowDown') {
238 e.preventDefault();
239 selectedIdx = Math.min(selectedIdx + 1, items.length - 1);
240 items.forEach(function(li, i) { li.classList.toggle('active', i === selectedIdx); });
241 } else if (e.key === 'ArrowUp') {
242 e.preventDefault();
243 selectedIdx = Math.max(selectedIdx - 1, 0);
244 items.forEach(function(li, i) { li.classList.toggle('active', i === selectedIdx); });
245 } else if (e.key === 'Enter' && selectedIdx >= 0) {
246 e.preventDefault();
247 items[selectedIdx].click();
248 selectedIdx = -1;
249 } else if (e.key === 'Escape') {
250 resultsList.style.display = 'none';
251 selectedIdx = -1;
252 }
253 });
254
255 // クリック外で閉じる
256 document.addEventListener('click', function(e) {
257 if (!e.target.closest('.school-create-form')) {
258 resultsList.style.display = 'none';
259 selectedIdx = -1;
260 }
261 });
262 })();
263 </script>
264 {{/html}}
265
266 #end
267 {{/velocity}}