Change participant role to select2
authorColeman Watts <coleman@civicrm.org>
Sun, 27 Apr 2014 05:11:39 +0000 (22:11 -0700)
committerColeman Watts <coleman@civicrm.org>
Sun, 27 Apr 2014 05:11:39 +0000 (22:11 -0700)
CRM/Event/Form/Participant.php
templates/CRM/Event/Form/Participant.tpl
xml/schema/Event/Participant.xml

index e1709c4b9e500f0897eb98b8cec960983f5819c9..75f6cfaec3669630e11844f48056169d51efd113 100644 (file)
@@ -192,11 +192,6 @@ class CRM_Event_Form_Participant extends CRM_Contact_Form_Task {
    */
   public $_paymentId = NULL;
 
-  /**
-   * array of participant role custom data
-   */
-  public $_participantRoleIds = array();
-
   /**
    * Function to set variables up before form is built
    *
@@ -459,52 +454,6 @@ class CRM_Event_Form_Participant extends CRM_Contact_Form_Task {
       );
     }
     $this->set('onlinePendingContributionId', $this->_onlinePendingContributionId);
-    $roleIds = CRM_Event_PseudoConstant::participantRole();
-    if (!empty($roleIds)) {
-      $query = "
-SELECT civicrm_custom_group.name as name,
-       civicrm_custom_group.id as id,
-       extends_entity_column_value as value
-  FROM civicrm_custom_group
- WHERE ( extends_entity_column_value REGEXP '[[:<:]]" . implode('[[:>:]]|[[:<:]]', array_keys($roleIds)) . "[[:>:]]'
-    OR extends_entity_column_value IS NULL )
-   AND extends_entity_column_id = '{$this->_roleCustomDataTypeID}'
-   AND extends = 'Participant'
-   AND is_active = 1";
-
-      $dao = CRM_Core_DAO::executeQuery($query);
-      while ($dao->fetch()) {
-        if ($dao->value) {
-          $getRole = explode(CRM_Core_DAO::VALUE_SEPARATOR, $dao->value);
-          foreach ($getRole as $r) {
-            if (!$r) {
-              continue;
-            }
-            if (isset($this->_participantRoleIds[$r])) {
-              $this->_participantRoleIds[$r] .= ',' . $dao->name;
-            }
-            else {
-              $this->_participantRoleIds[$r] = $dao->name;
-            }
-          }
-        }
-        else {
-          if (isset($this->_participantRoleIds[0])) {
-            $this->_participantRoleIds[0] .= ',' . $dao->name;
-          }
-          else {
-            $this->_participantRoleIds[0] = $dao->name;
-          }
-        }
-      }
-      $dao->free();
-    }
-    foreach ($roleIds as $k => $v) {
-      if (!isset($this->_participantRoleIds[$k])) {
-        $this->_participantRoleIds[$k] = '';
-      }
-    }
-    $this->assign('participantRoleIds', $this->_participantRoleIds);
   }
 
   /**
@@ -666,11 +615,7 @@ SELECT civicrm_custom_group.name as name,
       $roleIDs = explode(',', $urlRoleIDS);
     }
     if (isset($roleIDs)) {
-      foreach ($roleIDs as $roleID) {
-        $defaults[$this->_id]["role_id[{$roleID}]"] = 1;
-      }
-      unset($defaults[$this->_id]['role_id']);
-      $this->assign('roleID', $roleIDs);
+      $defaults[$this->_id]['role_id'] = implode(',', $roleIDs);
     }
 
     if (isset($eventID)) {
@@ -822,14 +767,7 @@ SELECT civicrm_custom_group.name as name,
       $this->assign('entityID', $this->_id);
     }
 
-    $roleids = CRM_Event_PseudoConstant::participantRole();
-
-    foreach ($roleids as $rolekey => $rolevalue) {
-      $roleTypes[] = $this->createElement('checkbox', $rolekey, NULL, $rolevalue);
-    }
-
-    $this->addGroup($roleTypes, 'role_id', ts('Participant Role'));
-    $this->addRule('role_id', ts('Role is required'), 'required');
+    $this->addSelect('role_id', array('multiple' => TRUE, 'class' => 'huge'), TRUE);
 
     // CRM-4395
     $checkCancelledJs = array('onchange' => "return sendNotification( );");
@@ -1154,7 +1092,7 @@ SELECT civicrm_custom_group.name as name,
       //modify params according to parameter used in create
       //participant method (addParticipant)
       $this->_params['participant_status_id'] = $params['status_id'];
-      $this->_params['participant_role_id'] = $params['role_id'];
+      $this->_params['participant_role_id'] = explode(',', $params['role_id']);
       $this->_params['participant_register_date'] = $params['register_date'];
 
       $eventTitle =
@@ -1213,9 +1151,8 @@ SELECT civicrm_custom_group.name as name,
       $contactID = CRM_Contact_BAO_Contact::createProfileContact($params, $fields, $this->_contactId, NULL, NULL, $ctype);
     }
 
-    $roleAllIds = CRM_Utils_Array::value('role_id', $params);
-    if ($roleAllIds) {
-      foreach ($roleAllIds as $rkey => $rvalue) {
+    if ($this->_params['participant_role_id']) {
+      foreach ($this->_params['participant_role_id'] as $rkey) {
         $customFieldsRole = CRM_Core_BAO_CustomField::getFields('Participant', FALSE, FALSE, $rkey, $this->_roleCustomDataTypeID);
         $customFieldsEvent = CRM_Core_BAO_CustomField::getFields('Participant',
           FALSE,
@@ -1312,7 +1249,7 @@ SELECT civicrm_custom_group.name as name,
       $participants = array();
       if (!empty($this->_params['participant_role_id']) && is_array($this->_params['participant_role_id'])) {
         $this->_params['participant_role_id'] = implode(CRM_Core_DAO::VALUE_SEPARATOR,
-          array_keys($this->_params['participant_role_id'])
+          $this->_params['participant_role_id']
         );
       }
       $participants[] = CRM_Event_Form_Registration::addParticipant($this->_params, $contactID);
@@ -1338,11 +1275,7 @@ SELECT civicrm_custom_group.name as name,
       $participants = array();
       if ($this->_single) {
         if ($params['role_id']) {
-          foreach ($params['role_id'] as $k => $v) {
-            $rolesIDS[] = $k;
-          }
-          $seperator = CRM_Core_DAO::VALUE_SEPARATOR;
-          $params['role_id'] = implode($seperator, $rolesIDS);
+          $params['role_id'] = str_replace(',', CRM_Core_DAO::VALUE_SEPARATOR, $params['role_id']);
         }
         else {
           $params['role_id'] = 'NULL';
@@ -1354,13 +1287,7 @@ SELECT civicrm_custom_group.name as name,
           $commonParams = $params;
           $commonParams['contact_id'] = $contactID;
           if ($commonParams['role_id']) {
-            $rolesIDS = array();
-            foreach ($commonParams['role_id'] as $k => $v) {
-              $rolesIDS[] = $k;
-            }
-            $seperator = CRM_Core_DAO::VALUE_SEPARATOR;
-            $commonParams['role_id'] = implode($seperator, $rolesIDS);
-            $commonParams['participant_role_id'] = implode($seperator, $rolesIDS);
+            $commonParams['participant_role_id'] = $commonParams['role_id'] = str_replace(',', CRM_Core_DAO::VALUE_SEPARATOR, $params['role_id']);
           }
           else {
             $commonParams['role_id'] = 'NULL';
index 4b76db50d84247250f6b15ec96462d795edba5f0..fae24aa2f2c44abcbd6206df1a9eaca83c482998 100644 (file)
           var info = $(this).select2('data').extra;
 
           // Set role from default
-          $('[name^=role_id]:checkbox', $form).each(function() {
-            var check = $(this).is('[name="role_id[' + info.default_role_id + ']"]');
-            if (check !== $(this).prop('checked')) {
-              $(this).prop('checked', check).change();
-            }
-          });
+          $('select[name^=role_id]', $form).select2('val', [info.default_role_id], true);
 
           // Set campaign default
           $('#campaign_id', $form).select2('val', info.campaign_id);
         });
 
         // Handle participant role selection
-        $('[name^=role_id]:checkbox', $form).change(function() {
-          var roleId = $(this).attr('name').replace(/role_id\[|\]/g, '');
-          showCustomData('Participant', roleId, {/literal}{$roleCustomDataTypeID}{literal});
-        });
-        $('[name^=role_id]:checked', $form).change();
+        $('select[name^=role_id]', $form).change(buildRoleCustomData);
+        if ($('select[name^=role_id]', $form).val()) {
+          buildRoleCustomData();
+        }
 
         buildFeeBlock();
 
           buildFeeBlock($('#discount_id', $form).val());
         }
 
+        function buildRoleCustomData() {
+          var roleId = $('select[name^=role_id]', $form).select2('val').join();
+          CRM.buildCustomData('Participant', roleId, {/literal}{$roleCustomDataTypeID}{literal});
+        }
+
         //build fee block
         function buildFeeBlock(discountId)  {
           var dataUrl = {/literal}"{crmURL p=$urlPath h=0 q="snippet=4&qfKey=$qfKey"}";
 
           $.ajax({
             url: dataUrl,
-            async: false,
-            global: false,
             success: function ( html ) {
               $("#feeBlock").html( html ).trigger('crmLoad');
             }
           }
         }
 
-        var roleGroupMapper = {/literal}{$participantRoleIds|@json_encode}{literal};
-        function showCustomData( type, subType, subName ) {
-          var dataUrl = {/literal}"{crmURL p=$urlPath h=0 q='snippet=4&type='}"{literal} + type;
-          var roleid = "role_id_"+subType;
-          var loadData = false;
-
-          if ( document.getElementById( roleid ).checked == true ) {
-            if ( typeof roleGroupMapper !== 'undefined' && roleGroupMapper[subType] ) {
-              var splitGroup = roleGroupMapper[subType].split(",");
-              for ( i = 0; i < splitGroup.length; i++ ) {
-                var roleCustomGroupId = splitGroup[i];
-                if ( $( '#'+roleCustomGroupId ).length > 0 ) {
-                  $( '#'+roleCustomGroupId ).remove( );
-                }
-              }
-              loadData = true;
-            }
-          }
-          else {
-            var groupUnload = [];
-            var x = 0;
-
-            if ( roleGroupMapper[0] ) {
-              var splitGroup = roleGroupMapper[0].split(",");
-              for ( x = 0; x < splitGroup.length; x++ ) {
-                groupUnload[x] = splitGroup[x];
-              }
-            }
-
-            for ( var i in roleGroupMapper ) {
-              if ( ( i > 0 ) && ( document.getElementById( "role_id_"+i ).checked ) ) {
-                var splitGroup = roleGroupMapper[i].split(",");
-                for ( j = 0; j < splitGroup.length; j++ ) {
-                  groupUnload[x+j+1] = splitGroup[j];
-                }
-              }
-            }
-
-            if ( roleGroupMapper[subType] ) {
-              var splitGroup = roleGroupMapper[subType].split(",");
-              for ( i = 0; i < splitGroup.length; i++ ) {
-                var roleCustomGroupId = splitGroup[i];
-                if ( $( '#'+roleCustomGroupId ).length > 0 ) {
-                  if ( $.inArray( roleCustomGroupId, groupUnload ) == -1  ) {
-                    $( '#'+roleCustomGroupId ).remove( );
-                  }
-                }
-              }
-            }
-          }
-
-          if ( !( loadData ) ) {
-            return false;
-          }
-
-          if ( subType ) {
-            dataUrl += '&subType=' + subType;
-          }
-
-          if ( subName ) {
-            dataUrl += '&subName=' + subName;
-            $( '#customData' + subName ).show( );
-          }
-          else {
-            $( '#customData' ).show( );
-          }
-
-          {/literal}
-          {if $urlPathVar}
-            dataUrl += '&{$urlPathVar}';
-          {/if}
-          {if $groupID}
-            dataUrl += '&groupID=' + '{$groupID}';
-          {/if}
-          {if $qfKey}
-            dataUrl += '&qfKey=' + '{$qfKey}';
-          {/if}
-          {if $entityID}
-            dataUrl += '&entityID=' + '{$entityID}';
-          {/if}
-
-          {literal}
-
-          if ( subName && subName != 'null' ) {
-            var fname = '#customData' + subName;
-          }
-          else {
-            var fname = '#customData';
-          }
-
-          var response = $.ajax({url: dataUrl,
-            async: false
-          }).responseText;
-
-          if ( subType != 'null' ) {
-            if ( document.getElementById(roleid).checked == true ) {
-              var response_text = '<div style="display:block;" id = '+subType+'_chk >'+response+'</div>';
-              $( fname ).append(response_text).trigger('crmLoad');
-            }
-            else {
-              $('#'+subType+'_chk').remove();
-            }
-          }
-        }
-
         {/literal}
         CRM.buildCustomData( '{$customDataType}', 'null', 'null' );
         {if $eventID}
index 6677e5fb582fcf632f93aeaf24fe3c3016b3d0f2..e76bca9db94923fb4816a71b66125bad001573fa 100644 (file)
@@ -89,7 +89,7 @@
   <field>
       <name>role_id</name>
       <uniqueName>participant_role_id</uniqueName>
-      <title>Participant Role Id</title>
+      <title>Participant Role(s)</title>
       <pseudoconstant>
         <optionGroupName>participant_role</optionGroupName>
       </pseudoconstant>