form rule fixes and js cleanup
authorkurund <kurund@civicrm.org>
Fri, 8 Aug 2014 14:01:58 +0000 (19:31 +0530)
committerkurund <kurund@civicrm.org>
Fri, 8 Aug 2014 14:01:58 +0000 (19:31 +0530)
CRM/Contribute/Form/Task/Invoice.php
CRM/Contribute/Form/Task/PDF.php
templates/CRM/Contribute/Form/Task/Invoice.tpl

index 757165e07631be9d63f1446e72aa6085063a5382..b7f9da96d8f0862ce3c43e4bc872f3076bfff503 100644 (file)
@@ -66,6 +66,11 @@ class CRM_Contribute_Form_Task_Invoice extends CRM_Contribute_Form_Task {
    */
   public $_invoiceTemplate;
 
+  /**
+   * selected output
+   */
+  public $_selectedOutput;
+
   /**
    * build all the data structures needed to build the form
    *
@@ -119,7 +124,11 @@ class CRM_Contribute_Form_Task_Invoice extends CRM_Contribute_Form_Task {
     );
 
     CRM_Utils_System::appendBreadCrumb($breadCrumb);
-    if (in_array("email", $this->urlPath)) {
+
+    $this->_selectedOutput = CRM_Utils_Request::retrieve('select', 'String', $this);
+    $this->assign('selectedOutput', $this->_selectedOutput);
+
+    if ($this->_selectedOutput == 'email') {
       CRM_Utils_System::setTitle(ts('Email Invoice'));
     }
     else {
@@ -163,11 +172,17 @@ class CRM_Contribute_Form_Task_Invoice extends CRM_Contribute_Form_Task {
       $fromEmailAddress[$key] = htmlspecialchars($fromEmailAddress[$key]);
     }
     $fromEmail = CRM_Utils_Array::crmArrayMerge($emails, $fromEmailAddress);
-
-    $this->addElement('radio', 'output', NULL, ts('Email Invoice'), 'email_invoice');
-    $this->addElement('radio', 'output', NULL, ts('PDF Invoice'), 'pdf_invoice');
     $this->add('select', 'from_email_address', ts('From Email Address'), array('' => '- select -') + $fromEmail);
-    $this->addFormRule(array('CRM_Contribute_Form_Task_Invoice', 'formRule'));
+    if ($this->_selectedOutput != 'email') {
+      $this->addElement('radio', 'output', NULL, ts('Email Invoice'), 'email_invoice');
+      $this->addElement('radio', 'output', NULL, ts('PDF Invoice'), 'pdf_invoice');
+      $this->addRule('output', ts('Selection required'), 'required');
+      $this->addFormRule(array('CRM_Contribute_Form_Task_Invoice', 'formRule'));
+    }
+    else {
+      $this->addRule('from_email_address', ts('From Email Address is required'), 'required');
+    }
+
     $this->addWysiwyg('email_comment', ts('If you would like to add personal message to email please add it here. (If sending to more then one receipient the same message will be sent to each contact.)'), array(
       'rows' => 2,
       'cols' => 40
@@ -216,19 +231,11 @@ class CRM_Contribute_Form_Task_Invoice extends CRM_Contribute_Form_Task {
    */
   static function formRule($values) {
     $errors = array();
-    if (!$values['from_email_address']) {
-      if (in_array("Email Invoice", $values)) {
-        $errors['from_email_address'] = ts("From Email Address is required");
-      }
-      else {
-        if (in_array("email_invoice", $values)) {
-          $errors['from_email_address'] = ts("From Email Address is required");
-        }
-        else {
-          $errors['output'] = ts("Selection required");
-        }
-      }
+
+    if ($values['output'] == 'email_invoice' && empty($values['from_email_address'])) {
+      $errors['from_email_address'] = ts("From Email Address is required");
     }
+
     return $errors;
   }
 
index 33f6eafac62a342e7037e75859ad00d3046190a5..e3cd688a7dd6e184a383c0463f20d0e686351a46 100644 (file)
@@ -256,7 +256,8 @@ AND    {$this->_componentClause}";
     $pdfElements['params'] = $params;
 
     $pdfElements['createPdf'] = FALSE;
-    if ($pdfElements['params']['output'] == "pdf_invoice" || $pdfElements['params']['output'] == "pdf_receipt") {
+    if (!empty($pdfElements['params']['output']) &&
+      ($pdfElements['params']['output'] == "pdf_invoice" || $pdfElements['params']['output'] == "pdf_receipt")) {
       $pdfElements['createPdf'] = TRUE;
     }
 
@@ -271,6 +272,7 @@ AND    {$this->_componentClause}";
 
       list($contactDetails) = CRM_Utils_Token::getTokenDetails($contactIds, $returnProperties, FALSE, FALSE);
       $pdfElements['suppressedEmails'] = 0;
+      $suppressedEmails = 0;
       foreach ($contactDetails as $id => $values) {
         if (empty($values['email']) || !empty($values['do_not_email']) ||
             CRM_Utils_Array::value('is_deceased', $values) || !empty($values['on_hold'])) {
index 72222ddefd0fc914e1e34c9f8004111e696b8acd..cfb5fe470aa2d7a064b7957a34125260eaca0e54 100644 (file)
 *}
 <div class="messages status no-popup">
   <div class="icon inform-icon"></div>
-      {include file="CRM/Contribute/Form/Task.tpl"}
+  {include file="CRM/Contribute/Form/Task.tpl"}
 </div>
-{if $smarty.get.select != 'email' || $smarty.get.q != 'civicrm/contribute/invoice/email'}
- <div id="help">
-    {ts}You may choose to email invoice to contributors OR download a PDF file containing one invoice per page to your local computer by clicking <strong>Process Invoice(s)</strong>. Your browser may display the file for you automatically, or you may need to open it for printing using any PDF reader (such as Adobe&reg; Reader).{/ts}
- </div>
+{if $selectedOutput ne 'email'}
+  <div id="help">
+    {ts}You may choose to email invoice to contributors OR download a PDF file containing one invoice per page to your local computer by clicking
+      <strong>Process Invoice(s)</strong>
+      . Your browser may display the file for you automatically, or you may need to open it for printing using any PDF reader (such as Adobe&reg; Reader).{/ts}
+  </div>
 {/if}
 
-<table class="form-layout-compressed">  
-  {if $smarty.get.select == 'email' || $smarty.get.q == 'civicrm/contribute/invoice/email'}
-    {literal}
-      <script type="text/javascript">
-        cj(document).ready(function() {
-         cj('#emailId').show();
-         cj('#comment').show();
-        }) 
-      </script>
-    {/literal}
-  {/if}
-
-    {literal}
-      <script type="text/javascript">
-        cj(document).ready(function() {
-         if(cj('input:radio[name=output]:checked').val() == 'email_invoice') {
-           cj('#selectPdfFormat').hide();
-           cj('#emailId').show();
-            cj('#comment').show();
-         }
-          cj('#CIVICRM_QFID_email_invoice_2').click(function() {
-            cj('#selectPdfFormat').hide();
-           cj('#emailId').show();
-            cj('#comment').show();
-         });
-        });
-      </script>
-    {/literal}
-
-  {if $smarty.get.select != 'email' && $smarty.get.q != 'civicrm/contribute/invoice/email'}
+<table class="form-layout-compressed">
+  {if $selectedOutput ne 'email'}
     <tr>
       <td>{$form.output.email_invoice.html}</td>
     </tr>
   {/if}
-  <tr id="emailId" style="display:none;">
+  <tr class="crm-email-element">
     <td>{$form.from_email_address.label}{$form.from_email_address.html}{help id ="id-from_email" isAdmin=$isAdmin}</td>
   </tr>
-  <tr id="comment" style="display:none;">
+  <tr class="crm-email-element">
     <td>{$form.email_comment.label}{$form.email_comment.html}</td>
   </tr>
-  {if $smarty.get.select != 'email' && $smarty.get.q != 'civicrm/contribute/invoice/email'}
+  {if $selectedOutput ne 'email'}
     <tr>
       <td>{$form.output.pdf_invoice.html}</td>
     </tr>
   {/if}
-  <tr id="selectPdfFormat" style="display: none;">
+  <tr class="crm-pdf-element">
     <td>{$form.pdf_format_id.html} {$form.pdf_format_id.label} </td>
   </tr>
 </table>
 
 <div class="spacer"></div>
 <div class="form-item">
- {$form.buttons.html}
 {$form.buttons.html}
 </div>
+
+
+<script type="text/javascript">
+  {literal}
+  CRM.$(function ($) {
+    var o = $('input[name="output"]');
+    if (o.length > 1) {
+      $('.crm-email-element').hide();
+      showhideEmailElements();
+      o.on('click', showhideEmailElements);
+    }
+    else {
+      $('.crm-email-element').show();
+    }
+
+    function showhideEmailElements() {
+      if ($('input[name="output"]:checked').val() == 'email_invoice') {
+        $('.crm-email-element').show();
+        $('.crm-pdf-element').hide();
+      }
+      else {
+        $('.crm-pdf-element').show();
+        $('.crm-email-element').hide();
+      }
+    }
+  });
+  {/literal}
+</script>
+