3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2016
33 class CRM_Contact_Form_Search_Custom_RandomSegment
extends CRM_Contact_Form_Search_Custom_Base
implements CRM_Contact_Form_Search_Interface
{
35 protected $_debug = 0;
36 protected $_aclFrom = NULL;
37 protected $_aclWhere = NULL;
42 * @param array $formValues
44 public function __construct(&$formValues) {
45 parent
::__construct($formValues);
47 $this->_columns
= array(
48 ts('Contact ID') => 'contact_id',
49 ts('Contact Type') => 'contact_type',
50 ts('Name') => 'sort_name',
51 ts('Email') => 'email',
57 public function initialize() {
58 $this->_segmentSize
= CRM_Utils_Array
::value('segmentSize', $this->_formValues
);
60 $this->_includeGroups
= CRM_Utils_Array
::value('includeGroups', $this->_formValues
);
62 $this->_excludeGroups
= CRM_Utils_Array
::value('excludeGroups', $this->_formValues
);
64 $this->_allSearch
= FALSE;
65 $this->_groups
= FALSE;
67 if (empty($this->_includeGroups
) && empty($this->_excludeGroups
)) {
69 $this->_allSearch
= TRUE;
72 if (!empty($this->_includeGroups
) ||
!empty($this->_excludeGroups
)) {
74 $this->_groups
= TRUE;
79 * @param CRM_Core_Form $form
81 public function buildForm(&$form) {
88 $groups = CRM_Core_PseudoConstant
::nestedGroup();
90 $select2style = array(
92 'style' => 'width: 100%; max-width: 60em;',
93 'class' => 'crm-select2',
94 'placeholder' => ts('- select -'),
97 $form->add('select', 'includeGroups',
98 ts('Include Group(s)'),
104 $form->add('select', 'excludeGroups',
105 ts('Exclude Group(s)'),
111 $this->setTitle('Create a random segment of contacts');
114 * if you are using the standard template, this array tells the template what elements
115 * are part of the search criteria
117 $form->assign('elements', array('segmentSize', 'includeGroups', 'excludeGroups'));
123 public function summary() {
129 * @param int $rowcount
131 * @param bool $includeContactIDs
132 * @param bool $justIDs
137 $offset = 0, $rowcount = 0, $sort = NULL,
138 $includeContactIDs = FALSE, $justIDs = FALSE
141 $selectClause = "contact_a.id as contact_id";
144 $selectClause = "contact_a.id as contact_id,
145 contact_a.contact_type as contact_type,
146 contact_a.sort_name as sort_name,
147 civicrm_email.email as email";
150 return $this->sql($selectClause,
151 $offset, $rowcount, $sort,
152 $includeContactIDs, NULL
159 public function from() {
161 $randomNum = md5(uniqid());
162 $this->_tableName
= "civicrm_temp_custom_{$randomNum}";
164 //block for Group search
165 $smartGroup = array();
166 $group = new CRM_Contact_DAO_Group();
167 $group->is_active
= 1;
169 while ($group->fetch()) {
170 $allGroups[] = $group->id
;
171 if ($group->saved_search_id
) {
172 $smartGroup[$group->saved_search_id
] = $group->id
;
175 $includedGroups = implode(',', $allGroups);
177 if (!empty($this->_includeGroups
)) {
178 $iGroups = implode(',', $this->_includeGroups
);
181 //if no group selected search for all groups
182 $iGroups = $includedGroups;
184 if (is_array($this->_excludeGroups
)) {
185 $xGroups = implode(',', $this->_excludeGroups
);
191 $sql = "DROP TEMPORARY TABLE IF EXISTS Xg_{$this->_tableName}";
192 CRM_Core_DAO
::executeQuery($sql);
193 $sql = "CREATE TEMPORARY TABLE Xg_{$this->_tableName} ( contact_id int primary key) ENGINE=HEAP";
194 CRM_Core_DAO
::executeQuery($sql);
196 //used only when exclude group is selected
198 $excludeGroup = "INSERT INTO Xg_{$this->_tableName} ( contact_id )
199 SELECT DISTINCT civicrm_group_contact.contact_id
200 FROM civicrm_group_contact
202 civicrm_group_contact.status = 'Added' AND
203 civicrm_group_contact.group_id IN ( {$xGroups} )";
205 CRM_Core_DAO
::executeQuery($excludeGroup);
207 //search for smart group contacts
208 foreach ($this->_excludeGroups
as $keys => $values) {
209 if (in_array($values, $smartGroup)) {
210 $ssId = CRM_Utils_Array
::key($values, $smartGroup);
212 $smartSql = CRM_Contact_BAO_SavedSearch
::contactIDsSQL($ssId);
214 $smartSql = $smartSql . " AND contact_a.id NOT IN (
215 SELECT contact_id FROM civicrm_group_contact
216 WHERE civicrm_group_contact.group_id = {$values} AND civicrm_group_contact.status = 'Removed')";
218 $smartGroupQuery = " INSERT IGNORE INTO Xg_{$this->_tableName}(contact_id) $smartSql";
220 CRM_Core_DAO
::executeQuery($smartGroupQuery);
225 $sql = "DROP TEMPORARY TABLE IF EXISTS Ig_{$this->_tableName}";
226 CRM_Core_DAO
::executeQuery($sql);
227 $sql = "CREATE TEMPORARY TABLE Ig_{$this->_tableName}
228 ( id int PRIMARY KEY AUTO_INCREMENT,
230 group_names varchar(64)) ENGINE=HEAP";
232 if ($this->_debug
> 0) {
233 print "-- Include groups query: <pre>";
238 CRM_Core_DAO
::executeQuery($sql);
240 $includeGroup = "INSERT INTO Ig_{$this->_tableName} (contact_id, group_names)
241 SELECT civicrm_group_contact.contact_id, civicrm_group.name as group_name
242 FROM civicrm_group_contact
243 LEFT JOIN civicrm_group
244 ON civicrm_group_contact.group_id = civicrm_group.id";
246 //used only when exclude group is selected
248 $includeGroup .= " LEFT JOIN Xg_{$this->_tableName}
249 ON civicrm_group_contact.contact_id = Xg_{$this->_tableName}.contact_id";
251 $includeGroup .= " WHERE
252 civicrm_group_contact.status = 'Added' AND
253 civicrm_group_contact.group_id IN($iGroups)";
255 //used only when exclude group is selected
257 $includeGroup .= " AND Xg_{$this->_tableName}.contact_id IS null";
260 if ($this->_debug
> 0) {
261 print "-- Include groups query: <pre>";
262 print "$includeGroup;";
266 CRM_Core_DAO
::executeQuery($includeGroup);
268 //search for smart group contacts
269 foreach ($this->_includeGroups
as $keys => $values) {
270 if (in_array($values, $smartGroup)) {
272 $ssId = CRM_Utils_Array
::key($values, $smartGroup);
274 $smartSql = CRM_Contact_BAO_SavedSearch
::contactIDsSQL($ssId);
276 $smartSql .= " AND contact_a.id NOT IN (
277 SELECT contact_id FROM civicrm_group_contact
278 WHERE civicrm_group_contact.group_id = {$values} AND civicrm_group_contact.status = 'Removed')";
280 //used only when exclude group is selected
282 $smartSql .= " AND contact_a.id NOT IN (SELECT contact_id FROM Xg_{$this->_tableName})";
285 $smartGroupQuery = " INSERT IGNORE INTO Ig_{$this->_tableName}(contact_id)
288 CRM_Core_DAO
::executeQuery($smartGroupQuery);
289 $insertGroupNameQuery = "UPDATE IGNORE Ig_{$this->_tableName}
290 SET group_names = (SELECT title FROM civicrm_group
291 WHERE civicrm_group.id = $values)
292 WHERE Ig_{$this->_tableName}.contact_id IS NOT NULL
293 AND Ig_{$this->_tableName}.group_names IS NULL";
294 CRM_Core_DAO
::executeQuery($insertGroupNameQuery);
297 $this->buildACLClause('contact_a');
299 $from = "FROM civicrm_contact contact_a";
301 $fromTail = "LEFT JOIN civicrm_email ON ( contact_a.id = civicrm_email.contact_id AND civicrm_email.is_primary = 1 )";
303 $fromTail .= " INNER JOIN Ig_{$this->_tableName} temptable1 ON (contact_a.id = temptable1.contact_id)";
305 // now create a temp table to store the randomized contacts
306 $sql = "DROP TEMPORARY TABLE IF EXISTS random_{$this->_tableName}";
307 CRM_Core_DAO
::executeQuery($sql);
308 $sql = "CREATE TEMPORARY TABLE random_{$this->_tableName} ( id int primary key ) ENGINE=HEAP";
309 CRM_Core_DAO
::executeQuery($sql);
311 if (substr($this->_segmentSize
, -1) == '%') {
312 $countSql = "SELECT DISTINCT contact_a.id $from $fromTail
313 WHERE " . $this->where();
314 $dao = CRM_Core_DAO
::executeQuery($countSql);
315 $totalSize = $dao->N
;
316 $multiplier = substr($this->_segmentSize
, 0, strlen($this->_segmentSize
) - 1);
318 $this->_segmentSize
= round($totalSize * $multiplier);
321 $sql = "INSERT INTO random_{$this->_tableName} ( id )
322 SELECT DISTINCT contact_a.id $from $fromTail
323 WHERE " . $this->where() . "
325 LIMIT {$this->_segmentSize}";
326 CRM_Core_DAO
::executeQuery($sql);
328 $from = "FROM random_{$this->_tableName} random";
330 $from .= " INNER JOIN civicrm_contact contact_a ON random.id = contact_a.id {$this->_aclFrom}";
332 $from .= " $fromTail";
339 * @param bool $includeContactIDs
343 public function where($includeContactIDs = FALSE) {
346 if ($this->_aclWhere
) {
347 $where .= " AND {$this->_aclWhere} ";
356 public function templateFile() {
357 return 'CRM/Contact/Form/Search/Custom.tpl';
363 public function setTitle($title) {
365 CRM_Utils_System
::setTitle($title);
368 CRM_Utils_System
::setTitle(ts('Search'));
378 public function count() {
381 $dao = CRM_Core_DAO
::executeQuery($sql);
386 * Destructor function.
388 public function __destruct() {
389 // the temporary tables are dropped automatically
390 // so we don't do it here
391 // but let mysql clean up
396 * @param string $tableAlias
398 public function buildACLClause($tableAlias = 'contact') {
399 list($this->_aclFrom
, $this->_aclWhere
) = CRM_Contact_BAO_Contact_Permission
::cacheClause($tableAlias);