]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/admin/local/course-reserves/course-page.component.ts
37654963e7d81da3f16a856a6768b4d067b66d97
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / admin / local / course-reserves / course-page.component.ts
1 import {Component, Input, ViewChild, OnInit, TemplateRef} from '@angular/core';
2 import {Router, ActivatedRoute} from '@angular/router';
3 import {Observable, Observer, of} from 'rxjs';
4 import {DialogComponent} from '@eg/share/dialog/dialog.component';
5 import {AuthService} from '@eg/core/auth.service';
6 import {NetService} from '@eg/core/net.service';
7 import {EventService} from '@eg/core/event.service';
8 import {OrgService} from '@eg/core/org.service';
9 import {PcrudService} from '@eg/core/pcrud.service';
10 import {Pager} from '@eg/share/util/pager';
11 import {NgbModal, NgbModalOptions} from '@ng-bootstrap/ng-bootstrap';
12 import {GridDataSource} from '@eg/share/grid/grid';
13 import {GridComponent} from '@eg/share/grid/grid.component';
14 import {IdlObject, IdlService} from '@eg/core/idl.service';
15 import {StringComponent} from '@eg/share/string/string.component';
16 import {StaffBannerComponent} from '@eg/staff/share/staff-banner.component';
17 import {ToastService} from '@eg/share/toast/toast.service';
18 import {CourseService} from '@eg/staff/share/course.service';
19
20 @Component({
21     selector: 'eg-course-page',
22     templateUrl: './course-page.component.html'
23 })
24
25 export class CoursePageComponent implements OnInit {
26
27     currentCourse: IdlObject;
28     courseId: any;
29     
30     // Edit Tab
31     @ViewChild('archiveFailedString', { static: true })
32         archiveFailedString: StringComponent;
33     @ViewChild('archiveSuccessString', { static: true })
34         archiveSuccessString: StringComponent;
35
36     // Materials Tab
37     materials: any[] = [];
38     @ViewChild('materialsGrid', {static: true}) materialsGrid: GridComponent;
39     @ViewChild('materialDeleteFailedString', { static: true })
40         materialDeleteFailedString: StringComponent;
41     @ViewChild('materialDeleteSuccessString', { static: true })
42         materialDeleteSuccessString: StringComponent;
43     @ViewChild('materialAddSuccessString', { static: true })
44         materialAddSuccessString: StringComponent;
45     @ViewChild('materialAddFailedString', { static: true })
46         materialAddFailedString: StringComponent;
47     @ViewChild('materialAddDifferentLibraryString', { static: true })
48         materialAddDifferentLibraryString: StringComponent;
49     materialsDataSource: GridDataSource;
50     @Input() barcodeInput: String;
51     @Input() relationshipInput: String;
52     @Input() tempCallNumber: String;
53     @Input() tempStatus: Number;
54     @Input() tempLocation: Number;
55     @Input() tempCircMod: String;
56     @Input() isModifyingStatus: Boolean;
57     @Input() isModifyingCircMod: Boolean;
58     @Input() isModifyingCallNumber: Boolean;
59     @Input() isModifyingLocation: Boolean;
60
61     // Users Tab
62     @Input() userBarcode: String;
63     @Input() userRoleInput: String;
64     constructor(
65         private auth: AuthService,
66         private course: CourseService,
67         private event: EventService,
68         private idl: IdlService,
69         private org: OrgService,
70         private pcrud: PcrudService,
71         private route: ActivatedRoute,
72         private toast: ToastService
73     ) {
74         this.materialsDataSource = new GridDataSource();
75     }
76
77     ngOnInit() {
78         this.courseId = parseInt(this.route.snapshot.paramMap.get('id'));
79         this.course.getCourses([this.courseId]).then(course => {
80             this.currentCourse = course[0];
81         });
82         this.materialsDataSource.getRows = (pager: Pager, sort: any[]) => {
83             return this.loadMaterialsGrid(pager);
84         }
85     }
86
87     // Edit Tab
88     archiveCourse() {
89         this.course.disassociateMaterials([this.currentCourse]).then(res => {
90             this.currentCourse.is_archived('t');
91             this.pcrud.update(this.currentCourse).subscribe(val => {
92                 console.debug('archived: ' + val);
93                 this.archiveSuccessString.current()
94                     .then(str => this.toast.success(str));
95             }, err => {
96                 this.archiveFailedString.current()
97                     .then(str => this.toast.danger(str));
98             });
99         });
100     }
101
102     // Materials Tab
103     loadMaterialsGrid(pager: Pager): Observable<any> {
104         return new Observable<any>(observer => {
105             this.course.getMaterials(this.courseId).then(materials => {
106                 materials.forEach(material => {
107                     this.course.fleshMaterial(material.item(), material.relationship()).then(fleshed_material => {
108                         this.materialsDataSource.data.push(fleshed_material);
109                     });
110                 });
111             });
112             observer.complete();
113         });
114     }
115     
116     associateItem(barcode, relationship) {
117         if (barcode) {
118             let args = {
119                 barcode: barcode,
120                 relationship: relationship,
121                 isModifyingCallNumber: this.isModifyingCallNumber,
122                 isModifyingCircMod: this.isModifyingCircMod,
123                 isModifyingLocation: this.isModifyingLocation,
124                 isModifyingStatus: this.isModifyingStatus,
125                 tempCircMod: this.tempCircMod,
126                 tempLocation: this.tempLocation,
127                 tempStatus: this.tempStatus,
128                 currentCourse: this.currentCourse
129             }
130             this.barcodeInput = null;
131
132             this.pcrud.search('acp', {barcode: args.barcode}, {
133                 flesh: 3, flesh_fields: {acp: ['call_number']}
134             }).subscribe(item => {
135                 let associatedMaterial = this.course.associateMaterials(item, args);
136                 associatedMaterial.material.then(res => {
137                     item = associatedMaterial.item;
138                     let new_cn = item.call_number().label();
139                     if (this.tempCallNumber) new_cn = this.tempCallNumber;
140                     this.course.updateItem(item, this.currentCourse.owning_lib(),
141                         new_cn, args.isModifyingCallNumber
142                     ).then(resp => {
143                         this.course.fleshMaterial(item.id(), args.relationship).then(fleshed_material => {
144                             this.materialsDataSource.data.push(fleshed_material);
145                         });
146                         if (item.circ_lib() != this.currentCourse.owning_lib()) {
147                             this.materialAddDifferentLibraryString.current()
148                             .then(str => this.toast.warning(str));
149                         } else {
150                             this.materialAddSuccessString.current()
151                             .then(str => this.toast.success(str));
152                         }
153                     });
154                 }, err => {
155                     this.materialAddFailedString.current()
156                     .then(str => this.toast.danger(str));
157                 });
158             });
159         }
160     }
161
162     deleteSelected(items) {
163         let item_ids = [];
164         items.forEach(item => {
165             this.materialsDataSource.data.splice(this.materialsDataSource.data.indexOf(item, 0), 1);
166             item_ids.push(item.id())
167         });
168         this.pcrud.search('acmcm', {course: this.courseId, item: item_ids}).subscribe(material => {
169             material.isdeleted(true);
170             this.pcrud.autoApply(material).subscribe(
171                 val => {
172                     this.course.resetItemFields(material, this.currentCourse.owning_lib());
173                     console.debug('deleted: ' + val);
174                     this.materialDeleteSuccessString.current().then(str => this.toast.success(str));
175                 },
176                 err => {
177                     this.materialDeleteFailedString.current()
178                         .then(str => this.toast.danger(str));
179                 }
180             );
181         });
182     }
183 }