| 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 to represent the actions that can be performed on a group of contacts. |
| 20 | * |
| 21 | * Used by the search forms. |
| 22 | */ |
| 23 | class CRM_Contribute_Task extends CRM_Core_Task { |
| 24 | |
| 25 | /** |
| 26 | * Contribution tasks |
| 27 | */ |
| 28 | const |
| 29 | UPDATE_STATUS = 401, |
| 30 | PDF_RECEIPT = 402, |
| 31 | PDF_THANKYOU = 403, |
| 32 | PDF_INVOICE = 404; |
| 33 | |
| 34 | /** |
| 35 | * @var string |
| 36 | */ |
| 37 | public static $objectType = 'contribution'; |
| 38 | |
| 39 | /** |
| 40 | * These tasks are the core set of tasks that the user can perform |
| 41 | * on a contact / group of contacts |
| 42 | * |
| 43 | * @return array |
| 44 | * the set of tasks for a group of contacts |
| 45 | */ |
| 46 | public static function tasks() { |
| 47 | if (!(self::$_tasks)) { |
| 48 | self::$_tasks = [ |
| 49 | self::TASK_DELETE => [ |
| 50 | 'title' => ts('Delete contributions'), |
| 51 | 'class' => 'CRM_Contribute_Form_Task_Delete', |
| 52 | 'result' => FALSE, |
| 53 | ], |
| 54 | self::TASK_PRINT => [ |
| 55 | 'title' => ts('Print selected rows'), |
| 56 | 'class' => 'CRM_Contribute_Form_Task_Print', |
| 57 | 'result' => FALSE, |
| 58 | ], |
| 59 | self::TASK_EXPORT => [ |
| 60 | 'title' => ts('Export contributions'), |
| 61 | 'class' => [ |
| 62 | 'CRM_Contribute_Export_Form_Select', |
| 63 | 'CRM_Contribute_Export_Form_Map', |
| 64 | ], |
| 65 | 'result' => FALSE, |
| 66 | ], |
| 67 | self::BATCH_UPDATE => [ |
| 68 | 'title' => ts('Update multiple contributions'), |
| 69 | 'class' => [ |
| 70 | 'CRM_Contribute_Form_Task_PickProfile', |
| 71 | 'CRM_Contribute_Form_Task_Batch', |
| 72 | ], |
| 73 | 'result' => TRUE, |
| 74 | ], |
| 75 | self::TASK_EMAIL => [ |
| 76 | 'title' => ts('Email - send now (to %1 or less)', [ |
| 77 | 1 => Civi::settings() |
| 78 | ->get('simple_mail_limit'), |
| 79 | ]), |
| 80 | 'class' => 'CRM_Contribute_Form_Task_Email', |
| 81 | 'result' => TRUE, |
| 82 | ], |
| 83 | self::UPDATE_STATUS => [ |
| 84 | 'title' => ts('Record payments for contributions'), |
| 85 | 'class' => 'CRM_Contribute_Form_Task_Status', |
| 86 | 'result' => TRUE, |
| 87 | ], |
| 88 | self::PDF_RECEIPT => [ |
| 89 | 'title' => ts('Receipts - print or email'), |
| 90 | 'class' => 'CRM_Contribute_Form_Task_PDF', |
| 91 | 'result' => FALSE, |
| 92 | 'title_single_mode' => ts('Send Receipt'), |
| 93 | 'name' => ts('Send Receipt'), |
| 94 | 'url' => 'civicrm/contribute/task?reset=1&task_item=receipt', |
| 95 | 'key' => 'receipt', |
| 96 | 'icon' => 'fa-envelope-o', |
| 97 | 'filters' => ['contribution_status_id' => [CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed')]], |
| 98 | 'is_single_mode' => TRUE, |
| 99 | ], |
| 100 | self::PDF_THANKYOU => [ |
| 101 | 'title' => ts('Thank-you letters - print or email'), |
| 102 | 'class' => 'CRM_Contribute_Form_Task_PDFLetter', |
| 103 | 'result' => FALSE, |
| 104 | 'url' => 'civicrm/contribute/task?reset=1&task_item=letter', |
| 105 | 'key' => 'letter', |
| 106 | 'name' => ts('Send Letter'), |
| 107 | 'is_single_mode' => TRUE, |
| 108 | 'title_single_mode' => ts('Thank-you letter - print or email'), |
| 109 | ], |
| 110 | self::PDF_INVOICE => [ |
| 111 | 'title' => ts('Invoices - print or email'), |
| 112 | 'class' => 'CRM_Contribute_Form_Task_Invoice', |
| 113 | 'result' => FALSE, |
| 114 | ], |
| 115 | ]; |
| 116 | |
| 117 | //CRM-4418, check for delete |
| 118 | if (!CRM_Core_Permission::check('delete in CiviContribute')) { |
| 119 | unset(self::$_tasks[self::TASK_DELETE]); |
| 120 | } |
| 121 | //CRM-12920 - check for edit permission |
| 122 | if (!CRM_Core_Permission::check('edit contributions')) { |
| 123 | unset(self::$_tasks[self::BATCH_UPDATE], self::$_tasks[self::UPDATE_STATUS]); |
| 124 | } |
| 125 | |
| 126 | // remove action "Invoices - print or email" |
| 127 | $invoicing = CRM_Invoicing_Utils::isInvoicingEnabled(); |
| 128 | if (!$invoicing) { |
| 129 | unset(self::$_tasks[self::PDF_INVOICE]); |
| 130 | } |
| 131 | |
| 132 | parent::tasks(); |
| 133 | } |
| 134 | |
| 135 | return self::$_tasks; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Get links appropriate to the context of the row. |
| 140 | * |
| 141 | * @param array $row |
| 142 | * |
| 143 | * @return array |
| 144 | */ |
| 145 | public static function getContextualLinks($row) { |
| 146 | $tasks = self::tasks(); |
| 147 | foreach ($tasks as $key => $task) { |
| 148 | if (empty($task['is_single_mode'])) { |
| 149 | unset($tasks[$key]); |
| 150 | continue; |
| 151 | } |
| 152 | if (!empty($task['filters'])) { |
| 153 | foreach ($task['filters'] as $filter => $values) { |
| 154 | if (!in_array($row[$filter], $values, FALSE)) { |
| 155 | unset($tasks[$key]); |
| 156 | continue 2; |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | $tasks[$key]['url'] = $task['url']; |
| 161 | $tasks[$key]['qs'] = ['id' => $row['contribution_id']]; |
| 162 | $tasks[$key]['title'] = $task['title_single_mode'] ?? $task['title']; |
| 163 | } |
| 164 | return $tasks; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Show tasks selectively based on the permission level |
| 169 | * of the user |
| 170 | * |
| 171 | * @param int $permission |
| 172 | * |
| 173 | * @param array $params |
| 174 | * bool softCreditFiltering: derived from CRM_Contribute_BAO_Query::isSoftCreditOptionEnabled |
| 175 | * |
| 176 | * @return array |
| 177 | * set of tasks that are valid for the user |
| 178 | */ |
| 179 | public static function permissionedTaskTitles($permission, $params = []) { |
| 180 | if (!isset($params['softCreditFiltering'])) { |
| 181 | $params['softCreditFiltering'] = FALSE; |
| 182 | } |
| 183 | if (($permission == CRM_Core_Permission::EDIT) |
| 184 | || CRM_Core_Permission::check('edit contributions') |
| 185 | ) { |
| 186 | $tasks = self::taskTitles(); |
| 187 | } |
| 188 | else { |
| 189 | $tasks = [ |
| 190 | self::TASK_EXPORT => self::$_tasks[self::TASK_EXPORT]['title'], |
| 191 | self::TASK_EMAIL => self::$_tasks[self::TASK_EMAIL]['title'], |
| 192 | self::PDF_RECEIPT => self::$_tasks[self::PDF_RECEIPT]['title'], |
| 193 | ]; |
| 194 | |
| 195 | //CRM-4418, |
| 196 | if (CRM_Core_Permission::check('delete in CiviContribute')) { |
| 197 | $tasks[self::TASK_DELETE] = self::$_tasks[self::TASK_DELETE]['title']; |
| 198 | } |
| 199 | } |
| 200 | if ($params['softCreditFiltering']) { |
| 201 | unset($tasks[self::BATCH_UPDATE], $tasks[self::PDF_RECEIPT]); |
| 202 | } |
| 203 | |
| 204 | $tasks = parent::corePermissionedTaskTitles($tasks, $permission, $params); |
| 205 | return $tasks; |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * These tasks are the core set of tasks that the user can perform |
| 210 | * on contributors |
| 211 | * |
| 212 | * @param int $value |
| 213 | * |
| 214 | * @return array |
| 215 | * the set of tasks for a group of contributors |
| 216 | */ |
| 217 | public static function getTask($value) { |
| 218 | self::tasks(); |
| 219 | if (!$value || empty(self::$_tasks[$value])) { |
| 220 | // make the print task by default |
| 221 | $value = self::TASK_PRINT; |
| 222 | } |
| 223 | return parent::getTask($value); |
| 224 | } |
| 225 | |
| 226 | } |