]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/admin/local/course-reserves/course-list.component.ts
LP#1917809 Create Course: Owning Library Default
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / admin / local / course-reserves / course-list.component.ts
1 import {Component, Input, ViewChild, OnInit, AfterViewInit} from '@angular/core';
2 import {Router} from '@angular/router';
3 import {IdlObject, IdlService} from '@eg/core/idl.service';
4 import {PcrudService} from '@eg/core/pcrud.service';
5 import {CourseService} from '@eg/staff/share/course.service';
6 import {GridComponent} from '@eg/share/grid/grid.component';
7 import {Pager} from '@eg/share/util/pager';
8 import {GridDataSource, GridColumn} from '@eg/share/grid/grid';
9 import {FmRecordEditorComponent} from '@eg/share/fm-editor/fm-editor.component';
10 import {StringComponent} from '@eg/share/string/string.component';
11 import {ToastService} from '@eg/share/toast/toast.service';
12 import {LocaleService} from '@eg/core/locale.service';
13 import {AuthService} from '@eg/core/auth.service';
14 import {OrgService} from '@eg/core/org.service';
15 import {OrgFamily} from '@eg/share/org-family-select/org-family-select.component';
16
17 import {CourseAssociateMaterialComponent
18     } from './course-associate-material.component';
19
20 import {CourseAssociateUsersComponent
21     } from './course-associate-users.component';
22
23 @Component({
24     templateUrl: './course-list.component.html'
25 })
26
27 export class CourseListComponent implements OnInit, AfterViewInit {
28
29     @ViewChild('editDialog', { static: true }) editDialog: FmRecordEditorComponent;
30     @ViewChild('grid') grid: GridComponent;
31     @ViewChild('successString', { static: true }) successString: StringComponent;
32     @ViewChild('createString') createString: StringComponent;
33     @ViewChild('createErrString') createErrString: StringComponent;
34     @ViewChild('updateFailedString') updateFailedString: StringComponent;
35     @ViewChild('deleteFailedString', { static: true }) deleteFailedString: StringComponent;
36     @ViewChild('deleteSuccessString', { static: true }) deleteSuccessString: StringComponent;
37     @ViewChild('archiveFailedString', { static: true }) archiveFailedString: StringComponent;
38     @ViewChild('archiveSuccessString', { static: true }) archiveSuccessString: StringComponent;
39     @ViewChild('courseMaterialDialog', {static: true})
40         private courseMaterialDialog: CourseAssociateMaterialComponent;
41     @ViewChild('courseUserDialog', {static: true})
42         private courseUserDialog: CourseAssociateUsersComponent;
43
44     @Input() sortField: string;
45     @Input() idlClass = 'acmc';
46     @Input() dialog_size: 'sm' | 'lg' = 'lg';
47     @Input() tableName = 'Course';
48     grid_source: GridDataSource = new GridDataSource();
49     currentMaterials: any[] = [];
50     search_value = '';
51     defaultOuId: number;
52     searchOrgs: OrgFamily;
53     defaultTerm: IdlObject;
54
55
56     constructor(
57         private courseSvc: CourseService,
58         private locale: LocaleService,
59         private auth: AuthService,
60         private idl: IdlService,
61         private org: OrgService,
62         private pcrud: PcrudService,
63         private router: Router,
64         private toast: ToastService
65     ) {}
66
67     ngOnInit() {
68         this.getSource();
69         this.defaultTerm = this.idl.create('acmt');
70         this.defaultOuId = this.auth.user().ws_ou() || this.org.root().id();
71         this.defaultTerm.owning_lib(this.defaultOuId);
72         this.searchOrgs = {primaryOrgId: this.defaultOuId};
73     }
74
75     ngAfterViewInit() {
76         this.grid.onRowActivate.subscribe((course: IdlObject) => {
77             const idToEdit = course.id();
78             this.navigateToCoursePage(idToEdit);
79         });
80
81     }
82
83     acmtcmQueryParams (row: any): {gridFilters: string} {
84         return {gridFilters: '{"course":' + row.id() + '}'};
85     }
86
87
88     /**
89      * Gets the data, specified by the class, that is available.
90      */
91     getSource() {
92         this.grid_source.getRows = (pager: Pager, sort: any[]) => {
93             const orderBy: any = {};
94             if (sort.length) {
95                 // Sort specified from grid
96                 orderBy[this.idlClass] = sort[0].name + ' ' + sort[0].dir;
97             } else if (this.sortField) {
98                 // Default sort field
99                 orderBy[this.idlClass] = this.sortField;
100             }
101             const search: any = new Array();
102             const orgFilter: any = {};
103             orgFilter['owning_lib'] =
104                 this.searchOrgs.orgIds || [this.defaultOuId];
105             search.push(orgFilter);
106             const searchOps = {
107                 offset: pager.offset,
108                 limit: pager.limit,
109                 order_by: orderBy
110             };
111             return this.pcrud.search(this.idlClass, search, searchOps, {fleshSelectors: true});
112         };
113     }
114
115     navigateToCoursePage(id_arr: IdlObject[]) {
116         if (typeof id_arr === 'number') { id_arr = [id_arr]; }
117         const urls = [];
118         id_arr.forEach(id => {console.log(this.router.url);
119             urls.push([this.locale.currentLocaleCode() + this.router.url + '/' +  id]);
120         });
121         if (id_arr.length === 1) {
122         this.router.navigate([this.router.url + '/' + id_arr[0]]);
123         } else {
124             urls.forEach(url => {
125                 window.open(url);
126             });
127         }
128     }
129
130     createNew() {
131         this.editDialog.mode = 'create';
132         const course_module_course = this.idl.create('acmc');
133         course_module_course.owning_lib(this.auth.user().ws_ou());
134         this.editDialog.recordId = null;
135         this.editDialog.record = course_module_course;
136         this.editDialog.open({size: this.dialog_size}).subscribe(
137             ok => {
138                 this.createString.current()
139                     .then(str => this.toast.success(str));
140                 this.grid.reload();
141             },
142             rejection => {
143                 if (!rejection.dismissed) {
144                     this.createErrString.current()
145                         .then(str => this.toast.danger(str));
146                 }
147             }
148         );
149     }
150
151     editSelected(fields: IdlObject[]) {
152         // Edit each IDL thing one at a time
153         const course_ids = [];
154         fields.forEach(field => {
155             if (typeof field['id'] === 'function') {
156                 course_ids.push(field.id());
157             } else {
158                 course_ids.push(field['id']);
159             }
160         });
161         this.navigateToCoursePage(course_ids);
162     }
163
164     archiveSelected(course: IdlObject[]) {
165         this.courseSvc.disassociateMaterials(course).then(res => {
166             course.forEach(courseToArchive => {
167                 courseToArchive.is_archived(true);
168             });
169             this.pcrud.update(course).subscribe(
170                 val => {
171                     console.debug('archived: ' + val);
172                     this.archiveSuccessString.current()
173                         .then(str => this.toast.success(str));
174                 }, err => {
175                     this.archiveFailedString.current()
176                         .then(str => this.toast.danger(str));
177                 }, () => {
178                     this.grid.reload();
179                 }
180             );
181         });
182     }
183
184     deleteSelected(idlObject: IdlObject[]) {
185         this.courseSvc.disassociateMaterials(idlObject).then(res => {
186             idlObject.forEach(object => {
187                 object.isdeleted(true);
188             });
189             this.pcrud.autoApply(idlObject).subscribe(
190                 val => {
191                     console.debug('deleted: ' + val);
192                     this.deleteSuccessString.current()
193                         .then(str => this.toast.success(str));
194                 },
195                 err => {
196                     this.deleteFailedString.current()
197                         .then(str => this.toast.danger(str));
198                 },
199                 () => this.grid.reload()
200             );
201         });
202     }
203 }
204