LP#1904244: fm-editor: add a linkedSearchConditions field option
authorGalen Charlton <gmc@equinoxOLI.org>
Tue, 20 Apr 2021 21:57:08 +0000 (17:57 -0400)
committerBill Erickson <berickxx@gmail.com>
Thu, 12 Aug 2021 19:29:26 +0000 (15:29 -0400)
This option allows the caller to specify conditions to add
when retrieving values for a linked field, e.g., to specify
that only active values should be fetched.

Example usage:

[fieldOptions]="{fund:{linkedSearchConditions:{'active':'t'}}}"

If the underlying record has a value set for the linked field, that
value will be included when preloadLinkedValues and the value
doesn't otherwise meet the search conditions.

This patch also sorts the values if preloadLinkedValues is used.

Signed-off-by: Galen Charlton <gmc@equinoxOLI.org>
Signed-off-by: Ruth Frasur <rfrasur@library.in.gov>
Signed-off-by: Bill Erickson <berickxx@gmail.com>
Open-ILS/src/eg2/src/app/share/fm-editor/fm-editor.component.ts
Open-ILS/src/eg2/src/app/staff/sandbox/sandbox.component.html

index 9d4ae66..fd59734 100644 (file)
@@ -56,6 +56,10 @@ export interface FmFieldOptions {
     // so the user can click or type to find values.
     preloadLinkedValues?: boolean;
 
+    // Additional search conditions to include when constructing
+    // the query for a linked field's combobox
+    linkedSearchConditions?: {[field: string]: string};
+
     // Directly override the required state of the field.
     // This only has an affect if the value is true.
     isRequired?: boolean;
@@ -584,7 +588,35 @@ export class FmRecordEditorComponent
         }
 
         if (fieldOptions.preloadLinkedValues || !selector) {
-            return this.pcrud.retrieveAll(field.class, {}, {atomic : true})
+            const search = {};
+            const orderBy = {order_by: {}};
+            if (selector) {
+                orderBy.order_by[field.class] = selector;
+            }
+            const idField = this.idl.classes[field.class].pkey || 'id';
+            search[idField] = {'!=' : null};
+            if (fieldOptions.linkedSearchConditions) {
+                const conditions = {};
+                Object.keys(fieldOptions.linkedSearchConditions).forEach(key => {
+                    conditions[key] = fieldOptions.linkedSearchConditions[key];
+                });
+                // ensure that the current value, if present, is included
+                // in case it doesn't otherwise meet the conditions
+                const linkedValue = this.record[field.name]();
+                if (linkedValue !== null && linkedValue !== undefined) {
+                    search['-or'] = [];
+                    const retrieveRec = {};
+                    retrieveRec[idField] = linkedValue;
+                    search['-or'].push(retrieveRec);
+                    search['-or'].push(conditions);
+                } else {
+                    // just tack on the conditions
+                    Object.keys(conditions).forEach(key => {
+                        search[key] = conditions[key];
+                    });
+                }
+            }
+            return this.pcrud.search(field.class, search, orderBy, {atomic : true})
             .toPromise().then(list => {
                 field.linkedValues =
                     this.flattenLinkedValues(field, list);
@@ -600,6 +632,11 @@ export class FmRecordEditorComponent
             const idField = this.idl.classes[field.class].pkey || 'id';
 
             search[selector] = {'ilike': `%${term}%`};
+            if (fieldOptions.linkedSearchConditions) {
+                Object.keys(fieldOptions.linkedSearchConditions).forEach(key => {
+                    search[key] = fieldOptions.linkedSearchConditions[key];
+                });
+            }
             orderBy.order_by[field.class] = selector;
 
             return this.pcrud.search(field.class, search, orderBy)
index 647cb0c..a693944 100644 (file)
   <eg-grid-column [sortable]="true" path="create_date"></eg-grid-column>
 </eg-grid>
 
-<eg-fm-record-editor #acpEditDialog idlClass="acp" hiddenFields="call_number,creator,create_date,editor,edit_time,loan_duration,fine_level,dummy_author,dummy_isbn,ref,floating,holdable,circ_as_type,active_date,mint_condition,cost,deleted,deposit,deposit_amount,circulate,status_changed_time,copy_number">
+<eg-fm-record-editor #acpEditDialog idlClass="acp" hiddenFields="call_number,creator,create_date,editor,edit_time,loan_duration,fine_level,dummy_author,dummy_isbn,ref,floating,holdable,circ_as_type,active_date,mint_condition,cost,deleted,deposit,deposit_amount,circulate,status_changed_time,copy_number"
+   [fieldOptions]="{status:{linkedSearchConditions:{'is_available':'t'},preloadLinkedValues:true}}">
 </eg-fm-record-editor>
 <eg-string #successString text="Updated succeeded!" i18n-text></eg-string>
 <eg-string #updateFailedString text="Updated failed!" i18n-text></eg-string>