Commit 419c2abb authored by RoSk0's avatar RoSk0

Added relation management screens.

parent dff6d5a0
<?php
/**
* Contact->User relation edit form.
*/
function crm_core_user_sync_edit_relation_form($form, &$form_state, $relation) {
if (empty($relation)) {
drupal_set_title('Create a new relation');
if (!empty($_GET['uid'])) {
$user = user_load($_GET['uid']);
$disabled_user_field = !empty($user);
}
if (!empty($_GET['contact_id'])) {
$contact = Contact::load($_GET['contact_id']);
$disabled_contact_field = !empty($contact);
}
}
else {
drupal_set_title('Edit relation');
if (!empty($relation->endpoints[LANGUAGE_NONE][0]['entity_id'])) {
$user = user_load($relation->endpoints[LANGUAGE_NONE][0]['entity_id']);
}
if (!empty($relation->endpoints[LANGUAGE_NONE][1]['entity_id'])) {
$contact = Contact::load($relation->endpoints[LANGUAGE_NONE][1]['entity_id']);
}
}
$form['relation'] = array(
'#type' => 'value',
'#value' => $relation,
);
$form['description'] = array(
'#type' => 'item',
'#markup' => t('You can edit the details of a contact record to user account relationship using the following form.'),
);
$form['contact'] = array(
'#title' => t('Contact record'),
'#type' => 'textfield',
'#default_value' => empty($contact) ? '' : $contact->label() . " [contact_id:{$contact->contact_id}]",
'#disabled' => empty($disabled_contact_field) ? FALSE : TRUE,
'#autocomplete_path' => 'admin/config/crm-core/user-sync/contact-to-user-management/contact-autocomplete',
);
$form['user'] = array(
'#title' => t('User record'),
'#type' => 'textfield',
'#default_value' => empty($user) ? '' : $user->name . " [uid:{$user->uid}]",
'#disabled' => empty($disabled_user_field) ? FALSE : TRUE,
'#autocomplete_path' => 'admin/config/crm-core/user-sync/contact-to-user-management/user-autocomplete',
);
$form['actions'] = array('#type' => 'actions');
$form['actions']['save'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
if (!empty($relation)) {
$form['actions']['delete'] = array(
'#type' => 'submit',
'#value' => t('Delete'),
);
}
$form['actions']['cancel'] = array(
'#markup' => l(t('Cancel'), 'admin/config/crm-core/user-sync/contact-to-user-management'),
);
return $form;
}
/**
* Validate callback on edit form.
*/
function crm_core_user_sync_edit_relation_form_validate($form, &$form_state) {
if ($form_state['values']['op'] == $form['actions']['save']['#value']) {
$user = _crm_core_user_sync_get_entity_id_from_text($form_state['values']['user'], 'user');
$contact = _crm_core_user_sync_get_entity_id_from_text($form_state['values']['contact'], 'crm_core_contact');
if (empty($user)) {
form_set_error('user', t('Could not load a user account.'));
}
if (empty($contact)) {
form_set_error('contact', t('Could not load a contact.'));
}
if (empty($user) || empty($contact)) {
return;
}
// Check if the same user->contact relation exists when adding or editing relation.
if (crm_core_user_sync_get_contact_from_uid($user->uid) && (empty($form_state['values']['relation']) ||
$form_state['values']['relation']->endpoints['und'][0]['entity_id'] != $user->uid)
) {
form_set_error('user', t('The user account has been already synchronized.'));
}
// Check if the same user->contact relation exists when adding or editing relation.
if (crm_core_user_sync_get_user_from_contact_id($contact->contact_id) && (empty($form_state['values']['relation']) ||
$form_state['values']['relation']->endpoints['und'][1]['entity_id'] != $contact->contact_id)
) {
form_set_error('contact', t('The contact record has been already synchronized.'));
}
}
}
/**
* Submit callback on edit form.
*/
function crm_core_user_sync_edit_relation_form_submit($form, &$form_state) {
if ($form_state['values']['op'] == $form['actions']['save']['#value']) {
$user = _crm_core_user_sync_get_entity_id_from_text($form_state['values']['user'], 'user');
$contact = _crm_core_user_sync_get_entity_id_from_text($form_state['values']['contact'], 'crm_core_contact');
if (!empty($form_state['values']['relation'])) {
$relation = $form_state['values']['relation'];
$relation->endpoints[LANGUAGE_NONE][0]['entity_id'] = $user->uid;
$relation->endpoints[LANGUAGE_NONE][1]['entity_id'] = $contact->contact_id;
$relation->save();
}
else {
$relation = Relation::create(array(
'relation_type' => 'crm_core_user_sync',
array(
array('entity_type' => 'user', 'entity_id' => $user->uid),
array(
'entity_type' => 'crm_core_contact',
'entity_id' => $contact->contact_id,
),
),
));
$relation->save();
}
$form_state['redirect'] = 'admin/config/crm-core/user-sync/contact-to-user-management';
drupal_set_message('Relation has been saved.');
}
elseif ($form_state['values']['op'] == $form['actions']['delete']['#value']) {
$relation = $form_state['values']['relation'];
$form_state['redirect'] = 'admin/config/crm-core/user-sync/contact-to-user-management/' . $relation->id() . '/delete';
}
}
/**
* User autocomplete for user->contact relation form.
*/
function crm_core_user_sync_user_autocomplete($search_string) {
$matches = array();
$query = db_select('users', 'user');
$on_cond = "endpoints.endpoints_entity_type = 'user' AND user.uid = endpoints.endpoints_entity_id";
$query->leftJoin('field_data_endpoints', 'endpoints', $on_cond);
$query->addField('user', 'uid');
$query->isNull('endpoints_entity_id');
$query->condition('name', $search_string . '%', 'LIKE');
watchdog('debug', $query->__toString());
$uids = $query->range(0, 20)->execute()->fetchCol();
if (!empty($uids)) {
$users = entity_load('user', $uids);
foreach ($users as $user) {
$matches[$user->name . " [uid:$user->uid]"] = $user->name;
}
}
drupal_json_output($matches);
}
/**
* User autocomplete for user->contact relation form.
*/
function crm_core_user_sync_contact_autocomplete($search_string) {
$matches = array();
$query = new EntityFieldQuery;
$query->entityCondition('entity_type', 'crm_core_contact')
->range(0, 20);
$query->addMetaData('match', $search_string)
->addTag(variable_get('crm_core_contact_search_query_tag', 'crm_core_contact_search'));
$query->addMetaData('user', FALSE);
$result = $query->execute();
if (!empty($result)) {
$contacts = entity_load('crm_core_contact', array_keys($result['crm_core_contact']));
foreach ($contacts as $contact) {
$crm_core_contact_title = $contact->label();
$matches[$crm_core_contact_title . " [contact_id:$contact->contact_id]"] = $crm_core_contact_title;
}
}
drupal_json_output($matches);
}
/**
* Delete relation form.
*/
function crm_core_user_sync_delete_relation_form($form, &$form_state, $relation) {
$form['relation'] = array(
'#type' => 'value',
'#value' => $relation,
);
$question = t('Are you sure you want to delete the relation?');
$path = 'admin/config/crm-core/user-sync/contact-to-user-management';
return confirm_form($form, $question, $path);
}
/**
* Submission handler for delete relation form.
*/
function crm_core_user_sync_delete_relation_form_submit($form, &$form_state) {
$relation = $form_state['values']['relation'];
Relation::load($relation->id())->delete();
$form_state['redirect'] = 'admin/config/crm-core/user-sync/contact-to-user-management';
drupal_set_message(t('Relation has been deleted.'));
}
......@@ -8,3 +8,4 @@ configure: crm_core_user_sync.sync
dependencies:
- crm_core:crm_core_contact
- drupal:views
crm_core_user_sync.config:
title: 'User Synchronization'
description: 'Manage CRM Core user synchronization settings.'
route_name: crm_core_user_sync.config
parent: crm_core.config
weight: 0
crm_core_user_sync.config:
title: Configuration
route_name: crm_core_user_sync.config
base_route: crm_core_user_sync.config
crm_core_user_sync.relations:
title: Relations
route_name: entity.crm_core_user_sync_relation.collection
base_route: crm_core_user_sync.config
weight: 3
......@@ -37,11 +37,11 @@ use Drupal\user\UserInterface;
* "uuid" = "uuid"
* },
* links = {
* "add-form" = "/admin/config/crm-core/user-sync/contact-to-user-management/add",
* "canonical" = "/admin/config/crm-core/user-sync/contact-to-user-management/{crm_core_user_sync_relation}",
* "edit-form" = "/admin/config/crm-core/user-sync/contact-to-user-management/{crm_core_user_sync_relation}/edit",
* "delete-form" = "/admin/config/crm-core/user-sync/contact-to-user-management/{crm_core_user_sync_relation}/delete",
* "collection" = "/admin/config/crm-core/user-sync/contact-to-user-management"
* "add-form" = "/admin/config/crm-core/user-sync/relation/add",
* "canonical" = "/admin/config/crm-core/user-sync/relation/{crm_core_user_sync_relation}",
* "edit-form" = "/admin/config/crm-core/user-sync/relation/{crm_core_user_sync_relation}/edit",
* "delete-form" = "/admin/config/crm-core/user-sync/relation/{crm_core_user_sync_relation}/delete",
* "collection" = "/admin/config/crm-core/user-sync/relation"
* }
* )
*/
......
......@@ -63,13 +63,14 @@ class RelationListBuilder extends EntityListBuilder {
* {@inheritdoc}
*/
public function render() {
$build['table'] = parent::render();
$total = \Drupal::database()
->query('SELECT COUNT(*) FROM {crm_core_user_sync_relation}')
->fetchField();
$build['summary']['#markup'] = $this->t('Total relations: @total', ['@total' => $total]);
$build['table'] = parent::render();
return $build;
}
......
Markdown is supported
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