3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
35 class CRM_Contact_Form_Search_Custom_RandomSegment
extends CRM_Contact_Form_Search_Custom_Base
implements CRM_Contact_Form_Search_Interface
{
37 protected $_debug = 0; function __construct(&$formValues) {
38 parent
::__construct($formValues);
40 $this->_columns
= array(
41 ts('Contact Id') => 'contact_id',
42 ts('Contact Type') => 'contact_type',
43 ts('Name') => 'sort_name',
44 ts('Email') => 'email',
50 function initialize() {
51 $this->_segmentSize
= CRM_Utils_Array
::value('segmentSize', $this->_formValues
);
53 $this->_includeGroups
= CRM_Utils_Array
::value('includeGroups', $this->_formValues
);
55 $this->_excludeGroups
= CRM_Utils_Array
::value('excludeGroups', $this->_formValues
);
57 $this->_allSearch
= FALSE;
58 $this->_groups
= FALSE;
60 if (empty($this->_includeGroups
) && empty($this->_excludeGroups
)) {
62 $this->_allSearch
= TRUE;
65 if (!empty($this->_includeGroups
) ||
!empty($this->_excludeGroups
)) {
67 $this->_groups
= TRUE;
71 function buildForm(&$form) {
78 $groups = CRM_Core_PseudoConstant
::group();
79 $inG = &$form->addElement('advmultiselect', 'includeGroups',
80 ts('Include Group(s)') . ' ', $groups,
83 'style' => 'width:240px',
84 'class' => 'advmultiselect',
88 $outG = &$form->addElement('advmultiselect', 'excludeGroups',
89 ts('Exclude Group(s)') . ' ', $groups,
92 'style' => 'width:240px',
93 'class' => 'advmultiselect',
97 $inG->setButtonAttributes('add', array('value' => ts('Add >>')));
98 $outG->setButtonAttributes('add', array('value' => ts('Add >>')));
99 $inG->setButtonAttributes('remove', array('value' => ts('<< Remove')));
100 $outG->setButtonAttributes('remove', array('value' => ts('<< Remove')));
102 $this->setTitle('Create a random segment of contacts');
105 * if you are using the standard template, this array tells the template what elements
106 * are part of the search criteria
108 $form->assign('elements', array('segmentSize', 'includeGroups', 'excludeGroups'));
115 function all($offset = 0, $rowcount = 0, $sort = NULL,
116 $includeContactIDs = FALSE, $justIDs = FALSE
119 $selectClause = "contact_a.id as contact_id";
122 $selectClause = "contact_a.id as contact_id,
123 contact_a.contact_type as contact_type,
124 contact_a.sort_name as sort_name,
125 civicrm_email.email as email";
128 return $this->sql($selectClause,
129 $offset, $rowcount, $sort,
130 $includeContactIDs, NULL
136 $randomNum = md5(uniqid());
137 $this->_tableName
= "civicrm_temp_custom_{$randomNum}";
139 //block for Group search
140 $smartGroup = array();
141 $group = new CRM_Contact_DAO_Group();
142 $group->is_active
= 1;
144 while ($group->fetch()) {
145 $allGroups[] = $group->id
;
146 if ($group->saved_search_id
) {
147 $smartGroup[$group->saved_search_id
] = $group->id
;
150 $includedGroups = implode(',', $allGroups);
152 if (!empty($this->_includeGroups
)) {
153 $iGroups = implode(',', $this->_includeGroups
);
156 //if no group selected search for all groups
157 $iGroups = $includedGroups;
159 if (is_array($this->_excludeGroups
)) {
160 $xGroups = implode(',', $this->_excludeGroups
);
166 $sql = "DROP TEMPORARY TABLE IF EXISTS Xg_{$this->_tableName}";
167 CRM_Core_DAO
::executeQuery($sql);
168 $sql = "CREATE TEMPORARY TABLE Xg_{$this->_tableName} ( contact_id int primary key) ENGINE=HEAP";
169 CRM_Core_DAO
::executeQuery($sql);
171 //used only when exclude group is selected
173 $excludeGroup = "INSERT INTO Xg_{$this->_tableName} ( contact_id )
174 SELECT DISTINCT civicrm_group_contact.contact_id
175 FROM civicrm_group_contact
177 civicrm_group_contact.status = 'Added' AND
178 civicrm_group_contact.group_id IN ( {$xGroups} )";
180 CRM_Core_DAO
::executeQuery($excludeGroup);
182 //search for smart group contacts
183 foreach ($this->_excludeGroups
as $keys => $values) {
184 if (in_array($values, $smartGroup)) {
185 $ssId = CRM_Utils_Array
::key($values, $smartGroup);
187 $smartSql = CRM_Contact_BAO_SavedSearch
::contactIDsSQL($ssId);
189 $smartSql = $smartSql . " AND contact_a.id NOT IN (
190 SELECT contact_id FROM civicrm_group_contact
191 WHERE civicrm_group_contact.group_id = {$values} AND civicrm_group_contact.status = 'Removed')";
193 $smartGroupQuery = " INSERT IGNORE INTO Xg_{$this->_tableName}(contact_id) $smartSql";
195 CRM_Core_DAO
::executeQuery($smartGroupQuery);
200 $sql = "DROP TEMPORARY TABLE IF EXISTS Ig_{$this->_tableName}";
201 CRM_Core_DAO
::executeQuery($sql);
202 $sql = "CREATE TEMPORARY TABLE Ig_{$this->_tableName}
203 ( id int PRIMARY KEY AUTO_INCREMENT,
205 group_names varchar(64)) ENGINE=HEAP";
207 if ($this->_debug
> 0) {
208 print "-- Include groups query: <pre>";
213 CRM_Core_DAO
::executeQuery($sql);
215 $includeGroup = "INSERT INTO Ig_{$this->_tableName} (contact_id, group_names)
216 SELECT civicrm_group_contact.contact_id, civicrm_group.name as group_name
217 FROM civicrm_group_contact
218 LEFT JOIN civicrm_group
219 ON civicrm_group_contact.group_id = civicrm_group.id";
221 //used only when exclude group is selected
223 $includeGroup .= " LEFT JOIN Xg_{$this->_tableName}
224 ON civicrm_group_contact.contact_id = Xg_{$this->_tableName}.contact_id";
226 $includeGroup .= " WHERE
227 civicrm_group_contact.status = 'Added' AND
228 civicrm_group_contact.group_id IN($iGroups)";
230 //used only when exclude group is selected
232 $includeGroup .= " AND Xg_{$this->_tableName}.contact_id IS null";
235 if ($this->_debug
> 0) {
236 print "-- Include groups query: <pre>";
237 print "$includeGroup;";
241 CRM_Core_DAO
::executeQuery($includeGroup);
243 //search for smart group contacts
244 foreach ($this->_includeGroups
as $keys => $values) {
245 if (in_array($values, $smartGroup)) {
247 $ssId = CRM_Utils_Array
::key($values, $smartGroup);
249 $smartSql = CRM_Contact_BAO_SavedSearch
::contactIDsSQL($ssId);
251 $smartSql .= " AND contact_a.id NOT IN (
252 SELECT contact_id FROM civicrm_group_contact
253 WHERE civicrm_group_contact.group_id = {$values} AND civicrm_group_contact.status = 'Removed')";
255 //used only when exclude group is selected
257 $smartSql .= " AND contact_a.id NOT IN (SELECT contact_id FROM Xg_{$this->_tableName})";
260 $smartGroupQuery = " INSERT IGNORE INTO Ig_{$this->_tableName}(contact_id)
263 CRM_Core_DAO
::executeQuery($smartGroupQuery);
264 $insertGroupNameQuery = "UPDATE IGNORE Ig_{$this->_tableName}
265 SET group_names = (SELECT title FROM civicrm_group
266 WHERE civicrm_group.id = $values)
267 WHERE Ig_{$this->_tableName}.contact_id IS NOT NULL
268 AND Ig_{$this->_tableName}.group_names IS NULL";
269 CRM_Core_DAO
::executeQuery($insertGroupNameQuery);
273 $from = "FROM civicrm_contact contact_a";
275 $fromTail = "LEFT JOIN civicrm_email ON ( contact_a.id = civicrm_email.contact_id AND civicrm_email.is_primary = 1 )";
277 $fromTail .= " INNER JOIN Ig_{$this->_tableName} temptable1 ON (contact_a.id = temptable1.contact_id)";
279 // now create a temp table to store the randomized contacts
280 $sql = "DROP TEMPORARY TABLE IF EXISTS random_{$this->_tableName}";
281 CRM_Core_DAO
::executeQuery($sql);
282 $sql = "CREATE TEMPORARY TABLE random_{$this->_tableName} ( id int primary key ) ENGINE=HEAP";
283 CRM_Core_DAO
::executeQuery($sql);
285 if (substr($this->_segmentSize
, -1) == '%') {
286 $countSql = "SELECT DISTINCT contact_a.id $from $fromTail
287 WHERE " . $this->where();
288 $dao = CRM_Core_DAO
::executeQuery($countSql);
289 $totalSize = $dao->N
;
290 $multiplier = substr($this->_segmentSize
, 0, strlen($this->_segmentSize
) - 1);
292 //CRM_Core_Error::debug( "Total size: $totalSize<br/>Multiplier: $multiplier<br/>");
293 $this->_segmentSize
= round($totalSize * $multiplier);
296 $sql = "INSERT INTO random_{$this->_tableName} ( id )
297 SELECT DISTINCT contact_a.id $from $fromTail
298 WHERE " . $this->where() . "
300 LIMIT {$this->_segmentSize}";
301 CRM_Core_DAO
::executeQuery($sql);
303 $from = "FROM random_{$this->_tableName} random";
305 $from .= " INNER JOIN civicrm_contact contact_a ON random.id = contact_a.id";
307 $from .= " $fromTail";
312 function where($includeContactIDs = FALSE) {
316 function templateFile() {
317 return 'CRM/Contact/Form/Search/Custom.tpl';
320 function setTitle($title) {
322 CRM_Utils_System
::setTitle($title);
325 CRM_Utils_System
::setTitle(ts('Search'));
332 $dao = CRM_Core_DAO
::executeQuery($sql);
336 function __destruct() {
337 // the temporary tables are dropped automatically
338 // so we dont do it here
339 // but let mysql clean up