Bug 34789: Update usage throughout Koha modules and Vue components
[koha.git] / koha-tmpl / intranet-tmpl / prog / js / vue / components / ERM / EHoldingsLocalTitlesFormAdd.vue
1 <template>
2     <div v-if="!initialized">{{ $__("Loading") }}</div>
3     <div v-else id="titles_add">
4         <h2 v-if="title.title_id">
5             {{ $__("Edit title #%s").format(title.title_id) }}
6         </h2>
7         <h2 v-else>{{ $__("New title") }}</h2>
8         <div>
9             <form @submit="onSubmit($event)">
10                 <fieldset class="rows">
11                     <ol>
12                         <li v-if="title.title_id">
13                             <label for="title_title_id"
14                                 >{{ $__("Title identifier") }}:</label
15                             >
16                             <span>
17                                 {{ title.title_id }}
18                             </span>
19                         </li>
20
21                         <li>
22                             <label class="required" for="title_name"
23                                 >{{ $__("Publication title") }}:</label
24                             >
25                             <input
26                                 id="title_publication_title"
27                                 v-model="title.publication_title"
28                                 :placeholder="$__('Publication title')"
29                                 required
30                             />
31                             <span class="required">{{ $__("Required") }}</span>
32                         </li>
33
34                         <li>
35                             <label for="title_print_identifier"
36                                 >{{ $__("Print-format identifier") }}:</label
37                             >
38                             <input
39                                 id="title_print_identifier"
40                                 v-model="title.print_identifier"
41                                 :placeholder="$__('Print-format identifier')"
42                             />
43                         </li>
44
45                         <li>
46                             <label for="title_online_identifier"
47                                 >{{ $__("Online-format identifier") }}:</label
48                             >
49                             <input
50                                 id="title_online_identifier"
51                                 v-model="title.online_identifier"
52                                 :placeholder="$__('Online-format identifier')"
53                             />
54                         </li>
55
56                         <li>
57                             <label for="title_date_first_issue_online"
58                                 >{{
59                                     $__(
60                                         "Date of first serial issue available online"
61                                     )
62                                 }}:</label
63                             >
64                             <input
65                                 id="title_date_first_issue_online"
66                                 v-model="title.date_first_issue_online"
67                                 :placeholder="
68                                     $__(
69                                         'Date of first serial issue available online'
70                                     )
71                                 "
72                             />
73                         </li>
74
75                         <li>
76                             <label for="title_num_first_vol_online"
77                                 >{{
78                                     $__(
79                                         "Number of first volume available online"
80                                     )
81                                 }}:</label
82                             >
83                             <input
84                                 id="title_num_first_vol_online"
85                                 v-model="title.num_first_vol_online"
86                                 :placeholder="
87                                     $__(
88                                         'Number of first volume available online'
89                                     )
90                                 "
91                             />
92                         </li>
93
94                         <li>
95                             <label for="title_num_first_issue_online"
96                                 >{{
97                                     $__(
98                                         "Number of first issue available online"
99                                     )
100                                 }}:</label
101                             >
102                             <input
103                                 id="title_num_first_issue_online"
104                                 v-model="title.num_first_issue_online"
105                                 :placeholder="
106                                     $__(
107                                         'Number of first issue available online'
108                                     )
109                                 "
110                             />
111                         </li>
112
113                         <li>
114                             <label for="title_date_last_issue_online"
115                                 >{{
116                                     $__("Date of last issue available online")
117                                 }}:</label
118                             >
119                             <input
120                                 id="title_date_last_issue_online"
121                                 v-model="title.date_last_issue_online"
122                                 :placeholder="
123                                     $__('Date of last issue available online')
124                                 "
125                             />
126                         </li>
127
128                         <li>
129                             <label for="title_num_last_vol_online"
130                                 >{{
131                                     $__(
132                                         "Number of last volume available online"
133                                     )
134                                 }}:</label
135                             >
136                             <input
137                                 id="title_num_last_vol_online"
138                                 v-model="title.num_last_vol_online"
139                                 :placeholder="
140                                     $__(
141                                         'Number of last volume available online'
142                                     )
143                                 "
144                             />
145                         </li>
146
147                         <li>
148                             <label for="title_num_last_issue_online"
149                                 >{{
150                                     $__(
151                                         "Number of last issue available online"
152                                     )
153                                 }}:</label
154                             >
155                             <input
156                                 id="title_num_last_issue_online"
157                                 v-model="title.num_last_issue_online"
158                                 :placeholder="
159                                     $__('Number of last issue available online')
160                                 "
161                             />
162                         </li>
163
164                         <li>
165                             <label for="title_title_url"
166                                 >{{ $__("Title-level URL") }}:</label
167                             >
168                             <input
169                                 id="title_title_url"
170                                 v-model="title.title_url"
171                                 :placeholder="$__('Title-level URL')"
172                             />
173                         </li>
174
175                         <li>
176                             <label for="title_first_author"
177                                 >{{ $__("First author") }}:</label
178                             >
179                             <input
180                                 id="title_first_author"
181                                 v-model="title.first_author"
182                                 :placeholder="$__('First author')"
183                             />
184                         </li>
185
186                         <li>
187                             <label for="title_embargo_info"
188                                 >{{ $__("Embargo information") }}:</label
189                             >
190                             <input
191                                 id="title_embargo_info"
192                                 v-model="title.embargo_info"
193                                 :placeholder="$__('Embargo information')"
194                             />
195                         </li>
196
197                         <li>
198                             <label for="title_coverage_depth"
199                                 >{{ $__("Coverage depth") }}:</label
200                             >
201                             <input
202                                 id="title_coverage_depth"
203                                 v-model="title.coverage_depth"
204                                 :placeholder="$__('Coverage depth')"
205                             />
206                         </li>
207
208                         <li>
209                             <label for="title_notes">{{ $__("Notes") }}:</label>
210                             <input
211                                 id="title_notes"
212                                 v-model="title.notes"
213                                 :placeholder="$__('Notes')"
214                             />
215                         </li>
216
217                         <li>
218                             <label for="title_publisher_name"
219                                 >{{ $__("Publisher name") }}:</label
220                             >
221                             <input
222                                 id="title_publisher_name"
223                                 v-model="title.publisher_name"
224                                 :placeholder="$__('Publisher name')"
225                             />
226                         </li>
227
228                         <li>
229                             <label for="title_publication_type"
230                                 >{{ $__("Publication type") }}:</label
231                             >
232                             <v-select
233                                 id="title_publication_type"
234                                 v-model="title.publication_type"
235                                 label="description"
236                                 :reduce="av => av.value"
237                                 :options="av_title_publication_types"
238                             />
239                         </li>
240
241                         <li>
242                             <label for="title_date_monograph_published_print"
243                                 >{{
244                                     $__(
245                                         "Date the monograph is first published in print"
246                                     )
247                                 }}:</label
248                             >
249                             <input
250                                 id="title_date_monograph_published_print"
251                                 v-model="title.date_monograph_published_print"
252                                 :placeholder="
253                                     $__(
254                                         'Date the monograph is first published in print'
255                                     )
256                                 "
257                             />
258                         </li>
259
260                         <li>
261                             <label for="title_date_monograph_published_online"
262                                 >{{
263                                     $__(
264                                         "Date the monograph is first published online"
265                                     )
266                                 }}:</label
267                             >
268                             <input
269                                 id="title_date_monograph_published_online"
270                                 v-model="title.date_monograph_published_online"
271                                 :placeholder="
272                                     $__(
273                                         'Date the monograph is first published online'
274                                     )
275                                 "
276                             />
277                         </li>
278
279                         <li>
280                             <label for="title_monograph_volume"
281                                 >{{
282                                     $__("Number of volume for monograph")
283                                 }}:</label
284                             >
285                             <input
286                                 id="title_monograph_volume"
287                                 v-model="title.monograph_volume"
288                                 :placeholder="
289                                     $__('Number of volume for monograph')
290                                 "
291                             />
292                         </li>
293
294                         <li>
295                             <label for="title_monograph_edition"
296                                 >{{ $__("Edition of the monograph") }}:</label
297                             >
298                             <input
299                                 id="title_monograph_edition"
300                                 v-model="title.monograph_edition"
301                                 :placeholder="$__('Edition of the monograph')"
302                             />
303                         </li>
304
305                         <li>
306                             <label for="title_first_editor"
307                                 >{{ $__("First editor") }}:</label
308                             >
309                             <input
310                                 id="title_first_editor"
311                                 v-model="title.first_editor"
312                                 :placeholder="$__('First editor')"
313                             />
314                         </li>
315
316                         <li>
317                             <label for="title_parent_publication_title_id"
318                                 >{{
319                                     $__(
320                                         "Title identifier of the parent publication"
321                                     )
322                                 }}:</label
323                             >
324                             <input
325                                 id="title_parent_publication_title_id"
326                                 v-model="title.parent_publication_title_id"
327                                 :placeholder="
328                                     $__(
329                                         'Title identifier of the parent publication'
330                                     )
331                                 "
332                             />
333                         </li>
334
335                         <li>
336                             <label for="title_preceding_publication_title_id"
337                                 >{{
338                                     $__(
339                                         "Title identifier of any preceding publication title"
340                                     )
341                                 }}:</label
342                             >
343                             <input
344                                 id="title_preceding_publication_title_id"
345                                 v-model="title.preceding_publication_title_id"
346                                 :placeholder="
347                                     $__(
348                                         'Title identifier of any preceding publication title'
349                                     )
350                                 "
351                             />
352                         </li>
353
354                         <li>
355                             <label for="title_access_type"
356                                 >{{
357                                     // FIXME May be fee-based (P) or Open Access (F).
358                                     $__("Access type")
359                                 }}:</label
360                             >
361                             <input
362                                 id="title_access_type"
363                                 v-model="title.access_type"
364                                 :placeholder="$__('Access type')"
365                             />
366                         </li>
367                     </ol>
368                 </fieldset>
369                 <EHoldingsTitlesFormAddResources :resources="title.resources" />
370                 <fieldset class="action">
371                     <input type="submit" value="Submit" />
372                     <router-link
373                         :to="{ name: 'EHoldingsLocalTitlesList' }"
374                         role="button"
375                         class="cancel"
376                         >{{ $__("Cancel") }}</router-link
377                     >
378                 </fieldset>
379             </form>
380         </div>
381     </div>
382 </template>
383
384 <script>
385 import { inject } from "vue"
386 import EHoldingsTitlesFormAddResources from "./EHoldingsLocalTitlesFormAddResources.vue"
387 import { setMessage, setError, setWarning } from "../../messages"
388 import { APIClient } from "../../fetch/api-client.js"
389 import { storeToRefs } from "pinia"
390
391 export default {
392     setup() {
393         const vendorStore = inject("vendorStore")
394         const { vendors } = storeToRefs(vendorStore)
395
396         const AVStore = inject("AVStore")
397         const { av_title_publication_types } = storeToRefs(AVStore)
398         const { get_lib_from_av } = AVStore
399
400         return {
401             vendors,
402             av_title_publication_types,
403             get_lib_from_av,
404         }
405     },
406     data() {
407         return {
408             title: {
409                 title_id: null,
410                 publication_title: "",
411                 external_id: "",
412                 print_identifier: "",
413                 online_identifier: "",
414                 date_first_issue_online: "",
415                 num_first_vol_online: "",
416                 num_first_issue_online: "",
417                 date_last_issue_online: "",
418                 num_last_vol_online: "",
419                 num_last_issue_online: "",
420                 title_url: "",
421                 first_author: "",
422                 embargo_info: "",
423                 coverage_depth: "",
424                 notes: "",
425                 publisher_name: "",
426                 publication_type: "",
427                 date_monograph_published_print: "",
428                 date_monograph_published_online: "",
429                 monograph_volume: "",
430                 monograph_edition: "",
431                 first_editor: "",
432                 parent_publication_title_id: "",
433                 preceding_publication_title_id: "",
434                 access_type: "",
435                 resources: [],
436             },
437             initialized: false,
438         }
439     },
440     beforeRouteEnter(to, from, next) {
441         next(vm => {
442             if (to.params.title_id) {
443                 vm.title = vm.getTitle(to.params.title_id)
444             } else {
445                 vm.initialized = true
446             }
447         })
448     },
449     methods: {
450         getTitle(title_id) {
451             const client = APIClient.erm
452             client.localTitles.get(title_id).then(
453                 title => {
454                     this.title = title
455                     this.initialized = true
456                 },
457                 error => {}
458             )
459         },
460         checkForm(title) {
461             let errors = []
462
463             let resources = title.resources
464             const package_ids = resources.map(al => al.package_id)
465             const duplicate_package_ids = package_ids.filter(
466                 (id, i) => package_ids.indexOf(id) !== i
467             )
468
469             if (duplicate_package_ids.length) {
470                 errors.push(this.$__("A package is used several times"))
471             }
472
473             errors.forEach(function (e) {
474                 setWarning(e)
475             })
476             return !errors.length
477         },
478         onSubmit(e) {
479             e.preventDefault()
480
481             let title = JSON.parse(JSON.stringify(this.title)) // copy
482
483             if (!this.checkForm(title)) {
484                 return false
485             }
486
487             let title_id = title.title_id
488             delete title.title_id
489             delete title.biblio_id
490
491             // Cannot use the map/keepAttrs because of the reserved keywork 'package'
492             title.resources.forEach(function (e) {
493                 delete e.package
494                 delete e.resource_id
495             })
496
497             const client = APIClient.erm
498             if (title_id) {
499                 client.localTitles.update(title, title_id).then(
500                     success => {
501                         setMessage(this.$__("Title updated"))
502                         this.$router.push({
503                             name: "EHoldingsLocalTitlesList",
504                         })
505                     },
506                     error => {}
507                 )
508             } else {
509                 client.localTitles.create(title).then(
510                     success => {
511                         setMessage(this.$__("Title created"))
512                         this.$router.push({
513                             name: "EHoldingsLocalTitlesList",
514                         })
515                     },
516                     error => {}
517                 )
518             }
519         },
520     },
521     components: { EHoldingsTitlesFormAddResources },
522     name: "EHoldingsLocalTitlesFormAdd",
523 }
524 </script>
525 <style scoped>
526 fieldset.rows label {
527     width: 25rem;
528 }
529 </style>