コード例 #1
0
 /**
 * Given a table and id pair, return the filter clause
 *
 * @param string $table -   The table owning the object
 * @param int $id   -       The ID of the object
 * @param array ref $tables - Tables that will be needed in the FROM
 *
 * @return string|null  -   WHERE-style clause to filter results,
 or null if $table or $id is null
 * @access public
 * @static
 */
 public static function getClause($table, $id, &$tables)
 {
     $table = CRM_Utils_Type::escape($table, 'String');
     $id = CRM_Utils_Type::escape($id, 'Integer');
     $whereTables = array();
     $ssTable = CRM_Contact_BAO_SavedSearch::getTableName();
     if (empty($table)) {
         return NULL;
     } elseif ($table == $ssTable) {
         return CRM_Contact_BAO_SavedSearch::whereClause($id, $tables, $whereTables);
     } elseif (!empty($id)) {
         $tables[$table] = TRUE;
         return "{$table}.id = {$id}";
     }
     return NULL;
 }
コード例 #2
0
 /**
  * Get from where email (whatever that means!).
  *
  * @param int $id
  *
  * @return array
  */
 public static function fromWhereEmail($id)
 {
     $params = self::getSearchParams($id);
     if ($params) {
         if (!empty($params['customSearchID'])) {
             return CRM_Contact_BAO_SearchCustom::fromWhereEmail(NULL, $id);
         } else {
             $tables = $whereTables = array('civicrm_contact' => 1, 'civicrm_email' => 1);
             $where = CRM_Contact_BAO_SavedSearch::whereClause($id, $tables, $whereTables);
             $from = CRM_Contact_BAO_Query::fromClause($whereTables);
             return array($from, $where);
         }
     } else {
         // fix for CRM-7240
         $from = "\nFROM      civicrm_contact contact_a\nLEFT JOIN civicrm_email ON (contact_a.id = civicrm_email.contact_id AND civicrm_email.is_primary = 1)\n";
         $where = " ( 1 ) ";
         $tables['civicrm_contact'] = $whereTables['civicrm_contact'] = 1;
         $tables['civicrm_email'] = $whereTables['civicrm_email'] = 1;
         return array($from, $where);
     }
 }
コード例 #3
0
ファイル: DrupalBase.php プロジェクト: nielosz/civicrm-core
 /**
  * Get group clause for this user. The group Clause filters the
  * list of groups that the user is permitted to see in a group listing.
  * For example it will filter both the list on the 'Manage Groups' page
  * and on the contact 'Groups' tab
  *
  * the aclGroup hook & configured ACLs contribute to this data.
  * If the contact is allowed to see all contacts the function will return  ( 1 )
  *
  * @todo the history of this function is that there was some confusion as to
  * whether it was filtering contacts or groups & some cruft may remain
  *
  * @param int $type
  *   The type of permission needed.
  * @param array $tables
  *   (reference) add the tables that are needed for the select clause.
  * @param array $whereTables
  *   (reference) add the tables that are needed for the where clause.
  *
  * @return string
  *   the clause to add to the query retrieving viewable groups
  */
 public function groupClause($type, &$tables, &$whereTables)
 {
     if (!isset($this->_viewPermissionedGroups)) {
         $this->group();
     }
     // we basically get all the groups here
     $groupKey = 'all';
     if ($type == CRM_Core_Permission::EDIT) {
         if ($this->_editAdminUser) {
             $clause = ' ( 1 ) ';
         } elseif (empty($this->_editPermissionedGroups[$groupKey])) {
             $clause = ' ( 0 ) ';
         } else {
             $clauses = array();
             $groups = implode(', ', $this->_editPermissionedGroups[$groupKey]);
             $clauses[] = ' ( civicrm_group_contact.group_id IN ( ' . implode(', ', array_keys($this->_editPermissionedGroups[$groupKey])) . " ) AND civicrm_group_contact.status = 'Added' ) ";
             $tables['civicrm_group_contact'] = 1;
             $whereTables['civicrm_group_contact'] = 1;
             // foreach group that is potentially a saved search, add the saved search clause
             foreach (array_keys($this->_editPermissionedGroups[$groupKey]) as $id) {
                 $group = new CRM_Contact_DAO_Group();
                 $group->id = $id;
                 if ($group->find(TRUE) && $group->saved_search_id) {
                     $clause = CRM_Contact_BAO_SavedSearch::whereClause($group->saved_search_id, $tables, $whereTables);
                     if (trim($clause)) {
                         $clauses[] = $clause;
                     }
                 }
             }
             $clause = ' ( ' . implode(' OR ', $clauses) . ' ) ';
         }
     } else {
         if ($this->_viewAdminUser) {
             $clause = ' ( 1 ) ';
         } elseif (empty($this->_viewPermissionedGroups[$groupKey])) {
             $clause = ' ( 0 ) ';
         } else {
             $clauses = array();
             $groups = implode(', ', $this->_viewPermissionedGroups[$groupKey]);
             $clauses[] = ' civicrm_group.id IN (' . implode(', ', array_keys($this->_viewPermissionedGroups[$groupKey])) . " )  ";
             $tables['civicrm_group'] = 1;
             $whereTables['civicrm_group'] = 1;
             $clause = ' ( ' . implode(' OR ', $clauses) . ' ) ';
         }
     }
     return $clause;
 }
コード例 #4
0
 static function fromWhereEmail($id)
 {
     $params =& self::getSearchParams($id);
     if ($params) {
         if (CRM_Utils_Array::value('customSearchID', $params)) {
             require_once 'CRM/Contact/BAO/SearchCustom.php';
             return CRM_Contact_BAO_SearchCustom::fromWhereEmail(null, $id);
         } else {
             $tables = $whereTables = array('civicrm_contact' => 1, 'civicrm_email' => 1);
             $where = CRM_Contact_BAO_SavedSearch::whereClause($id, $tables, $whereTables);
             $from = CRM_Contact_BAO_Query::fromClause($whereTables);
             return array($from, $where);
         }
     } else {
         // fix for CRM-7240
         $from = "\nFROM      civicrm_contact contact_a \nLEFT JOIN civicrm_email ON (contact_a.id = civicrm_email.contact_id AND civicrm_email.is_primary = 1)\n";
         $where = " ( 1 ) ";
         $tables['civicrm_contact'] = $whereTables['civicrm_contact'] = 1;
         $tables['civicrm_email'] = $whereTables['civicrm_email'] = 1;
         return array($from, $where);
     }
 }
コード例 #5
0
ファイル: Query.php プロジェクト: bhirsch/voipdrupal-4.7-1.0
 /**
  * where / qill clause for smart groups
  *
  * @return void
  * @access public
  */
 function savedSearch()
 {
     $config =& CRM_Core_Config::singleton();
     $ssWhere = array();
     $group =& new CRM_Contact_BAO_Group();
     foreach (array_keys($this->_params['group']) as $group_id) {
         $group->id = $group_id;
         $group->find(true);
         if (isset($group->saved_search_id)) {
             require_once 'CRM/Contact/BAO/SavedSearch.php';
             if ($config->mysqlVersion >= 4.1) {
                 $sfv =& CRM_Contact_BAO_SavedSearch::getFormValues($group->saved_search_id);
                 $smarts =& CRM_Contact_BAO_Contact::searchQuery($sfv, 0, 0, null, false, false, false, true, true);
                 $ssWhere[] = " \n                            (civicrm_contact.id IN ({$smarts})  \n                            AND civicrm_contact.id NOT IN ( \n                            SELECT contact_id FROM civicrm_group_contact \n                            WHERE civicrm_group_contact.group_id = " . CRM_Utils_Type::escape($group_id, 'Integer') . " AND civicrm_group_contact.status = 'Removed'))";
             } else {
                 $ssw = CRM_Contact_BAO_SavedSearch::whereClause($group->saved_search_id, $this->_tables, $this->_whereTables);
                 /* FIXME: bug with multiple group searches */
                 $ssWhere[] = "({$ssw} AND\n                                   (civicrm_group_contact.id is null OR\n                                     (civicrm_group_contact.group_id = " . CRM_Utils_Type::escape($group_id, 'Integer') . " AND\n                                      civicrm_group_contact.status = 'Added')))";
             }
         }
         $group->reset();
         $group->selectAdd('*');
     }
     if (!empty($ssWhere)) {
         $this->_tables['civicrm_group_contact'] = "civicrm_contact.id = civicrm_group_contact.contact_id AND civicrm_group_contact.group_id IN (" . implode(',', array_keys($this->_params['group'])) . ')';
         $this->_whereTables['civicrm_group_contact'] = $this->_tables['civicrm_group_contact'];
         return implode(' OR ', $ssWhere);
     }
     return null;
 }
コード例 #6
0
ファイル: Drupal.php プロジェクト: hampelm/Ginsberg-CiviDemo
 /**
  * Get group clause for this user
  *
  * @param int $type the type of permission needed
  * @param  array $tables (reference ) add the tables that are needed for the select clause
  * @param  array $whereTables (reference ) add the tables that are needed for the where clause
  *
  * @return string the group where clause for this user
  * @access public
  */
 public static function groupClause($type, &$tables, &$whereTables)
 {
     if (!isset(self::$_viewPermissionedGroups)) {
         self::group();
     }
     if ($type == CRM_Core_Permission::EDIT) {
         if (self::$_editAdminUser) {
             $clause = ' ( 1 ) ';
         } else {
             if (empty(self::$_editPermissionedGroups)) {
                 $clause = ' ( 0 ) ';
             } else {
                 $clauses = array();
                 $groups = implode(', ', self::$_editPermissionedGroups);
                 $clauses[] = ' ( civicrm_group_contact.group_id IN ( ' . implode(', ', array_keys(self::$_editPermissionedGroups)) . " ) AND civicrm_group_contact.status = 'Added' ) ";
                 $tables['civicrm_group_contact'] = 1;
                 $whereTables['civicrm_group_contact'] = 1;
                 // foreach group that is potentially a saved search, add the saved search clause
                 foreach (array_keys(self::$_editPermissionedGroups) as $id) {
                     $group = new CRM_Contact_DAO_Group();
                     $group->id = $id;
                     if ($group->find(true) && $group->saved_search_id) {
                         require_once 'CRM/Contact/BAO/SavedSearch.php';
                         $clause = CRM_Contact_BAO_SavedSearch::whereClause($group->saved_search_id, $tables, $whereTables);
                         if (trim($clause)) {
                             $clauses[] = $clause;
                         }
                     }
                 }
                 $clause = ' ( ' . implode(' OR ', $clauses) . ' ) ';
             }
         }
     } else {
         if (self::$_viewAdminUser) {
             $clause = ' ( 1 ) ';
         } else {
             if (empty(self::$_viewPermissionedGroups)) {
                 $clause = ' ( 0 ) ';
             } else {
                 $clauses = array();
                 $groups = implode(', ', self::$_viewPermissionedGroups);
                 $clauses[] = ' ( civicrm_group_contact.group_id IN (' . implode(', ', array_keys(self::$_viewPermissionedGroups)) . " ) AND civicrm_group_contact.status = 'Added' ) ";
                 $tables['civicrm_group_contact'] = 1;
                 $whereTables['civicrm_group_contact'] = 1;
                 // foreach group that is potentially a saved search, add the saved search clause
                 foreach (array_keys(self::$_viewPermissionedGroups) as $id) {
                     $group = new CRM_Contact_DAO_Group();
                     $group->id = $id;
                     if ($group->find(true) && $group->saved_search_id) {
                         require_once 'CRM/Contact/BAO/SavedSearch.php';
                         $clause = CRM_Contact_BAO_SavedSearch::whereClause($group->saved_search_id, $tables, $whereTables);
                         if (trim($clause)) {
                             $clauses[] = $clause;
                         }
                     }
                 }
                 $clause = ' ( ' . implode(' OR ', $clauses) . ' ) ';
             }
         }
     }
     return $clause;
 }
コード例 #7
0
ファイル: Drupal.php プロジェクト: bhirsch/voipdrupal-4.7-1.0
 /**
  * Get group clause for this user
  *
  * @param int $type the type of permission needed
  * @param  array $tables (reference ) add the tables that are needed for the select clause
  * @param  array $whereTables (reference ) add the tables that are needed for the where clause
  *
  * @return string the group where clause for this user
  * @access public
  */
 function groupClause($type, &$tables, &$whereTables)
 {
     if (!isset($GLOBALS['_CRM_CORE_PERMISSION_DRUPAL']['_viewPermissionedGroups'])) {
         CRM_Core_Permission_Drupal::group();
     }
     if ($type == CRM_CORE_PERMISSION_EDIT) {
         if ($GLOBALS['_CRM_CORE_PERMISSION_DRUPAL']['_editAdminUser']) {
             $clause = ' ( 1 ) ';
         } else {
             if (empty($GLOBALS['_CRM_CORE_PERMISSION_DRUPAL']['_editPermissionedGroups'])) {
                 $clause = ' ( 0 ) ';
             } else {
                 $clauses = array();
                 $groups = implode(', ', $GLOBALS['_CRM_CORE_PERMISSION_DRUPAL']['_editPermissionedGroups']);
                 $clauses[] = ' ( civicrm_group_contact.group_id IN ( ' . implode(', ', array_keys($GLOBALS['_CRM_CORE_PERMISSION_DRUPAL']['_editPermissionedGroups'])) . " ) AND civicrm_group_contact.status = 'Added' ) ";
                 $tables['civicrm_group_contact'] = 1;
                 $whereTables['civicrm_group_contact'] = 1;
                 // foreach group that is potentially a saved search, add the saved search clause
                 foreach (array_keys($GLOBALS['_CRM_CORE_PERMISSION_DRUPAL']['_editPermissionedGroups']) as $id) {
                     $group =& new CRM_Contact_DAO_Group();
                     $group->id = $id;
                     if ($group->find(true) && $group->saved_search_id) {
                         require_once 'CRM/Contact/BAO/SavedSearch.php';
                         $clauses[] = CRM_Contact_BAO_SavedSearch::whereClause($group->saved_search_id, $tables, $whereTables);
                     }
                 }
                 $clause = ' ( ' . implode(' OR ', $clauses) . ' ) ';
             }
         }
     } else {
         if ($GLOBALS['_CRM_CORE_PERMISSION_DRUPAL']['_viewAdminUser']) {
             $clause = ' ( 1 ) ';
         } else {
             if (empty($GLOBALS['_CRM_CORE_PERMISSION_DRUPAL']['_viewPermissionedGroups'])) {
                 $clause = ' ( 0 ) ';
             } else {
                 $clauses = array();
                 $groups = implode(', ', $GLOBALS['_CRM_CORE_PERMISSION_DRUPAL']['_viewPermissionedGroups']);
                 $clauses[] = ' ( civicrm_group_contact.group_id IN (' . implode(', ', array_keys($GLOBALS['_CRM_CORE_PERMISSION_DRUPAL']['_viewPermissionedGroups'])) . " ) AND civicrm_group_contact.status = 'Added' ) ";
                 $tables['civicrm_group_contact'] = 1;
                 $whereTables['civicrm_group_contact'] = 1;
                 // foreach group that is potentially a saved search, add the saved search clause
                 foreach (array_keys($GLOBALS['_CRM_CORE_PERMISSION_DRUPAL']['_viewPermissionedGroups']) as $id) {
                     $group =& new CRM_Contact_DAO_Group();
                     $group->id = $id;
                     if ($group->find(true) && $group->saved_search_id) {
                         require_once 'CRM/Contact/BAO/SavedSearch.php';
                         $whereTables = array();
                         $clauses[] = CRM_Contact_BAO_SavedSearch::whereClause($group->saved_search_id, $tables, $whereTables);
                     }
                 }
                 $clause = ' ( ' . implode(' OR ', $clauses) . ' ) ';
             }
         }
     }
     return $clause;
 }
コード例 #8
0
ファイル: SavedSearch.php プロジェクト: ksecor/civicrm
 static function fromWhereEmail($id)
 {
     $params =& self::getSearchParams($id);
     if ($params) {
         if (CRM_Utils_Array::value('customSearchID', $params)) {
             require_once 'CRM/Contact/BAO/SearchCustom.php';
             return CRM_Contact_BAO_SearchCustom::fromWhereEmail(null, $id);
         } else {
             $tables = $whereTables = array('civicrm_contact' => 1, 'civicrm_email' => 1);
             $where = CRM_Contact_BAO_SavedSearch::whereClause($id, $tables, $whereTables);
             $from = CRM_Contact_BAO_Query::fromClause($whereTables);
             return array($from, $where);
         }
     } else {
         CRM_Core_Error::fatal('No contactID clause');
     }
 }
コード例 #9
0
 /**
  * Find all intended recipients of a mailing
  *
  * @param int $job_id       Job ID
  * @return object           A DAO loaded with results of the form
  *                              (email_id, contact_id)
  */
 function &getRecipients($job_id)
 {
     $mailingGroup =& new CRM_Mailing_DAO_Group();
     $mailing = CRM_Mailing_BAO_Mailing::getTableName();
     $job = CRM_Mailing_BAO_Job::getTableName();
     $mg = CRM_Mailing_DAO_Group::getTableName();
     $eq = CRM_Mailing_Event_DAO_Queue::getTableName();
     $ed = CRM_Mailing_Event_DAO_Delivered::getTableName();
     $eb = CRM_Mailing_Event_DAO_Bounce::getTableName();
     $email = CRM_Core_DAO_Email::getTableName();
     $contact = CRM_Contact_DAO_Contact::getTableName();
     $location = CRM_Core_DAO_Location::getTableName();
     $group = CRM_Contact_DAO_Group::getTableName();
     $g2contact = CRM_Contact_DAO_GroupContact::getTableName();
     /* Create a temp table for contact exclusion */
     $mailingGroup->query("CREATE TEMPORARY TABLE X_{$job_id} \n            (contact_id int primary key) \n            ENGINE=HEAP");
     /* Add all the members of groups excluded from this mailing to the temp
      * table */
     $excludeSubGroup = "INSERT INTO        X_{$job_id} (contact_id)\n                    SELECT              {$g2contact}.contact_id\n                    FROM                {$g2contact}\n                    INNER JOIN          {$mg}\n                            ON          {$g2contact}.group_id = {$mg}.entity_id AND {$mg}.entity_table = '{$group}'\n                    WHERE\n                                        {$mg}.mailing_id = {$this->id}\n                        AND             {$g2contact}.status = 'Added'\n                        AND             {$mg}.group_type = 'Exclude'";
     $mailingGroup->query($excludeSubGroup);
     /* Add all the (intended) recipients of an excluded prior mailing to
      * the temp table */
     $excludeSubMailing = "INSERT IGNORE INTO X_{$job_id} (contact_id)\n                    SELECT              {$eq}.contact_id\n                    FROM                {$eq}\n                    INNER JOIN          {$job}\n                            ON          {$eq}.job_id = {$job}.id\n                    INNER JOIN          {$mg}\n                            ON          {$job}.mailing_id = {$mg}.entity_id AND {$mg}.entity_table = '{$mailing}'\n                    WHERE\n                                        {$mg}.mailing_id = {$this->id}\n                        AND             {$mg}.group_type = 'Exclude'";
     $mailingGroup->query($excludeSubMailing);
     /* Add all the succesful deliveries of this mailing (but any job/retry)
      * to the exclude temp table */
     $excludeRetry = "INSERT IGNORE INTO X_{$job_id} (contact_id)\n                    SELECT              {$eq}.contact_id\n                    FROM                {$eq}\n                    INNER JOIN          {$job}\n                            ON          {$eq}.job_id = {$job}.id\n                    INNER JOIN          {$ed}\n                            ON          {$eq}.id = {$ed}.event_queue_id\n                    LEFT JOIN           {$eb}\n                            ON          {$eq}.id = {$eb}.event_queue_id\n                    WHERE\n                                        {$job}.mailing_id = {$this->id}\n                        AND             {$eb}.id IS null";
     $mailingGroup->query($excludeRetry);
     $ss =& new CRM_Core_DAO();
     $ss->query("SELECT             {$group}.saved_search_id as saved_search_id\n                FROM                {$group}\n                INNER JOIN          {$mg}\n                        ON          {$mg}.entity_id = {$group}.id\n                WHERE               {$mg}.entity_table = '{$group}'\n                    AND             {$mg}.group_type = 'Exclude'\n                    AND             {$mg}.mailing_id = {$this->id}\n                    AND             {$group}.saved_search_id IS NOT null");
     $whereTables = array();
     while ($ss->fetch()) {
         /* run the saved search query and dump result contacts into the temp
          * table */
         $tables = array($contact => 1);
         $where = CRM_Contact_BAO_SavedSearch::whereClause($ss->saved_search_id, $tables, $whereTables);
         $from = CRM_Contact_BAO_Query::fromClause($tables);
         $mailingGroup->query("INSERT IGNORE INTO X_{$job_id} (contact_id)\n                    SELECT              {$contact}.id\n                                    {$from}\n                    WHERE               {$where}");
     }
     /* Get all the group contacts we want to include */
     $mailingGroup->query("CREATE TEMPORARY TABLE I_{$job_id} \n            (email_id int, contact_id int primary key)\n            ENGINE=HEAP");
     /* Get the group contacts, but only those which are not in the
      * exclusion temp table */
     /* Get the emails with no override */
     $mailingGroup->query("INSERT INTO        I_{$job_id} (email_id, contact_id)\n                    SELECT DISTINCT     {$email}.id as email_id,\n                                        {$contact}.id as contact_id\n                    FROM                {$email}\n                    INNER JOIN          {$location}\n                            ON          {$email}.location_id = {$location}.id\n                    INNER JOIN          {$contact}\n                            ON          {$location}.entity_id = {$contact}.id\n                                AND     {$location}.entity_table = '{$contact}'\n                    INNER JOIN          {$g2contact}\n                            ON          {$contact}.id = {$g2contact}.contact_id\n                    INNER JOIN          {$mg}\n                            ON          {$g2contact}.group_id = {$mg}.entity_id\n                                AND     {$mg}.entity_table = '{$group}'\n                    LEFT JOIN           X_{$job_id}\n                            ON          {$contact}.id = X_{$job_id}.contact_id\n                    WHERE           \n                                        {$mg}.group_type = 'Include'\n                        AND             {$g2contact}.status = 'Added'\n                        AND             {$g2contact}.location_id IS null\n                        AND             {$g2contact}.email_id IS null\n                        AND             {$contact}.do_not_email = 0\n                        AND             {$contact}.is_opt_out = 0\n                        AND             {$location}.is_primary = 1\n                        AND             {$email}.is_primary = 1\n                        AND             {$email}.on_hold = 0\n                        AND             {$mg}.mailing_id = {$this->id}\n                        AND             X_{$job_id}.contact_id IS null");
     /* Query prior mailings */
     $mailingGroup->query("REPLACE INTO       I_{$job_id} (email_id, contact_id)\n                    SELECT DISTINCT     {$email}.id as email_id,\n                                        {$contact}.id as contact_id\n                    FROM                {$email}\n                    INNER JOIN          {$location}\n                            ON          {$email}.location_id = {$location}.id\n                    INNER JOIN          {$contact}\n                            ON          {$location}.entity_id = {$contact}.id\n                                AND     {$location}.entity_table = '{$contact}'\n                    INNER JOIN          {$eq}\n                            ON          {$eq}.contact_id = {$contact}.id\n                    INNER JOIN          {$job}\n                            ON          {$eq}.job_id = {$job}.id\n                    INNER JOIN          {$mg}\n                            ON          {$job}.mailing_id = {$mg}.entity_id AND {$mg}.entity_table = '{$mailing}'\n                    LEFT JOIN           X_{$job_id}\n                            ON          {$contact}.id = X_{$job_id}.contact_id\n                    WHERE\n                                        {$mg}.group_type = 'Include'\n                        AND             {$contact}.do_not_email = 0\n                        AND             {$contact}.is_opt_out = 0\n                        AND             {$location}.is_primary = 1\n                        AND             {$email}.is_primary = 1\n                        AND             {$email}.on_hold = 0\n                        AND             {$mg}.mailing_id = {$this->id}\n                        AND             X_{$job_id}.contact_id IS null");
     /* Construct the saved-search queries */
     $ss->query("SELECT          {$group}.saved_search_id as saved_search_id\n                    FROM            {$group}\n                    INNER JOIN      {$mg}\n                            ON      {$mg}.entity_id = {$group}.id\n                                AND {$mg}.entity_table = '{$group}'\n                    WHERE               \n                                    {$mg}.group_type = 'Include'\n                        AND         {$mg}.mailing_id = {$this->id}\n                        AND         {$group}.saved_search_id IS NOT null");
     $whereTables = array();
     while ($ss->fetch()) {
         $tables = array($contact => 1, $location => 1, $email => 1);
         $where = CRM_Contact_BAO_SavedSearch::whereClause($ss->saved_search_id, $tables, $whereTables);
         $from = CRM_Contact_BAO_Query::fromClause($tables);
         $ssq = "INSERT IGNORE INTO  I_{$job_id} (email_id, contact_id)\n                    SELECT DISTINCT     {$email}.id as email_id,\n                                        {$contact}.id as contact_id \n                    {$from}\n                    LEFT JOIN           X_{$job_id}\n                            ON          {$contact}.id = X_{$job_id}.contact_id\n                    WHERE           \n                                        {$contact}.do_not_email = 0\n                        AND             {$contact}.is_opt_out = 0\n                        AND             {$location}.is_primary = 1\n                        AND             {$email}.is_primary = 1\n                        AND             {$email}.on_hold = 0\n                        AND             {$where}\n                        AND             X_{$job_id}.contact_id IS null ";
         $mailingGroup->query($ssq);
     }
     /* Get the emails with only location override */
     $mailingGroup->query("REPLACE INTO       I_{$job_id} (email_id, contact_id)\n                    SELECT DISTINCT     {$email}.id as local_email_id,\n                                        {$contact}.id as contact_id\n                    FROM                {$email}\n                    INNER JOIN          {$location}\n                            ON          {$email}.location_id = {$location}.id\n                    INNER JOIN          {$contact}\n                            ON          {$location}.entity_id = {$contact}.id\n                                AND     {$location}.entity_table = '{$contact}'\n                    INNER JOIN          {$g2contact}\n                            ON          {$contact}.id = {$g2contact}.contact_id\n                                AND     {$location}.id = {$g2contact}.location_id\n                    INNER JOIN          {$mg}\n                            ON          {$g2contact}.group_id = {$mg}.entity_id\n                    LEFT JOIN           X_{$job_id}\n                            ON          {$contact}.id = X_{$job_id}.contact_id\n                    WHERE           \n                                        {$mg}.entity_table = '{$group}'\n                        AND             {$mg}.group_type = 'Include'\n                        AND             {$g2contact}.status = 'Added'\n                        AND             {$g2contact}.location_id IS NOT null\n                        AND             {$g2contact}.email_id is null\n                        AND             {$contact}.do_not_email = 0\n                        AND             {$contact}.is_opt_out = 0\n                        AND             {$email}.is_primary = 1\n                        AND             {$email}.on_hold = 0\n                        AND             {$mg}.mailing_id = {$this->id}\n                        AND             X_{$job_id}.contact_id IS null");
     /* Get the emails with full override */
     $mailingGroup->query("REPLACE INTO       I_{$job_id} (email_id, contact_id)\n                    SELECT DISTINCT     {$email}.id as email_id,\n                                        {$contact}.id as contact_id\n                    FROM                {$email}\n                    INNER JOIN          {$g2contact}\n                            ON          {$email}.id = {$g2contact}.email_id\n                    INNER JOIN          {$contact}\n                            ON          {$contact}.id = {$g2contact}.contact_id\n                    INNER JOIN          {$mg}\n                            ON          {$g2contact}.group_id = {$mg}.entity_id\n                    LEFT JOIN           X_{$job_id}\n                            ON          {$contact}.id = X_{$job_id}.contact_id\n                    WHERE           \n                                        {$mg}.entity_table = '{$group}'\n                        AND             {$mg}.group_type = 'Include'\n                        AND             {$g2contact}.status = 'Added'\n                        AND             {$g2contact}.location_id IS NOT null\n                        AND             {$g2contact}.email_id IS NOT null\n                        AND             {$contact}.do_not_email = 0\n                        AND             {$contact}.is_opt_out = 0\n                        AND             {$email}.on_hold = 0\n                        AND             {$mg}.mailing_id = {$this->id}\n                        AND             X_{$job_id}.contact_id IS null");
     $results = array();
     $eq =& new CRM_Mailing_Event_BAO_Queue();
     $eq->query("SELECT contact_id, email_id \n                    FROM I_{$job_id} \n                    ORDER BY contact_id, email_id");
     /* Delete the temp table */
     $mailingGroup->reset();
     $mailingGroup->query("DROP TEMPORARY TABLE X_{$job_id}");
     $mailingGroup->query("DROP TEMPORARY TABLE I_{$job_id}");
     return $eq;
 }