From 001b1a078af6be1cd08578fc2b039b486ac33ff8 Mon Sep 17 00:00:00 2001 From: Daniel Barreto Date: Wed, 25 Feb 2015 17:53:53 -0500 Subject: [PATCH] Add attachments without template - refs BT#9461 --- main/inc/lib/pdf.lib.php | 14 +++- .../src/AdvancedSubscriptionPlugin.php | 76 +++++++++++++++++-- .../src/terms_and_conditions.php | 10 +-- .../test/terms_to_pdf.php | 47 ++++++++++++ .../views/terms_and_conditions_to_pdf.tpl | 23 ++++++ 5 files changed, 151 insertions(+), 19 deletions(-) create mode 100644 plugin/advanced_subscription/test/terms_to_pdf.php create mode 100644 plugin/advanced_subscription/views/terms_and_conditions_to_pdf.tpl diff --git a/main/inc/lib/pdf.lib.php b/main/inc/lib/pdf.lib.php index a28c7d2404..836a480f80 100755 --- a/main/inc/lib/pdf.lib.php +++ b/main/inc/lib/pdf.lib.php @@ -351,7 +351,8 @@ class PDF $document_html, $css = '', $pdf_name = '', - $course_code = null + $course_code = null, + $outputMode = 'D' ) { global $_configuration; @@ -433,10 +434,15 @@ class PDF $output_file = 'pdf_'.date('Y-m-d-his').'.pdf'; } else { $pdf_name = replace_dangerous_char($pdf_name); - $output_file = $pdf_name.'.pdf'; + // Save temporally into Archive folder + $output_file = api_get_path(SYS_ARCHIVE_PATH) . $pdf_name.'.pdf'; + } + $this->pdf->Output($output_file, $outputMode); // F to save the pdf in a file + if ($outputMode == 'F') { + // Do NOT exit when export to file + } else { + exit; } - $this->pdf->Output($output_file, 'D'); // F to save the pdf in a file - exit; } /** diff --git a/plugin/advanced_subscription/src/AdvancedSubscriptionPlugin.php b/plugin/advanced_subscription/src/AdvancedSubscriptionPlugin.php index b3e6515714..73850528bb 100644 --- a/plugin/advanced_subscription/src/AdvancedSubscriptionPlugin.php +++ b/plugin/advanced_subscription/src/AdvancedSubscriptionPlugin.php @@ -302,15 +302,31 @@ class AdvancedSubscriptionPlugin extends Plugin implements HookPluginInterface * @param string $content * @param int $sessionId * @param bool $save + * @param array $fileAttachments * @return bool|int */ - public function sendMailMessage($studentId, $receiverId, $subject, $content, $sessionId, $save = false) + public function sendMailMessage($studentId, $receiverId, $subject, $content, $sessionId, $save = false, $fileAttachments = array()) { - $mailId = MessageManager::send_message( - $receiverId, - $subject, - $content - ); + if ( + !empty($fileAttachments) && + is_array($fileAttachments) && + isset($fileAttachments['files']) && + isset($fileAttachments['comments']) + ) { + $mailId = MessageManager::send_message( + $receiverId, + $subject, + $content, + $fileAttachments['files'], + $fileAttachments['comments'] + ); + } else { + $mailId = MessageManager::send_message( + $receiverId, + $subject, + $content + ); + } if ($save && !empty($mailId)) { // Save as sent message @@ -523,6 +539,34 @@ class AdvancedSubscriptionPlugin extends Plugin implements HookPluginInterface ); break; case ADVANCED_SUBSCRIPTION_ACTION_ADMIN_APPROVE: + $fileAttachments = array(); + if (api_get_plugin_setting('courselegal', 'tool_enable')) { + $courseLegal = CourseLegalPlugin::create(); + $courses = SessionManager::get_course_list_by_session_id($data['sessionId']); + $course = current($courses); + $data['courseId'] = $course['id']; + $data['course'] = api_get_course_info_by_id($data['courseId']); + $termsAndConditions = $courseLegal->getData($data['courseId'], $data['sessionId']); + $termsAndConditions = $termsAndConditions['content']; + $termsAndConditions = $this->renderTemplateString($termsAndConditions, $data); + $tpl = new Template(get_lang('TermsAndConditions')); + $tpl->assign('session', $data['session']); + $tpl->assign('student', $data['student']); + $tpl->assign('sessionId', $data['sessionId']); + $tpl->assign('termsContent', $termsAndConditions); + $termsAndConditions = $tpl->fetch('/advanced_subscription/views/terms_and_conditions_to_pdf.tpl'); + $pdf = new PDF(); + $filename = 'terms' . sha1(rand(0,99999)); + $pdf->content_to_pdf($termsAndConditions, null, $filename, null, 'F'); + $fileAttachments['file'][] = array( + 'name' => $filename . '.pdf', + 'application/pdf' => $filename . '.pdf', + 'tmp_name' => api_get_path(SYS_ARCHIVE_PATH) . $filename . '.pdf', + 'error' => UPLOAD_ERR_OK, + 'size' => filesize(api_get_path(SYS_ARCHIVE_PATH) . $filename . '.pdf'), + ); + $fileAttachments['comments'][] = get_lang('TermsAndConditions'); + } // Mail to student $mailIds[] = $this->sendMailMessage( $data['studentUserId'], @@ -530,7 +574,8 @@ class AdvancedSubscriptionPlugin extends Plugin implements HookPluginInterface $this->get_lang('MailAdminAccept'), $template->fetch('/advanced_subscription/views/admin_accepted_notice_student.tpl'), $data['sessionId'], - true + true, + $fileAttachments ); // Mail to superior $mailIds[] = $this->sendMailMessage( @@ -1115,4 +1160,21 @@ class AdvancedSubscriptionPlugin extends Plugin implements HookPluginInterface return false; } + + /** + * Return string replacing tags "{{}}"with variables assigned in $data + * @param string $templateContent + * @param array $data + * @return string + */ + public function renderTemplateString($templateContent, $data = array()) + { + $twigString = new \Twig_Environment(new \Twig_Loader_String()); + $templateContent = $twigString->render( + $templateContent, + $data + ); + + return $templateContent; + } } diff --git a/plugin/advanced_subscription/src/terms_and_conditions.php b/plugin/advanced_subscription/src/terms_and_conditions.php index 78e0adf9f1..bbc16b03f9 100644 --- a/plugin/advanced_subscription/src/terms_and_conditions.php +++ b/plugin/advanced_subscription/src/terms_and_conditions.php @@ -52,17 +52,11 @@ if ( $data['session'] = api_get_session_info($data['sessionId']); $data['student'] = Usermanager::get_user_info_by_id($data['studentUserId']); + $data['course'] = api_get_course_info_by_id($data['courseId']); $data['acceptTermsUrl'] = $plugin->getQueueUrl($data); $data['rejectTermsUrl'] = $plugin->getTermsUrl($data, ADVANCED_SUBSCRIPTION_TERMS_MODE_REJECT); // Use Twig with String loader - $twigString = new \Twig_Environment(new \Twig_Loader_String()); - $termsContent = $twigString->render( - $termsAndConditions, - array( - 'session' => $data['session'], - 'student' => $data['student'], - ) - ); + $termsContent = $plugin->renderTemplateString($termsAndConditions, $data); } else { $termsContent = ''; diff --git a/plugin/advanced_subscription/test/terms_to_pdf.php b/plugin/advanced_subscription/test/terms_to_pdf.php new file mode 100644 index 0000000000..57a4f52699 --- /dev/null +++ b/plugin/advanced_subscription/test/terms_to_pdf.php @@ -0,0 +1,47 @@ +getData($data['courseId'], $data['sessionId']); + $termsAndConditions = $termsAndConditions['content']; + $termsAndConditions = $plugin->renderTemplateString($termsAndConditions, $data); + $tpl = new Template($plugin->get_lang('Terms')); + $tpl->assign('session', $data['session']); + $tpl->assign('student', $data['student']); + $tpl->assign('sessionId', $data['sessionId']); + $tpl->assign('termsContent', $termsAndConditions); + $termsAndConditions = $tpl->fetch('/advanced_subscription/views/terms_and_conditions_to_pdf.tpl'); + $pdf = new PDF(); + $filename = 'terms' . sha1(rand(0,99999)); + $pdf->content_to_pdf($termsAndConditions, null, $filename, null, 'F'); + $fileDir = api_get_path(WEB_ARCHIVE_PATH) . $filename . '.pdf'; + echo '
', print_r($fileDir, 1), '
'; + } +} \ No newline at end of file diff --git a/plugin/advanced_subscription/views/terms_and_conditions_to_pdf.tpl b/plugin/advanced_subscription/views/terms_and_conditions_to_pdf.tpl new file mode 100644 index 0000000000..5e3fab89e6 --- /dev/null +++ b/plugin/advanced_subscription/views/terms_and_conditions_to_pdf.tpl @@ -0,0 +1,23 @@ +{# start copy from head.tpl #} + + + + +{{ prefetch }} +{{ favico }} +{{ browser_specific_head }} + + + + +{# Use the latest engine in ie8/ie9 or use google chrome engine if available #} +{# Improve usability in portal devices #} + +{{ title_string }} +{{ css_file_to_string }} +{{ css_style_print }} +{{ js_file_to_string }} +{# end copy from head.tpl #} + \ No newline at end of file