Commit 13f8ed33 authored by Christian Wach's avatar Christian Wach
Browse files

Implement functionality of precedence array as constant

parent 971a7a77
......@@ -59,6 +59,15 @@ class CiviCRM_Network_Groups {
*/
public $civicrm;
/**
* Hooks object.
*
* @since 0.1
* @access public
* @var object $civicrm The Hooks object.
*/
public $hooks;
/**
* Contact Layout Editor Block object.
*
......@@ -116,6 +125,9 @@ class CiviCRM_Network_Groups {
// Load our CiviCRM-facing class.
require( CIVICRM_NETWORK_GROUPS_PATH . 'includes/civicrm-network-groups-civicrm.php' );
// Load our Hooks class.
require( CIVICRM_NETWORK_GROUPS_PATH . 'includes/civicrm-network-groups-hooks.php' );
// Load our "Extension" Utility class.
require( CIVICRM_NETWORK_GROUPS_PATH . 'assets/civicrm/block/civicrm-network-groups-cle-e.php' );
......@@ -139,6 +151,9 @@ class CiviCRM_Network_Groups {
// Always instantiate CiviCRM class.
$this->civicrm = new CiviCRM_Network_Groups_CiviCRM( $this );
// Always instantiate Hooks class.
$this->hooks = new CiviCRM_Network_Groups_Hooks( $this );
// Always instantiate CLE Block class.
$this->cle = new CiviCRM_Network_Groups_CLE( $this );
......
......@@ -46,11 +46,7 @@ class CiviCRM_Network_Groups_CiviCRM {
*/
public function initialise() {
// Filter groups to keep only those on the main site.
add_filter( 'civicrm_network_groups_synced_groups', array( $this, 'filter_by_main_site_groups' ), 10 );
// Filter groups to keep only those defined in our constant.
add_filter( 'civicrm_network_groups_synced_groups', array( $this, 'filter_by_defined_groups' ), 20 );
// None yet.
}
......@@ -115,26 +111,30 @@ class CiviCRM_Network_Groups_CiviCRM {
//##########################################################################
/**
* Get the Groups for a given CiviCRM contact ID.
*
* @since 0.1
*
* @param int $contact_id The numeric CiviCRM contact ID.
* @return array $user_groups The CiviCRM groups.
* @return array $groups The CiviCRM groups.
*/
public function group_ids_get_for_contact( $contact_id ) {
public function groups_get_for_contact( $contact_id ) {
// Init user groups.
$user_groups = array();
// Init groups.
$groups = array();
// Bail if no CiviCRM.
if ( ! $this->plugin->is_civicrm_initialised() ) {
return $user_groups;
return $groups;
}
// Get groups that the user is in.
$groups = civicrm_api( 'GroupContact', 'get', array(
$user_groups = civicrm_api( 'GroupContact', 'get', array(
'version' => 3,
'sequential' => 1,
'contact_id' => $contact_id,
......@@ -144,17 +144,50 @@ class CiviCRM_Network_Groups_CiviCRM {
));
// Sanity check.
if ( $groups['is_error'] == '0' AND absint( $groups['count'] ) > 0 ) {
if ( $user_groups['is_error'] == '0' AND absint( $user_groups['count'] ) > 0 ) {
// Build array of group IDs.
foreach( $groups['values'] AS $group ) {
$user_groups[] = $group['group_id'];
foreach( $user_groups['values'] AS $user_group ) {
$groups[] = $user_group;
}
}
// --<
return $user_groups;
return $groups;
}
/**
* Get the Group IDs for a given CiviCRM contact ID.
*
* @since 0.1
*
* @param int $contact_id The numeric CiviCRM contact ID.
* @return array $groups The CiviCRM group IDs.
*/
public function group_ids_get_for_contact( $contact_id ) {
// Init groups.
$groups = array();
// Get groups that the user is in.
$user_groups = $this->groups_get_for_contact( $contact_id );
// Bail if no groups.
if ( empty( $user_groups ) ) {
return $groups;
}
// Build array of group IDs.
foreach( $user_groups AS $user_group ) {
$groups[] = $user_group['group_id'];
}
// --<
return $groups;
}
......@@ -247,102 +280,6 @@ class CiviCRM_Network_Groups_CiviCRM {
/**
* Filter the groups to include only those on the main site.
*
* @since 0.1
*
* @param array $groups All CiviCRM groups with sync.
* @return array $groups The CiviCRM groups with sync on the main site.
*/
public function filter_by_main_site_groups( $civicrm_groups ) {
// Bail if none.
if ( empty( $civicrm_groups ) ) {
return $civicrm_groups;
}
// Init return.
$groups = array();
// Maybe switch to main site.
$switched = false;
if ( ! is_main_site() ) {
switch_to_blog( get_main_site_id() );
$switched = true;
}
// Filter by existence of corresponding "Groups" group.
foreach( $civicrm_groups AS $group ) {
$wp_group = civicrm_groups_sync()->wordpress->group_get_by_civicrm_id( $group['id'] );
if ( $wp_group !== false ) $groups[] = $group;
}
// Switch back.
if ( $switched === true ) {
restore_current_blog();
}
// --<
return $groups;
}
/**
* Filter the groups to include only those defined in the constant.
*
* The constant is: CIVICRM_NETWORK_GROUPS_PRECEDENCE
*
* @since 0.1
*
* @param array $groups The array of CiviCRM groups.
* @return array $groups The CiviCRM groups defined in the constant.
*/
public function filter_by_defined_groups( $civicrm_groups ) {
// Bail if none.
if ( empty( $civicrm_groups ) ) {
return $civicrm_groups;
}
// Bail if our constant is not defined.
if ( ! defined( 'CIVICRM_NETWORK_GROUPS_PRECEDENCE' ) ) {
return $civicrm_groups;
}
// Init return.
$groups = array();
// Filter by existence of in defined array.
foreach( $civicrm_groups AS $group ) {
// Get "Groups" group ID from source string.
$tmp = explode( 'synced-group-', $group['source'] );
$wp_group_id = isset( $tmp[1] ) ? absint( trim( $tmp[1] ) ) : false;
// Bail if there isn't one.
if ( $wp_group_id === false ) continue;
// Is it in our defined array?
if ( in_array( $wp_group_id, CIVICRM_NETWORK_GROUPS_PRECEDENCE ) ) {
$groups[] = $group;
}
}
// --<
return $groups;
}
//##########################################################################
/**
* Get a CiviCRM contact ID for a given WordPress user ID.
*
......
<?php
/**
* CiviCRM Network Groups Hooks class.
*
* A class which adds hooks to the results of queries in this plugin.
*
* This class essentially configures the plugin for the specific purpose that it
* has been written for. At some point, this class should be externalised into
* a plugin in its own right so that the base plugin can be more generic and
* therefore more widely applicable.
*
* @since 0.1
*/
class CiviCRM_Network_Groups_Hooks {
/**
* Plugin (calling) object.
*
* @since 0.1
* @access public
* @var object $plugin The plugin object.
*/
public $plugin;
/**
* Constructor.
*
* @since 0.1
*
* @param object $plugin The plugin object.
*/
public function __construct( $plugin ) {
// Store reference to plugin.
$this->plugin = $plugin;
// Initialise when plugin is loaded.
add_action( 'civicrm_network_groups_loaded', array( $this, 'initialise' ) );
}
/**
* Initialise this object.
*
* @since 0.1
*/
public function initialise() {
// Filter groups to keep only those on the main site.
add_filter( 'civicrm_network_groups_synced_groups', array( $this, 'filter_by_main_site_groups' ), 10 );
// Filter groups to keep only those defined in our constant.
add_filter( 'civicrm_network_groups_synced_groups', array( $this, 'filter_by_defined_groups' ), 20 );
// Filter groups by logged-in user precedence.
add_filter( 'civicrm_network_groups_synced_groups', array( $this, 'filter_by_precedence' ), 30 );
}
//##########################################################################
/**
* Filter the groups to include only those synced on the main site.
*
* @since 0.1
*
* @param array $groups All CiviCRM groups with sync.
* @return array $groups The CiviCRM groups with sync on the main site.
*/
public function filter_by_main_site_groups( $civicrm_groups ) {
// Bail if none.
if ( empty( $civicrm_groups ) ) {
return $civicrm_groups;
}
// Init return.
$groups = array();
// Maybe switch to main site.
$switched = false;
if ( is_multisite() AND ! is_main_site() ) {
switch_to_blog( get_main_site_id() );
$switched = true;
}
// Filter by existence of corresponding "Groups" group.
foreach( $civicrm_groups AS $group ) {
$wp_group = civicrm_groups_sync()->wordpress->group_get_by_civicrm_id( $group['id'] );
if ( $wp_group !== false ) $groups[] = $group;
}
// Switch back.
if ( $switched === true ) {
restore_current_blog();
}
// --<
return $groups;
}
/**
* Filter the groups to include only those defined in the constant.
*
* The constant is: CIVICRM_NETWORK_GROUPS_PRECEDENCE
*
* This method also returns the CiviCRM groups ordered by their position in
* the defined array in the constant.
*
* @since 0.1
*
* @param array $groups The array of CiviCRM groups.
* @return array $groups The CiviCRM groups defined in the constant.
*/
public function filter_by_defined_groups( $civicrm_groups ) {
// Bail if none.
if ( empty( $civicrm_groups ) ) {
return $civicrm_groups;
}
// Bail if our constant is not defined.
if ( ! defined( 'CIVICRM_NETWORK_GROUPS_PRECEDENCE' ) ) {
return $civicrm_groups;
}
// Init return.
$groups = array();
// Parse defined groups ordered by descending precedence.
foreach( CIVICRM_NETWORK_GROUPS_PRECEDENCE AS $defined_group_id ) {
// Filter by existence of in defined array.
foreach( $civicrm_groups AS $group ) {
// Get "Groups" group ID from source string.
$tmp = explode( 'synced-group-', $group['source'] );
$wp_group_id = isset( $tmp[1] ) ? absint( trim( $tmp[1] ) ) : false;
// Bail if there isn't one.
if ( $wp_group_id === false ) continue;
// Is it in our visible array?
if ( $wp_group_id == $defined_group_id ) {
$groups[] = $group;
}
}
}
// --<
return $groups;
}
/**
* Filter the groups to include only those with less precedence.
*
* Users will only be able to grant/remove membership of groups that are
* lower in the precedence stack than the one they themselves hold.
*
* @since 0.1.1
*
* @param array $groups The array of CiviCRM groups.
* @return array $groups The CiviCRM groups defined in the constant.
*/
public function filter_by_precedence( $civicrm_groups ) {
// Network Admins don't get groups filtered by precedence.
if ( is_super_admin() ) {
return $civicrm_groups;
}
// Bail if none.
if ( empty( $civicrm_groups ) ) {
return $civicrm_groups;
}
// Init "Groups" groups visible to the user/contact.
$groups = array();
// Get current user data.
$user = wp_get_current_user();
// Although unlikely, return empty array if user not found.
if ( ! ( $user instanceof WP_User ) ) {
return $groups;
}
// Get user's "Groups" groups.
$wp_group_ids = $this->plugin->wordpress->group_ids_get_for_user( $user->ID );
// Init allowed flag.
$allowed = false;
// Filter by existence in user's groups array.
foreach( $civicrm_groups AS $civicrm_group ) {
// Get "Groups" group ID from source string.
$tmp = explode( 'synced-group-', $civicrm_group['source'] );
$wp_group_id = isset( $tmp[1] ) ? absint( trim( $tmp[1] ) ) : false;
// Bail if there isn't one.
if ( $wp_group_id === false ) continue;
/*
* The logic here is that users will only be able to grant/remove
* membership of groups that are lower in the precedence stack than
* the one they themselves hold.
*
* To switch this to become "at the same level as themselves" move
* the "Add if allowed" block below the array check so that the
* group is added immediately.
*/
// Add if allowed.
if ( $allowed ) {
$groups[] = $civicrm_group;
}
// Is it in the array for the current user?
if ( in_array( $wp_group_id, $wp_group_ids ) ) {
$allowed = true;
}
}
// --<
return $groups;
}
} // Class ends.
......@@ -217,6 +217,64 @@ class CiviCRM_Network_Groups_WordPress {
/**
* Get the "Groups" groups for a given WordPress user ID.
*
* @since 0.1
*
* @param int $user_id The numeric WordPress user ID.
* @return array $groups The "Groups" groups for the WordPress user.
*/
public function groups_get_for_user( $user_id ) {
// Use "Groups" method to find user groups.
$user = new Groups_User( $user_id );
$groups = $user->groups;
// --<
return $groups;
}
/**
* Get the "Groups" group IDs for a given WordPress user ID.
*
* @since 0.1
*
* @param int $user_id The numeric WordPress user ID.
* @return array $groups The "Groups" group IDs for the WordPress user.
*/
public function group_ids_get_for_user( $user_id ) {
// Init user groups.
$groups = array();
// Get "Groups" groups for this user.
$user_groups = $this->groups_get_for_user( $user_id );
// Bail if no groups.
if ( empty( $user_groups ) ) {
return $groups;
}
// Build array of group IDs.
foreach( $user_groups AS $user_group ) {
$groups[] = $user_group->group_id;
}
// --<
return $groups;
}
//##########################################################################
/**
* Get a WordPress user ID for a given CiviCRM contact ID.
*
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment