| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.6 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
| 7 | +--------------------------------------------------------------------+ |
| 8 | | This file is a part of CiviCRM. | |
| 9 | | | |
| 10 | | CiviCRM is free software; you can copy, modify, and distribute it | |
| 11 | | under the terms of the GNU Affero General Public License | |
| 12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | |
| 13 | | | |
| 14 | | CiviCRM is distributed in the hope that it will be useful, but | |
| 15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
| 17 | | See the GNU Affero General Public License for more details. | |
| 18 | | | |
| 19 | | You should have received a copy of the GNU Affero General Public | |
| 20 | | License and the CiviCRM Licensing Exception along | |
| 21 | | with this program; if not, contact CiviCRM LLC | |
| 22 | | at info[AT]civicrm[DOT]org. If you have questions about the | |
| 23 | | GNU Affero General Public License or the licensing of CiviCRM, | |
| 24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | |
| 25 | +--------------------------------------------------------------------+ |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * |
| 30 | * @package CRM |
| 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
| 32 | * $Id$ |
| 33 | * |
| 34 | */ |
| 35 | |
| 36 | /** |
| 37 | * class to represent the actions that can be performed on a group of contacts |
| 38 | * used by the search forms |
| 39 | * |
| 40 | */ |
| 41 | class CRM_Contact_Task { |
| 42 | const |
| 43 | GROUP_CONTACTS = 1, |
| 44 | REMOVE_CONTACTS = 2, |
| 45 | TAG_CONTACTS = 3, |
| 46 | REMOVE_TAGS = 4, |
| 47 | EXPORT_CONTACTS = 5, |
| 48 | EMAIL_CONTACTS = 6, |
| 49 | SMS_CONTACTS = 7, |
| 50 | DELETE_CONTACTS = 8, |
| 51 | HOUSEHOLD_CONTACTS = 9, |
| 52 | ORGANIZATION_CONTACTS = 10, |
| 53 | RECORD_CONTACTS = 11, |
| 54 | MAP_CONTACTS = 12, |
| 55 | SAVE_SEARCH = 13, |
| 56 | SAVE_SEARCH_UPDATE = 14, |
| 57 | PRINT_CONTACTS = 15, |
| 58 | LABEL_CONTACTS = 16, |
| 59 | BATCH_UPDATE = 17, |
| 60 | ADD_EVENT = 18, |
| 61 | PRINT_FOR_CONTACTS = 19, |
| 62 | CREATE_MAILING = 20, |
| 63 | MERGE_CONTACTS = 21, |
| 64 | EMAIL_UNHOLD = 22, |
| 65 | RESTORE = 23, |
| 66 | DELETE_PERMANENTLY = 24, |
| 67 | COMMUNICATION_PREFS = 25; |
| 68 | |
| 69 | /** |
| 70 | * The task array |
| 71 | * |
| 72 | * @var array |
| 73 | * @static |
| 74 | */ |
| 75 | static $_tasks = NULL; |
| 76 | |
| 77 | /** |
| 78 | * The optional task array |
| 79 | * |
| 80 | * @var array |
| 81 | * @static |
| 82 | */ |
| 83 | static $_optionalTasks = NULL; |
| 84 | |
| 85 | public static function initTasks() { |
| 86 | if (!self::$_tasks) { |
| 87 | self::$_tasks = array( |
| 88 | self::GROUP_CONTACTS => array( |
| 89 | 'title' => ts('Add Contacts to Group'), |
| 90 | 'class' => 'CRM_Contact_Form_Task_AddToGroup', |
| 91 | ), |
| 92 | self::REMOVE_CONTACTS => array( |
| 93 | 'title' => ts('Remove Contacts from Group'), |
| 94 | 'class' => 'CRM_Contact_Form_Task_RemoveFromGroup', |
| 95 | ), |
| 96 | self::TAG_CONTACTS => array( |
| 97 | 'title' => ts('Tag Contacts (assign tags)'), |
| 98 | 'class' => 'CRM_Contact_Form_Task_AddToTag', |
| 99 | ), |
| 100 | self::REMOVE_TAGS => array( |
| 101 | 'title' => ts('Untag Contacts (remove tags)'), |
| 102 | 'class' => 'CRM_Contact_Form_Task_RemoveFromTag', |
| 103 | ), |
| 104 | self::EXPORT_CONTACTS => array( |
| 105 | 'title' => ts('Export Contacts'), |
| 106 | 'class' => array( |
| 107 | 'CRM_Export_Form_Select', |
| 108 | 'CRM_Export_Form_Map', |
| 109 | ), |
| 110 | 'result' => FALSE, |
| 111 | ), |
| 112 | self::EMAIL_CONTACTS => array( |
| 113 | 'title' => ts('Send Email to Contacts'), |
| 114 | 'class' => 'CRM_Contact_Form_Task_Email', |
| 115 | 'result' => TRUE, |
| 116 | ), |
| 117 | self::SMS_CONTACTS => array( |
| 118 | 'title' => ts('Send SMS to Contacts'), |
| 119 | 'class' => 'CRM_Contact_Form_Task_SMS', |
| 120 | 'result' => TRUE, |
| 121 | ), |
| 122 | self::DELETE_CONTACTS => array( |
| 123 | 'title' => ts('Delete Contacts'), |
| 124 | 'class' => 'CRM_Contact_Form_Task_Delete', |
| 125 | 'result' => FALSE, |
| 126 | ), |
| 127 | self::RECORD_CONTACTS => array( |
| 128 | 'title' => ts('Record Activity for Contacts'), |
| 129 | 'class' => 'CRM_Activity_Form_Activity', |
| 130 | ), |
| 131 | self::SAVE_SEARCH => array( |
| 132 | 'title' => ts('New Smart Group'), |
| 133 | 'class' => 'CRM_Contact_Form_Task_SaveSearch', |
| 134 | 'result' => TRUE, |
| 135 | ), |
| 136 | self::SAVE_SEARCH_UPDATE => array( |
| 137 | 'title' => ts('Update Smart Group'), |
| 138 | 'class' => 'CRM_Contact_Form_Task_SaveSearch_Update', |
| 139 | 'result' => TRUE, |
| 140 | ), |
| 141 | self::PRINT_CONTACTS => array( |
| 142 | 'title' => ts('Print Selected Rows'), |
| 143 | 'class' => 'CRM_Contact_Form_Task_Print', |
| 144 | 'result' => FALSE, |
| 145 | ), |
| 146 | self::LABEL_CONTACTS => array( |
| 147 | 'title' => ts('Mailing Labels'), |
| 148 | 'class' => 'CRM_Contact_Form_Task_Label', |
| 149 | 'result' => TRUE, |
| 150 | ), |
| 151 | self::BATCH_UPDATE => array( |
| 152 | 'title' => ts('Batch Update via Profile'), |
| 153 | 'class' => array( |
| 154 | 'CRM_Contact_Form_Task_PickProfile', |
| 155 | 'CRM_Contact_Form_Task_Batch', |
| 156 | ), |
| 157 | 'result' => TRUE, |
| 158 | ), |
| 159 | self::PRINT_FOR_CONTACTS => array( |
| 160 | 'title' => ts('Print PDF Letter for Contacts'), |
| 161 | 'class' => 'CRM_Contact_Form_Task_PDF', |
| 162 | 'result' => TRUE, |
| 163 | ), |
| 164 | self::EMAIL_UNHOLD => array( |
| 165 | 'title' => ts('Unhold Emails'), |
| 166 | 'class' => 'CRM_Contact_Form_Task_Unhold', |
| 167 | ), |
| 168 | self::COMMUNICATION_PREFS => array( |
| 169 | 'title' => ts('Alter Contact Communication Preferences'), |
| 170 | 'class' => 'CRM_Contact_Form_Task_AlterPreferences', |
| 171 | ), |
| 172 | self::RESTORE => array( |
| 173 | 'title' => ts('Restore Contacts'), |
| 174 | 'class' => 'CRM_Contact_Form_Task_Delete', |
| 175 | 'result' => FALSE, |
| 176 | ), |
| 177 | self::DELETE_PERMANENTLY => array( |
| 178 | 'title' => ts('Delete Permanently'), |
| 179 | 'class' => 'CRM_Contact_Form_Task_Delete', |
| 180 | 'result' => FALSE, |
| 181 | ), |
| 182 | ); |
| 183 | |
| 184 | if (CRM_Contact_BAO_ContactType::isActive('Household')) { |
| 185 | $label = CRM_Contact_BAO_ContactType::getLabel('Household'); |
| 186 | self::$_tasks[self::HOUSEHOLD_CONTACTS] = array( |
| 187 | 'title' => ts('Add Contacts to %1', |
| 188 | array(1 => $label) |
| 189 | ), |
| 190 | 'class' => 'CRM_Contact_Form_Task_AddToHousehold', |
| 191 | ); |
| 192 | } |
| 193 | |
| 194 | if (CRM_Contact_BAO_ContactType::isActive('Organization')) { |
| 195 | $label = CRM_Contact_BAO_ContactType::getLabel('Organization'); |
| 196 | self::$_tasks[self::ORGANIZATION_CONTACTS] = array( |
| 197 | 'title' => ts('Add Contacts to %1', |
| 198 | array(1 => $label) |
| 199 | ), |
| 200 | 'class' => 'CRM_Contact_Form_Task_AddToOrganization', |
| 201 | ); |
| 202 | } |
| 203 | |
| 204 | if (CRM_Core_Permission::check('merge duplicate contacts')) { |
| 205 | self::$_tasks[self::MERGE_CONTACTS] = array( |
| 206 | 'title' => ts('Merge Contacts'), |
| 207 | 'class' => 'CRM_Contact_Form_Task_Merge', |
| 208 | 'result' => TRUE, |
| 209 | ); |
| 210 | } |
| 211 | |
| 212 | //CRM-4418, check for delete |
| 213 | if (!CRM_Core_Permission::check('delete contacts')) { |
| 214 | unset(self::$_tasks[self::DELETE_CONTACTS]); |
| 215 | } |
| 216 | |
| 217 | //show map action only if map provider and geoprovider are set (Google doesn't need geoprovider) |
| 218 | // should fix this to be more flexible as providers are added ?? |
| 219 | $config = CRM_Core_Config::singleton(); |
| 220 | |
| 221 | if ($config->mapProvider && |
| 222 | ($config->mapProvider == 'Google' || |
| 223 | ($config->mapProvider == 'OpenStreetMaps' || |
| 224 | $config->geoProvider == 'Google' |
| 225 | ) |
| 226 | ) |
| 227 | ) { |
| 228 | self::$_tasks[self::MAP_CONTACTS] = array( |
| 229 | 'title' => ts('Map Contacts'), |
| 230 | 'class' => 'CRM_Contact_Form_Task_Map', |
| 231 | 'result' => FALSE, |
| 232 | ); |
| 233 | } |
| 234 | |
| 235 | if (CRM_Core_Permission::access('CiviEvent')) { |
| 236 | self::$_tasks[self::ADD_EVENT] = array( |
| 237 | 'title' => ts('Add Contacts to Event'), |
| 238 | 'class' => 'CRM_Event_Form_Participant', |
| 239 | ); |
| 240 | } |
| 241 | |
| 242 | if (CRM_Core_Permission::access('CiviMail')) { |
| 243 | self::$_tasks[self::CREATE_MAILING] = array( |
| 244 | 'title' => ts('Schedule/Send a Mass Mailing'), |
| 245 | 'class' => array( |
| 246 | 'CRM_Mailing_Form_Group', |
| 247 | 'CRM_Mailing_Form_Settings', |
| 248 | 'CRM_Mailing_Form_Upload', |
| 249 | 'CRM_Mailing_Form_Test', |
| 250 | 'CRM_Mailing_Form_Schedule', |
| 251 | ), |
| 252 | 'result' => FALSE, |
| 253 | ); |
| 254 | } |
| 255 | elseif (CRM_Mailing_Info::workflowEnabled() && |
| 256 | CRM_Core_Permission::check('create mailings') |
| 257 | ) { |
| 258 | self::$_tasks[self::CREATE_MAILING] = array( |
| 259 | 'title' => ts('Create a Mass Mailing'), |
| 260 | 'class' => array( |
| 261 | 'CRM_Mailing_Form_Group', |
| 262 | 'CRM_Mailing_Form_Settings', |
| 263 | 'CRM_Mailing_Form_Upload', |
| 264 | 'CRM_Mailing_Form_Test', |
| 265 | ), |
| 266 | 'result' => FALSE, |
| 267 | ); |
| 268 | } |
| 269 | |
| 270 | self::$_tasks += CRM_Core_Component::taskList(); |
| 271 | |
| 272 | CRM_Utils_Hook::searchTasks('contact', self::$_tasks); |
| 273 | |
| 274 | asort(self::$_tasks); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * These tasks are the core set of tasks that the user can perform |
| 280 | * on a contact / group of contacts |
| 281 | * |
| 282 | * @return array the set of tasks for a group of contacts |
| 283 | * @static |
| 284 | * @access public |
| 285 | */ |
| 286 | public static function &taskTitles() { |
| 287 | self::initTasks(); |
| 288 | |
| 289 | $titles = array(); |
| 290 | foreach (self::$_tasks as $id => $value) { |
| 291 | $titles[$id] = $value['title']; |
| 292 | } |
| 293 | |
| 294 | // hack unset update saved search |
| 295 | unset($titles[self::SAVE_SEARCH_UPDATE]); |
| 296 | |
| 297 | if (!CRM_Utils_Mail::validOutBoundMail()) { |
| 298 | unset($titles[self::EMAIL_CONTACTS]); |
| 299 | unset($titles[self::CREATE_MAILING]); |
| 300 | } |
| 301 | |
| 302 | // CRM-6806 |
| 303 | if (!CRM_Core_Permission::check('access deleted contacts') || |
| 304 | !CRM_Core_Permission::check('delete contacts') |
| 305 | ) { |
| 306 | unset($titles[self::DELETE_PERMANENTLY]); |
| 307 | } |
| 308 | asort($titles); |
| 309 | return $titles; |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * Show tasks selectively based on the permission level |
| 314 | * of the user |
| 315 | * |
| 316 | * @param int $permission |
| 317 | * @param bool $deletedContacts are these tasks for operating on deleted contacts? |
| 318 | * |
| 319 | * @return array set of tasks that are valid for the user |
| 320 | * @access public |
| 321 | */ |
| 322 | public static function &permissionedTaskTitles($permission, $deletedContacts = FALSE) { |
| 323 | self::initTasks(); |
| 324 | $tasks = array(); |
| 325 | if ($deletedContacts) { |
| 326 | if (CRM_Core_Permission::check('access deleted contacts')) { |
| 327 | $tasks[self::RESTORE] = self::$_tasks[self::RESTORE]['title']; |
| 328 | if (CRM_Core_Permission::check('delete contacts')) { |
| 329 | $tasks[self::DELETE_PERMANENTLY] = self::$_tasks[self::DELETE_PERMANENTLY]['title']; |
| 330 | } |
| 331 | } |
| 332 | } |
| 333 | elseif ($permission == CRM_Core_Permission::EDIT) { |
| 334 | $tasks = self::taskTitles(); |
| 335 | } |
| 336 | else { |
| 337 | $tasks = array( |
| 338 | self::EXPORT_CONTACTS => self::$_tasks[self::EXPORT_CONTACTS]['title'], |
| 339 | self::EMAIL_CONTACTS => self::$_tasks[self::EMAIL_CONTACTS]['title'], |
| 340 | self::LABEL_CONTACTS => self::$_tasks[self::LABEL_CONTACTS]['title'], |
| 341 | ); |
| 342 | |
| 343 | if (isset(self::$_tasks[self::MAP_CONTACTS]) && |
| 344 | !empty(self::$_tasks[self::MAP_CONTACTS]['title']) |
| 345 | ) { |
| 346 | $tasks[self::MAP_CONTACTS] = self::$_tasks[self::MAP_CONTACTS]['title']; |
| 347 | } |
| 348 | |
| 349 | if (isset(self::$_tasks[self::CREATE_MAILING]) && |
| 350 | !empty(self::$_tasks[self::CREATE_MAILING]['title']) |
| 351 | ) { |
| 352 | $tasks[self::CREATE_MAILING] = self::$_tasks[self::CREATE_MAILING]['title']; |
| 353 | } |
| 354 | } |
| 355 | return $tasks; |
| 356 | } |
| 357 | |
| 358 | /** |
| 359 | * These tasks get added based on the context the user is in |
| 360 | * |
| 361 | * @return array the set of optional tasks for a group of contacts |
| 362 | * @static |
| 363 | * @access public |
| 364 | */ |
| 365 | public static function &optionalTaskTitle() { |
| 366 | $tasks = array( |
| 367 | self::SAVE_SEARCH_UPDATE => self::$_tasks[self::SAVE_SEARCH_UPDATE]['title'], |
| 368 | ); |
| 369 | return $tasks; |
| 370 | } |
| 371 | |
| 372 | /** |
| 373 | * @param $value |
| 374 | * |
| 375 | * @return array |
| 376 | */ |
| 377 | public static function getTask($value) { |
| 378 | self::initTasks(); |
| 379 | |
| 380 | if (!CRM_Utils_Array::value($value, self::$_tasks)) { |
| 381 | // make it the print task by default |
| 382 | $value = self::PRINT_CONTACTS; |
| 383 | } |
| 384 | return array( |
| 385 | CRM_Utils_Array::value('class', self::$_tasks[$value]), |
| 386 | CRM_Utils_Array::value('result', self::$_tasks[$value]), |
| 387 | ); |
| 388 | } |
| 389 | } |
| 390 | |