CRM-15428 - CiviEvent - Add participants to a group
authorStan Dragnev <sdragnev@rnao.org>
Tue, 21 Oct 2014 14:27:08 +0000 (10:27 -0400)
committerStan Dragnev <sdragnev@rnao.org>
Tue, 21 Oct 2014 14:27:08 +0000 (10:27 -0400)
Adds the ability to add participants records to a group via the actions dropdown on a participants list page.
https://issues.civicrm.org/jira/browse/CRM-15428

CRM/Event/Form/Task/AddToGroup.php [new file with mode: 0644]
CRM/Event/Task.php
templates/CRM/Event/Form/Task/AddToGroup.tpl [new file with mode: 0644]

diff --git a/CRM/Event/Form/Task/AddToGroup.php b/CRM/Event/Form/Task/AddToGroup.php
new file mode 100644 (file)
index 0000000..2594f2d
--- /dev/null
@@ -0,0 +1,251 @@
+<?php
+/*
+ +--------------------------------------------------------------------+
+ | CiviCRM version 4.4                                                |
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC (c) 2004-2013                                |
+ +--------------------------------------------------------------------+
+ | This file is a part of CiviCRM.                                    |
+ |                                                                    |
+ | CiviCRM is free software; you can copy, modify, and distribute it  |
+ | under the terms of the GNU Affero General Public License           |
+ | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
+ |                                                                    |
+ | CiviCRM is distributed in the hope that it will be useful, but     |
+ | WITHOUT ANY WARRANTY; without even the implied warranty of         |
+ | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
+ | See the GNU Affero General Public License for more details.        |
+ |                                                                    |
+ | You should have received a copy of the GNU Affero General Public   |
+ | License and the CiviCRM Licensing Exception along                  |
+ | with this program; if not, contact CiviCRM LLC                     |
+ | at info[AT]civicrm[DOT]org. If you have questions about the        |
+ | GNU Affero General Public License or the licensing of CiviCRM,     |
+ | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
+ +--------------------------------------------------------------------+
+*/
+
+/**
+ *
+ * @package CRM
+ * @copyright CiviCRM LLC (c) 2004-2013
+ * $Id$
+ *
+ */
+
+/**
+ * This class provides the functionality to group
+ * contacts. This class provides functionality for the actual
+ * addition of contacts to groups.
+ */
+class CRM_Event_Form_Task_AddToGroup extends CRM_Event_Form_Task {
+
+  /**
+   * The context that we are working on
+   *
+   * @var string
+   */
+  protected $_context;
+
+  /**
+   * the groupId retrieved from the GET vars
+   *
+   * @var int
+   */
+  protected $_id;
+
+  /**
+   * the title of the group
+   *
+   * @var string
+   */
+  protected $_title;
+
+  /**
+   * build all the data structures needed to build the form
+   *
+   * @return void
+   * @access public
+   */
+  function preProcess() {
+    /*
+     * initialize the task and row fields
+     */
+    parent::preProcess();
+
+    parent::setContactIDs();
+    $this->_context = $this->get('context');
+    $this->_id = $this->get('amtgID');
+  }
+
+  /**
+   * Build the form
+   *
+   * @access public
+   *
+   * @return void
+   */
+  function buildQuickForm() {
+
+    //create radio buttons to select existing group or add a new group
+    $options = array(ts('Add Contact To Existing Group'), ts('Create New Group'));
+
+    if (!$this->_id) {
+      $this->addRadio('group_option', ts('Group Options'), $options, array('onclick' => "return showElements();"));
+
+      $this->add('text', 'title', ts('Group Name:') . ' ',
+        CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Group', 'title')
+      );
+      $this->addRule('title', ts('Name already exists in Database.'),
+        'objectExists', array('CRM_Contact_DAO_Group', $this->_id, 'title')
+      );
+
+      $this->add('textarea', 'description', ts('Description:') . ' ',
+        CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Group', 'description')
+      );
+
+      $groupTypes = CRM_Core_OptionGroup::values('group_type', TRUE);
+      if (!CRM_Core_Permission::access('CiviMail')) {
+        $isWorkFlowEnabled = CRM_Mailing_Info::workflowEnabled();
+        if ($isWorkFlowEnabled &&
+          !CRM_Core_Permission::check('create mailings') &&
+          !CRM_Core_Permission::check('schedule mailings') &&
+          !CRM_Core_Permission::check('approve mailings')
+        ) {
+          unset($groupTypes['Mailing List']);
+        }
+      }
+
+      if (!empty($groupTypes)) {
+        $this->addCheckBox('group_type',
+          ts('Group Type'),
+          $groupTypes,
+          NULL, NULL, NULL, NULL, '&nbsp;&nbsp;&nbsp;'
+        );
+      }
+    }
+
+    // add select for groups
+    $group = array('' => ts('- select group -')) + CRM_Core_PseudoConstant::group();
+
+    $groupElement = $this->add('select', 'group_id', ts('Select Group'), $group);
+
+    $this->_title = $group[$this->_id];
+
+    if ($this->_context === 'amtg') {
+      $groupElement->freeze();
+
+      // also set the group title
+      $groupValues = array('id' => $this->_id, 'title' => $this->_title);
+      $this->assign_by_ref('group', $groupValues);
+    }
+
+    // Set dynamic page title for 'Add Members Group (confirm)'
+    if ($this->_id) {
+      CRM_Utils_System::setTitle(ts('Add Contacts: %1', array(1 => $this->_title)));
+    }
+    else {
+      CRM_Utils_System::setTitle(ts('Add Contacts to A Group'));
+    }
+
+    $this->addDefaultButtons(ts('Add to Group'));
+  }
+
+  /**
+   * Set the default form values
+   *
+   * @access protected
+   *
+   * @return array the default array reference
+   */
+  function setDefaultValues() {
+    $defaults = array();
+
+    if ($this->_context === 'amtg') {
+      $defaults['group_id'] = $this->_id;
+    }
+
+    $defaults['group_option'] = 0;
+    return $defaults;
+  }
+
+  /**
+   * Add local and global form rules
+   *
+   * @access protected
+   *
+   * @return void
+   */
+  function addRules() {
+    $this->addFormRule(array('CRM_Event_Form_Task_AddToGroup', 'formRule'));
+  }
+
+  /**
+   * global validation rules for the form
+   *
+   * @param array $fields posted values of the form
+   *
+   * @return array list of errors to be posted back to the form
+   * @static
+   * @access public
+   */
+  static function formRule($params) {
+    $errors = array();
+
+    if (!empty($params['group_option']) && empty($params['title'])) {
+      $errors['title'] = "Group Name is a required field";
+    }
+    elseif (empty($params['group_option']) && empty($params['group_id'])) {
+      $errors['group_id'] = "Select Group is a required field.";
+    }
+
+    return empty($errors) ? TRUE : $errors;
+  }
+
+  /**
+   * process the form after the input has been submitted and validated
+   *
+   * @access public
+   *
+   * @return None
+   */
+  public function postProcess() {
+    $params = $this->controller->exportValues();
+    $groupOption = CRM_Utils_Array::value('group_option', $params, NULL);
+    if ($groupOption) {
+      $groupParams = array();
+      $groupParams['title'] = $params['title'];
+      $groupParams['description'] = $params['description'];
+      $groupParams['visibility'] = "User and User Admin Only";
+      if (array_key_exists('group_type', $params) && is_array($params['group_type'])) {
+        $groupParams['group_type'] = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR,
+          array_keys($params['group_type'])
+        ) . CRM_Core_DAO::VALUE_SEPARATOR;
+      }
+      else {
+        $groupParams['group_type'] = '';
+      }
+      $groupParams['is_active'] = 1;
+
+      $createdGroup = CRM_Contact_BAO_Group::create($groupParams);
+      $groupID      = $createdGroup->id;
+      $groupName    = $groupParams['title'];
+    }
+    else {
+      $groupID   = $params['group_id'];
+      $group     = CRM_Core_PseudoConstant::group();
+      $groupName = $group[$groupID];
+    }
+
+    list($total, $added, $notAdded) = CRM_Contact_BAO_GroupContact::addContactsToGroup($this->_contactIds, $groupID);
+
+    $status = array(ts('%count contact added to group', array('count' => $added, 'plural' => '%count contacts added to group')));
+    if ($notAdded) {
+      $status[] = ts('%count contact was already in group', array('count' => $notAdded, 'plural' => '%count contacts were already in group'));
+    }
+    $status = '<ul><li>' . implode('</li><li>', $status) . '</li></ul>';
+    CRM_Core_Session::setStatus($status, ts('Added Contact to %1', array(1 => $groupName, 'count' => $added, 'plural' => 'Added Contacts to %1')), 'success', array('expires' => 0));
+  }
+  //end of function
+}
+
index ad3f9caad0f976ea50b0ca3b60616776caea0a36..36a76706dce960c904dbe4f52c692cf7aed3a3f2 100644 (file)
@@ -43,7 +43,7 @@ class CRM_Event_Task {
   CONST DELETE_EVENTS = 1, PRINT_EVENTS = 2, EXPORT_EVENTS = 3, BATCH_EVENTS = 4, CANCEL_REGISTRATION = 5, EMAIL_CONTACTS = 6,
   // Value for LABEL_CONTACTS is set to 16 in accordance with CRM_Contact_Task::LABEL_CONTACTS
   SAVE_SEARCH = 13, SAVE_SEARCH_UPDATE = 14, PARTICIPANT_STATUS = 15,
-  LABEL_CONTACTS = 16;
+  LABEL_CONTACTS = 16, GROUP_CONTACTS = 20;
 
   /**
    * the task array
@@ -127,6 +127,11 @@ class CRM_Event_Task {
           'class' => 'CRM_Event_Form_Task_Badge',
           'result' => FALSE,
         ),
+        20 => array(
+          'title' => ts('Add Contacts to Group'),
+          'class' => 'CRM_Event_Form_Task_AddToGroup',
+          'result' => FALSE,
+        ),
       );
 
       //CRM-4418, check for delete
diff --git a/templates/CRM/Event/Form/Task/AddToGroup.tpl b/templates/CRM/Event/Form/Task/AddToGroup.tpl
new file mode 100644 (file)
index 0000000..8dbefa7
--- /dev/null
@@ -0,0 +1,85 @@
+{*
+ +--------------------------------------------------------------------+
+ | CiviCRM version 4.5                                                |
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC (c) 2004-2014                                |
+ +--------------------------------------------------------------------+
+ | This file is a part of CiviCRM.                                    |
+ |                                                                    |
+ | CiviCRM is free software; you can copy, modify, and distribute it  |
+ | under the terms of the GNU Affero General Public License           |
+ | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
+ |                                                                    |
+ | CiviCRM is distributed in the hope that it will be useful, but     |
+ | WITHOUT ANY WARRANTY; without even the implied warranty of         |
+ | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
+ | See the GNU Affero General Public License for more details.        |
+ |                                                                    |
+ | You should have received a copy of the GNU Affero General Public   |
+ | License and the CiviCRM Licensing Exception along                  |
+ | with this program; if not, contact CiviCRM LLC                     |
+ | at info[AT]civicrm[DOT]org. If you have questions about the        |
+ | GNU Affero General Public License or the licensing of CiviCRM,     |
+ | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
+ +--------------------------------------------------------------------+
+*}
+<div class="crm-block crm-form-block crm-contact-task-addtogroup-form-block">
+<table class="form-layout">
+    {if $group.id}
+       <tr class="crm-contact-task-addtogroup-form-block-group_id">
+          <td class="label">{ts}Group{/ts}</td>
+          <td>{$form.group_id.html}</td>
+       </tr>
+    {else}
+        <tr><td>{$form.group_option.html}</td></tr>
+        <tr id="id_existing_group">
+            <td>
+                <table class="form-layout">
+                <tr><td class="label">{$form.group_id.label}<span class="marker">*</span></td><td>{$form.group_id.html}</td></tr>
+                </table>
+            </td>
+        </tr>
+        <tr id="id_new_group" class="html-adjust">
+            <td>
+                <table class="form-layout">
+                <tr class="crm-contact-task-addtogroup-form-block-title">
+                   <td class="label">{$form.title.label}<span class="marker">*</span></td>
+                   <td>{$form.title.html}</td>
+                <tr>
+                <tr class="crm-contact-task-addtogroup-form-block-description">
+                   <td class="label">{$form.description.label}</td>
+                   <td>{$form.description.html}</td></tr>
+                {if $form.group_type}
+                <tr class="crm-contact-task-addtogroup-form-block-group_type">
+        <td class="label">{$form.group_type.label}</td>
+                    <td>{$form.group_type.html}</td>
+                </tr>
+                {/if}
+                </table>
+            </td>
+        </tr>
+    {/if}
+</table>
+<table class="form-layout">
+        <tr><td>{include file="CRM/Event/Form/Task.tpl"}</td></tr>
+</table>
+<div class="crm-submit-buttons">{include file="CRM/common/formButtons.tpl" location="bottom"}</div>
+</div>
+{include file="CRM/common/showHide.tpl"}
+
+{if !$group.id}
+{literal}
+<script type="text/javascript">
+showElements();
+function showElements() {
+    if ( document.getElementsByName('group_option')[0].checked ) {
+      cj('#id_existing_group').show();
+      cj('#id_new_group').hide();
+    } else {
+      cj('#id_new_group').show();
+      cj('#id_existing_group').hide();
+    }
+}
+</script>
+{/literal}
+{/if}