$defaultStyle->getAlignment()->setWrapText(true);
$defaultStyle->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
$sheet = $excel->getActiveSheet();
- $sheet->setTitle('Récapitulatif de commande');
+ $sheet->setTitle('Récapitulatif de demande');
// Setup column headings
$current_column_index = 0;
$current_row = 1;
+ $sheet->getRowDimension(1)->setRowHeight(22); // Larger row height for headings
foreach ($column_headings as $column_heading) {
$sheet->setCellValueByColumnAndRow($current_column_index, $current_row, $column_heading);
$cell_style = $sheet->getStyleByColumnAndRow($current_column_index, $current_row);
foreach ($cart_items as $cart_item) {
$current_row++;
+ // Set a slightly larger row height to give better vertical spacing
+ $sheet->getRowDimension($current_row)->setRowHeight(22);
+
// Look up the value by the column keys to ensure the correct order of values
foreach (array_keys($column_headings) as $column_index => $column_key) {
$details_label_style->getFont()->setBold(true);
$details_label_style->getAlignment()->setWrapText(false);
$details_label_style->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
- $details_sheet->setCellValueByColumnAndRow(0, $current_row, $user_detail['label'] . ' :');
+ $details_label_style->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_TOP);
+ // Top align the values because we'll be using a slightly larger row height
+ $details_value_style = $details_sheet->getStyleByColumnAndRow(1, $current_row);
+ $details_value_style->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_TOP);
+
+ // Set values
+ $details_sheet->setCellValueByColumnAndRow(0, $current_row, $user_detail['label'] . ' :');
$details_sheet->setCellValueByColumnAndRow(1, $current_row, $user_detail['value']);
- $details_sheet->getRowDimension($current_row)->setRowHeight(-1); // Auto row height from content
+
+ // $details_sheet->getStyleByColumnAndRow(1, $current_row)->getAlignment()->setWrapText(true);
+ // $details_sheet->getRowDimension($current_row)->setRowHeight(-1); // Auto row height from content
+
+ // Unfortunately, LibreOffice has a long-standing bug where auto row height doesn't work :(
+ // Ref: https://github.com/PHPOffice/PHPExcel/issues/588#issuecomment-249544915
+ // Since we may have multi-line text for the address and message, we need to figure out how high
+ // the row should be, based on the number of lines. The default row height is 12.75pt
+ // Ref: https://github.com/PHPOffice/PHPExcel/blob/1.8/Documentation/markdown/Overview/08-Recipes.md#setting-a-rows-height
+ $single_row_height = 12.75; // Basis for a single line of text
+ $line_count = count(explode("\n", $user_detail['value']));
+ // Add a slight extra buffer to row height (+3) so it isn't tight against the next one
+ $details_sheet->getRowDimension($current_row)->setRowHeight($line_count * $single_row_height + 3);
+
$current_row++;
}
$details_sheet->getColumnDimensionByColumn(1)->setAutoSize(true);
//==== Save the Excel spreadsheet to disk temporarily
- $writer = PHPExcel_IOFactory::createWriter($excel, 'Excel2007'); // .xlsx format
+ $xls_writer = PHPExcel_IOFactory::createWriter($excel, 'Excel2007'); // .xlsx format
$xls_file = CubeIT_Files::tempnam();
- $writer->save($xls_file);
+ $xls_writer->save($xls_file);
+
+ //==== Convert the XLSX file to PDF for the client
+ $libreoffice = new CubeIT_CommandLine('libreoffice', false, false);
+ $libreoffice->setArg('--convert-to', 'pdf');
+ // libreoffice doesn't allow us to set a destination name (it's a batch conversion command),
+ // so make sure the file is written back to the same temporary path:
+ $libreoffice->setArg('--outdir', sys_get_temp_dir());
+ $libreoffice->setArg(null, $xls_file);
+ $libreoffice->execute();
+
+ $pdf_file = "{$xls_file}.pdf"; // Filename of converted file should be the original + .pdf
//==== Prepare the e-mail
// The Fluidbook can pass on a parameter that overrides the default recipient
switch(strtolower($recipient_code)) {
case 'guadeloupe':
- $email_to = 'contact2@bastide-guadeloupe.com';
+ $email_for_bastide = 'contact2@bastide-guadeloupe.com';
break;
case 'commercial':
- $email_to = 'commercial@bastide-guadeloupe.com';
+ $email_for_bastide = 'commercial@bastide-guadeloupe.com';
break;
case 'martinique':
- $email_to = 'commercial2@bastide-martinique.com';
+ $email_for_bastide = 'commercial2@bastide-martinique.com';
break;
case 'réunion':
case 'reunion':
- $email_to = 'info@bastide.re';
+ $email_for_bastide = 'info@bastide.re';
break;
default:
- $email_to = 'ca@hcm-medical.fr';
+ $email_for_bastide = 'ca@hcm-medical.fr';
}
- $email_cc = [
- $user_details['email']['value'],
- ];
$return_address = 'postmaster@fluidbook.com';
$email_from_name = "Bastide";
- $email_reply_to = $email_to;
- $email_subject = 'Récapitulatif de votre commande Bastide';
- $email_body = "Bonjour,\n\nVotre commande est en cours de traitement (récapitulatif en pièce jointe). Nous vous recontacterons dans les meilleurs délais.\n\nCordialement,\n\nL'équipe Bastide";
+ $email_reply_to = $email_for_bastide;
+ $email_subject = 'Récapitulatif de votre demande Bastide';
+ $email_body = "Bonjour,\n\nVotre demande est en cours de traitement (récapitulatif en pièce jointe). Nous reviendrons vers vous avec un devis dans les meilleurs délais.\n\nCordialement,\n\nL'équipe Bastide";
- // Send the email
- $transport = new CubeIT_Mail_Transport_Mailjet();
- $mail = new CubeIT_Mail_Mailjet();
- $mail->setFrom($return_address, $email_from_name);
- $mail->setReplyTo($email_reply_to, $email_from_name);
- $mail->setReturnPath($return_address);
- $mail->addTo($email_to);
- foreach ($email_cc as $cc_recipient) {
- $mail->addCc($cc_recipient);
- }
- $mail->setSubject($email_subject);
- $mail->setBodyText($email_body);
+ // Filename for attachment (without extension)
+ $attachment_basename = 'Bastide-' . date('YmdHis');
- $attachment_filename = 'Bastide-' . date('YmdHis') . '.xlsx';
+ //=== Send the email
+ // There are two almost identical e-mails sent:
+ // - E-mail to Bastide, containing the request details in XLS format
+ // - E-mail to the user, containing the same information but converted to PDF format
+ $transport = new CubeIT_Mail_Transport_Mailjet();
+ $mail_to_bastide = new CubeIT_Mail_Mailjet();
+ $mail_to_bastide->setFrom($return_address, $email_from_name);
+ $mail_to_bastide->setReplyTo($email_reply_to, $email_from_name);
+ $mail_to_bastide->setReturnPath($return_address);
+ $mail_to_bastide->setSubject($email_subject);
+ $mail_to_bastide->setBodyText($email_body);
+
+ // Duplicate the common e-mail settings and apply them to the user e-mail
+ $mail_to_user = clone $mail_to_bastide;
+ $mail_to_user->addTo($user_details['email']['value']);
+ $mail_to_user->createAttachment(
+ file_get_contents($pdf_file),
+ 'application/pdf',
+ Zend_Mime::DISPOSITION_ATTACHMENT,
+ Zend_Mime::ENCODING_BASE64,
+ $attachment_basename . '.pdf'
+ );
- $mail->createAttachment(
+ // Back to setting up the mail for Bastide
+ $mail_to_bastide->addTo($email_for_bastide);
+ $mail_to_bastide->createAttachment(
file_get_contents($xls_file),
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
Zend_Mime::DISPOSITION_ATTACHMENT,
Zend_Mime::ENCODING_BASE64,
- $attachment_filename
+ $attachment_basename . '.xlsx'
);
+ //=== Send the e-mails
try {
- print_r($mail->send($transport));
+ $mail_to_bastide->send($transport);
+ $mail_to_user->send($transport);
$result['success'] = true;
} catch (Exception $e){
error_log('#### Bastide: Failed sending message via Mailjet ####');
$result['success'] = false;
}
- unlink($xls_file); // Tidy up temporary file after sending
+ // Tidy up temporary files after sending
+ unlink($xls_file);
+ unlink($pdf_file);
if ($result['success']) {
- $result['message'] = 'Merci, votre commande a été bien reçue. Nous vous recontacterons dans les meilleurs délais.';
+ $result['message'] = 'Merci, votre demande a été bien reçue. Nous reviendrons vers vous avec un devis dans les meilleurs délais.';
} else {
$result['message'] = "Désolé, une erreur s'est produite. Veuillez réessayer ou contactez {$email_reply_to}.";
}