3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
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;
39 function __construct(&$formValues) {
40 parent
::__construct($formValues);
42 $this->_columns
= array(
43 ts('Contact Id') => 'contact_id',
44 ts('Contact Type') => 'contact_type',
45 ts('Name') => 'sort_name',
46 ts('Email') => 'email',
52 function initialize() {
53 $this->_segmentSize
= CRM_Utils_Array
::value('segmentSize', $this->_formValues
);
55 $this->_includeGroups
= CRM_Utils_Array
::value('includeGroups', $this->_formValues
);
57 $this->_excludeGroups
= CRM_Utils_Array
::value('excludeGroups', $this->_formValues
);
59 $this->_allSearch
= FALSE;
60 $this->_groups
= FALSE;
62 if (empty($this->_includeGroups
) && empty($this->_excludeGroups
)) {
64 $this->_allSearch
= TRUE;
67 if (!empty($this->_includeGroups
) ||
!empty($this->_excludeGroups
)) {
69 $this->_groups
= TRUE;
73 function buildForm(&$form) {
80 $groups = CRM_Core_PseudoConstant
::group();
81 $inG = &$form->addElement('advmultiselect', 'includeGroups',
82 ts('Include Group(s)') . ' ', $groups,
85 'style' => 'width:240px',
86 'class' => 'advmultiselect',
90 $outG = &$form->addElement('advmultiselect', 'excludeGroups',
91 ts('Exclude Group(s)') . ' ', $groups,
94 'style' => 'width:240px',
95 'class' => 'advmultiselect',
99 $inG->setButtonAttributes('add', array('value' => ts('Add >>')));
100 $outG->setButtonAttributes('add', array('value' => ts('Add >>')));
101 $inG->setButtonAttributes('remove', array('value' => ts('<< Remove')));
102 $outG->setButtonAttributes('remove', array('value' => ts('<< Remove')));
104 $this->setTitle('Create a random segment of contacts');
107 * if you are using the standard template, this array tells the template what elements
108 * are part of the search criteria
110 $form->assign('elements', array('segmentSize', 'includeGroups', 'excludeGroups'));
117 function all($offset = 0, $rowcount = 0, $sort = NULL,
118 $includeContactIDs = FALSE, $justIDs = FALSE
121 $selectClause = "contact_a.id as contact_id";
124 $selectClause = "contact_a.id as contact_id,
125 contact_a.contact_type as contact_type,
126 contact_a.sort_name as sort_name,
127 civicrm_email.email as email";
130 return $this->sql($selectClause,
131 $offset, $rowcount, $sort,
132 $includeContactIDs, NULL
138 $randomNum = md5(uniqid());
139 $this->_tableName
= "civicrm_temp_custom_{$randomNum}";
141 //block for Group search
142 $smartGroup = array();
143 $group = new CRM_Contact_DAO_Group();
144 $group->is_active
= 1;
146 while ($group->fetch()) {
147 $allGroups[] = $group->id
;
148 if ($group->saved_search_id
) {
149 $smartGroup[$group->saved_search_id
] = $group->id
;
152 $includedGroups = implode(',', $allGroups);
154 if (!empty($this->_includeGroups
)) {
155 $iGroups = implode(',', $this->_includeGroups
);
158 //if no group selected search for all groups
159 $iGroups = $includedGroups;
161 if (is_array($this->_excludeGroups
)) {
162 $xGroups = implode(',', $this->_excludeGroups
);
168 $sql = "DROP TEMPORARY TABLE IF EXISTS Xg_{$this->_tableName}";
169 CRM_Core_DAO
::executeQuery($sql);
170 $sql = "CREATE TEMPORARY TABLE Xg_{$this->_tableName} ( contact_id int primary key) ENGINE=HEAP";
171 CRM_Core_DAO
::executeQuery($sql);
173 //used only when exclude group is selected
175 $excludeGroup = "INSERT INTO Xg_{$this->_tableName} ( contact_id )
176 SELECT DISTINCT civicrm_group_contact.contact_id
177 FROM civicrm_group_contact
179 civicrm_group_contact.status = 'Added' AND
180 civicrm_group_contact.group_id IN ( {$xGroups} )";
182 CRM_Core_DAO
::executeQuery($excludeGroup);
184 //search for smart group contacts
185 foreach ($this->_excludeGroups
as $keys => $values) {
186 if (in_array($values, $smartGroup)) {
187 $ssId = CRM_Utils_Array
::key($values, $smartGroup);
189 $smartSql = CRM_Contact_BAO_SavedSearch
::contactIDsSQL($ssId);
191 $smartSql = $smartSql . " AND contact_a.id NOT IN (
192 SELECT contact_id FROM civicrm_group_contact
193 WHERE civicrm_group_contact.group_id = {$values} AND civicrm_group_contact.status = 'Removed')";
195 $smartGroupQuery = " INSERT IGNORE INTO Xg_{$this->_tableName}(contact_id) $smartSql";
197 CRM_Core_DAO
::executeQuery($smartGroupQuery);
202 $sql = "DROP TEMPORARY TABLE IF EXISTS Ig_{$this->_tableName}";
203 CRM_Core_DAO
::executeQuery($sql);
204 $sql = "CREATE TEMPORARY TABLE Ig_{$this->_tableName}
205 ( id int PRIMARY KEY AUTO_INCREMENT,
207 group_names varchar(64)) ENGINE=HEAP";
209 if ($this->_debug
> 0) {
210 print "-- Include groups query: <pre>";
215 CRM_Core_DAO
::executeQuery($sql);
217 $includeGroup = "INSERT INTO Ig_{$this->_tableName} (contact_id, group_names)
218 SELECT civicrm_group_contact.contact_id, civicrm_group.name as group_name
219 FROM civicrm_group_contact
220 LEFT JOIN civicrm_group
221 ON civicrm_group_contact.group_id = civicrm_group.id";
223 //used only when exclude group is selected
225 $includeGroup .= " LEFT JOIN Xg_{$this->_tableName}
226 ON civicrm_group_contact.contact_id = Xg_{$this->_tableName}.contact_id";
228 $includeGroup .= " WHERE
229 civicrm_group_contact.status = 'Added' AND
230 civicrm_group_contact.group_id IN($iGroups)";
232 //used only when exclude group is selected
234 $includeGroup .= " AND Xg_{$this->_tableName}.contact_id IS null";
237 if ($this->_debug
> 0) {
238 print "-- Include groups query: <pre>";
239 print "$includeGroup;";
243 CRM_Core_DAO
::executeQuery($includeGroup);
245 //search for smart group contacts
246 foreach ($this->_includeGroups
as $keys => $values) {
247 if (in_array($values, $smartGroup)) {
249 $ssId = CRM_Utils_Array
::key($values, $smartGroup);
251 $smartSql = CRM_Contact_BAO_SavedSearch
::contactIDsSQL($ssId);
253 $smartSql .= " AND contact_a.id NOT IN (
254 SELECT contact_id FROM civicrm_group_contact
255 WHERE civicrm_group_contact.group_id = {$values} AND civicrm_group_contact.status = 'Removed')";
257 //used only when exclude group is selected
259 $smartSql .= " AND contact_a.id NOT IN (SELECT contact_id FROM Xg_{$this->_tableName})";
262 $smartGroupQuery = " INSERT IGNORE INTO Ig_{$this->_tableName}(contact_id)
265 CRM_Core_DAO
::executeQuery($smartGroupQuery);
266 $insertGroupNameQuery = "UPDATE IGNORE Ig_{$this->_tableName}
267 SET group_names = (SELECT title FROM civicrm_group
268 WHERE civicrm_group.id = $values)
269 WHERE Ig_{$this->_tableName}.contact_id IS NOT NULL
270 AND Ig_{$this->_tableName}.group_names IS NULL";
271 CRM_Core_DAO
::executeQuery($insertGroupNameQuery);
275 $from = "FROM civicrm_contact contact_a";
277 $fromTail = "LEFT JOIN civicrm_email ON ( contact_a.id = civicrm_email.contact_id AND civicrm_email.is_primary = 1 )";
279 $fromTail .= " INNER JOIN Ig_{$this->_tableName} temptable1 ON (contact_a.id = temptable1.contact_id)";
281 // now create a temp table to store the randomized contacts
282 $sql = "DROP TEMPORARY TABLE IF EXISTS random_{$this->_tableName}";
283 CRM_Core_DAO
::executeQuery($sql);
284 $sql = "CREATE TEMPORARY TABLE random_{$this->_tableName} ( id int primary key ) ENGINE=HEAP";
285 CRM_Core_DAO
::executeQuery($sql);
287 if (substr($this->_segmentSize
, -1) == '%') {
288 $countSql = "SELECT DISTINCT contact_a.id $from $fromTail
289 WHERE " . $this->where();
290 $dao = CRM_Core_DAO
::executeQuery($countSql);
291 $totalSize = $dao->N
;
292 $multiplier = substr($this->_segmentSize
, 0, strlen($this->_segmentSize
) - 1);
294 //CRM_Core_Error::debug( "Total size: $totalSize<br/>Multiplier: $multiplier<br/>");
295 $this->_segmentSize
= round($totalSize * $multiplier);
298 $sql = "INSERT INTO random_{$this->_tableName} ( id )
299 SELECT DISTINCT contact_a.id $from $fromTail
300 WHERE " . $this->where() . "
302 LIMIT {$this->_segmentSize}";
303 CRM_Core_DAO
::executeQuery($sql);
305 $from = "FROM random_{$this->_tableName} random";
307 $from .= " INNER JOIN civicrm_contact contact_a ON random.id = contact_a.id";
309 $from .= " $fromTail";
314 function where($includeContactIDs = FALSE) {
318 function templateFile() {
319 return 'CRM/Contact/Form/Search/Custom.tpl';
322 function setTitle($title) {
324 CRM_Utils_System
::setTitle($title);
327 CRM_Utils_System
::setTitle(ts('Search'));
334 $dao = CRM_Core_DAO
::executeQuery($sql);
338 function __destruct() {
339 // the temporary tables are dropped automatically
340 // so we dont do it here
341 // but let mysql clean up