Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | CiviCRM version 4.3 | | |
5 | +--------------------------------------------------------------------+ | |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
7 | +--------------------------------------------------------------------+ | |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
31 | * @copyright CiviCRM LLC (c) 2004-2013 | |
32 | * $Id$ | |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * This class provides the common functionality for creating PDF letter for | |
38 | * one or a group of contact ids. | |
39 | */ | |
40 | class CRM_Contact_Form_Task_PDFLetterCommon { | |
41 | ||
42 | /** | |
43 | * build all the data structures needed to build the form | |
44 | * | |
45 | * @return void | |
46 | * @access public | |
47 | */ | |
48 | static function preProcess(&$form) { | |
49 | $messageText = array(); | |
50 | $messageSubject = array(); | |
51 | $dao = new CRM_Core_BAO_MessageTemplates(); | |
52 | $dao->is_active = 1; | |
53 | $dao->find(); | |
54 | while ($dao->fetch()) { | |
55 | $messageText[$dao->id] = $dao->msg_text; | |
56 | $messageSubject[$dao->id] = $dao->msg_subject; | |
57 | } | |
58 | ||
59 | $form->assign('message', $messageText); | |
60 | $form->assign('messageSubject', $messageSubject); | |
61 | } | |
62 | ||
63 | static function preProcessSingle(&$form, $cid) { | |
64 | $form->_contactIds = array($cid); | |
65 | // put contact display name in title for single contact mode | |
66 | CRM_Contact_Page_View::setTitle($cid); | |
67 | } | |
68 | ||
69 | /** | |
70 | * Build the form | |
71 | * | |
72 | * @access public | |
73 | * | |
74 | * @return void | |
75 | */ | |
76 | static function buildQuickForm(&$form) { | |
4c71ccb4 RN |
77 | //Added for CRM-12682: Add activity subject and campaign fields |
78 | CRM_Campaign_BAO_Campaign::addCampaign($form); | |
79 | $form->add( | |
80 | 'text', | |
81 | 'subject', | |
82 | ts('Activity Subject'), | |
83 | array('size' => 45, 'maxlength' => 255), | |
84 | FALSE | |
85 | ); | |
86 | ||
6a488035 | 87 | $form->add('static', 'pdf_format_header', NULL, ts('Page Format')); |
ed106721 DL |
88 | $form->add( |
89 | 'select', | |
90 | 'format_id', | |
91 | ts('Select Format'), | |
92 | array(0 => ts('- default -')) + CRM_Core_BAO_PdfFormat::getList(TRUE), | |
93 | FALSE, | |
6a488035 | 94 | array('onChange' => "selectFormat( this.value, false );") |
ed106721 DL |
95 | );; |
96 | $form->add( | |
97 | 'select', | |
98 | 'paper_size', | |
99 | ts('Paper Size'), | |
100 | array(0 => ts('- default -')) + CRM_Core_BAO_PaperSize::getList(TRUE), | |
101 | FALSE, | |
6a488035 TO |
102 | array('onChange' => "selectPaper( this.value ); showUpdateFormatChkBox();") |
103 | ); | |
104 | $form->add('static', 'paper_dimensions', NULL, ts('Width x Height')); | |
ed106721 DL |
105 | $form->add( |
106 | 'select', | |
107 | 'orientation', | |
108 | ts('Orientation'), | |
109 | CRM_Core_BAO_PdfFormat::getPageOrientations(), | |
110 | FALSE, | |
6a488035 TO |
111 | array('onChange' => "updatePaperDimensions(); showUpdateFormatChkBox();") |
112 | ); | |
ed106721 DL |
113 | $form->add( |
114 | 'select', | |
115 | 'metric', | |
116 | ts('Unit of Measure'), | |
117 | CRM_Core_BAO_PdfFormat::getUnits(), | |
118 | FALSE, | |
6a488035 TO |
119 | array('onChange' => "selectMetric( this.value );") |
120 | ); | |
ed106721 DL |
121 | $form->add( |
122 | 'text', | |
123 | 'margin_left', | |
124 | ts('Left Margin'), | |
125 | array('size' => 8, 'maxlength' => 8, 'onkeyup' => "showUpdateFormatChkBox();"), | |
126 | TRUE | |
6a488035 | 127 | ); |
ed106721 DL |
128 | $form->add( |
129 | 'text', | |
130 | 'margin_right', | |
131 | ts('Right Margin'), | |
132 | array('size' => 8, 'maxlength' => 8, 'onkeyup' => "showUpdateFormatChkBox();"), | |
133 | TRUE | |
6a488035 | 134 | ); |
ed106721 DL |
135 | $form->add( |
136 | 'text', | |
137 | 'margin_top', | |
138 | ts('Top Margin'), | |
139 | array('size' => 8, 'maxlength' => 8, 'onkeyup' => "showUpdateFormatChkBox();"), | |
140 | TRUE | |
6a488035 | 141 | ); |
ed106721 DL |
142 | $form->add( |
143 | 'text', | |
144 | 'margin_bottom', | |
145 | ts('Bottom Margin'), | |
146 | array('size' => 8, 'maxlength' => 8, 'onkeyup' => "showUpdateFormatChkBox();"), | |
147 | TRUE | |
6a488035 TO |
148 | ); |
149 | $form->add('checkbox', 'bind_format', ts('Always use this Page Format with the selected Template')); | |
150 | $form->add('checkbox', 'update_format', ts('Update Page Format (this will affect all templates that use this format)')); | |
151 | ||
152 | $form->assign('useThisPageFormat', ts('Always use this Page Format with the new template?')); | |
153 | $form->assign('useSelectedPageFormat', ts('Should the new template always use the selected Page Format?')); | |
154 | $form->assign('totalSelectedContacts', count($form->_contactIds)); | |
155 | ||
156 | CRM_Mailing_BAO_Mailing::commonLetterCompose($form); | |
157 | ||
158 | if ($form->_single) { | |
ed106721 DL |
159 | $cancelURL = CRM_Utils_System::url( |
160 | 'civicrm/contact/view', | |
6a488035 | 161 | "reset=1&cid={$form->_cid}&selectedChild=activity", |
ed106721 DL |
162 | FALSE, |
163 | NULL, | |
164 | FALSE | |
6a488035 TO |
165 | ); |
166 | if ($form->get('action') == CRM_Core_Action::VIEW) { | |
167 | $form->addButtons(array( | |
168 | array( | |
169 | 'type' => 'cancel', | |
170 | 'name' => ts('Done'), | |
171 | 'js' => array('onclick' => "location.href='{$cancelURL}'; return false;"), | |
172 | ), | |
173 | ) | |
174 | ); | |
175 | } | |
176 | else { | |
177 | $form->addButtons(array( | |
178 | array( | |
179 | 'type' => 'submit', | |
180 | 'name' => ts('Make PDF Letter'), | |
181 | 'isDefault' => TRUE, | |
182 | ), | |
183 | array( | |
184 | 'type' => 'cancel', | |
185 | 'name' => ts('Done'), | |
186 | 'js' => array('onclick' => "location.href='{$cancelURL}'; return false;"), | |
187 | ), | |
188 | ) | |
189 | ); | |
190 | } | |
191 | } | |
192 | else { | |
193 | $form->addDefaultButtons(ts('Make PDF Letters')); | |
194 | } | |
195 | ||
196 | $form->addFormRule(array('CRM_Contact_Form_Task_PDFLetterCommon', 'formRule'), $form); | |
197 | } | |
198 | ||
199 | /** | |
200 | * Set default values | |
201 | */ | |
202 | static function setDefaultValues() { | |
203 | $defaultFormat = CRM_Core_BAO_PdfFormat::getDefaultValues(); | |
204 | $defaultFormat['format_id'] = $defaultFormat['id']; | |
205 | return $defaultFormat; | |
206 | } | |
207 | ||
208 | /** | |
209 | * form rule | |
210 | * | |
211 | * @param array $fields the input form values | |
212 | * @param array $dontCare | |
213 | * @param array $self additional values form 'this' | |
214 | * | |
215 | * @return true if no errors, else array of errors | |
216 | * @access public | |
217 | * | |
218 | */ | |
219 | static function formRule($fields, $dontCare, $self) { | |
220 | $errors = array(); | |
221 | $template = CRM_Core_Smarty::singleton(); | |
222 | ||
223 | //Added for CRM-1393 | |
224 | if (CRM_Utils_Array::value('saveTemplate', $fields) && empty($fields['saveTemplateName'])) { | |
225 | $errors['saveTemplateName'] = ts("Enter name to save message template"); | |
226 | } | |
227 | if (!is_numeric($fields['margin_left'])) { | |
228 | $errors['margin_left'] = 'Margin must be numeric'; | |
229 | } | |
230 | if (!is_numeric($fields['margin_right'])) { | |
231 | $errors['margin_right'] = 'Margin must be numeric'; | |
232 | } | |
233 | if (!is_numeric($fields['margin_top'])) { | |
234 | $errors['margin_top'] = 'Margin must be numeric'; | |
235 | } | |
236 | if (!is_numeric($fields['margin_bottom'])) { | |
237 | $errors['margin_bottom'] = 'Margin must be numeric'; | |
238 | } | |
239 | return empty($errors) ? TRUE : $errors; | |
240 | } | |
241 | ||
242 | /** | |
243 | * part of the post process which prepare and extract information from the template | |
244 | * | |
245 | * @access protected | |
246 | * | |
247 | * @return array( $categories, $html_message, $messageToken, $returnProperties ) | |
248 | */ | |
249 | static protected function processMessageTemplate(&$form) { | |
250 | $formValues = $form->controller->exportValues($form->getName()); | |
251 | ||
252 | // process message template | |
253 | if (CRM_Utils_Array::value('saveTemplate', $formValues) || CRM_Utils_Array::value('updateTemplate', $formValues)) { | |
254 | $messageTemplate = array( | |
255 | 'msg_text' => NULL, | |
256 | 'msg_html' => $formValues['html_message'], | |
257 | 'msg_subject' => NULL, | |
258 | 'is_active' => TRUE, | |
259 | ); | |
260 | ||
261 | $messageTemplate['pdf_format_id'] = 'null'; | |
262 | if (CRM_Utils_Array::value('bind_format', $formValues) && $formValues['format_id'] > 0) { | |
263 | $messageTemplate['pdf_format_id'] = $formValues['format_id']; | |
264 | } | |
265 | if (CRM_Utils_Array::value('saveTemplate', $formValues) && $formValues['saveTemplate']) { | |
266 | $messageTemplate['msg_title'] = $formValues['saveTemplateName']; | |
267 | CRM_Core_BAO_MessageTemplates::add($messageTemplate); | |
268 | } | |
269 | ||
270 | if (CRM_Utils_Array::value('updateTemplate', $formValues) && $formValues['template'] && $formValues['updateTemplate']) { | |
271 | $messageTemplate['id'] = $formValues['template']; | |
272 | ||
273 | unset($messageTemplate['msg_title']); | |
274 | CRM_Core_BAO_MessageTemplates::add($messageTemplate); | |
275 | } | |
276 | } | |
277 | elseif (CRM_Utils_Array::value('template', $formValues) > 0) { | |
278 | if (CRM_Utils_Array::value('bind_format', $formValues) && $formValues['format_id'] > 0) { | |
279 | $query = "UPDATE civicrm_msg_template SET pdf_format_id = {$formValues['format_id']} WHERE id = {$formValues['template']}"; | |
280 | } | |
281 | else { | |
282 | $query = "UPDATE civicrm_msg_template SET pdf_format_id = NULL WHERE id = {$formValues['template']}"; | |
283 | } | |
284 | CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray); | |
285 | } | |
286 | if (CRM_Utils_Array::value('update_format', $formValues)) { | |
287 | $bao = new CRM_Core_BAO_PdfFormat(); | |
288 | $bao->savePdfFormat($formValues, $formValues['format_id']); | |
289 | } | |
290 | ||
291 | $html = array(); | |
292 | ||
293 | $tokens = array(); | |
294 | CRM_Utils_Hook::tokens($tokens); | |
295 | $categories = array_keys($tokens); | |
296 | ||
297 | $html_message = $formValues['html_message']; | |
298 | ||
299 | //time being hack to strip ' ' | |
300 | //from particular letter line, CRM-6798 | |
301 | self::formatMessage($html_message); | |
302 | ||
303 | $messageToken = CRM_Utils_Token::getTokens($html_message); | |
304 | ||
305 | $returnProperties = array(); | |
306 | if (isset($messageToken['contact'])) { | |
307 | foreach ($messageToken['contact'] as $key => $value) { | |
308 | $returnProperties[$value] = 1; | |
309 | } | |
310 | } | |
311 | ||
312 | return array($formValues, $categories, $html_message, $messageToken, $returnProperties); | |
313 | } | |
314 | ||
315 | /** | |
316 | * process the form after the input has been submitted and validated | |
317 | * | |
318 | * @access public | |
319 | * | |
320 | * @return None | |
321 | */ | |
322 | static function postProcess(&$form) { | |
323 | list($formValues, $categories, $html_message, $messageToken, $returnProperties) = self::processMessageTemplate($form); | |
324 | ||
325 | $skipOnHold = isset($form->skipOnHold) ? $form->skipOnHold : FALSE; | |
326 | $skipDeceased = isset($form->skipDeceased) ? $form->skipDeceased : TRUE; | |
327 | ||
328 | foreach ($form->_contactIds as $item => $contactId) { | |
329 | $params = array('contact_id' => $contactId); | |
330 | ||
331 | list($contact) = CRM_Utils_Token::getTokenDetails($params, | |
332 | $returnProperties, | |
333 | $skipOnHold, | |
334 | $skipDeceased, | |
335 | NULL, | |
336 | $messageToken, | |
337 | 'CRM_Contact_Form_Task_PDFLetterCommon' | |
338 | ); | |
339 | if (civicrm_error($contact)) { | |
340 | $notSent[] = $contactId; | |
341 | continue; | |
342 | } | |
343 | ||
344 | $tokenHtml = CRM_Utils_Token::replaceContactTokens($html_message, $contact[$contactId], TRUE, $messageToken); | |
345 | $tokenHtml = CRM_Utils_Token::replaceHookTokens($tokenHtml, $contact[$contactId], $categories, TRUE); | |
346 | ||
347 | if (defined('CIVICRM_MAIL_SMARTY') && CIVICRM_MAIL_SMARTY) { | |
348 | $smarty = CRM_Core_Smarty::singleton(); | |
349 | // also add the contact tokens to the template | |
350 | $smarty->assign_by_ref('contact', $contact); | |
351 | $tokenHtml = $smarty->fetch("string:$tokenHtml"); | |
352 | } | |
353 | ||
354 | $html[] = $tokenHtml; | |
355 | } | |
356 | ||
357 | self::createActivities($form, $html_message, $form->_contactIds); | |
358 | ||
359 | CRM_Utils_PDF_Utils::html2pdf($html, "CiviLetter.pdf", FALSE, $formValues); | |
360 | ||
361 | $form->postProcessHook(); | |
362 | ||
363 | CRM_Utils_System::civiExit(1); | |
364 | } | |
365 | ||
366 | function createActivities($form, $html_message, $contactIds) { | |
4c71ccb4 RN |
367 | //Added for CRM-12682: Add activity subject and campaign fields |
368 | $formValues = $form->controller->exportValues($form->getName()); | |
6a488035 TO |
369 | |
370 | $session = CRM_Core_Session::singleton(); | |
371 | $userID = $session->get('userID'); | |
1d85d241 DL |
372 | $activityTypeID = CRM_Core_OptionGroup::getValue( |
373 | 'activity_type', | |
6a488035 TO |
374 | 'Print PDF Letter', |
375 | 'name' | |
376 | ); | |
377 | $activityParams = array( | |
4c71ccb4 RN |
378 | 'subject' => $formValues['subject'], |
379 | 'campaign_id' => $formValues['campaign_id'], | |
6a488035 TO |
380 | 'source_contact_id' => $userID, |
381 | 'activity_type_id' => $activityTypeID, | |
382 | 'activity_date_time' => date('YmdHis'), | |
383 | 'details' => $html_message, | |
384 | ); | |
385 | if (!empty($form->_activityId)) { | |
386 | $activityParams += array('id' => $form->_activityId); | |
387 | } | |
388 | if ($form->_cid) { | |
389 | $activity = CRM_Activity_BAO_Activity::create($activityParams); | |
390 | } | |
391 | else { | |
392 | // create Print PDF activity for each selected contact. CRM-6886 | |
393 | $activityIds = array(); | |
394 | foreach ($contactIds as $contactId) { | |
395 | $activityID = CRM_Activity_BAO_Activity::create($activityParams); | |
396 | $activityIds[$contactId] = $activityID->id; | |
397 | } | |
398 | } | |
399 | ||
e7e657f0 | 400 | $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name'); |
9e74e3ce | 401 | $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts); |
8ef12e64 | 402 | |
6a488035 TO |
403 | foreach ($form->_contactIds as $contactId) { |
404 | $activityTargetParams = array( | |
405 | 'activity_id' => empty($activity->id) ? $activityIds[$contactId] : $activity->id, | |
1d85d241 | 406 | 'contact_id' => $contactId, |
9e74e3ce | 407 | 'record_type_id' => $targetID |
6a488035 | 408 | ); |
1d85d241 | 409 | CRM_Activity_BAO_ActivityContact::create($activityTargetParams); |
6a488035 TO |
410 | } |
411 | } | |
412 | ||
413 | function formatMessage(&$message) { | |
414 | $newLineOperators = array( | |
415 | 'p' => array( | |
416 | 'oper' => '<p>', | |
417 | 'pattern' => '/<(\s+)?p(\s+)?>/m', | |
418 | ), | |
419 | 'br' => array( | |
420 | 'oper' => '<br />', | |
421 | 'pattern' => '/<(\s+)?br(\s+)?\/>/m', | |
422 | ), | |
423 | ); | |
424 | ||
425 | $htmlMsg = preg_split($newLineOperators['p']['pattern'], $message); | |
426 | foreach ($htmlMsg as $k => & $m) { | |
427 | $messages = preg_split($newLineOperators['br']['pattern'], $m); | |
428 | foreach ($messages as $key => & $msg) { | |
429 | $msg = trim($msg); | |
430 | $matches = array(); | |
431 | if (preg_match('/^( )+/', $msg, $matches)) { | |
432 | $spaceLen = strlen($matches[0]) / 6; | |
433 | $trimMsg = ltrim($msg, ' '); | |
434 | $charLen = strlen($trimMsg); | |
435 | $totalLen = $charLen + $spaceLen; | |
436 | if ($totalLen > 100) { | |
437 | $spacesCount = 10; | |
438 | if ($spaceLen > 50) { | |
439 | $spacesCount = 20; | |
440 | } | |
441 | if ($charLen > 100) { | |
442 | $spacesCount = 1; | |
443 | } | |
444 | $msg = str_repeat(' ', $spacesCount) . $trimMsg; | |
445 | } | |
446 | } | |
447 | } | |
448 | $m = implode($newLineOperators['br']['oper'], $messages); | |
449 | } | |
450 | $message = implode($newLineOperators['p']['oper'], $htmlMsg); | |
451 | } | |
452 | } | |
453 |