Additional QA Fix for CRM-15427
[civicrm-core.git] / CRM / Event / Form / Participant.php
index cf82ce510b4e3860039085d9b209e8943ac644ac..69ba3d965554e1861a61b40d2731f8be38a5b33f 100644 (file)
@@ -218,8 +218,10 @@ class CRM_Event_Form_Participant extends CRM_Contact_Form_Task {
     if ($this->_contactId) {
       $displayName = CRM_Contact_BAO_Contact::displayName($this->_contactId);
       $this->assign('displayName', $displayName);
-      // omitting contactImage from title for now since the summary overlay css doesn't work outside of our crm-container
-      CRM_Utils_System::setTitle(ts('Event Registration for') .  ' ' . $displayName);
+      $this->setPageTitle(ts('Event Registration for %1', array(1 => $displayName)));
+    }
+    else {
+      $this->setPageTitle(ts('Event Registration'));
     }
 
     // check the current path, if search based, then dont get participantID
@@ -694,39 +696,9 @@ class CRM_Event_Form_Participant extends CRM_Contact_Form_Task {
     }
 
 
-    if ($this->_single) {
-      $urlPath = 'civicrm/contact/view/participant';
-      $urlParams = "reset=1&cid={$this->_contactId}&context=participant";
-      if ($this->_context == 'standalone') {
-        $this->addEntityRef('contact_id', ts('Contact'), array('create' => TRUE), TRUE);
-        $urlParams = 'reset=1&context=standalone';
-        $urlPath = 'civicrm/participant/add';
-      }
-
-      if ($this->_id) {
-        $urlParams .= "&action=update&id={$this->_id}";
-      }
-      else {
-        $urlParams .= "&action=add";
-      }
-
-      if ($this->_mode) {
-        $urlParams .= "&mode={$this->_mode}";
-      }
-
-      $url = CRM_Utils_System::url($urlPath, $urlParams,
-        FALSE, NULL, FALSE
-      );
+    if ($this->_single && $this->_context == 'standalone') {
+      $this->addEntityRef('contact_id', ts('Contact'), array('create' => TRUE, 'api' => array('extra' => array('email'))), TRUE);
     }
-    else {
-      $currentPath = CRM_Utils_System::currentPath();
-
-      $url = CRM_Utils_System::url($currentPath, '_qf_Participant_display=true',
-        FALSE, NULL, FALSE
-      );
-    }
-
-    $this->assign('refreshURL', $url);
 
     $eventFieldParams = array(
       'entity' => 'event',
@@ -931,6 +903,21 @@ class CRM_Event_Form_Participant extends CRM_Contact_Form_Task {
         CRM_Price_BAO_PriceField::priceSetValidation($priceSetId, $values, $errorMsg, TRUE);
       }
     }
+    // For single additions - show validation error if the contact has already been registered
+    // for this event with the same role.
+    if($self->_single && ($self->_action & CRM_Core_Action::ADD)) {
+      $contactId = $self->_contactId;
+      $eventId = CRM_Utils_Array::value('event_id', $values);
+      if(!empty($contactId) && !empty($eventId)) {
+        $dupeCheck = new CRM_Event_BAO_Participant;
+        $dupeCheck->contact_id = $contactId;
+        $dupeCheck->event_id = $eventId;
+        $dupeCheck->find(TRUE);
+        if(!empty($dupeCheck->id)) {
+          $errorMsg['event_id'] = ts("This contact has already been assigned to this event.");
+        }
+      }
+    }
     return CRM_Utils_Array::crmIsEmptyArray($errorMsg) ? TRUE : $errorMsg;
   }
 
@@ -962,6 +949,45 @@ class CRM_Event_Form_Participant extends CRM_Contact_Form_Task {
       }
       return;
     }
+    // When adding a single contact, the formRule prevents you from adding duplicates
+    // (See above in formRule()). When adding more than one contact, the duplicates are
+    // removed automatically and the user receives one notification.
+    if ($this->_action & CRM_Core_Action::ADD) {
+      $event_id = $this->_eventId;
+      if(empty($event_id) && !empty($params['event_id'])) {
+        $event_id = $params['event_id'];
+      }
+      if(!$this->_single && !empty($event_id)) {
+        $duplicateContacts = 0;
+        while(list($k,$dupeCheckContactId) = each($this->_contactIds)) {
+          // Eliminate contacts that have already been assigned to this event.
+          $dupeCheck = new CRM_Event_BAO_Participant;
+          $dupeCheck->contact_id = $dupeCheckContactId;
+          $dupeCheck->event_id = $event_id;
+          $dupeCheck->find(TRUE);
+          if(!empty($dupeCheck->id)) {
+            $duplicateContacts++;
+            unset($this->_contactIds[$k]);
+          }
+        }
+        if($duplicateContacts > 0) {
+          $msg = ts(
+            "%1 contacts have already been assigned to this event. They were not added a second time.",
+            array(1 => $duplicateContacts)
+          );
+          CRM_Core_Session::setStatus($msg);
+        }
+        if(count($this->_contactIds) == 0) {
+          CRM_Core_Session::setStatus(ts("No participants were added."));
+          return;
+        }
+        // We have to re-key $this->_contactIds so each contact has the same
+        // key as their corresponding record in the $participants array that
+        // will be created below.
+        $this->_contactIds = array_values($this->_contactIds);
+      }
+    }
+
 
     $participantStatus = CRM_Event_PseudoConstant::participantStatus();
     // set the contact, when contact is selected
@@ -1222,7 +1248,7 @@ class CRM_Event_Form_Participant extends CRM_Contact_Form_Task {
 
       $payment = CRM_Core_Payment::singleton($this->_mode, $this->_paymentProcessor, $this);
 
-      $result = &$payment->doDirectPayment($paymentParams);
+      $result = $payment->doDirectPayment($paymentParams);
 
       if (is_a($result, 'CRM_Core_Error')) {
         CRM_Core_Error::displaySessionError($result);
@@ -1265,7 +1291,11 @@ class CRM_Event_Form_Participant extends CRM_Contact_Form_Task {
           $this->_params['role_id']
         );
       }
-      $participants[] = CRM_Event_Form_Registration::addParticipant($this->_params, $contactID);
+      
+      //CRM-15372 patch to fix fee amount replacing amount
+      $this->_params['fee_amount'] =  $this->_params['amount'];
+
+      $participants[] = CRM_Event_Form_Registration::addParticipant($this, $contactID);
 
       //add custom data for participant
       CRM_Core_BAO_CustomValueTable::postProcess($this->_params,
@@ -1369,6 +1399,9 @@ class CRM_Event_Form_Participant extends CRM_Contact_Form_Task {
         if ($contributionParams['contribution_status_id'] == CRM_Core_OptionGroup::getValue('contribution_status', 'Pending', 'name')) {
           $contributionParams['is_pay_later'] = 1;
         }
+        elseif ($contributionParams['contribution_status_id'] == CRM_Core_OptionGroup::getValue('contribution_status', 'Completed', 'name')) {
+               $contributionParams['is_pay_later'] = 0;
+             }
 
         if ($params['status_id'] == array_search('Partially paid', $participantStatus)) {
           if (!$amountOwed && $this->_action & CRM_Core_Action::UPDATE) {
@@ -1487,7 +1520,7 @@ class CRM_Event_Form_Participant extends CRM_Contact_Form_Task {
       $participantRoles = CRM_Utils_Array::value('role_id', $params);
       if (is_array($participantRoles)) {
         $selectedRoles = array();
-        foreach (array_keys($participantRoles) as $roleId) {
+        foreach ($participantRoles as $roleId) {
           $selectedRoles[] = $role[$roleId];
         }
         $event['participant_role'] = implode(', ', $selectedRoles);
@@ -1660,6 +1693,7 @@ class CRM_Event_Form_Participant extends CRM_Contact_Form_Task {
           $sent[] = $contactID;
           foreach ($participants as $ids => $values) {
             if ($values->contact_id == $contactID) {
+              $values->details = CRM_Utils_Array::value('receipt_text', $params);
               CRM_Activity_BAO_Activity::addActivity($values, 'Email');
               break;
             }