Merge pull request #17298 from demeritcowboy/activity-attachment-delete
[civicrm-core.git] / CRM / Member / Form / Task.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * Class for member form task actions.
20 * FIXME: This needs refactoring to properly inherit from CRM_Core_Form_Task and share more functions.
21 */
22 class CRM_Member_Form_Task extends CRM_Core_Form_Task {
23
24 /**
25 * The array that holds all the member ids.
26 *
27 * @var array
28 */
29 protected $_memberIds;
30
31 /**
32 * Build all the data structures needed to build the form.
33 *
34 * @param
35 *
36 * @return void
37 * @throws \CRM_Core_Exception
38 */
39 public function preProcess() {
40 self::preProcessCommon($this);
41 }
42
43 /**
44 * @param CRM_Core_Form $form
45 *
46 * @throws \CRM_Core_Exception
47 */
48 public static function preProcessCommon(&$form) {
49 $form->_memberIds = [];
50
51 $values = $form->controller->exportValues($form->get('searchFormName'));
52
53 $form->_task = $values['task'];
54 $tasks = CRM_Member_Task::permissionedTaskTitles(CRM_Core_Permission::getPermission());
55 if (!array_key_exists($form->_task, $tasks)) {
56 CRM_Core_Error::statusBounce(ts('You do not have permission to access this page.'));
57 }
58 $form->assign('taskName', $tasks[$form->_task]);
59
60 $ids = [];
61 if ($values['radio_ts'] === 'ts_sel') {
62 foreach ($values as $name => $value) {
63 if (substr($name, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) {
64 $ids[] = substr($name, CRM_Core_Form::CB_PREFIX_LEN);
65 }
66 }
67 }
68 else {
69 $queryParams = $form->get('queryParams');
70 $sortOrder = NULL;
71 if ($form->get(CRM_Utils_Sort::SORT_ORDER)) {
72 $sortOrder = $form->get(CRM_Utils_Sort::SORT_ORDER);
73 }
74 $query = new CRM_Contact_BAO_Query($queryParams, NULL, NULL, FALSE, FALSE,
75 CRM_Contact_BAO_Query::MODE_MEMBER
76 );
77 $query->_distinctComponentClause = ' civicrm_membership.id';
78 $query->_groupByComponentClause = ' GROUP BY civicrm_membership.id ';
79 $result = $query->searchQuery(0, 0, $sortOrder);
80
81 while ($result->fetch()) {
82 $ids[] = $result->membership_id;
83 }
84 }
85
86 if (!empty($ids)) {
87 $form->_componentClause = ' civicrm_membership.id IN ( ' . implode(',', $ids) . ' ) ';
88 $form->assign('totalSelectedMembers', count($ids));
89 }
90
91 $form->_memberIds = $form->_componentIds = $ids;
92
93 //set the context for redirection for any task actions
94 $session = CRM_Core_Session::singleton();
95
96 $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $form);
97 $urlParams = 'force=1';
98 if (CRM_Utils_Rule::qfKey($qfKey)) {
99 $urlParams .= "&qfKey=$qfKey";
100 }
101
102 $searchFormName = strtolower($form->get('searchFormName'));
103 if ($searchFormName === 'search') {
104 $session->replaceUserContext(CRM_Utils_System::url('civicrm/member/search', $urlParams));
105 }
106 else {
107 $session->replaceUserContext(CRM_Utils_System::url("civicrm/contact/search/$searchFormName",
108 $urlParams
109 ));
110 }
111 }
112
113 /**
114 * Given the membership id, compute the contact id
115 * since its used for things like send email
116 */
117 public function setContactIDs() {
118 $this->_contactIds = CRM_Core_DAO::getContactIDsFromComponent($this->_memberIds,
119 'civicrm_membership'
120 );
121 }
122
123 }