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