CRM-13863 - Update pdf letter to use select2
authorColeman Watts <coleman@civicrm.org>
Tue, 18 Mar 2014 20:16:42 +0000 (16:16 -0400)
committerColeman Watts <coleman@civicrm.org>
Tue, 18 Mar 2014 20:16:42 +0000 (16:16 -0400)
CRM/Contact/Form/Task/PDFLetterCommon.php
templates/CRM/Contact/Form/Task/PDFLetterCommon.tpl
xml/schema/Core/MessageTemplate.xml

index add5035ca5837f200818ad033ede16606f07ba36..4fe0cfb8c30be5e10b5e8da3b590da87be526826 100644 (file)
@@ -87,15 +87,14 @@ class CRM_Contact_Form_Task_PDFLetterCommon {
       FALSE
     );
 
-    $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('static', 'pdf_format_header', NULL, ts('Page Format: %1', array(1 => '<span class="pdf-format-header-label"></span>')));
+    $form->addSelect('format_id', array(
+      'label' => ts('Select Format'),
+      'placeholder' => ts('Default'),
+      'entity' => 'message_template',
+      'field' => 'pdf_format_id',
+      'option_url' => 'civicrm/admin/pdfFormats',
+    ));
     $form->add(
       'select',
       'paper_size',
@@ -272,7 +271,7 @@ class CRM_Contact_Form_Task_PDFLetterCommon {
       );
 
       $messageTemplate['pdf_format_id'] = 'null';
-      if (!empty($formValues['bind_format']) && $formValues['format_id'] > 0) {
+      if (!empty($formValues['bind_format']) && $formValues['format_id']) {
         $messageTemplate['pdf_format_id'] = $formValues['format_id'];
       }
       if (!empty($formValues['saveTemplate']) && $formValues['saveTemplate']) {
@@ -288,7 +287,7 @@ class CRM_Contact_Form_Task_PDFLetterCommon {
       }
     }
     elseif (CRM_Utils_Array::value('template', $formValues) > 0) {
-      if (!empty($formValues['bind_format']) && $formValues['format_id'] > 0) {
+      if (!empty($formValues['bind_format']) && $formValues['format_id']) {
         $query = "UPDATE civicrm_msg_template SET pdf_format_id = {$formValues['format_id']} WHERE id = {$formValues['template']}";
       }
       else {
index 01a2f69c24a81fb6e3a0e2e02d9538b09eb5e51f..080f001f9708e2cb94098bb4c9e90b8aa8ae39e7 100644 (file)
@@ -49,9 +49,9 @@
       <div class="crm-block crm-form-block crm-pdf-format-form-block">
     <table class="form-layout-compressed">
       <tr>
-        <td class="label-left">{$form.format_id.label}</td><td>{$form.format_id.html}{help id="id-pdf-format" file="CRM/Contact/Form/Task/PDFLetterCommon.hlp"}</td>
-        <td colspan="2">&nbsp;</td>
-            </tr>
+        <td class="label-left">{$form.format_id.label} {help id="id-pdf-format" file="CRM/Contact/Form/Task/PDFLetterCommon.hlp"}</td>
+        <td>{$form.format_id.html}</td>
+      </tr>
       <tr>
         <td class="label-left">{$form.paper_size.label}</td><td>{$form.paper_size.html}</td>
         <td class="label-left">{$form.orientation.label}</td><td>{$form.orientation.html}</td>
 
 {literal}
 <script type="text/javascript">
-cj(function() {
-    cj().crmAccordions();
+cj(function($) {
+  var $form = $('form#{/literal}{$form.formName}{literal}');
+  $('#format_id', $form).on('change', function() {
+    selectFormat($(this).val());
+  });
+  $().crmAccordions();
 });
 
 var currentWidth;
@@ -133,10 +137,7 @@ function showBindFormatChkBox()
     if ( document.getElementById('template') == null || document.getElementById('template').value == '' ) {
         templateExists = false;
     }
-    var formatExists = true;
-    if ( document.getElementById('format_id').value == 0 ) {
-        formatExists = false;
-    }
+    var formatExists = !!cj('#format_id').val();
     if ( templateExists && formatExists ) {
         document.getElementById("bindFormat").style.display = "block";
     } else if ( formatExists && document.getElementById("saveTemplate") != null && document.getElementById("saveTemplate").checked ) {
@@ -153,20 +154,21 @@ function showBindFormatChkBox()
 
 function showUpdateFormatChkBox()
 {
-    if ( document.getElementById('format_id').value != 0 ) {
-        document.getElementById("updateFormat").style.display = "block";
+    if (cj('#format_id').val()) {
+      cj("#updateFormat").show();
     }
 }
 
-function hideUpdateFormatChkBox()
-{
-    document.getElementById("update_format").checked = false;
-    document.getElementById("updateFormat").style.display = "none";
+function updateFormatLabel() {
+  cj('.pdf-format-header-label').html(cj('#format_id option:selected').text() || cj('#format_id').attr('placeholder'));
 }
 
+updateFormatLabel();
+
 function selectFormat( val, bind )
 {
-    if ( val == null || val == 0 ) {
+  updateFormatLabel();
+    if (!val) {
         val = 0;
         bind = false;
     }
@@ -181,7 +183,7 @@ function selectFormat( val, bind )
         cj("#margin_left").val( data.margin_left );
         cj("#margin_right").val( data.margin_right );
         selectPaper( data.paper_size );
-        hideUpdateFormatChkBox();
+        cj("#update_format").prop({checked: false}).parent().hide();
         document.getElementById('bind_format').checked = bind;
         showBindFormatChkBox();
     }, 'json');
index 464a9d5b85201839a60b3f2fb024cfa947bea073..312a3a4014e5f62d73ee0a933ad76f0a6ef257df 100644 (file)
       <name>pdf_format_id</name>
       <type>int unsigned</type>
       <comment>FK to civicrm_option_value containing PDF Page Format.</comment>
+      <pseudoconstant>
+        <optionGroupName>pdf_format</optionGroupName>
+        <keyColumn>id</keyColumn>
+      </pseudoconstant>
       <add>3.4</add>
   </field>
   <foreignKey>