Merge pull request #6496 from totten/master-regen-leak
[civicrm-core.git] / CRM / Contact / Form / Search / Custom / ContribSYBNT.php
index 48927a2835ca4a9c7bd4ece57f890483c2585e28..3236149eca93f68a3e3ade8fb909862837ab8a57 100644 (file)
@@ -1,9 +1,9 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.6                                                |
+ | CiviCRM version 4.7                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2014                                |
+ | Copyright CiviCRM LLC (c) 2004-2015                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2014
+ * @copyright CiviCRM LLC (c) 2004-2015
  * $Id$
  *
  */
 class CRM_Contact_Form_Search_Custom_ContribSYBNT extends CRM_Contact_Form_Search_Custom_Base implements CRM_Contact_Form_Search_Interface {
 
   protected $_formValues;
+  protected $_aclFrom = NULL;
+  protected $_aclWhere = NULL;
   public $_permissionedComponent;
 
   /**
@@ -128,7 +130,7 @@ class CRM_Contact_Form_Search_Custom_ContribSYBNT extends CRM_Contact_Form_Searc
    * @param int $offset
    * @param int $rowcount
    * @param null $sort
-   * @param boolean $returnSQL Not used; included for consistency with parent; SQL is always returned
+   * @param bool $returnSQL Not used; included for consistency with parent; SQL is always returned
    *
    * @return string
    */
@@ -165,23 +167,20 @@ class CRM_Contact_Form_Search_Custom_ContribSYBNT extends CRM_Contact_Form_Searc
 
     $from = $this->from();
 
+    $select = $this->select();
     if ($justIDs) {
-      $select = $this->select();
       $select .= ', contact_a.id, display_name';
     }
     else {
-      $select = $this->select();
       $select = "
            DISTINCT contact_a.id as contact_id,
            contact_a.display_name as display_name,
-           $select
-";
-
+           $select ";
     }
-
+    $this->buildACLClause('contact_a');
     $sql = "
 SELECT     $select
-FROM       civicrm_contact AS contact_a
+FROM       civicrm_contact AS contact_a {$this->_aclFrom}
 LEFT JOIN  civicrm_contribution contrib_1 ON contrib_1.contact_id = contact_a.id
            $from
 WHERE      contrib_1.contact_id = contact_a.id
@@ -198,7 +197,6 @@ ORDER BY   donation_amount desc
       $dao = CRM_Core_DAO::executeQuery($query);
       $sql = "SELECT contact_a.id as contact_id FROM CustomSearch_SYBNT_temp as contact_a";
     }
-
     return $sql;
   }
 
@@ -336,7 +334,9 @@ AND      c.receive_date < {$this->start_date_1}
 
       $clauses[] = " xg.contact_id IS NULL ";
     }
-
+    if ($this->_aclWhere) {
+      $clauses[] .= " {$this->_aclWhere} ";
+    }
     return implode(' AND ', $clauses);
   }
 
@@ -393,4 +393,11 @@ AND      c.receive_date < {$this->start_date_1}
     }
   }
 
+  /**
+   * @param string $tableAlias
+   */
+  public function buildACLClause($tableAlias = 'contact') {
+    list($this->_aclFrom, $this->_aclWhere) = CRM_Contact_BAO_Contact_Permission::cacheClause($tableAlias);
+  }
+
 }