コード例 #1
0
 /**
  * Method to get Group Id.
  *
  * @param int $groupContactID
  *   Id of a perticuler group.
  *
  *
  * @return groupID
  */
 public static function getGroupId($groupContactID)
 {
     $dao = new CRM_Contact_DAO_GroupContact();
     $dao->id = $groupContactID;
     $dao->find(TRUE);
     return $dao->group_id;
 }
コード例 #2
0
ファイル: GroupContact.php プロジェクト: ksecor/civicrm
 /**
  * class constructor
  */
 function __construct()
 {
     parent::__construct();
 }
コード例 #3
0
 /**
  * Construct a WHERE clause to handle permissions to $object_*
  *
  * @param array ref $tables -   Any tables that may be needed in the FROM
  * @param string $operation -   The operation being attempted
  * @param string $object_table -    The table of the object in question
  * @param int $object_id    -   The ID of the object in question
  * @param int $acl_id   -       If it's a grant/revoke operation, the ACL ID
  * @param boolean $acl_role -  For grant operations, this flag determines if we're granting a single acl (false) or an entire group.
  *
  * @return string           -   The WHERE clause, or 0 on failure
  * @access public
  * @static
  */
 public static function permissionClause(&$tables, $operation, $object_table = NULL, $object_id = NULL, $acl_id = NULL, $acl_role = FALSE)
 {
     $dao = new CRM_ACL_DAO_ACL();
     $t = array('ACL' => self::getTableName(), 'ACLRole' => 'civicrm_acl_role', 'ACLEntityRole' => CRM_ACL_DAO_EntityRole::getTableName(), 'Contact' => CRM_Contact_DAO_Contact::getTableName(), 'Group' => CRM_Contact_DAO_Group::getTableName(), 'GroupContact' => CRM_Contact_DAO_GroupContact::getTableName());
     $session = CRM_Core_Session::singleton();
     $contact_id = $session->get('userID');
     $where = " {$t['ACL']}.operation = '" . CRM_Utils_Type::escape($operation, 'String') . "'";
     /* Include clause if we're looking for a specific table/id permission */
     if (!empty($object_table)) {
         $where .= " AND ( {$t['ACL']}.object_table IS null\n                         OR ({$t['ACL']}.object_table   = '" . CRM_Utils_Type::escape($object_table, 'String') . "'";
         if (!empty($object_id)) {
             $where .= " AND ({$t['ACL']}.object_id IS null\n                            OR {$t['ACL']}.object_id = " . CRM_Utils_Type::escape($object_id, 'Integer') . ')';
         }
         $where .= '))';
     }
     /* Include clause if we're granting an ACL or ACL Role */
     if (!empty($acl_id)) {
         $where .= " AND ({$t['ACL']}.acl_id IS null\n                        OR {$t['ACL']}.acl_id   = " . CRM_Utils_Type::escape($acl_id, 'Integer') . ')';
         if ($acl_role) {
             $where .= " AND {$t['ACL']}.acl_table = '{$t['ACLRole']}'";
         } else {
             $where .= " AND {$t['ACL']}.acl_table = '{$t['ACL']}'";
         }
     }
     $query = array();
     /* Query for permissions granted to all contacts in the domain */
     $query[] = "SELECT      {$t['ACL']}.*, 0 as override\n                    FROM        {$t['ACL']}\n\n                    WHERE       {$t['ACL']}.entity_table    = '{$t['Domain']}'\n                            AND ({$where})";
     /* Query for permissions granted to all contacts through an ACL group */
     $query[] = "SELECT      {$t['ACL']}.*, 0 as override\n                    FROM        {$t['ACL']}\n\n                    INNER JOIN  {$t['ACLEntityRole']}\n                            ON  ({$t['ACL']}.entity_table = '{$t['ACLRole']}'\n                            AND     {$t['ACL']}.entity_id =\n                                    {$t['ACLEntityRole']}.acl_role_id)\n\n                    INNER JOIN  {$t['ACLRole']}\n                            ON      {$t['ACL']}.entity_id =\n                                    {$t['ACLRole']}.id\n\n                    WHERE       {$t['ACLEntityRole']}.entity_table =\n                                    '{$t['Domain']}'\n                            AND {$t['ACLRole']}.is_active      = 1\n                            AND ({$where})";
     /* Query for permissions granted directly to the contact */
     $query[] = "SELECT      {$t['ACL']}.*, 1 as override\n                    FROM        {$t['ACL']}\n\n                    INNER JOIN  {$t['Contact']}\n                            ON  ({$t['ACL']}.entity_table = '{$t['Contact']}'\n                            AND     {$t['ACL']}.entity_id = {$t['Contact']}.id)\n\n                    WHERE       {$t['Contact']}.id          = {$contact_id}\n                            AND ({$where})";
     /* Query for permissions granted to the contact through an ACL group */
     $query[] = "SELECT      {$t['ACL']}.*, 1 as override\n                    FROM        {$t['ACL']}\n\n                    INNER JOIN  {$t['ACLEntityRole']}\n                            ON  ({$t['ACL']}.entity_table = '{$t['ACLRole']}'\n                            AND     {$t['ACL']}.entity_id =\n                                    {$t['ACLEntityRole']}.acl_role_id)\n\n                    INNER JOIN  {$t['ACLRole']}\n                            ON  {$t['ACL']}.entity_id = {$t['ACLRole']}.id\n\n                    WHERE       {$t['ACLEntityRole']}.entity_table =\n                                    '{$t['Contact']}'\n                        AND     {$t['ACLRole']}.is_active      = 1\n                        AND     {$t['ACLEntityRole']}.entity_id  = {$contact_id}\n                        AND     ({$where})";
     /* Query for permissions granted to the contact through a group */
     $query[] = "SELECT      {$t['ACL']}.*, 0 as override\n                    FROM        {$t['ACL']}\n\n                    INNER JOIN  {$t['GroupContact']}\n                            ON  ({$t['ACL']}.entity_table = '{$t['Group']}'\n                            AND     {$t['ACL']}.entity_id =\n                                    {$t['GroupContact']}.group_id)\n\n                    WHERE       ({$where})\n                        AND     {$t['GroupContact']}.contact_id = {$contact_id}\n                        AND     {$t['GroupContact']}.status     = 'Added')";
     /* Query for permissions granted through an ACL group to a Contact
      * group */
     $query[] = "SELECT      {$t['ACL']}.*, 0 as override\n                    FROM        {$t['ACL']}\n\n                    INNER JOIN  {$t['ACLEntityRole']}\n                            ON  ({$t['ACL']}.entity_table = '{$t['ACLRole']}'\n                            AND     {$t['ACL']}.entity_id =\n                                    {$t['ACLEntityRole']}.acl_role_id)\n\n                    INNER JOIN  {$t['ACLRole']}\n                            ON  {$t['ACL']}.entity_id = {$t['ACLRole']}.id\n\n                    INNER JOIN  {$t['GroupContact']}\n                            ON  ({$t['ACLEntityRole']}.entity_table =\n                                    '{$t['Group']}'\n                            AND     {$t['ACLEntityRole']}.entity_id =\n                                    {$t['GroupContact']}.group_id)\n\n                    WHERE       ({$where})\n                        AND     {$t['ACLRole']}.is_active      = 1\n                        AND     {$t['GroupContact']}.contact_id = {$contact_id}\n                        AND     {$t['GroupContact']}.status     = 'Added'";
     $union = '(' . implode(') UNION DISTINCT (', $query) . ')';
     $dao->query($union);
     $allow = array(0);
     $deny = array(0);
     $override = array();
     while ($dao->fetch()) {
         /* Instant bypass for the following cases:
          * 1) the rule governs all tables
          * 2) the rule governs all objects in the table in question
          * 3) the rule governs the specific object we want
          */
         if (empty($dao->object_table) || $dao->object_table == $object_table && (empty($dao->object_id) || $dao->object_id == $object_id)) {
             $clause = 1;
         } else {
             /* Otherwise try to generate a clause for this rule */
             $clause = self::getClause($dao->object_table, $dao->object_id, $tables);
             /* If the clause returned is null, then the rule is a blanket
              * (id is null) on a table other than the one we're interested
              * in.  So skip it. */
             if (empty($clause)) {
                 continue;
             }
         }
         /* Now we figure out if this is an allow or deny rule, and possibly
          * a contact-level override */
         if ($dao->deny) {
             $deny[] = $clause;
         } else {
             $allow[] = $clause;
             if ($dao->override) {
                 $override[] = $clause;
             }
         }
     }
     $allows = '(' . implode(' OR ', $allow) . ')';
     $denies = '(' . implode(' OR ', $deny) . ')';
     if (!empty($override)) {
         $denies = '(NOT (' . implode(' OR ', $override) . ") AND {$denies})";
     }
     return "({$allows} AND NOT {$denies})";
 }
コード例 #4
0
ファイル: Form.php プロジェクト: hyebahi/civicrm-core
 /**
  * Process the user submitted custom data values.
  *
  *
  * @return void
  */
 public function postProcess()
 {
     $params = $this->controller->exportValues($this->_name);
     //if the delete record button is clicked
     if ($this->_deleteButtonName) {
         if (!empty($_POST[$this->_deleteButtonName]) && $this->_recordId) {
             $filterParams['id'] = $this->_customGroupId;
             $returnProperties = array('is_multiple', 'table_name');
             CRM_Core_DAO::commonRetrieve("CRM_Core_DAO_CustomGroup", $filterParams, $returnValues, $returnProperties);
             if (!empty($returnValues['is_multiple'])) {
                 if ($tableName = CRM_Utils_Array::value('table_name', $returnValues)) {
                     $sql = "DELETE FROM {$tableName} WHERE id = %1 AND entity_id = %2";
                     $sqlParams = array(1 => array($this->_recordId, 'Integer'), 2 => array($this->_id, 'Integer'));
                     CRM_Core_DAO::executeQuery($sql, $sqlParams);
                     CRM_Core_Session::setStatus(ts('Your record has been deleted.'), ts('Deleted'), 'success');
                 }
             }
             return;
         }
     }
     CRM_Utils_Hook::processProfile($this->_ufGroup['name']);
     if (!empty($params['image_URL'])) {
         CRM_Contact_BAO_Contact::processImageParams($params);
     }
     $greetingTypes = array('addressee' => 'addressee_id', 'email_greeting' => 'email_greeting_id', 'postal_greeting' => 'postal_greeting_id');
     $details = array();
     if ($this->_id) {
         $contactDetails = CRM_Contact_BAO_Contact::getHierContactDetails($this->_id, $greetingTypes);
         $details = $contactDetails[0][$this->_id];
     }
     if (!(!empty($details['addressee_id']) || !empty($details['email_greeting_id']) || CRM_Utils_Array::value('postal_greeting_id', $details))) {
         $profileType = CRM_Core_BAO_UFField::getProfileType($this->_gid);
         //Though Profile type is contact we need
         //Individual/Household/Organization for setting Greetings.
         if ($profileType == 'Contact') {
             $profileType = 'Individual';
             //if we editing Household/Organization.
             if ($this->_id) {
                 $profileType = CRM_Contact_BAO_Contact::getContactType($this->_id);
             }
         }
         if (CRM_Contact_BAO_ContactType::isaSubType($profileType)) {
             $profileType = CRM_Contact_BAO_ContactType::getBasicType($profileType);
         }
         foreach ($greetingTypes as $key => $value) {
             if (!array_key_exists($key, $params)) {
                 $params[$key] = CRM_Contact_BAO_Contact_Utils::defaultGreeting($profileType, $key);
             }
         }
     }
     $transaction = new CRM_Core_Transaction();
     //used to send subscribe mail to the group which user want.
     //if the profile double option in is enabled
     $mailingType = array();
     $result = NULL;
     foreach ($params as $name => $values) {
         if (substr($name, 0, 6) == 'email-') {
             $result['email'] = $values;
         }
     }
     //array of group id, subscribed by contact
     $contactGroup = array();
     if (!empty($params['group']) && CRM_Core_BAO_UFGroup::isProfileDoubleOptin()) {
         $groupSubscribed = array();
         if (!empty($result['email'])) {
             if ($this->_id) {
                 $contactGroups = new CRM_Contact_DAO_GroupContact();
                 $contactGroups->contact_id = $this->_id;
                 $contactGroups->status = 'Added';
                 $contactGroups->find();
                 $contactGroup = array();
                 while ($contactGroups->fetch()) {
                     $contactGroup[] = $contactGroups->group_id;
                     $groupSubscribed[$contactGroups->group_id] = 1;
                 }
             }
             foreach ($params['group'] as $key => $val) {
                 if (!$val) {
                     unset($params['group'][$key]);
                     continue;
                 }
                 $groupTypes = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $key, 'group_type', 'id');
                 $groupType = explode(CRM_Core_DAO::VALUE_SEPARATOR, substr($groupTypes, 1, -1));
                 //filter group of mailing type and unset it from params
                 if (in_array(2, $groupType)) {
                     //if group is already subscribed , ignore it
                     $groupExist = CRM_Utils_Array::key($key, $contactGroup);
                     if (!isset($groupExist)) {
                         $mailingType[] = $key;
                         unset($params['group'][$key]);
                     }
                 }
             }
         }
     }
     $addToGroupId = CRM_Utils_Array::value('add_to_group_id', $this->_ufGroup);
     if (!empty($addToGroupId)) {
         //run same check whether group is a mailing list
         $groupTypes = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $addToGroupId, 'group_type', 'id');
         $groupType = explode(CRM_Core_DAO::VALUE_SEPARATOR, substr($groupTypes, 1, -1));
         //filter group of mailing type and unset it from params
         if (in_array(2, $groupType) && !empty($result['email']) && CRM_Core_BAO_UFGroup::isProfileAddToGroupDoubleOptin()) {
             if (!count($contactGroup)) {
                 //array of group id, subscribed by contact
                 $contactGroup = array();
                 if ($this->_id) {
                     $contactGroups = new CRM_Contact_DAO_GroupContact();
                     $contactGroups->contact_id = $this->_id;
                     $contactGroups->status = 'Added';
                     $contactGroups->find();
                     $contactGroup = array();
                     while ($contactGroups->fetch()) {
                         $contactGroup[] = $contactGroups->group_id;
                         $groupSubscribed[$contactGroups->group_id] = 1;
                     }
                 }
             }
             //if group is already subscribed , ignore it
             $groupExist = CRM_Utils_Array::key($addToGroupId, $contactGroup);
             if (!isset($groupExist)) {
                 $mailingType[] = $addToGroupId;
                 $addToGroupId = NULL;
             }
         } else {
             // since we are directly adding contact to group lets unset it from mailing
             if ($key = array_search($addToGroupId, $mailingType)) {
                 unset($mailingType[$key]);
             }
         }
     }
     if ($this->_grid) {
         $params['group'] = $groupSubscribed;
     }
     // commenting below code, since we potentially
     // triggered maximum name field formatting cases during CRM-4430.
     // CRM-4343
     // $params['preserveDBName'] = true;
     $profileFields = $this->_fields;
     if ($this->_mode & self::MODE_EDIT && $this->_activityId && $this->_isContactActivityProfile) {
         $profileFields = $activityParams = array();
         foreach ($this->_fields as $fieldName => $field) {
             if (CRM_Utils_Array::value('field_type', $field) == 'Activity') {
                 if (isset($params[$fieldName])) {
                     $activityParams[$fieldName] = $params[$fieldName];
                 }
                 if (isset($params['activity_date_time'])) {
                     $activityParams['activity_date_time'] = CRM_Utils_Date::processDate($params['activity_date_time'], $params['activity_date_time_time']);
                 }
                 if (!empty($params[$fieldName]) && isset($params["{$fieldName}_id"])) {
                     $activityParams[$fieldName] = $params["{$fieldName}_id"];
                 }
             } else {
                 $profileFields[$fieldName] = $field;
             }
         }
         if (!empty($activityParams)) {
             $activityParams['version'] = 3;
             $activityParams['id'] = $this->_activityId;
             $activityParams['skipRecentView'] = TRUE;
             civicrm_api('Activity', 'create', $activityParams);
         }
     }
     if ($this->_multiRecord && $this->_recordId && $this->_multiRecordFields && $this->_recordExists) {
         $params['customRecordValues'][$this->_recordId] = array_keys($this->_multiRecordFields);
     }
     $this->_id = CRM_Contact_BAO_Contact::createProfileContact($params, $profileFields, $this->_id, $addToGroupId, $this->_gid, $this->_ctype, TRUE);
     //mailing type group
     if (!empty($mailingType)) {
         // we send in the contactID so we match the same groups and are exact, rather than relying on email
         // CRM-8710
         CRM_Mailing_Event_BAO_Subscribe::commonSubscribe($mailingType, $result, $this->_id, 'profile');
     }
     $ufGroups = array();
     if ($this->_gid) {
         $ufGroups[$this->_gid] = 1;
     } elseif ($this->_mode == self::MODE_REGISTER) {
         $ufGroups = CRM_Core_BAO_UFGroup::getModuleUFGroup('User Registration');
     }
     foreach ($ufGroups as $gId => $val) {
         if ($notify = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $gId, 'notify')) {
             $values = CRM_Core_BAO_UFGroup::checkFieldsEmptyValues($gId, $this->_id, NULL);
             CRM_Core_BAO_UFGroup::commonSendMail($this->_id, $values);
         }
     }
     //create CMS user (if CMS user option is selected in profile)
     if (!empty($params['cms_create_account']) && $this->_mode == self::MODE_CREATE) {
         $params['contactID'] = $this->_id;
         if (!CRM_Core_BAO_CMSUser::create($params, $this->_mail)) {
             CRM_Core_Session::setStatus(ts('Your profile is not saved and Account is not created.'), ts('Profile Error'), 'error');
             CRM_Core_Error::debug_log_message("Rolling back transaction as CMSUser Create failed in Profile_Form for contact " . $params['contactID']);
             $transaction->rollback();
             return CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/profile/create', 'reset=1&gid=' . $this->_gid));
         }
     }
     $transaction->commit();
 }
コード例 #5
0
 /**
  * note that $job_id is used only as a variable in the temp table construction
  * and does not play a role in the queries generated
  * @param int $job_id
  *   (misnomer) a nonce value used to name temporary tables.
  * @param int $mailing_id
  * @param bool $storeRecipients
  * @param bool $dedupeEmail
  * @param null $mode
  *
  * @return CRM_Mailing_Event_BAO_Queue|string
  */
 public static function getRecipients($job_id, $mailing_id = NULL, $storeRecipients = FALSE, $dedupeEmail = FALSE, $mode = NULL)
 {
     $mailingGroup = new CRM_Mailing_DAO_MailingGroup();
     $mailing = CRM_Mailing_BAO_Mailing::getTableName();
     $job = CRM_Mailing_BAO_MailingJob::getTableName();
     $mg = CRM_Mailing_DAO_MailingGroup::getTableName();
     $eq = CRM_Mailing_Event_DAO_Queue::getTableName();
     $email = CRM_Core_DAO_Email::getTableName();
     if ($mode == 'sms') {
         $phone = CRM_Core_DAO_Phone::getTableName();
     }
     $contact = CRM_Contact_DAO_Contact::getTableName();
     $group = CRM_Contact_DAO_Group::getTableName();
     $g2contact = CRM_Contact_DAO_GroupContact::getTableName();
     $m = new CRM_Mailing_DAO_Mailing();
     $m->id = $mailing_id;
     $m->find(TRUE);
     $email_selection_method = $m->email_selection_method;
     $location_type_id = $m->location_type_id;
     // Note: When determining the ORDER that results are returned, it's
     // the record that comes last that counts. That's because we are
     // INSERT'ing INTO a table with a primary id so that last record
     // over writes any previous record.
     switch ($email_selection_method) {
         case 'location-exclude':
             $location_filter = "({$email}.location_type_id != {$location_type_id})";
             // If there is more than one email that doesn't match the location,
             // prefer the one marked is_bulkmail, followed by is_primary.
             $order_by = "ORDER BY {$email}.is_bulkmail, {$email}.is_primary";
             break;
         case 'location-only':
             $location_filter = "({$email}.location_type_id = {$location_type_id})";
             // If there is more than one email of the desired location, prefer
             // the one marked is_bulkmail, followed by is_primary.
             $order_by = "ORDER BY {$email}.is_bulkmail, {$email}.is_primary";
             break;
         case 'location-prefer':
             $location_filter = "({$email}.is_bulkmail = 1 OR {$email}.is_primary = 1 OR {$email}.location_type_id = {$location_type_id})";
             // ORDER BY is more complicated because we have to set an arbitrary
             // order that prefers the location that we want. We do that using
             // the FIELD function. For more info, see:
             // https://dev.mysql.com/doc/refman/5.5/en/string-functions.html#function_field
             // We assign the location type we want the value "1" by putting it
             // in the first position after we name the field. All other location
             // types are left out, so they will be assigned the value 0. That
             // means, they will all be equally tied for first place, with our
             // location being last.
             $order_by = "ORDER BY FIELD({$email}.location_type_id, {$location_type_id}), {$email}.is_bulkmail, {$email}.is_primary";
             break;
         case 'automatic':
             // fall through to default
         // fall through to default
         default:
             $location_filter = "({$email}.is_bulkmail = 1 OR {$email}.is_primary = 1)";
             $order_by = "ORDER BY {$email}.is_bulkmail";
     }
     /* 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  DISTINCT    {$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 = {$mailing_id}\n                        AND             {$g2contact}.status = 'Added'\n                        AND             {$mg}.group_type = 'Exclude'";
     $mailingGroup->query($excludeSubGroup);
     /* Add all unsubscribe members of base group from this mailing to the temp
      * table */
     $unSubscribeBaseGroup = "INSERT INTO        X_{$job_id} (contact_id)\n                    SELECT  DISTINCT    {$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 = {$mailing_id}\n                        AND             {$g2contact}.status = 'Removed'\n                        AND             {$mg}.group_type = 'Base'";
     $mailingGroup->query($unSubscribeBaseGroup);
     /* 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  DISTINCT    {$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 = {$mailing_id}\n                        AND             {$mg}.group_type = 'Exclude'";
     $mailingGroup->query($excludeSubMailing);
     // get all the saved searches AND hierarchical groups
     // and load them in the cache
     $sql = "\nSELECT     {$group}.id, {$group}.cache_date, {$group}.saved_search_id, {$group}.children\nFROM       {$group}\nINNER JOIN {$mg} ON {$mg}.entity_id = {$group}.id\nWHERE      {$mg}.entity_table = '{$group}'\n  AND      {$mg}.group_type = 'Exclude'\n  AND      {$mg}.mailing_id = {$mailing_id}\n  AND      ( saved_search_id != 0\n   OR        saved_search_id IS NOT NULL\n   OR        children IS NOT NULL )\n";
     $groupDAO = CRM_Core_DAO::executeQuery($sql);
     while ($groupDAO->fetch()) {
         if ($groupDAO->cache_date == NULL) {
             CRM_Contact_BAO_GroupContactCache::load($groupDAO);
         }
         $smartGroupExclude = "\nINSERT IGNORE INTO X_{$job_id} (contact_id)\nSELECT c.contact_id\nFROM   civicrm_group_contact_cache c\nWHERE  c.group_id = {$groupDAO->id}\n";
         $mailingGroup->query($smartGroupExclude);
     }
     $tempColumn = 'email_id';
     if ($mode == 'sms') {
         $tempColumn = 'phone_id';
     }
     /* Get all the group contacts we want to include */
     $mailingGroup->query("CREATE TEMPORARY TABLE I_{$job_id}\n            ({$tempColumn} int, contact_id int primary key)\n            ENGINE=HEAP");
     /* Get the group contacts, but only those which are not in the
      * exclusion temp table */
     $query = "REPLACE INTO       I_{$job_id} (email_id, contact_id)\n\n                    SELECT DISTINCT     {$email}.id as email_id,\n                                        {$contact}.id as contact_id\n                    FROM                {$email}\n                    INNER JOIN          {$contact}\n                            ON          {$email}.contact_id = {$contact}.id\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             {$mg}.search_id IS NULL\n                        AND             {$g2contact}.status = 'Added'\n                        AND             {$contact}.do_not_email = 0\n                        AND             {$contact}.is_opt_out = 0\n                        AND             {$contact}.is_deceased <> 1\n                        AND             {$location_filter}\n                        AND             {$email}.email IS NOT NULL\n                        AND             {$email}.email != ''\n                        AND             {$email}.on_hold = 0\n                        AND             {$mg}.mailing_id = {$mailing_id}\n                        AND             X_{$job_id}.contact_id IS null\n                    {$order_by}";
     if ($mode == 'sms') {
         $phoneTypes = CRM_Core_OptionGroup::values('phone_type', TRUE, FALSE, FALSE, NULL, 'name');
         $query = "REPLACE INTO       I_{$job_id} (phone_id, contact_id)\n\n                    SELECT DISTINCT     {$phone}.id as phone_id,\n                                        {$contact}.id as contact_id\n                    FROM                {$phone}\n                    INNER JOIN          {$contact}\n                            ON          {$phone}.contact_id = {$contact}.id\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             {$mg}.search_id IS NULL\n                        AND             {$g2contact}.status = 'Added'\n                        AND             {$contact}.do_not_sms = 0\n                        AND             {$contact}.is_opt_out = 0\n                        AND             {$contact}.is_deceased <> 1\n                        AND             {$phone}.phone_type_id = {$phoneTypes['Mobile']}\n                        AND             {$phone}.phone IS NOT NULL\n                        AND             {$phone}.phone != ''\n                        AND             {$mg}.mailing_id = {$mailing_id}\n                        AND             X_{$job_id}.contact_id IS null";
     }
     $mailingGroup->query($query);
     /* Query prior mailings */
     $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          {$contact}\n                            ON          {$email}.contact_id = {$contact}.id\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             {$contact}.is_deceased <> 1\n                        AND             {$location_filter}\n                        AND             {$email}.on_hold = 0\n                        AND             {$mg}.mailing_id = {$mailing_id}\n                        AND             X_{$job_id}.contact_id IS null\n                    {$order_by}";
     if ($mode == 'sms') {
         $query = "REPLACE INTO       I_{$job_id} (phone_id, contact_id)\n                    SELECT DISTINCT     {$phone}.id as phone_id,\n                                        {$contact}.id as contact_id\n                    FROM                {$phone}\n                    INNER JOIN          {$contact}\n                            ON          {$phone}.contact_id = {$contact}.id\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_sms = 0\n                        AND             {$contact}.is_opt_out = 0\n                        AND             {$contact}.is_deceased <> 1\n                        AND             {$phone}.phone_type_id = {$phoneTypes['Mobile']}\n                        AND             {$mg}.mailing_id = {$mailing_id}\n                        AND             X_{$job_id}.contact_id IS null";
     }
     $mailingGroup->query($query);
     $sql = "\nSELECT     {$group}.id, {$group}.cache_date, {$group}.saved_search_id, {$group}.children\nFROM       {$group}\nINNER JOIN {$mg} ON {$mg}.entity_id = {$group}.id\nWHERE      {$mg}.entity_table = '{$group}'\n  AND      {$mg}.group_type = 'Include'\n  AND      {$mg}.search_id IS NULL\n  AND      {$mg}.mailing_id = {$mailing_id}\n  AND      ( saved_search_id != 0\n   OR        saved_search_id IS NOT NULL\n   OR        children IS NOT NULL )\n";
     $groupDAO = CRM_Core_DAO::executeQuery($sql);
     while ($groupDAO->fetch()) {
         if ($groupDAO->cache_date == NULL) {
             CRM_Contact_BAO_GroupContactCache::load($groupDAO);
         }
         $smartGroupInclude = "\nREPLACE INTO I_{$job_id} (email_id, contact_id)\nSELECT     civicrm_email.id as email_id, c.id as contact_id\nFROM       civicrm_contact c\nINNER JOIN civicrm_email                ON civicrm_email.contact_id         = c.id\nINNER JOIN civicrm_group_contact_cache gc ON gc.contact_id        = c.id\nLEFT  JOIN X_{$job_id}                      ON X_{$job_id}.contact_id = c.id\nWHERE      gc.group_id = {$groupDAO->id}\n  AND      c.do_not_email = 0\n  AND      c.is_opt_out = 0\n  AND      c.is_deceased <> 1\n  AND      {$location_filter}\n  AND      civicrm_email.on_hold = 0\n  AND      X_{$job_id}.contact_id IS null\n{$order_by}\n";
         if ($mode == 'sms') {
             $smartGroupInclude = "\nREPLACE INTO I_{$job_id} (phone_id, contact_id)\nSELECT     p.id as phone_id, c.id as contact_id\nFROM       civicrm_contact c\nINNER JOIN civicrm_phone p                ON p.contact_id         = c.id\nINNER JOIN civicrm_group_contact_cache gc ON gc.contact_id        = c.id\nLEFT  JOIN X_{$job_id}                      ON X_{$job_id}.contact_id = c.id\nWHERE      gc.group_id = {$groupDAO->id}\n  AND      c.do_not_sms = 0\n  AND      c.is_opt_out = 0\n  AND      c.is_deceased <> 1\n  AND      p.phone_type_id = {$phoneTypes['Mobile']}\n  AND      X_{$job_id}.contact_id IS null";
         }
         $mailingGroup->query($smartGroupInclude);
     }
     /**
      * Construct the filtered search queries
      */
     $query = "\nSELECT search_id, search_args, entity_id\nFROM   {$mg}\nWHERE  {$mg}.search_id IS NOT NULL\nAND    {$mg}.mailing_id = {$mailing_id}\n";
     $dao = CRM_Core_DAO::executeQuery($query);
     while ($dao->fetch()) {
         $customSQL = CRM_Contact_BAO_SearchCustom::civiMailSQL($dao->search_id, $dao->search_args, $dao->entity_id);
         $query = "REPLACE INTO       I_{$job_id} ({$tempColumn}, contact_id)\n                         {$customSQL}";
         $mailingGroup->query($query);
     }
     /* Get the emails with only location override */
     $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          {$contact}\n                            ON          {$email}.contact_id = {$contact}.id\n                    INNER JOIN          {$g2contact}\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             {$contact}.do_not_email = 0\n                        AND             {$contact}.is_opt_out = 0\n                        AND             {$contact}.is_deceased <> 1\n                        AND             {$location_filter}\n                        AND             {$email}.on_hold = 0\n                        AND             {$mg}.mailing_id = {$mailing_id}\n                        AND             X_{$job_id}.contact_id IS null\n                    {$order_by}";
     if ($mode == "sms") {
         $query = "REPLACE INTO       I_{$job_id} (phone_id, contact_id)\n                    SELECT DISTINCT     {$phone}.id as phone_id,\n                                        {$contact}.id as contact_id\n                    FROM                {$phone}\n                    INNER JOIN          {$contact}\n                            ON          {$phone}.contact_id = {$contact}.id\n                    INNER JOIN          {$g2contact}\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             {$contact}.do_not_sms = 0\n                        AND             {$contact}.is_opt_out = 0\n                        AND             {$contact}.is_deceased <> 1\n                        AND             {$phone}.phone_type_id = {$phoneTypes['Mobile']}\n                        AND             {$mg}.mailing_id = {$mailing_id}\n                        AND             X_{$job_id}.contact_id IS null";
     }
     $mailingGroup->query($query);
     $eq = new CRM_Mailing_Event_BAO_Queue();
     list($aclFrom, $aclWhere) = CRM_Contact_BAO_Contact_Permission::cacheClause();
     $aclWhere = $aclWhere ? "WHERE {$aclWhere}" : '';
     if ($storeRecipients && $mailing_id) {
         $sql = "\nDELETE\nFROM   civicrm_mailing_recipients\nWHERE  mailing_id = %1\n";
         $params = array(1 => array($mailing_id, 'Integer'));
         CRM_Core_DAO::executeQuery($sql, $params);
         // CRM-3975
         $groupBy = $groupJoin = '';
         if ($dedupeEmail) {
             $groupJoin = " INNER JOIN civicrm_email e ON e.id = i.email_id";
             $groupBy = " GROUP BY e.email ";
         }
         $sql = "\nINSERT INTO civicrm_mailing_recipients ( mailing_id, contact_id, {$tempColumn} )\nSELECT %1, i.contact_id, i.{$tempColumn}\nFROM       civicrm_contact contact_a\nINNER JOIN I_{$job_id} i ON contact_a.id = i.contact_id\n           {$groupJoin}\n           {$aclFrom}\n           {$aclWhere}\n           {$groupBy}\nORDER BY   i.contact_id, i.{$tempColumn}\n";
         CRM_Core_DAO::executeQuery($sql, $params);
         // if we need to add all emails marked bulk, do it as a post filter
         // on the mailing recipients table
         if (CRM_Core_BAO_Email::isMultipleBulkMail()) {
             self::addMultipleEmails($mailing_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;
 }
コード例 #6
0
 /**
  * class constructor
  */
 function CRM_Contact_BAO_GroupContact()
 {
     parent::CRM_Contact_DAO_GroupContact();
 }
コード例 #7
0
ファイル: Group.php プロジェクト: rameshrr99/civicrm-core
 /**
  * Get the count of a members in a group with the specific status.
  *
  * @param int $id
  *   Group id.
  * @param string $status
  *   status of members in group
  * @param bool $countChildGroups
  *
  * @return int
  *   count of members in the group with above status
  */
 public static function memberCount($id, $status = 'Added', $countChildGroups = FALSE)
 {
     $groupContact = new CRM_Contact_DAO_GroupContact();
     $groupIds = array($id);
     if ($countChildGroups) {
         $groupIds = CRM_Contact_BAO_GroupNesting::getDescendentGroupIds($groupIds);
     }
     $count = 0;
     $contacts = self::getGroupContacts($id);
     foreach ($groupIds as $groupId) {
         $groupContacts = self::getGroupContacts($groupId);
         foreach ($groupContacts as $gcontact) {
             if ($groupId != $id) {
                 // Loop through main group's contacts
                 // and subtract from the count for each contact which
                 // matches one in the present group, if it is not the
                 // main group
                 foreach ($contacts as $contact) {
                     if ($contact['contact_id'] == $gcontact['contact_id']) {
                         $count--;
                     }
                 }
             }
         }
         $groupContact->group_id = $groupId;
         if (isset($status)) {
             $groupContact->status = $status;
         }
         $groupContact->_query['condition'] = 'WHERE contact_id NOT IN (SELECT id FROM civicrm_contact WHERE is_deleted = 1)';
         $count += $groupContact->count();
     }
     return $count;
 }
コード例 #8
0
ファイル: Form.php プロジェクト: ksecor/civicrm
 /**
  * Process the user submitted custom data values.
  *
  * @access public
  * @return void
  */
 public function postProcess()
 {
     $params = $this->controller->exportValues($this->_name);
     if ($this->_mode == self::MODE_REGISTER) {
         require_once 'CRM/Core/BAO/Address.php';
         CRM_Core_BAO_Address::setOverwrite(false);
     }
     require_once 'CRM/Core/Transaction.php';
     $transaction = new CRM_Core_Transaction();
     //used to send subcribe mail to the group which user want.
     //if the profile double option in is enabled
     $mailingType = array();
     $config =& CRM_Core_Config::singleton();
     if ($config->profileDoubleOptIn && CRM_Utils_Array::value('group', $params)) {
         $result = null;
         foreach ($params as $name => $values) {
             if (substr($name, 0, 6) == 'email-') {
                 $result['email'] = $values;
             }
         }
         $groupSubscribed = array();
         if (CRM_Utils_Array::value('email', $result)) {
             require_once 'CRM/Contact/DAO/Group.php';
             //array of group id, subscribed by contact
             $contactGroup = array();
             if ($this->_id) {
                 $contactGroups = new CRM_Contact_DAO_GroupContact();
                 $contactGroups->contact_id = $this->_id;
                 $contactGroups->status = 'Added';
                 $contactGroups->find();
                 $contactGroup = array();
                 while ($contactGroups->fetch()) {
                     $contactGroup[] = $contactGroups->group_id;
                     $groupSubscribed[$contactGroups->group_id] = 1;
                 }
             }
             foreach ($params['group'] as $key => $val) {
                 if (!$val) {
                     unset($params['group'][$key]);
                     continue;
                 }
                 $groupTypes = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $key, 'group_type', 'id');
                 $groupType = explode(CRM_Core_BAO_CustomOption::VALUE_SEPERATOR, substr($groupTypes, 1, -1));
                 //filter group of mailing type and unset it from params
                 if (in_array(2, $groupType)) {
                     //if group is already subscribed , ignore it
                     $groupExist = CRM_Utils_Array::key($key, $contactGroup);
                     if (!isset($groupExist)) {
                         $mailingType[] = $key;
                         unset($params['group'][$key]);
                     }
                 }
             }
         }
     }
     if ($this->_grid) {
         $params['group'] = $groupSubscribed;
     }
     // commenting below code, since we potentially
     // triggered maximum name field formatting cases during CRM-4430.
     // CRM-4343
     // $params['preserveDBName'] = true;
     $this->_id = CRM_Contact_BAO_Contact::createProfileContact($params, $this->_fields, $this->_id, $this->_addToGroupID, $this->_gid, $this->_ctype, true);
     //mailing type group
     if (!empty($mailingType)) {
         require_once 'CRM/Mailing/Event/BAO/Subscribe.php';
         CRM_Mailing_Event_BAO_Subscribe::commonSubscribe($mailingType, $result);
     }
     require_once 'CRM/Core/BAO/UFGroup.php';
     $ufGroups = array();
     if ($this->_gid) {
         $ufGroups[$this->_gid] = 1;
     } else {
         if ($this->_mode == self::MODE_REGISTER) {
             $ufGroups =& CRM_Core_BAO_UFGroup::getModuleUFGroup('User Registration');
         }
     }
     foreach ($ufGroups as $gId => $val) {
         if ($notify = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $gId, 'notify')) {
             $values = CRM_Core_BAO_UFGroup::checkFieldsEmptyValues($gId, $this->_id, null);
             CRM_Core_BAO_UFGroup::commonSendMail($this->_id, $values);
         }
     }
     //create CMS user (if CMS user option is selected in profile)
     if (CRM_Utils_Array::value('cms_create_account', $params) && $this->_mode == self::MODE_CREATE) {
         $params['contactID'] = $this->_id;
         require_once "CRM/Core/BAO/CMSUser.php";
         if (!CRM_Core_BAO_CMSUser::create($params, $this->_mail)) {
             CRM_Core_Session::setStatus(ts('Your profile is not saved and Account is not created.'));
             $transaction->rollback();
             return CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/profile/create', 'reset=1&gid=' . $this->_gid));
         }
     }
     $transaction->commit();
 }
コード例 #9
0
 static function &getRecipients($job_id, $mailing_id = NULL, $offset = NULL, $limit = NULL, $storeRecipients = FALSE, $dedupeEmail = FALSE, $mode = NULL)
 {
     $mailingGroup = new CRM_Mailing_DAO_MailingGroup();
     $mailing = CRM_Mailing_BAO_Mailing::getTableName();
     $job = CRM_Mailing_BAO_MailingJob::getTableName();
     $mg = CRM_Mailing_DAO_MailingGroup::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();
     if ($mode == 'sms') {
         $phone = CRM_Core_DAO_Phone::getTableName();
     }
     $contact = CRM_Contact_DAO_Contact::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  DISTINCT    {$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 = {$mailing_id}\n                        AND             {$g2contact}.status = 'Added'\n                        AND             {$mg}.group_type = 'Exclude'";
     $mailingGroup->query($excludeSubGroup);
     /* Add all unsubscribe members of base group from this mailing to the temp
      * table */
     $unSubscribeBaseGroup = "INSERT INTO        X_{$job_id} (contact_id)\n                    SELECT  DISTINCT    {$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 = {$mailing_id}\n                        AND             {$g2contact}.status = 'Removed'\n                        AND             {$mg}.group_type = 'Base'";
     $mailingGroup->query($unSubscribeBaseGroup);
     /* 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  DISTINCT    {$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 = {$mailing_id}\n                        AND             {$mg}.group_type = 'Exclude'";
     $mailingGroup->query($excludeSubMailing);
     // get all the saved searches AND hierarchical groups
     // and load them in the cache
     $sql = "\nSELECT     {$group}.id, {$group}.cache_date, {$group}.saved_search_id, {$group}.children\nFROM       {$group}\nINNER JOIN {$mg} ON {$mg}.entity_id = {$group}.id\nWHERE      {$mg}.entity_table = '{$group}'\n  AND      {$mg}.group_type = 'Exclude'\n  AND      {$mg}.mailing_id = {$mailing_id}\n  AND      ( saved_search_id != 0\n   OR        saved_search_id IS NOT NULL\n   OR        children IS NOT NULL )\n";
     $groupDAO = CRM_Core_DAO::executeQuery($sql);
     while ($groupDAO->fetch()) {
         if ($groupDAO->cache_date == NULL) {
             CRM_Contact_BAO_GroupContactCache::load($groupDAO);
         }
         $smartGroupExclude = "\nINSERT IGNORE INTO X_{$job_id} (contact_id)\nSELECT c.contact_id\nFROM   civicrm_group_contact_cache c\nWHERE  c.group_id = {$groupDAO->id}\n";
         $mailingGroup->query($smartGroupExclude);
     }
     $tempColumn = 'email_id';
     if ($mode == 'sms') {
         $tempColumn = 'phone_id';
     }
     /* Get all the group contacts we want to include */
     $mailingGroup->query("CREATE TEMPORARY TABLE I_{$job_id}\n            ({$tempColumn} int, contact_id int primary key)\n            ENGINE=HEAP");
     /* Get the group contacts, but only those which are not in the
      * exclusion temp table */
     $query = "REPLACE INTO       I_{$job_id} (email_id, contact_id)\n\n                    SELECT DISTINCT     {$email}.id as email_id,\n                                        {$contact}.id as contact_id\n                    FROM                {$email}\n                    INNER JOIN          {$contact}\n                            ON          {$email}.contact_id = {$contact}.id\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             {$mg}.search_id IS NULL\n                        AND             {$g2contact}.status = 'Added'\n                        AND             {$contact}.do_not_email = 0\n                        AND             {$contact}.is_opt_out = 0\n                        AND             {$contact}.is_deceased = 0\n                        AND            ({$email}.is_bulkmail = 1 OR {$email}.is_primary = 1)\n                        AND             {$email}.email IS NOT NULL\n                        AND             {$email}.email != ''\n                        AND             {$email}.on_hold = 0\n                        AND             {$mg}.mailing_id = {$mailing_id}\n                        AND             X_{$job_id}.contact_id IS null\n                    ORDER BY {$email}.is_bulkmail";
     if ($mode == 'sms') {
         $phoneTypes = CRM_Core_OptionGroup::values('phone_type', TRUE, FALSE, FALSE, NULL, 'name');
         $query = "REPLACE INTO       I_{$job_id} (phone_id, contact_id)\n\n                    SELECT DISTINCT     {$phone}.id as phone_id,\n                                        {$contact}.id as contact_id\n                    FROM                {$phone}\n                    INNER JOIN          {$contact}\n                            ON          {$phone}.contact_id = {$contact}.id\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             {$mg}.search_id IS NULL\n                        AND             {$g2contact}.status = 'Added'\n                        AND             {$contact}.do_not_sms = 0\n                        AND             {$contact}.is_opt_out = 0\n                        AND             {$contact}.is_deceased = 0\n                        AND             {$phone}.phone_type_id = {$phoneTypes['Mobile']}\n                        AND             {$phone}.phone IS NOT NULL\n                        AND             {$phone}.phone != ''\n                        AND             {$mg}.mailing_id = {$mailing_id}\n                        AND             X_{$job_id}.contact_id IS null";
     }
     $mailingGroup->query($query);
     /* Query prior mailings */
     $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          {$contact}\n                            ON          {$email}.contact_id = {$contact}.id\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             {$contact}.is_deceased = 0\n                        AND            ({$email}.is_bulkmail = 1 OR {$email}.is_primary = 1)\n                        AND             {$email}.on_hold = 0\n                        AND             {$mg}.mailing_id = {$mailing_id}\n                        AND             X_{$job_id}.contact_id IS null\n                    ORDER BY {$email}.is_bulkmail";
     if ($mode == 'sms') {
         $query = "REPLACE INTO       I_{$job_id} (phone_id, contact_id)\n                    SELECT DISTINCT     {$phone}.id as phone_id,\n                                        {$contact}.id as contact_id\n                    FROM                {$phone}\n                    INNER JOIN          {$contact}\n                            ON          {$phone}.contact_id = {$contact}.id\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_sms = 0\n                        AND             {$contact}.is_opt_out = 0\n                        AND             {$contact}.is_deceased = 0\n                        AND             {$phone}.phone_type_id = {$phoneTypes['Mobile']}\n                        AND             {$mg}.mailing_id = {$mailing_id}\n                        AND             X_{$job_id}.contact_id IS null";
     }
     $mailingGroup->query($query);
     $sql = "\nSELECT     {$group}.id, {$group}.cache_date, {$group}.saved_search_id, {$group}.children\nFROM       {$group}\nINNER JOIN {$mg} ON {$mg}.entity_id = {$group}.id\nWHERE      {$mg}.entity_table = '{$group}'\n  AND      {$mg}.group_type = 'Include'\n  AND      {$mg}.search_id IS NULL\n  AND      {$mg}.mailing_id = {$mailing_id}\n  AND      ( saved_search_id != 0\n   OR        saved_search_id IS NOT NULL\n   OR        children IS NOT NULL )\n";
     $groupDAO = CRM_Core_DAO::executeQuery($sql);
     while ($groupDAO->fetch()) {
         if ($groupDAO->cache_date == NULL) {
             CRM_Contact_BAO_GroupContactCache::load($groupDAO);
         }
         $smartGroupInclude = "\nINSERT IGNORE INTO I_{$job_id} (email_id, contact_id)\nSELECT     e.id as email_id, c.id as contact_id\nFROM       civicrm_contact c\nINNER JOIN civicrm_email e                ON e.contact_id         = c.id\nINNER JOIN civicrm_group_contact_cache gc ON gc.contact_id        = c.id\nLEFT  JOIN X_{$job_id}                      ON X_{$job_id}.contact_id = c.id\nWHERE      gc.group_id = {$groupDAO->id}\n  AND      c.do_not_email = 0\n  AND      c.is_opt_out = 0\n  AND      c.is_deceased = 0\n  AND      (e.is_bulkmail = 1 OR e.is_primary = 1)\n  AND      e.on_hold = 0\n  AND      X_{$job_id}.contact_id IS null\nORDER BY   e.is_bulkmail\n";
         if ($mode == 'sms') {
             $smartGroupInclude = "\nINSERT IGNORE INTO I_{$job_id} (phone_id, contact_id)\nSELECT     p.id as phone_id, c.id as contact_id\nFROM       civicrm_contact c\nINNER JOIN civicrm_phone p                ON p.contact_id         = c.id\nINNER JOIN civicrm_group_contact_cache gc ON gc.contact_id        = c.id\nLEFT  JOIN X_{$job_id}                      ON X_{$job_id}.contact_id = c.id\nWHERE      gc.group_id = {$groupDAO->id}\n  AND      c.do_not_sms = 0\n  AND      c.is_opt_out = 0\n  AND      c.is_deceased = 0\n  AND      p.phone_type_id = {$phoneTypes['Mobile']}\n  AND      X_{$job_id}.contact_id IS null";
         }
         $mailingGroup->query($smartGroupInclude);
     }
     /**
      * Construct the filtered search queries
      */
     $query = "\nSELECT search_id, search_args, entity_id\nFROM   {$mg}\nWHERE  {$mg}.search_id IS NOT NULL\nAND    {$mg}.mailing_id = {$mailing_id}\n";
     $dao = CRM_Core_DAO::executeQuery($query);
     while ($dao->fetch()) {
         $customSQL = CRM_Contact_BAO_SearchCustom::civiMailSQL($dao->search_id, $dao->search_args, $dao->entity_id);
         $query = "REPLACE INTO       I_{$job_id} ({$tempColumn}, contact_id)\n                         {$customSQL}";
         $mailingGroup->query($query);
     }
     /* Get the emails with only location override */
     $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          {$contact}\n                            ON          {$email}.contact_id = {$contact}.id\n                    INNER JOIN          {$g2contact}\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             {$contact}.do_not_email = 0\n                        AND             {$contact}.is_opt_out = 0\n                        AND             {$contact}.is_deceased = 0\n                        AND             ({$email}.is_bulkmail = 1 OR {$email}.is_primary = 1)\n                        AND             {$email}.on_hold = 0\n                        AND             {$mg}.mailing_id = {$mailing_id}\n                        AND             X_{$job_id}.contact_id IS null\n                    ORDER BY {$email}.is_bulkmail";
     if ($mode == "sms") {
         $query = "REPLACE INTO       I_{$job_id} (phone_id, contact_id)\n                    SELECT DISTINCT     {$phone}.id as phone_id,\n                                        {$contact}.id as contact_id\n                    FROM                {$phone}\n                    INNER JOIN          {$contact}\n                            ON          {$phone}.contact_id = {$contact}.id\n                    INNER JOIN          {$g2contact}\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             {$contact}.do_not_sms = 0\n                        AND             {$contact}.is_opt_out = 0\n                        AND             {$contact}.is_deceased = 0\n                        AND             {$phone}.phone_type_id = {$phoneTypes['Mobile']}\n                        AND             {$mg}.mailing_id = {$mailing_id}\n                        AND             X_{$job_id}.contact_id IS null";
     }
     $mailingGroup->query($query);
     $results = array();
     $eq = new CRM_Mailing_Event_BAO_Queue();
     list($aclFrom, $aclWhere) = CRM_Contact_BAO_Contact_Permission::cacheClause();
     $aclWhere = $aclWhere ? "WHERE {$aclWhere}" : '';
     $limitString = NULL;
     if ($limit && $offset !== NULL) {
         $offset = CRM_Utils_Type::escape($offset, 'Int');
         $limit = CRM_Utils_Type::escape($limit, 'Int');
         $limitString = "LIMIT {$offset}, {$limit}";
     }
     if ($storeRecipients && $mailing_id) {
         $sql = "\nDELETE\nFROM   civicrm_mailing_recipients\nWHERE  mailing_id = %1\n";
         $params = array(1 => array($mailing_id, 'Integer'));
         CRM_Core_DAO::executeQuery($sql, $params);
         // CRM-3975
         $groupBy = $groupJoin = '';
         if ($dedupeEmail) {
             $groupJoin = " INNER JOIN civicrm_email e ON e.id = i.email_id";
             $groupBy = " GROUP BY e.email ";
         }
         $sql = "\nINSERT INTO civicrm_mailing_recipients ( mailing_id, contact_id, {$tempColumn} )\nSELECT %1, i.contact_id, i.{$tempColumn}\nFROM       civicrm_contact contact_a\nINNER JOIN I_{$job_id} i ON contact_a.id = i.contact_id\n           {$groupJoin}\n           {$aclFrom}\n           {$aclWhere}\n           {$groupBy}\nORDER BY   i.contact_id, i.{$tempColumn}\n";
         CRM_Core_DAO::executeQuery($sql, $params);
         // if we need to add all emails marked bulk, do it as a post filter
         // on the mailing recipients table
         if (CRM_Core_BAO_Email::isMultipleBulkMail()) {
             self::addMultipleEmails($mailing_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;
 }
コード例 #10
0
ファイル: Mailing.php プロジェクト: hampelm/Ginsberg-CiviDemo
 function &getRecipients($job_id, $includeDelivered = false, $mailing_id = null, $offset = NULL, $limit = NULL)
 {
     $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();
     require_once 'CRM/Contact/DAO/Group.php';
     $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  DISTINCT    {$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 = {$mailing_id}\n                        AND             {$g2contact}.status = 'Added'\n                        AND             {$mg}.group_type = 'Exclude'";
     $mailingGroup->query($excludeSubGroup);
     /* Add all unsubscribe members of base group from this mailing to the temp
      * table */
     $unSubscribeBaseGroup = "INSERT INTO        X_{$job_id} (contact_id)\n                    SELECT  DISTINCT    {$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 = {$mailing_id}\n                        AND             {$g2contact}.status = 'Removed'\n                        AND             {$mg}.group_type = 'Base'";
     $mailingGroup->query($unSubscribeBaseGroup);
     /* 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  DISTINCT    {$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 = {$mailing_id}\n                        AND             {$mg}.group_type = 'Exclude'";
     $mailingGroup->query($excludeSubMailing);
     $ss = new CRM_Core_DAO();
     $ss->query("SELECT             {$group}.saved_search_id as saved_search_id,\n                                    {$group}.id as 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 = {$mailing_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);
         $sql = CRM_Contact_BAO_SavedSearch::contactIDsSQL($ss->saved_search_id);
         $sql = $sql . " AND contact_a.id NOT IN ( \n                              SELECT contact_id FROM {$g2contact} \n                              WHERE {$g2contact}.group_id = {$ss->id} AND {$g2contact}.status = 'Removed')";
         $mailingGroup->query("INSERT IGNORE INTO X_{$job_id} (contact_id) {$sql}");
     }
     /* 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 */
     $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          {$contact}\n                            ON          {$email}.contact_id = {$contact}.id\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' OR {$mg}.group_type = 'Base')\n                        AND             {$mg}.search_id IS NULL\n                        AND             {$g2contact}.status = 'Added'\n                        AND             {$g2contact}.email_id IS null\n                        AND             {$contact}.do_not_email = 0\n                        AND             {$contact}.is_opt_out = 0\n                        AND             {$contact}.is_deceased = 0\n                        AND            ({$email}.is_bulkmail = 1 OR {$email}.is_primary = 1)\n                        AND             {$email}.on_hold = 0\n                        AND             {$mg}.mailing_id = {$mailing_id}\n                        AND             X_{$job_id}.contact_id IS null\n                    ORDER BY {$email}.is_bulkmail";
     $mailingGroup->query($query);
     /* 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          {$contact}\n                            ON          {$email}.contact_id = {$contact}.id\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' OR {$mg}.group_type = 'Base')\n                        AND             {$contact}.do_not_email = 0\n                        AND             {$contact}.is_opt_out = 0\n                        AND             {$contact}.is_deceased = 0\n                        AND            ({$email}.is_bulkmail = 1 OR {$email}.is_primary = 1)\n                        AND             {$email}.on_hold = 0\n                        AND             {$mg}.mailing_id = {$mailing_id}\n                        AND             X_{$job_id}.contact_id IS null\n                    ORDER BY {$email}.is_bulkmail");
     /* Construct the saved-search queries */
     $ss->query("SELECT          {$group}.saved_search_id as saved_search_id,\n                                    {$group}.id as 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}.search_id IS NULL\n                        AND         {$mg}.mailing_id = {$mailing_id}\n                        AND         {$group}.saved_search_id IS NOT null");
     $whereTables = array();
     while ($ss->fetch()) {
         $tables = array($contact => 1, $location => 1, $email => 1);
         list($from, $where) = CRM_Contact_BAO_SavedSearch::fromWhereEmail($ss->saved_search_id);
         $where = trim($where);
         if ($where) {
             $where = " AND {$where} ";
         }
         $ssq = "INSERT IGNORE INTO  I_{$job_id} (email_id, contact_id)\n                    SELECT DISTINCT     {$email}.id as email_id,\n                                        contact_a.id as contact_id \n                    {$from}\n                    LEFT JOIN           X_{$job_id}\n                            ON          contact_a.id = X_{$job_id}.contact_id\n                    WHERE           \n                                        contact_a.do_not_email = 0\n                        AND             contact_a.is_opt_out = 0\n                        AND             contact_a.is_deceased = 0\n                        AND             ({$email}.is_bulkmail = 1 OR {$email}.is_primary = 1)\n                        AND             {$email}.on_hold = 0\n                                        {$where}\n                        AND             contact_a.id NOT IN ( \n                                          SELECT contact_id FROM {$g2contact} \n                                          WHERE {$g2contact}.group_id = {$ss->id} AND {$g2contact}.status = 'Removed') \n                        AND             X_{$job_id}.contact_id IS null\n                    ORDER BY {$email}.is_bulkmail";
         $mailingGroup->query($ssq);
     }
     /**
      * Construct the filtered search queries
      */
     $query = "\nSELECT search_id, search_args, entity_id\nFROM   {$mg}\nWHERE  {$mg}.search_id IS NOT NULL\nAND    {$mg}.mailing_id = {$mailing_id}\n";
     $dao = CRM_Core_DAO::executeQuery($query);
     require_once 'CRM/Contact/BAO/SearchCustom.php';
     while ($dao->fetch()) {
         $customSQL = CRM_Contact_BAO_SearchCustom::civiMailSQL($dao->search_id, $dao->search_args, $dao->entity_id);
         $query = "REPLACE INTO       I_{$job_id} (email_id, contact_id)\n                         {$customSQL}";
         $mailingGroup->query($query);
     }
     /* Get the emails with only location override */
     $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          {$contact}\n                            ON          {$email}.contact_id = {$contact}.id\n                    INNER JOIN          {$g2contact}\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}.email_id is null\n                        AND             {$contact}.do_not_email = 0\n                        AND             {$contact}.is_opt_out = 0\n                        AND             {$contact}.is_deceased = 0\n                        AND             ({$email}.is_bulkmail = 1 OR {$email}.is_primary = 1)\n                        AND             {$email}.on_hold = 0\n                        AND             {$mg}.mailing_id = {$mailing_id}\n                        AND             X_{$job_id}.contact_id IS null\n                    ORDER BY {$email}.is_bulkmail";
     $mailingGroup->query($query);
     /* 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}.email_id IS NOT null\n                        AND             {$contact}.do_not_email = 0\n                        AND             {$contact}.is_opt_out = 0\n                        AND             {$contact}.is_deceased = 0\n                        AND             ({$email}.is_bulkmail = 1 OR {$email}.is_primary = 1)\n                        AND             {$email}.on_hold = 0\n                        AND             {$mg}.mailing_id = {$mailing_id}\n                        AND             X_{$job_id}.contact_id IS null\n                    ORDER BY {$email}.is_bulkmail");
     $results = array();
     $eq = new CRM_Mailing_Event_BAO_Queue();
     require_once 'CRM/Contact/BAO/Contact/Permission.php';
     list($aclFrom, $aclWhere) = CRM_Contact_BAO_Contact_Permission::cacheClause();
     $aclWhere = $aclWhere ? "WHERE {$aclWhere}" : '';
     $limitString = null;
     if ($limit && $offset !== null) {
         $limitString = "LIMIT {$offset}, {$limit}";
     }
     $eq->query("SELECT i.contact_id, i.email_id \n                    FROM  civicrm_contact contact_a\n                    INNER JOIN I_{$job_id} i ON contact_a.id = i.contact_id\n                    {$aclFrom}\n                    {$aclWhere}\n                    ORDER BY i.contact_id, i.email_id\n                    {$limitString}");
     /* Delete the temp table */
     $mailingGroup->reset();
     $mailingGroup->query("DROP TEMPORARY TABLE X_{$job_id}");
     $mailingGroup->query("DROP TEMPORARY TABLE I_{$job_id}");
     return $eq;
 }
コード例 #11
0
 /**
  * adds $value['foo_display'] for each $value['foo'] enum from civicrm_group_contact
  *
  * @param array $values (reference)  the array up for enhancing
  * @return void
  */
 static function addDisplayEnums(&$values)
 {
     $enumFields =& CRM_Contact_DAO_GroupContact::getEnums();
     foreach ($enumFields as $enum) {
         if (isset($values[$enum])) {
             $values[$enum . '_display'] = CRM_Contact_DAO_GroupContact::tsEnum($enum, $values[$enum]);
         }
     }
 }
コード例 #12
0
ファイル: GroupContact.php プロジェクト: agroknow/mermix
 /**
  * Returns the list of fields that can be exported
  *
  * @param bool $prefix
  *
  * @return array
  */
 static function &export($prefix = false)
 {
     if (!self::$_export) {
         self::$_export = array();
         $fields = self::fields();
         foreach ($fields as $name => $field) {
             if (CRM_Utils_Array::value('export', $field)) {
                 if ($prefix) {
                     self::$_export['group_contact'] =& $fields[$name];
                 } else {
                     self::$_export[$name] =& $fields[$name];
                 }
             }
         }
     }
     return self::$_export;
 }
コード例 #13
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;
 }
コード例 #14
0
ファイル: Form.php プロジェクト: hampelm/Ginsberg-CiviDemo
 /**
  * Process the user submitted custom data values.
  *
  * @access public
  * @return void
  */
 public function postProcess()
 {
     $params = $this->controller->exportValues($this->_name);
     if (CRM_Utils_Array::value('image_URL', $params)) {
         CRM_Contact_BAO_Contact::processImageParams($params);
     }
     $greetingTypes = array('addressee' => 'addressee_id', 'email_greeting' => 'email_greeting_id', 'postal_greeting' => 'postal_greeting_id');
     if ($this->_id) {
         $contactDetails = CRM_Contact_BAO_Contact::getHierContactDetails($this->_id, $greetingTypes);
         $details = $contactDetails[0][$this->_id];
     }
     if (!(CRM_Utils_Array::value('addressee_id', $details) || CRM_Utils_Array::value('email_greeting_id', $details) || CRM_Utils_Array::value('postal_greeting_id', $details))) {
         $profileType = CRM_Core_BAO_UFField::getProfileType($this->_gid);
         //Though Profile type is contact we need
         //Individual/Household/Organization for setting Greetings.
         if ($profileType == 'Contact') {
             $profileType = 'Individual';
             //if we editing Household/Organization.
             if ($this->_id) {
                 $profileType = CRM_Contact_BAO_Contact::getContactType($this->_id);
             }
         }
         if (CRM_Contact_BAO_ContactType::isaSubType($profileType)) {
             $profileType = CRM_Contact_BAO_ContactType::getBasicType($profileType);
         }
         $contactTypeFilters = array(1 => 'Individual', 2 => 'Household', 3 => 'Organization');
         $filter = CRM_Utils_Array::key($profileType, $contactTypeFilters);
         if ($filter) {
             foreach ($greetingTypes as $key => $value) {
                 if (!array_key_exists($key, $params)) {
                     $defaultGreetingTypeId = CRM_Core_OptionGroup::values($key, null, null, null, "AND is_default =1\n                                                                               AND (filter = \n                                                                               {$filter} OR \n                                                                               filter = 0 )", 'value');
                     $params[$key] = key($defaultGreetingTypeId);
                 }
             }
         }
         if ($profileType == 'Organization') {
             unset($params['email_greeting'], $params['postal_greeting']);
         }
     }
     if ($this->_mode == self::MODE_REGISTER) {
         require_once 'CRM/Core/BAO/Address.php';
         CRM_Core_BAO_Address::setOverwrite(false);
     }
     require_once 'CRM/Core/Transaction.php';
     $transaction = new CRM_Core_Transaction();
     //used to send subcribe mail to the group which user want.
     //if the profile double option in is enabled
     $mailingType = array();
     $config = CRM_Core_Config::singleton();
     if ($config->profileDoubleOptIn && CRM_Utils_Array::value('group', $params)) {
         $result = null;
         foreach ($params as $name => $values) {
             if (substr($name, 0, 6) == 'email-') {
                 $result['email'] = $values;
             }
         }
         $groupSubscribed = array();
         if (CRM_Utils_Array::value('email', $result)) {
             require_once 'CRM/Contact/DAO/Group.php';
             //array of group id, subscribed by contact
             $contactGroup = array();
             if ($this->_id) {
                 $contactGroups = new CRM_Contact_DAO_GroupContact();
                 $contactGroups->contact_id = $this->_id;
                 $contactGroups->status = 'Added';
                 $contactGroups->find();
                 $contactGroup = array();
                 while ($contactGroups->fetch()) {
                     $contactGroup[] = $contactGroups->group_id;
                     $groupSubscribed[$contactGroups->group_id] = 1;
                 }
             }
             foreach ($params['group'] as $key => $val) {
                 if (!$val) {
                     unset($params['group'][$key]);
                     continue;
                 }
                 $groupTypes = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $key, 'group_type', 'id');
                 $groupType = explode(CRM_Core_BAO_CustomOption::VALUE_SEPERATOR, substr($groupTypes, 1, -1));
                 //filter group of mailing type and unset it from params
                 if (in_array(2, $groupType)) {
                     //if group is already subscribed , ignore it
                     $groupExist = CRM_Utils_Array::key($key, $contactGroup);
                     if (!isset($groupExist)) {
                         $mailingType[] = $key;
                         unset($params['group'][$key]);
                     }
                 }
             }
         }
     }
     if (CRM_Utils_Array::value('add_to_group', $params)) {
         $addToGroupId = $params['add_to_group'];
         // since we are directly adding contact to group lets unset it from mailing
         if ($key = array_search($addToGroupId, $mailingType)) {
             unset($mailingType[$key]);
         }
     }
     if ($this->_grid) {
         $params['group'] = $groupSubscribed;
     }
     // commenting below code, since we potentially
     // triggered maximum name field formatting cases during CRM-4430.
     // CRM-4343
     // $params['preserveDBName'] = true;
     $this->_id = CRM_Contact_BAO_Contact::createProfileContact($params, $this->_fields, $this->_id, $this->_addToGroupID, $this->_gid, $this->_ctype, true);
     //mailing type group
     if (!empty($mailingType)) {
         require_once 'CRM/Mailing/Event/BAO/Subscribe.php';
         CRM_Mailing_Event_BAO_Subscribe::commonSubscribe($mailingType, $result);
     }
     require_once 'CRM/Core/BAO/UFGroup.php';
     $ufGroups = array();
     if ($this->_gid) {
         $ufGroups[$this->_gid] = 1;
     } else {
         if ($this->_mode == self::MODE_REGISTER) {
             $ufGroups =& CRM_Core_BAO_UFGroup::getModuleUFGroup('User Registration');
         }
     }
     foreach ($ufGroups as $gId => $val) {
         if ($notify = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $gId, 'notify')) {
             $values = CRM_Core_BAO_UFGroup::checkFieldsEmptyValues($gId, $this->_id, null);
             CRM_Core_BAO_UFGroup::commonSendMail($this->_id, $values);
         }
     }
     //create CMS user (if CMS user option is selected in profile)
     if (CRM_Utils_Array::value('cms_create_account', $params) && $this->_mode == self::MODE_CREATE) {
         $params['contactID'] = $this->_id;
         require_once "CRM/Core/BAO/CMSUser.php";
         if (!CRM_Core_BAO_CMSUser::create($params, $this->_mail)) {
             CRM_Core_Session::setStatus(ts('Your profile is not saved and Account is not created.'));
             $transaction->rollback();
             return CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/profile/create', 'reset=1&gid=' . $this->_gid));
         }
     }
     $transaction->commit();
 }