diff --git a/views/webform.views.inc b/views/webform.views.inc old mode 100644 new mode 100755 index f8e7c6f..929f7c3 --- a/views/webform.views.inc +++ b/views/webform.views.inc @@ -13,7 +13,7 @@ function webform_views_data() { $data['webform_submissions']['table']['base'] = array( 'field' => 'sid', 'title' => t('Webform submissions'), - 'help' => t('Submissions generated from Webform forms.'), + 'help' => t('Submissions grenerated from Webform forms.'), ); // sid @@ -136,6 +136,14 @@ function webform_views_data() { 'link_type' => 'delete', ), ); + + $data['webform_submissions']['data'] = array( + 'title' => t('Submitted data'), + 'help' => t('Pull in user-submitted data from a single webform.'), + 'field' => array( + 'handler' => 'webform_handler_field_data', + ), + ); return $data; } @@ -227,6 +235,10 @@ function webform_views_handlers() { 'parent' => 'views_handler_filter_in_operator', 'file' => 'webform_handler_filter_is_draft.inc', ), + 'webform_handler_field_data' => array( + 'parent' => 'views_handler_field', + 'file' => 'webform_handler_field_data.inc', + ), ), ); } \ No newline at end of file diff --git a/views/webform_handler_field_data.inc b/views/webform_handler_field_data.inc new file mode 100755 index 0000000..71b351f --- /dev/null +++ b/views/webform_handler_field_data.inc @@ -0,0 +1,198 @@ + ''); + return $options; + } + + /** + * Default options form that provides the label widget that all fields + * should have. + */ + function options_form(&$form, &$form_state) { + parent::options_form($form, $form_state); + $form['nid'] = array( + '#type' => 'textfield', + '#title' => t('Webform NID'), + '#default_value' => $this->options['nid'], + '#description' => t('Enter the Webform NID that will be used for pulling in submitted Webform data.'), + ); + } + + /** + * Taken from http://drupal.org/node/66183.... + * + * @param type $arr1 + * @param type $key + * @param type $arr2 + * @param type $before + * @return type + */ + function array_insert($arr1, $key, $arr2, $before = FALSE) { + $index = array_search($key, array_keys($arr1)); + if($index === FALSE){ + $index = count($arr1); // insert @ end of array if $key not found + } + else { + if(!$before){ + $index++; + } + } + $end = array_splice($arr1, $index); + return array_merge($arr1, $arr2, $end); + } + + // Clone this field as another field... + function clone_field($last_handler) { + + // Clone this handler... + $handler = clone $this; + + // Insert the new handler and retain field orders... + $this->view->display_handler->handlers['field'] = $this->array_insert($this->view->display_handler->handlers['field'], $last_handler, array($this->alias => $handler)); + $this->view->field = $this->array_insert($this->view->field, $last_handler, array($this->alias => $handler)); + + // Return the new handler. + return $handler; + } + + // Get the webform object. + function get_webform() { + static $webform = null; + if (!$webform) { + + // Load the node. + $webform = node_load($this->options['nid']); + } + + // Return the webform object. + return $webform; + } + + // Query function for this field. + function pre_query() { + + // Only continue if this is the base field... + if (!isset($this->alias)) { + + $this->ensure_my_table(); + + // This is typically created with a relationship handler... however, + // we need to dynamically build relationships for each component within the + // webform. Because of this, we will need to create the relationship here. + $join = new views_join(); + $join->definition = array( + 'base_field' => 'nid', + 'base' => 'webform_submitted_data', + 'handler' => 'views_handler_relationship', + 'field' => 'sid', + 'left_field' => 'sid', + 'left_table' => 'webform_submissions', + 'relationship field' => 'nid', + 'relationship table' => 'webform_submissions', + 'skip base' => array('node', 'node_revisions'), + 'table' => 'webform_submitted_data' + ); + $join->construct(); + $join->adjusted = TRUE; + + // Get the webform... make a function so that other classes can override this + // and figure out better ways to get the webform. + $webform = $this->get_webform(); + + // Keep track of the webform fields. + $this->webform_fields = array(); + + // Iterate through all of our components... + $index = 0; + $last_handler = $this->field; + foreach ($webform->webform['components'] as $cid => $component) { + + // Ignore fieldsets. + if ($component['type'] != 'fieldset') { + + // Make sure we clone so that we get a non-reference of the join object. + $newjoin = clone $join; + + // Add a join. + $newjoin->extra = array( + array( + 'field' => 'cid', + 'operator' => '=', + 'value' => $cid, + 'numeric' => TRUE, + ), + ); + + // Create an alias for this relationship. + $alias = 'webform_submitted_data_' . $cid; + + // Add our table alias. + $relationship->alias = $this->query->add_relationship($alias, $newjoin, 'webform_submitted_data'); + + // Add the field ID. + $id = 'webform_submitted_data_node_' . $webform->nid . '_cid_' . $cid; + + // Now add the field we actually want to retreive as data. + $this->alias = $this->query->add_field($relationship->alias, 'data', $id); + + // Get the new field handler for this field. + $handler = ($index > 0) ? $this->clone_field($last_handler) : $this; + + // Change the label to match the component name. + $handler->options['label'] = $component['name']; + + // Add this to our handlers array. + $this->webform_fields[] = $this->alias; + + // Increment our index. + $last_handler = ($index == 0) ? $last_handler : $id; + } + + $index++; + } + + // Count the number of fields. + $this->num_fields = count($this->webform_fields); + + return TRUE; + } + + return FALSE; + } + + function query() {} + + /** + * This render function will sequentially render the data from each component as the fields are + * dynamically added via the post_render function. + * + * @param type $values + * @return type + */ + function render($values) { + $this->field_index = ($this->field_index < $this->num_fields) ? $this->field_index : 0; + return check_plain($values->{$this->webform_fields[$this->field_index++]}); + } +} \ No newline at end of file