Skip to content

Commit

Permalink
Add door_permission_until field to users
Browse files Browse the repository at this point in the history
This field may only be edited by admins (actually only by
"Schließberechtigungsadmins").
It will be used to group the "Liste der Aktiven".
  • Loading branch information
sedrubal committed Apr 27, 2019
1 parent aa43cff commit 89b8186
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 83 deletions.
6 changes: 6 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
root = true

[*.php]
end_of_line = lf
insert_final_newline = true
indent_style = tab
75 changes: 72 additions & 3 deletions faufablab_custom_user_fields.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
// add custom user fields (mobile number and FAU ID) and add button to export FAU IDs as CSV.
// add custom user fields (mobile number, FAU ID and door permission) and add button to export FAU IDs as CSV.

defined('ABSPATH') or die("[!] This script must be executed by a wordpress instance!\r\n");

Expand All @@ -13,6 +13,74 @@ function faufablab_custom_contact_methods( $fields ) {
}
add_filter( 'user_contactmethods', 'faufablab_custom_contact_methods' );

/**
* Add custom form for door permission (must not be edited by the user itself but only by admins).
*/
function faufablab_door_permission_add_user_fields( $user ) {
$door_permission_until = get_user_meta( $user->ID, "faufablab_door_permission_until", true );
$is_admin = current_user_can( 'edit_users' );
?>
<table class="form-table">
<tr>
<th><label for="door_permission_until">Schließberechtigung bis</label></th>
<td>
<div>
<input
type="date"
id="faufablab_door_permission_until"
name="faufablab_door_permission_until"
value="<?= $door_permission_until ?>"
<?php if ( ! $is_admin ) { echo "readonly"; } ?>
>
<p>
<span class="description">
Dieses Feld kann nur von Administratoren bearbeitet werden. Es sollte aber nur vom Schließberechtigungsadmin bearbeitet werden.
</span>
</p>
</div>
</td>
</tr>
</table>
<?php
}
add_action( 'show_user_profile', 'faufablab_door_permission_add_user_fields' );
add_action( 'edit_user_profile', 'faufablab_door_permission_add_user_fields' );

/**
* Save door permission.
*/
function faufablab_door_permission_save_user_fields( $user_id ) {
$user = get_user_by( 'id', $user_id );

$current_user = wp_get_current_user();

$old_door_permission_until = get_user_meta( $user->ID, "faufablab_door_permission_until", true );
$door_permission_until = $_POST['faufablab_door_permission_until'];
// ignore, when there are no changes
if ( $door_permission_until === $old_door_permission_until ) {
return false;
}

if ( ! current_user_can( 'edit_users' ) ) {
FAUFabLabAdminNotice::displayError(__('You are not allowed to edit the door permission of this user!'));
return false;
}

// validation
$match = preg_match( '/^(\d\d\d\d-\d\d-\d\d|)$/', $door_permission_until );
if ( ! $match ) {
FAUFabLabAdminNotice::displayError(__('Invalid characters for door_permission.'));
return false;
}

// save
update_user_meta( $user_id, 'faufablab_door_permission_until', $door_permission_until );

}
add_action( 'personal_options_update', 'faufablab_door_permission_save_user_fields' );
add_action( 'edit_user_profile_update', 'faufablab_door_permission_save_user_fields' );


/**
* export a CSV file including the name and their FAU Card ID of all FabLab Betreuer.
*/
Expand All @@ -24,15 +92,16 @@ function fablab_export_fauids( $args ) {
</button>
<script>
function export_fauids_csv() {
var csv_text = '"id"\t"name"\t"FAU Card ID"\n';
var csv_text = '"id"\t"name"\t"FAU Card ID"\t"door permission until"\n';
<?php
$wp_users = get_users( array( 'role_in' => array( 'editor' ) ) );
foreach( $wp_users as $wp_user ) {
?>
var user_id = <?= $wp_user->id ?>;
var user_display_name = "<?= str_replace('"', '\\\"', $wp_user->display_name) ?>";
var user_fau_id = "<?= str_replace('"', '\\\"', $wp_user->fau_id) ?>";
csv_text += `${user_id}\t"${user_display_name}"\t"${user_fau_id}"\n`;
var door_permission_until = "<?= get_user_meta( $wp_user->id, 'faufablab_door_permission_until' , true ) ?>";
csv_text += `${user_id}\t"${user_display_name}"\t"${user_fau_id}"\t${door_permission_until}\n`;
<?php
}
?>
Expand Down
172 changes: 100 additions & 72 deletions faufablab_liste_der_aktiven.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,67 +13,93 @@ function faufablab_print_listederaktiven( $attrs ) {
// fablab_export_fauids();
}

// FabLab Aktive == editor or admin.
$users = get_users( array(
'role__in' => array( 'editor', 'administrator' )
) );

// sort people by first name.
usort( $users, function( $user1, $user2 ) {
return $user1->user_firstname > $user2->user_firstname;
} );

$ret = '
<table id="listederaktiven">
<thead>
<tr>
<th>Bild</th>
<th>Name</th>
<th>E-Mail</th>
<th>Mobile</th>
<th>VCard</th>
</tr>
</thead>';
// group people by door permission date
$grouped = array();
foreach ( $users as $user ) {
$door_perm_until = get_user_meta( $user->ID, "faufablab_door_permission_until", true );
$grouped[$door_perm_until][] = $user;
}

// sort door permission groups descending. No door permission goes last.
uksort( $grouped, function( $group1, $group2 ) {
if ( ! $group1 ) {
return +1;
} else if ( ! $group2 ) {
return -1;
} else {
return ( $group1 > $group2 ) ? -1 : +1;
}
} );

// iterate over all door permission groups and their users and generate a table with contact data.
$ret = '';
foreach ( $grouped as $door_perm_until => $gusers ) {
$title = ( $door_perm_until ? "Schließberechtigung bis " . $door_perm_until : "Keine Schließberechtigung" );
$ret = $ret . '<h2>' . $title . '</h2>';

$ret = $ret . '
<table id="listederaktiven">
<thead>
<tr>
<th>Bild</th>
<th>Name</th>
<th>E-Mail</th>
<th>Mobile</th>
<th>VCard</th>
</tr>
</thead>';

$image_type = array( 'jpg' => 'JPEG', 'png' => 'PNG' );
foreach ( $users as &$user ) {
// $user->user_nicename,
$avatar_url = faufablab_profile_image_url( $user );
$avatar_file_path = faufablab_profile_image_path( $user );
$image_type = array( 'jpg' => 'JPEG', 'png' => 'PNG' );
foreach ( $gusers as &$user ) {
// $user->user_nicename,
$avatar_url = faufablab_profile_image_url( $user );
$avatar_file_path = faufablab_profile_image_path( $user );

// construct image for vcard:
$avatar_vcard_line = 'PHOTO;ENCODING=BASE64;';
// determine image type
try {
// try catch, weil das zu 90% schief geht
$tmp = explode( ".", $avatar_url );
$avatar_type = $image_type[end( $tmp )];
if ( $avatar_type == '' ) {
throw new Exception('Could not determine image type');
// construct image for vcard:
$avatar_vcard_line = 'PHOTO;ENCODING=BASE64;';
// determine image type
try {
// try catch, weil das zu 90% schief geht
$tmp = explode( ".", $avatar_url );
$avatar_type = $image_type[end( $tmp )];
if ( $avatar_type == '' ) {
throw new Exception('Could not determine image type');
}
$avatar_vcard_line .= $avatar_type;
} catch (Exception $exc) {
// wird schon jpeg sein
$avatar_vcard_line .= 'JPEG';
}
$avatar_vcard_line .= $avatar_type;
} catch (Exception $exc) {
// wird schon jpeg sein
$avatar_vcard_line .= 'JPEG';
}
$avatar_vcard_line .= ':';
$avatar_vcard_line .= ':';

try {
if ( $avatar_file_path == '' ) {
throw new Exception('User has no avatar.');
try {
if ( $avatar_file_path == '' ) {
throw new Exception('User has no avatar.');
}
$avatar_file = fopen( $avatar_file_path, 'r' );
$avatar_content = fread( $avatar_file, filesize( $avatar_file_path ) );
fclose( $avatar_file );
$avatar_base64 = base64_encode( $avatar_content );
$avatar_vcard_line .= $avatar_base64;
} catch (Exception $exc) {
// don't include image in vcard
$avatar_vcard_line = '';
}
$avatar_file = fopen( $avatar_file_path, 'r' );
$avatar_content = fread( $avatar_file, filesize( $avatar_file_path ) );
fclose( $avatar_file );
$avatar_base64 = base64_encode( $avatar_content );
$avatar_vcard_line .= $avatar_base64;
} catch (Exception $exc) {
// don't include image in vcard
$avatar_vcard_line = '';
}

$ret = $ret . '
<tbody>
<tr>
<script>
$ret = $ret . '
<tbody>
<tr>
<script>
var faufablab_vcard_' . $user->ID . ' = `BEGIN:VCARD
VERSION:2.1
N:' . $user->user_lastname . ';' . $user->user_firstname . ';;;
Expand All @@ -86,33 +112,35 @@ function faufablab_print_listederaktiven( $attrs ) {
REV:' . date("Ymd\THis\Z") . '
END:VCARD
`;
</script>
<td>
<div style="
width:100px;
height:100px;
padding:1px;
background-image:url(' . $avatar_url . ');
background-size:cover;
background-repeat:no-repeat;
background-position:50% 50%;
"></div>
</td>
<td>' . $user->user_firstname . ' ' . $user->user_lastname . '<br><small>' . $user->nickname . '</small></td>
<td><a href="mailto:' . $user->user_email . '">' . $user->user_email . '</a></td>
<td><a href="tel:' . $user->mobile . '">' . $user->mobile . '</a></td>
<td>
<button class="faufablab_vcard_export" type="button"
onclick="location.href=\'data:text/vcard;charset=utf-8,\' + encodeURIComponent(faufablab_vcard_' . $user->ID . ');">
</button>
</td>
</tr>
</tbody>
';
</script>
<td>
<div style="
width:100px;
height:100px;
padding:1px;
background-image:url(' . $avatar_url . ');
background-size:cover;
background-repeat:no-repeat;
background-position:50% 50%;
"></div>
</td>
<td>' . $user->user_firstname . ' ' . $user->user_lastname . '<br><small>' . $user->nickname . '</small></td>
<td><a href="mailto:' . $user->user_email . '">' . $user->user_email . '</a></td>
<td><a href="tel:' . $user->mobile . '">' . $user->mobile . '</a></td>
<td>
<button class="faufablab_vcard_export" type="button"
onclick="location.href=\'data:text/vcard;charset=utf-8,\' + encodeURIComponent(faufablab_vcard_' . $user->ID . ');">
</button>
</td>
</tr>
</tbody>
';
}

$ret = $ret . "</table>";
}

$ret = $ret . "</table>";
return $ret;
}
add_shortcode( 'aktivenliste', 'faufablab_print_listederaktiven' );
16 changes: 8 additions & 8 deletions faufablab_profile_image.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ function faufablab_profile_image_url( $user ) {
/**
* Add custom profile image upload form.
*/
function faufablab_add_user_fields( $user ) {
function faufablab_avatar_add_user_fields( $user ) {
?>
<table class="form-table">
<tr>
Expand Down Expand Up @@ -114,21 +114,21 @@ function faufablab_add_user_fields( $user ) {
</table>
<?php
}
add_action( 'show_user_profile', 'faufablab_add_user_fields' );
add_action( 'edit_user_profile', 'faufablab_add_user_fields' );
add_action( 'show_user_profile', 'faufablab_avatar_add_user_fields' );
add_action( 'edit_user_profile', 'faufablab_avatar_add_user_fields' );

/**
* Set the attribute enctype to multipart for the default profile editing form to support image upload.
*/
function faufablab_enctype_multipart( ) {
function faufablab_avatar_enctype_multipart( ) {
echo ' enctype="multipart/form-data"';
}
add_action( 'user_edit_form_tag' , 'faufablab_enctype_multipart' );
add_action( 'user_edit_form_tag' , 'faufablab_avatar_enctype_multipart' );

/**
* Save custom profile image.
*/
function faufablab_save_user_fields( $user_id ) {
function faufablab_avatar_save_user_fields( $user_id ) {
$user = get_user_by( 'id', $user_id );

$current_user = wp_get_current_user();
Expand Down Expand Up @@ -186,5 +186,5 @@ function faufablab_save_user_fields( $user_id ) {
return false;
}
}
add_action( 'personal_options_update', 'faufablab_save_user_fields' );
add_action( 'edit_user_profile_update', 'faufablab_save_user_fields' );
add_action( 'personal_options_update', 'faufablab_avatar_save_user_fields' );
add_action( 'edit_user_profile_update', 'faufablab_avatar_save_user_fields' );

0 comments on commit 89b8186

Please sign in to comment.