Patron reg field validation (WIP)
authorBill Erickson <berickxx@gmail.com>
Sun, 6 Mar 2016 16:35:31 +0000 (11:35 -0500)
committerBill Erickson <berickxx@gmail.com>
Sun, 6 Mar 2016 16:35:31 +0000 (11:35 -0500)
Signed-off-by: Bill Erickson <berickxx@gmail.com>
Open-ILS/src/templates/staff/circ/patron/t_edit.tt2
Open-ILS/web/js/ui/default/staff/circ/patron/regctl.js

index 5dc691a..acde40d 100644 (file)
@@ -87,7 +87,7 @@
 [% 
 # draws a vanilla form input field for inputs that require no 
 # special additions.
-MACRO draw_form_input(cls, field, path, type, required) BLOCK;
+MACRO draw_form_input(cls, field, path, type, disable) BLOCK;
   IF !type; type = 'text'; END;
   base_obj = path ? 'patron.' _ path : 'patron';
   model = base_obj _ '.' _ field;
@@ -101,6 +101,7 @@ MACRO draw_form_input(cls, field, path, type, required) BLOCK;
       ng-required="field_required('[% cls %]', '[% field %]')"
       ng-blur="handle_field_changed([% base_obj %], '[% field %]')"
       ng-pattern="field_pattern('[% cls %]', '[% field %]')"
+      [% IF disable %]ng-disabled="[% disable %]"[% END %]
       ng-model="[% model %]"/>
   </div>
 [% END %]
@@ -416,7 +417,7 @@ within the "form" by name for validation.
     </div>
   </div>
   <div class="col-md-3">
-    <button class="btn btn-default" ng-disabled="!has_group_link_perm"
+    <button class="btn btn-default" ng-disabled="!perms.CREATE_USER_GROUP_LINK"
       ng-click="secondary_groups_dialog()">[% l('Secondary Groups') %]</button>
   </div> 
 </div>
@@ -474,7 +475,8 @@ within the "form" by name for validation.
 
 <div class="row reg-field-row" ng-show="show_field('au.claims_returned_count')">
   [% draw_field_label('au', 'claims_returned_count') %]
-  [% draw_form_input('au', 'claims_returned_count', '', 'number') %]
+  [% draw_form_input('au', 'claims_returned_count', 
+    '', 'number', '!perms.UPDATE_PATRON_CLAIM_RETURN_COUNT') %]
   <div class="col-md-6 patron-reg-example">
     [% draw_example_text('au', 'claims_returned_count') %]
   </div>
@@ -484,7 +486,8 @@ within the "form" by name for validation.
 
 <div class="row reg-field-row" ng-show="show_field('au.claims_never_checked_out_count')">
   [% draw_field_label('au', 'claims_never_checked_out_count') %]
-  [% draw_form_input('au', 'claims_never_checked_out_count', '', 'number') %]
+  [% draw_form_input('au', 'claims_never_checked_out_count',
+    '', 'number', '!perms.UPDATE_PATRON_CLAIM_NEVER_CHECKED_OUT_COUNT') %]
   <div class="col-md-6 patron-reg-example">
     [% draw_example_text('au', 'claims_never_checked_out_count') %]
   </div>
@@ -547,6 +550,23 @@ within the "form" by name for validation.
   </div>
 </div>
 
+<!-- TODO: Add circ.collections.exempt to master SQL seed data -->
+<div class="row reg-field-row" 
+  ng-if="user_setting_types['circ.collections.exempt']">
+  <div class="col-md-3 reg-field-label">
+    <label>{{user_setting_types['circ.collections.exempt'].label()}}</label>
+  </div>
+  <div class="col-md-3 reg-field-input">
+    <div class='checkbox'>
+      <input 
+        type='checkbox' 
+        ng-change="field_modified()" 
+        ng-disabled="!perms.UPDATE_PATRON_COLLECTIONS_EXEMPT"
+        ng-model="user_settings['circ.collections.exempt']"/>
+    </div>
+  </div>
+</div>
+
 <div class="row reg-field-row">
   <div class="col-md-3 reg-field-label">
     <label>[% l('Holds Notices') %]</label>
index bd56ecf..d0baa68 100644 (file)
@@ -155,14 +155,29 @@ angular.module('egCoreMod')
         );
     }
 
-    service.has_group_link_perms = function(org_id) {
-        return egCore.perm.hasPermAt('CREATE_USER_GROUP_LINK', true)
-        .then(function(p) { return p.indexOf(org_id) > -1; });
-    }
+    // resolves to a hash of perm-name => boolean value indicating
+    // wether the user has the permission at org_id.
+    service.has_perms_for_org = function(org_id) {
+
+        var perms_needed = [
+            'UPDATE_USER',
+            'CREATE_USER',
+            'CREATE_USER_GROUP_LINK', 
+            'UPDATE_PATRON_COLLECTIONS_EXEMPT',
+            'UPDATE_PATRON_CLAIM_RETURN_COUNT',
+            'UPDATE_PATRON_CLAIM_NEVER_CHECKED_OUT_COUNT'
+        ];
+
+        return egCore.perm.hasPermAt(perms_needed, true)
+        .then(function(perm_map) {
+
+            angular.forEach(perms_needed, function(perm) {
+                perm_map[perm] = 
+                    Boolean(perm_map[perm].indexOf(org_id) > -1);
+            });
 
-    service.has_user_edit_perms = function(org_id) {
-        return egCore.perm.hasPermAt('CREATE_USER', true)
-        .then(function(p) { return p.indexOf(org_id) > -1; });
+            return perm_map;
+        });
     }
 
     service.get_surveys = function() {
@@ -995,8 +1010,6 @@ angular.module('egCoreMod')
             var cls = parts[1];
             var name = parts[2];
             patterns[cls][name] = new RegExp(val);
-            console.log('adding field pattern for ' + 
-              name + ' : ' + patterns[cls][name]);
         });
     }
 
@@ -1019,6 +1032,10 @@ function PatronRegCtrl($scope, $routeParams,
     $scope.focus_bc = !Boolean($scope.patron_id);
     $scope.dupe_counts = {};
 
+    // map of perm name to true/false for perms the logged in user
+    // has at the currently selected patron home org unit.
+    $scope.perms = {};
+
     if (!$scope.edit_passthru) {
         // in edit more, scope.edit_passthru is delivered to us by
         // the enclosing controller.  In register mode, there is 
@@ -1483,15 +1500,11 @@ function PatronRegCtrl($scope, $routeParams,
         });
     }
 
-    // TODO: check edit perms
     $scope.handle_home_org_changed = function() {
         org_id = $scope.patron.home_ou.id();
-
-        patronRegSvc.has_group_link_perms(org_id)
-        .then(function(bool) {$scope.has_group_link_perm = bool});
-
-        patronRegSvc.has_user_edit_perms(org_id)
-        .then(function(bool) {$scope.has_user_edit_perm = bool});
+        patronRegSvc.has_perms_for_org(org_id).then(function(map) {
+            angular.forEach(map, function(v, k) { $scope.perms[k] = v });
+        });
     }
 
     // This is called with every character typed in a form field,
@@ -1567,10 +1580,6 @@ function PatronRegCtrl($scope, $routeParams,
                 maintain_juvenile_flag();
                 break;
         }
-
-        // Not every form field has an on-change handler. 
-        // Fire it here to cover those.
-        $scope.field_modified();
     }
 
     // patron.juvenile is set to true if the user was born after
@@ -1618,12 +1627,16 @@ function PatronRegCtrl($scope, $routeParams,
 
     // Returns true if any input elements are tagged as invalid
     $scope.edit_passthru.has_invalid_fields = function() {
-        return $('#patron-reg-container.ng-invalid').length > 0;
+        return $('#patron-reg-container .ng-invalid').length > 0;
     }
 
     $scope.edit_passthru.hide_save_actions = function() {
+
+        var can_save = $scope.patron.isnew ?
+            $scope.perms.CREATE_USER : $scope.perms.UPDATE_USER;
+
         return (
-            !$scope.has_user_edit_perm ||
+            !can_save ||
             $scope.edit_passthru.has_invalid_fields()
         );
     }