Commit 878136b1 authored by RoSk0's avatar RoSk0

Non required anymore - provided by core.

parent bb059263
.view-crm-core-contacts .views-field-nothing-3 a {
padding-right: 5px;
}
<?php
class MigrateDestinationCRMCoreContact extends MigrateDestinationEntity {
static public function getKeySchema() {
return array(
'contact_id' => array(
'type' => 'int',
'unsigned' => TRUE,
'description' => 'The primary identifer for a contact.',
),
);
}
/**
* Return an options array for contact destinations.
*
* @param string $language
* Default language for contacts created via this destination class.
* @param string $text_format
* Default text format for contacts created via this destination class.
*/
static public function options($language, $text_format) {
return compact('language', 'text_format');
}
public function __construct($bundle, array $options = array()) {
parent::__construct('crm_core_contact', $bundle, $options);
}
/**
* Returns a list of fields available to be mapped for the contact type (bundle)
*
* @param Migration $migration
* Optionally, the migration containing this destination.
* @return array
* Keys: machine names of the fields (to be passed to addFieldMapping)
* Values: Human-friendly descriptions of the fields.
*/
public function fields($migration = NULL) {
$fields = array();
// First the core (contact table) properties
$fields['contact_id'] = t('CRM Core Contact: Existing Contact ID');
$fields['created'] = t('Created timestamp');
$fields['changed'] = t('Modified timestamp');
$fields['uid'] = t('Authored by (uid)');
$fields += migrate_handler_invoke_all('Entity', 'fields', $this->entityType, $this->bundle, $migration);
return $fields;
}
/**
* Delete a batch of contacts at once.
*
* @param $contact_ids
* Array of contact IDs to be deleted.
*/
public function bulkRollback(array $contact_ids) {
migrate_instrument_start('crm_core_contact_delete_multiple');
$this->prepareRollback($contact_ids);
crm_core_contact_delete_multiple($contact_ids);
$this->completeRollback($contact_ids);
migrate_instrument_stop('crm_core_contact_delete_multiple');
}
/**
* Import a single contact.
*
* @param $contact
* Contact object to build. Prefilled with any fields mapped in the Migration.
* @param $row
* Raw source data object - passed through to prepare/complete handlers.
* @return array
* Array of key fields (contact_id only in this case) of the contact that was saved if
* successful. FALSE on failure.
*/
public function import(stdClass $contact, stdClass $row) {
$migration = Migration::currentMigration();
if (isset($row->migrate_map_destid1)) {
// This is an update to an existing contact.
if (isset($contact->contact_id)) {
if ($contact->contact_id != $row->migrate_map_destid1) {
$string = "Incoming contact_id !contact_id and map destination contact_id !destid1 don't match";
$params = array(
'!contact_id' => $contact->contact_id,
'!destid1' => $row->migrate_map_destid1,
);
throw new MigrateException(t($string), $params);
}
}
else {
$contact->contact_id = $row->migrate_map_destid1;
}
}
if ($migration->getSystemOfRecord() == Migration::DESTINATION) {
if (!isset($contact->contact_id)) {
throw new MigrateException(t('System-of-record is DESTINATION, but no destination contact_id provided'));
}
$old_contact = crm_core_contact_load($contact->contact_id);
if (empty($old_contact)) {
$string = 'System-of-record is DESTINATION, but contact !contact_id does not exist';
$params = array('!contact_id' => $contact->contact_id);
throw new MigrateException(t($string), $params);
}
}
if (!isset($contact->type)) {
// Default the type to our designated destination bundle (by doing this
// conditionally, we permit some flexibility in terms of implementing
// migrations which can affect more than one type).
$contact->type = $this->bundle;
}
// Set default properties.
if ($migration->getSystemOfRecord() == Migration::SOURCE) {
if (!isset($contact->created)) {
$contact->created = REQUEST_TIME;
}
}
$this->prepare($contact, $row);
if (isset($contact->contact_id)) {
$updating = TRUE;
}
else {
$updating = FALSE;
}
migrate_instrument_start('contact_save');
$contact->save();
migrate_instrument_stop('contact_save');
if (isset($contact->contact_id)) {
if ($updating) {
$this->numUpdated++;
}
else {
$this->numCreated++;
}
$return = array($contact->contact_id);
}
else {
$return = FALSE;
}
$this->complete($contact, $row);
return $return;
}
}
<?php
if (!module_exists('entityreference')) {
exit();
}
/**
* Overwrite entity reference handler class so autocomplete widget works with crm_core_contact
*/
class EntityReference_SelectionHandler_Generic_crm_core_contact extends EntityReference_SelectionHandler_Generic {
/**
* Build an EntityFieldQuery to get referencable entities.
*/
protected function buildEntityFieldQuery($match = NULL, $match_operator = 'CONTAINS') {
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', $this->field['settings']['target_type']);
if (!empty($this->field['settings']['handler_settings']['target_bundles'])) {
$query->entityCondition('bundle', $this->field['settings']['handler_settings']['target_bundles'], 'IN');
}
if (isset($match)) {
$query->addMetaData('match', $match);
$query->addMetaData('match_operator', $match_operator);
$query->addTag(variable_get('crm_core_contact_search_query_tag', 'crm_core_contact_search'));
// Fields are added in crm_core_contact_query_crm_core_contact_search_alter().
// You can override variable crm_core_contact_search_query_tag to change hook name.
}
// Add a generic entity access tag to the query.
$query->addTag($this->field['settings']['target_type'] . '_access');
$query->addTag('entityreference');
$query->addMetaData('field', $this->field);
$query->addMetaData('entityreference_selection_handler', $this);
// Add the sort option.
if (!empty($this->field['settings']['handler_settings']['sort'])) {
$sort_settings = $this->field['settings']['handler_settings']['sort'];
if ($sort_settings['type'] == 'property') {
$query->propertyOrderBy($sort_settings['property'], $sort_settings['direction']);
}
elseif ($sort_settings['type'] == 'field') {
list($field, $column) = explode(':', $sort_settings['field'], 2);
$query->fieldOrderBy($field, $column, $sort_settings['direction']);
}
}
return $query;
}
}
<?php
use Drupal\crm_core_contact\Entity\Contact;
/**
* @file
* Resource definitions for services module.
*/
/**
* Determine whether the current user can access a crm_core_contact resource.
*
* @param string $op
* Operation, access for which should be checked.
* @param mixed $arg
* Contact ID or contact data array, in case $op is 'create'.
*
* @return bool
* Boolean.
*
* @see crm_core_contact_access()
*/
function _crm_core_contact_resource_access($op, $arg) {
if (is_array($arg[0])) {
$contact_data = array_shift($arg);
return crm_core_contact_access($op, $contact_data['type']);
}
elseif (is_numeric($arg[0])) {
$crm_core_contact = Contact::load($arg[0]);
return crm_core_contact_access($op, $crm_core_contact);
}
elseif (!isset($arg)) {
return (user_access('administer crm_core_contact entities') || user_access('view any crm_core_contact entity'));
}
return FALSE;
}
/**
* Return an array of crm_core_contacts.
*
* @param int $page
* Page number of results to return (in pages of 20).
* @param int $page_size
* Integer number of items to be returned.
*
* @return array
* An array of node objects.
*/
function _crm_core_contact_resource_index($page, $page_size) {
$cids = db_select('crm_core_contact', 'c')
->orderBy('created', 'DESC')
->fields('c', array('contact_id'))
->range($page * $page_size, $page_size)
->execute()
->fetchCol();
$contacts = Contact::loadMultiple($cids);
return services_resource_build_index_list($contacts, 'crm_core_contact', 'contact_id');
}
/**
* Callback for the 'create' method.
*/
function _crm_core_contact_resource_create($contact_data) {
try {
$contact = entity_create('crm_core_contact', $contact_data);
unset($contact->is_new);
$contact->save();
return $contact;
}
catch (Exception $exception) {
watchdog_exception('crm_core_contact', $exception);
return services_error($exception, 406, $contact_data);
}
}
/**
* Callback for the 'update' method.
*/
function _crm_core_contact_resource_update($contact_id, $contact_data) {
return _crm_core_contact_resource_create($contact_data);
}
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