Merge pull request #3943 from civicrm/master
[civicrm-core.git] / CRM / Event / Form / Participant.php
index 8ecbe28f56feb2d31b0e63f2e18abac3815bdafd..ef44f134932292c7e8f5184d86dc8d2b32f69754 100644 (file)
@@ -192,6 +192,12 @@ class CRM_Event_Form_Participant extends CRM_Contact_Form_Task {
    */
   public $_paymentId = NULL;
 
+  /**
+   * @todo add explanatory note about this
+   * @var null
+   */
+  public $_onlinePendingContributionId = NULL;
+
   /**
    * Function to set variables up before form is built
    *
@@ -212,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
@@ -413,11 +421,12 @@ class CRM_Event_Form_Participant extends CRM_Contact_Form_Task {
 
     // when custom data is included in this page
     if (!empty($_POST['hidden_custom'])) {
-      //custom data of type participant role
+      // Custom data of type participant role
+      // Note: Some earlier commits imply $_POST['role_id'] could be a comma separated string,
+      //       not sure if that ever really happens
       if (!empty($_POST['role_id'])) {
-        foreach ($_POST['role_id'] as $k => $val) {
-          $roleID = $val;
-          CRM_Custom_Form_CustomData::preProcess($this, $this->_roleCustomDataTypeID, $k, 1, 'Participant', $this->_id);
+        foreach ($_POST['role_id'] as $roleID) {
+          CRM_Custom_Form_CustomData::preProcess($this, $this->_roleCustomDataTypeID, $roleID, 1, 'Participant', $this->_id);
           CRM_Custom_Form_CustomData::buildQuickForm($this);
           CRM_Custom_Form_CustomData::setDefaultValues($this);
         }
@@ -554,7 +563,7 @@ class CRM_Event_Form_Participant extends CRM_Contact_Form_Task {
         $roleID = $submittedRole[0];
       }
       $submittedEvent = $this->getElementValue('event_id');
-      if ($submittedEvent[0]) {
+      if (!empty($submittedEvent[0])) {
         $eventID = $submittedEvent[0];
       }
     }
@@ -867,7 +876,11 @@ class CRM_Event_Form_Participant extends CRM_Contact_Form_Task {
   /**
    * global validation rules for the form
    *
-   * @param array $fields posted values of the form
+   * @param $values
+   * @param $files
+   * @param $self
+   *
+   * @internal param array $fields posted values of the form
    *
    * @return array list of errors to be posted back to the form
    * @static
@@ -920,6 +933,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;
   }
 
@@ -951,6 +979,41 @@ 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) {
+      if(!$this->_single && !empty($this->_eventId)) {
+        $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 = $this->_eventId;
+          $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
@@ -1083,15 +1146,17 @@ class CRM_Event_Form_Participant extends CRM_Contact_Form_Task {
       list($this->_contributorDisplayName, $this->_contributorEmail, $this->_toDoNotEmail) = CRM_Contact_BAO_Contact::getContactDetails($this->_contactId);
     }
 
+    //modify params according to parameter used in create
+    //participant method (addParticipant)
+    $this->_params['participant_status_id'] = $params['status_id'];
+    $this->_params['participant_role_id'] = is_array($params['role_id']) ? $params['role_id'] : explode(',', $params['role_id']);
+    $this->_params['participant_register_date'] = $params['register_date'];
+    $roleIdWithSeparator = implode(CRM_Core_DAO::VALUE_SEPARATOR, $this->_params['participant_role_id']);
+
     if ($this->_mode) {
       if (!$this->_isPaidEvent) {
         CRM_Core_Error::fatal(ts('Selected Event is not Paid Event '));
       }
-      //modify params according to parameter used in create
-      //participant method (addParticipant)
-      $this->_params['participant_status_id'] = $params['status_id'];
-      $this->_params['participant_role_id'] = explode(',', $params['role_id']);
-      $this->_params['participant_register_date'] = $params['register_date'];
 
       $eventTitle =
         CRM_Core_DAO::getFieldValue(
@@ -1149,32 +1214,34 @@ class CRM_Event_Form_Participant extends CRM_Contact_Form_Task {
       $contactID = CRM_Contact_BAO_Contact::createProfileContact($params, $fields, $this->_contactId, NULL, NULL, $ctype);
     }
 
-    if ($this->_params['role_id']) {
-      foreach ($this->_params['role_id'] as $rkey) {
-        $customFieldsRole = CRM_Core_BAO_CustomField::getFields('Participant', FALSE, FALSE, $rkey, $this->_roleCustomDataTypeID);
-        $customFieldsEvent = CRM_Core_BAO_CustomField::getFields('Participant',
-          FALSE,
-          FALSE,
-          CRM_Utils_Array::value('event_id', $params),
-          $this->_eventNameCustomDataTypeID
-        );
-        $customFieldsEventType = CRM_Core_BAO_CustomField::getFields('Participant',
-          FALSE,
-          FALSE,
-          $this->_eventTypeId,
-          $this->_eventTypeCustomDataTypeID
-        );
-        $customFields = CRM_Utils_Array::crmArrayMerge($customFieldsRole,
-          CRM_Core_BAO_CustomField::getFields('Participant', FALSE, FALSE, NULL, NULL, TRUE)
-        );
-        $customFields     = CRM_Utils_Array::crmArrayMerge($customFieldsEvent, $customFields);
-        $customFields     = CRM_Utils_Array::crmArrayMerge($customFieldsEventType, $customFields);
-        $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params,
-          $customFields,
-          $this->_id,
-          'Participant'
-        );
+    if (!empty($this->_params['participant_role_id'])) {
+      $customFieldsRole = array();
+      foreach ($this->_params['participant_role_id'] as $roleKey) {
+        $customFieldsRole = CRM_Utils_Array::crmArrayMerge(CRM_Core_BAO_CustomField::getFields('Participant',
+          FALSE, FALSE, $roleKey, $this->_roleCustomDataTypeID), $customFieldsRole);
       }
+      $customFieldsEvent = CRM_Core_BAO_CustomField::getFields('Participant',
+        FALSE,
+        FALSE,
+        CRM_Utils_Array::value('event_id', $params),
+        $this->_eventNameCustomDataTypeID
+      );
+      $customFieldsEventType = CRM_Core_BAO_CustomField::getFields('Participant',
+        FALSE,
+        FALSE,
+        $this->_eventTypeId,
+        $this->_eventTypeCustomDataTypeID
+      );
+      $customFields = CRM_Utils_Array::crmArrayMerge($customFieldsRole,
+        CRM_Core_BAO_CustomField::getFields('Participant', FALSE, FALSE, NULL, NULL, TRUE)
+      );
+      $customFields = CRM_Utils_Array::crmArrayMerge($customFieldsEvent, $customFields);
+      $customFields = CRM_Utils_Array::crmArrayMerge($customFieldsEventType, $customFields);
+      $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params,
+        $customFields,
+        $this->_id,
+        'Participant'
+      );
     }
 
     //do cleanup line  items if participant edit the Event Fee.
@@ -1273,7 +1340,7 @@ class CRM_Event_Form_Participant extends CRM_Contact_Form_Task {
       $participants = array();
       if ($this->_single) {
         if ($params['role_id']) {
-          $params['role_id'] = str_replace(',', CRM_Core_DAO::VALUE_SEPARATOR, $params['role_id']);
+          $params['role_id'] = $roleIdWithSeparator;
         }
         else {
           $params['role_id'] = 'NULL';
@@ -1472,7 +1539,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);
@@ -1645,6 +1712,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;
             }