Skip to content

Commit

Permalink
Added civirules action for adding and removing contacts from a protec…
Browse files Browse the repository at this point in the history
…ted group.
  • Loading branch information
jaapjansma committed Oct 19, 2016
1 parent b3a70c7 commit c02caaa
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 4 deletions.
17 changes: 15 additions & 2 deletions CRM/Groupprotect/BAO/GroupProtect.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@
*/
class CRM_Groupprotect_BAO_GroupProtect {

/**
* This variable is used to bypass the permission check. This is used
* from the action Add to Protected group in CiviRules. The action sets the bypass and then adds the contact
* to the group.
*
* @var bool
*/
private static $bypassPermissionCheck = false;

public static function bypassPermissionCheck() {
self::$bypassPermissionCheck = true;
}

/**
* Method to process civicrm buildForm hook
*
Expand Down Expand Up @@ -44,7 +57,7 @@ public static function pre($op, $objectName, $objectId, $params)
}
}
if (!$webFormRequest) {
if (!CRM_Core_Permission::check('manage protected groups')) {
if (!CRM_Core_Permission::check('manage protected groups') && !self::$bypassPermissionCheck) {
CRM_Core_Session::setStatus(ts("You are not allowed to add or remove contacts to this group"), ts("Not allowed"), "error");
// if from report, redirect to report instance
if (isset($request['q']) && substr($request['q'], 0, 15) == "civicrm/report/") {
Expand All @@ -71,4 +84,4 @@ private static function groupIsProtected($groupId) {
$config->getGroupProtectCustomGroup('table_name')." WHERE entity_id = %1";
return CRM_Core_DAO::singleValueQuery($query, array(1 => array($groupId, 'Integer')));
}
}
}
24 changes: 24 additions & 0 deletions CRM/Groupprotect/CiviRulesActions/Add.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* @author Jaap Jansma (CiviCooP) <jaap.jansma@civicoop.org>
* @license http://www.gnu.org/licenses/agpl-3.0.html
*/

class CRM_Groupprotect_CiviRulesActions_Add extends CRM_CivirulesActions_GroupContact_GroupContact {

/**
* Method to set the api action
*
* @return string
* @access protected
*/
protected function getApiAction() {
return 'create';
}

protected function alterApiParameters($params, CRM_Civirules_TriggerData_TriggerData $triggerData) {
CRM_Groupprotect_BAO_GroupProtect::bypassPermissionCheck();
return parent::alterApiParameters($params, $triggerData);
}

}
56 changes: 56 additions & 0 deletions CRM/Groupprotect/CiviRulesActions/Remove.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

/**
* Class for CiviRules Group Contact remove action.
*
* Adds a user to a group
*
* @author Jaap Jansma (CiviCooP) <jaap.jansma@civicoop.org>
* @license AGPL-3.0
*/

class CRM_Groupprotect_CiviRulesActions_Remove extends CRM_CivirulesActions_GroupContact_GroupContact {

/**
* Method to set the api action
*
* @return string
* @access protected
*/
protected function getApiAction() {
return 'delete';
}

/**
* Process the action
*
* @param CRM_Civirules_TriggerData_TriggerData $triggerData
* @access public
*/
public function processAction(CRM_Civirules_TriggerData_TriggerData $triggerData) {
$entity = $this->getApiEntity();
$action = $this->getApiAction();
$contactId = $triggerData->getContactId();

$action_params = $this->getActionParameters();
$group_ids = array();
if (!empty($action_params['group_id'])) {
$group_ids = array($action_params['group_id']);
} elseif (!empty($action_params['group_ids']) && is_array($action_params['group_ids'])) {
$group_ids = $action_params['group_ids'];
}
foreach($group_ids as $group_id) {
if (CRM_CivirulesConditions_Utils_GroupContact::isContactInGroup($contactId, $group_id)) {
$params = array();
$params['group_id'] = $group_id;

//alter parameters by subclass
$params = $this->alterApiParameters($params, $triggerData);

//execute the action
CRM_Groupprotect_BAO_GroupProtect::bypassPermissionCheck();
$this->executeApiAction($entity, $action, $params);
}
}
}
}
36 changes: 36 additions & 0 deletions CRM/Groupprotect/CiviRulesActions/actions.mgd.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* @author Jaap Jansma (CiviCooP) <jaap.jansma@civicoop.org>
* @license http://www.gnu.org/licenses/agpl-3.0.html
*/

if (_groupprotect_is_civirules_installed()) {
return array (
0 =>
array (
'name' => 'Civirules:Action.AddToProtectedGroup',
'entity' => 'CiviRuleAction',
'params' =>
array (
'version' => 3,
'name' => 'AddToProtectedGroup',
'label' => 'Add to protected group',
'class_name' => 'CRM_Groupprotect_CiviRulesActions_Add',
'is_active' => 1
),
),
1 =>
array (
'name' => 'Civirules:Action.RemoveFromProtectedGroup',
'entity' => 'CiviRuleAction',
'params' =>
array (
'version' => 3,
'name' => 'RemoveFromProtectedGroup',
'label' => 'Remove from protected group',
'class_name' => 'CRM_Groupprotect_CiviRulesActions_Remove',
'is_active' => 1
),
),
);
}
16 changes: 16 additions & 0 deletions groupprotect.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,22 @@ function groupprotect_civicrm_config(&$config) {
_groupprotect_civix_civicrm_config($config);
}

function _groupprotect_is_civirules_installed() {
$installed = false;
try {
$extensions = civicrm_api3('Extension', 'get');
foreach($extensions['values'] as $ext) {
if ($ext['key'] == 'org.civicoop.civirules' && $ext['status'] == 'installed') {
$installed = true;
}
}
return $installed;
} catch (Exception $e) {
return false;
}
return false;
}

/**
* Implements hook_civicrm_xmlMenu().
*
Expand Down
4 changes: 2 additions & 2 deletions info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
<url desc="Support">http://www.civicoop.org</url>
<url desc="Licensing">http://www.gnu.org/licenses/agpl-3.0.html</url>
</urls>
<releaseDate>2016-01-12</releaseDate>
<version>1.1</version>
<releaseDate>2016-10-19</releaseDate>
<version>1.2</version>
<develStage>beta</develStage>
<compatibility>
<ver>4.4</ver>
Expand Down

0 comments on commit c02caaa

Please sign in to comment.