Commit 82bd9c90 authored by RoSk0's avatar RoSk0

Added configuration forms, controllers, routing, some cleanup.

parent 32a29f30
rules: {}
auto_sync_user_create: FALSE
contact_load: FALSE
contact_show: FALSE
# Schema for the configuration files of the CRM Core User Synchronization module.
crm_core_user_sync.settings:
type: config_object
label: 'CRM Core User Synchronization settings'
mapping:
rules:
type: sequence
label: 'Rules'
sequence:
type: crm_core_user_sync.rule
auto_sync_user_create:
type: boolean
label: 'Auto sync user create'
contact_load:
type: boolean
label: 'Load contact related to the current user'
contact_show:
type: boolean
label: 'Show related contact information on the user profile'
crm_core_user_sync.rule:
type: mapping
label: 'Rule'
mapping:
role:
type: string
label: 'Role'
contact_type:
type: string
label: 'Contact type'
enabled:
type: boolean
label: 'Enabled'
weight:
type: integer
label: 'Weight'
name: CRM Core User Synchronization
type: module
description: 'Synchronizes crm contacts with users.'
description: 'Synchronizes CRM contacts with users.'
package: CRM Core
core: 8.x
......@@ -8,4 +8,3 @@ configure: crm_core_user_sync.sync
dependencies:
- crm_core:crm_core_contact
- relation:relation
<?php
use Drupal\relation\Entity\RelationType;
/**
* Implements hook_install()
*/
function crm_core_user_sync_install() {
$t = get_t();
// Add relation type
$relation_type_info = array(
'relation_type' => 'crm_core_user_sync',
'label' => $t('Contact'),
'reverse_label' => $t('User'),
'source_bundles' => array('user:user'),
'target_bundles' => array('crm_core_contact:*'),
'r_unique' => TRUE,
'directional' => TRUE,
);
RelationType::create($relation_type_info)->save();
}
/**
* Implements hook_uninstall()
*/
function crm_core_user_sync_uninstall() {
relation_type_delete('crm_core_user_sync');
variable_del('crm_core_user_sync_rules');
variable_del('crm_core_user_sync_auto_sync_user_create');
}
/**
* Implements hook_enable().
*/
function crm_core_user_sync_enable() {
$menu_items = array(
array(
'link_path' => 'admin/config/crm-core/user-sync',
'link_title' => 'Administer User Synchronization',
'menu_name' => 'crm-core-admin-menu',
),
);
foreach ($menu_items as $item) {
menu_link_save($item);
}
}
/**
* Implements hook_disable().
*/
function crm_core_user_sync_disable() {
//Remove links from crm-core-menu and crm-core-admin-menu.
$conditions = array(
'crm-core-admin-menu' => array(
'admin/config/crm-core/user-sync'
),
);
crm_core_ui_remove_links($conditions);
}
administer crm-core-user-sync:
title: 'Administer User Synchronization'
description: 'Access to configuration pages for User Synchronization'
edit own contact information:
title: 'Edit own contact information'
description: 'Allows user to edit his/her own contact record from the user profile form'
crm_core_user_sync.config:
path: '/admin/config/crm-core/user-sync'
defaults:
_form: 'Drupal\crm_core_user_sync\Form\SettingsForm'
_title: 'User Synchronization'
requirements:
_permission: 'administer crm-core-user-sync'
crm_core_user_sync.settings:
path: '/admin/config/crm-core/user-sync/settings'
defaults:
_controller: '\Drupal\crm_core_user_sync\Controller\UserSyncController::adminMenuBlockPage'
_title: 'User Synchronization Settings'
requirements:
_permission: 'administer crm-core-user-sync'
crm_core_user_sync.rule.new:
path: '/admin/config/crm-core/user-sync/new'
defaults:
_title: 'Add a new rule'
_form: 'Drupal\crm_core_user_sync\Form\RuleForm'
requirements:
_permission: 'administer crm-core-user-sync'
crm_core_user_sync.rule.edit:
path: '/admin/config/crm-core/user-sync/{rule_key}/edit'
defaults:
_title: 'Edit rule'
_form: 'Drupal\crm_core_user_sync\Form\RuleForm'
requirements:
_permission: 'administer crm-core-user-sync'
rule_key: \d+
crm_core_user_sync.rule.delete:
path: '/admin/config/crm-core/user-sync/{rule_key}/delete'
defaults:
_title: 'Are you sure you want to delete this rule?'
_form: 'Drupal\crm_core_user_sync\Form\RuleDeleteConfirmForm'
requirements:
_permission: 'administer crm-core-user-sync'
rule_key: \d+
crm_core_user_sync.rule.enable:
path: '/admin/config/crm-core/user-sync/{rule_key}/enable'
defaults:
_title: 'Enable rule'
_controller: '\Drupal\crm_core_user_sync\Controller\RuleStatusController::enable'
requirements:
_permission: 'administer crm-core-user-sync'
crm_core_user_sync.rule.disable:
path: '/admin/config/crm-core/user-sync/{rule_key}/disable'
defaults:
_title: 'Disable rule'
_controller: '\Drupal\crm_core_user_sync\Controller\RuleStatusController::disable'
requirements:
_permission: 'administer crm-core-user-sync'
<?php
namespace Drupal\crm_core_user_sync\Controller;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
/**
* Returns responses for CRM Core User Synchronization routes.
*/
class RuleStatusController extends ControllerBase {
/**
* Current request.
*
* @var \Symfony\Component\HttpFoundation\Request
*/
protected $request;
/**
* Constructs the controller object.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* Current request.
* @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
* Config factory.
*/
public function __construct(Request $request, ConfigFactoryInterface $configFactory) {
$this->request = $request;
$this->configFactory = $configFactory;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('request_stack')->getCurrentRequest(),
$container->get('config.factory')
);
}
/**
* Enable rule.
*/
public function enable() {
$rule_key = $this->request->get('rule_key');
$rules = $this->configFactory->getEditable('crm_core_user_sync.settings')->get('rules');
$rules[$rule_key]['enabled'] = TRUE;
$this->configFactory->getEditable('crm_core_user_sync.settings')->set('rules', $rules)->save();
return $this->redirect('crm_core_user_sync.config');
}
/**
* Disable rule.
*/
public function disable() {
$rule_key = $this->request->get('rule_key');
$rules = $this->configFactory->getEditable('crm_core_user_sync.settings')->get('rules');
$rules[$rule_key]['enabled'] = FALSE;
$this->configFactory->getEditable('crm_core_user_sync.settings')->set('rules', $rules)->save();
return $this->redirect('crm_core_user_sync.config');
}
}
<?php
namespace Drupal\crm_core_user_sync\Form;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a confirmation form before clearing out the examples.
*/
class RuleDeleteConfirmForm extends ConfirmFormBase {
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static();
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'crm_core_user_sync_rule_delete_confirm_form';
}
/**
* {@inheritdoc}
*/
public function getQuestion() {
return $this->t('Are you sure you want to delete this rule?');
}
/**
* {@inheritdoc}
*/
public function getCancelUrl() {
return new Url('crm_core_user_sync.config');
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$rule_key = $this->getRequest()->get('rule_key');
$rules = $this->configFactory()->getEditable('crm_core_user_sync.settings')->get('rules');
unset($rules[$rule_key]);
// Re-key.
$rules = array_values($rules);
$this->configFactory()->getEditable('crm_core_user_sync.settings')->set('rules', $rules)->save();
$this->messenger()->addMessage('Rule was deleted');
$form_state->setRedirectUrl($this->getCancelUrl());
}
}
<?php
namespace Drupal\crm_core_user_sync\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\crm_core_contact\Entity\IndividualType;
/**
* Configure CRM Core User Synchronization settings for this site.
*/
class RuleForm extends ConfigFormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'crm_core_user_sync_rule_form';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return ['crm_core_user_sync.settings'];
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, $rule_key = 'new') {
$config = $this->config('crm_core_user_sync.settings');
$rules = $config->get('rules');
$rule = [
'role' => '',
'contact_type' => '',
'enabled' => TRUE,
'weight' => 0,
];
if ($rule_key === 'new') {
$rule = [
'role' => '',
'contact_type' => '',
'enabled' => TRUE,
'weight' => 0,
];
$form_state->set('rule_key', 'new');
}
else {
if (isset($rules[$rule_key])) {
$rule = $rules[$rule_key];
$form_state->set('rule', $rule);
$form_state->set('rule_key', $rule_key);
}
else {
$rule = [
'role' => '',
'contact_type' => '',
'enabled' => TRUE,
'weight' => 0,
];
$form_state->set('rule_key', 'new');
}
}
$types_options = ['' => t('- Select -')];
foreach (IndividualType::loadMultiple() as $type) {
$types_options[$type->id()] = $type->label();
}
$role_options = ['' => $this->t('- Select -')];
foreach (user_roles(TRUE) as $role) {
$role_options[$role->id()] = $role->label();
}
$form['role'] = array(
'#type' => 'select',
'#title' => t('User Role'),
'#options' => $role_options,
'#default_value' => isset($rule['role']) ? $rule['role'] : '',
'#required' => TRUE,
);
$form['contact_type'] = array(
'#type' => 'select',
'#title' => t('Contact Type'),
'#options' => $types_options,
'#default_value' => isset($rule['contact_type']) ? $rule['contact_type'] : '',
'#required' => TRUE,
);
$form['weight'] = array(
'#type' => 'weight',
'#title' => t('Weight'),
'#default_value' => isset($rule['weight']) ? $rule['weight'] : 0,
);
$form['enabled'] = array(
'#type' => 'checkbox',
'#title' => t('Enabled'),
'#default_value' => isset($rule['enabled']) ? $rule['enabled'] : TRUE,
'#description' => t('When checked, this rule will be used to synchronize user accounts. When unchecked, it will be ignored throughout the system.'),
);
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$rule = $form_state->get('rule');
foreach (['role', 'contact_type', 'weight', 'enabled'] as $key) {
$rule[$key] = $form_state->getValue($key);
}
$rules = $this
->config('crm_core_user_sync.settings')
->get('rules');
$rule_key = $form_state->get('rule_key');
if ($rule_key === 'new') {
$rules[] = $rule;
}
else {
$rules[$rule_key] = $rule;
}
$this->config('crm_core_user_sync.settings')
->set('rules', $rules)
->save();
$form_state->setRedirect('crm_core_user_sync.config');
parent::submitForm($form, $form_state);
}
}
<?php
namespace Drupal\crm_core_user_sync\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\crm_core_contact\Entity\IndividualType;
/**
* Configure crm_core_user_sync settings.
*/
class SettingsForm extends ConfigFormBase {
/**
* {@inheritdoc}
*/
public function getEditableConfigNames() {
return ['crm_core_user_sync.settings'];
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'crm_core_user_sync_settings_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$roles = user_roles(TRUE);
$types = IndividualType::loadMultiple();
$config = $this->config('crm_core_user_sync.settings');
$rules = $config->get('rules');
uasort($rules, [$this, 'weightCmp']);
$form['description'] = [
'#plain_text' => $this->t('CRM Core User Synchronization can automatically create contact records associated with user accounts under certain conditions.'),
];
$form['auto_sync_user_create'] = [
'#type' => 'checkbox',
'#title' => $this->t('Automatically create an associated contact when account is created'),
'#description' => $this->t('When checked, this checkbox will automatically create new contacts when a new user account is created according to rules listed above. Rules will be processed in order until a new contact is created.'),
'#default_value' => $config->get('auto_sync_user_create'),
];
$form['contact_load'] = [
'#type' => 'checkbox',
'#title' => $this->t('Load contact related to the current user'),
'#description' => $this->t('When checked, contact related to the current user will be loaded as part of the user account object in $account->crm_core["contact"]. In certain situations, loading contact data as part of a user entity can create performance issues (for instance, when there are hundreds of fields associated with each contact). Uncheck this box if it is creating problems with performance.'),
'#default_value' => $config->get('contact_load'),
];
$form['contact_show'] = [
'#type' => 'checkbox',
'#title' => $this->t('Show contact information'),
'#description' => $this->t('When checked, contact related to the current user will be shown on user profile page. Configurable from "Manage display" page.'),
'#default_value' => $config->get('contact_show'),
];
$form['rule_add'] = [
'#markup' => $this->l($this->t('Add new synchronization rule'), Url::fromRoute('crm_core_user_sync.rule.new')),
];
$form['rules'] = [
'#type' => 'table',
'#header' => [
$this->t('Role'),
$this->t('Contact type'),
$this->t('Status'),
$this->t('Operations'),
$this->t('Weight'),
],
'#tabledrag' => [
[
'action' => 'order',
'relationship' => 'sibling',
'group' => 'rule-weight',
],
],
'#empty' => $this->t('No rules configured'),
];
foreach ($rules as $key => $rule) {
$row = [];
$row['#attributes']['class'][] = 'draggable';
$row['#weight'] = $rule['weight'];
$row['role'] = ['#plain_text' => $roles[$rule['role']]->label()];
$row['contact_type'] = ['#plain_text' => $types[$rule['contact_type']]->label()];
$row['enabled'] = ['#plain_text' => $rule['enabled'] ? 'Enabled' : 'Disabled'];
$row['operations'] = [
'#type' => 'operations',
'#links' => [],
];
$row['weight'] = [
'#type' => 'weight',
'#title_display' => 'invisible',
'#default_value' => $rule['weight'],
'#attributes' => ['class' => ['rule-weight']],
];
$links = & $row['operations']['#links'];
$links['edit'] = [
'title' => 'Edit',
'url' => Url::fromRoute('crm_core_user_sync.rule.edit', ['rule_key' => $key]),
];
$links['delete'] = [
'title' => 'Delete',
'url' => Url::fromRoute('crm_core_user_sync.rule.delete', ['rule_key' => $key]),
];
if ($rule['enabled']) {
$links['disable'] = [
'title' => 'Disable',
'url' => Url::fromRoute('crm_core_user_sync.rule.disable', ['rule_key' => $key]),
];
}
else {
$links['enable'] = [
'title' => 'Enable',
'url' => Url::fromRoute('crm_core_user_sync.rule.enable', ['rule_key' => $key]),
];
}
$form['rules'][$key] = $row;
}
$form['crm_core_user_sync_wrapper'] = [
'#type' => 'details',
'#title' => $this->t('Sync Current Users'),
];
$form['crm_core_user_sync_wrapper']['user_sync'] = [
'#type' => 'submit',
'#value' => $this->t('Synchronize Users'),
'#submit' => ['crm_core_user_sync_admin_form_user_sync_submit'],
];
$form['crm_core_user_sync_wrapper']['description'] = [
'#type' => 'item',
'#markup' => $this->t('Click this button to apply user synchronization rules to all user accounts that are currently not associated with a contact in the system. It will create an associated contact record for each user according to the rules configured above. Warning: this cannot be undone.'),
];
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$config = $this->config('crm_core_user_sync.settings');
$rules = $config->get('rules');
$rule_values = $form_state->getValue('rules', []);
if (!empty($rule_values)) {
foreach ($rule_values as $key => $values) {
if (!empty($values['weight'])) {
$rules[$key]['weight'] = $values['weight'];
}
}
}
uasort($rules, [$this, 'weightCmp']);
$config
->set('rules', $rules)
->set('auto_sync_user_create', $form_state->getValue('auto_sync_user_create'))
->set('contact_load', $form_state->getValue('contact_load'))
->set('contact_show', $form_state->getValue('contact_show'))
->save();
parent::submitForm($form, $form_state);
}
/**
* Weight comparison callback.
*/
private function weightCmp($a, $b) {
if ($a['weight'] == $b['weight']) {
return 0;
}
return ($a['weight'] < $b['weight']) ? -1 : 1;
}
}
<?php
/**
* @file
* Provides support for the Views module.
*/
/**
* Implements hook_views_plugins().
*/
function crm_core_user_sync_views_plugins() {
return array(
'argument default' => array(
'crm_core_user_sync' => array(
'title' => t('CRM Contact ID (from user id from URL)'),
'handler' => 'views_plugin_argument_default_contact_id',
'parent' => 'views_plugin_argument_default',
),
),
);
}
<?php
/**
* @file
* Contains the user from URL argument default plugin.
*/
/**
* Default argument plugin to extract a user via menu_get_object
*/
class views_plugin_argument_default_contact_id extends views_plugin_argument_default {
function option_definition() {
$options = parent::option_definition();
$options['user'] = array('default' => '', 'bool' => TRUE, 'translatable' => FALSE);
return $options;
}
function options_form(&$form, &$form_state) {
$form['user'] = array(
'#type' => 'checkbox',
'#title' => t('Also look for a node and use the node author'),
'#default_value' => $this->options['user'],
);
}