[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/sites/all/modules/webform/ -> webform.api.php (source)

   1  <?php
   2  
   3  /**
   4   * @file
   5   * Sample hooks demonstrating usage in Webform.
   6   */
   7  
   8  /**
   9   * @defgroup webform_hooks Webform Module Hooks
  10   * @{
  11   * Webform's hooks enable other modules to intercept events within Webform, such
  12   * as the completion of a submission or adding validation. Webform's hooks also
  13   * allow other modules to provide additional components for use within forms.
  14   */
  15  
  16  /**
  17   * Define callbacks that can be used as select list options.
  18   *
  19   * When users create a select component, they may select a pre-built list of
  20   * certain options. Webform core provides a few of these lists such as the
  21   * United States, countries of the world, and days of the week. This hook
  22   * provides additional lists that may be utilized.
  23   *
  24   * @see webform_options_example()
  25   * @see hook_webform_select_options_info_alter()
  26   *
  27   * @return
  28   *   An array of callbacks that can be used for select list options. This array
  29   *   should be keyed by the "name" of the pre-defined list. The values should
  30   *   be an array with the following additional keys:
  31   *     - title: The translated title for this list.
  32   *     - options callback: The name of the function that will return the list.
  33   *     - options arguments: Any additional arguments to send to the callback.
  34   *     - file: Optional. The file containing the options callback, relative to
  35   *       the module root.
  36   */
  37  function hook_webform_select_options_info() {
  38    $items = array();
  39  
  40    $items['days'] = array(
  41      'title' => t('Days of the week'),
  42      'options callback' => 'webform_options_days',
  43      'file' => 'includes/webform.options.inc',
  44    );
  45  
  46    return $items;
  47  }
  48  
  49  /**
  50   * Alter the list of select list options provided by Webform and other modules.
  51   *
  52   * @see hook_webform_select_options_info().
  53   */
  54  function hook_webform_select_options_info_alter(&$items) {
  55    // Remove the days of the week options.
  56    unset($items['days']);
  57  }
  58  
  59  /**
  60   * This is an example function to demonstrate a webform options callback.
  61   *
  62   * This function returns a list of options that Webform may use in a select
  63   * component. In order to be called, the function name
  64   * ("webform_options_example" in this case), needs to be specified as a callback
  65   * in hook_webform_select_options_info().
  66   *
  67   * @param $component
  68   *   The Webform component array for the select component being displayed.
  69   * @param $flat
  70   *   Boolean value indicating whether the returned list needs to be a flat array
  71   *   of key => value pairs. Select components support up to one level of
  72   *   nesting, but when results are displayed, the list needs to be returned
  73   *   without the nesting.
  74   * @param $filter
  75   *   Boolean value indicating whether the included options should be passed
  76   *   through the _webform_filter_values() function for token replacement (only)
  77   *   needed if your list contains tokens).
  78   * @param $arguments
  79   *   The "options arguments" specified in hook_webform_select_options_info().
  80   * @return
  81   *   An array of key => value pairs suitable for a select list's #options
  82   *   FormAPI property.
  83   */
  84  function webform_options_example($component, $flat, $filter, $arguments) {
  85    $options = array(
  86      'one' => t('Pre-built option one'),
  87      'two' => t('Pre-built option two'),
  88      'three' => t('Pre-built option three'),
  89    );
  90  
  91    return $options;
  92  }
  93  
  94  /**
  95   * Respond to the loading of Webform submissions.
  96   *
  97   * @param $submissions
  98   *   An array of Webform submissions that are being loaded, keyed by the
  99   *   submission ID. Modifications to the submissions are done by reference.
 100   */
 101  function hook_webform_submission_load(&$submissions) {
 102    foreach ($submissions as $sid => $submission) {
 103      $submissions[$sid]->new_property = 'foo';
 104    }
 105  }
 106  
 107  /**
 108   * Modify a Webform submission, prior to saving it in the database.
 109   *
 110   * @param $node
 111   *   The Webform node on which this submission was made.
 112   * @param $submission
 113   *   The Webform submission that is about to be saved to the database.
 114   */
 115  function hook_webform_submission_presave($node, &$submission) {
 116    // Update some component's value before it is saved.
 117    $component_id = 4;
 118    $submission->data[$component_id]['value'][0] = 'foo';
 119  }
 120  
 121  /**
 122   * Respond to a Webform submission being inserted.
 123   *
 124   * Note that this hook is called after a submission has already been saved to
 125   * the database. If needing to modify the submission prior to insertion, use
 126   * hook_webform_submission_presave().
 127   *
 128   * @param $node
 129   *   The Webform node on which this submission was made.
 130   * @param $submission
 131   *   The Webform submission that was just inserted into the database.
 132   */
 133  function hook_webform_submission_insert($node, $submission) {
 134    // Insert a record into a 3rd-party module table when a submission is added.
 135    db_query("INSERT INTO {mymodule_table} nid = %d, sid = %d, foo = '%s'", $node->nid, $submission->sid, 'foo_data');
 136  }
 137  
 138  /**
 139   * Respond to a Webform submission being updated.
 140   *
 141   * Note that this hook is called after a submission has already been saved to
 142   * the database. If needing to modify the submission prior to updating, use
 143   * hook_webform_submission_presave().
 144   *
 145   * @param $node
 146   *   The Webform node on which this submission was made.
 147   * @param $submission
 148   *   The Webform submission that was just updated in the database.
 149   */
 150  function hook_webform_submission_update($node, $submission) {
 151    // Update a record in a 3rd-party module table when a submission is updated.
 152    db_query("UPDATE {mymodule_table} SET (foo) VALUES ('%s') WHERE nid = %d, sid = %d", 'foo_data', $node->nid, $submission->sid);
 153  }
 154  
 155  /**
 156   * Respond to a Webform submission being deleted.
 157   *
 158   * @param $node
 159   *   The Webform node on which this submission was made.
 160   * @param $submission
 161   *   The Webform submission that was just deleted from the database.
 162   */
 163  function hook_webform_submission_delete($node, $submission) {
 164    // Delete a record from a 3rd-party module table when a submission is deleted.
 165    db_query("DELETE FROM {mymodule_table} WHERE nid = %d, sid = %d", $node->nid, $submission->sid);
 166  }
 167  
 168  /**
 169   * Provide a list of actions that can be executed on a submission.
 170   *
 171   * Some actions are displayed in the list of submissions such as edit, view, and
 172   * delete. All other actions are displayed only when viewing the submission.
 173   * These additional actions may be specified in this hook. Examples included
 174   * directly in the Webform module include PDF, print, and resend e-mails. Other
 175   * modules may extend this list by using this hook.
 176   *
 177   * @param $node
 178   *   The Webform node on which this submission was made.
 179   * @param $submission
 180   *   The Webform submission on which the actions may be performed.
 181   */
 182  function hook_webform_submission_actions($node, $submission) {
 183    if (webform_results_access($node)) {
 184      $actions['myaction'] = array(
 185        'title' => t('Do my action'),
 186        'href' => 'node/' . $node->nid . '/submission/' . $submission->sid . '/myaction',
 187        'query' => drupal_get_destination(),
 188      );
 189    }
 190  
 191    return $actions;
 192  }
 193  
 194  /**
 195   * Alter the display of a Webform submission.
 196   *
 197   * This function applies to both e-mails sent by Webform and normal display of
 198   * submissions when viewing through the adminsitrative interface.
 199   *
 200   * @param $renderable
 201   *   The Webform submission in a renderable array, similar to FormAPI's
 202   *   structure. This variable must be passed in by-reference. Important
 203   *   properties of this array include #node, #submission, #email, and #format,
 204   *   which can be used to find the context of the submission that is being
 205   *   rendered.
 206   */
 207  function hook_webform_submission_render_alter(&$renderable) {
 208    // Remove page breaks from sent e-mails.
 209    if (isset($renderable['#email'])) {
 210      foreach (element_children($renderable) as $key) {
 211        if ($renderable[$key]['#component']['type'] == 'pagebreak') {
 212          unset($renderable[$key]);
 213        }
 214      }
 215    }
 216  }
 217  
 218  /**
 219   * Modify a loaded Webform component.
 220   *
 221   * IMPORTANT: This hook does not actually exist because components are loaded
 222   * in bulk as part of webform_node_load(). Use hook_nodeapi() to modify loaded
 223   * components when the node is loaded. This example is provided merely to point
 224   * to hook_nodeapi().
 225   *
 226   * @see hook_nodeapi()
 227   * @see webform_node_load()
 228   */
 229  function hook_webform_component_load() {
 230    // This hook does not exist. Instead use hook_nodeapi().
 231  }
 232  
 233  /**
 234   * Modify a Webform component before it is saved to the database.
 235   *
 236   * Note that most of the time this hook is not necessary, because Webform will
 237   * automatically add data to the component based on the component form. Using
 238   * hook_form_alter() will be sufficient in most cases.
 239   *
 240   * @see hook_form_alter()
 241   * @see webform_component_edit_form()
 242   *
 243   * @param $component
 244   *   The Webform component being saved.
 245   */
 246  function hook_webform_component_presave(&$component) {
 247    $component['extra']['new_option'] = 'foo';
 248  }
 249  
 250  /**
 251   * Respond to a Webform component being inserted into the database.
 252   */
 253  function hook_webform_component_insert($component) {
 254    // Insert a record into a 3rd-party module table when a component is inserted.
 255    db_query("INSERT INTO {mymodule_table} (nid, cid) VALUES (%d, %d)", $component['nid'], $component['cid']);
 256  }
 257  
 258  /**
 259   * Respond to a Webform component being updated in the database.
 260   */
 261  function hook_webform_component_update($component) {
 262    // Update a record in a 3rd-party module table when a component is updated.
 263    db_query('UPDATE {mymodule_table} SET value "%s" WHERE nid = %d AND cid = %d)', 'foo', $component['nid'], $component['cid']);
 264  }
 265  
 266  /**
 267   * Respond to a Webform component being deleted.
 268   */
 269  function hook_webform_component_delete($component) {
 270    // Delete a record in a 3rd-party module table when a component is deleted.
 271    db_query('DELETE FROM {mymodule_table} WHERE nid = %d AND cid = %d)', $component['nid'], $component['cid']);
 272  }
 273  
 274  /**
 275   * Define components to Webform.
 276   *
 277   * @return
 278   *   An array of components, keyed by machine name. Required properties are
 279   *   "label" and "description". The "features" array defines which capabilities
 280   *   the component has, such as being displayed in e-mails or csv downloads.
 281   *   A component like "markup" for example would not show in these locations.
 282   *   The possible features of a component include:
 283   *
 284   *     - csv
 285   *     - email
 286   *     - email_address
 287   *     - email_name
 288   *     - required
 289   *     - conditional
 290   *     - spam_analysis
 291   *     - group
 292   *
 293   *   Note that most of these features do not indicate the default state, but 
 294   *   determine if the component can have this property at all. Setting
 295   *   "required" to TRUE does not mean that a component's fields will always be 
 296   *   required, but instead give the option to the administrator to choose the
 297   *   requiredness. See the example implementation for details on how these
 298   *   features may be set.
 299   *
 300   *   An optional "file" may be specified to be loaded when the component is
 301   *   needed. A set of callbacks will be established based on the name of the
 302   *   component. All components follow the pattern:
 303   *
 304   *   _webform_[callback]_[component]
 305   *
 306   *   Where [component] is the name of the key of the component and [callback] is
 307   *   any of the following:
 308   *
 309   *     - defaults
 310   *     - edit
 311   *     - render
 312   *     - display
 313   *     - submit
 314   *     - delete
 315   *     - help
 316   *     - theme
 317   *     - analysis
 318   *     - table
 319   *     - csv_headers
 320   *     - csv_data
 321   *
 322   * See the sample component implementation for details on each one of these
 323   * callbacks.
 324   *
 325   * @see webform_components()
 326   */
 327  function hook_webform_component_info() {
 328    $components = array();
 329  
 330    $components['textfield'] = array(
 331      'label' => t('Textfield'),
 332      'description' => t('Basic textfield type.'),
 333      'features' => array(
 334        // Add content to CSV downloads. Defaults to TRUE.
 335        'csv' => TRUE,
 336  
 337        // This component supports default values. Defaults to TRUE.
 338        'default_value' => FALSE,
 339  
 340        // This component supports a description field. Defaults to TRUE.
 341        'description' => FALSE,
 342  
 343        // Show this component in e-mailed submissions. Defaults to TRUE.
 344        'email' => TRUE,
 345  
 346        // Allow this component to be used as an e-mail FROM or TO address.
 347        // Defaults to FALSE.
 348        'email_address' => FALSE,
 349  
 350        // Allow this component to be used as an e-mail SUBJECT or FROM name.
 351        // Defaults to FALSE.
 352        'email_name' => TRUE,
 353  
 354        // This component may be toggled as required or not. Defaults to TRUE.
 355        'required' => TRUE,
 356  
 357        // This component supports a title attribute. Defaults to TRUE.
 358        'title' => FALSE,
 359  
 360        // This component has a title that can be toggled as displayed or not.
 361        'title_display' => TRUE,
 362  
 363        // This component has a title that can be displayed inline.
 364        'title_inline' => TRUE,
 365  
 366        // If this component can be used as a conditional SOURCE. All components
 367        // may always be displayed conditionally, regardless of this setting.
 368        // Defaults to TRUE.
 369        'conditional' => TRUE,
 370  
 371        // If this component allows other components to be grouped within it 
 372        // (like a fieldset or tabs). Defaults to FALSE.
 373        'group' => FALSE,
 374  
 375        // If this component can be used for SPAM analysis, usually with Mollom.
 376        'spam_analysis' => FALSE,
 377  
 378        // If this component saves a file that can be used as an e-mail
 379        // attachment. Defaults to FALSE.
 380        'attachment' => FALSE,
 381      ),
 382      'file' => 'components/textfield.inc',
 383    );
 384  
 385    return $components;
 386  }
 387  
 388  /**
 389   * Alter the list of available Webform components.
 390   *
 391   * @param $components
 392   *   A list of existing components as defined by hook_webform_component_info().
 393   *
 394   * @see hook_webform_component_info()
 395   */
 396  function hook_webform_component_info_alter(&$components) {
 397    // Completely remove a component.
 398    unset($components['grid']);
 399  
 400    // Change the name of a component.
 401    $components['textarea']['label'] = t('Text box');
 402  }
 403  
 404  /**
 405   * Return an array of files associated with the component.
 406   *
 407   * The output of this function will be used to attach files to e-mail messages.
 408   *
 409   * @param $component
 410   *   A Webform component array.
 411   * @param $value
 412   *   An array of information containing the submission result, directly
 413   *   correlating to the webform_submitted_data database schema.
 414   * @return
 415   *   An array of files, each file is an array with following keys:
 416   *     - filepath: The relative path to the file.
 417   *     - filename: The name of the file including the extension.
 418   *     - filemime: The mimetype of the file.
 419   *   This will result in an array looking something like this:
 420   *   @code
 421   *   array[0] => array(
 422   *     'filepath' => '/sites/default/files/attachment.txt',
 423   *     'filename' => 'attachment.txt',
 424   *     'filemime' => 'text/plain',
 425   *   );
 426   *   @endcode
 427   */
 428  function _webform_attachments_component($component, $value) {
 429    $files = array();
 430    $files[] = db_fetch_array(db_query("SELECT * FROM {files} WHERE fid = %d", $value[0]));
 431    return $files;
 432  }
 433  
 434  
 435  /**
 436   * @}
 437   */
 438  
 439  /**
 440   * @defgroup webform_component Sample Webform Component
 441   * @{
 442   * In each of these examples, the word "component" should be replaced with the,
 443   * name of the component type (such as textfield or select). These are not
 444   * actual hooks, but instead samples of how Webform integrates with its own
 445   * built-in components.
 446   */
 447  
 448  /**
 449   * Specify the default properties of a component.
 450   *
 451   * @return
 452   *   An array defining the default structure of a component.
 453   */
 454  function _webform_defaults_component() {
 455    return array(
 456      'name' => '',
 457      'form_key' => NULL,
 458      'mandatory' => 0,
 459      'pid' => 0,
 460      'weight' => 0,
 461      'extra' => array(
 462        'options' => '',
 463        'questions' => '',
 464        'optrand' => 0,
 465        'qrand' => 0,
 466        'description' => '',
 467      ),
 468    );
 469  }
 470  
 471  /**
 472   * Generate the form for editing a component.
 473   *
 474   * Create a set of form elements to be displayed on the form for editing this
 475   * component. Use care naming the form items, as this correlates directly to the
 476   * database schema. The component "Name" and "Description" fields are added to
 477   * every component type and are not necessary to specify here (although they
 478   * may be overridden if desired).
 479   *
 480   * @param $component
 481   *   A Webform component array.
 482   * @return
 483   *   An array of form items to be displayed on the edit component page
 484   */
 485  function _webform_edit_component($component) {
 486    $form = array();
 487  
 488    // Disabling the description if not wanted.
 489    $form['description'] = array();
 490  
 491    // Most options are stored in the "extra" array, which stores any settings
 492    // unique to a particular component type.
 493    $form['extra']['options'] = array(
 494      '#type' => 'textarea',
 495      '#title' => t('Options'),
 496      '#default_value' => $component['extra']['options'],
 497      '#description' => t('Key-value pairs may be entered separated by pipes. i.e. safe_key|Some readable option') . theme('webform_token_help'),
 498      '#cols' => 60,
 499      '#rows' => 5,
 500      '#weight' => -3,
 501      '#required' => TRUE,
 502    );
 503    return $form;
 504  }
 505  
 506  /**
 507   * Render a Webform component to be part of a form.
 508   *
 509   * @param $component
 510   *   A Webform component array.
 511   * @param $value
 512   *   If editing an existing submission or resuming a draft, this will contain
 513   *   an array of values to be shown instead of the default in the component
 514   *   configuration. This value will always be an array, keyed numerically for
 515   *   each value saved in this field.
 516   * @param $filter
 517   *   Whether or not to filter the contents of descriptions and values when
 518   *   rendering the component. Values need to be unfiltered to be editable by
 519   *   Form Builder.
 520   *
 521   * @see _webform_client_form_add_component()
 522   */
 523  function _webform_render_component($component, $value = NULL) {
 524    $form_item = array(
 525      '#type' => 'textfield',
 526      '#title' => $filter ? _webform_filter_xss($component['name']) : $component['name'],
 527      '#required' => $component['mandatory'],
 528      '#weight' => $component['weight'],
 529      '#description'   => $filter ? _webform_filter_descriptions($component['extra']['description']) : $component['extra']['description'],
 530      '#default_value' => $filter ? _webform_filter_values($component['value']) : $component['value'],
 531      '#prefix' => '<div class="webform-component-' . $component['type'] . '" id="webform-component-' . $component['form_key'] . '">',
 532      '#suffix' => '</div>',
 533    );
 534  
 535    if (isset($value)) {
 536      $form_item['#default_value'] = $value[0];
 537    }
 538  
 539    return $form_item;
 540  }
 541  
 542  /**
 543   * Display the result of a submission for a component.
 544   *
 545   * The output of this function will be displayed under the "Results" tab then
 546   * "Submissions". This should output the saved data in some reasonable manner.
 547   *
 548   * @param $component
 549   *   A Webform component array.
 550   * @param $value
 551   *   An array of information containing the submission result, directly
 552   *   correlating to the webform_submitted_data database table schema.
 553   * @param $format
 554   *   Either 'html' or 'text'. Defines the format that the content should be
 555   *   returned as. Make sure that returned content is run through check_plain()
 556   *   or other filtering functions when returning HTML.
 557   * @return
 558   *   A renderable element containing at the very least these properties:
 559   *    - #title
 560   *    - #weight
 561   *    - #component
 562   *    - #format
 563   *    - #value
 564   *   Webform also uses #theme_wrappers to output the end result to the user,
 565   *   which will properly format the label and content for use within an e-mail
 566   *   (such as wrapping the text) or as HTML (ensuring consistent output).
 567   */
 568  function _webform_display_component($component, $value, $format = 'html') {
 569    return array(
 570      '#title' => $component['name'],
 571      '#weight' => $component['weight'],
 572      '#theme' => 'webform_display_textfield',
 573      '#theme_wrappers' => $format == 'html' ? array('webform_element') : array('webform_element_text'),
 574      '#post_render' => array('webform_element_wrapper'),
 575      '#field_prefix' => $component['extra']['field_prefix'],
 576      '#field_suffix' => $component['extra']['field_suffix'],
 577      '#component' => $component,
 578      '#format' => $format,
 579      '#value' => isset($value[0]) ? $value[0] : '',
 580    );
 581  }
 582  
 583  /**
 584   * A hook for changing the input values before saving to the database.
 585   *
 586   * Webform expects a component to consist of a single field, or a single array 
 587   * of fields. If you have a component that requires a deeper form tree
 588   * you must flatten the data into a single array using this callback 
 589   * or by setting #parents on each field to avoid data loss and/or unexpected
 590   * behavior. 
 591   *
 592   * Note that Webform will save the result of this function directly into the
 593   * database.
 594   *
 595   * @param $component
 596   *   A Webform component array.
 597   * @param $value
 598   *   The POST data associated with the user input.
 599   * @return
 600   *   An array of values to be saved into the database. Note that this should be
 601   *   a numerically keyed array.
 602   */
 603  function _webform_submit_component($component, $value) {
 604    // Clean up a phone number into 123-456-7890 format.
 605    if ($component['extra']['phone_number']) {
 606      $matches = array();
 607      $number = preg_replace('[^0-9]', $value[0]);
 608      if (strlen($number) == 7) {
 609        $number = substr($number, 0, 3) . '-' . substr($number, 3, 4);
 610      }
 611      else {
 612        $number = substr($number, 0, 3) . '-' . substr($number, 3, 3) . '-' . substr($number, 6, 4);
 613      }
 614    }
 615  
 616    $value[0] = $number;
 617    return $value;
 618  }
 619  
 620  /**
 621   * Delete operation for a component or submission.
 622   *
 623   * @param $component
 624   *   A Webform component array.
 625   * @param $value
 626   *   An array of information containing the submission result, directly
 627   *   correlating to the webform_submitted_data database schema.
 628   */
 629  function _webform_delete_component($component, $value) {
 630    // Delete corresponding files when a submission is deleted.
 631    $filedata = unserialize($value['0']);
 632    if (isset($filedata['filepath']) && is_file($filedata['filepath'])) {
 633      unlink($filedata['filepath']);
 634      db_query("DELETE FROM {files} WHERE filepath = '%s'", $filedata['filepath']);
 635    }
 636  }
 637  
 638  /**
 639   * Module specific instance of hook_help().
 640   *
 641   * This allows each Webform component to add information into hook_help().
 642   */
 643  function _webform_help_component($section) {
 644    switch ($section) {
 645      case 'admin/settings/webform#grid_description':
 646        return t('Allows creation of grid questions, denoted by radio buttons.');
 647    }
 648  }
 649  
 650  /**
 651   * Module specific instance of hook_theme().
 652   *
 653   * This allows each Webform component to add information into hook_theme().
 654   */
 655  function _webform_theme_component() {
 656    return array(
 657      'webform_grid' => array(
 658        'arguments' => array('grid_element' => NULL),
 659        'file' => 'components/grid.inc',
 660      ),
 661      'webform_display_grid' => array(
 662        'arguments' => array('element' => NULL),
 663        'file' => 'components/grid.inc',
 664      ),
 665    );
 666  }
 667  
 668  /**
 669   * Calculate and returns statistics about results for this component.
 670   *
 671   * This takes into account all submissions to this webform. The output of this
 672   * function will be displayed under the "Results" tab then "Analysis".
 673   *
 674   * @param $component
 675   *   An array of information describing the component, directly correlating to
 676   *   the webform_component database schema.
 677   * @param $sids
 678   *   An optional array of submission IDs (sid). If supplied, the analysis will
 679   *   be limited to these sids.
 680   * @param $single
 681   *   Boolean flag determining if the details about a single component are being
 682   *   shown. May be used to provided detailed information about a single
 683   *   component's analysis, such as showing "Other" options within a select list.
 684   * @return
 685   *   An array of data rows, each containing a statistic for this component's
 686   *   submissions.
 687   */
 688  function _webform_analysis_component($component, $sids = array(), $single = FALSE) {
 689    // Generate the list of options and questions.
 690    $options = _webform_component_options($component['extra']['options']);
 691    $questions = array_values(_webform_component_options($component['extra']['questions']));
 692  
 693    // Generate a lookup table of results.
 694    $sidfilter = count($sids) ? " AND sid in (" . db_placeholders($sids, 'int') . ")" : "";
 695    $query = 'SELECT no, data, count(data) as datacount '.
 696      ' FROM {webform_submitted_data} '.
 697      ' WHERE nid = %d '.
 698      ' AND cid = %d '.
 699      " AND data != '' ". $sidfilter .
 700      ' GROUP BY no, data';
 701    $result = db_query($query, array_merge(array($component['nid'], $component['cid']), $sids));
 702    $counts = array();
 703    while ($data = db_fetch_object($result)) {
 704      $counts[$data->no][$data->data] = $data->datacount;
 705    }
 706  
 707    // Create an entire table to be put into the returned row.
 708    $rows = array();
 709    $header = array('');
 710  
 711    // Add options as a header row.
 712    foreach ($options as $option) {
 713      $header[] = $option;
 714    }
 715  
 716    // Add questions as each row.
 717    foreach ($questions as $qkey => $question) {
 718      $row = array($question);
 719      foreach ($options as $okey => $option) {
 720        $row[] = !empty($counts[$qkey][$okey]) ? $counts[$qkey][$okey] : 0;
 721      }
 722      $rows[] = $row;
 723    }
 724    $output = theme('table', $header, $rows, array('class' => 'webform-grid'));
 725  
 726    return array(array(array('data' => $output, 'colspan' => 2)));
 727  }
 728  
 729  /**
 730   * Return the result of a component value for display in a table.
 731   *
 732   * The output of this function will be displayed under the "Results" tab then
 733   * "Table".
 734   *
 735   * @param $component
 736   *   A Webform component array.
 737   * @param $value
 738   *   An array of information containing the submission result, directly
 739   *   correlating to the webform_submitted_data database schema.
 740   * @return
 741   *   Textual output formatted for human reading.
 742   */
 743  function _webform_table_component($component, $value) {
 744    $questions = array_values(_webform_component_options($component['extra']['questions']));
 745    $output = '';
 746    // Set the value as a single string.
 747    if (is_array($value)) {
 748      foreach ($value as $item => $value) {
 749        if ($value !== '') {
 750          $output .= $questions[$item] . ': ' . check_plain($value) . '<br />';
 751        }
 752      }
 753    }
 754    else {
 755      $output = check_plain(!isset($value['0']) ? '' : $value['0']);
 756    }
 757    return $output;
 758  }
 759  
 760  /**
 761   * Return the header for this component to be displayed in a CSV file.
 762   *
 763   * The output of this function will be displayed under the "Results" tab then
 764   * "Download".
 765   *
 766   * @param $component
 767   *   A Webform component array.
 768   * @param $export_options
 769   *   An array of options that may configure export of this field.
 770   * @return
 771   *   An array of data to be displayed in the first three rows of a CSV file, not
 772   *   including either prefixed or trailing commas.
 773   */
 774  function _webform_csv_headers_component($component, $export_options) {
 775    $header = array();
 776    $header[0] = array('');
 777    $header[1] = array($component['name']);
 778    $items = _webform_component_options($component['extra']['questions']);
 779    $count = 0;
 780    foreach ($items as $key => $item) {
 781      // Empty column per sub-field in main header.
 782      if ($count != 0) {
 783        $header[0][] = '';
 784        $header[1][] = '';
 785      }
 786      // The value for this option.
 787      $header[2][] = $item;
 788      $count++;
 789    }
 790  
 791    return $header;
 792  }
 793  
 794  /**
 795   * Format the submitted data of a component for CSV downloading.
 796   *
 797   * The output of this function will be displayed under the "Results" tab then
 798   * "Download".
 799   *
 800   * @param $component
 801   *   A Webform component array.
 802   * @param $export_options
 803   *   An array of options that may configure export of this field.
 804   * @param $value
 805   *   An array of information containing the submission result, directly
 806   *   correlating to the webform_submitted_data database schema.
 807   * @return
 808   *   An array of items to be added to the CSV file. Each value within the array
 809   *   will be another column within the file. This function is called once for
 810   *   every row of data.
 811   */
 812  function _webform_csv_data_component($component, $export_options, $value) {
 813    $questions = array_keys(_webform_select_options($component['extra']['questions']));
 814    $return = array();
 815    foreach ($questions as $key => $question) {
 816      $return[] = isset($value[$key]) ? $value[$key] : '';
 817    }
 818    return $return;
 819  }
 820  
 821  /**
 822   * @}
 823   */


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7