| 1 |
<?php
|
| 2 |
// $Id: salesforce.module,v 1.6 2006/10/24 00:39:27 stevemckenzie Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Drupal and Salesforce.com (mainly only working with contacts / leads but can be extended to do anything the salesforce API version 6 can do)
|
| 7 |
* Maintainer & Developer: Steve McKenzie (Raincity Studios - www.raincitystudios.com)
|
| 8 |
* Last Updated: 23/10/06
|
| 9 |
*/
|
| 10 |
|
| 11 |
define('SALESFORCE_LEADS_CREATE_FORMS', variable_get('salesforce_leads_create_forms', "user_register\nuser_edit\n"));
|
| 12 |
define('SALESFORCE_EVENTS_CREATE_FORMS', variable_get('salesforce_events_create_forms', "user_register | user registration\nuser_edit | user edit form\n"));
|
| 13 |
|
| 14 |
require_once('includes/salesforce_api.inc');
|
| 15 |
|
| 16 |
/**
|
| 17 |
* Implementation of hook_help().
|
| 18 |
*/
|
| 19 |
function salesforce_help($section) {
|
| 20 |
switch ($section) {
|
| 21 |
case 'admin/modules#description':
|
| 22 |
return t('Drupal and Salesforce.com (mainly only working with contacts / leads but can do anything the salesforce API version 6 can do.)');
|
| 23 |
}
|
| 24 |
}
|
| 25 |
|
| 26 |
/**
|
| 27 |
* Implementation of hook_perm().
|
| 28 |
*/
|
| 29 |
function salesforce_perm() {
|
| 30 |
return array('access salesforce', 'administer salesforce');
|
| 31 |
}
|
| 32 |
|
| 33 |
/**
|
| 34 |
* Implementation of hook_menu().
|
| 35 |
*/
|
| 36 |
function salesforce_menu($may_cache) {
|
| 37 |
$items = array();
|
| 38 |
|
| 39 |
$items[] = array('path' => 'admin/salesforce',
|
| 40 |
'title' => t('salesforce'),
|
| 41 |
'access' => user_access('administer salesforce'),
|
| 42 |
'callback' => 'salesforce_admin_page');
|
| 43 |
|
| 44 |
$items[] = array('path' => 'admin/salesforce/logs',
|
| 45 |
'title' => t('logs'),
|
| 46 |
'access' => user_access('administer salesforce'),
|
| 47 |
'type' => MENU_DEFAULT_LOCAL_TASK,
|
| 48 |
'weight' => -1,
|
| 49 |
'callback' => 'salesforce_admin_page');
|
| 50 |
|
| 51 |
$items[] = array('path' => 'admin/salesforce/lead',
|
| 52 |
'title' => t('leads'),
|
| 53 |
'access' => user_access('administer salesforce'),
|
| 54 |
'type' => MENU_LOCAL_TASK,
|
| 55 |
'weight' => 0,
|
| 56 |
'callback' => 'salesforce_admin_page');
|
| 57 |
|
| 58 |
$items[] = array('path' => 'admin/salesforce/contact',
|
| 59 |
'title' => t('contacts'),
|
| 60 |
'access' => user_access('administer salesforce'),
|
| 61 |
'type' => MENU_LOCAL_TASK,
|
| 62 |
'weight' => 1,
|
| 63 |
'callback' => 'salesforce_admin_page');
|
| 64 |
|
| 65 |
$items[] = array('path' => 'admin/salesforce/log/delete',
|
| 66 |
'title' => t('delete log'),
|
| 67 |
'access' => user_access('administer salesforce'),
|
| 68 |
'type' => MENU_CALLBACK,
|
| 69 |
'callback' => 'salesforce_log_manage_page');
|
| 70 |
|
| 71 |
$items[] = array('path' => 'admin/salesforce/log/enable',
|
| 72 |
'title' => t('enable log'),
|
| 73 |
'access' => user_access('administer salesforce'),
|
| 74 |
'type' => MENU_CALLBACK,
|
| 75 |
'callback' => 'salesforce_log_manage_page');
|
| 76 |
|
| 77 |
$items[] = array('path' => 'admin/salesforce/log/disable',
|
| 78 |
'title' => t('disable log'),
|
| 79 |
'access' => user_access('administer salesforce'),
|
| 80 |
'type' => MENU_CALLBACK,
|
| 81 |
'callback' => 'salesforce_log_manage_page');
|
| 82 |
|
| 83 |
$items[] = array('path' => 'admin/salesforce/log/run',
|
| 84 |
'title' => t('run logged entry'),
|
| 85 |
'access' => user_access('administer salesforce'),
|
| 86 |
'type' => MENU_CALLBACK,
|
| 87 |
'callback' => 'salesforce_log_run_page');
|
| 88 |
|
| 89 |
return $items;
|
| 90 |
}
|
| 91 |
|
| 92 |
/**
|
| 93 |
* Implementation of hook_settings().
|
| 94 |
*/
|
| 95 |
function salesforce_settings() {
|
| 96 |
drupal_set_title(t('SalesForce.com Settings'));
|
| 97 |
|
| 98 |
_salesforce_settings();
|
| 99 |
|
| 100 |
$form['salesforce_account'] = array(
|
| 101 |
'#type' => 'fieldset',
|
| 102 |
'#title' => t('SalesForce.com Account'),
|
| 103 |
'#collapsible' => true,
|
| 104 |
'#description' => t('this information is required for this module to work')
|
| 105 |
);
|
| 106 |
$form['salesforce_account']['salesforce_user'] = array(
|
| 107 |
'#type' => 'textfield',
|
| 108 |
'#title' => t('Username'),
|
| 109 |
'#default_value' => variable_get('salesforce_user', NULL),
|
| 110 |
'#required' => true
|
| 111 |
);
|
| 112 |
|
| 113 |
// TODO: need to encrypt / decrypt this
|
| 114 |
if (!variable_get('salesforce_password', NULL)) {
|
| 115 |
$form['salesforce_account']['salesforce_password'] = array(
|
| 116 |
'#type' => 'password',
|
| 117 |
'#title' => t('Password'),
|
| 118 |
'#default_value' => variable_get('salesforce_password', NULL),
|
| 119 |
'#required' => true
|
| 120 |
);
|
| 121 |
} else {
|
| 122 |
$form['salesforce_account']['salesforce_password_delete'] = array(
|
| 123 |
'#prefix' => t('<strong>***** (TODO: setup like this until a better password saving feature can be implemented)</strong>'),
|
| 124 |
'#type' => 'checkbox',
|
| 125 |
'#title' => t('Delete Password'),
|
| 126 |
'#default_value' => variable_get('salesforce_password_delete', false)
|
| 127 |
);
|
| 128 |
}
|
| 129 |
|
| 130 |
$form['salesforce_options'] = array(
|
| 131 |
'#type' => 'fieldset',
|
| 132 |
'#title' => t('Additional Options'),
|
| 133 |
'#collapsible' => true
|
| 134 |
);
|
| 135 |
|
| 136 |
$form['salesforce_options']['salesforce_leads_create_forms'] = array(
|
| 137 |
'#type' => 'textarea',
|
| 138 |
'#title' => t('Create Leads on form processes'),
|
| 139 |
'#description' => t('a new line for each form_id (drupal 5.0 will make this feature much easier as I will be able to generate a select box with the names of the forms)'),
|
| 140 |
'#default_value' => SALESFORCE_LEADS_CREATE_FORMS
|
| 141 |
);
|
| 142 |
|
| 143 |
$form['salesforce_options']['salesforce_events_create_forms'] = array(
|
| 144 |
'#type' => 'textarea',
|
| 145 |
'#title' => t('Create Events on form processes'),
|
| 146 |
'#description' => t('a new line for each form_id | message (drupal 5.0 will make this feature much easier as I will be able to generate a select box with the names of the forms)'),
|
| 147 |
'#default_value' => SALESFORCE_EVENTS_CREATE_FORMS
|
| 148 |
);
|
| 149 |
|
| 150 |
return $form;
|
| 151 |
}
|
| 152 |
|
| 153 |
/**
|
| 154 |
* Implementation of hook_cron().
|
| 155 |
*/
|
| 156 |
function salesforce_cron() {
|
| 157 |
$result = db_query("SELECT sid, type, type_id, message, timestamp, status FROM {salesforce_log} WHERE status = 1 ORDER BY timestamp DESC");
|
| 158 |
if (db_num_rows($result) > 0) {
|
| 159 |
while ($row = db_fetch_object($result)) {
|
| 160 |
_salesforce_cron_run($row);
|
| 161 |
}
|
| 162 |
}
|
| 163 |
}
|
| 164 |
|
| 165 |
/**
|
| 166 |
* Implementation of hook_user().
|
| 167 |
*/
|
| 168 |
function salesforce_user($op, &$edit, &$account, $category = NULL) {
|
| 169 |
switch ($op) {
|
| 170 |
case 'load':
|
| 171 |
// create the salesforce array and append all salesforce id's associated to this account
|
| 172 |
$account->salesforce = array();
|
| 173 |
$ids = _salesforce_user_ids($account);
|
| 174 |
if (!empty($ids)) {
|
| 175 |
foreach ($ids as $id => $value) {
|
| 176 |
$account->salesforce[$id] = $value;
|
| 177 |
}
|
| 178 |
}
|
| 179 |
break;
|
| 180 |
case 'insert':
|
| 181 |
// we check on a form value so we can display a checkbox instead of a hidden field if it's an admin viewing the page
|
| 182 |
if ($edit['create_lead']) {
|
| 183 |
// must reload the object with user_load() to grab the updated profile fields
|
| 184 |
salesforce_lead('insert', user_load(array('uid' => $account->uid)), array('Description' => t('new site user: %date', array('%date' => format_date(time(), 'large')))));
|
| 185 |
}
|
| 186 |
break;
|
| 187 |
|
| 188 |
case 'update':
|
| 189 |
if (_salesforce_is_form('leads', 'user_edit')) {
|
| 190 |
// must reload the object with user_load() to grab the updated profile fields
|
| 191 |
$updated_account = user_load(array('uid' => $account->uid));
|
| 192 |
if ($account->salesforce['contact_id']) {
|
| 193 |
salesforce_contact('update', $updated_account);
|
| 194 |
} else if ($account->salesforce['lead_id']) {
|
| 195 |
salesforce_lead('update', $updated_account, array('Description' => t('last updated account information: %date', array('%date' => format_date(time(), 'large')))));
|
| 196 |
} else {
|
| 197 |
// we check on a form value so we can display a checkbox instead of a hidden field if it's an admin viewing the page
|
| 198 |
if ($edit['create_lead']) {
|
| 199 |
// must reload the object with user_load() to grab the updated profile fields
|
| 200 |
salesforce_lead('insert', user_load(array('uid' => $account->uid)), array('Description' => t('admin adding lead: %date', array('%date' => format_date(time(), 'large')))));
|
| 201 |
}
|
| 202 |
}
|
| 203 |
}
|
| 204 |
break;
|
| 205 |
case 'view':
|
| 206 |
break;
|
| 207 |
}
|
| 208 |
}
|
| 209 |
|
| 210 |
/**
|
| 211 |
* Implementation of hook_form_alter().
|
| 212 |
*/
|
| 213 |
function salesforce_form_alter($form_id, &$form) {
|
| 214 |
if (_salesforce_is_form('leads', $form_id)) {
|
| 215 |
// TODO: not needed yet but do we need this?
|
| 216 |
//$form['#submit']['salesforce_form_lead_submit'] = array();
|
| 217 |
}
|
| 218 |
|
| 219 |
if (_salesforce_is_form('events', $form_id)) {
|
| 220 |
$form['#submit']['salesforce_form_event_submit'] = array();
|
| 221 |
}
|
| 222 |
|
| 223 |
if (module_exist('contact') && $form_id == 'contact_mail_page') {
|
| 224 |
$form['#submit']['salesforce_contact_form'] = array();
|
| 225 |
}
|
| 226 |
|
| 227 |
if ($form_id == 'user_register' || $form_id == 'user_edit') {
|
| 228 |
$leads = _salesforce_is_form('leads', $form_id);
|
| 229 |
if (user_access('administer salesforce')) {
|
| 230 |
$uid = arg(1);
|
| 231 |
if (is_numeric($uid)) {
|
| 232 |
$account = user_load(array('uid' => $uid));
|
| 233 |
if ($account->salesforce['lead_id']) return;
|
| 234 |
}
|
| 235 |
|
| 236 |
if ($leads) {
|
| 237 |
$form['create_lead'] = array('#type' => 'checkbox', '#title' => t('create a lead in salesforce'), '#default_value' => true);
|
| 238 |
}
|
| 239 |
} else {
|
| 240 |
if ($leads) {
|
| 241 |
$form['create_lead'] = array('#type' => 'hidden', '#value' => true);
|
| 242 |
}
|
| 243 |
}
|
| 244 |
}
|
| 245 |
}
|
| 246 |
|
| 247 |
/**
|
| 248 |
* handle the contact form in a special way since we're building the $user object manually from the contact form values
|
| 249 |
*/
|
| 250 |
function salesforce_contact_form() {
|
| 251 |
global $user;
|
| 252 |
|
| 253 |
if ($user->uid > 0) {
|
| 254 |
$account->uid = $user->uid;
|
| 255 |
}
|
| 256 |
|
| 257 |
$name = explode(' ', $_POST['edit']['name']);
|
| 258 |
|
| 259 |
if (count($name) > 1 && count($name) < 2) {
|
| 260 |
$account->first_name = $name[0];
|
| 261 |
$account->last_name = $name[1];
|
| 262 |
} else {
|
| 263 |
$account->first_name = $_POST['edit']['name'];
|
| 264 |
$account->last_name = $_POST['edit']['name'];
|
| 265 |
}
|
| 266 |
$account->company = 'n/a';
|
| 267 |
$account->mail = $_POST['edit']['mail'];
|
| 268 |
$result = salesforce_lead('insert', $account, array('Description' => t('contact us form with the subject: %subject', array('%subject' => $_POST['edit']['subject']))));
|
| 269 |
}
|
| 270 |
|
| 271 |
/**
|
| 272 |
* all forms set to use lead / event actions receive these submit functions
|
| 273 |
* separated right now just because.. TODO: should probably be reduced 1 one function but we're waiting..
|
| 274 |
*/
|
| 275 |
function salesforce_form_lead_submit() {
|
| 276 |
$message =_salesforce_is_form('leads', $_POST['edit']['form_id'], true);
|
| 277 |
// TODO: ? (currently not needed but.. hmm...)
|
| 278 |
}
|
| 279 |
function salesforce_form_event_submit() {
|
| 280 |
global $user;
|
| 281 |
|
| 282 |
// only track logged in users since we need an account to associated them with
|
| 283 |
if ($user->uid == 0) return;
|
| 284 |
|
| 285 |
// grab our message to display as the description of this event in salesforce
|
| 286 |
$message =_salesforce_is_form('events', $_POST['edit']['form_id'], true);
|
| 287 |
// insert our event and yet again i find myself having to reload the user object to get my stuff? wtf?
|
| 288 |
$result = salesforce_event('insert', $_POST['edit']['form_id'], $message, user_load(array('mail' => $_POST['edit']['mail'])));
|
| 289 |
}
|
| 290 |
|
| 291 |
/**
|
| 292 |
* admin page for viewing the salesforce_logs table
|
| 293 |
*/
|
| 294 |
function salesforce_admin_page() {
|
| 295 |
theme('add_style', drupal_get_path('module', 'salesforce') .'/salesforce.css');
|
| 296 |
$op = arg(2);
|
| 297 |
|
| 298 |
if ($op) {
|
| 299 |
$breadcrumb = drupal_get_breadcrumb();
|
| 300 |
$breadcrumb[] = l(t('salesforce'), 'admin/salesforce');
|
| 301 |
drupal_set_breadcrumb($breadcrumb);
|
| 302 |
|
| 303 |
drupal_set_title(t('SalesForce - %op', array('%op' => ucfirst($op) .'s')));
|
| 304 |
switch ($op) {
|
| 305 |
default:
|
| 306 |
$output .= _salesforce_admin_list($op);
|
| 307 |
break;
|
| 308 |
break;
|
| 309 |
}
|
| 310 |
} else {
|
| 311 |
drupal_set_title(t('SalesForce'));
|
| 312 |
|
| 313 |
$result = db_query("SELECT sid, type, type_id, message, timestamp, status FROM {salesforce_log} ORDER BY timestamp DESC");
|
| 314 |
$total = db_num_rows($result);
|
| 315 |
|
| 316 |
$output .= '<p>'. t('All tasks that were not completed successfully are logged and displayed here. If you have a cron job setup to use drupal\'s cron.php, these logged items will be attempted again on cron run.') .'</p>'. "\n";
|
| 317 |
|
| 318 |
if ($total > 0) {
|
| 319 |
// table columns (damn these big ass arrays kinda bug me)
|
| 320 |
$cols = array(array(
|
| 321 |
'data' => t('ID'),
|
| 322 |
'class' => 'id'),
|
| 323 |
t('type'),
|
| 324 |
t('user'),
|
| 325 |
array('data' => 'message', 'class' => 'message'),
|
| 326 |
t('date'),
|
| 327 |
array('data' => t('status'), 'class' => 'status'),
|
| 328 |
array('data' => t('operations'), 'class' => 'op'));
|
| 329 |
|
| 330 |
while ($row = db_fetch_object($result)) {
|
| 331 |
if ($row->status) {
|
| 332 |
$state = 'active';
|
| 333 |
} else {
|
| 334 |
$state = 'completed';
|
| 335 |
}
|
| 336 |
// this row kinda looks nasty but ya.. here it is
|
| 337 |
$rows[] = array('class' => $state,
|
| 338 |
'data' => array($row->sid, str_replace('_', ' ', $row->type),
|
| 339 |
theme('username', user_load(array('uid' => $row->type_id))),
|
| 340 |
$row->message, format_date(strtotime($row->timestamp), 'large'), $state,
|
| 341 |
(($row->status) ?
|
| 342 |
l(t('run'), 'admin/salesforce/log/run/'. $row->sid) .' | ' .
|
| 343 |
l(t('disable'), 'admin/salesforce/log/disable/'. $row->sid, array('onclick' => 'if (confirm("'. t('are you sure you want to disable this log?') .'")) { return true; } else { return false; }')) .' | ' :
|
| 344 |
l(t('enable'), 'admin/salesforce/log/enable/'. $row->sid) .' | ') .
|
| 345 |
l(t('delete'), 'admin/salesforce/log/delete/'. $row->sid, array('onclick' => 'if (confirm("'. t('are you sure you want to delete this log?') .'")) { return true; } else { return false; }'))
|
| 346 |
)
|
| 347 |
);
|
| 348 |
}
|
| 349 |
|
| 350 |
$rows[] = array(array('class' => 'total', 'colspan' => 7, 'data' => t('<strong>Total:</strong> %total', array('%total' => $total))));
|
| 351 |
|
| 352 |
$output .= theme('table', $cols, $rows, array('class' => 'salesforce_admin'));
|
| 353 |
} else {
|
| 354 |
$output .= '<p><strong>'. t('currently no logged items') .'</strong></p>' ."\n";
|
| 355 |
}
|
| 356 |
}
|
| 357 |
|
| 358 |
print theme('page', $output);
|
| 359 |
}
|
| 360 |
|
| 361 |
/**
|
| 362 |
* delete or disable a logged item
|
| 363 |
*/
|
| 364 |
function salesforce_log_manage_page() {
|
| 365 |
$op = arg(3);
|
| 366 |
$sid = arg(4);
|
| 367 |
|
| 368 |
if (is_numeric($sid)) {
|
| 369 |
switch ($op) {
|
| 370 |
case 'disable':
|
| 371 |
$disable = true;
|
| 372 |
$message = t('disabled');
|
| 373 |
break;
|
| 374 |
case 'enable':
|
| 375 |
$enable = true;
|
| 376 |
$disable = false;
|
| 377 |
$message = t('enabled');
|
| 378 |
break;
|
| 379 |
default:
|
| 380 |
$disable = false;
|
| 381 |
$message = t('deleted');
|
| 382 |
break;
|
| 383 |
}
|
| 384 |
|
| 385 |
_salesforce_log_manage($sid, $disable, $enable);
|
| 386 |
if (user_access('administer salesforce')) {
|
| 387 |
drupal_set_message(t('%message log entry', array('%message' => $message)));
|
| 388 |
}
|
| 389 |
drupal_goto('admin/salesforce');
|
| 390 |
}
|
| 391 |
}
|
| 392 |
|
| 393 |
/**
|
| 394 |
* run a logged cron task
|
| 395 |
*/
|
| 396 |
function salesforce_log_run_page() {
|
| 397 |
$sid = arg(4);
|
| 398 |
|
| 399 |
if (is_numeric($sid)) {
|
| 400 |
$result = _salesforce_cron_run($sid);
|
| 401 |
if ($result) {
|
| 402 |
drupal_set_message(t('the logged task completed successfully'));
|
| 403 |
} else {
|
| 404 |
drupal_set_message(t('the logged task failed to complete'), 'error');
|
| 405 |
}
|
| 406 |
}
|
| 407 |
|
| 408 |
drupal_goto('admin/salesforce');
|
| 409 |
}
|
| 410 |
|
| 411 |
/**
|
| 412 |
* helper functions
|
| 413 |
*/
|
| 414 |
|
| 415 |
function _salesforce_admin_list($op) {
|
| 416 |
$sf = 'https://na1.salesforce.com/';
|
| 417 |
|
| 418 |
if ($op == 'lead' || $op == 'contact') {
|
| 419 |
$result = db_query("SELECT s.uid FROM {salesforce_users} s WHERE %s != ''", 's.'. $op .'_id');
|
| 420 |
if (db_num_rows($result) > 0) {
|
| 421 |
$cols = array(array('data' => t('User'), 'class' => 'user'), array('data' => t('%i', array('%i' => ucfirst($op) .' ID')), 'class' => 'id'));
|
| 422 |
if ($op == 'contact') {
|
| 423 |
$cols[] = array('data' => t('Account ID', array('%i' => ucfirst($op))), 'class' => 'id');
|
| 424 |
}
|
| 425 |
|
| 426 |
while ($row = db_fetch_object($result)) {
|
| 427 |
if ($op == 'contact') {
|
| 428 |
// we do this incase we don't have a cached account id
|
| 429 |
salesforce_account_select(user_load(array('uid' => $row->uid)));
|
| 430 |
}
|
| 431 |
$account = user_load(array('uid' => $row->uid));
|
| 432 |
// remove the extra 3 chars that are not used in this case
|
| 433 |
$link = $sf . substr($account->salesforce[$op.'_id'], 0, strlen($account->salesforce[$op.'_id']) - 3);
|
| 434 |
$rows[$row->uid] = array(theme('username', $account), l($account->salesforce[$op.'_id'], $link));
|
| 435 |
if ($op == 'contact') {
|
| 436 |
if ($account->salesforce['account_id']) {
|
| 437 |
$account_link = $sf . substr($account->salesforce['account_id'], 0, strlen($account->salesforce['account_id']) - 3);
|
| 438 |
$account_label = l($account->salesforce['account_id'], $account_link);
|
| 439 |
} else {
|
| 440 |
$account_label = t('N/A');
|
| 441 |
}
|
| 442 |
$rows[$row->uid][] = $account_label;
|
| 443 |
}
|
| 444 |
}
|
| 445 |
|
| 446 |
$output .= '<p>'. t('%type in salesforce', array('%type' => ucfirst($op) .'s')) .'</p>'. "\n";
|
| 447 |
$output .= theme('table', $cols, $rows, array('class' => 'salesforce_'. $op .'_list'));
|
| 448 |
} else {
|
| 449 |
drupal_set_message(t('currently no %op', array('%op' => $op .'s')));
|
| 450 |
}
|
| 451 |
}
|
| 452 |
|
| 453 |
return $output;
|
| 454 |
}
|
| 455 |
|
| 456 |
/**
|
| 457 |
* run a logged cron item
|
| 458 |
*/
|
| 459 |
function _salesforce_cron_run($row) {
|
| 460 |
if (is_numeric($row)) {
|
| 461 |
$result = db_query("SELECT sid, type, type_id, message, timestamp, status, data FROM {salesforce_log} WHERE status = 1 AND sid = %d ORDER BY timestamp DESC", $row);
|
| 462 |
if (db_num_rows($result) > 0) {
|
| 463 |
$row = db_fetch_object($result);
|
| 464 |
} else {
|
| 465 |
return false;
|
| 466 |
}
|
| 467 |
unset($result);
|
| 468 |
}
|
| 469 |
|
| 470 |
switch ($row->type) {
|
| 471 |
case 'lead_insert': case 'lead_update':
|
| 472 |
$result = salesforce_lead(str_replace('lead_', '', $row->type), user_load(array('uid' => $row->type_id)));
|
| 473 |
break;
|
| 474 |
case 'contact_insert': case 'contact_update':
|
| 475 |
$result = salesforce_contact(str_replace('contact_', '', $row->type), user_load(array('uid' => $row->type_id)));
|
| 476 |
break;
|
| 477 |
case 'event_insert':
|
| 478 |
// TODO: why do i have to do this? why doesn't it like the object?
|
| 479 |
$event = (array) unserialize($row->data);
|
| 480 |
$result = salesforce_event('insert', $event['values']['Subject'], $event['values']['Description'], user_load(array('uid' => $row->type_id)));
|
| 481 |
break;
|
| 482 |
default:
|
| 483 |
$result = array('error' => 'NO_METHOD_PROVIDED');
|
| 484 |
break;
|
| 485 |
}
|
| 486 |
|
| 487 |
// remove it from the cron runs if it went through
|
| 488 |
if (!$result['error'] || $result['error'] == 'NO_METHOD_PROVIDED') {
|
| 489 |
_salesforce_log_manage($row->sid);
|
| 490 |
watchdog('salesforce', t('removed log entry %sid', array('%sid' => l($row->sid, 'admin/salesforce'))));
|
| 491 |
|
| 492 |
if ($result['error'] == 'NO_METHOD_PROVIDED') {
|
| 493 |
db_query("UPDATE {salesforce_logs} l SET l.message = l.message + ' - (%s)' WHERE sid = %d", $result['error'], $row->sid);
|
| 494 |
if (user_access('administer salesforce')) {
|
| 495 |
drupal_set_message(t('the logged task with the message "%message" was disabled because it has no methods provided in the module. Please post this issue as a bug.', array('%message' => $row->message)));
|
| 496 |
}
|
| 497 |
}
|
| 498 |
return true;
|
| 499 |
}
|
| 500 |
}
|
| 501 |
|
| 502 |
/**
|
| 503 |
* used for inserting a single column into the salesforce_users table
|
| 504 |
*/
|
| 505 |
function _salesforce_insert($col, $value, $account = NULL) {
|
| 506 |
$account = _salesforce_select_account($account);
|
| 507 |
|
| 508 |
if (db_num_rows(db_query("SELECT * FROM {salesforce_users} WHERE uid = %d", $account->uid)) > 0) {
|
| 509 |
$result = db_query("UPDATE {salesforce_users} SET %s = '%s' WHERE uid = %d", $col, $value, $account->uid);
|
| 510 |
} else {
|
| 511 |
$result = db_query("INSERT INTO {salesforce_users} (uid, %s) VALUES (%d, '%s')", $col, $account->uid, $value);
|
| 512 |
}
|
| 513 |
|
| 514 |
if (db_affected_rows() > 0) {
|
| 515 |
return true;
|
| 516 |
} else {
|
| 517 |
return false;
|
| 518 |
}
|
| 519 |
}
|
| 520 |
|
| 521 |
/**
|
| 522 |
* check the settings to see if a form is allowed lead or event actions and return its message if needed
|
| 523 |
*/
|
| 524 |
function _salesforce_is_form($type, $form_id, $message = false) {
|
| 525 |
$ids = explode("\n", variable_get('salesforce_'. $type .'_create_forms', SALESFORCE_EVENTS_CREATE_FORMS));
|
| 526 |
foreach ($ids as $id) {
|
| 527 |
if (strchr($id, ' | ')) {
|
| 528 |
$result = explode(' | ', $id);
|
| 529 |
$id = $result[0];
|
| 530 |
$msg = $result[1];
|
| 531 |
}
|
| 532 |
if (trim($id) == $form_id) {
|
| 533 |
if ($msg && $message) {
|
| 534 |
return $msg;
|
| 535 |
} else {
|
| 536 |
return true;
|
| 537 |
}
|
| 538 |
}
|
| 539 |
}
|
| 540 |
}
|
| 541 |
|
| 542 |
/**
|
| 543 |
* handle logging of user errors
|
| 544 |
*/
|
| 545 |
function _salesforce_log_user($type, $message = NULL, $account = NULL, $status = 1, $data = NULL) {
|
| 546 |
$account = _salesforce_select_account($account);
|
| 547 |
|
| 548 |
if (!$message) {
|
| 549 |
$message = t('the connection to salesforce was probably lost');
|
| 550 |
}
|
| 551 |
|
| 552 |
$sql = "SELECT type_id FROM {salesforce_log} WHERE type_id = %d AND type = '%s'";
|
| 553 |
/*
|
| 554 |
if ($data) {
|
| 555 |
$sql .= " AND data = '%s'";
|
| 556 |
}
|
| 557 |
*/
|
| 558 |
$result = db_query($sql, $account->uid, $type, $data);
|
| 559 |
if ((int) db_num_rows($result) == 0) {
|
| 560 |
$cols = array('type', 'type_id', 'message', 'status');
|
| 561 |
$keys = array("'%s'", "'%s'", "'%s'", '%d');
|
| 562 |
|
| 563 |
/*
|
| 564 |
if ($data) {
|
| 565 |
$cols[] = 'data';
|
| 566 |
$keys[] = "'%s'";
|
| 567 |
}
|
| 568 |
*/
|
| 569 |
|
| 570 |
db_query("INSERT INTO {salesforce_log} (". implode(', ', $cols) .") VALUES (". implode(', ', $keys) .")", $type, $account->uid, $message, $status, $data);
|
| 571 |
watchdog('salesforce', t('%message - for the user %user', array('%message' => $message, '%account' => $account)));
|
| 572 |
}
|
| 573 |
}
|
| 574 |
|
| 575 |
function _salesforce_log_manage($sid, $disable = true, $enable = false) {
|
| 576 |
if ($disable) {
|
| 577 |
db_query("UPDATE {salesforce_log} SET status = 0 WHERE sid = %d", $sid);
|
| 578 |
} else if ($enable) {
|
| 579 |
db_query("UPDATE {salesforce_log} SET status = 1 WHERE sid = %d", $sid);
|
| 580 |
} else {
|
| 581 |
db_query("DELETE FROM {salesforce_log} WHERE sid = %d", $sid);
|
| 582 |
}
|
| 583 |
}
|
| 584 |
|
| 585 |
/**
|
| 586 |
* helper of hook_settings().. making things a little easier for the user
|
| 587 |
*/
|
| 588 |
function _salesforce_settings() {
|
| 589 |
// remove the password
|
| 590 |
if (variable_get('salesforce_password_delete', false)) {
|
| 591 |
variable_set('salesforce_password', NULL);
|
| 592 |
variable_set('salesforce_password_delete', false);
|
| 593 |
}
|
| 594 |
}
|
| 595 |
|
| 596 |
function _salesforce_user_ids($account = NULL) {
|
| 597 |
$account = _salesforce_select_account($account);
|
| 598 |
$result = db_query("SELECT lead_id, contact_id, opp_id, account_id FROM {salesforce_users} WHERE uid = %d", $account->uid);
|
| 599 |
|
| 600 |
if (db_num_rows($result) > 0) {
|
| 601 |
return db_fetch_array($result);
|
| 602 |
}
|
| 603 |
}
|
| 604 |
|
| 605 |
function _salesforce_select_account($account = NULL) {
|
| 606 |
if (!$account) {
|
| 607 |
global $user;
|
| 608 |
$account = user_load(array('uid' => $user->uid));
|
| 609 |
}
|
| 610 |
|
| 611 |
return $account;
|
| 612 |
}
|
| 613 |
|
| 614 |
/**
|
| 615 |
* theme functions
|
| 616 |
*/
|
| 617 |
|