Last active
July 16, 2019 16:13
-
-
Save sean-e-dietrich/4c12f98e54538e10993d6b7cc4ca136d to your computer and use it in GitHub Desktop.
Patch for Webform Query Module
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| diff --git a/src/WebformQuery.php b/src/WebformQuery.php | |
| index 8df00f7..5116d59 100644 | |
| --- a/src/WebformQuery.php | |
| +++ b/src/WebformQuery.php | |
| @@ -4,6 +4,11 @@ namespace Drupal\webform_query; | |
| use Drupal\Core\Database\Connection; | |
| +/** | |
| + * Class WebformQuery | |
| + * | |
| + * @package Drupal\webform_query | |
| + */ | |
| class WebformQuery { | |
| /** | |
| @@ -31,28 +36,30 @@ class WebformQuery { | |
| public function __construct(Connection $connection) { | |
| $this->connection = $connection; | |
| } | |
| - | |
| - /** | |
| + | |
| + /** | |
| * @param integer $webform_id | |
| */ | |
| public function setWebform($webform_id = NULL) { | |
| if (!is_null($webform_id)) { | |
| - $this->addCondition('webform_id', $webform_id); | |
| - } | |
| + $this->addCondition('webform_id', $webform_id, '=', FALSE); | |
| + } | |
| } | |
| - | |
| + | |
| /** | |
| - * | |
| + * | |
| * @param string $field | |
| * Field name. | |
| * @param mixed $value | |
| * Value to compare. | |
| - * @param type $operator | |
| + * @param string $operator | |
| * Operator. | |
| - * | |
| + * @param bool $submissionData | |
| + * Data located in webform_submission_data table. | |
| + * | |
| * @return $this | |
| */ | |
| - public function addCondition($field, $value = NULL, $operator = '=') { | |
| + public function addCondition($field, $value = NULL, $operator = '=', $submissionData = TRUE) { | |
| // Check for webform_id. | |
| if ($field === 'webform_id') { | |
| // Check for existing condition at 0. | |
| @@ -63,6 +70,7 @@ class WebformQuery { | |
| 'field' => $field, | |
| 'value' => $value, | |
| 'operator' => $operator, | |
| + 'table' => $submissionData ? 'wsd' : 'ws', | |
| ]; | |
| } | |
| else { | |
| @@ -71,7 +79,7 @@ class WebformQuery { | |
| $operator = '='; | |
| } | |
| - // Validate opertaor. | |
| + // Validate operator. | |
| $operator = $this->validateOperator($operator); | |
| // If operator is good then add the condition. | |
| @@ -80,6 +88,7 @@ class WebformQuery { | |
| 'field' => $field, | |
| 'value' => $value, | |
| 'operator' => $operator, | |
| + 'table' => $submissionData ? 'wsd' : 'ws', | |
| ]; | |
| } | |
| } | |
| @@ -87,87 +96,76 @@ class WebformQuery { | |
| return $this; | |
| } | |
| - public function orderBy($field, $direction = 'ASC') { | |
| + /** | |
| + * @param $field | |
| + * @param string $direction | |
| + * @param string $table | |
| + * | |
| + * @return $this | |
| + */ | |
| + public function orderBy($field, $direction = 'ASC', $submissionData = TRUE) { | |
| // Make sure direction is valid. | |
| $direction = ($direction !== 'ASC') ? 'DESC' : 'ASC'; | |
| $this->sort[] = [ | |
| 'field' => $field, | |
| 'direction' => $direction, | |
| + 'table' => $submissionData ? 'wsd' : 'ws', | |
| ]; | |
| return $this; | |
| - | |
| } | |
| /** | |
| - * | |
| * Execute the query. | |
| * | |
| * @return array | |
| * Array of objects with one property: sid | |
| */ | |
| public function execute() { | |
| - // Generate query elements from the conditions. | |
| - $query_elements = $this->buildQuery(); | |
| - | |
| - // Clear the conditions and sorting. | |
| - $this->conditions = []; | |
| - $this->sort = []; | |
| - | |
| - // Execute the query. | |
| - $response = $this->connection->query($query_elements['query'], $query_elements['values']); | |
| - | |
| - // Return the results. | |
| - return $response->fetchAll(); | |
| + return $this->buildQuery()->execute()->fetchCol(); | |
| } | |
| /** | |
| * Build the query from the conditions. | |
| */ | |
| - public function buildQuery() { | |
| - $query = 'SELECT DISTINCT sid FROM {webform_submission_data} wsd'; | |
| - $values = []; | |
| - foreach ($this->conditions as $key => $condition) { | |
| - // Check if it's the first condition. | |
| - if ($key === 0) { | |
| - // Check for database field webform_id. | |
| - if ($condition['field'] == 'webform_id') { | |
| - $query .= ' WHERE wsd.webform_id ' . $condition['operator'] . ' :' . $condition['field']; | |
| - } | |
| - else { | |
| - $query .= ' WHERE wsd.name = :' . $condition['field'] . '_name AND wsd.value ' . $condition['operator'] . ' :' . $condition['field']; | |
| - $values[':' . $condition['field'] .'_name'] = $condition['field']; | |
| - } | |
| + private function buildQuery() { | |
| + $query = $this->connection->select('webform_submission', 'ws'); | |
| + $query->addField('ws', 'sid', 'ws_sid'); | |
| + | |
| + foreach ($this->conditions AS $key => $condition) { | |
| + $alias = $condition['table'] . $key; | |
| + $table = $condition['table'] == 'wsd' ? 'webform_submission_data' : 'webform_submission'; | |
| + $subquery = $this->connection->select($table, $alias); | |
| + $subquery->addField($alias, 'sid', $alias . '_sid'); | |
| + if ($condition['table'] == 'wsd') { | |
| + $subquery->condition($alias . '.name', $condition['field']); | |
| + $subquery->condition($alias . '.value', $condition['value'], $condition['operator']); | |
| } | |
| - else { | |
| - // Normal condition for a webform submission field. | |
| - $alias = 'wsd' . $key; | |
| - $query .= ' AND sid IN (SELECT sid from {webform_submission_data} ' . $alias . ' WHERE ' . $alias . '.name = :' . $condition['field'] . '_name'; | |
| - $query .= ' AND ' . $alias . '.value ' . $condition['operator'] . ' :' . $condition['field'] . ')'; | |
| - $values[':' . $condition['field'] .'_name'] = $condition['field']; | |
| + elseif ($condition['table'] == 'ws') { | |
| + $subquery->condition($alias . '.' . $condition['field'], $condition['value'], $condition['operator']); | |
| } | |
| - $values[':' . $condition['field']] = $condition['value']; | |
| + $query->condition('ws.sid', $subquery, 'IN'); | |
| } | |
| // Check for sort criteria. | |
| foreach ($this->sort as $key => $orderby) { | |
| - // Add comma separator for for additional ORDER BY. | |
| - if ($key > 0) { | |
| - $query .= ','; | |
| - } | |
| - // "obt": Order By Table. | |
| - $orderby_alias = 'obt' . $key; | |
| - | |
| - $query .= ' ORDER BY (' | |
| - . 'SELECT ' . $orderby_alias . '.value FROM {webform_submission_data} ' . $orderby_alias | |
| - . ' WHERE ' . $orderby_alias . '.name=\'' . $orderby['field'] . '\'' | |
| - . ' AND ' . $orderby_alias . '.sid=wsd.sid' | |
| - . ') ' . $orderby['direction']; | |
| + $alias = 'obt' . $orderby['table'] . $key; | |
| + $table = $orderby['table'] == 'wsd' ? 'webform_submission_data' : 'webform_submission'; | |
| + | |
| + $subquery = $this->connection->select($table, $alias); | |
| + $subquery->addField($alias, 'value'); | |
| + $subquery->condition($alias . '.name', $orderby['field']); | |
| + $subquery->condition($alias . '.sid', 'ws.ws_sid'); | |
| + | |
| + $query->orderBy($subquery, $orderby['direction']); | |
| } | |
| - return ['query' => $query, 'values' => $values]; | |
| + // Reset the queries | |
| + $this->conditions = []; | |
| + $this->sort = []; | |
| + return $query; | |
| } | |
| /** | |
| @@ -178,7 +176,7 @@ class WebformQuery { | |
| * @return string | |
| * Return operator or nothing. | |
| */ | |
| - public function validateOperator($operator) { | |
| + private function validateOperator($operator) { | |
| if (stripos($operator, 'UNION') !== FALSE || strpbrk($operator, '[-\'"();') !== FALSE) { | |
| trigger_error('Invalid characters in query operator: ' . $operator, E_USER_ERROR); | |
| return ''; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment