Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
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 | */ | |
35 | class CRM_Contact_Form_Search_Custom_RandomSegment extends CRM_Contact_Form_Search_Custom_Base implements CRM_Contact_Form_Search_Interface { | |
36 | ||
430ae6dd TO |
37 | protected $_debug = 0; |
38 | ||
86538308 EM |
39 | /** |
40 | * @param $formValues | |
41 | */ | |
00be9182 | 42 | public function __construct(&$formValues) { |
6a488035 TO |
43 | parent::__construct($formValues); |
44 | ||
45 | $this->_columns = array( | |
7b99ead3 | 46 | ts('Contact ID') => 'contact_id', |
6a488035 TO |
47 | ts('Contact Type') => 'contact_type', |
48 | ts('Name') => 'sort_name', | |
49 | ts('Email') => 'email', | |
50 | ); | |
51 | ||
52 | $this->initialize(); | |
53 | } | |
54 | ||
00be9182 | 55 | public function initialize() { |
6a488035 TO |
56 | $this->_segmentSize = CRM_Utils_Array::value('segmentSize', $this->_formValues); |
57 | ||
58 | $this->_includeGroups = CRM_Utils_Array::value('includeGroups', $this->_formValues); | |
59 | ||
60 | $this->_excludeGroups = CRM_Utils_Array::value('excludeGroups', $this->_formValues); | |
61 | ||
62 | $this->_allSearch = FALSE; | |
63 | $this->_groups = FALSE; | |
64 | ||
65 | if (empty($this->_includeGroups) && empty($this->_excludeGroups)) { | |
66 | //empty search | |
67 | $this->_allSearch = TRUE; | |
68 | } | |
69 | ||
70 | if (!empty($this->_includeGroups) || !empty($this->_excludeGroups)) { | |
71 | //group(s) selected | |
72 | $this->_groups = TRUE; | |
73 | } | |
74 | } | |
75 | ||
86538308 | 76 | /** |
11cac306 | 77 | * @param CRM_Core_Form $form |
86538308 | 78 | */ |
00be9182 | 79 | public function buildForm(&$form) { |
6a488035 TO |
80 | $form->add('text', |
81 | 'segmentSize', | |
82 | ts('Segment Size'), | |
83 | TRUE | |
84 | ); | |
85 | ||
24431f7b | 86 | $groups = CRM_Core_PseudoConstant::nestedGroup(); |
11cac306 CW |
87 | |
88 | $select2style = array( | |
89 | 'multiple' => TRUE, | |
90 | 'style' => 'width: 100%; max-width: 60em;', | |
91 | 'class' => 'crm-select2', | |
92 | 'placeholder' => ts('- select -'), | |
6a488035 TO |
93 | ); |
94 | ||
11cac306 CW |
95 | $form->add('select', 'includeGroups', |
96 | ts('Include Group(s)'), | |
97 | $groups, | |
98 | FALSE, | |
99 | $select2style | |
6a488035 TO |
100 | ); |
101 | ||
11cac306 CW |
102 | $form->add('select', 'excludeGroups', |
103 | ts('Exclude Group(s)'), | |
104 | $groups, | |
105 | FALSE, | |
106 | $select2style | |
107 | ); | |
6a488035 TO |
108 | |
109 | $this->setTitle('Create a random segment of contacts'); | |
110 | ||
111 | /** | |
112 | * if you are using the standard template, this array tells the template what elements | |
113 | * are part of the search criteria | |
114 | */ | |
115 | $form->assign('elements', array('segmentSize', 'includeGroups', 'excludeGroups')); | |
116 | } | |
117 | ||
86538308 EM |
118 | /** |
119 | * @return null | |
120 | */ | |
00be9182 | 121 | public function summary() { |
6a488035 TO |
122 | return NULL; |
123 | } | |
124 | ||
86538308 EM |
125 | /** |
126 | * @param int $offset | |
127 | * @param int $rowcount | |
128 | * @param null $sort | |
129 | * @param bool $includeContactIDs | |
130 | * @param bool $justIDs | |
131 | * | |
132 | * @return string | |
133 | */ | |
389bcebf | 134 | public function all( |
51ccfbbe | 135 | $offset = 0, $rowcount = 0, $sort = NULL, |
6a488035 TO |
136 | $includeContactIDs = FALSE, $justIDs = FALSE |
137 | ) { | |
138 | if ($justIDs) { | |
139 | $selectClause = "contact_a.id as contact_id"; | |
140 | } | |
141 | else { | |
142 | $selectClause = "contact_a.id as contact_id, | |
143 | contact_a.contact_type as contact_type, | |
144 | contact_a.sort_name as sort_name, | |
145 | civicrm_email.email as email"; | |
146 | } | |
147 | ||
148 | return $this->sql($selectClause, | |
149 | $offset, $rowcount, $sort, | |
150 | $includeContactIDs, NULL | |
151 | ); | |
152 | } | |
153 | ||
86538308 EM |
154 | /** |
155 | * @return string | |
156 | */ | |
00be9182 | 157 | public function from() { |
6a488035 TO |
158 | //define table name |
159 | $randomNum = md5(uniqid()); | |
160 | $this->_tableName = "civicrm_temp_custom_{$randomNum}"; | |
161 | ||
162 | //block for Group search | |
353ffa53 TO |
163 | $smartGroup = array(); |
164 | $group = new CRM_Contact_DAO_Group(); | |
6a488035 TO |
165 | $group->is_active = 1; |
166 | $group->find(); | |
167 | while ($group->fetch()) { | |
168 | $allGroups[] = $group->id; | |
169 | if ($group->saved_search_id) { | |
170 | $smartGroup[$group->saved_search_id] = $group->id; | |
171 | } | |
172 | } | |
173 | $includedGroups = implode(',', $allGroups); | |
174 | ||
175 | if (!empty($this->_includeGroups)) { | |
176 | $iGroups = implode(',', $this->_includeGroups); | |
177 | } | |
178 | else { | |
179 | //if no group selected search for all groups | |
180 | $iGroups = $includedGroups; | |
181 | } | |
182 | if (is_array($this->_excludeGroups)) { | |
183 | $xGroups = implode(',', $this->_excludeGroups); | |
184 | } | |
185 | else { | |
186 | $xGroups = 0; | |
187 | } | |
188 | ||
189 | $sql = "DROP TEMPORARY TABLE IF EXISTS Xg_{$this->_tableName}"; | |
190 | CRM_Core_DAO::executeQuery($sql); | |
191 | $sql = "CREATE TEMPORARY TABLE Xg_{$this->_tableName} ( contact_id int primary key) ENGINE=HEAP"; | |
192 | CRM_Core_DAO::executeQuery($sql); | |
193 | ||
194 | //used only when exclude group is selected | |
195 | if ($xGroups != 0) { | |
196 | $excludeGroup = "INSERT INTO Xg_{$this->_tableName} ( contact_id ) | |
197 | SELECT DISTINCT civicrm_group_contact.contact_id | |
198 | FROM civicrm_group_contact | |
199 | WHERE | |
200 | civicrm_group_contact.status = 'Added' AND | |
201 | civicrm_group_contact.group_id IN ( {$xGroups} )"; | |
202 | ||
203 | CRM_Core_DAO::executeQuery($excludeGroup); | |
204 | ||
205 | //search for smart group contacts | |
206 | foreach ($this->_excludeGroups as $keys => $values) { | |
207 | if (in_array($values, $smartGroup)) { | |
208 | $ssId = CRM_Utils_Array::key($values, $smartGroup); | |
209 | ||
210 | $smartSql = CRM_Contact_BAO_SavedSearch::contactIDsSQL($ssId); | |
211 | ||
212 | $smartSql = $smartSql . " AND contact_a.id NOT IN ( | |
213 | SELECT contact_id FROM civicrm_group_contact | |
214 | WHERE civicrm_group_contact.group_id = {$values} AND civicrm_group_contact.status = 'Removed')"; | |
215 | ||
216 | $smartGroupQuery = " INSERT IGNORE INTO Xg_{$this->_tableName}(contact_id) $smartSql"; | |
217 | ||
218 | CRM_Core_DAO::executeQuery($smartGroupQuery); | |
219 | } | |
220 | } | |
221 | } | |
222 | ||
223 | $sql = "DROP TEMPORARY TABLE IF EXISTS Ig_{$this->_tableName}"; | |
224 | CRM_Core_DAO::executeQuery($sql); | |
225 | $sql = "CREATE TEMPORARY TABLE Ig_{$this->_tableName} | |
226 | ( id int PRIMARY KEY AUTO_INCREMENT, | |
227 | contact_id int, | |
228 | group_names varchar(64)) ENGINE=HEAP"; | |
229 | ||
230 | if ($this->_debug > 0) { | |
231 | print "-- Include groups query: <pre>"; | |
232 | print "$sql;"; | |
233 | print "</pre>"; | |
234 | } | |
235 | ||
236 | CRM_Core_DAO::executeQuery($sql); | |
237 | ||
238 | $includeGroup = "INSERT INTO Ig_{$this->_tableName} (contact_id, group_names) | |
239 | SELECT civicrm_group_contact.contact_id, civicrm_group.name as group_name | |
240 | FROM civicrm_group_contact | |
241 | LEFT JOIN civicrm_group | |
242 | ON civicrm_group_contact.group_id = civicrm_group.id"; | |
243 | ||
244 | //used only when exclude group is selected | |
245 | if ($xGroups != 0) { | |
246 | $includeGroup .= " LEFT JOIN Xg_{$this->_tableName} | |
247 | ON civicrm_group_contact.contact_id = Xg_{$this->_tableName}.contact_id"; | |
248 | } | |
249 | $includeGroup .= " WHERE | |
250 | civicrm_group_contact.status = 'Added' AND | |
251 | civicrm_group_contact.group_id IN($iGroups)"; | |
252 | ||
253 | //used only when exclude group is selected | |
254 | if ($xGroups != 0) { | |
255 | $includeGroup .= " AND Xg_{$this->_tableName}.contact_id IS null"; | |
256 | } | |
257 | ||
258 | if ($this->_debug > 0) { | |
259 | print "-- Include groups query: <pre>"; | |
260 | print "$includeGroup;"; | |
261 | print "</pre>"; | |
262 | } | |
263 | ||
264 | CRM_Core_DAO::executeQuery($includeGroup); | |
265 | ||
266 | //search for smart group contacts | |
267 | foreach ($this->_includeGroups as $keys => $values) { | |
268 | if (in_array($values, $smartGroup)) { | |
269 | ||
270 | $ssId = CRM_Utils_Array::key($values, $smartGroup); | |
271 | ||
272 | $smartSql = CRM_Contact_BAO_SavedSearch::contactIDsSQL($ssId); | |
273 | ||
274 | $smartSql .= " AND contact_a.id NOT IN ( | |
275 | SELECT contact_id FROM civicrm_group_contact | |
276 | WHERE civicrm_group_contact.group_id = {$values} AND civicrm_group_contact.status = 'Removed')"; | |
277 | ||
278 | //used only when exclude group is selected | |
279 | if ($xGroups != 0) { | |
280 | $smartSql .= " AND contact_a.id NOT IN (SELECT contact_id FROM Xg_{$this->_tableName})"; | |
281 | } | |
282 | ||
283 | $smartGroupQuery = " INSERT IGNORE INTO Ig_{$this->_tableName}(contact_id) | |
284 | $smartSql"; | |
285 | ||
286 | CRM_Core_DAO::executeQuery($smartGroupQuery); | |
287 | $insertGroupNameQuery = "UPDATE IGNORE Ig_{$this->_tableName} | |
288 | SET group_names = (SELECT title FROM civicrm_group | |
289 | WHERE civicrm_group.id = $values) | |
290 | WHERE Ig_{$this->_tableName}.contact_id IS NOT NULL | |
291 | AND Ig_{$this->_tableName}.group_names IS NULL"; | |
292 | CRM_Core_DAO::executeQuery($insertGroupNameQuery); | |
293 | } | |
294 | } | |
295 | ||
296 | $from = "FROM civicrm_contact contact_a"; | |
297 | ||
298 | $fromTail = "LEFT JOIN civicrm_email ON ( contact_a.id = civicrm_email.contact_id AND civicrm_email.is_primary = 1 )"; | |
299 | ||
300 | $fromTail .= " INNER JOIN Ig_{$this->_tableName} temptable1 ON (contact_a.id = temptable1.contact_id)"; | |
301 | ||
302 | // now create a temp table to store the randomized contacts | |
303 | $sql = "DROP TEMPORARY TABLE IF EXISTS random_{$this->_tableName}"; | |
304 | CRM_Core_DAO::executeQuery($sql); | |
305 | $sql = "CREATE TEMPORARY TABLE random_{$this->_tableName} ( id int primary key ) ENGINE=HEAP"; | |
306 | CRM_Core_DAO::executeQuery($sql); | |
307 | ||
308 | if (substr($this->_segmentSize, -1) == '%') { | |
309 | $countSql = "SELECT DISTINCT contact_a.id $from $fromTail | |
310 | WHERE " . $this->where(); | |
353ffa53 TO |
311 | $dao = CRM_Core_DAO::executeQuery($countSql); |
312 | $totalSize = $dao->N; | |
6a488035 TO |
313 | $multiplier = substr($this->_segmentSize, 0, strlen($this->_segmentSize) - 1); |
314 | $multiplier /= 100; | |
6a488035 TO |
315 | $this->_segmentSize = round($totalSize * $multiplier); |
316 | } | |
317 | ||
318 | $sql = "INSERT INTO random_{$this->_tableName} ( id ) | |
319 | SELECT DISTINCT contact_a.id $from $fromTail | |
320 | WHERE " . $this->where() . " | |
321 | ORDER BY RAND() | |
322 | LIMIT {$this->_segmentSize}"; | |
323 | CRM_Core_DAO::executeQuery($sql); | |
324 | ||
325 | $from = "FROM random_{$this->_tableName} random"; | |
326 | ||
327 | $from .= " INNER JOIN civicrm_contact contact_a ON random.id = contact_a.id"; | |
328 | ||
329 | $from .= " $fromTail"; | |
330 | ||
331 | return $from; | |
332 | } | |
333 | ||
86538308 EM |
334 | /** |
335 | * @param bool $includeContactIDs | |
336 | * | |
337 | * @return string | |
338 | */ | |
00be9182 | 339 | public function where($includeContactIDs = FALSE) { |
6a488035 TO |
340 | return '(1)'; |
341 | } | |
342 | ||
86538308 EM |
343 | /** |
344 | * @return string | |
345 | */ | |
00be9182 | 346 | public function templateFile() { |
6a488035 TO |
347 | return 'CRM/Contact/Form/Search/Custom.tpl'; |
348 | } | |
349 | ||
86538308 EM |
350 | /** |
351 | * @param $title | |
352 | */ | |
00be9182 | 353 | public function setTitle($title) { |
6a488035 TO |
354 | if ($title) { |
355 | CRM_Utils_System::setTitle($title); | |
356 | } | |
357 | else { | |
358 | CRM_Utils_System::setTitle(ts('Search')); | |
359 | } | |
360 | } | |
361 | ||
dcc94695 EM |
362 | /** |
363 | * @return mixed | |
364 | */ | |
bb271167 EM |
365 | /** |
366 | * @return mixed | |
367 | */ | |
00be9182 | 368 | public function count() { |
6a488035 TO |
369 | $sql = $this->all(); |
370 | ||
371 | $dao = CRM_Core_DAO::executeQuery($sql); | |
372 | return $dao->N; | |
373 | } | |
374 | ||
2e2605fe EM |
375 | /** |
376 | * Destructor function. | |
377 | */ | |
00be9182 | 378 | public function __destruct() { |
6a488035 | 379 | // the temporary tables are dropped automatically |
bb271167 | 380 | // so we don't do it here |
6a488035 | 381 | // but let mysql clean up |
389bcebf | 382 | return NULL; |
6a488035 | 383 | } |
96025800 | 384 | |
6a488035 | 385 | } |