salesforce_api.install in Salesforce Suite 7.2
Same filename and directory in other branches
Installs any global tables needed for Salesforce integration.
File
salesforce_api/salesforce_api.installView source
<?php
// $Id$
/**
* @file
* Installs any global tables needed for Salesforce integration.
*/
/**
* Implements hook_install().
*/
function salesforce_api_install() {
variable_set('salesforce_api_enabled_objects', array(
'Campaign',
'Contact',
'Lead',
));
// Do not show these messages if module is installed via Drush
if (!drupal_is_cli()) {
drupal_set_message(t('Salesforce API: Before making any Salesforce connections, please <a href="!url_wsdl">upload your WSDL</a> and <a href="!url_cred">enter your Salesforce API credentials</a>.', array(
'!url_wsdl' => url(SALESFORCE_PATH_UPDATE_WSDL),
'!url_cred' => url(SALESFORCE_PATH_ADMIN),
)), 'warning');
drupal_set_message(t('Salesforce API: The default Salesforce objects have been enabled. To export/import any other objects see the <a href="!url">Object setup</a> page.', array(
'!url' => url(SALESFORCE_PATH_OBJECT),
)), 'warning');
}
}
/**
* Implements hook_uninstall().
*/
function salesforce_api_uninstall() {
db_delete('variable')
->condition('name', 'salesforce%', 'LIKE')
->execute();
db_delete('cache')
->condition('cid', 'salesforce_api_sf_objects')
->execute();
}
/**
* Implements hook_schema().
*/
function salesforce_api_schema() {
require_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'salesforce_api') . '/salesforce_api.module';
// Object mapping table.
$schema['salesforce_object_map'] = array(
'description' => 'Drupal to Salesforce object mapping table',
'fields' => array(
'name' => array(
'description' => 'Foreign key for salesforce_field_map - the fieldmap that corresponds to this record.',
'type' => 'varchar',
'length' => 64,
),
'oid' => array(
'description' => 'Specific Drupal object identifier (e.g. node id or comment id)',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'sfid' => array(
'description' => 'Salesforce object identifier (e.g. 000000000000000000)',
'type' => 'varchar',
'length' => 32,
'not null' => TRUE,
'default' => '',
),
'drupal_entity' => array(
'description' => 'Drupal entity name (e.g. "node", "user")',
'type' => 'varchar',
'length' => 128,
'not null' => TRUE,
'default' => '',
),
'drupal_bundle' => array(
'description' => 'Drupal bundle name (e.g. "page", or vocabulary name)',
'type' => 'varchar',
'length' => 128,
'not null' => TRUE,
'default' => '',
),
'created' => array(
'description' => 'The Unix timestamp when the mapping was created.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
'last_import' => array(
'description' => 'The Unix timestamp when the record was last imported from Salesforce to Drupal.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
'last_export' => array(
'description' => 'The Unix timestamp when the record was last exported from Drupal to Salesforce.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
),
'indexes' => array(
'sfid' => array(
'sfid',
),
'name' => array(
'name',
),
'oid' => array(
'oid',
),
'entity_bundle_oid' => array(
'drupal_entity',
'drupal_bundle',
'oid',
),
),
// Primary key has been changed to the fieldmap:
// one fieldmapping for each Drupal object - cf. [#1018690-8]
// @todo: Write the 7.x upgrade routine to reflect this.
'primary key' => array(
'name',
'oid',
),
);
// Field mapping table.
$schema['salesforce_fieldmap'] = array(
'description' => 'Drupal to Salesforce field mappings',
'export' => array(
'key' => 'name',
'identifier' => 'salesforce_fieldmap',
'default hook' => 'default_salesforce_fieldmaps',
'export callback' => 'salesforce_api_salesforce_fieldmap_export',
),
'fields' => array(
'fieldmap' => array(
'description' => 'Numeric ID of the fieldmap. Exists for backwards compatibility only.',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
'no export' => TRUE,
),
'name' => array(
'description' => 'Unique ID for this object. Used to identify it programmatically, and make it exportable via CTools.',
'type' => 'varchar',
'length' => 64,
),
'automatic' => array(
'description' => 'Indicates whether this fieldmap automatically exports to Salesforce.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => SALESFORCE_AUTO_SYNC_OFF,
'size' => 'small',
),
'salesforce' => array(
'description' => 'The Salesforce object type for this fieldmap.',
'type' => 'varchar',
'length' => 32,
'not null' => TRUE,
'default' => '',
),
'drupal_entity' => array(
'description' => 'The Drupal entity for this fieldmap (e.g. "node", "user").',
'type' => 'varchar',
'length' => 128,
'not null' => TRUE,
'default' => '',
),
'drupal_bundle' => array(
'description' => 'The Drupal bundle for this fieldmap (e.g. "page", or vocabulary name)',
'type' => 'varchar',
'length' => 128,
'not null' => TRUE,
'default' => '',
),
'description' => array(
'description' => 'Name or brief description of this fieldmap',
'type' => 'varchar',
'length' => 255,
'not null' => FALSE,
'default' => '',
),
'fields' => array(
'description' => 'Serialized fieldmap',
'type' => 'text',
'not null' => TRUE,
'serialize' => TRUE,
),
),
'primary key' => array(
'fieldmap',
),
'unique keys' => array(
'name' => array(
'name',
),
),
);
return $schema;
}
/**
* Implements hook_requirements().
*
* Check the Salesforce API configuration to see if we have valid credentials.
*/
function salesforce_api_requirements($phase) {
$req = array();
if ($phase == 'runtime') {
require_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'salesforce_api') . '/salesforce_api.module';
$username = variable_get('salesforce_api_username', FALSE);
if (!salesforce_api_toolkit_installed()) {
$description = t('Salesforce module installed, but missing Salesforce PHP Toolkit. Please make sure Salesforce PHP Toolkit is available at ' . SALESFORCE_DIR_SOAPCLIENT . '/SforceEnterpriseClient.php');
$severity = REQUIREMENT_WARNING;
}
elseif (!$username) {
$description = l(t('Salesforce module installed but not configured. Please enter your credentials.'), SALESFORCE_PATH_ADMIN);
$severity = REQUIREMENT_WARNING;
}
elseif (!salesforce_api_connect()) {
$description = t('Unable to connect to Salesforce using <a href="!url">current credentials</a>.', array(
'!url' => url(SALESFORCE_PATH_ADMIN),
));
$severity = REQUIREMENT_WARNING;
}
elseif (!salesforce_api_encryption_available(array(
'check_config' => FALSE,
))) {
$description = t('Encryption is unavailable. Using encryption is recommended in order to better secure your data.');
$severity = REQUIREMENT_INFO;
}
elseif (!salesforce_api_encryption_available(array(
'check_config' => TRUE,
))) {
$description = t('Salesforce encryption is enabled but not configured securely.');
$severity = REQUIREMENT_WARNING;
}
else {
$description = t('Salesforce libraries and credentials OK.');
$severity = REQUIREMENT_OK;
}
}
if (!empty($description)) {
$req[] = array(
'title' => t('Salesforce Configuration'),
'value' => l('Salesforce Admin', SALESFORCE_PATH_ADMIN),
'description' => $description,
'severity' => $severity,
);
}
return $req;
}
/**
* We have fundamentally changed the schema of SF module. Primary identifier is
* now "name". Although the value of name is not particularly important, it must
* be set and it must be unique. In this update function, we are doing a few
* things:
* 1) Add "name" columns to salesforce_field_map and salesforce_object_map.
* 2) Assign unique values to "name" column in salesforce_field_map.
* 3) Assign "name" values to columns in salesforce_object_map.
* 4) Drop "fieldmap" column from salesforce_object_map.
*
*/
function salesforce_api_update_6201() {
module_enable(array(
'ctools',
));
if (!module_exists('ctools')) {
drupal_set_message(t('This version of Salesforce module depends on <a href="@ctools">Chaos Tool Suite</a>, which is not available. Please install Chaos Tool Suite, then run update again.', array(
'@ctools' => url('http://drupal.org/project/ctools'),
)), 'error');
return array(
array(
'success' => FALSE,
'query' => 'Chaos Tool Suite not found.',
),
);
}
$ret[] = array(
'success' => TRUE,
'query' => 'Chaos Tool Suite enabled.',
);
db_add_column($ret, 'salesforce_field_map', 'description', 'varchar(255)');
db_add_column($ret, 'salesforce_field_map', 'name', 'varchar(255)');
db_add_column($ret, 'salesforce_object_map', 'name', 'varchar(255)');
$result = db_query('SELECT fieldmap FROM {salesforce_field_map}');
while ($fieldmap = $result
->fetchField()) {
$name = md5($fieldmap);
$sql = 'UPDATE {salesforce_object_map} SET name = "%s" WHERE fieldmap = %d';
// TODO Please review the conversion of this statement to the D7 database API syntax.
/* db_query($sql, $name, $fieldmap) */
$update_result = db_update('salesforce_object_map')
->fields(array(
'name' => $name,
))
->condition('fieldmap', $fieldmap)
->execute();
$ret[] = array(
'success' => $update_result !== FALSE,
'query' => check_plain($sql),
);
$sql = 'UPDATE {salesforce_field_map} SET name = "%s" WHERE fieldmap = %d';
// TODO Please review the conversion of this statement to the D7 database API syntax.
/* db_query($sql, $name, $fieldmap) */
$update_result = db_update('salesforce_field_map')
->fields(array(
'name' => $name,
))
->condition('fieldmap', $fieldmap)
->execute();
$ret[] = array(
'success' => $update_result !== FALSE,
'query' => check_plain($sql),
);
}
db_add_index('salesforce_object_map', 'name', array(
'name',
));
db_add_unique_key('salesforce_field_map', 'name', array(
'name',
));
// TODO update_sql has been removed. Use the database API for any schema or data changes.
$ret[] = array();
// Set the WSDL directory
if (!($dir = variable_get('salesforce_api_dir_wsdl', FALSE))) {
$dir = SALESFORCE_DIR_WSDL;
$wsdl = $dir . '/enterprise.wsdl';
if (file_exists($wsdl)) {
variable_set('salesforce_api_dir_wsdl', $dir);
}
else {
variable_set('salesforce_api_dir_wsdl', SALESFORCE_DIR_SOAPCLIENT);
}
}
return t('Added CTools support to Salesforce Suite, making name the primary key.');
}
/**
* Try to fix existing maps based on the new CCK support.
*/
function salesforce_api_update_6202() {
$fieldmaps = salesforce_api_salesforce_fieldmap_load_all();
$ret = array();
$changes = array();
module_enable(array(
'sf_contrib',
));
$ret[] = array(
'success' => TRUE,
'query' => t('Enabled SF Contrib module', array(
'@map' => $new_map->name,
)),
);
foreach (content_types() as $type) {
// Add each of the fields to the node object definition.
foreach ((array) $type['fields'] as $field) {
if (empty($field['columns']['value'])) {
$changes[$field['field_name']] = array(
'old' => $field['field_name'],
'new' => $field['field_name'] . ':' . key($field['columns']),
);
}
if ($field['type'] == 'date' && !empty($field['todate'])) {
$changes[$field['field_name'] . '_todate'] = array(
'old' => $field['field_name'] . '_todate',
'new' => $field['field_name'] . ':value2',
);
}
}
}
foreach ($fieldmaps as $map) {
$new_map = $map;
$updates = FALSE;
foreach ($map->fields as $i => $field) {
if (!empty($changes[$field])) {
$new_map->fields[$i] = $changes[$field]['new'];
$updates = TRUE;
}
}
if ($updates) {
salesforce_api_fieldmap_save($new_map);
$ret[] = array(
'success' => TRUE,
'query' => t('Updated fieldmap @map', array(
'@map' => $new_map->name,
)),
);
}
}
return t('Attempted to fix fieldmaps using CCK support; you may need to review these manually.');
}
/**
* Set fieldmaps to automatic.
*/
function salesforce_api_update_6203() {
$ret = array();
db_change_field('salesforce_field_map', 'automatic', 'automatic', array(
'type' => 'int',
'size' => 'small',
'not null' => TRUE,
'default' => SALESFORCE_AUTO_SYNC_OFF,
));
$num_updated = db_update('salesforce_field_map')
->fields(array(
'automatic' => SALESFORCE_AUTO_SYNC_CREATE | SALESFORCE_AUTO_SYNC_UPDATE,
))
->execute();
return t('Set fieldmaps to automatic.');
}
/**
* Remove erroneous sf_user_skip_export elements from database.
*/
function salesforce_api_update_6204(&$sandbox) {
$ret = array();
if (empty($sandbox['uids'])) {
$where = " data like '%%sf_user_skip_export%%'";
$sandbox['n_users'] = 0;
$sandbox['uids'] = array();
// TODO Please convert this statement to the D7 database API syntax.
$result = db_query('SELECT uid FROM {users} WHERE ' . $where);
while ($uid = $result
->fetchField()) {
$sandbox['uids'][] = $uid;
$sandbox['n_users']++;
}
if (empty($sandbox['n_users'])) {
return array(
array(
'success' => TRUE,
'query' => t('No updates to process.'),
),
);
}
$sandbox['position'] = 0;
}
$uid = $sandbox['uids'][$sandbox['position']];
$account = user_load($uid);
$account->sf_user_skip_export = TRUE;
user_save($account, array(
'sf_user_skip_export' => NULL,
));
$sandbox['position']++;
$finished = $sandbox['position'] / $sandbox['n_users'];
if ($finished >= 1) {
return t('Fixed !n user accounts.', array(
'!n' => $sandbox['n_users'],
));
}
else {
return t('No user accounts needed to be updated.');
}
}
/**
* Alter {salesforce_object_map} to add last_import and last_export fields.
* @todo: Update existing items in {salesforce_object_map} with best guesses of
* last import and last export times.
*/
function salesforce_api_update_6205(&$sandbox) {
db_add_field('salesforce_object_map', 'created', array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'description' => 'The Unix timestamp when the mapping was created.',
));
db_add_field('salesforce_object_map', 'last_import', array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'description' => 'The Unix timestamp when the last import occurred for this item.',
));
db_add_field('salesforce_object_map', 'last_export', array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'description' => 'The Unix timestamp when the last export occurred for this item.',
));
return t('Updated the {salesforce_object_map} table to track object created, import, and export times.');
}
/**
* Adds in the new fields to salesforce_object_map if they are not already set.
* @todo: Add the upgrade path from 6.x to 7.x, immediately prior to this.
*/
function salesforce_api_update_7001(&$sandbox) {
$added_fields = array();
if (!db_field_exists('salesforce_object_map', 'created')) {
db_add_field('salesforce_object_map', 'created', array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'description' => 'The Unix timestamp when the mapping was created.',
));
$added_fields[] = 'created';
}
if (!db_field_exists('salesforce_object_map', 'last_import')) {
db_add_field('salesforce_object_map', 'last_import', array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'description' => 'The Unix timestamp when the last import occurred for this item.',
));
$added_fields[] = 'last_import';
}
if (!db_field_exists('salesforce_object_map', 'last_export')) {
db_add_field('salesforce_object_map', 'last_export', array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'description' => 'The Unix timestamp when the last export occurred for this item.',
));
$added_fields[] = 'last_export';
}
if (empty($added_fields)) {
return t('The created, last_import, and last_export fields already existed in {salesforce_object_map}. No action performed.');
}
else {
$added_fields = implode(', ', $added_fields);
return t('Added the following fields to {salesforce_object_map}: @fields', array(
'@fields' => $added_fields,
));
}
}
/**
* Adds index on oid
*/
function salesforce_api_update_7002(&$sandbox) {
db_add_index('salesforce_object_map', 'oid', array(
'oid',
));
}
Functions
Name | Description |
---|---|
salesforce_api_install | Implements hook_install(). |
salesforce_api_requirements | Implements hook_requirements(). |
salesforce_api_schema | Implements hook_schema(). |
salesforce_api_uninstall | Implements hook_uninstall(). |
salesforce_api_update_6201 | We have fundamentally changed the schema of SF module. Primary identifier is now "name". Although the value of name is not particularly important, it must be set and it must be unique. In this update function, we are doing a few things: 1)… |
salesforce_api_update_6202 | Try to fix existing maps based on the new CCK support. |
salesforce_api_update_6203 | Set fieldmaps to automatic. |
salesforce_api_update_6204 | Remove erroneous sf_user_skip_export elements from database. |
salesforce_api_update_6205 | Alter {salesforce_object_map} to add last_import and last_export fields. @todo: Update existing items in {salesforce_object_map} with best guesses of last import and last export times. |
salesforce_api_update_7001 | Adds in the new fields to salesforce_object_map if they are not already set. @todo: Add the upgrade path from 6.x to 7.x, immediately prior to this. |
salesforce_api_update_7002 | Adds index on oid |