Eileen has uploaded a new change for review.
https://gerrit.wikimedia.org/r/282086
Change subject: CRM-18332 add first cut of api for revert actions
......................................................................
CRM-18332 add first cut of api for revert actions
Later I expect to add more parameters & deal with situation when log_date is
not passed
Change-Id: Ib3bc639d12f13a513b52874c868742d2e1560fb9
---
M CRM/Logging/Differ.php
M CRM/Logging/ReportDetail.php
M CRM/Logging/Reverter.php
M CRM/Logging/Schema.php
A api/v3/Logging.php
A api/v3/examples/Logging/Revert.php
6 files changed, 188 insertions(+), 9 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/wikimedia/fundraising/crm/civicrm
refs/changes/86/282086/1
diff --git a/CRM/Logging/Differ.php b/CRM/Logging/Differ.php
index e98aeb5..38b3947 100644
--- a/CRM/Logging/Differ.php
+++ b/CRM/Logging/Differ.php
@@ -388,4 +388,33 @@
return array($titles, $values);
}
+ /**
+ * Get all changes made in the connection.
+ *
+ * @param array $tables
+ * Array of tables to inspect.
+ *
+ * @return array
+ */
+ public function getAllChangesForConnection($tables) {
+ $params = array(1 => array($this->log_conn_id, 'String'));
+ foreach ($tables as $table) {
+ if (empty($sql)) {
+ $sql = " SELECT '{$table}' as table_name, id FROM
{$this->db}.log_{$table} WHERE log_conn_id = %1";
+ }
+ else {
+ $sql .= " UNION SELECT '{$table}' as table_name, id FROM
{$this->db}.log_{$table} WHERE log_conn_id = %1";
+ }
+ }
+ $diffs = array();
+ $dao = CRM_Core_DAO::executeQuery($sql, $params);
+ while ($dao->fetch()) {
+ if (empty($this->log_date)) {
+ $this->log_date = CRM_Core_DAO::singleValueQuery("SELECT log_date FROM
{$this->db}.log_{$table} WHERE log_conn_id = %1 LIMIT 1", $params);
+ }
+ $diffs = array_merge($diffs, $this->diffsInTableForId($dao->table_name,
$dao->id));
+ }
+ return $diffs;
+ }
+
}
diff --git a/CRM/Logging/ReportDetail.php b/CRM/Logging/ReportDetail.php
index 20a5290..1211ac7 100644
--- a/CRM/Logging/ReportDetail.php
+++ b/CRM/Logging/ReportDetail.php
@@ -237,9 +237,9 @@
$this->altered_name = CRM_Utils_Request::retrieve('alteredName', 'String',
CRM_Core_DAO::$_nullObject);
$this->altered_by = CRM_Utils_Request::retrieve('alteredBy', 'String',
CRM_Core_DAO::$_nullObject);
$this->altered_by_id = CRM_Utils_Request::retrieve('alteredById',
'Integer', CRM_Core_DAO::$_nullObject);
- }
+ }
- /**
+ /**
* Store the dsn for the logging database in $this->db.
*/
protected function setDB() {
@@ -252,7 +252,8 @@
*/
protected function revert() {
$reverter = new CRM_Logging_Reverter($this->log_conn_id, $this->log_date);
- $reverter->revert($this->tables);
+ $reverter->calculateDiffsFromLogConnAndDate($this->tables);
+ $reverter->revert();
CRM_Core_Session::setStatus(ts('The changes have been reverted.'),
ts('Reverted'), 'success');
if ($this->cid) {
CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contact/view',
"reset=1&selectedChild=log&cid={$this->cid}", FALSE, NULL, FALSE));
diff --git a/CRM/Logging/Reverter.php b/CRM/Logging/Reverter.php
index 411b823..e885cf5 100644
--- a/CRM/Logging/Reverter.php
+++ b/CRM/Logging/Reverter.php
@@ -36,6 +36,13 @@
private $log_date;
/**
+ * The diffs to be reverted.
+ *
+ * @var array
+ */
+ private $diffs = array();
+
+ /**
* Class constructor.
*
* @param string $log_conn_id
@@ -49,11 +56,24 @@
}
/**
- * Revert changes in the array of diffs in $this->diffs.
*
- * @param $tables
+ * Calculate a set of diffs based on the connection_id and changes at a
close time.
+ *
+ * @param array $tables
*/
- public function revert($tables) {
+ public function calculateDiffsFromLogConnAndDate($tables) {
+ $differ = new CRM_Logging_Differ($this->log_conn_id, $this->log_date);
+ $this->diffs = $differ->diffsInTables($tables);
+ }
+
+ public function setDiffs($diffs) {
+ $this->diffs = $diffs;
+ }
+
+ /**
+ * Revert changes in the array of diffs in $this->diffs.
+ */
+ public function revert() {
// get custom data tables, columns and types
$ctypes = array();
@@ -65,9 +85,7 @@
$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);
-
+ $diffs = $this->diffs;
$deletes = array();
$reverts = array();
foreach ($diffs as $table => $changes) {
diff --git a/CRM/Logging/Schema.php b/CRM/Logging/Schema.php
index cca99a7..308dc83 100644
--- a/CRM/Logging/Schema.php
+++ b/CRM/Logging/Schema.php
@@ -893,4 +893,15 @@
}
}
+ /**
+ * Get all the log tables that reference civicrm_contact.
+ *
+ * Note that it might make sense to wrap this in a getLogTablesForEntity
+ * but this is the only entity currently available...
+ */
+ public function getLogTablesForContact() {
+ $tables = array_keys(CRM_Dedupe_Merger::cidRefs());
+ return array_intersect($tables, $this->tables);
+ }
+
}
diff --git a/api/v3/Logging.php b/api/v3/Logging.php
new file mode 100644
index 0000000..fc2890a
--- /dev/null
+++ b/api/v3/Logging.php
@@ -0,0 +1,50 @@
+<?php
+/*
+ +--------------------------------------------------------------------+
+ | CiviCRM version 4.7 |
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC (c) 2004-2015 |
+ +--------------------------------------------------------------------+
+ | This file is a part of CiviCRM. |
+ | |
+ | CiviCRM is free software; you can copy, modify, and distribute it |
+ | under the terms of the GNU Affero General Public License |
+ | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
+ | |
+ | CiviCRM is distributed in the hope that it will be useful, but |
+ | WITHOUT ANY WARRANTY; without even the implied warranty of |
+ | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
+ | See the GNU Affero General Public License for more details. |
+ | |
+ | You should have received a copy of the GNU Affero General Public |
+ | License and the CiviCRM Licensing Exception along |
+ | with this program; if not, contact CiviCRM LLC |
+ | at info[AT]civicrm[DOT]org. If you have questions about the |
+ | GNU Affero General Public License or the licensing of CiviCRM, |
+ | see the CiviCRM license FAQ at http://civicrm.org/licensing |
+ +--------------------------------------------------------------------+
+ */
+
+/**
+ * This api exposes functionality for interacting with the logging
functionality.
+ *
+ * @package CiviCRM_APIv3
+ */
+
+/**
+ * Revert a log change.
+ *
+ * @param array $params
+ *
+ * @return array
+ * API Success Array
+ * @throws \API_Exception
+ * @throws \Civi\API\Exception\UnauthorizedException
+ */
+function civicrm_api3_logging_revert($params) {
+ $schema = new CRM_Logging_Schema();
+ $reverter = new CRM_Logging_Reverter($params['log_conn_id'],
$params['log_date']);
+
$reverter->calculateDiffsFromLogConnAndDate($schema->getLogTablesForContact());
+ $reverter->revert();
+ return civicrm_api3_create_success(1);
+}
diff --git a/api/v3/examples/Logging/Revert.php
b/api/v3/examples/Logging/Revert.php
new file mode 100644
index 0000000..fcdf924
--- /dev/null
+++ b/api/v3/examples/Logging/Revert.php
@@ -0,0 +1,70 @@
+<?php
+/**
+ * Test Generated example demonstrating the Logging.revert API.
+ *
+ * @return array
+ * API result array
+ */
+function logging_revert_example() {
+ $params = array(
+ 'log_conn_id' => 'woot',
+ 'log_date' => '2016-04-05 23:52:59',
+ );
+
+ try{
+ $result = civicrm_api3('Logging', 'revert', $params);
+ }
+ catch (CiviCRM_API3_Exception $e) {
+ // Handle error here.
+ $errorMessage = $e->getMessage();
+ $errorCode = $e->getErrorCode();
+ $errorData = $e->getExtraParams();
+ return array(
+ 'error' => $errorMessage,
+ 'error_code' => $errorCode,
+ 'error_data' => $errorData,
+ );
+ }
+
+ return $result;
+}
+
+/**
+ * Function returns array of result expected from previous function.
+ *
+ * @return array
+ * API result array
+ */
+function logging_revert_expectedresult() {
+
+ $expectedResult = array(
+ 'is_error' => 0,
+ 'version' => 3,
+ 'count' => 1,
+ 'values' => 1,
+ );
+
+ return $expectedResult;
+}
+
+/*
+* This example has been generated from the API test suite.
+* The test that created it is called
"/Users/emcnaughton/buildkit/build/dmaster/sites/all/modules/civicrm/tests/phpunit/api/v3/LoggingTest.php"
+* and can be found at:
+*
https://github.com/civicrm/civicrm-core/blob/master/tests/phpunit/api/v3/Revert
+*
+* You can see the outcome of the API tests at
+* https://test.civicrm.org/job/CiviCRM-master-git/
+*
+* To Learn about the API read
+* http://wiki.civicrm.org/confluence/display/CRMDOC/Using+the+API
+*
+* Browse the api on your own site with the api explorer
+* http://MYSITE.ORG/path/to/civicrm/api
+*
+* Read more about testing here
+* http://wiki.civicrm.org/confluence/display/CRM/Testing
+*
+* API Standards documentation:
+* http://wiki.civicrm.org/confluence/display/CRM/API+Architecture+Standards
+*/
--
To view, visit https://gerrit.wikimedia.org/r/282086
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib3bc639d12f13a513b52874c868742d2e1560fb9
Gerrit-PatchSet: 1
Gerrit-Project: wikimedia/fundraising/crm/civicrm
Gerrit-Branch: master
Gerrit-Owner: Eileen <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits