From 6f6fc960e9231c61b932521114dda1f7e8bd2a4c Mon Sep 17 00:00:00 2001
From: Jaap Jansma <jaap.jansma@civicoop.org>
Date: Tue, 26 Feb 2019 11:51:13 +0100
Subject: [PATCH] changed simple non required join to accept extra filters

---
 .../SimpleNonRequiredJoin.php                 | 21 +++++++++++-
 .../SqlDataFlow/IsNotNullWhereClause.php      | 34 +++++++++++++++++++
 2 files changed, 54 insertions(+), 1 deletion(-)
 create mode 100644 Civi/DataProcessor/DataFlow/SqlDataFlow/IsNotNullWhereClause.php

diff --git a/Civi/DataProcessor/DataFlow/MultipleDataFlows/SimpleNonRequiredJoin.php b/Civi/DataProcessor/DataFlow/MultipleDataFlows/SimpleNonRequiredJoin.php
index 8c0bf862..ef84eb4e 100644
--- a/Civi/DataProcessor/DataFlow/MultipleDataFlows/SimpleNonRequiredJoin.php
+++ b/Civi/DataProcessor/DataFlow/MultipleDataFlows/SimpleNonRequiredJoin.php
@@ -11,9 +11,13 @@ use Civi\DataProcessor\DataFlow\CombinedDataFlow\CombinedSqlDataFlow;
 use Civi\DataProcessor\DataFlow\SqlDataFlow;
 use Civi\DataProcessor\DataFlow\SqlTableDataFlow;
 use Civi\DataProcessor\ProcessorType\AbstractProcessorType;
+use Civi\DataProcessor\DataFlow\SqlDataFlow\WhereClauseInterface;
 
 class SimpleNonRequiredJoin  extends  SimpleJoin {
 
+  /**
+   * @var WhereClauseInterface[]
+   */
   protected $filterClauses = array();
 
   public function __construct($left_prefix = null, $left_field = null, $right_prefix = null, $right_field = null, $type = "INNER") {
@@ -38,6 +42,17 @@ class SimpleNonRequiredJoin  extends  SimpleJoin {
     return parent::setConfiguration($configuration);
   }
 
+
+  /**
+   * @param WhereClauseInterface $clause
+   *
+   * @return \Civi\DataProcessor\DataFlow\MultipleDataFlows\JoinInterface
+   */
+  public function addFilterClause(WhereClauseInterface $clause) {
+    $this->filterClauses[] = $clause;
+    return $this;
+  }
+
   /**
    * Returns the SQL join statement
    *
@@ -75,7 +90,11 @@ class SimpleNonRequiredJoin  extends  SimpleJoin {
       }
     }
     if (count($this->filterClauses)) {
-      $extraClause = " AND (".implode(" AND ", $this->filterClauses). ")";
+      $extraClauses = array();
+      foreach($this->filterClauses as $filterClause) {
+        $extraClauses[] = $filterClause->getWhereClause();
+      }
+      $extraClause = " AND (".implode(" AND ", $extraClauses). ")";
     }
 
     return "{$this->type} JOIN `{$table}` `{$table_alias}` {$joinClause} {$extraClause}";
diff --git a/Civi/DataProcessor/DataFlow/SqlDataFlow/IsNotNullWhereClause.php b/Civi/DataProcessor/DataFlow/SqlDataFlow/IsNotNullWhereClause.php
new file mode 100644
index 00000000..97074654
--- /dev/null
+++ b/Civi/DataProcessor/DataFlow/SqlDataFlow/IsNotNullWhereClause.php
@@ -0,0 +1,34 @@
+<?php
+/**
+ * @author Jaap Jansma <jaap.jansma@civicoop.org>
+ * @license AGPL-3.0
+ */
+
+namespace Civi\DataProcessor\DataFlow\SqlDataFlow;
+
+class IsNotNullWhereClause implements WhereClauseInterface {
+
+  protected $table_alias;
+
+  protected $field;
+
+  protected $operator;
+
+  protected $value;
+
+  public function __construct($table_alias, $field) {
+    $this->table_alias = $table_alias;
+    $this->field = $field;
+  }
+
+  /**
+   * Returns the where clause
+   * E.g. contact_type = 'Individual'
+   *
+   * @return string
+   */
+  public function getWhereClause() {
+    return "`{$this->table_alias}`.`{$this->field}` IS NOT NULL";
+  }
+
+}
\ No newline at end of file
-- 
GitLab