/** * HoldEmail() method (set and reset on_hold condition) */ public function testHoldEmail() { $contactId = Contact::createIndividual(); $params = array(); $params = array('email' => '*****@*****.**', 'is_primary' => 1, 'location_type_id' => 1, 'contact_id' => $contactId); CRM_Core_BAO_Email::add($params); $emailId = $this->assertDBNotNull('CRM_Core_DAO_Email', '*****@*****.**', 'id', 'email', 'Database check for created email address.'); // Now call add() to update on_hold=true and check record state $params = array(); $params = array('id' => $emailId, 'contact_id' => $contactId, 'on_hold' => 1); CRM_Core_BAO_Email::add($params); // Use assertDBNotNull to get back value of hold_date and check if it's in the current year. // NOTE: The assertEquals will fail IF this test is run just as the year is changing (low likelihood). $holdDate = $this->assertDBNotNull('CRM_Core_DAO_Email', $emailId, 'hold_date', 'id', 'Retrieve hold_date from the updated email record.'); $this->assertEquals(substr($holdDate, 0, 4), substr(date('YmdHis'), 0, 4), 'Compare hold_date (' . $holdDate . ') in DB to current year.'); $this->assertDBCompareValue('CRM_Core_DAO_Email', $emailId, 'on_hold', 'id', 1, 'Check if on_hold=1 in updated email record.'); // Now call add() with on_hold=false and verify that reset_date is set. $params = array(); $params = array('id' => $emailId, 'contact_id' => $contactId, 'on_hold' => 'null'); CRM_Core_BAO_Email::add($params); $this->assertDBCompareValue('CRM_Core_DAO_Email', $emailId, 'on_hold', 'id', 0, 'Check if on_hold=0 in updated email record.'); $this->assertDBCompareValue('CRM_Core_DAO_Email', $emailId, 'hold_date', 'id', '', 'Check if hold_date has been set to empty string.'); // Use assertDBNotNull to get back value of reset_date and check if it's in the current year. // NOTE: The assertEquals will fail IF this test is run just as the year is changing (low likelihood). $resetDate = $this->assertDBNotNull('CRM_Core_DAO_Email', $emailId, 'reset_date', 'id', 'Retrieve reset_date from the updated email record.'); $this->assertEquals(substr($resetDate, 0, 4), substr(date('YmdHis'), 0, 4), 'Compare reset_date (' . $resetDate . ') in DB to current year.'); Contact::delete($contactId); }
static function create($params) { if (is_numeric(CRM_Utils_Array::value('is_primary', $params)) || empty($params['id'])) { CRM_Core_BAO_Block::handlePrimary($params, get_class()); } $email = CRM_Core_BAO_Email::add($params); return $email; }
/** * Create email address - note that the create function calls 'add' but * has more business logic * * @param array $params * Input parameters. * * @return object */ public static function create($params) { // if id is set & is_primary isn't we can assume no change if (is_numeric(CRM_Utils_Array::value('is_primary', $params)) || empty($params['id'])) { CRM_Core_BAO_Block::handlePrimary($params, get_class()); } $email = CRM_Core_BAO_Email::add($params); return $email; }
function testFindReferences() { $params = array('first_name' => 'Testy', 'last_name' => 'McScallion', 'contact_type' => 'Individual'); $contact = CRM_Contact_BAO_Contact::add($params); $this->assertNotNull($contact->id); $params = array('email' => '*****@*****.**', 'contact_id' => $contact->id, 'is_primary' => 0, 'location_type_id' => 1); $email = CRM_Core_BAO_Email::add($params); $refs = $contact->findReferences(); $refsByTable = array(); foreach ($refs as $refObj) { $refsByTable[$refObj->__table] = $refObj; } $this->assertTrue(array_key_exists('civicrm_email', $refsByTable)); $refDao = $refsByTable['civicrm_email']; $refDao->find(TRUE); $this->assertEquals($contact->id, $refDao->contact_id); }
/** * Ensure that civicrm_contact.modified_date is updated when manipulating a phone record. */ public function testTimestampsEmail() { $test = $this; $this->_testTimestamps(array('INSERT' => function ($contactId) use($test) { $params = array('email' => '*****@*****.**', 'is_primary' => 1, 'location_type_id' => 1, 'contact_id' => $contactId); CRM_Core_BAO_Email::add($params); $test->assertDBQuery('*****@*****.**', 'SELECT email FROM civicrm_email WHERE contact_id = %1 ORDER BY id DESC LIMIT 1', array(1 => array($contactId, 'Integer'))); }, 'UPDATE' => function ($contactId) use($test) { CRM_Core_DAO::executeQuery('UPDATE civicrm_email SET email = "*****@*****.**" WHERE contact_id = %1', array(1 => array($contactId, 'Integer'))); }, 'DELETE' => function ($contactId) use($test) { CRM_Core_DAO::executeQuery('DELETE FROM civicrm_email WHERE contact_id = %1', array(1 => array($contactId, 'Integer'))); })); }
/** * Put a Mailchimp subscriber's email On Hold in CiviCRM. */ static function mailchimpWebhookCleaned($request_data) { $mailchimp_contact = CRM_CiviMailchimp_Utils::getContactInMailchimpListByEmail($request_data['email'], $request_data['list_id']); foreach ($mailchimp_contact->email as $email) { if ($email->email === $request_data['email']) { // We have to go the circuitous route to saving so we can trigger // CiviCRM's hooks to allow other extensions to act. $params = array(); CRM_Core_DAO::storeValues($email, $params); $params['on_hold'] = 1; CRM_Core_BAO_Email::add($params); } } }
/** * takes an associative array and creates a contact object * * the function extract all the params it needs to initialize the create a * contact object. the params array could contain additional unused name/value * pairs * * @param array $params (reference ) an assoc array of name/value pairs * @param array $ids the array that holds all the db ids * @param array $locationId * * @return object CRM_Core_BAO_Location object on success, null otherwise * @access public * @static */ function add(&$params, &$ids, $locationId) { if (!CRM_Core_BAO_Location::dataExists($params, $locationId, $ids)) { return null; } $location =& new CRM_Core_BAO_Location(); if (!isset($params['contact_id'])) { require_once 'CRM/Core/BAO/Domain.php'; $location->entity_table = CRM_Core_BAO_Domain::getTableName(); $location->entity_id = $params['domain_id']; } else { $location->entity_table = CRM_Contact_BAO_Contact::getTableName(); $location->entity_id = $params['contact_id']; } $location->location_type_id = CRM_Utils_Array::value('location_type_id', $params['location'][$locationId]); $location->name = CRM_Utils_Array::value('name', $params['location'][$locationId]); $location->is_primary = CRM_Utils_Array::value('is_primary', $params['location'][$locationId], false); // check if there exists another location has is_primary set, and if so reset that // if no location has is_primary, make this one is_primart if ($location->is_primary) { // reset all other locations with the same entity table entity id $sql = "UPDATE " . CRM_Core_BAO_Location::getTableName() . "\n SET is_primary = 0 WHERE \n entity_table = '{$location->entity_table}' AND\n entity_id = '{$location->entity_id}' "; CRM_Core_DAO::executeQuery($sql); } else { // make sure there is at once location with is_primary set $sql = "SELECT count( " . CRM_Core_BAO_Location::getTableName() . ".id )\n FROM " . CRM_Core_BAO_Location::getTableName() . " WHERE\n entity_table = '{$location->entity_table}' AND\n entity_id = '{$location->entity_id}' AND\n is_primary = 1"; $count = CRM_Core_DAO::singleValueQuery($sql); if ($count == 0) { $location->is_primary = true; } } $location->id = CRM_Utils_Array::value('id', $ids['location'][$locationId]); $location->save(); $params['location'][$locationId]['id'] = $location->id; $address_object = CRM_Core_BAO_Address::add($params, $ids, $locationId); $location->address = $address_object; // set this to true if this has been made the primary IM. // the rule is the first entered value is the primary object $isPrimaryPhone = $isPrimaryEmail = $isPrimaryIM = true; $location->phone = array(); $location->email = array(); $location->im = array(); for ($i = 1; $i <= CRM_CONTACT_FORM_LOCATION_BLOCKS; $i++) { $location->phone[$i] = CRM_Core_BAO_Phone::add($params, $ids, $locationId, $i, $isPrimaryPhone); $location->email[$i] = CRM_Core_BAO_Email::add($params, $ids, $locationId, $i, $isPrimaryEmail); $location->im[$i] = CRM_Core_BAO_IM::add($params, $ids, $locationId, $i, $isPrimaryIM); } return $location; }