/[drupal]/contributions/modules/crmapi/crmapi.module
ViewVC logotype

Contents of /contributions/modules/crmapi/crmapi.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.26 - (show annotations) (download) (as text)
Tue Nov 3 19:00:34 2009 UTC (3 weeks, 3 days ago) by seanr
Branch: MAIN
CVS Tags: HEAD
Changes since 1.25: +2 -2 lines
File MIME type: text/x-php
filter thank you page content
1 <?php
2 // $Id: crmapi.module,v 1.25 2009/07/06 17:49:00 seanr Exp $
3
4 /**
5 * @file
6 * This module provides an level of abstraction for interfacing with CRM systems
7 * and the ability to assign specific crms to sections of the site.
8 */
9
10 define("CRMAPI_DEFAULT", variable_get("crmapi_crmapi", NULL));
11 define("CRMAPI_USER_CRM", variable_get("crmapi_crmapi_users", NULL));
12
13 /* Hard include this form now. Give this back to the civicrm drupal module later. */
14 if (module_exists('civicrm')) {
15 include ('civicrm_crmapi.inc');
16 }
17
18
19 /**
20 * Implementation of hook_help().
21 */
22 function crmapi_help($path, $args) {
23 $output = '';
24 switch($path) {
25 case 'admin/help#crmapi':
26 $output = 'Some very useful help text';
27 break;
28 }
29 return $output;
30 }
31
32
33 /**
34 * Implementation of hook_theme().
35 */
36 function crmapi_theme() {
37 return array(
38 'crmapi_user_view' => array(
39 'arguments' => array('fields' => array()),
40 ),
41 'crmapi_contact_form' => array(
42 'arguments' => array('form' => array(), 'form_state' => array()),
43 ),
44 );
45 }
46
47
48 /**
49 * Implementation of hook_perm().
50 */
51 function crmapi_perm() {
52 return array('view user contact information', 'administer CRM API');
53 }
54
55
56 /**
57 * Implementation of hook_menu().
58 */
59 function crmapi_menu() {
60 $items = array();
61
62 $items['admin/settings/crmapi'] = array(
63 'title' => 'CRM API',
64 'description' => 'Summary and settings for the CRM API',
65 'page callback' => 'crmapi_settings',
66 'access arguments' => array('administer CRM API'),
67 'type' => MENU_NORMAL_ITEM,
68 );
69 $items['admin/settings/crmapi/enabled'] = array(
70 'title' => 'Enabled CRMs',
71 'type' => MENU_DEFAULT_LOCAL_TASK,
72 'access arguments' => array('administer CRM API'),
73 'weight' => 0,
74 );
75 $items['admin/settings/crmapi/sections'] = array(
76 'title' => 'Sections',
77 'description' => 'Assign CRM roles to sections.',
78 'page callback' => 'drupal_get_form',
79 'page arguments' => array('crmapi_settings_sections'),
80 'access arguments' => array('administer CRM API'),
81 'type' => MENU_LOCAL_TASK,
82 'weight' => 1,
83 );
84 $items['admin/settings/crmapi/fields'] = array(
85 'title' => 'Fields',
86 'description' => 'Choose which fields to display in each section.',
87 'page callback' => 'drupal_get_form',
88 'page arguments' => array('crmapi_settings_fields'),
89 'access arguments' => array('administer CRM API'),
90 'type' => MENU_LOCAL_TASK,
91 'weight' => 2,
92 );
93 $items['crmapi/thanks/%crmapi_section'] = array(
94 'title' => 'Thank You',
95 'page callback' => 'crmapi_thanks',
96 'page arguments' => array(2),
97 'access arguments' => array('access content'),
98 'type' => MENU_CALLBACK,
99 );
100 $items['crmapi/%crmapi_section'] = array(
101 'title callback' => 'crmapi_title',
102 'title arguments' => array(1),
103 'page callback' => 'drupal_get_form',
104 'page arguments' => array('crmapi_contact_form', NULL, 1),
105 'access arguments' => array('access content'),
106 'type' => MENU_CALLBACK,
107 );
108 return $items;
109 }
110
111 function crmapi_section_load($section) {
112 return $section;
113 }
114
115 function crmapi_title($section) {
116 $sections = _crmapi_get_sections();
117 return $sections[$section]['name'];
118 }
119
120 /**
121 * Settings page to handle setting up aspects of the system.
122 *
123 * I'm not convinced yet if a drupal settings form is fitting but its an easy
124 * starting point.
125 *
126 * @return array
127 * Drupal settings form.
128 */
129 function crmapi_settings() {
130 $crms = module_invoke_all('crmapi', 'meta');
131 $headers = array('Name', 'Description', 'Version');
132 foreach ($crms as $module => $meta) {
133 $rows[] = array($meta['name'], $meta['description'], $meta['version']);
134 }
135
136 return theme('table', $headers, $rows);
137
138 return system_settings_form($form);
139 }
140
141 /**
142 * Section settings. The goal here is to build a list of sections and then
143 * allow enabled CRM systems to be attached to
144 *
145 * @return unknown
146 */
147 function crmapi_settings_sections() {
148 $meta = _crmapi_get_enabled_crms();
149 $crms = array('' => '<none>');
150 foreach ($meta as $module => $info) {
151 $crms[$module] = $info['name'];
152 }
153 $crm_mods = array_keys($crms);
154
155 $sections = _crmapi_get_sections();
156
157 foreach($sections as $section) {
158 $form['crmapi_'. $section['module']] = array(
159 '#title' => $section['name'],
160 '#type' => 'select',
161 '#options' => $crms,
162 '#description' => $section['description'],
163 '#default_value' => variable_get('crmapi_'. $section['module'], $crm_mods[0]),
164 );
165 }
166
167 return system_settings_form($form);
168 }
169
170
171 /**
172 * Section settings. The goal here is to build a list of sections and then
173 * allow enabled CRM systems to be attached to
174 *
175 * @return unknown
176 */
177 function crmapi_settings_fields() {
178 $meta = _crmapi_get_enabled_crms();
179 foreach ($meta as $module => $info) {
180 $crms[$module] = $info['name'];
181 }
182 $crm_mods = array_keys($crms);
183 $sections = _crmapi_get_sections();
184
185 foreach($sections as $section) {
186 //print '<pre>'.print_r($section).'</pre>'; exit;
187 $fields = crmapi_contact_fields($section['type']);
188 $form['crmapi_'. $section['type']] = array(
189 '#title' => $section['name'],
190 '#type' => 'fieldset',
191 '#description' => $section['description'],
192 '#collapsible' => true,
193 '#collapsed' => true,
194 );
195
196 $form['crmapi_'. $section['type']]['crmapi_'.$section['type'].'_fields'] = array(
197 '#type' => 'fieldset',
198 '#title' => t('Visible Fields'),
199 '#description' => t('Select the fields that will be visible on the user profile forms.'),
200 '#collapsible' => true,
201 '#tree' => true,
202 );
203 $visible = variable_get('crmapi_'.$section['type'].'_fields', array());
204 foreach ($fields as $field => $info) {
205 $form['crmapi_'. $section['type']]['crmapi_'.$section['type'].'_fields'][$field] = array(
206 '#type' => (($info['#type'] == 'hidden') && !empty($info['#required'])) ? 'hidden' : 'checkbox',
207 '#title' => (($info['#type'] == 'hidden') && !empty($info['#required'])) ? NULL : $info['#title'],
208 '#default_value' => (!empty($visible[$field]) || !empty($info['#required'])) ? true : false,
209 '#disabled' => (!empty($info['#required'])) ? true : false,
210 );
211 if (!empty($info['#required'])) {
212 $form['crmapi_'. $section['type']]['crmapi_'.$section['type'].'_fields'][$field]['#value'] = true;
213 }
214 if (!empty($info['settings-disabled']) && (is_array($info['settings-values']))) {
215 $form['crmapi_'. $section['type']]['crmapi_'.$section['type'].'_fields'][$field]['#disabled'] = true;
216 $form['crmapi_'. $section['type']]['crmapi_'.$section['type'].'_fields'][$field]['#value'] = $info['settings-values']['visible'];
217 }
218 /*
219 // Show settings form
220 if ($info['settings']) {
221 $basic = array('#type' => 'fieldset',
222 '#title' => $info['#title'] . t(' Settings'),
223 );
224
225 $form['crmapi_'. $section['type']]['crmapi_'.$section['type'].'_fields'][$field . '_settings'] =
226 array_merge($basic, $info['settings']);
227 }
228 */
229 }
230
231
232 $form['crmapi_'. $section['type']]['crmapi_'.$section['type'].'_fields_required'] = array(
233 '#type' => 'fieldset',
234 '#title' => t('Required Fields'),
235 '#description' => t('Select the fields that will be visible on the user profile forms.'),
236 '#collapsible' => true,
237 '#tree' => true,
238 );
239 $required = variable_get('crmapi_'.$section['type'].'_fields_required', array());
240 foreach ($fields as $field => $info) {
241 $form['crmapi_'. $section['type']]['crmapi_'.$section['type'].'_fields_required'][$field] = array(
242 '#type' => ($info['#type'] == 'hidden') ? 'hidden' : 'checkbox',
243 '#title' => ($info['#type'] == 'hidden') ? NULL : $info['#title'],
244 '#default_value' => (!empty($required[$field]) || (!empty($info['#required']) && ($info['#type'] != 'hidden'))) ? true : false,
245 '#disabled' => (!empty($info['#required']) && ($info['#type'] != 'hidden')) ? true : false,
246 );
247 if (!empty($info['#required']) && ($info['#type'] != 'hidden')) {
248 $form['crmapi_'. $section['type']]['crmapi_'.$section['type'].'_fields_required'][$field]['#value'] = true;
249 }
250 if (!empty($info['settings-disabled']) && (is_array($info['settings-values']))) {
251 $form['crmapi_'. $section['type']]['crmapi_'.$section['type'].'_fields_required'][$field]['#disabled'] = true;
252 $form['crmapi_'. $section['type']]['crmapi_'.$section['type'].'_fields_required'][$field]['#value'] = $info['settings-values']['required'];
253 }
254 }
255
256
257 /* Field Settings Section */
258 $settings_base =
259 array('#type' => 'fieldset',
260 '#title' => t('Field Settings'),
261 '#description' => t('Configure field-specific settings for this form'),
262 '#tree' => true,
263 );
264
265 $current_settings = variable_get('crmapi_'.$section['type'].'_fields_settings', array());
266 //_debug($current_settings);
267 foreach($fields as $field => $info) {
268 if(!empty($info['settings'])) {
269 $settings[$field] = $info['settings'];
270 if (!empty($current_settings[$field]) && (is_array($current_settings[$field]))) {
271 foreach($current_settings[$field] as $field_setting_key => $field_setting_val) {
272 $settings[$field][$field_setting_key]['#default_value'] = $field_setting_val;
273 }
274 }
275 }
276 }
277 if(count($settings)) {
278 $form['crmapi_' . $section['type']]['crmapi_'.$section['type'].'_fields_settings'] =
279 array_merge($settings_base, $settings);
280 }
281 //_debug($settings, 'settings');
282
283 /* Thank You */
284 $form['crmapi_'. $section['type']]['crmapi_'.$section['type'].'_thankyou'] = array(
285 '#type' => 'textarea',
286 '#title' => t('Thank you message'),
287 '#default_value' => variable_get('crmapi_'.$section['type'].'_thankyou', $section['message']),
288 '#cols' => 40,
289 '#rows' => 10,
290 '#description' => t('This text appears when the user submits the !section form.', array('section' => $section['name'])),
291 );
292 }
293 $form['submit'] = array(
294 '#type' => 'submit',
295 '#value' => t('Save Fields'),
296 );
297
298 //_debug($form, 'Form as sent CRMAPI');
299 return $form;
300 }
301
302 function crmapi_settings_fields_submit($form, &$form_state) {
303 $meta = _crmapi_get_enabled_crms();
304 foreach ($meta as $module => $info) {
305 $crms[$module] = $info['name'];
306 }
307 $crm_mods = array_keys($crms);
308 $sections = _crmapi_get_sections();
309
310 //print '<pre>'.print_r($form_state['values'],1).'</pre>';
311
312 foreach($sections as $section) {
313 $fields = array();
314 foreach($form_state['values']['crmapi_' . $section['type'] . '_fields'] as $field => $value) {
315 if ($value == '1') { $fields[$field] = $field; }
316 }
317 //print '<pre>'.print_r($fields,1).'</pre>';
318 $required = array();
319 foreach($form_state['values']['crmapi_' . $section['type'] . '_fields_required'] as $field => $value) {
320 if ($value == '1') { $required[$field] = $field; }
321 }
322 variable_set('crmapi_'.$section['type'] . '_fields', $fields);
323 variable_set('crmapi_'.$section['type'] . '_fields_required', $required);
324 variable_set('crmapi_'.$section['type'] . '_fields_settings', $form_state['values']['crmapi_' . $section['type'] . '_fields_settings']);
325 variable_set('crmapi_'.$section['type'] . '_thankyou', $form_state['values']['crmapi_' . $section['type'] . '_thankyou']);
326
327 //print '<pre>'.print_r(variable_get('crmapi_'.$section['type'] . '_fields', array()),1).'</pre>'; exit;
328
329 //_debug($form, 'form_vals');
330 }
331 //print '<pre>'.print_r(variable_get('crmapi_contribute_fields', ''),1).'</pre>';
332 drupal_set_message(t('The configuration options have been saved.'));
333 }
334
335
336 /**
337 * Thank you page presented to user after completing a form
338 **/
339 function crmapi_thanks($section) {
340 $sections = _crmapi_get_sections();
341 return filter_xss_admin(variable_get('crmapi_'.$section.'_thankyou', $sections[$section]['message']));
342 }
343
344
345 /**
346 * implementation of hook_user();
347 **/
348 function crmapi_user($op, &$edit, &$account, $category = NULL){
349 switch ($op) {
350 case "load":
351 return crmapi_invoke('user', 'crmapi_user', $op, $edit, $account, $category);
352 case 'categories':
353 $data[] = array('name' => 'crmapi-contact', 'title' => t('Contact Information'), 'weight' => 3);
354 return $data;
355 case "view":
356 if (user_access('view user contact information')) {
357 return crmapi_user_view($account);
358 }
359 case "form":
360 if($category =='crmapi-contact') {
361 $form = crmapi_invoke('user', 'crmapi_user', $op, $edit, $account, $category);
362 $form['crmapi_mark'] = array('#type' => 'hidden', '#value' => true,);
363 return $form;
364 }
365 break;
366 case "register":
367 $form = crmapi_invoke('user', 'crmapi_user', $op, $edit, $account, $category);
368 $form['crmapi_mark'] = array('#type' => 'hidden', '#value' => true,);
369 return $form;
370 case "insert":
371 $contact = crmapi_invoke('user', 'crmapi_user', $op, $edit, $account, $category);
372 /* Retain data in case of deleted or deduped contacts
373 foreach (variable_get('crmapi_user_fields', array()) as $key => $label) {
374 $edit[$key] = NULL;
375 } */
376 $edit['crmapi_contact'] = $contact;
377 return array('crmapi_contact' => $contact);
378 case "validate":
379 if (!empty($edit['crmapi_mark'])) {
380 crmapi_invoke('user', 'crmapi_user', $op, $edit, $account, $category);
381 //drupal_set_message('here: '.$contact);
382 //$edit['crmapi_contact'] = $contact;
383 //return array('crmapi_contact' => $contact);
384 }
385 break;
386 case "update":
387 if (!empty($edit['crmapi_mark'])) {
388 $contact = crmapi_invoke('user', 'crmapi_user', $op, $edit, $account, $category);
389 /* Retain data in case of deleted or deduped contacts
390 foreach (variable_get('crmapi_user_fields', array()) as $key => $label) {
391 $edit[$key] = NULL;
392 } */
393 //drupal_set_message('there: '.$contact);
394 $edit['crmapi_contact'] = $contact;
395 return array('crmapi_contact' => $contact);
396 }
397 break;
398 }
399 }
400
401 function crmapi_user_view($user) {
402 if (!empty($user->crmapi_contact)) {
403 $visible_fields = variable_get('crmapi_user_fields', array());
404 $crm = variable_get('crmapi_crmapi_user', '');
405 $items = array();
406
407 $data = crmapi_contact_load($crm, $user->crmapi_contact);
408 $fields = array();
409
410 if (is_array($data)) {
411 foreach($data as $field_name => $field) {
412 if (in_array($field_name, $visible_fields)) {
413 $fields[$field_name] = "<div class='contact_field'>".$field."</div>";
414 }
415 }
416
417 $items[t('Contact Information')]['crmapi'] = array(
418 'value' => theme("crmapi_user_view", $fields),
419 'class' => 'crmapi_contact',
420 );
421 }
422
423 return $items;
424 }
425 }
426
427
428 /**
429 * Theme function for user/x view pages
430 */
431 function theme_crmapi_user_view($fields) {
432 $output = theme("item_list", $fields);
433 return $output;
434 }
435
436 /**
437 * master loader for contacts
438 *
439 * Pass it the desired CRM, and the CRM's ID of the contact you want.
440 *
441 **/
442 function crmapi_contact_load($crm = '', $crm_contact_id){
443 if ($crm == '') {
444 $crm = variable_get('crmapi_crmapi', '');
445 }
446 return module_invoke($crm, "crmapi_contact_load", $crm_contact_id);
447 }
448
449 function crmapi_contact_search($crm = '', $contact){
450 if ($crm == '') {
451 $crm = variable_get('crmapi_crmapi', '');
452 }
453 return module_invoke($crm, "crmapi_contact_search", $contact);
454 }
455
456 /**
457 * Hook to save contacts
458 *
459 * @param array $contact
460 * Array containing contact data.
461 **/
462 function crmapi_contact_save($contact){
463 $crm = variable_get('crmapi_crmapi', '');
464 return module_invoke($crm, "crmapi_contact_save", $contact);
465 }
466
467 /**
468 * Hook to save activities
469 *
470 * @param array $contact
471 * Array containing contact data.
472 **/
473 function crmapi_activity_save($activity){
474 $crm = variable_get('crmapi_crmapi', '');
475 return module_invoke($crm, "crmapi_activity_save", $activity);
476 }
477
478
479 /**
480 * Wrapper for firing of CRM API calls.
481 *
482 * @param string $api
483 * Api hook like contact.
484 * @param string $section
485 * Section used for determining CRM to use
486 */
487 function crmapi_invoke($api, $section) {
488 //dsm($api);
489 //dsm($section);
490 static $crm_mod;
491 if(!isset($crm_mods)) {
492 $meta = _crmapi_get_enabled_crms();
493 foreach ($meta as $module => $info) {
494 $crms[$module] = $info['name'];
495 }
496 $crm_mods = array_keys($crms);
497 }
498
499 // Get arguements for passing.
500 $args = func_get_args();
501 array_shift($args);
502 array_shift($args);
503
504 // Get CRM.
505 $crm = variable_get("crmapi_$section", $crm_mods[0]);
506
507 $args = array_merge(array($crm, "crmapi_$api"), $args);
508
509 //dsm($args);
510
511 // Fire of action.
512 return call_user_func_array("module_invoke", $args);
513 }
514
515 /*************************
516 * CRM API HOOKS
517 *************************/
518
519 /**
520 * Hook wrapper for getting a contact by UID
521 * Thanks, ECiviCRM!
522 **/
523 function crmapi_get_user_contact($uid){
524 $contact = module_invoke(CRMAPI_USER_CRM, "crmapi_get_user_contact", $uid);
525 return $contact;
526 }
527
528
529
530 /**
531 * Master contact information form.
532 * To be used as a basis for all other forms... email signup, volunteer, etc.
533 *
534 * TODO:
535 * Create a master $form array for all CRMs.
536 * Create hook to allow the crmapi_modules to define the data for those fields
537 **/
538 function crmapi_contact_form(&$form_state, $contact_id = NULL, $section = NULL, $options = NULL){
539 global $user;
540 $form_state['section'] = $section;
541 $form = crmapi_invoke('contact_form', 'crmapi_'.$section, $form_state, $section, $contact_id, $options);
542 $form['crmapi_indicator'] =
543 array('#type' => 'hidden',
544 '#value' => 'crmapi'
545 );
546 if ($section != 'user') {
547 $form_state['redirect'] = 'crmapi/' . $section . '/thanks';
548 }
549
550 return $form;
551 }
552
553
554 /**
555 * Contact Form Validate Wrapper
556 **/
557 function crmapi_contact_form_validate($form, &$form_state){
558 crmapi_invoke('contact_form_validate', 'crmapi_contact_form', $form, $form_state);
559 }
560
561 /**
562 * Contact Form Submit Wrapper
563 **/
564 function crmapi_contact_form_submit($form, &$form_state){
565 crmapi_invoke('contact_form_submit', 'crmapi_contact_form', $form, $form_state);
566 if ($form_state['values']['section'] != 'user') {
567 $form_state['redirect'] = 'crmapi/' . $form_state['values']['section'] . '/thanks';
568 }
569 }
570
571 /**
572 * Contact Form Theme Wrapper
573 **/
574 function theme_crmapi_contact_form($form){
575 return crmapi_invoke('contact_form_theme', $form['section']['#value'], $form);
576 }
577
578
579 /**
580 * This is an accessor for retrieving available crm solutions.
581 *
582 * @return array
583 * An array of available crm solutions. Keyed by module and value as name?
584 */
585 function _crmapi_get_enabled_crms() {
586 return module_invoke_all('crmapi', 'meta');
587
588 $crms = variable_get('crmapi_enabled_crms', module_invoke_all('crmapi', 'name'));
589 $crms = array_filter($crms);
590 return $crms;
591 }
592
593 function theme_crmapi_settings_section() {
594 return 'here';
595 }
596
597 function _crmapi_get_sections() {
598 $sections = array_merge(
599 array(
600 /*'default' => array(
601 'name' => 'default',
602 'module' => 'crmapi',
603 'type' => 'default',
604 'description' => t('This is the basic default form'),
605 ),*/
606 'user' => array(
607 'name' => 'User Profile',
608 'module' => 'crmapi_user',
609 'type' => 'user',
610 'description' => t('This form will capture the contact information of drupal user signups.'),
611 'message' => '',
612 )
613 ),
614
615 module_invoke_all('crmapi_sections')
616 );
617
618 return $sections;
619 }
620
621
622 function crmapi_contact_fields($section){
623 return crmapi_invoke('contact_fields', 'contact', $section);
624 }
625
626
627
628 /**
629 * Helper function to clean up fields before passed to Forms API
630 * Right now, just removes 'settings' attribute
631 **/
632
633 function crmapi_clean_fields($fields) {
634
635 $fields_final = array();
636
637 foreach($fields as $key => $field) {
638
639 $fields_final[$key] = $field;
640 unset($fields_final[$key]['settings']);
641
642 }
643
644 return $fields_final;
645
646 }

  ViewVC Help
Powered by ViewVC 1.1.2