From: Lisa Marie Maginnis Date: Mon, 21 Sep 2015 17:14:15 +0000 (-0400) Subject: Added pdflatex files X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=19045a9937251651a7c9ef837a5d1619b1396c14;p=civicrm-core.git Added pdflatex files --- diff --git a/CRM/Contact/Form/Task/LatexPDF.php b/CRM/Contact/Form/Task/LatexPDF.php new file mode 100644 index 0000000000..3fccdd8d7b --- /dev/null +++ b/CRM/Contact/Form/Task/LatexPDF.php @@ -0,0 +1,125 @@ +skipOnHold = $this->skipDeceased = FALSE; + CRM_Contact_Form_Task_PDFLatexCommon::preProcess($this); + + // store case id if present + $this->_caseId = CRM_Utils_Request::retrieve('caseid', 'Positive', $this, FALSE); + + // retrieve contact ID if this is 'single' mode + $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this, FALSE); + + if ($cid) { + // this is true in non-search context / single mode + // in search context 'id' is the default profile id for search display + // CRM-11227 + $this->_activityId = CRM_Utils_Request::retrieve('id', 'Positive', $this, FALSE); + } + + if ($cid) { + CRM_Contact_Form_Task_PDFLatexCommon::preProcessSingle($this, $cid); + $this->_single = TRUE; + $this->_cid = $cid; + } + else { + parent::preProcess(); + } + $this->assign('single', $this->_single); + } + + function setDefaultValues() { + $defaults = array(); + if (isset($this->_activityId)) { + $params = array('id' => $this->_activityId); + CRM_Activity_BAO_Activity::retrieve($params, $defaults); + $defaults['html_message'] = CRM_Utils_Array::value('details', $defaults); + } + $defaults = $defaults + CRM_Contact_Form_Task_PDFLatexCommon::setDefaultValues(); + return $defaults; + } + + /** + * Build the form + * + * @access public + * + * @return void + */ + public function buildQuickForm() { + //enable form element + $this->assign('suppressForm', FALSE); + CRM_Contact_Form_Task_PDFLatexCommon::buildQuickForm($this); + } + + /** + * process the form after the input has been submitted and validated + * + * @access public + * + * @return None + */ + public function postProcess() { + CRM_Contact_Form_Task_PDFLatexCommon::postProcess($this); + } +} + diff --git a/CRM/Contact/Form/Task/PDFLatexCommon.php b/CRM/Contact/Form/Task/PDFLatexCommon.php new file mode 100644 index 0000000000..4dbdb1806b --- /dev/null +++ b/CRM/Contact/Form/Task/PDFLatexCommon.php @@ -0,0 +1,435 @@ +is_active = 1; + $dao->find(); + while ($dao->fetch()) { + $messageText[$dao->id] = $dao->msg_text; + $messageSubject[$dao->id] = $dao->msg_subject; + } + + $form->assign('message', $messageText); + $form->assign('messageSubject', $messageSubject); + } + + static function preProcessSingle(&$form, $cid) { + $form->_contactIds = array($cid); + // put contact display name in title for single contact mode + CRM_Contact_Page_View::setTitle($cid); + } + + /** + * Build the form + * + * @access public + * + * @return void + */ + static function buildQuickForm(&$form) { + $form->add('static', 'pdf_format_header', NULL, ts('Page Format')); + $form->add( + 'select', + 'format_id', + ts('Select Format'), + array(0 => ts('- default -')) + CRM_Core_BAO_PdfFormat::getList(TRUE), + FALSE, + array('onChange' => "selectFormat( this.value, false );") + );; + $form->add( + 'select', + 'paper_size', + ts('Paper Size'), + array(0 => ts('- default -')) + CRM_Core_BAO_PaperSize::getList(TRUE), + FALSE, + array('onChange' => "selectPaper( this.value ); showUpdateFormatChkBox();") + ); + $form->add('static', 'paper_dimensions', NULL, ts('Width x Height')); + $form->add( + 'select', + 'orientation', + ts('Orientation'), + CRM_Core_BAO_PdfFormat::getPageOrientations(), + FALSE, + array('onChange' => "updatePaperDimensions(); showUpdateFormatChkBox();") + ); + $form->add( + 'select', + 'metric', + ts('Unit of Measure'), + CRM_Core_BAO_PdfFormat::getUnits(), + FALSE, + array('onChange' => "selectMetric( this.value );") + ); + $form->add( + 'text', + 'margin_left', + ts('Left Margin'), + array('size' => 8, 'maxlength' => 8, 'onkeyup' => "showUpdateFormatChkBox();"), + TRUE + ); + $form->add( + 'text', + 'margin_right', + ts('Right Margin'), + array('size' => 8, 'maxlength' => 8, 'onkeyup' => "showUpdateFormatChkBox();"), + TRUE + ); + $form->add( + 'text', + 'margin_top', + ts('Top Margin'), + array('size' => 8, 'maxlength' => 8, 'onkeyup' => "showUpdateFormatChkBox();"), + TRUE + ); + $form->add( + 'text', + 'margin_bottom', + ts('Bottom Margin'), + array('size' => 8, 'maxlength' => 8, 'onkeyup' => "showUpdateFormatChkBox();"), + TRUE + ); + $form->add('checkbox', 'bind_format', ts('Always use this Page Format with the selected Template')); + $form->add('checkbox', 'update_format', ts('Update Page Format (this will affect all templates that use this format)')); + + $form->assign('useThisPageFormat', ts('Always use this Page Format with the new template?')); + $form->assign('useSelectedPageFormat', ts('Should the new template always use the selected Page Format?')); + $form->assign('totalSelectedContacts', count($form->_contactIds)); + + CRM_Mailing_BAO_Mailing::commonLetterCompose($form); + + if ($form->_single) { + $cancelURL = CRM_Utils_System::url( + 'civicrm/contact/view', + "reset=1&cid={$form->_cid}&selectedChild=activity", + FALSE, + NULL, + FALSE + ); + if ($form->get('action') == CRM_Core_Action::VIEW) { + $form->addButtons(array( + array( + 'type' => 'cancel', + 'name' => ts('Done'), + 'js' => array('onclick' => "location.href='{$cancelURL}'; return false;"), + ), + ) + ); + } + else { + $form->addButtons(array( + array( + 'type' => 'submit', + 'name' => ts('Make PDF Letter'), + 'isDefault' => TRUE, + ), + array( + 'type' => 'cancel', + 'name' => ts('Done'), + 'js' => array('onclick' => "location.href='{$cancelURL}'; return false;"), + ), + ) + ); + } + } + else { + $form->addDefaultButtons(ts('Make PDF Letters')); + } + + $form->addFormRule(array('CRM_Contact_Form_Task_PDFLatexCommon', 'formRule'), $form); + } + + /** + * Set default values + */ + static function setDefaultValues() { + $defaultFormat = CRM_Core_BAO_PdfFormat::getDefaultValues(); + $defaultFormat['format_id'] = $defaultFormat['id']; + return $defaultFormat; + } + + /** + * form rule + * + * @param array $fields the input form values + * @param array $dontCare + * @param array $self additional values form 'this' + * + * @return true if no errors, else array of errors + * @access public + * + */ + static function formRule($fields, $dontCare, $self) { + $errors = array(); + $template = CRM_Core_Smarty::singleton(); + + //Added for CRM-1393 + if (CRM_Utils_Array::value('saveTemplate', $fields) && empty($fields['saveTemplateName'])) { + $errors['saveTemplateName'] = ts("Enter name to save message template"); + } + if (!is_numeric($fields['margin_left'])) { + $errors['margin_left'] = 'Margin must be numeric'; + } + if (!is_numeric($fields['margin_right'])) { + $errors['margin_right'] = 'Margin must be numeric'; + } + if (!is_numeric($fields['margin_top'])) { + $errors['margin_top'] = 'Margin must be numeric'; + } + if (!is_numeric($fields['margin_bottom'])) { + $errors['margin_bottom'] = 'Margin must be numeric'; + } + return empty($errors) ? TRUE : $errors; + } + + /** + * part of the post process which prepare and extract information from the template + * + * @access protected + * + * @return array( $categories, $html_message, $messageToken, $returnProperties ) + */ + static protected function processMessageTemplate(&$form) { + $formValues = $form->controller->exportValues($form->getName()); + + // process message template + if (CRM_Utils_Array::value('saveTemplate', $formValues) || CRM_Utils_Array::value('updateTemplate', $formValues)) { + $messageTemplate = array( + 'msg_text' => NULL, + 'msg_html' => $formValues['html_message'], + 'msg_subject' => NULL, + 'is_active' => TRUE, + ); + + $messageTemplate['pdf_format_id'] = 'null'; + if (CRM_Utils_Array::value('bind_format', $formValues) && $formValues['format_id'] > 0) { + $messageTemplate['pdf_format_id'] = $formValues['format_id']; + } + if (CRM_Utils_Array::value('saveTemplate', $formValues) && $formValues['saveTemplate']) { + $messageTemplate['msg_title'] = $formValues['saveTemplateName']; + CRM_Core_BAO_MessageTemplate::add($messageTemplate); + } + + if (CRM_Utils_Array::value('updateTemplate', $formValues) && $formValues['template'] && $formValues['updateTemplate']) { + $messageTemplate['id'] = $formValues['template']; + + unset($messageTemplate['msg_title']); + CRM_Core_BAO_MessageTemplate::add($messageTemplate); + } + } + elseif (CRM_Utils_Array::value('template', $formValues) > 0) { + if (CRM_Utils_Array::value('bind_format', $formValues) && $formValues['format_id'] > 0) { + $query = "UPDATE civicrm_msg_template SET pdf_format_id = {$formValues['format_id']} WHERE id = {$formValues['template']}"; + } + else { + $query = "UPDATE civicrm_msg_template SET pdf_format_id = NULL WHERE id = {$formValues['template']}"; + } + CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray); + } + if (CRM_Utils_Array::value('update_format', $formValues)) { + $bao = new CRM_Core_BAO_PdfFormat(); + $bao->savePdfFormat($formValues, $formValues['format_id']); + } + + $html = array(); + + $tokens = array(); + CRM_Utils_Hook::tokens($tokens); + $categories = array_keys($tokens); + + $html_message = $formValues['html_message']; + + //time being hack to strip ' ' + //from particular letter line, CRM-6798 + self::formatMessage($html_message); + + $messageToken = CRM_Utils_Token::getTokens($html_message); + + $returnProperties = array(); + if (isset($messageToken['contact'])) { + foreach ($messageToken['contact'] as $key => $value) { + $returnProperties[$value] = 1; + } + } + + return array($formValues, $categories, $html_message, $messageToken, $returnProperties); + } + + /** + * process the form after the input has been submitted and validated + * + * @access public + * + * @return None + */ + static function postProcess(&$form) { + list($formValues, $categories, $html_message, $messageToken, $returnProperties) = self::processMessageTemplate($form); + + $skipOnHold = isset($form->skipOnHold) ? $form->skipOnHold : FALSE; + $skipDeceased = isset($form->skipDeceased) ? $form->skipDeceased : TRUE; + + foreach ($form->_contactIds as $item => $contactId) { + $params = array('contact_id' => $contactId); + + list($contact) = CRM_Utils_Token::getTokenDetails($params, + $returnProperties, + $skipOnHold, + $skipDeceased, + NULL, + $messageToken, + 'CRM_Contact_Form_Task_PDFLatexCommon' + ); + if (civicrm_error($contact)) { + $notSent[] = $contactId; + continue; + } + + $tokenHtml = CRM_Utils_Token::replaceContactTokens($html_message, $contact[$contactId], TRUE, $messageToken); + $tokenHtml = CRM_Utils_Token::replaceHookTokens($tokenHtml, $contact[$contactId], $categories, TRUE); + + if (defined('CIVICRM_MAIL_SMARTY') && CIVICRM_MAIL_SMARTY) { + $smarty = CRM_Core_Smarty::singleton(); + // also add the contact tokens to the template + $smarty->assign_by_ref('contact', $contact); + $tokenHtml = $smarty->fetch("string:$tokenHtml"); + } + + $html[] = $tokenHtml; + } + + self::createActivities($form, $html_message, $form->_contactIds); + + CRM_Utils_PDF_Utils::latex2pdf($html, "CiviLatex.pdf", FALSE, $formValues); + + $form->postProcessHook(); + + CRM_Utils_System::civiExit(1); + } + + function createActivities($form, $html_message, $contactIds) { + + $session = CRM_Core_Session::singleton(); + $userID = $session->get('userID'); + $activityTypeID = CRM_Core_OptionGroup::getValue('activity_type', + 'Print PDF LaTeX Letter', + 'name' + ); + $activityParams = array( + 'source_contact_id' => $userID, + 'activity_type_id' => $activityTypeID, + 'activity_date_time' => date('YmdHis'), + 'details' => $html_message, + ); + if (!empty($form->_activityId)) { + $activityParams += array('id' => $form->_activityId); + } + if ($form->_cid) { + $activity = CRM_Activity_BAO_Activity::create($activityParams); + } + else { + // create Print PDF activity for each selected contact. CRM-6886 + $activityIds = array(); + foreach ($contactIds as $contactId) { + $activityID = CRM_Activity_BAO_Activity::create($activityParams); + $activityIds[$contactId] = $activityID->id; + } + } + + foreach ($form->_contactIds as $contactId) { + $activityTargetParams = array( + 'contact_id' => $contactId, + 'activity_id' => empty($activity->id) ? $activityIds[$contactId] : $activity->id, + 'target_contact_id' => $contactId, + ); + CRM_Activity_BAO_ActivityTarget::create($activityTargetParams); + } + } + + function formatMessage(&$message) { + $newLineOperators = array( + 'p' => array( + 'oper' => '

', + 'pattern' => '/<(\s+)?p(\s+)?>/m', + ), + 'br' => array( + 'oper' => '
', + 'pattern' => '/<(\s+)?br(\s+)?\/>/m', + ), + ); + + $htmlMsg = preg_split($newLineOperators['p']['pattern'], $message); + foreach ($htmlMsg as $k => & $m) { + $messages = preg_split($newLineOperators['br']['pattern'], $m); + foreach ($messages as $key => & $msg) { + $msg = trim($msg); + $matches = array(); + if (preg_match('/^( )+/', $msg, $matches)) { + $spaceLen = strlen($matches[0]) / 6; + $trimMsg = ltrim($msg, '  '); + $charLen = strlen($trimMsg); + $totalLen = $charLen + $spaceLen; + if ($totalLen > 100) { + $spacesCount = 10; + if ($spaceLen > 50) { + $spacesCount = 20; + } + if ($charLen > 100) { + $spacesCount = 1; + } + $msg = str_repeat(' ', $spacesCount) . $trimMsg; + } + } + } + $m = implode($newLineOperators['br']['oper'], $messages); + } + $message = implode($newLineOperators['p']['oper'], $htmlMsg); + } +} + diff --git a/CRM/Contribute/Form/Task/PDFLatex.php b/CRM/Contribute/Form/Task/PDFLatex.php new file mode 100644 index 0000000000..f353e00c4e --- /dev/null +++ b/CRM/Contribute/Form/Task/PDFLatex.php @@ -0,0 +1,147 @@ +skipOnHold = $this->skipDeceased = FALSE; + CRM_Contact_Form_Task_PDFLetterCommon::preProcess($this); + + // store case id if present + $this->_caseId = CRM_Utils_Request::retrieve('caseid', 'Positive', $this, FALSE); + + // retrieve contact ID if this is 'single' mode + $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this, FALSE); + + $this->_activityId = CRM_Utils_Request::retrieve('id', 'Positive', $this, FALSE); + + if ($cid) { + CRM_Contact_Form_Task_PDFLetterCommon::preProcessSingle($this, $cid); + $this->_single = TRUE; + $this->_cid = $cid; + } + else { + parent::preProcess(); + } + $this->assign('single', $this->_single); + } + + function setDefaultValues() { + $defaults = array(); + if (isset($this->_activityId)) { + $params = array('id' => $this->_activityId); + CRM_Activity_BAO_Activity::retrieve($params, $defaults); + $defaults['html_message'] = $defaults['details']; + } + $defaults = $defaults + CRM_Contact_Form_Task_PDFLetterCommon::setDefaultValues(); + return $defaults; + } + + /** + * Build the form + * + * @access public + * + * @return void + */ + public function buildQuickForm() { + //enable form element + $this->assign('suppressForm', FALSE); + + // use contact form as a base + CRM_Contact_Form_Task_PDFLetterCommon::buildQuickForm($this); + + // specific need for contributions + $this->add('static', 'more_options_header', NULL, ts('Record Update Options')); + $this->add('checkbox', 'receipt_update', ts('Update receipt dates for these contributions'), FALSE); + $this->add('checkbox', 'thankyou_update', ts('Update thank-you dates for these contributions'), FALSE); + + // Group options for tokens are not yet implemented. dgg + $options = array(ts('Contact'), ts('Recurring')); + $this->addRadio('is_group_by', ts('Grouping contributions in one letter based on'), $options, array(), "
", FALSE); + + $this->addButtons(array( + array( + 'type' => 'submit', + 'name' => ts('Make Thank-you Letters'), + 'isDefault' => TRUE, + ), + array( + 'type' => 'cancel', + 'name' => ts('Done'), + ), + ) + ); + + } + + /** + * process the form after the input has been submitted and validated + * + * @access public + * + * @return None + */ + public function postProcess() { + // TODO: rewrite using contribution token and one letter by contribution + $this->setContactIDs(); + + CRM_Contribute_Form_Task_PDFLatexCommon::postProcess($this); + } + + +} + diff --git a/CRM/Contribute/Form/Task/PDFLatexCommon.php b/CRM/Contribute/Form/Task/PDFLatexCommon.php new file mode 100644 index 0000000000..862b03f279 --- /dev/null +++ b/CRM/Contribute/Form/Task/PDFLatexCommon.php @@ -0,0 +1,116 @@ +skipOnHold) ? $form->skipOnHold : FALSE; + $skipDeceased = isset($form->skipDeceased) ? $form->skipDeceased : TRUE; + + foreach ($form->getVar('_contributionIds') as $item => $contributionId) { + + // get contact information + $contactId = civicrm_api("Contribution", "getvalue", array('version' => '3', 'id' => $contributionId, 'return' => 'contact_id')); + $params = array('contact_id' => $contactId); + + list($contact) = CRM_Utils_Token::getTokenDetails($params, + $returnProperties, + $skipOnHold, + $skipDeceased, + NULL, + $messageToken, + 'CRM_Contribution_Form_Task_PDFLetterCommon' + ); + if (civicrm_error($contact)) { + $notSent[] = $contributionId; + continue; + } + + // get contribution information + $params = array('contribution_id' => $contributionId); + $contribution = CRM_Utils_Token::getContributionTokenDetails($params, + $returnProperties, + NULL, + $messageToken, + 'CRM_Contribution_Form_Task_PDFLetterCommon' + ); + if (civicrm_error($contribution)) { + $notSent[] = $contributionId; + continue; + } + + $tokenHtml = CRM_Utils_Token::replaceContactTokens($html_message, $contact[$contactId], FALSE, $messageToken); + $tokenHtml = CRM_Utils_Token::replaceContributionTokens($tokenHtml, $contribution[$contributionId], TRUE, $messageToken); + $tokenHtml = CRM_Utils_Token::replaceHookTokens($tokenHtml, $contact[$contactId], $categories, TRUE); + + if (defined('CIVICRM_MAIL_SMARTY') && CIVICRM_MAIL_SMARTY) { + $smarty = CRM_Core_Smarty::singleton(); + // also add the tokens to the template + $smarty->assign_by_ref('contact', $contact); + $tokenHtml = $smarty->fetch("string:$tokenHtml"); + } + + $html[] = $tokenHtml; + + // update dates (do it for each contribution including grouped recurring contribution) + if ($receipt_update) { + $result = CRM_Core_DAO::setFieldValue('CRM_Contribute_DAO_Contribution', $contributionId, 'receipt_date', $nowDate); + // We can't use CRM_Core_Error::fatal here because the api error elevates the exception level. FIXME. dgg + if ($result) { + $receipts++; + } + } + if ($thankyou_update) { + $result = CRM_Core_DAO::setFieldValue('CRM_Contribute_DAO_Contribution', $contributionId, 'thankyou_date', $nowDate); + // We can't use CRM_Core_Error::fatal here because the api error elevates the exception level. FIXME. dgg + if ($result) { + $thanks++; + } + } + } + + self::createActivities($form, $html_message, $form->_contactIds); + + CRM_Utils_PDF_Utils::latex2pdf($html, "CiviLatex.pdf", FALSE, $formValues); + + $form->postProcessHook(); + + if ($receipts) { + $updateStatus = ts('Receipt date has been updated for %1 contributions.', array(1 => $receipts)); + } + if ($thanks) { + $updateStatus .= ' ' . ts('Thank-you date has been updated for %1 contributions.', array(1 => $thanks)); + } + + if ($updateStatus) { + CRM_Core_Session::setStatus($updateStatus); + } + + CRM_Utils_System::civiExit(1); + } + //end of function +} +