webform_handler_relationship_submission_data.inc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /**
  3. * Views' relationship handlers.
  4. */
  5. class webform_handler_relationship_submission_data extends views_handler_relationship {
  6. /**
  7. * {@inheritdoc}
  8. */
  9. public function option_definition() {
  10. $options = parent::option_definition();
  11. $options['webform_nid'] = array('default' => NULL);
  12. $options['webform_cid'] = array('default' => NULL);
  13. $options['webform_form_key'] = array('default' => NULL);
  14. $options['webform_join_by'] = array('default' => 'nid_cid');
  15. return $options;
  16. }
  17. /**
  18. * {@inheritdoc}
  19. */
  20. public function options_form(&$form, &$form_state) {
  21. parent::options_form($form, $form_state);
  22. form_load_include($form_state, 'inc', 'webform', 'views/webform.views');
  23. $nid = (int) $this->options['webform_nid'];
  24. $cid = (int) $this->options['webform_cid'];
  25. // Helper function provides webform_nid and webform_cid options.
  26. _webform_views_options_form($form, $form_state, $nid, $cid);
  27. $form['webform_join_by'] = array(
  28. '#type' => 'select',
  29. '#title' => t('Relate using'),
  30. '#default_value' => $this->options['webform_join_by'],
  31. '#options' => array(
  32. 'nid_cid' => t('Node and Component ID'),
  33. 'cid' => t('Component ID'),
  34. 'form_key' => t('Component Form Key'),
  35. ),
  36. '#description' => t('Choose <em>Node and Component ID</em> when this view will display data from only this webform.</br>Choose <em>Component ID</em> when this view will display data from other webforms and where the Component ID is identical.</br>Choose <em>Component Form Key</em> when this view will display data from other webforms with varying Component IDs.'),
  37. );
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function options_validate(&$form, &$form_state) {
  43. parent::options_validate($form, $form_state);
  44. _webform_views_options_validate($form, $form_state);
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function options_submit(&$form, &$form_state) {
  50. parent::options_submit($form, $form_state);
  51. _webform_views_options_submit($form, $form_state);
  52. $options =& $form_state['values']['options'];
  53. $options['webform_form_key'] = $options['webform_join_by'] == 'form_key' && ($node = node_load($options['webform_nid']))
  54. ? $node->webform['components'][$options['webform_cid']]['form_key']
  55. : NULL;
  56. // Drop PHP reference.
  57. unset($options);
  58. }
  59. /**
  60. * Called to implement a relationship in a query.
  61. *
  62. * It respects the given component ids, provided via options form.
  63. */
  64. public function query() {
  65. // When defining extra clauses, the 'table' key can be used to specify the
  66. // alias of another table. If NULL is specified, then the field is not
  67. // qualified with a table. Therefore, do NOT specify "'table' => NULL".
  68. switch ($this->options['webform_join_by']) {
  69. case 'form_key':
  70. $form_key = $this->options['webform_form_key'];
  71. $join_type = $this->options['required'] ? 'INNER' : 'LEFT';
  72. $this->ensure_my_table();
  73. $join = new views_join();
  74. $join->construct(
  75. // The table to be joined.
  76. 'webform_component',
  77. // The left table (i.e. this table, webform_submission).
  78. $this->table,
  79. // The left field (i.e. the webform node id).
  80. 'nid',
  81. // The field (i.e. webform_components.nid).
  82. 'nid',
  83. // Extra array of additional conditions.
  84. array(
  85. array(
  86. // Extra join of form_key.
  87. 'field' => 'form_key',
  88. // ... = $form_key (operator '=' is default)
  89. 'value' => $form_key,
  90. ),
  91. ),
  92. // Join type is the same as this relationship's join type.
  93. $join_type);
  94. $alias = $this->query->add_relationship(
  95. // Requested alias for new reliationship.
  96. 'webform_component_' . $form_key,
  97. // Addition join to be added to this relatinship.
  98. $join,
  99. // Base table (i.e. drivingevals_submission)
  100. $this->table_alias,
  101. // Add the view to this relationship.
  102. $this->relationship);
  103. // The actual alias for this relationship's join is not known yet. Becasue
  104. // of name conflicts, it may have a number appended to the end. %alias is
  105. // substitued when the query is build with the actual alias name.
  106. $this->definition['extra'][] = "%alias.cid = {$alias}.cid";
  107. break;
  108. case 'nid_cid':
  109. $this->definition['extra'][] = array(
  110. 'field' => "nid",
  111. 'value' => $this->options['webform_nid'],
  112. );
  113. // FALL THROUGH.
  114. case 'cid':
  115. $this->definition['extra'][] = array(
  116. 'field' => "cid",
  117. 'value' => $this->options['webform_cid'],
  118. );
  119. break;
  120. }
  121. // The rest of building the join is performed by the parent.
  122. parent::query();
  123. }
  124. }