3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
33 class CRM_Contact_Form_Search_Custom_TagContributions
extends CRM_Contact_Form_Search_Custom_Base
implements CRM_Contact_Form_Search_Interface
{
35 protected $_formValues;
36 protected $_aclFrom = NULL;
37 protected $_aclWhere = NULL;
38 public $_permissionedComponent;
43 * @param array $formValues
45 public function __construct(&$formValues) {
46 $this->_formValues
= self
::formatSavedSearchFields($formValues);
47 $this->_permissionedComponent
= 'CiviContribute';
50 * Define the columns for search result rows
53 ts('Contact ID') => 'contact_id',
54 ts('Full Name') => 'sort_name',
55 ts('First Name') => 'first_name',
56 ts('Last Name') => 'last_name',
57 ts('Tag') => 'tag_name',
58 ts('Totals') => 'amount',
63 * @param CRM_Core_Form $form
65 public function buildForm(&$form) {
68 * You can define a custom title for the search form
70 $this->setTitle('Find Contribution Amounts by Tag');
73 * Define the search form fields here
76 $form->add('datepicker', 'start_date', ts('Contribution Date From'), [], FALSE, ['time' => FALSE]);
77 $form->add('datepicker', 'end_date', ts('...through'), [], FALSE, ['time' => FALSE]);
78 $tag = ['' => ts('- any tag -')] + CRM_Core_PseudoConstant
::get('CRM_Core_DAO_EntityTag', 'tag_id', ['onlyActive' => FALSE]);
79 $form->addElement('select', 'tag', ts('Tagged'), $tag);
82 * If you are using the sample template, this array tells the template fields to render
83 * for the search form.
85 $form->assign('elements', ['start_date', 'end_date', 'tag']);
89 * Define the smarty template used to layout the search form and results listings.
91 public function templateFile() {
92 return 'CRM/Contact/Form/Search/Custom.tpl';
96 * Construct the search query.
99 * @param int $rowcount
100 * @param string $sort
101 * @param bool $includeContactIDs
102 * @param bool $onlyIDs
107 $offset = 0, $rowcount = 0, $sort = NULL,
108 $includeContactIDs = FALSE, $onlyIDs = FALSE
111 // SELECT clause must include contact_id as an alias for civicrm_contact.id
113 $select = "contact_a.id as contact_id";
118 contact_a.id as contact_id,
119 contact_a.sort_name as sort_name,
120 contact_a.first_name as first_name,
121 contact_a.last_name as last_name,
122 GROUP_CONCAT(DISTINCT civicrm_tag.name ORDER BY civicrm_tag.name ASC ) as tag_name,
123 sum(civicrm_contribution.total_amount) as amount
126 $from = $this->from();
128 $where = $this->where($includeContactIDs);
136 $sql .= " GROUP BY contact_a.id";
137 // Define ORDER BY for query in $sort, with default value
139 if (is_string($sort)) {
140 $sort = CRM_Utils_Type
::escape($sort, 'String');
141 $sql .= " ORDER BY $sort ";
144 $sql .= " ORDER BY " . trim($sort->orderBy());
156 public function from() {
157 $this->buildACLClause('contact_a');
159 civicrm_contribution,
160 civicrm_contact contact_a
161 LEFT JOIN civicrm_entity_tag ON ( civicrm_entity_tag.entity_table = 'civicrm_contact' AND
162 civicrm_entity_tag.entity_id = contact_a.id )
163 LEFT JOIN civicrm_tag ON civicrm_tag.id = civicrm_entity_tag.tag_id {$this->_aclFrom}
169 * WHERE clause is an array built from any required JOINS plus conditional filters based on search criteria field values
174 * @param bool $includeContactIDs
178 public function where($includeContactIDs = FALSE) {
181 $clauses[] = "contact_a.contact_type = 'Individual'";
182 $clauses[] = "civicrm_contribution.contact_id = contact_a.id";
184 if ($this->_formValues
['start_date']) {
185 $clauses[] = "civicrm_contribution.receive_date >= '{$this->_formValues['start_date']} 00:00:00'";
188 if ($this->_formValues
['end_date']) {
189 $clauses[] = "civicrm_contribution.receive_date <= '{$this->_formValues['end_date']} 23:59:59'";
192 $tag = CRM_Utils_Array
::value('tag', $this->_formValues
);
194 $clauses[] = "civicrm_entity_tag.tag_id = $tag";
195 $clauses[] = "civicrm_tag.id = civicrm_entity_tag.tag_id";
198 $clauses[] = "civicrm_entity_tag.tag_id IS NOT NULL";
201 if ($includeContactIDs) {
203 foreach ($this->_formValues
as $id => $value) {
205 substr($id, 0, CRM_Core_Form
::CB_PREFIX_LEN
) == CRM_Core_Form
::CB_PREFIX
207 $contactIDs[] = substr($id, CRM_Core_Form
::CB_PREFIX_LEN
);
211 if (!empty($contactIDs)) {
212 $contactIDs = implode(', ', $contactIDs);
213 $clauses[] = "contact_a.id IN ( $contactIDs )";
216 if ($this->_aclWhere
) {
217 $clauses[] = " {$this->_aclWhere} ";
219 return implode(' AND ', $clauses);
223 * Functions below generally don't need to be modified
229 public function count() {
232 $dao = CRM_Core_DAO
::executeQuery($sql,
233 CRM_Core_DAO
::$_nullArray
240 * @param int $rowcount
242 * @param bool $returnSQL Not used; included for consistency with parent; SQL is always returned
246 public function contactIDs($offset = 0, $rowcount = 0, $sort = NULL, $returnSQL = TRUE) {
247 return $this->all($offset, $rowcount, $sort, FALSE, TRUE);
253 public function &columns() {
254 return $this->_columns
;
260 public function setTitle($title) {
262 CRM_Utils_System
::setTitle($title);
265 CRM_Utils_System
::setTitle(ts('Search'));
272 public function summary() {
277 * @param string $tableAlias
279 public function buildACLClause($tableAlias = 'contact') {
280 list($this->_aclFrom
, $this->_aclWhere
) = CRM_Contact_BAO_Contact_Permission
::cacheClause($tableAlias);
284 * Format saved search fields for this custom group.
286 * Note this is a function to facilitate the transition to jcalendar for
287 * saved search groups. In time it can be stripped out again.
289 * @param array $formValues
293 public static function formatSavedSearchFields($formValues) {
298 foreach ($formValues as $element => $value) {
299 if (in_array($element, $dateFields) && !empty($value)) {
300 $formValues[$element] = date('Y-m-d', strtotime($value));