@@ -778,11 +778,69 @@ function changeHoldPickupLocation(User $patron, $recordId, $itemToUpdateId, $new
778
778
return $ result ;
779
779
}
780
780
781
- function updatePatronInfo (User $ patron , $ canUpdateContactInfo , $ fromMasquerade ): array {
782
- return [
781
+ public function updatePatronInfo (User $ patron , $ canUpdateContactInfo , $ fromMasquerade ): array {
782
+ $ authToken = $ this ->getAPIAuthToken ($ patron , true );
783
+ $ userMessages = [
783
784
'success ' => false ,
784
- 'messages ' => ['Cannot update patron information with this ILS. ' ],
785
+ 'messages ' => [],
786
+ ];
787
+
788
+ if (!$ authToken || !$ canUpdateContactInfo ) {
789
+ $ userMessages ['messages ' ][] = 'Your contact information cannot be updated. ' ;
790
+ return $ userMessages ;
791
+ }
792
+ $ propertyName = '' ;
793
+
794
+ if (!isset ($ _REQUEST ['email ' ])) {
795
+ return $ userMessages ;
796
+ }
797
+ $ propertyName = 'email ' ;
798
+ $ propertyValue = $ _REQUEST ['email ' ];
799
+ $ success = $ this ->updatePatronProperty ($ propertyName , $ propertyValue , $ patron ->ils_password , $ authToken );
800
+ $ userMessages ['messages ' ][] = $ success ? 'Your email has been updated. ' : 'Your email could not be updated. Please contact your library ' ;
801
+ $ userMessages ['success ' ] = $ success ;
802
+
803
+ return $ userMessages ;
804
+ }
805
+
806
+ private function updatePatronProperty ($ propertyName , $ propertyValue , $ patronIlsPassword , $ authToken ): bool {
807
+ $ evergreenUrl = $ this ->accountProfile ->patronApiUrl . "/osrf-gateway-v1/ $ propertyName " ;
808
+ $ headers = [
809
+ 'Content-Type: application/x-www-form-urlencoded ' ,
785
810
];
811
+ $ this ->apiCurlWrapper ->addCustomHeaders ($ headers , false );
812
+ /**The order of the request parameters is crucial
813
+ * This Evergreen API only handles unnamed parameters,
814
+ * And so it is only their position within the URL that
815
+ * determines which is which
816
+ */
817
+ $ request = 'service=open-ils.actor ' ;
818
+ $ request .= "&method=open-ils.actor.user. $ propertyName.update " ;
819
+ $ request .= '¶m= ' . json_encode ($ authToken );
820
+ $ request .= '¶m= ' . json_encode ($ propertyValue );
821
+ $ request .= '¶m= ' . json_encode ($ patronIlsPassword );
822
+
823
+ $ apiResponse = $ this ->apiCurlWrapper ->curlPostPage ($ evergreenUrl , $ request );
824
+ ExternalRequestLogEntry::logRequest ('evergreen.updatePatronProperty ' , 'POST ' , $ evergreenUrl , $ this ->apiCurlWrapper ->getHeaders (), $ request , $ this ->apiCurlWrapper ->getResponseCode (), $ apiResponse , []);
825
+
826
+ /** It seems the response sent back by the Evergreen API will have a
827
+ * status code of 200 even upon failure (eg. if the patron password
828
+ * is incorrect.) However, a successful update will always result
829
+ * in the "payload" property being set to 1. Check for this instead.
830
+ */
831
+ $ responseData = json_decode ($ apiResponse , true );
832
+ $ success = isset ($ responseData ['payload ' ]) && $ responseData ['payload ' ][0 ] == 1 ;
833
+ return $ success ;
834
+ }
835
+
836
+
837
+ private function isDuplicateUserName ($ username ):bool {
838
+ global $ aspen_db ;
839
+ $ condition = "ils_username= " . "' " . $ username . "' " ;
840
+ $ query = "SELECT COUNT(ils_username) FROM user WHERE $ condition; " ;
841
+ $ queryObject = $ aspen_db ->query ($ query , PDO ::FETCH_ASSOC );
842
+ $ results = $ queryObject ->fetch ();
843
+ return isset ($ results ["COUNT(ils_username) " ]) && $ results ["COUNT(ils_username) " ];
786
844
}
787
845
788
846
public function hasNativeReadingHistory (): bool {
@@ -1102,6 +1160,28 @@ public function getHolds(User $patron): array {
1102
1160
}
1103
1161
return $ holds ;
1104
1162
}
1163
+
1164
+ public function updateEditableUsername (User $ patron , string $ username ): array {
1165
+ $ authToken = $ this ->getAPIAuthToken ($ patron , true );
1166
+ if ($ this ->isDuplicateUserName ($ username )) {
1167
+ $ userMessages ['message ' ] = 'This username is already in use. Please choose another username. ' ;
1168
+ $ userMessages ['success ' ] = false ;
1169
+ return $ userMessages ;
1170
+ }
1171
+ $ success = $ this ->updatePatronProperty ('username ' , $ username , $ patron ->ils_password , $ authToken );
1172
+ $ userMessages ['message ' ] = $ success ? 'Your username has been updated. ' : 'Your username could not be updated. Please contact your library ' ;
1173
+ $ userMessages ['success ' ] = $ success ;
1174
+ return $ userMessages ;
1175
+ }
1176
+
1177
+ public function getEditableUsername (User $ user ) {
1178
+ $ this ->loadContactInformation ($ user );
1179
+ return $ user ->ils_username ;
1180
+ }
1181
+
1182
+ public function hasEditableUsername () {
1183
+ return true ;
1184
+ }
1105
1185
1106
1186
/**
1107
1187
* @inheritDoc
@@ -2530,6 +2610,12 @@ public function loadContactInformation(User $user) {
2530
2610
if (!empty ($ mappedPatronData ['suffix ' ])) {
2531
2611
$ user ->_fullname .= ' ' . $ mappedPatronData ['suffix ' ];
2532
2612
}
2613
+ if (!empty ($ mappedPatronData ['email ' ])) {
2614
+ $ user ->email = $ mappedPatronData ['email ' ];
2615
+ }
2616
+ if (!empty ($ mappedPatronData ['usrname ' ])) {
2617
+ $ user ->ils_username = $ mappedPatronData ['usrname ' ];
2618
+ }
2533
2619
$ user ->_fullname = trim ($ user ->_fullname );
2534
2620
2535
2621
if (!empty ($ mappedPatronData ['expire_date ' ])) {
0 commit comments