Lines 21-27
use strict;
Link Here
|
21 |
use warnings; |
21 |
use warnings; |
22 |
use C4::Context; |
22 |
use C4::Context; |
23 |
|
23 |
|
24 |
|
24 |
use Koha::Validation; |
25 |
|
25 |
|
26 |
=head1 NAME |
26 |
=head1 NAME |
27 |
|
27 |
|
Lines 253-258
sub SetMessagingPreferencesFromDefaults {
Link Here
|
253 |
$default_pref->{borrowernumber} = $params->{borrowernumber}; |
253 |
$default_pref->{borrowernumber} = $params->{borrowernumber}; |
254 |
SetMessagingPreference( $default_pref ); |
254 |
SetMessagingPreference( $default_pref ); |
255 |
} |
255 |
} |
|
|
256 |
# Finally, delete all misconfigured preferences |
257 |
DeleteAllMisconfiguredPreferences($params->{borrowernumber}); |
258 |
} |
259 |
|
260 |
=head2 DeleteAllMisconfiguredPreferences |
261 |
|
262 |
C4::Members::Messaging::DeleteAllMisconfiguredPreferences( [ $borrowernumber ] ); |
263 |
|
264 |
Deletes all misconfigured preferences for ALL borrowers that have invalid contact information for |
265 |
the transport types. Given a borrowernumber, deletes misconfigured preferences only for this borrower. |
266 |
|
267 |
return: returns array of arrayrefs to the deleted preferences (borrower_message_preference_id and message_transport_type) |
268 |
|
269 |
=cut |
270 |
|
271 |
sub DeleteAllMisconfiguredPreferences { |
272 |
my $borrowernumber = shift; |
273 |
|
274 |
my @deleted_prefs; |
275 |
|
276 |
push(@deleted_prefs, DeleteMisconfiguredPreference("email", "email", "email", |
277 |
$borrowernumber)); |
278 |
push(@deleted_prefs, DeleteMisconfiguredPreference("phone", "phone", "phone", |
279 |
$borrowernumber)); |
280 |
push(@deleted_prefs, DeleteMisconfiguredPreference("sms", "smsalertnumber", |
281 |
"phone", $borrowernumber)); |
282 |
|
283 |
return @deleted_prefs; |
284 |
} |
285 |
|
286 |
=head2 DeleteMisconfiguredPreference |
287 |
|
288 |
C4::Members::Messaging::DeleteMisconfiguredPreference( |
289 |
$type, $contact, $validator [, $borrowernumber ] |
290 |
); |
291 |
|
292 |
Takes a messaging preference type and the primary contact method for it, and |
293 |
a string to define the Koha::Validation that should be used to determine whether |
294 |
the preference is misconfigured. |
295 |
|
296 |
A messaging preference is misconfigured when it is linked with invalid contact |
297 |
information. E.g. messaging type email expects the user to have a valid e-mail |
298 |
address in order to work. |
299 |
|
300 |
Deletes misconfigured preferences for ALL borrowers that have invalid contact |
301 |
information for that transport type. Given a borrowernumber, deletes misconfigured |
302 |
preferences only for this borrower. |
303 |
|
304 |
return: returns array of arrayrefs to the deleted preferences |
305 |
(borrower_message_preference_id and message_transport_type) |
306 |
|
307 |
=cut |
308 |
|
309 |
sub DeleteMisconfiguredPreference { |
310 |
my ($type, $contact, $validator, $borrowernumber) = @_; |
311 |
|
312 |
if (not defined $type or not defined $contact or not defined $validator) { |
313 |
return 0; |
314 |
} |
315 |
|
316 |
my @misconfigured_prefs; |
317 |
|
318 |
# Get all messaging preferences and borrower's contact information |
319 |
my $dbh = C4::Context->dbh(); |
320 |
my $query = " |
321 |
|
322 |
SELECT |
323 |
borrower_message_preferences.borrower_message_preference_id, |
324 |
borrower_message_transport_preferences.message_transport_type, |
325 |
borrowers.$contact |
326 |
|
327 |
FROM |
328 |
borrower_message_preferences, |
329 |
borrower_message_transport_preferences, |
330 |
borrowers |
331 |
|
332 |
WHERE |
333 |
borrower_message_preferences.borrower_message_preference_id |
334 |
= |
335 |
borrower_message_transport_preferences.borrower_message_preference_id |
336 |
|
337 |
AND |
338 |
borrowers.borrowernumber = borrower_message_preferences.borrowernumber |
339 |
|
340 |
AND |
341 |
borrower_message_transport_preferences.message_transport_type = ? |
342 |
"; |
343 |
|
344 |
$query .= " AND borrowers.borrowernumber = ?" if defined $borrowernumber; |
345 |
|
346 |
my $sth = $dbh->prepare($query); |
347 |
|
348 |
if (defined $borrowernumber){ |
349 |
$sth->execute($type, $borrowernumber); |
350 |
} else { |
351 |
$sth->execute($type); |
352 |
} |
353 |
|
354 |
while (my $ref = $sth->fetchrow_arrayref) { |
355 |
if ($$ref[1] eq $type) { |
356 |
if (not defined $$ref[2] or defined $$ref[2] and $$ref[2] eq "") { |
357 |
my $valid_contact = 0; # delete prefs if empty contact |
358 |
unless ($valid_contact && $$ref[2]) { |
359 |
# push the misconfigured preferences into an array |
360 |
push(@misconfigured_prefs, $$ref[0]); |
361 |
} |
362 |
} |
363 |
else { |
364 |
my ($valid_contact, $err, $err_msg) = |
365 |
Koha::Validation->$validator($$ref[2]); |
366 |
unless ($valid_contact && $$ref[2]) { |
367 |
# push the misconfigured preferences into an array |
368 |
push(@misconfigured_prefs, $$ref[0]); |
369 |
} |
370 |
} |
371 |
} |
372 |
} |
373 |
|
374 |
$sth = $dbh->prepare(" |
375 |
|
376 |
DELETE FROM |
377 |
borrower_message_transport_preferences |
378 |
|
379 |
WHERE |
380 |
borrower_message_preference_id = ? AND message_transport_type = ? |
381 |
"); |
382 |
|
383 |
my @deleted_prefs; |
384 |
foreach my $id (@misconfigured_prefs){ |
385 |
# delete the misconfigured pref |
386 |
$sth->execute($id, $type); |
387 |
# push it into array that we will return |
388 |
push (@deleted_prefs, [$id,$type]); |
389 |
} |
390 |
|
391 |
return @deleted_prefs; |
256 |
} |
392 |
} |
257 |
|
393 |
|
258 |
=head1 TABLES |
394 |
=head1 TABLES |