Version 22.1 by Super Admin on 2026/03/09 11:59

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