Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
0f03f337 | 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
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 | |
0f03f337 | 31 | * @copyright CiviCRM LLC (c) 2004-2017 |
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; | |
86538308 | 36 | /** |
5a409b50 | 37 | * Class constructor. |
38 | * | |
39 | * @param array $formValues | |
86538308 | 40 | */ |
00be9182 | 41 | public function __construct(&$formValues) { |
6a488035 TO |
42 | parent::__construct($formValues); |
43 | ||
44 | $this->_columns = array( | |
770a7ef9 JV |
45 | // If possible, don't use aliases for the columns you select. |
46 | // You can prefix columns with table aliases, if needed. | |
37990ecd | 47 | // |
770a7ef9 JV |
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. | |
37990ecd | 61 | ts('State') => 'state_province.name', |
6a488035 TO |
62 | ); |
63 | } | |
64 | ||
86538308 | 65 | /** |
c490a46a | 66 | * @param CRM_Core_Form $form |
86538308 | 67 | */ |
00be9182 | 68 | public function buildForm(&$form) { |
24431f7b CW |
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')); | |
6a488035 TO |
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 | ||
1cd3ffa9 EM |
79 | /** |
80 | * @param int $offset | |
81 | * @param int $rowcount | |
82 | * @param null $sort | |
83 | * @param bool $returnSQL | |
84 | * | |
85 | * @return string | |
86 | */ | |
00be9182 | 87 | public function contactIDs($offset = 0, $rowcount = 0, $sort = NULL, $returnSQL = FALSE) { |
7c34ab11 | 88 | return $this->all($offset, $rowcount, $sort, FALSE, TRUE); |
89 | } | |
90 | ||
86538308 EM |
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 | */ | |
59f4c9ee | 100 | public function all( |
51ccfbbe | 101 | $offset = 0, $rowcount = 0, $sort = NULL, |
6a488035 TO |
102 | $includeContactIDs = FALSE, $justIDs = FALSE |
103 | ) { | |
104 | if ($justIDs) { | |
105 | $selectClause = "contact_a.id as contact_id"; | |
562f502d | 106 | // Don't change sort order when $justIDs is TRUE, see CRM-14920. |
6a488035 TO |
107 | } |
108 | else { | |
770a7ef9 JV |
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. | |
51ccfbbe | 114 | $selectClause = " |
f18d5be8 | 115 | DISTINCT contact_a.id as contact_id , |
770a7ef9 | 116 | contact_a.id, |
f18d5be8 JV |
117 | contact_a.contact_type as contact_type, |
118 | contact_a.sort_name as sort_name, | |
37990ecd JV |
119 | address.street_address, |
120 | state_province.name | |
6a488035 TO |
121 | "; |
122 | } | |
123 | ||
124 | return $this->sql($selectClause, | |
125 | $offset, $rowcount, $sort, | |
126 | $includeContactIDs, NULL | |
127 | ); | |
128 | } | |
129 | ||
86538308 EM |
130 | /** |
131 | * @return string | |
132 | */ | |
00be9182 | 133 | public function from() { |
d14ccbdc SL |
134 | $this->buildACLClause('contact_a'); |
135 | $from = " | |
6a488035 TO |
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 ) | |
d14ccbdc | 140 | LEFT JOIN civicrm_state_province state_province ON state_province.id = address.state_province_id {$this->_aclFrom} |
6a488035 | 141 | "; |
d14ccbdc | 142 | return $from; |
6a488035 TO |
143 | } |
144 | ||
86538308 EM |
145 | /** |
146 | * @param bool $includeContactIDs | |
147 | * | |
148 | * @return string | |
149 | */ | |
00be9182 | 150 | public function where($includeContactIDs = FALSE) { |
6a488035 TO |
151 | $params = array(); |
152 | ||
353ffa53 TO |
153 | $count = 1; |
154 | $clause = array(); | |
6a488035 TO |
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')"; | |
7bbd0c9d | 168 | |
47b8444f SL |
169 | if ($this->_aclWhere) { |
170 | $clause[] = " {$this->_aclWhere} "; | |
171 | } | |
7bbd0c9d | 172 | |
6a488035 | 173 | if (!empty($clause)) { |
5b7c52cf | 174 | $where = implode(' AND ', $clause); |
6a488035 TO |
175 | } |
176 | ||
177 | return $this->whereClause($where, $params); | |
178 | } | |
179 | ||
86538308 EM |
180 | /** |
181 | * @return string | |
182 | */ | |
00be9182 | 183 | public function templateFile() { |
6a488035 TO |
184 | return 'CRM/Contact/Form/Search/Custom.tpl'; |
185 | } | |
96025800 | 186 | |
d14ccbdc SL |
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 | ||
6a488035 | 194 | } |