Merge pull request #12834 from pradpnayak/preRelHook
[civicrm-core.git] / CRM / Contact / Form / Search / Custom / PostalMailing.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2019
32 */
33 class CRM_Contact_Form_Search_Custom_PostalMailing extends CRM_Contact_Form_Search_Custom_Base implements CRM_Contact_Form_Search_Interface {
34 protected $_aclFrom = NULL;
35 protected $_aclWhere = NULL;
36 /**
37 * Class constructor.
38 *
39 * @param array $formValues
40 */
41 public function __construct(&$formValues) {
42 parent::__construct($formValues);
43
44 $this->_columns = array(
45 // If possible, don't use aliases for the columns you select.
46 // You can prefix columns with table aliases, if needed.
47 //
48 // If you don't do this, selecting individual records from the
49 // custom search result won't work if your results are sorted on the
50 // aliased colums.
51 // (This is why we map Contact ID on contact_a.id, and not on contact_id).
52 ts('Contact ID') => 'contact_a.id',
53 ts('Address') => 'street_address',
54 ts('Contact Type') => 'contact_type',
55 ts('Name') => 'sort_name',
56 // You need to provide a table alias if there field exists in multiple
57 // tables of your join. Name is also a field of address, so we prefix it
58 // by state_province.
59 // If you don't do this, the patch of CRM-16587 might cause database
60 // errors.
61 ts('State') => 'state_province.name',
62 );
63 }
64
65 /**
66 * @param CRM_Core_Form $form
67 */
68 public function buildForm(&$form) {
69 $groups = array('' => ts('- select group -')) + CRM_Core_PseudoConstant::nestedGroup(FALSE);
70 $form->addElement('select', 'group_id', ts('Group'), $groups, array('class' => 'crm-select2 huge'));
71
72 /**
73 * if you are using the standard template, this array tells the template what elements
74 * are part of the search criteria
75 */
76 $form->assign('elements', array('group_id'));
77 }
78
79 /**
80 * @param int $offset
81 * @param int $rowcount
82 * @param null $sort
83 * @param bool $returnSQL
84 *
85 * @return string
86 */
87 public function contactIDs($offset = 0, $rowcount = 0, $sort = NULL, $returnSQL = FALSE) {
88 return $this->all($offset, $rowcount, $sort, FALSE, TRUE);
89 }
90
91 /**
92 * @param int $offset
93 * @param int $rowcount
94 * @param null $sort
95 * @param bool $includeContactIDs
96 * @param bool $justIDs
97 *
98 * @return string
99 */
100 public function all(
101 $offset = 0, $rowcount = 0, $sort = NULL,
102 $includeContactIDs = FALSE, $justIDs = FALSE
103 ) {
104 if ($justIDs) {
105 $selectClause = "contact_a.id as contact_id";
106 // Don't change sort order when $justIDs is TRUE, see CRM-14920.
107 }
108 else {
109 // YOU NEED to select contact_a.id as contact_id, if you want to be able
110 // to select individual records from the result.
111 // But if you want to display the contact ID in your result set, you
112 // also need to select contact_a.id. This is because of the patch we
113 // use for CRM-16587.
114 $selectClause = "
115 DISTINCT contact_a.id as contact_id ,
116 contact_a.id,
117 contact_a.contact_type as contact_type,
118 contact_a.sort_name as sort_name,
119 address.street_address,
120 state_province.name
121 ";
122 }
123
124 return $this->sql($selectClause,
125 $offset, $rowcount, $sort,
126 $includeContactIDs, NULL
127 );
128 }
129
130 /**
131 * @return string
132 */
133 public function from() {
134 $this->buildACLClause('contact_a');
135 $from = "
136 FROM civicrm_group_contact as cgc,
137 civicrm_contact as contact_a
138 LEFT JOIN civicrm_address address ON (address.contact_id = contact_a.id AND
139 address.is_primary = 1 )
140 LEFT JOIN civicrm_state_province state_province ON state_province.id = address.state_province_id {$this->_aclFrom}
141 ";
142 return $from;
143 }
144
145 /**
146 * @param bool $includeContactIDs
147 *
148 * @return string
149 */
150 public function where($includeContactIDs = FALSE) {
151 $params = array();
152
153 $count = 1;
154 $clause = array();
155 $groupID = CRM_Utils_Array::value('group_id',
156 $this->_formValues
157 );
158 if ($groupID) {
159 $params[$count] = array($groupID, 'Integer');
160 $clause[] = "cgc.group_id = %{$count}";
161 }
162
163 $clause[] = "cgc.status = 'Added'";
164 $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))),
165 (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))),
166 cgc.contact_id )";
167 $clause[] = "contact_a.contact_type IN ('Individual','Household')";
168
169 if ($this->_aclWhere) {
170 $clause[] = " {$this->_aclWhere} ";
171 }
172
173 if (!empty($clause)) {
174 $where = implode(' AND ', $clause);
175 }
176
177 return $this->whereClause($where, $params);
178 }
179
180 /**
181 * @return string
182 */
183 public function templateFile() {
184 return 'CRM/Contact/Form/Search/Custom.tpl';
185 }
186
187 /**
188 * @param string $tableAlias
189 */
190 public function buildACLClause($tableAlias = 'contact') {
191 list($this->_aclFrom, $this->_aclWhere) = CRM_Contact_BAO_Contact_Permission::cacheClause($tableAlias);
192 }
193
194 }