Commit f5201554 authored by Christian Wach's avatar Christian Wach
Browse files

Introduce admin UI for selecting and ordering network groups

parent d4de2b9c
# CiviCRM Network Groups
A WordPress plugin that allows membership of "network groups" to be managed by offering options on the WordPress "Edit User" screen and in a Contact Layout block in the CiviCRM.
A WordPress plugin that allows membership of "network groups" to be managed by offering options on the WordPress "Edit User" screen and in a Contact Layout Editor block in CiviCRM.
The groups are synced between CiviCRM and the "Groups" plugin using the "CiviCRM Groups Sync" plugin. These "network-level" groups can be used, for example, to display different Contact Layouts to different users depending on their group membership.
......@@ -39,17 +39,7 @@ If you have cloned the code from GitLab, it is assumed that you know what you're
In order to use this plugin, you will need to create a number of "Groups" groups on the main site of your WordPress multisite instance. These groups should be synced to CiviCRM using the checkbox provided by the "CiviCRM Groups Sync" plugin.
Once you have created these, note down their IDs (as shown when you hover of their "Edit" links) and create an array of IDs in order of precedence. So, for example, if you created the following groups of decreasing precedence:
1. National Leaders ("Groups" ID: 3)
2. Regional Leaders ("Groups" ID: 5)
3. Local Leaders ("Groups" ID: 6)
Then your array should look like:
`define( 'CIVICRM_NETWORK_GROUPS_PRECEDENCE', array( 3, 4, 6 ) );`
You should put this code into your `wp-config.php` file so that this plugin can read it.
Once you have created these, visit the CiviCRM Network Groups "Settings" page in Network Admin. Here, you can edit the title given to Network Groups wherever it is displayed as well as select and order the groups you want to define as Network Groups.
----
......
......@@ -250,7 +250,7 @@ class CiviCRM_Network_Groups_CLE {
// Add our block now.
$block['civinetworkgroups']['blocks']['civinetworkgroups'] = [
'title' => ts('Network Groups'),
'title' => civicrm_network_groups_title(),
'tpl_file' => 'CRM/CiviNetworkGroups/Page/Inline/CiviNetworkGroups.tpl',
'sample' => [E::ts('Subject'), E::ts('Details'), E::ts('Call Result')],
'edit' => FALSE,
......
......@@ -65,7 +65,7 @@ class CRM_CiviNetworkGroups_Form_Inline_CiviNetworkGroups extends CRM_Contact_Fo
}
// Add network groups elements.
$this->addGroup($groups, 'network_groups', ts('Network Groups'), '&nbsp;<br/>');
$this->addGroup($groups, 'network_groups', civicrm_network_groups_title(), '&nbsp;<br/>');
// Build form elements.
$wrapper = array();
......
......@@ -46,6 +46,12 @@ class CRM_CiviNetworkGroups_Page_Inline_CiviNetworkGroups extends CRM_Core_Page
// Add network groups for this user.
$page->assign('network_user_groups', CRM_CiviNetworkGroups_Utils_CiviNetworkGroups::userGroups($contactId));
// Add title.
$page->assign('ng_title', civicrm_network_groups_title());
// Add "edit" title.
$page->assign('ng_title_edit', sprintf( __( 'Edit %s' ), civicrm_network_groups_title() ));
}
}
<div id="crm-civinetworkgroups-content" class="crm-inline-edit" {literal}data-edit-params='{"cid": {/literal}{$contactId}{literal}, "class_name": "CRM_CiviNetworkGroups_Form_Inline_CiviNetworkGroups"}'{/literal}>
<!-- templates/CRM/CiviNetworkGroups/Page/Inline/CiviNetworkGroups.tpl -->
<div class="crm-clear crm-inline-block-content" title="{ts escape='html'}Network Groups{/ts}">
<div class="crm-clear crm-inline-block-content" title="{$ng_title}">
<div class="crm-summary-row">
<div class="crm-label">{ts}Member of{/ts}</div>
<div class="crm-content crm-contact-network_groups">
......@@ -9,6 +9,6 @@
{/foreach}
</div>
</div>
<div class="crm-edit-help"><span class="crm-i fa-pencil"></span> {ts}Edit Network Groups{/ts}</div>
<div class="crm-edit-help"><span class="crm-i fa-pencil"></span> {$ng_title_edit}</div>
</div>
</div>
/**
* CiviCRM Network Groups "Settings" page Stylesheet.
*
* @package WordPress
* @subpackage CiviCRM_Network_Groups
*/
/* Style the sortable list */
.civicrm-network-groups-list {
margin: 1.5em 0;
}
/* Style the sortable list items */
.civicrm-network-groups-list li {
padding: 0.5em;
margin: 0.5em 0;
background: #fafafa;
border: 1px solid #f3f3f3;
}
/**
* CiviCRM Network Groups "Settings" page Javascript.
*
* @package WordPress
* @subpackage CiviCRM_Network_Groups
*/
/**
* Pass the jQuery shortcut in.
*
* @since 0.2
*
* @param {Object} $ The jQuery object.
*/
( function( $ ) {
/**
* Act on document ready.
*
* @since 0.2
*/
$(document).ready( function() {
// Make groups list sortable.
$('.civicrm-network-groups-list').sortable();
/**
* Receive data from the sortable "update" event.
*
* Should we want to enhance the UI, we could use this data to submit
* via AJAX. At present, it so happens that the order of the checkboxes
* changes the order of the items in the checkbox POST array, so we
* don't need to act on this here.
*
* @since 0.2
*
* @param {Object} event The event object.
* @param {Object} ui The UI object.
*/
$( '.civicrm-network-groups-list' ).on( 'sortupdate', function( event, ui ) {
// Get sorted list as string.
var list = $('.civicrm-network-groups-list').sortable( 'serialize' );
//console.log( 'list', list );
});
});
} )( jQuery );
<!-- assets/templates/network-settings.php -->
<div class="wrap">
<h1><?php _e( 'CiviCRM Network Groups', 'civicrm-network-groups' ); ?></h1>
<hr />
<?php if ( isset( $_GET['updated'] ) AND $_GET['updated'] == 'true' ) : ?>
<div id="setting-error-settings_updated" class="updated settings-error notice is-dismissible">
<p><strong><?php _e( 'Settings saved.', 'civicrm-network-groups' ); ?></strong></p>
<button type="button" class="notice-dismiss">
<span class="screen-reader-text"><?php _e( 'Dismiss this notice.', 'civicrm-network-groups' ); ?></span>
</button>
</div>
<?php endif; ?>
<form method="post" id="civicrm_network_groups_settings_form" action="<?php echo $this->admin_form_url_get(); ?>">
<?php wp_nonce_field( 'civicrm_network_groups_settings_action', 'civicrm_network_groups_settings_nonce' ); ?>
<h3><?php _e( 'General Settings', 'civicrm-network-groups' ); ?></h3>
<table class="form-table">
<tr>
<th scope="row">
<label class="civicrm_network_groups_settings_label" for="cng_title"><?php _e( 'Network Groups Title', 'civicrm-network-groups' ); ?></label>
</th>
<td>
<input type="text" class="regular-text" id="cng_title" name="cng_title" value="<?php echo $cng_title; ?>" />
<p class="description"><?php _e( 'Change the title of "Network Groups" to reflect the needs of your site. You might, for example, want to call them "Access Groups", "National Groups" or "Global Groups".', 'civicrm-network-groups' ); ?></p>
</td>
</tr>
</table>
<hr />
<h3><?php _e( 'Network Groups Configuration', 'civicrm-network-groups' ); ?></h3>
<p><?php _e( 'Choose your Network Groups and arrange them in order of priority.', 'civicrm-network-groups' ); ?></p>
<?php echo $groups_markup; ?>
<hr />
<p class="submit">
<input class="button-primary" type="submit" id="civicrm_network_groups_settings_submit" name="civicrm_network_groups_settings_submit" value="<?php _e( 'Save Changes', 'civicrm-network-groups' ); ?>" />
</p>
</form>
</div><!-- /.wrap -->
<!-- assets/templates/user-edit.php -->
<tr>
<th scope="row"><?php echo esc_html_e( 'Network Groups', 'civicrm-network-groups' ); ?></th>
<th scope="row"><?php echo civicrm_network_groups_title(); ?></th>
<td><?php echo $groups_markup; ?></td>
</tr>
......@@ -41,6 +41,15 @@ if ( ! defined( 'CIVICRM_NETWORK_GROUPS_PATH' ) ) {
*/
class CiviCRM_Network_Groups {
/**
* Admin object.
*
* @since 0.2
* @access public
* @var object $admin The Admin object.
*/
public $admin;
/**
* WordPress object.
*
......@@ -119,6 +128,9 @@ class CiviCRM_Network_Groups {
*/
public function include_files() {
// Load our Admin class.
require( CIVICRM_NETWORK_GROUPS_PATH . 'includes/civicrm-network-groups-admin.php' );
// Load our WordPress-facing class.
require( CIVICRM_NETWORK_GROUPS_PATH . 'includes/civicrm-network-groups-wordpress.php' );
......@@ -145,6 +157,9 @@ class CiviCRM_Network_Groups {
*/
public function setup_objects() {
// Always instantiate Admin class.
$this->admin = new CiviCRM_Network_Groups_Admin( $this );
// Always instantiate WordPress class.
$this->wordpress = new CiviCRM_Network_Groups_WordPress( $this );
......
<?php
/**
* CiviCRM Network Groups Admin class.
*
* A class which encapsulates admin functionality.
*
* @since 0.2
*/
class CiviCRM_Network_Groups_Admin {
/**
* Plugin (calling) object.
*
* @since 0.2
* @access public
* @var object $plugin The plugin object.
*/
public $plugin;
/**
* The installed version of the plugin.
*
* @since 0.2
* @access public
* @var str $plugin_version The plugin version stored as a numeric string.
*/
public $plugin_version;
/**
* Settings page.
*
* @since 0.2
* @access public
* @var str $settings_page The settings page.
*/
public $settings_page;
/**
* Settings data.
*
* @since 0.2
* @access public
* @var array $settings The plugin settings data.
*/
public $settings = array();
/**
* Constructor.
*
* @since 0.2
*
* @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.2
*/
public function initialise() {
// Assign installed plugin version.
$this->plugin_version = $this->option_get( 'civicrm_network_groups_version', false );
// Do upgrade tasks.
$this->upgrade_tasks();
// Store version for later reference if there has been a change.
if ( $this->plugin_version != CIVICRM_NETWORK_GROUPS_VERSION ) {
$this->option_set( 'civicrm_network_groups_version', CIVICRM_NETWORK_GROUPS_VERSION );
}
// Store default settings if none exist.
if ( ! $this->option_exists( 'civicrm_network_groups_settings' ) ) {
$this->option_set( 'civicrm_network_groups_settings', $this->settings_get_defaults() );
}
// Load settings array.
$this->settings = $this->option_get( 'civicrm_network_groups_settings', $this->settings );
// Upgrade settings.
$this->upgrade_settings();
// Lastly, register hooks.
$this->register_hooks();
}
/**
* Perform tasks when an upgrade is required.
*
* @since 0.2
*/
public function upgrade_tasks() {
/*
// For upgrades by version, use something like the following.
if ( version_compare( CIVICRM_NETWORK_GROUPS_VERSION, '0.3.4', '>=' ) ) {
// Do something
}
*/
}
/**
* Upgrade settings when required.
*
* @since 0.2
*/
public function upgrade_settings() {
// Don't save by default.
$save = false;
/*
// Some setting may not exist.
if ( ! $this->setting_exists( 'some_setting' ) ) {
// Add it from defaults.
$settings = $this->settings_get_defaults();
$this->setting_set( 'some_setting', $settings['some_setting'] );
$save = true;
}
*/
// Save settings if need be.
if ( $save === true ) {
$this->settings_save();
}
}
/**
* Register hooks.
*
* @since 0.2
*/
public function register_hooks() {
// Add menu item to Network Admin Settings submenu.
add_action( 'network_admin_menu', array( $this, 'admin_menu' ) );
}
//##########################################################################
/**
* Add Settings menu item for this plugin.
*
* @since 0.2
*/
public function admin_menu() {
// We must be network admin in multisite.
if ( is_multisite() AND ! is_super_admin() ) {
return;
}
// Check user permissions.
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
// Try and update settings.
$this->settings_update();
// Add the admin page to the Network Settings menu.
$this->settings_page = add_submenu_page(
'settings.php',
__( 'CiviCRM Network Groups: Settings', 'civicrm-network-groups' ), // Page title.
__( 'CiviCRM Network Groups', 'civicrm-network-groups' ), // Menu title.
'manage_options', // Required cap.
'civicrm_network_groups', // Slug name.
array( $this, 'page_settings' ) // Callback.
);
// Add utilities.
add_action( 'admin_head-' . $this->settings_page, array( $this, 'admin_head' ), 50 );
// Add scripts and styles.
add_action( 'admin_print_styles-' . $this->settings_page, array( $this, 'admin_css' ) );
add_action( 'admin_print_scripts-' . $this->settings_page, array( $this, 'admin_js' ) );
}
/**
* Initialise plugin help.
*
* @since 0.2
*/
public function admin_head() {
// Grab screen object.
$screen = get_current_screen();
// Prevent warning if screen not defined.
if ( empty( $screen ) ) return;
// Use method in this class.
$this->admin_help( $screen );
}
/**
* Adds help copy to admin pages.
*
* @since 0.2
*
* @param object $screen The existing WordPress screen object.
* @return object $screen The amended WordPress screen object.
*/
public function admin_help( $screen ) {
// Kick out if not our screen.
if ( $screen->id != $this->settings_page . '-network' ) {
return $screen;
}
// Add a tab - we can add more later.
$screen->add_help_tab( array(
'id' => 'civicrm_ng',
'title' => __( 'CiviCRM Network Groups', 'civicrm-network-groups' ),
'content' => $this->admin_help_text(),
));
// --<
return $screen;
}
/**
* Get help text.
*
* @since 0.2
*
* @return string $help Help formatted as HTML.
*/
public function admin_help_text() {
// Stub help text, to be developed further.
$help = '<p>' . __( 'For further information about using CiviCRM Network Groups, please refer to the README.md that comes with this plugin.', 'civicrm-network-groups' ) . '</p>';
// --<
return $help;
}
/**
* Enqueue stylesheet for this plugin's "Settings" page.
*
* @since 0.2
*/
public function admin_css() {
// Add stylesheet.
wp_enqueue_style(
'civicrm_network_groups_css',
plugins_url( 'assets/css/civicrm-network-groups-settings.css', CIVICRM_NETWORK_GROUPS_FILE ),
false,
CIVICRM_NETWORK_GROUPS_VERSION, // Version.
'all' // Media.
);
}
/**
* Enqueue Javascript for this plugin's "Settings" page.
*
* @since 0.2
*/
public function admin_js() {
// Enqueue javascript.
wp_enqueue_script(
'civicrm_network_groups_js',
plugins_url( 'assets/js/civicrm-network-groups-settings.js', CIVICRM_NETWORK_GROUPS_FILE ),
array( 'jquery', 'jquery-ui-core', 'jquery-ui-sortable' ),
CIVICRM_NETWORK_GROUPS_VERSION // Version.
);
}
//##########################################################################
/**
* Show our admin settings page.
*
* @since 0.2
*/
public function page_settings() {
// Only allow network admins through.
if( ! is_super_admin() ) {
wp_die( __( 'You do not have permission to access this page.', 'civicrm-network-groups' ) );
}
// Get default settings.
$defaults = $this->settings_get_defaults();
// Assign title with fallback if empty.
$cng_title = esc_attr( $this->setting_get( 'title' ) );
if ( empty( $cng_title ) ) {
$cng_title = esc_attr( $defaults['title'] );
}
// Init final markup.
$groups_markup = '';
// Get network groups.
$network_groups = $this->setting_get( 'precedence' );
// Remove filters.
$this->plugin->hooks->filters_remove();
// Get synced groups.
$synced_groups = $this->plugin->civicrm->groups_with_sync_get();
// Add filters.
$this->plugin->hooks->filters_add();
// Skip if we get no synced groups.
if ( ! empty( $synced_groups ) ) {
// Init groups and checkbox arrays.
$groups = array();
$checkboxes = array();