CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Limiting contact search actions using hook_civicrm_searchTasks
Pages: [1]

Author Topic: Limiting contact search actions using hook_civicrm_searchTasks  (Read 953 times)

herb

  • I’m new here
  • *
  • Posts: 27
  • Karma: 2
Limiting contact search actions using hook_civicrm_searchTasks
October 13, 2010, 11:36:18 am
We created a simple module called customSearchTasks to limit the available actions from a contact search. We used a Drupal hook called hook_civicrm_searchTasks() (http://wiki.civicrm.org/confluence/display/CRMDOC32/CiviCRM+hook+specification#CiviCRMhookspecification-hookcivicrmsearchTasks).

hook_civicrm_searchTasks( $objectType, &$tasks )

The $objectType can be 'Contact'. 'Participant', I think, is enabled also possible in CiviCRM 3.x. $tasks is the array of tasks. With a print_r($tasks) we got a list of available the tasks.

The code is simple enough. Define the permissions in hook_perm(), then in hook_civicrm_searchTasks check if the user looking at the search results form has the permission using user_access('name of permission'). If they don't then remove the task from the list with something like unset($task[1]).

Code: [Select]
<?php

function customSearchTasks_perm() {
return array(
'access add contacts to group search action',
'access remove contacts from group search action',
'access tag contacts search action',
'access untag contacts search action',
'access export contacts search action',
'access send email to contacts search action',
'access send sms to contacts search action',
'access delete contacts search action',
'access add contacts to household search action',
'access add contacts to organization search action',
'access record activity for contacts search action',
'access map contacts search action',
'access new smart group search action',
'access update smart group search action',
// unsetting this creates an error - conflict with other custom module?
//'access print contacts search action',
'access mailing labels search action',
'access batch update via profile search action', 
'access print pdf letter for contacts search action',
'access merge contacts search action', 
'access unhold emails search action', 
'access restore contacts search action',
'access delete permanently search action'
);
}

function 
customSearchTasks_civicrm_searchTasks ($objectType, &$tasks) {
global $user;

if ($objectType = "Contact") {
if (! user_access( 'access add contacts to group search action' )) {
unset($tasks[1]);
}
if  (! user_access( 'access remove contacts from group search action' )) {
unset($tasks[2]);
}
if  (! user_access( 'access tag contacts search action' )) {
unset($tasks[3]);
}
if  (! user_access( 'access untag contacts search action' )) {
unset($tasks[4]);
}
if  (! user_access( 'access export contacts search action' )) {
unset($tasks[5]);
}
if  (! user_access( 'access send email to contacts search action' )) {
unset($tasks[6]);
}
if  (! user_access( 'access send sms to contacts search action' )) {
unset($tasks[7]);
}
if  (! user_access( 'access delete contacts search action' )) {
unset($tasks[8]);
}
if  (! user_access( 'access add contacts to household search action' )) {
unset($tasks[9]);
}
if  (! user_access( 'access add contacts to organization search action' )) {
unset($tasks[10]);
}
if  (! user_access( 'access record activity for contacts search action' )) {
unset($tasks[11]);
}
if  (! user_access( 'access map contacts search action' )) {
unset($tasks[12]);
}
if  (! user_access( 'access new smart group search action' )) {
unset($tasks[13]);
}
if  (! user_access( 'access update smart group search action' )) {
unset($tasks[14]);
}
// unsetting this creates an error - conflict with other custom module?
//if  (! user_access( 'access print contacts search action' )) {
// unset($tasks[15]);
//}
if  (! user_access( 'access mailing labels search action' )) {
unset($tasks[16]);
}
if  (! user_access( 'access batch update via profile search action' )) {
unset($tasks[17]);
}
if  (! user_access( 'access print pdf letter for contacts search action' )) {
unset($tasks[19]);
}
if  (! user_access( 'access merge contacts search action' )) {
unset($tasks[21]);
}
if  (! user_access( 'access unhold emails search action' )) {
unset($tasks[22]);
}
if  (! user_access( 'access restore contacts search action' )) {
unset($tasks[23]);
}
if  (! user_access( 'access delete permanently search action' )) {
unset($tasks[24]);
}
//CRM_Core_Error::debug( $tasks );

}

}
Web Developer
Freeform Solutions

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Limiting contact search actions using hook_civicrm_searchTasks
October 13, 2010, 11:34:27 pm
Cool, thanks for sharing.

Is this hosted somewhere (drupal module, git hub..) ?

X+
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

herb

  • I’m new here
  • *
  • Posts: 27
  • Karma: 2
Re: Limiting contact search actions using hook_civicrm_searchTasks
October 14, 2010, 05:04:38 pm
It's not hosted anywhere public. The entirety of the module is in the original post. I have also put an example for hook_civicrm_searchTasks() in the wiki: http://wiki.civicrm.org/confluence/display/CRMDOC32/CiviCRM+hook+specification#CiviCRMhookspecification-hookcivicrmsearchTasks
Web Developer
Freeform Solutions

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Limiting contact search actions using hook_civicrm_searchTasks

This forum was archived on 2017-11-26.