Merge pull request #6640 from eileenmcnaughton/comments-3
[civicrm-core.git] / CRM / Contact / Form / Search / Custom / PostalMailing.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
6a488035
TO
32 * $Id$
33 *
34 */
35class CRM_Contact_Form_Search_Custom_PostalMailing extends CRM_Contact_Form_Search_Custom_Base implements CRM_Contact_Form_Search_Interface {
d14ccbdc
SL
36 protected $_aclFrom = NULL;
37 protected $_aclWhere = NULL;
86538308
EM
38 /**
39 * @param $formValues
40 */
00be9182 41 public function __construct(&$formValues) {
6a488035
TO
42 parent::__construct($formValues);
43
44 $this->_columns = array(
7b99ead3 45 ts('Contact ID') => 'contact_id',
6a488035
TO
46 ts('Address') => 'address',
47 ts('Contact Type') => 'contact_type',
48 ts('Name') => 'sort_name',
49 ts('State') => 'state_province',
50 );
51 }
52
86538308 53 /**
c490a46a 54 * @param CRM_Core_Form $form
86538308 55 */
00be9182 56 public function buildForm(&$form) {
24431f7b
CW
57 $groups = array('' => ts('- select group -')) + CRM_Core_PseudoConstant::nestedGroup(FALSE);
58 $form->addElement('select', 'group_id', ts('Group'), $groups, array('class' => 'crm-select2 huge'));
6a488035
TO
59
60 /**
61 * if you are using the standard template, this array tells the template what elements
62 * are part of the search criteria
63 */
64 $form->assign('elements', array('group_id'));
65 }
66
1cd3ffa9
EM
67 /**
68 * @param int $offset
69 * @param int $rowcount
70 * @param null $sort
71 * @param bool $returnSQL
72 *
73 * @return string
74 */
00be9182 75 public function contactIDs($offset = 0, $rowcount = 0, $sort = NULL, $returnSQL = FALSE) {
7c34ab11 76 return $this->all($offset, $rowcount, $sort, FALSE, TRUE);
77 }
78
86538308
EM
79 /**
80 * @param int $offset
81 * @param int $rowcount
82 * @param null $sort
83 * @param bool $includeContactIDs
84 * @param bool $justIDs
85 *
86 * @return string
87 */
59f4c9ee 88 public function all(
51ccfbbe 89 $offset = 0, $rowcount = 0, $sort = NULL,
6a488035
TO
90 $includeContactIDs = FALSE, $justIDs = FALSE
91 ) {
92 if ($justIDs) {
93 $selectClause = "contact_a.id as contact_id";
7c34ab11 94 $sort = 'contact_a.id';
6a488035
TO
95 }
96 else {
51ccfbbe 97 $selectClause = "
6a488035
TO
98DISTINCT contact_a.id as contact_id ,
99contact_a.contact_type as contact_type,
100contact_a.sort_name as sort_name,
101address.street_address as address,
102state_province.name as state_province
103";
104 }
105
106 return $this->sql($selectClause,
107 $offset, $rowcount, $sort,
108 $includeContactIDs, NULL
109 );
110 }
111
86538308
EM
112 /**
113 * @return string
114 */
00be9182 115 public function from() {
d14ccbdc
SL
116 $this->buildACLClause('contact_a');
117 $from = "
6a488035
TO
118FROM civicrm_group_contact as cgc,
119 civicrm_contact as contact_a
120LEFT JOIN civicrm_address address ON (address.contact_id = contact_a.id AND
121 address.is_primary = 1 )
d14ccbdc 122LEFT JOIN civicrm_state_province state_province ON state_province.id = address.state_province_id {$this->_aclFrom}
6a488035 123";
d14ccbdc 124 return $from;
6a488035
TO
125 }
126
86538308
EM
127 /**
128 * @param bool $includeContactIDs
129 *
130 * @return string
131 */
00be9182 132 public function where($includeContactIDs = FALSE) {
6a488035
TO
133 $params = array();
134
353ffa53
TO
135 $count = 1;
136 $clause = array();
6a488035
TO
137 $groupID = CRM_Utils_Array::value('group_id',
138 $this->_formValues
139 );
140 if ($groupID) {
141 $params[$count] = array($groupID, 'Integer');
142 $clause[] = "cgc.group_id = %{$count}";
143 }
144
145 $clause[] = "cgc.status = 'Added'";
146 $clause[] = "contact_a.id = IF( EXISTS(select cr.id from civicrm_relationship cr where (cr.contact_id_a = cgc.contact_id AND (cr.relationship_type_id = 7 OR cr.relationship_type_id = 6))),
147 (select cr.contact_id_b from civicrm_relationship cr where (cr.contact_id_a = cgc.contact_id AND (cr.relationship_type_id = 7 OR cr.relationship_type_id = 6))),
148 cgc.contact_id )";
149 $clause[] = "contact_a.contact_type IN ('Individual','Household')";
7bbd0c9d 150
47b8444f
SL
151 if ($this->_aclWhere) {
152 $clause[] = " {$this->_aclWhere} ";
153 }
7bbd0c9d 154
6a488035 155 if (!empty($clause)) {
5b7c52cf 156 $where = implode(' AND ', $clause);
6a488035
TO
157 }
158
159 return $this->whereClause($where, $params);
160 }
161
86538308
EM
162 /**
163 * @return string
164 */
00be9182 165 public function templateFile() {
6a488035
TO
166 return 'CRM/Contact/Form/Search/Custom.tpl';
167 }
96025800 168
d14ccbdc
SL
169 /**
170 * @param string $tableAlias
171 */
172 public function buildACLClause($tableAlias = 'contact') {
173 list($this->_aclFrom, $this->_aclWhere) = CRM_Contact_BAO_Contact_Permission::cacheClause($tableAlias);
174 }
175
6a488035 176}