webform.api.php 27 KB

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