| 1 |
<?php
|
| 2 |
// $Id: dcl_importer.module,v 1.8 2009/07/10 00:01:51 hadsie Exp $
|
| 3 |
/**
|
| 4 |
* @file
|
| 5 |
* Imports contacts through OpenInviter library from various service providers.
|
| 6 |
*
|
| 7 |
* The module imports contacts from various service providers. This module
|
| 8 |
* in itself doesn't do anything in itself, but requires one of the connector
|
| 9 |
* modules in the modules/ folder to give it more functionality.
|
| 10 |
*/
|
| 11 |
|
| 12 |
/**
|
| 13 |
* Implementation of hook_help().
|
| 14 |
*/
|
| 15 |
function dcl_importer_help($path, $arg) {
|
| 16 |
switch ($path) {
|
| 17 |
case 'admin/help#dcl_importer':
|
| 18 |
return '<p>'. t('DCL Importer imports email contacts from various service providers.') .'</p>';
|
| 19 |
}
|
| 20 |
}
|
| 21 |
|
| 22 |
/**
|
| 23 |
* Implementation of hook_theme().
|
| 24 |
*/
|
| 25 |
function dcl_importer_theme() {
|
| 26 |
return array(
|
| 27 |
'dcl_importer_admin_plugin_table' => array(
|
| 28 |
'arguments' => array('form' => NULL),
|
| 29 |
),
|
| 30 |
'dcl_importer_existing_contacts_field' => array(
|
| 31 |
'arguments' => array('form' => NULL)
|
| 32 |
),
|
| 33 |
);
|
| 34 |
}
|
| 35 |
|
| 36 |
/**
|
| 37 |
* Implementation of hook_perm().
|
| 38 |
*/
|
| 39 |
function dcl_importer_perm() {
|
| 40 |
return array('access DCL Importer');
|
| 41 |
}
|
| 42 |
|
| 43 |
/**
|
| 44 |
* Implementation of hook_init().
|
| 45 |
*/
|
| 46 |
function dcl_importer_init() {
|
| 47 |
module_load_include('inc', 'dcl_importer', 'dcl_importer_api');
|
| 48 |
}
|
| 49 |
|
| 50 |
/**
|
| 51 |
* Implementation of hook_menu().
|
| 52 |
*/
|
| 53 |
function dcl_importer_menu() {
|
| 54 |
$items = array();
|
| 55 |
|
| 56 |
$items['admin/settings/dcl_importer'] = array(
|
| 57 |
'title' => t('DCL importer settings'),
|
| 58 |
'description' => t('Drupal contact list importer module settings'),
|
| 59 |
'page callback' => 'drupal_get_form',
|
| 60 |
'page arguments' => array('dcl_importer_admin'),
|
| 61 |
'access arguments' => array('access administration pages'),
|
| 62 |
'type' => MENU_NORMAL_ITEM,
|
| 63 |
);
|
| 64 |
|
| 65 |
return $items;
|
| 66 |
}
|
| 67 |
|
| 68 |
// Run user_access on several permissions
|
| 69 |
function dcl_importer_multi_user_access() {
|
| 70 |
foreach (func_get_args() as $arg) {
|
| 71 |
if (!user_access($arg)) {
|
| 72 |
return FALSE;
|
| 73 |
}
|
| 74 |
}
|
| 75 |
return TRUE;
|
| 76 |
}
|
| 77 |
|
| 78 |
|
| 79 |
function dcl_importer_admin() {
|
| 80 |
|
| 81 |
$form['openinviter_cookie_path'] = array(
|
| 82 |
'#type' => 'textfield',
|
| 83 |
'#title' => t('OpenInviter cookie path'),
|
| 84 |
'#default_value' => variable_get('openinviter_cookie_path', file_directory_temp()),
|
| 85 |
'#required' => TRUE,
|
| 86 |
);
|
| 87 |
|
| 88 |
$form['openinviter_transport'] = array(
|
| 89 |
'#type' => 'select',
|
| 90 |
'#title' => t('OpenInviter transport method'),
|
| 91 |
'#default_value' => variable_get('openinviter_transport', 'curl'),
|
| 92 |
'#description' => t('The library to use for fetching URLs.'),
|
| 93 |
'#required' => TRUE,
|
| 94 |
'#options' => array(
|
| 95 |
'curl' => t('curl'),
|
| 96 |
'wget' => t('wget'),
|
| 97 |
),
|
| 98 |
);
|
| 99 |
|
| 100 |
$form['openinviter_plugins'] = array(
|
| 101 |
'#type' => 'fieldset',
|
| 102 |
'#title' => t('OpenInviter Plugins'),
|
| 103 |
'#descriptions' => t('Select all of the services users should be allowed to import contacts from.'),
|
| 104 |
'#collapsible' => TRUE,
|
| 105 |
'#collapsed' => TRUE,
|
| 106 |
'#tree' => TRUE,
|
| 107 |
'#theme' => 'dcl_importer_admin_plugin_table',
|
| 108 |
);
|
| 109 |
|
| 110 |
$plugin_settings = variable_get('openinviter_plugins', NULL);
|
| 111 |
foreach (dcl_importer_get_providers(TRUE) as $plugin => $name) {
|
| 112 |
$default = $plugin_settings != NULL ? $plugin_settings[$plugin] : TRUE;
|
| 113 |
$form['openinviter_plugins'][$plugin] = array(
|
| 114 |
'#type' => 'checkbox',
|
| 115 |
'#title' => $name,
|
| 116 |
'#default_value' => $default,
|
| 117 |
);
|
| 118 |
}
|
| 119 |
|
| 120 |
return system_settings_form($form);
|
| 121 |
}
|
| 122 |
|
| 123 |
/**
|
| 124 |
* Implementation of hook_import_action().
|
| 125 |
*
|
| 126 |
* Use this hook to alter various import functionality.
|
| 127 |
* Parameters:
|
| 128 |
* $op The kind of action being performed. Possible values:
|
| 129 |
* * "form": alter the importer form (step 2 of the process) that lists the
|
| 130 |
* imported contacts.
|
| 131 |
* * "submit": add to the submit action of the importer form.
|
| 132 |
* * "contact_actions": add actions to perform to existing contacts
|
| 133 |
*
|
| 134 |
* &$a2
|
| 135 |
* * For "form" and "submit" this contains the "new" contacts
|
| 136 |
* (i.e. contacts not already in the system).
|
| 137 |
* * For "contact_actions" this contains the existing contacts in the system
|
| 138 |
*
|
| 139 |
* &$a3
|
| 140 |
* * For "form" this contains the "existing" contacts
|
| 141 |
* (i.e. contacts in the system).
|
| 142 |
* * For "submit" this field is not used
|
| 143 |
* * For "contact_actions" this contains the contacts in (uid, email) pairs
|
| 144 |
*
|
| 145 |
*/
|
| 146 |
function dcl_importer_import_action($op, &$a2, &$a3) {
|
| 147 |
if ($op == 'form') {
|
| 148 |
$form['dcl_importer']['existing'] = array(
|
| 149 |
'#value' => $a3,
|
| 150 |
'#theme' => 'dcl_importer_existing_contacts_field',
|
| 151 |
);
|
| 152 |
return $form;
|
| 153 |
}
|
| 154 |
}
|
| 155 |
|
| 156 |
function dcl_importer_form($form_state) {
|
| 157 |
$step = $form_state['storage']['step'] ? $form_state['storage']['step'] + 1 : 1;
|
| 158 |
|
| 159 |
switch ($step) {
|
| 160 |
case 1:
|
| 161 |
$form['dcl_importer']['info'] = array(
|
| 162 |
'#value' => '<strong>' . t('Import contacts from your existing social networking accounts and email address books.') . '</strong>',
|
| 163 |
);
|
| 164 |
|
| 165 |
$form['dcl_importer']['provider'] = array(
|
| 166 |
'#type' => 'select',
|
| 167 |
'#title' => t('Service Provider'),
|
| 168 |
'#default_value' => 'gmail',
|
| 169 |
'#options' => dcl_importer_get_providers(),
|
| 170 |
'#required' => TRUE,
|
| 171 |
'#description' => t('The service provider that is hosting your contacts'),
|
| 172 |
);
|
| 173 |
|
| 174 |
$form['dcl_importer']['username'] = array(
|
| 175 |
'#title' => t('Username'),
|
| 176 |
'#type' => 'textfield',
|
| 177 |
'#maxlength' => 44,
|
| 178 |
'#required' => TRUE,
|
| 179 |
);
|
| 180 |
|
| 181 |
$form['dcl_importer']['password'] = array(
|
| 182 |
'#title' => t('Password'),
|
| 183 |
'#type' => 'password',
|
| 184 |
'#maxlength' => 64,
|
| 185 |
'#required' => TRUE,
|
| 186 |
);
|
| 187 |
|
| 188 |
$form['dcl_importer']['submit'] = array(
|
| 189 |
'#type' => 'submit',
|
| 190 |
'#value' => t('Get My Contacts'),
|
| 191 |
);
|
| 192 |
break;
|
| 193 |
|
| 194 |
case 2:
|
| 195 |
// Get all form actions from import_action() hooks.
|
| 196 |
$imported_emails = $form_state['storage']['imported_emails'];
|
| 197 |
list($new, $existing) = dcl_importer_split_users($imported_emails);
|
| 198 |
$form = _dcl_importer_invoke_import_action('form', $new, $existing);
|
| 199 |
break;
|
| 200 |
}
|
| 201 |
$form['dcl_importer']['step'] = array(
|
| 202 |
'#type' => 'value',
|
| 203 |
'#value' => $step,
|
| 204 |
);
|
| 205 |
|
| 206 |
return $form;
|
| 207 |
}
|
| 208 |
|
| 209 |
function dcl_importer_form_validate($form, &$form_state) {
|
| 210 |
if ($form_state['values']['step'] == '1') {
|
| 211 |
// Authenticate and check for errors
|
| 212 |
list($inviter, $errors) = dcl_importer_authenticate(
|
| 213 |
$form_state['values']['provider'],
|
| 214 |
$form_state['values']['username'],
|
| 215 |
$form_state['values']['password']
|
| 216 |
);
|
| 217 |
if (!empty($errors)) {
|
| 218 |
foreach ($errors as $error) {
|
| 219 |
form_set_error('username', $error);
|
| 220 |
}
|
| 221 |
}
|
| 222 |
else {
|
| 223 |
$form_state['storage']['inviter'] = $inviter;
|
| 224 |
}
|
| 225 |
}
|
| 226 |
}
|
| 227 |
|
| 228 |
function dcl_importer_form_submit($form, &$form_state) {
|
| 229 |
if ($form_state['values']['step'] == '1') {
|
| 230 |
// Step 1: Import email contacts
|
| 231 |
$contacts = dcl_importer_get_contacts($form_state['storage']['inviter']);
|
| 232 |
$form_state['storage']['imported_emails'] = $contacts;
|
| 233 |
}
|
| 234 |
elseif ($form_state['values']['step'] == '2') {
|
| 235 |
// Step 2: Load contacts into the session and redirect to the invite form
|
| 236 |
$contacts = $form_state['values']['contacts'];
|
| 237 |
// Remove all contacts that weren't selected
|
| 238 |
$contacts = array_filter($contacts);
|
| 239 |
_dcl_importer_invoke_import_action('submit', $contacts, $form_state);
|
| 240 |
return;
|
| 241 |
}
|
| 242 |
$form_state['storage']['step'] = $form_state['values']['step'];
|
| 243 |
}
|
| 244 |
|
| 245 |
/**
|
| 246 |
* Theme function for the admin settings plugin table.
|
| 247 |
* Displays the plugin checkboxes as a table
|
| 248 |
*/
|
| 249 |
function theme_dcl_importer_admin_plugin_table(&$form) {
|
| 250 |
$plugins = array();
|
| 251 |
|
| 252 |
// Flatten forms array.
|
| 253 |
foreach (element_children($form) as $plugin) {
|
| 254 |
$plugins[] = drupal_render($form[$plugin]);
|
| 255 |
}
|
| 256 |
|
| 257 |
// Split checkboxes into rows with 3 columns.
|
| 258 |
$total = count($plugins);
|
| 259 |
$rows = array();
|
| 260 |
for ($i = 0; $i < $total; $i++) {
|
| 261 |
$row = array();
|
| 262 |
$row[] = array('data' => $plugins[$i]);
|
| 263 |
if (isset($plugins[++$i])) {
|
| 264 |
$row[] = array('data' => $plugins[$i]);
|
| 265 |
}
|
| 266 |
if (isset($plugins[++$i])) {
|
| 267 |
$row[] = array('data' => $plugins[$i]);
|
| 268 |
}
|
| 269 |
$rows[] = $row;
|
| 270 |
}
|
| 271 |
|
| 272 |
$output = theme('table', array(), $rows, array('width' => '100%'));
|
| 273 |
|
| 274 |
return $output;
|
| 275 |
}
|
| 276 |
|
| 277 |
/**
|
| 278 |
* Theme function for the existing contacts field
|
| 279 |
*/
|
| 280 |
function theme_dcl_importer_existing_contacts_field($form) {
|
| 281 |
// Setup the contacts array
|
| 282 |
$contacts = array();
|
| 283 |
foreach ($form['#value'] as $uid => $contact) {
|
| 284 |
$contacts[$uid] = array($contact['mail'], 'children' => array());
|
| 285 |
}
|
| 286 |
|
| 287 |
_dcl_importer_invoke_import_action('contact_actions', $contacts, $form['#value']);
|
| 288 |
|
| 289 |
// Flatten the $contacts array
|
| 290 |
$contacts_tmp = array();
|
| 291 |
foreach ($contacts as $contact) {
|
| 292 |
foreach ($contact['children'] as $action) {
|
| 293 |
$contacts_tmp[$contact[0]][] = $action;
|
| 294 |
}
|
| 295 |
}
|
| 296 |
// Convert back into the proper format
|
| 297 |
$contacts = array();
|
| 298 |
foreach ($contacts_tmp as $email => $children) {
|
| 299 |
$contacts[] = array($email, 'children' => $children);
|
| 300 |
}
|
| 301 |
|
| 302 |
return theme('item_list', $contacts, t('The following users are already members of this Drupal site'));
|
| 303 |
}
|
| 304 |
|
| 305 |
function _dcl_importer_invoke_import_action($op, &$a2 = NULL, &$a3 = NULL) {
|
| 306 |
$return = array();
|
| 307 |
$hook = 'import_action';
|
| 308 |
foreach (module_implements($hook) as $module) {
|
| 309 |
$function = $module . '_' . $hook;
|
| 310 |
$result = $function($op, $a2, $a3);
|
| 311 |
if (isset($result) && is_array($result)) {
|
| 312 |
$return = array_merge_recursive($return, $result);
|
| 313 |
}
|
| 314 |
elseif (isset($result)) {
|
| 315 |
$return[] = $result;
|
| 316 |
}
|
| 317 |
}
|
| 318 |
return $return;
|
| 319 |
}
|