Skip to content

Commit

Permalink
pre-release to be tested
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikHommel committed Dec 14, 2015
0 parents commit 44dc4f0
Show file tree
Hide file tree
Showing 7 changed files with 1,371 additions and 0 deletions.
57 changes: 57 additions & 0 deletions CRM/Groupprotect/BAO/GroupProtect.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

/**
* Class for Group Protect specific processing
*
* @author Erik Hommel (CiviCooP) <erik.hommel@civicoop.org>
* @date 8 Dec 2015
* @license AGPL-3.0
*/
class CRM_Groupprotect_BAO_GroupProtect {

// TODO: if group is_reserved -> set protect = yes

/**
* Method to process civicrm buildForm hook
*
* User can only manage group settings for non-protected groups or if user has permissions
*/
public static function buildForm($formName, $form) {
if ($formName == 'CRM_Group_Form_Edit') {
$protectGroupAllowed = CRM_Core_Permission::check('manage protected groups');
if (!$protectGroupAllowed) {
CRM_Core_Region::instance('page-body')->add(array('template' => 'GroupProtect.tpl'));
}
}
}

/**
* Method to process civicrm pre hook:
* If objectName = GroupContact and Group is a protected group, check if user has permission.
* When user does not have permission, redirect to user context with status message
*
*/
public static function pre($op, $objectName, $objectId, $params) {
if ($objectName == 'GroupContact' && self::groupIsProtected($objectId) == TRUE) {
if (!CRM_Core_Permission::check('manage protected groups')) {
CRM_Core_Session::setStatus(ts("You are not allowed to add or remove contacts to this group"), ts("Not allowed"), "error");
$session = CRM_Core_Session::singleton();
CRM_Utils_System::redirect($session->readUserContext());
}
}
}

/**
* Method to check if group is protected with custom field
*
* @param int $groupId
* @return boolean
* @throws Exception when API errors retrieving custom group and field
*/
private static function groupIsProtected($groupId) {
$config = CRM_Groupprotect_Config::singleton();
$query = "SELECT ".$config->getGroupProtectCustomField('column_name')." FROM ".
$config->getGroupProtectCustomGroup('table_name')." WHERE entity_id = %1";
return CRM_Core_DAO::singleValueQuery($query, array(1 => array($groupId, 'Integer')));
}
}
135 changes: 135 additions & 0 deletions CRM/Groupprotect/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php

/**
* Class following Singleton pattern for specific extension configuration
*
* @author Erik Hommel (CiviCooP) <erik.hommel@civicoop.org>
* @date 8 Dec 2015
* @license AGPL-3.0
*/
class CRM_Groupprotect_Config {

// singleton pattern
static private $_singleton = NULL;

// property holds the custom group for protected groups with custom fields in array ['custom_fields']
protected $_groupProtectCustomGroup = array();

/**
* CRM_Groupprotect_Config constructor.
*/
function __construct() {
$this->setGroupProtectCustomGroup();
}

/**
* Getter for custom group
*
* @return array
*/
public function getGroupProtectCustomGroup($key = NULL) {
if ($key) {
return $this->_groupProtectCustomGroup[$key];
} else {
return $this->_groupProtectCustomGroup;
}
}
/**
* Getter for group protect custom field
*
* @return array
*/
public function getGroupProtectCustomField($key = NULL) {
foreach ($this->_groupProtectCustomGroup['custom_fields'] as $customFieldId => $customField) {
if ($customField['name'] == 'group_protect') {
if ($key) {
return $customField[$key];
} else {
return $customField;
}
}
}
}

/**
* Gets or creates custom groups (with custom fields) for group_protect
*
* @throws API_Exception when unable to find or create custom group
*/
private function setGroupProtectCustomGroup() {
try {
$customGroup = civicrm_api3('CustomGroup', 'Getsingle', array('name' => 'group_protect'));
} catch (CiviCRM_API3_Exception $ex) {
$customGroupParams = array(
'name' => 'group_protect',
'extends' => 'Group',
'title' => 'Protect Group',
'table_name' => 'civicrm_value_group_protect',
'is_active' => 1,
'is_reserved' => 1);
try {
$createdGroup = civicrm_api3('CustomGroup', 'Create', $customGroupParams);
foreach ($createdGroup['values'] as $createdGroupId => $createdGroup) {
$customGroup = $createdGroup;
}
} catch (CiviCRM_API3_Exception $ex) {
throw new API_Exception('Could not create custom group "group_protect" nor find an existing one.
Error from API CustomGroup Create: '.$ex->getMessage(), 9010);
}
}
$customGroup['custom_fields'] = $this->setGroupProtectCustomField($customGroup['id']);
$this->_groupProtectCustomGroup = $customGroup;
}

/**
* Method to create custom field for group protect
* @param $customGroupId
* @return array
* @throws API_Exception
*/
private function setGroupProtectCustomField($customGroupId) {
$customField = array();
if (!empty($customGroupId)) {
$findFieldParams = array(
'name' => 'group_protect',
'custom_group_id' => $customGroupId);
try {
$retrievedField = civicrm_api3('CustomField', 'Getsingle', $findFieldParams);
$customField[$retrievedField['id']] = $retrievedField;
} catch (CiviCRM_API3_Exception $ex) {
$createFieldParams = array(
'custom_group_id' => $customGroupId,
'name' => 'group_protect',
'label' => 'Protect Group?',
'column_name' => 'group_protect',
'data_type' => 'Boolean',
'html_type' => 'Radio',
'is_searchable' => 1,
'is_active' => 1);
try {
$createdField = civicrm_api3('CustomField', 'Create', $createFieldParams);
foreach ($createdField['values'] as $createdFieldId => $createdField) {
$customField[$createdFieldId] = $createdField;
}
} catch (CiviCRM_API3_Exception $ex) {
throw new API_Exception('Could not create custom field "group_protect" nor find an existing one.
Error from API CustomField Create: '.$ex->getMessage(), 9011);
}
}
}
return $customField;
}
/**
* Function to return singleton object
*
* @return object $_singleton
* @access public
* @static
*/
public static function &singleton() {
if (self::$_singleton === NULL) {
self::$_singleton = new CRM_Groupprotect_Config();
}
return self::$_singleton;
}
}

0 comments on commit 44dc4f0

Please sign in to comment.