Skip to content

Instantly share code, notes, and snippets.

@nilovelez
Created November 20, 2019 10:30
Show Gist options
  • Save nilovelez/2f5809f0f44a3fae741bf051119893a6 to your computer and use it in GitHub Desktop.
Save nilovelez/2f5809f0f44a3fae741bf051119893a6 to your computer and use it in GitHub Desktop.
camptix-gravatar-badges.php
<?php
header('Content-Type: text/html; charset=iso-8859-1');
set_time_limit ( 300 );
/*
* Generate a CSV for InDesign with attendee info and Gravatars
*
* See http://plan.wordcamp.org/helpful-documents-and-templates/create-wordcamp-badges-with-gravatars/ for instructions
*
* input is a CSV export of CampTix attendees. Format is: "First Name","Last Name","E-mail Address","Twitter Username"
* the script downloads the attendee's Gravatars, and adds a column to the CSV with the filename of the image
* the CSV can then be used by InDesign to generate wordcamp badges with the attendee's gravatar
*
* make sure you update the input/output file paths before running
*/
//$_SERVER['HTTP_HOST'] = 'example.org';
//define( 'ABSPATH', '/path/to/example.org/' );
//require dirname( dirname( __FILE__ ) ) . '/wp-config.php';
/*
Campos:
0 ID de asistente
1 Tipo de entrada
2 Nombre
3 Apellido(s)
4 Dirección de correo electrónico
5 Fecha de compra
6 Fecha de la última modificación
7 Estado
8 ID de transacción
9 Cupón
10 Nombre del comprador de la entrada
11 Dirección de correo electrónico del comprador de la entrada
12 Método de pago
13 ¿Vas a asistir a los talleres del viernes?
14 ¿Qué estilo de de camiseta quieres?
15 ¿Cuál es tu usuario de twitter? (Para que aparezca en tu acreditación. Escríbela con el formato '@nombredeusuario, no la url de la cuenta)
16 ¿Con qué género te identificas? (Pregunta no obligatoria, se pregunta a efecto estadísticos para estudiar el problema de baja participación de algunos géneros en eventos tecnológicos)
17 ¿Qué relación tienes con WordPress? Ayúdanos a conocerte mejor para adaptar el evento a ti.
18 ¿Necesitarás servicio de WordPress for Kids?
19 Ahora decide sobre tu camiseta:
20 ¿Qué talla de camiseta quieres?
21 ¿De qué equipo eres?
22 ¿Vas a asistir al Contributor Day (domingo)?
23 ¿Cuál consideras que es tu nivel como desarrollador?
24 Asistió al evento
25 Alergia potencialmente mortal
26 Necesidades de accesibilidad
*/
$valid_fields = array(
'first_name' => 2,
'last_name' => 3,
'email' => 4,
'twitter' => 15,
'camiseta_estilo' => 14,
'camiseta_talla' => 20,
'equipo' => 21,
'nivel' => 23,
'alergia' => 25
);
// Configuration
$csv_dir = __DIR__ . '\\csv\\';
$gravatar_dir = __DIR__ . '\\gravatars\\';
$original_csv = fopen( $csv_dir . 'camptix-export-2019-09-29.csv', 'r' ); // @todo accept filenames from cli argument
$final_csv = fopen( $csv_dir . 'badges-output.csv', 'w' );
$destination_directory = 'D:\\Dropbox\\webs\\acreditaciones_2019\\gravatars\\'; // @todo accept from cli argument
$icon_directory = 'D:\\Dropbox\\webs\\acreditaciones_2019\\icons\\'; // @todo accept from cli argument
$replace_empty_twitter = false;
$missing_gravatars = array();
$stock_camisetas = array();
$output_headers = array(
'First Name',
'Last Name',
'Twitter',
'@Gravatar', // prefixed with a @ to let InDesign know that it contains images
'Info camiseta',
'@Camiseta', // prefixed with a @ to let InDesign know that it contains images
'@Equipo', // prefixed with a @ to let InDesign know that it contains images
'@Nivel', // prefixed with a @ to let InDesign know that it contains images
'@Alergia' // prefixed with a @ to let InDesign know that it contains images
);
echo '<pre>';
echo "\nRunning...";
if ( $original_csv && $final_csv ) {
$row = -1;
$gravatar_file = null;
fputcsv( $final_csv, $output_headers );
while ( ( $original_attendee = fgetcsv( $original_csv, 1000, "," ) ) !== FALSE ) {
$row++;
if ( $row == 0 ) {
continue; // skip header
}
// todo: saltar los tipos de entrada que no llevan acreditación
/*
if ( $row == 10 ) {
break; // test only
}*/
/*
foreach ( $attendee as $key => $value ) {
$attendee[ $key ] = trim( $value );
}
*/
$input = array();
foreach ( $valid_fields as $label => $index ) {
$input[$label] = utf8_decode(trim( $original_attendee[$index] ));
}
/*************** First Name & Last name ****************/
// Create empty badges for unknown attendees
if ( 'unknown.attendee@example.org' == $input['email'] ) {
$attendee[0] = $attendee[1] = $attendee[2] = $attendee[3] = $attendee[4] = $attendee[5] = $attendee[6] = $attendee[7] = $attendee[8] = '';
}
// Capitalize the first and last names so they look better
// todo if there's more than one word in either field, don't do anything b/c could be something like "Ines van Essen" and they want the "van" lowercase
$attendee[0] = ucwords( strtolower($input['first_name']) );
$attendee[1] = ucwords( strtolower($input['last_name']));
/*************** Twitter ****************/
// If they don't have a Twitter handle, add their first name instead. Add a @ to Twitter handles to avoid confusing them with first names.
if ( empty ( $input['twitter'] ) ) {
if ( $replace_empty_twitter ) {
$attendee[2] = $attendee[0];
}else{
$attendee[2] = '';
}
} else {
// Strip out everything but the username
$input['twitter'] = preg_replace(
'/
(https?:\/\/)?
(twitter\.com\/)?
(@)?
/ix',
'',
$input['twitter']
);
if ( "'" == substr($input['twitter'], 0, 1) ){
$input['twitter'] = substr($input['twitter'], 1);
}
$attendee[2] = '@'.$input['twitter'];
}
/*************** Gravatar ****************/
// Download the Gravatar
$filename = 'default.png';
if ( $input['email'] ) {
//$gravatar_source = @file_get_contents( 'http://www.gravatar.com/avatar/' . md5( strtolower( trim( $input['email'] ) ) ) . '.jpg?s=512&default=404' );
$gravatar_source = @file_get_contents( 'http://www.gravatar.com/avatar/' . md5( strtolower( trim( $input['email'] ) ) ) . '.jpg?s=512&default=robohash' );
if ( $gravatar_source !== false ) {
$filename = md5( strtolower( trim( $input['email'] ) ) ) . '.jpg';
$gravatar_file = fopen( $gravatar_dir . '/' . $filename, 'w' );
fwrite( $gravatar_file, $gravatar_source );
fclose( $gravatar_file );
}
}
if ( 'default.png' === $filename ) {
$missing_gravatars[] = $attendee[0] .' '. $attendee[1];
}
// Update the final CSV
$attendee[3] = $destination_directory . $filename;
/*************** Camiseta ****************/
// Camiseta
$camiseta = array();
// Camiseta recta defecto
$camiseta[0] = 'recta';
if ( 'Entallada' == $input['camiseta_estilo'] ) {
$camiseta[0] = 'entallada';
}
// Camiseta classic por defecto
$camiseta[1] = 'classic';
if ( 'Gutenberg' == $input['equipo'] ) {
$camiseta[1] = 'gutenberg';
}
// Camiseta M por defecto
$camiseta[2] = 'M';
if ( in_array( $input['camiseta_talla'], array('S', 'M', 'L', 'XL') ) ){
$camiseta[2] = $input['camiseta_talla'];
}
$attendee[4] = join(' ', $camiseta);
$filebase_camiseta = join('_', $camiseta);
$attendee[5] = $icon_directory . 'camiseta_' . $filebase_camiseta . '.png';
if (array_key_exists( $filebase_camiseta, $stock_camisetas) ) {
$stock_camisetas[$filebase_camiseta]++;
}else{
$stock_camisetas[$filebase_camiseta] = 1;
}
/*************** Equipo ****************/
$attendee[6] = 'classic';
if ( 'Gutenberg' == $input['equipo'] ) {
$attendee[6] = 'gutenberg';
}
$attendee[6] = $icon_directory . 'equipo_' . $attendee[6] . '.png';
/*************** Nivel ****************/
$attendee[7] = 'intermediate';
if ( 'Advanced' == $input['nivel'] ) {
$attendee[7] = 'advanced';
}
if ( in_array( $input['nivel'] , array( 'Begginer', 'Beginner') ) ) {
$attendee[7] = 'beginner';
}
$attendee[7] = $icon_directory . 'nivel_' . $attendee[7] . '.png';
/*************** Alergia ****************/
echo "alergia: ". $input['alergia'];
$attendee[8] = 'no';
if ( 'No' !== $input['alergia'] ) {
$attendee[8] = 'si';
}
$attendee[8] = $icon_directory . 'alergia_' . $attendee[8] . '.png';
print_r( $attendee );
fputcsv( $final_csv, $attendee );
// Output progress
if ( 0 == $row % 50 ) {
echo "\nProcessed " . $row . " attendees.";
}
// Limit to best case of ~10 iterations per second, to avoid flooding gravatar.com or draining the host server
usleep( 100000 );
}
fclose( $original_csv );
fclose( $final_csv );
$status = "\nFinished processing " . $row . " attendees.\n";
if ( $missing_gravatars ) {
$status .= "\nThe following people did not have a Gravatar:\n\n";
$status .= implode( "\n", $missing_gravatars );
}
echo 'Stock de camisetas:';
foreach ($stock_camisetas as $tipo => $cantidad) {
echo "$tipo\t$cantidad<br>";
}
} else {
$status = "Error: Couldn't open files.";
}
echo "\n". $status ."\n\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment