Transform a Webform submission into a Ticket Node in another site using Services

Events happening in the community are now at Drupal community events on www.drupal.org.
lelizondo's picture

I needed to create a Webform to act as a Frontend to receive feedback in one site, but I needed to send this submissions to another site. This is what I did, hope you find it interesting.

First, I created two sites, one is the Frontend and another the Backend. In the backend, I have a Content Type "Ticket" with a few fields mapped to the webform fields in my frontend. I also have Subscriptions and Taxonomy Access Control.

I then created a new module in the frontend, which is going to save a node on the backend everytime a webform submission is created. This is the main function which I call from the Webform.

<?php
function mymodule_new_node(&$form_state, $webform = NULL) {
 
// This is more practical.
 
$values = $form_state['values']['submitted_tree'];

 
// Get the Form Values
 
$email = $values['email'];
 
$name = $values['name'];
 
$city = $values['city'];
 
$state = $values['state'];
 
$country = $values['country'];

 
// Map the first 80 characters of the message to the title variable.
 
$title = check_plain(truncate_utf8($values['msg'], 80, TRUE, TRUE));; 
 
 
// Map the Message to the Body variable
 
$body = $values['msg'];

 
// Create a new object
 
$node = new stdClass();
 
$node->type = 'ticket';
 
 
$user = user_load(array('mail' => $email));
 
// If the author of the node is the same one as the user, then support will notify automatically about new comments and changes.
 
$node->uid = $user->uid;
 
// Enable comments
 
$node->comment = 2;
 
$node->title = $title;
 
$node->body = $body;
     
 
// Map the cck fields
 
$node->field_ticket_email[0]['email'] = $email;
 
$node->field_ticket_name[0]['value'] = $name;
 
$node->field_ticket_city[0]['value'] = $city;
 
$node->field_ticket_state[0]['value'] = $state;
 
$node->field_ticket_country[0]['value'] = $country;
 
$node->field_ticket_status[0]['value'] = "1";

 
// Check the webform and department
 
$vid = 1; // VID is always the same
 
if($webform == "some_string") {
   
$tid = 1;
   
$term = "Some Term";
  }
  elseif(
$webform == "some_other_string") {
   
$tid = 2;
   
$term = "Some other Term";
  }
 
 
$node->taxonomy[$tid]=new stdClass();
 
$node->taxonomy[$tid]->tid=$tid;
 
$node->taxonomy[$tid]->vid=$vid;
 
 
$uri = 'http://www.example.com/services/xmlrpc';
 
$user = "some_username"; // This one is tricky. Since I have Taxonomy Access Control, I created an Admin Role that can create content with any particular Term, this way I avoid an access denied message from Services.
 
$password = "somepassword";
 
// Connect as anonymous user.
 
$xmlrpc_result = xmlrpc($uri, 'system.connect');
$annonymous = $xmlrpc_result['sessid'];
 
 
// Now using the sessid connect as an admin user.
 
$authenticated_session = xmlrpc($uri, 'user.login', $annonymous, $user, $password);
 
   
// Save the node as an admin user.
$xmlrpc_result = xmlrpc($uri, 'node.save', $authenticated_session['sessid'], $node);
  
   if (
$xmlrpc_result === FALSE) {
        return
'<pre>;' . print_r(xmlrpc_error(), TRUE) . '</pre>';
    }
  else {
// If success, then build the $event to notify any user that is subscribed to any Ticket Content Type or to the Term provided.
     
$op = 'insert';
     
$event = array(
     
'module' => 'node',
     
'uid' => $node->uid,
     
'load_function' => 'subscriptions_content_node_load',
     
'load_args' => $node->nid,
     
'type' => 'node',
     
'action' => $op,
     
'is_new' => ($op == 'insert'),
     
'node' => $node,
    );
   
$unpublished_nid = NULL;

   
$xmlrpc_result = xmlrpc($uri, 'subscriptions.queue', $authenticated_session['sessid'], $event);

       return
$xmlrpc_result;
}
}
?>

In this process, I created a new module, the Subscriptions Service, I hope you can try it and test it. http://drupal.org/project/subscriptions_service

Comments

Webform Submission.

Anjuanoo's picture

Hi ,

I have a webform where user can input some values and has the submit button which is there as deafult for the webforms.
But when i click on submit ,i want these values to go into a database that is created by me.(Not the web_submission_database).
Can you please help me in that or is it possible to do it.

It's really helpful

gorillaz.f's picture

I'm also thinking about using 2 drupal sites with services communication.
But I'm not sure whether it's performance is better then just one drupal site, suppose that it's a complicated drupal site.

Services

Group organizers

Group categories

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds:

Hot content this week