/**
* Prints next pending letter type for requested operations
* Operation IDs are passed as an array (operations[]) via GET or POST
* Invalid operation IDs are ignored.
*
* @throws CHttpException
*/
public function actionPrintLetters()
{
Audit::add('waiting list', @$_REQUEST['all'] == 'true' ? 'print all' : 'print selected', serialize($_POST));
if (isset($_REQUEST['event_id'])) {
$operations = Element_OphTrOperationbooking_Operation::model()->findAll('event_id=?', array($_REQUEST['event_id']));
$auto_confirm = true;
} else {
$operation_ids = isset($_REQUEST['operations']) ? $_REQUEST['operations'] : null;
$auto_confirm = isset($_REQUEST['confirm']) && $_REQUEST['confirm'] == 1;
if (!is_array($operation_ids)) {
throw new CHttpException('400', 'Invalid operation list');
}
$operations = Element_OphTrOperationbooking_Operation::model()->findAllByPk($operation_ids);
}
$this->layout = '//layouts/print';
$cmd = Yii::app()->db->createCommand('SELECT GET_LOCK(?, 1)');
while (!$cmd->queryScalar(array('waitingListPrint'))) {
}
$directory = Yii::app()->assetManager->basePath . '/waitingList';
Yii::app()->db->createCommand('SELECT RELEASE_LOCK(?)')->execute(array('waitingListPrint'));
$html = '';
$docrefs = array();
$barcodes = array();
$patients = array();
$documents = 0;
// FIXME: provide a means by which progress can be reported back to the user, possibly via session and parallel polling?
foreach ($operations as $operation) {
$letter_status = $operation->getDueLetter();
if ($letter_status === null && $operation->getLastLetter() == Element_OphTrOperationbooking_Operation::LETTER_GP) {
$letter_status = Element_OphTrOperationbooking_Operation::LETTER_GP;
}
set_time_limit(3);
$html .= $this->printLetter($operation, $auto_confirm);
$docrefs[] = "E:{$operation->event->id}/" . strtoupper(base_convert(time() . sprintf('%04d', Yii::app()->user->getId()), 10, 32)) . '/{{PAGE}}';
$barcodes[] = $operation->event->barcodeHTML;
$patients[] = $operation->event->episode->patient;
++$documents;
if ($letter_status == Element_OphTrOperationbooking_Operation::LETTER_GP) {
// Patient letter is another document
$docrefs[] = "E:{$operation->event->id}/" . strtoupper(base_convert(time() . sprintf('%04d', Yii::app()->user->getId()), 10, 32)) . '/{{PAGE}}';
$barcodes[] = $operation->event->barcodeHTML;
$patients[] = $operation->event->episode->patient;
++$documents;
}
}
set_time_limit(10);
$pdf_suffix = 'waitingList_' . Yii::app()->user->id . '_' . rand();
$wk = new WKHtmlToPDF();
$wk->setDocuments($documents);
$wk->setDocrefs($docrefs);
$wk->setBarcodes($barcodes);
$wk->setPatients($patients);
$wk->generatePDF($directory, $pdf_suffix, '', $html);
$pdf = $directory . "/{$pdf_suffix}.pdf";
header('Content-Type: application/pdf');
header('Content-Length: ' . filesize($pdf));
readfile($pdf);
@unlink($pdf);
}