3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 class CRM_Contact_Form_Search_Custom_TagContributions
extends CRM_Contact_Form_Search_Custom_Base
implements CRM_Contact_Form_Search_Interface
{
19 protected $_formValues;
20 protected $_aclFrom = NULL;
21 protected $_aclWhere = NULL;
22 public $_permissionedComponent;
27 * @param array $formValues
29 public function __construct(&$formValues) {
30 $this->_formValues
= self
::formatSavedSearchFields($formValues);
31 $this->_permissionedComponent
= 'CiviContribute';
34 * Define the columns for search result rows
37 ts('Contact ID') => 'contact_id',
38 ts('Full Name') => 'sort_name',
39 ts('First Name') => 'first_name',
40 ts('Last Name') => 'last_name',
41 ts('Tag') => 'tag_name',
42 ts('Totals') => 'amount',
47 * @param CRM_Core_Form $form
49 public function buildForm(&$form) {
52 * You can define a custom title for the search form
54 $this->setTitle('Find Contribution Amounts by Tag');
57 * Define the search form fields here
60 $form->add('datepicker', 'start_date', ts('Contribution Date From'), [], FALSE, ['time' => FALSE]);
61 $form->add('datepicker', 'end_date', ts('...through'), [], FALSE, ['time' => FALSE]);
62 $tag = ['' => ts('- any tag -')] + CRM_Core_PseudoConstant
::get('CRM_Core_DAO_EntityTag', 'tag_id', ['onlyActive' => FALSE]);
63 $form->addElement('select', 'tag', ts('Tagged'), $tag);
66 * If you are using the sample template, this array tells the template fields to render
67 * for the search form.
69 $form->assign('elements', ['start_date', 'end_date', 'tag']);
73 * Define the smarty template used to layout the search form and results listings.
75 public function templateFile() {
76 return 'CRM/Contact/Form/Search/Custom.tpl';
80 * Construct the search query.
83 * @param int $rowcount
85 * @param bool $includeContactIDs
86 * @param bool $onlyIDs
91 $offset = 0, $rowcount = 0, $sort = NULL,
92 $includeContactIDs = FALSE, $onlyIDs = FALSE
95 // SELECT clause must include contact_id as an alias for civicrm_contact.id
97 $select = "contact_a.id as contact_id";
102 contact_a.id as contact_id,
103 contact_a.sort_name as sort_name,
104 contact_a.first_name as first_name,
105 contact_a.last_name as last_name,
106 GROUP_CONCAT(DISTINCT civicrm_tag.name ORDER BY civicrm_tag.name ASC ) as tag_name,
107 sum(civicrm_contribution.total_amount) as amount
110 $from = $this->from();
112 $where = $this->where($includeContactIDs);
120 $sql .= " GROUP BY contact_a.id";
121 // Define ORDER BY for query in $sort, with default value
123 if (is_string($sort)) {
124 $sort = CRM_Utils_Type
::escape($sort, 'String');
125 $sql .= " ORDER BY $sort ";
128 $sql .= " ORDER BY " . trim($sort->orderBy());
140 public function from() {
141 $this->buildACLClause('contact_a');
143 civicrm_contribution,
144 civicrm_contact contact_a
145 LEFT JOIN civicrm_entity_tag ON ( civicrm_entity_tag.entity_table = 'civicrm_contact' AND
146 civicrm_entity_tag.entity_id = contact_a.id )
147 LEFT JOIN civicrm_tag ON civicrm_tag.id = civicrm_entity_tag.tag_id {$this->_aclFrom}
153 * WHERE clause is an array built from any required JOINS plus conditional filters based on search criteria field values
158 * @param bool $includeContactIDs
162 public function where($includeContactIDs = FALSE) {
165 $clauses[] = "contact_a.contact_type = 'Individual'";
166 $clauses[] = "civicrm_contribution.contact_id = contact_a.id";
168 if ($this->_formValues
['start_date']) {
169 $clauses[] = "civicrm_contribution.receive_date >= '{$this->_formValues['start_date']} 00:00:00'";
172 if ($this->_formValues
['end_date']) {
173 $clauses[] = "civicrm_contribution.receive_date <= '{$this->_formValues['end_date']} 23:59:59'";
176 $tag = CRM_Utils_Array
::value('tag', $this->_formValues
);
178 $clauses[] = "civicrm_entity_tag.tag_id = $tag";
179 $clauses[] = "civicrm_tag.id = civicrm_entity_tag.tag_id";
182 $clauses[] = "civicrm_entity_tag.tag_id IS NOT NULL";
185 if ($includeContactIDs) {
187 foreach ($this->_formValues
as $id => $value) {
189 substr($id, 0, CRM_Core_Form
::CB_PREFIX_LEN
) == CRM_Core_Form
::CB_PREFIX
191 $contactIDs[] = substr($id, CRM_Core_Form
::CB_PREFIX_LEN
);
195 if (!empty($contactIDs)) {
196 $contactIDs = implode(', ', $contactIDs);
197 $clauses[] = "contact_a.id IN ( $contactIDs )";
200 if ($this->_aclWhere
) {
201 $clauses[] = " {$this->_aclWhere} ";
203 return implode(' AND ', $clauses);
207 * Functions below generally don't need to be modified
213 public function count() {
216 $dao = CRM_Core_DAO
::executeQuery($sql);
222 * @param int $rowcount
224 * @param bool $returnSQL Not used; included for consistency with parent; SQL is always returned
228 public function contactIDs($offset = 0, $rowcount = 0, $sort = NULL, $returnSQL = TRUE) {
229 return $this->all($offset, $rowcount, $sort, FALSE, TRUE);
235 public function &columns() {
236 return $this->_columns
;
242 public function setTitle($title) {
244 CRM_Utils_System
::setTitle($title);
247 CRM_Utils_System
::setTitle(ts('Search'));
254 public function summary() {
259 * @param string $tableAlias
261 public function buildACLClause($tableAlias = 'contact') {
262 list($this->_aclFrom
, $this->_aclWhere
) = CRM_Contact_BAO_Contact_Permission
::cacheClause($tableAlias);
266 * Format saved search fields for this custom group.
268 * Note this is a function to facilitate the transition to jcalendar for
269 * saved search groups. In time it can be stripped out again.
271 * @param array $formValues
275 public static function formatSavedSearchFields($formValues) {
280 foreach ($formValues as $element => $value) {
281 if (in_array($element, $dateFields) && !empty($value)) {
282 $formValues[$element] = date('Y-m-d', strtotime($value));