/** * Generate a random contact of type $cType. * * @param string Contact Type, default as 'Individual' * @return int Contact ID of created contact */ private function randomContact($cType = 'Individual') { $contact = new CRM_Contact_DAO_Contact(); //TODO : in future if we need cType as 'Organization' //Generate contact of type individual randomly $contact->contact_type = $cType; if ($cType == 'Individual') { list($gender_id, $gender) = $this->randomKeyValue($this->gender); $contact->gender_id = $gender_id; $contact->first_name = $this->randomItem($gender . '_name'); $contact->middle_name = $this->probability(0.5) ? '' : ucfirst($this->randomChar()); $contact->last_name = $this->randomItem('last_name'); $contact->sort_name = $contact->last_name . ', ' . $contact->first_name; $contact->display_name = $contact->first_name . ' ' . $contact->last_name; } $contact->save(); $email = $this->_individualEmail($contact); $this->_addEmail($contact->id, $email, self::WORK); return $contact->id; }
/** * Updates contacts affected by the option value passed. * * @param int $optionValueId * The option value id. * @param int $action * The action describing whether prefix/suffix was UPDATED or DELETED. * * @return bool */ public static function updateRecords(&$optionValueId, $action) { //finding group name $optionValue = new CRM_Core_DAO_OptionValue(); $optionValue->id = $optionValueId; $optionValue->find(TRUE); $optionGroup = new CRM_Core_DAO_OptionGroup(); $optionGroup->id = $optionValue->option_group_id; $optionGroup->find(TRUE); // group name $gName = $optionGroup->name; // value $value = $optionValue->value; // get the proper group name & affected field name // todo: this may no longer be needed for individuals - check inputs $individuals = array('gender' => 'gender_id', 'individual_prefix' => 'prefix_id', 'individual_suffix' => 'suffix_id', 'communication_style' => 'communication_style_id'); $contributions = array('payment_instrument' => 'payment_instrument_id'); $activities = array('activity_type' => 'activity_type_id'); $participant = array('participant_role' => 'role_id'); $eventType = array('event_type' => 'event_type_id'); $aclRole = array('acl_role' => 'acl_role_id'); $all = array_merge($individuals, $contributions, $activities, $participant, $eventType, $aclRole); $fieldName = ''; foreach ($all as $name => $id) { if ($gName == $name) { $fieldName = $id; } } if ($fieldName == '') { return TRUE; } if (array_key_exists($gName, $individuals)) { $contactDAO = new CRM_Contact_DAO_Contact(); $contactDAO->{$fieldName} = $value; $contactDAO->find(); while ($contactDAO->fetch()) { if ($action == CRM_Core_Action::DELETE) { $contact = new CRM_Contact_DAO_Contact(); $contact->id = $contactDAO->id; $contact->find(TRUE); // make sure dates doesn't get reset $contact->birth_date = CRM_Utils_Date::isoToMysql($contact->birth_date); $contact->deceased_date = CRM_Utils_Date::isoToMysql($contact->deceased_date); $contact->{$fieldName} = 'NULL'; $contact->save(); } } return TRUE; } if (array_key_exists($gName, $contributions)) { $contribution = new CRM_Contribute_DAO_Contribution(); $contribution->{$fieldName} = $value; $contribution->find(); while ($contribution->fetch()) { if ($action == CRM_Core_Action::DELETE) { $contribution->{$fieldName} = 'NULL'; $contribution->save(); } } return TRUE; } if (array_key_exists($gName, $activities)) { $activity = new CRM_Activity_DAO_Activity(); $activity->{$fieldName} = $value; $activity->find(); while ($activity->fetch()) { $activity->delete(); } return TRUE; } //delete participant role, type and event type option value if (array_key_exists($gName, $participant)) { $participantValue = new CRM_Event_DAO_Participant(); $participantValue->{$fieldName} = $value; if ($participantValue->find(TRUE)) { return FALSE; } return TRUE; } //delete event type option value if (array_key_exists($gName, $eventType)) { $event = new CRM_Event_DAO_Event(); $event->{$fieldName} = $value; if ($event->find(TRUE)) { return FALSE; } return TRUE; } //delete acl_role option value if (array_key_exists($gName, $aclRole)) { $entityRole = new CRM_ACL_DAO_EntityRole(); $entityRole->{$fieldName} = $value; $aclDAO = new CRM_ACL_DAO_ACL(); $aclDAO->entity_id = $value; if ($entityRole->find(TRUE) || $aclDAO->find(TRUE)) { return FALSE; } return TRUE; } }
/** * Function to set is_delete true or restore deleted contact. * * @param CRM_Contact_DAO_Contact $contact * Contact DAO object. * @param bool $restore * True to set the is_delete = 1 else false to restore deleted contact, * i.e. is_delete = 0 * * @return bool */ public static function contactTrashRestore($contact, $restore = FALSE) { $updateParams = array('id' => $contact->id, 'is_deleted' => $restore ? 0 : 1); CRM_Utils_Hook::pre('update', $contact->contact_type, $contact->id, $updateParams); $params = array(1 => array($contact->id, 'Integer')); if (!$restore) { $query = "DELETE FROM civicrm_uf_match WHERE contact_id = %1"; CRM_Core_DAO::executeQuery($query, $params); } $contact->copyValues($updateParams); $contact->save(); CRM_Utils_Hook::post('update', $contact->contact_type, $contact->id, $contact); return TRUE; }
/** * updates contacts affected by the option value passed. * * @param Integer $optionValueId the option value id. * @param int $action the action describing whether prefix/suffix was UPDATED or DELETED * * @return void */ static function updateRecords(&$optionValueId, $action) { //finding group name $optionValue =& new CRM_Core_DAO_OptionValue(); $optionValue->id = $optionValueId; $optionValue->find(true); $optionGroup =& new CRM_Core_DAO_OptionGroup(); $optionGroup->id = $optionValue->option_group_id; $optionGroup->find(true); $gName = $optionGroup->name; //group name $value = $optionValue->value; //value // get the proper group name & affected field name $individuals = array('gender' => 'gender_id', 'individual_prefix' => 'prefix_id', 'individual_suffix' => 'suffix_id'); $contributions = array('payment_instrument' => 'payment_instrument_id'); $activities = array('activity_type' => 'activity_type_id'); $participant = array('participant_role' => 'role_id'); $eventType = array('event_type' => 'event_type_id'); $aclRole = array('acl_role' => 'acl_role_id'); $all = array_merge($individuals, $contributions, $activities, $participant, $eventType, $aclRole); $fieldName = ''; foreach ($all as $name => $id) { if ($gName == $name) { $fieldName = $id; } } if ($fieldName == '') { return true; } if (array_key_exists($gName, $individuals)) { require_once 'CRM/Contact/BAO/Contact.php'; $contactDAO =& new CRM_Contact_DAO_Contact(); $contactDAO->{$fieldName} = $value; $contactDAO->find(); while ($contactDAO->fetch()) { if ($action == CRM_Core_Action::DELETE) { $contact = new CRM_Contact_DAO_Contact(); $contact->id = $contactDAO->id; $contact->find(true); // make sure dates doesn't get reset $contact->birth_date = CRM_Utils_Date::isoToMysql($contact->birth_date); $contact->deceased_date = CRM_Utils_Date::isoToMysql($contact->deceased_date); $contact->{$fieldName} = 'NULL'; $contact->save(); } } return true; } if (array_key_exists($gName, $contributions)) { require_once 'CRM/Contribute/DAO/Contribution.php'; $contribution =& new CRM_Contribute_DAO_Contribution(); $contribution->{$fieldName} = $value; $contribution->find(); while ($contribution->fetch()) { if ($action == CRM_Core_Action::DELETE) { $contribution->{$fieldName} = 'NULL'; $contribution->save(); } } return true; } if (array_key_exists($gName, $activities)) { require_once 'CRM/Activity/DAO/Activity.php'; $activity =& new CRM_Activity_DAO_Activity(); $activity->{$fieldName} = $value; $activity->find(); while ($activity->fetch()) { $activity->delete(); } return true; } //delete participant role, type and event type option value if (array_key_exists($gName, $participant)) { require_once 'CRM/Event/DAO/Participant.php'; $participantValue =& new CRM_Event_DAO_Participant(); $participantValue->{$fieldName} = $value; if ($participantValue->find(true)) { return false; } return true; } //delete event type option value if (array_key_exists($gName, $eventType)) { require_once 'CRM/Event/DAO/Event.php'; $event =& new CRM_Event_DAO_Event(); $event->{$fieldName} = $value; if ($event->find(true)) { return false; } return true; } //delete acl_role option value if (array_key_exists($gName, $aclRole)) { require_once 'CRM/ACL/DAO/EntityRole.php'; require_once 'CRM/ACL/DAO/ACL.php'; $entityRole =& new CRM_ACL_DAO_EntityRole(); $entityRole->{$fieldName} = $value; $aclDAO =& new CRM_ACL_DAO_ACL(); $aclDAO->entity_id = $value; if ($entityRole->find(true) || $aclDAO->find(true)) { return false; } return true; } }
function revert($tables) { // FIXME: split off the table → DAO mapping to a GenCode-generated class $daos = array('civicrm_address' => 'CRM_Core_DAO_Address', 'civicrm_contact' => 'CRM_Contact_DAO_Contact', 'civicrm_email' => 'CRM_Core_DAO_Email', 'civicrm_im' => 'CRM_Core_DAO_IM', 'civicrm_openid' => 'CRM_Core_DAO_OpenID', 'civicrm_phone' => 'CRM_Core_DAO_Phone', 'civicrm_website' => 'CRM_Core_DAO_Website', 'civicrm_contribution' => 'CRM_Contribute_DAO_Contribution', 'civicrm_note' => 'CRM_Core_DAO_Note', 'civicrm_relationship' => 'CRM_Contact_DAO_Relationship'); // get custom data tables, columns and types $ctypes = array(); $dao = CRM_Core_DAO::executeQuery('SELECT table_name, column_name, data_type FROM civicrm_custom_group cg JOIN civicrm_custom_field cf ON (cf.custom_group_id = cg.id)'); while ($dao->fetch()) { if (!isset($ctypes[$dao->table_name])) { $ctypes[$dao->table_name] = array('entity_id' => 'Integer'); } $ctypes[$dao->table_name][$dao->column_name] = $dao->data_type; } $differ = new CRM_Logging_Differ($this->log_conn_id, $this->log_date); $diffs = $differ->diffsInTables($tables); $deletes = array(); $reverts = array(); foreach ($diffs as $table => $changes) { foreach ($changes as $change) { switch ($change['action']) { case 'Insert': if (!isset($deletes[$table])) { $deletes[$table] = array(); } $deletes[$table][] = $change['id']; break; case 'Delete': case 'Update': if (!isset($reverts[$table])) { $reverts[$table] = array(); } if (!isset($reverts[$table][$change['id']])) { $reverts[$table][$change['id']] = array('log_action' => $change['action']); } $reverts[$table][$change['id']][$change['field']] = $change['from']; break; } } } // revert inserts by deleting foreach ($deletes as $table => $ids) { CRM_Core_DAO::executeQuery("DELETE FROM `{$table}` WHERE id IN (" . implode(', ', array_unique($ids)) . ')'); } // revert updates by updating to previous values foreach ($reverts as $table => $row) { switch (TRUE) { // DAO-based tables case in_array($table, array_keys($daos)): require_once str_replace('_', DIRECTORY_SEPARATOR, $daos[$table]) . '.php'; eval("\$dao = new {$daos[$table]};"); foreach ($row as $id => $changes) { $dao->id = $id; foreach ($changes as $field => $value) { if ($field == 'log_action') { continue; } if (empty($value) and $value !== 0 and $value !== '0') { $value = 'null'; } $dao->{$field} = $value; } $changes['log_action'] == 'Delete' ? $dao->insert() : $dao->update(); $dao->reset(); } break; // custom data tables // custom data tables case in_array($table, array_keys($ctypes)): foreach ($row as $id => $changes) { $inserts = array('id' => '%1'); $updates = array(); $params = array(1 => array($id, 'Integer')); $counter = 2; foreach ($changes as $field => $value) { // don’t try reverting a field that’s no longer there if (!isset($ctypes[$table][$field])) { continue; } switch ($ctypes[$table][$field]) { case 'Date': $value = substr(CRM_Utils_Date::isoToMysql($value), 0, 8); break; case 'Timestamp': $value = CRM_Utils_Date::isoToMysql($value); break; } $inserts[$field] = "%{$counter}"; $updates[] = "{$field} = %{$counter}"; $params[$counter] = array($value, $ctypes[$table][$field]); $counter++; } if ($changes['log_action'] == 'Delete') { $sql = "INSERT INTO `{$table}` (" . implode(', ', array_keys($inserts)) . ') VALUES (' . implode(', ', $inserts) . ')'; } else { $sql = "UPDATE `{$table}` SET " . implode(', ', $updates) . ' WHERE id = %1'; } CRM_Core_DAO::executeQuery($sql, $params); } break; } } // CRM-7353: if nothing altered civicrm_contact, touch it; this will // make sure there’s an entry in log_civicrm_contact for this revert if (empty($diffs['civicrm_contact'])) { $query = "\n SELECT id FROM `{$this->db}`.log_civicrm_contact\n WHERE log_conn_id = %1 AND log_date BETWEEN DATE_SUB(%2, INTERVAL 10 SECOND) AND DATE_ADD(%2, INTERVAL 10 SECOND)\n ORDER BY log_date DESC LIMIT 1\n "; $params = array(1 => array($this->log_conn_id, 'Integer'), 2 => array($this->log_date, 'String')); $cid = CRM_Core_DAO::singleValueQuery($query, $params); if (!$cid) { return; } $dao = new CRM_Contact_DAO_Contact(); $dao->id = $cid; if ($dao->find(TRUE)) { // CRM-8102: MySQL can’t parse its own dates $dao->birth_date = CRM_Utils_Date::isoToMysql($dao->birth_date); $dao->deceased_date = CRM_Utils_Date::isoToMysql($dao->deceased_date); $dao->save(); } } }
/** * Revert changes in the array of diffs in $this->diffs. */ public function revert() { // get custom data tables, columns and types $ctypes = array(); $dao = CRM_Core_DAO::executeQuery('SELECT table_name, column_name, data_type FROM civicrm_custom_group cg JOIN civicrm_custom_field cf ON (cf.custom_group_id = cg.id)'); while ($dao->fetch()) { if (!isset($ctypes[$dao->table_name])) { $ctypes[$dao->table_name] = array('entity_id' => 'Integer'); } $ctypes[$dao->table_name][$dao->column_name] = $dao->data_type; } $diffs = $this->diffs; $deletes = array(); $reverts = array(); foreach ($diffs as $table => $changes) { foreach ($changes as $change) { switch ($change['action']) { case 'Insert': if (!isset($deletes[$table])) { $deletes[$table] = array(); } $deletes[$table][] = $change['id']; break; case 'Delete': case 'Update': if (!isset($reverts[$table])) { $reverts[$table] = array(); } if (!isset($reverts[$table][$change['id']])) { $reverts[$table][$change['id']] = array('log_action' => $change['action']); } $reverts[$table][$change['id']][$change['field']] = $change['from']; break; } } } // revert inserts by deleting foreach ($deletes as $table => $ids) { CRM_Core_DAO::executeQuery("DELETE FROM `{$table}` WHERE id IN (" . implode(', ', array_unique($ids)) . ')'); } // revert updates by updating to previous values foreach ($reverts as $table => $row) { switch (TRUE) { // DAO-based tables case ($tableDAO = CRM_Core_DAO_AllCoreTables::getClassForTable($table)) != FALSE: $dao = new $tableDAO(); foreach ($row as $id => $changes) { $dao->id = $id; foreach ($changes as $field => $value) { if ($field == 'log_action') { continue; } if (empty($value) and $value !== 0 and $value !== '0') { $value = 'null'; } $dao->{$field} = $value; } $changes['log_action'] == 'Delete' ? $dao->insert() : $dao->update(); $dao->reset(); } break; // custom data tables // custom data tables case in_array($table, array_keys($ctypes)): foreach ($row as $id => $changes) { $inserts = array('id' => '%1'); $updates = array(); $params = array(1 => array($id, 'Integer')); $counter = 2; foreach ($changes as $field => $value) { // don’t try reverting a field that’s no longer there if (!isset($ctypes[$table][$field])) { continue; } $fldVal = "%{$counter}"; switch ($ctypes[$table][$field]) { case 'Date': $value = substr(CRM_Utils_Date::isoToMysql($value), 0, 8); break; case 'Timestamp': $value = CRM_Utils_Date::isoToMysql($value); break; case 'Boolean': if ($value === '') { $fldVal = 'DEFAULT'; } } $inserts[$field] = "%{$counter}"; $updates[] = "{$field} = {$fldVal}"; if ($fldVal != 'DEFAULT') { $params[$counter] = array($value, $ctypes[$table][$field]); } $counter++; } if ($changes['log_action'] == 'Delete') { $sql = "INSERT INTO `{$table}` (" . implode(', ', array_keys($inserts)) . ') VALUES (' . implode(', ', $inserts) . ')'; } else { $sql = "UPDATE `{$table}` SET " . implode(', ', $updates) . ' WHERE id = %1'; } CRM_Core_DAO::executeQuery($sql, $params); } break; } } // CRM-7353: if nothing altered civicrm_contact, touch it; this will // make sure there’s an entry in log_civicrm_contact for this revert if (empty($diffs['civicrm_contact'])) { $query = "\n SELECT id FROM `{$this->db}`.log_civicrm_contact\n WHERE log_conn_id = %1 AND log_date BETWEEN DATE_SUB(%2, INTERVAL 10 SECOND) AND DATE_ADD(%2, INTERVAL 10 SECOND)\n ORDER BY log_date DESC LIMIT 1\n "; $params = array(1 => array($this->log_conn_id, 'String'), 2 => array($this->log_date, 'String')); $cid = CRM_Core_DAO::singleValueQuery($query, $params); if (!$cid) { return; } $dao = new CRM_Contact_DAO_Contact(); $dao->id = $cid; if ($dao->find(TRUE)) { // CRM-8102: MySQL can’t parse its own dates $dao->birth_date = CRM_Utils_Date::isoToMysql($dao->birth_date); $dao->deceased_date = CRM_Utils_Date::isoToMysql($dao->deceased_date); $dao->save(); } } }
/** * Function to delete the relationship * * @param int $id relationship id * * @return null * @access public * * @static */ static function del($id) { // delete from relationship table require_once 'CRM/Utils/Hook.php'; CRM_Utils_Hook::pre('delete', 'Relationship', $id, CRM_Core_DAO::$_nullArray); $relationship =& new CRM_Contact_DAO_Relationship(); $relationship->id = $id; $relationship->find(true); //to delete relationship between household and individual //or between individual and orgnization if ($relationship->relationship_type_id == 4 || $relationship->relationship_type_id == 7) { $sharedContact = new CRM_Contact_DAO_Contact(); $sharedContact->id = $relationship->contact_id_a; $sharedContact->find(true); if ($relationship->relationship_type_id == 4 && $relationship->contact_id_b == $sharedContact->employer_id) { $sharedContact->organization_name = 'NULL'; $sharedContact->employer_id = 'NULL'; $sharedContact->save(); } else { if ($sharedContact->mail_to_household_id == $relationship->contact_id_b) { self::deleteSharedAddress($relationship->contact_id_a); } } } if (CRM_Core_Permission::access('CiviMember')) { // create $params array which isrequired to delete memberships // of the related contacts. $params = array('relationship_type_id' => "{$relationship->relationship_type_id}_a_b", 'contact_check' => array($relationship->contact_id_b => 1)); $ids = array(); // calling relatedMemberships to delete the memberships of // related contacts. self::relatedMemberships($relationship->contact_id_a, $params, $ids, CRM_Core_Action::DELETE); } $relationship->delete(); CRM_Core_Session::setStatus(ts('Selected Relationship has been Deleted Successfuly.')); CRM_Utils_Hook::post('delete', 'Relationship', $relationship->id, $relationship); return $relationship; }