diff --git a/CRM/Activity/StateMachine/Search.php b/CRM/Activity/StateMachine/Search.php index 6aa81146fe0d..992a79986b48 100644 --- a/CRM/Activity/StateMachine/Search.php +++ b/CRM/Activity/StateMachine/Search.php @@ -76,7 +76,7 @@ public function __construct($controller, $action = CRM_Core_Action::NONE) { * * @param string $formName * - * @return string + * @return array * the name of the form that will handle the task */ public function taskName($controller, $formName = 'Search') { diff --git a/CRM/Campaign/StateMachine/Search.php b/CRM/Campaign/StateMachine/Search.php index 4c04c01a7f65..3ff6997b4604 100644 --- a/CRM/Campaign/StateMachine/Search.php +++ b/CRM/Campaign/StateMachine/Search.php @@ -78,7 +78,7 @@ public function __construct($controller, $action = CRM_Core_Action::NONE) { * * @param string $formName * - * @return string + * @return array * the name of the form that will handle the task */ public function taskName($controller, $formName = 'Search') { diff --git a/CRM/Case/StateMachine/Search.php b/CRM/Case/StateMachine/Search.php index 0cfa120af760..54488648b934 100644 --- a/CRM/Case/StateMachine/Search.php +++ b/CRM/Case/StateMachine/Search.php @@ -80,7 +80,7 @@ public function __construct($controller, $action = CRM_Core_Action::NONE) { * * @param string $formName * - * @return string + * @return array * the name of the form that will handle the task */ public function taskName($controller, $formName = 'Search') { diff --git a/CRM/Contact/StateMachine/Search.php b/CRM/Contact/StateMachine/Search.php index 61bed6a48293..545b6e93d334 100644 --- a/CRM/Contact/StateMachine/Search.php +++ b/CRM/Contact/StateMachine/Search.php @@ -92,7 +92,7 @@ public function __construct($controller, $action = CRM_Core_Action::NONE) { * * @param string $formName * - * @return string + * @return array * the name of the form that will handle the task */ public function taskName($controller, $formName = 'Search') { diff --git a/CRM/Contribute/StateMachine/Search.php b/CRM/Contribute/StateMachine/Search.php index 6d437472e328..487b82849be5 100644 --- a/CRM/Contribute/StateMachine/Search.php +++ b/CRM/Contribute/StateMachine/Search.php @@ -78,7 +78,7 @@ public function __construct($controller, $action = CRM_Core_Action::NONE) { * * @param string $formName * - * @return string + * @return array [ 'class' => task classname, 'result' => TRUE ] * the name of the form that will handle the task */ public function taskName($controller, $formName = 'Search') { diff --git a/CRM/Core/Task.php b/CRM/Core/Task.php new file mode 100644 index 000000000000..96a64e8a56e3 --- /dev/null +++ b/CRM/Core/Task.php @@ -0,0 +1,202 @@ + The Task title, + * 'class' => The Task Form class name, + * 'result => Boolean. FIXME: Not sure what this is for + * ] + */ + public static function tasks() { + CRM_Utils_Hook::searchTasks(self::$objectType, self::$_tasks); + asort(self::$_tasks); + + return self::$_tasks; + } + + /** + * These tasks are the core set of tasks that the user can perform + * on a contact / group of contacts + * + * @return array + * the set of tasks for a group of contacts + */ + public static function taskTitles() { + static::tasks(); + + $titles = array(); + foreach (self::$_tasks as $id => $value) { + $titles[$id] = $value['title']; + } + + if (!CRM_Utils_Mail::validOutBoundMail()) { + unset($titles[self::TASK_EMAIL]); + unset($titles[self::CREATE_MAILING]); + } + + // Users who do not have 'access deleted contacts' should NOT have the 'Delete Permanently' option in search result tasks + if (!CRM_Core_Permission::check('access deleted contacts') || + !CRM_Core_Permission::check('delete contacts') + ) { + unset($titles[self::DELETE_PERMANENTLY]); + } + return $titles; + } + + /** + * Show tasks selectively based on the permission level + * of the user + * This function should be call parent::corePermissionedTaskTitles + * + * @param int $permission + * @param array $params + * "ssID: Saved Search ID": If !empty we are in saved search context + * + * @return array + * set of tasks that are valid for the user + */ + abstract public static function permissionedTaskTitles($permission, $params); + + /** + * Show tasks selectively based on the permission level + * of the user + * This function should be called by permissionedTaskTitles in children + * + * @param array $tasks The array of tasks generated by permissionedTaskTitles + * @param int $permission + * @param array $params + * "ssID: Saved Search ID": If !empty we are in saved search context + * + * @return array + * set of tasks that are valid for the user + */ + public static function corePermissionedTaskTitles($tasks, $permission, $params) { + // Only offer the "Update Smart Group" task if a smart group/saved search is already in play and we have edit permissions + if (!empty($params['ssID']) && ($permission == CRM_Core_Permission::EDIT)) { + $tasks[self::SAVE_SEARCH_UPDATE] = self::$_tasks[self::SAVE_SEARCH_UPDATE]['title']; + } + else { + unset($tasks[self::SAVE_SEARCH_UPDATE]); + } + + asort($tasks); + return $tasks; + } + + /** + * These tasks are the core set of tasks that the user can perform + * on participants + * + * @param int $value + * + * @return array + * the set of tasks for a group of participants + */ + public static function getTask($value) { + static::tasks(); + + if (!CRM_Utils_Array::value($value, self::$_tasks)) { + // Children can specify a default task (eg. print), we don't here + return array(); + } + return array( + CRM_Utils_Array::value('class', self::$_tasks[$value]), + CRM_Utils_Array::value('result', self::$_tasks[$value]), + ); + } + + /** + * Function to return the task information on basis of provided task's form name + * + * @param string $className + * + * @return array [ 0 => Task ID, 1 => Task Title ] + */ + public static function getTaskAndTitleByClass($className) { + static::tasks(); + + foreach (self::$_tasks as $task => $value) { + if ((!empty($value['url']) || $task == self::TASK_EXPORT) + && ((is_array($value['class']) && in_array($className, $value['class'])) + || ($value['class'] == $className))) { + return array($task, CRM_Utils_Array::value('title', $value)); + } + } + return array(); + } + +} diff --git a/CRM/Event/Form/Search.php b/CRM/Event/Form/Search.php index 5f5783d575e4..282ae87d8220 100644 --- a/CRM/Event/Form/Search.php +++ b/CRM/Event/Form/Search.php @@ -223,14 +223,10 @@ public function buildQuickForm() { $this->assign('participantCount', $participantCount); $this->assign('lineItems', $lineItems); - $permission = CRM_Core_Permission::getPermission(); + $taskParams['ssID'] = isset($this->_ssID) ? $this->_ssID : NULL; + $tasks = CRM_Event_Task::permissionedTaskTitles(CRM_Core_Permission::getPermission(), $taskParams); - $tasks = CRM_Event_Task::permissionedTaskTitles($permission); if (isset($this->_ssID)) { - if ($permission == CRM_Core_Permission::EDIT) { - $tasks = $tasks + CRM_Event_Task::optionalTaskTitle(); - } - $savedSearchValues = array( 'id' => $this->_ssID, 'name' => CRM_Contact_BAO_SavedSearch::getName($this->_ssID, 'title'), diff --git a/CRM/Event/Form/Task.php b/CRM/Event/Form/Task.php index 1929edd6b8cc..3bc742a451d1 100644 --- a/CRM/Event/Form/Task.php +++ b/CRM/Event/Form/Task.php @@ -88,8 +88,11 @@ public static function preProcessCommon(&$form, $useTable = FALSE) { $values = $form->controller->exportValues($form->get('searchFormName')); $form->_task = $values['task']; - $eventTasks = CRM_Event_Task::tasks(); - $form->assign('taskName', $eventTasks[$form->_task]); + $tasks = CRM_Event_Task::permissionedTaskTitles(CRM_Core_Permission::getPermission()); + if (!array_key_exists($form->_task, $tasks)) { + CRM_Core_Error::statusBounce(ts('You do not have permission to access this page.')); + } + $form->assign('taskName', $tasks[$form->_task]); $ids = array(); if ($values['radio_ts'] == 'ts_sel') { diff --git a/CRM/Event/StateMachine/Search.php b/CRM/Event/StateMachine/Search.php index c4ca4e7d5be9..6347afed1178 100644 --- a/CRM/Event/StateMachine/Search.php +++ b/CRM/Event/StateMachine/Search.php @@ -45,7 +45,7 @@ class CRM_Event_StateMachine_Search extends CRM_Core_StateMachine { * Class constructor. * * @param object $controller - * @param \const|int $action + * @param int $action */ public function __construct($controller, $action = CRM_Core_Action::NONE) { parent::__construct($controller, $action); @@ -82,7 +82,7 @@ public function __construct($controller, $action = CRM_Core_Action::NONE) { * * @param string $formName * - * @return string + * @return array * the name of the form that will handle the task */ public function taskName($controller, $formName = 'Search') { diff --git a/CRM/Event/Task.php b/CRM/Event/Task.php index 01f6fef5b44d..1637e0d9f59c 100644 --- a/CRM/Event/Task.php +++ b/CRM/Event/Task.php @@ -26,10 +26,8 @@ */ /** - * * @package CRM * @copyright CiviCRM LLC (c) 2004-2017 - * $Id$ * */ @@ -38,48 +36,39 @@ * used by the search forms * */ -class CRM_Event_Task { - // Value for SAVE_SEARCH is set to 13 in accordance with CRM_Contact_Task::SAVE_SEARCH - 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, GROUP_CONTACTS = 20; +class CRM_Event_Task extends CRM_Core_Task { - /** - * The task array - * - * @var array - */ - static $_tasks = NULL; + const + // Event tasks + CANCEL_REGISTRATION = 301, + PARTICIPANT_STATUS = 302; - /** - * The optional task array - * - * @var array - */ - static $_optionalTasks = NULL; + static $objectType = 'event'; /** * These tasks are the core set of tasks that the user can perform * on a contact / group of contacts * - * @return array - * the set of tasks for a group of contacts + * @return array The set of tasks for a group of contacts + * [ 'title' => The Task title, + * 'class' => The Task Form class name, + * 'result => Boolean. FIXME: Not sure what this is for + * ] */ - public static function &tasks() { - if (!(self::$_tasks)) { + public static function tasks() { + if (!self::$_tasks) { self::$_tasks = array( - 1 => array( + self::TASK_DELETE => array( 'title' => ts('Delete participants from event'), 'class' => 'CRM_Event_Form_Task_Delete', 'result' => FALSE, ), - 2 => array( + self::TASK_PRINT => array( 'title' => ts('Print selected rows'), 'class' => 'CRM_Event_Form_Task_Print', 'result' => FALSE, ), - 3 => array( + self::TASK_EXPORT => array( 'title' => ts('Export participants'), 'class' => array( 'CRM_Export_Form_Select', @@ -87,7 +76,7 @@ public static function &tasks() { ), 'result' => FALSE, ), - 4 => array( + self::BATCH_UPDATE => array( 'title' => ts('Update multiple participants'), 'class' => array( 'CRM_Event_Form_Task_PickProfile', @@ -95,12 +84,12 @@ public static function &tasks() { ), 'result' => TRUE, ), - 5 => array( + self::CANCEL_REGISTRATION => array( 'title' => ts('Cancel registration'), 'class' => 'CRM_Event_Form_Task_Cancel', 'result' => FALSE, ), - 6 => array( + self::TASK_EMAIL => array( 'title' => ts('Email - send now (to %1 or less)', array( 1 => Civi::settings() ->get('simple_mail_limit'), @@ -108,32 +97,32 @@ public static function &tasks() { 'class' => 'CRM_Event_Form_Task_Email', 'result' => TRUE, ), - 13 => array( + self::SAVE_SEARCH => array( 'title' => ts('Group - create smart group'), 'class' => 'CRM_Event_Form_Task_SaveSearch', 'result' => TRUE, ), - 14 => array( + self::SAVE_SEARCH_UPDATE => array( 'title' => ts('Group - update smart group'), 'class' => 'CRM_Event_Form_Task_SaveSearch_Update', 'result' => TRUE, ), - 15 => array( + self::PARTICIPANT_STATUS => array( 'title' => ts('Participant status - change'), 'class' => 'CRM_Event_Form_Task_ParticipantStatus', 'result' => TRUE, ), - 16 => array( + self::LABEL_CONTACTS => array( 'title' => ts('Name badges - print'), 'class' => 'CRM_Event_Form_Task_Badge', 'result' => FALSE, ), - 17 => array( + self::TASK_PRINT => array( 'title' => ts('PDF letter - print for participants'), 'class' => 'CRM_Event_Form_Task_PDF', 'result' => TRUE, ), - 20 => array( + self::GROUP_ADD => array( 'title' => ts('Group - add contacts'), 'class' => 'CRM_Event_Form_Task_AddToGroup', 'result' => FALSE, @@ -142,62 +131,30 @@ public static function &tasks() { //CRM-4418, check for delete if (!CRM_Core_Permission::check('delete in CiviEvent')) { - unset(self::$_tasks[1]); + unset(self::$_tasks[self::TASK_DELETE]); } //CRM-12920 - check for edit permission if (!CRM_Core_Permission::check('edit event participants')) { - unset(self::$_tasks[4], self::$_tasks[5], self::$_tasks[15]); + unset(self::$_tasks[self::BATCH_UPDATE], self::$_tasks[self::CANCEL_REGISTRATION], self::$_tasks[self::PARTICIPANT_STATUS]); } - CRM_Utils_Hook::searchTasks('event', self::$_tasks); + parent::tasks(); } return self::$_tasks; } - /** - * These tasks are the core set of task titles - * for participants - * - * @return array - * the set of task titles - */ - public static function &taskTitles() { - self::tasks(); - $titles = array(); - foreach (self::$_tasks as $id => $value) { - // skip Update Smart Group task - if ($id != self::SAVE_SEARCH_UPDATE) { - $titles[$id] = $value['title']; - } - } - return $titles; - } - - /** - * These tasks get added based on the context the user is in. - * - * @return array - * the set of optional tasks for a group of contacts - */ - public static function &optionalTaskTitle() { - $tasks = array( - 14 => self::$_tasks[14]['title'], - ); - return $tasks; - } - /** * Show tasks selectively based on the permission level * of the user * * @param int $permission + * @param array $params * * @return array * set of tasks that are valid for the user */ - public static function &permissionedTaskTitles($permission) { - $tasks = array(); + public static function permissionedTaskTitles($permission, $params = array()) { if (($permission == CRM_Core_Permission::EDIT) || CRM_Core_Permission::check('edit event participants') ) { @@ -205,15 +162,17 @@ public static function &permissionedTaskTitles($permission) { } else { $tasks = array( - 3 => self::$_tasks[3]['title'], - 6 => self::$_tasks[6]['title'], + self::TASK_EXPORT => self::$_tasks[self::TASK_EXPORT]['title'], + self::TASK_EMAIL => self::$_tasks[self::TASK_EMAIL]['title'], ); //CRM-4418, if (CRM_Core_Permission::check('delete in CiviEvent')) { - $tasks[1] = self::$_tasks[1]['title']; + $tasks[self::TASK_DELETE] = self::$_tasks[self::TASK_DELETE]['title']; } } + + $tasks = parent::corePermissionedTaskTitles($tasks, $permission, $params); return $tasks; } @@ -230,13 +189,9 @@ public static function getTask($value) { self::tasks(); if (!$value || !CRM_Utils_Array::value($value, self::$_tasks)) { // make the print task by default - $value = 2; + $value = self::TASK_PRINT; } - asort(self::$_tasks); - return array( - self::$_tasks[$value]['class'], - self::$_tasks[$value]['result'], - ); + return parent::getTask($value); } } diff --git a/CRM/Grant/StateMachine/Search.php b/CRM/Grant/StateMachine/Search.php index 2b387d63b0c5..549db4e65467 100644 --- a/CRM/Grant/StateMachine/Search.php +++ b/CRM/Grant/StateMachine/Search.php @@ -82,7 +82,7 @@ public function __construct($controller, $action = CRM_Core_Action::NONE) { * * @param string $formName * - * @return string + * @return array * the name of the form that will handle the task */ public function taskName($controller, $formName = 'Search') { diff --git a/CRM/Member/StateMachine/Search.php b/CRM/Member/StateMachine/Search.php index 49453bc2753c..a4521ea16378 100644 --- a/CRM/Member/StateMachine/Search.php +++ b/CRM/Member/StateMachine/Search.php @@ -80,7 +80,7 @@ public function __construct($controller, $action = CRM_Core_Action::NONE) { * * @param string $formName * - * @return string + * @return array * the name of the form that will handle the task */ public function taskName($controller, $formName = 'Search') { diff --git a/CRM/Pledge/StateMachine/Search.php b/CRM/Pledge/StateMachine/Search.php index a809e91fd2e8..653ee39d113f 100644 --- a/CRM/Pledge/StateMachine/Search.php +++ b/CRM/Pledge/StateMachine/Search.php @@ -80,7 +80,7 @@ public function __construct($controller, $action = CRM_Core_Action::NONE) { * * @param string $formName * - * @return string + * @return array * the name of the form that will handle the task */ public function taskName($controller, $formName = 'Search') {