3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
18 use Civi\Api4\Membership
;
21 * Class for member form task actions.
22 * FIXME: This needs refactoring to properly inherit from CRM_Core_Form_Task and share more functions.
24 class CRM_Member_Form_Task
extends CRM_Core_Form_Task
{
27 * The array that holds all the member ids.
31 protected $_memberIds;
34 * Build all the data structures needed to build the form.
39 * @throws \CRM_Core_Exception
41 public function preProcess() {
42 self
::preProcessCommon($this);
46 * @param \CRM_Core_Form_Task $form
48 * @throws \CRM_Core_Exception
50 public static function preProcessCommon(&$form) {
51 $form->_memberIds
= [];
53 $values = $form->getSearchFormValues();
55 $form->_task
= $values['task'];
56 $tasks = CRM_Member_Task
::permissionedTaskTitles(CRM_Core_Permission
::getPermission());
57 if (!array_key_exists($form->_task
, $tasks)) {
58 CRM_Core_Error
::statusBounce(ts('You do not have permission to access this page.'));
61 $ids = $form->getSelectedIDs($values);
64 $queryParams = $form->get('queryParams');
66 if ($form->get(CRM_Utils_Sort
::SORT_ORDER
)) {
67 $sortOrder = $form->get(CRM_Utils_Sort
::SORT_ORDER
);
69 $query = new CRM_Contact_BAO_Query($queryParams, NULL, NULL, FALSE, FALSE,
70 CRM_Contact_BAO_Query
::MODE_MEMBER
72 $query->_distinctComponentClause
= ' civicrm_membership.id';
73 $query->_groupByComponentClause
= ' GROUP BY civicrm_membership.id ';
74 $result = $query->searchQuery(0, 0, $sortOrder);
76 while ($result->fetch()) {
77 $ids[] = $result->membership_id
;
82 $form->_componentClause
= ' civicrm_membership.id IN ( ' . implode(',', $ids) . ' ) ';
83 $form->assign('totalSelectedMembers', count($ids));
86 $form->_memberIds
= $form->_componentIds
= $ids;
87 $form->setNextUrl('member');
91 * Given the membership id, compute the contact id
92 * since its used for things like send email
94 public function setContactIDs() {
95 $this->_contactIds
= CRM_Core_DAO
::getContactIDsFromComponent($this->_memberIds
,
103 protected function getIDS() {
104 return $this->_memberIds
;
108 * Get the rows form the search, keyed to make the token processor happy.
110 * @throws \API_Exception
112 protected function getRows(): array {
113 if (empty($this->rows
)) {
114 // checkPermissions set to false - in case form is bypassing in some way.
115 $memberships = Membership
::get(FALSE)
116 ->addWhere('id', 'IN', $this->getIDs())
117 ->setSelect(['id', 'contact_id'])->execute();
118 foreach ($memberships as $membership) {
120 'contact_id' => $membership['contact_id'],
121 'membership_id' => $membership['id'],
123 'contactId' => $membership['contact_id'],
124 'membershipId' => $membership['id'],
133 * Get the token processor schema required to list any tokens for this task.
137 public function getTokenSchema(): array {
138 return ['membershipId', 'contactId'];