Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DQT] add delete feature #8078

Merged
merged 23 commits into from Mar 17, 2023
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 43 additions & 0 deletions modules/dqt/ajax/DeleteDoc.php
@@ -0,0 +1,43 @@
<?php
maltheism marked this conversation as resolved.
Show resolved Hide resolved
kongtiaowang marked this conversation as resolved.
Show resolved Hide resolved
/**
* Data Querying Module
*
* PHP Version 5
*
* @category Data_Querying_Module
* @package Loris
* @author Loris Team <loris-dev@bic.mni.mcgill.ca>
* @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3
* @link https://www.github.com/aces/Loris/
*/
// ########################## IBIS OVERRIDE START #######################
kongtiaowang marked this conversation as resolved.
Show resolved Hide resolved
require_once __DIR__ . '/../../../../vendor/autoload.php';
$client = new NDB_Client();
$client->makeCommandLine();
$client->initialize(__DIR__ . "/../../../../project/config.xml");
// ########################## IBIS OVERRIDE END #######################
header("Content-Type: application/json");
$config = \NDB_Config::singleton();
$couchConfig = $config->getSetting('CouchDB');
$cdb = \NDB_Factory::singleton()->couchDB(
$couchConfig['dbName'],
$couchConfig['hostname'],
intval($couchConfig['port']),
$couchConfig['admin'],
$couchConfig['adminpass']
);
$is_author = false;
$docID = urlencode($_REQUEST['DocID']);
$user = User::singleton();
$tmp_author = explode("_", $docID);
$doc_author = str_replace("global:", '', $tmp_author[0]);
if ($doc_author == $user->getUsername()) {
$is_author = true;
}

if ($user->hasPermission('superuser') || $is_author) {
$results = $cdb->deleteDoc($docID);
print json_encode($results);
} else {
print json_encode("false");
kongtiaowang marked this conversation as resolved.
Show resolved Hide resolved
}
2 changes: 1 addition & 1 deletion modules/dqt/ajax/saveQuery.php
Expand Up @@ -69,7 +69,7 @@
': ' .
$_REQUEST['QueryName'];
}
$fields = json_decode($_REQUEST['Fields']);
$fields = json_decode(strval($_REQUEST['Fields']));
kongtiaowang marked this conversation as resolved.
Show resolved Hide resolved
$cond = $_REQUEST['Filters'];
$baseDocument['Conditions'] = $cond;
$baseDocument['Fields'] = $fields;
Expand Down
2 changes: 1 addition & 1 deletion modules/dqt/css/dataquery.css
Expand Up @@ -556,7 +556,7 @@ tr.statsReport:nth-child(even) {
}
.tableFieldsCell {
overflow-x: scroll;
max-width: 400px;
max-width: 280px;
min-height: 100px;
}
.tableFiltersCell {
Expand Down
2 changes: 2 additions & 0 deletions modules/dqt/jsx/react.app.js
Expand Up @@ -40,6 +40,7 @@ class DataQueryApp extends Component {
queryIDs: {
user: [],
shared: [],
author: [],
},
savedQueries: {},
queriesLoaded: false,
Expand Down Expand Up @@ -1325,6 +1326,7 @@ class DataQueryApp extends Component {
<SavedQueriesList
userQueries={this.state.queryIDs.user}
globalQueries={this.state.queryIDs.shared}
author={this.state.queryIDs.author}
queryDetails={this.state.savedQueries}
queriesLoaded={this.state.queriesLoaded}
onSelectQuery={this.loadSavedQuery}
Expand Down
55 changes: 52 additions & 3 deletions modules/dqt/jsx/react.savedqueries.js
Expand Up @@ -4,6 +4,7 @@
*/
import React, {useEffect, useState} from 'react';
import PropTypes from 'prop-types';
import swal from 'sweetalert2';

const ManageSavedQueryFilters = (props) => {
const [content, setContent] = useState(null);
Expand Down Expand Up @@ -70,7 +71,35 @@ const ManageSavedQueryFilters = (props) => {
const ManageSavedQueryRow = (props) => {
const [fieldsVisible, setFields] = useState(null);
const [filtersVisible, setFilters] = useState(null);

/**
* @deleteclick
*/
function publicquerydelete() {
const id = props.Query['_id'];
swal.fire({
title: 'Are you sure?',
text: 'You won\'t be able to revert this!',
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, delete it!',
}).then((result) => {
if (result.value) {
let deleteurl = loris.BaseURL +
'/AjaxHelper.php?Module=dqt&script=DeleteDoc.php&DocID='
+ encodeURIComponent(id);
fetch(deleteurl, {
cache: 'no-cache',
credentials: 'same-origin',
}).then((resp) => resp.json())
kongtiaowang marked this conversation as resolved.
Show resolved Hide resolved
.then(()=>{
location.reload();
swal.fire('delete Successful!', '', 'success');
});
}
});
};
useEffect(() => {
let fields = [];
let filters = [];
Expand Down Expand Up @@ -157,7 +186,21 @@ const ManageSavedQueryRow = (props) => {
setFilters(filters);
setFields(fields);
}, []);

let docName = props.Query.Meta['name'];
let docAuthor = docName.substring(0, docName.lastIndexOf(':'));
let btn = '';
if (props.author == docAuthor) {
btn = (
<button className='btn btn-danger'
onClick={()=> { // eslint-disable-line
publicquerydelete(); // eslint-disable-line
} // eslint-disable-line
} // eslint-disable-line
>
delete
</button>
);
}
return (
<tr>
<td>
Expand All @@ -175,6 +218,11 @@ const ManageSavedQueryRow = (props) => {
{filtersVisible}
</div>
</td>
<td>
<div className={'tableNameCell'}>
{btn}
</div>
</td>
</tr>
);
};
Expand All @@ -199,7 +247,6 @@ const SavedQueriesList = (props) => {
props.queryDetails[queryName].Conditions
);
};

if (props.queriesLoaded === false) {
return null;
}
Expand All @@ -222,6 +269,7 @@ const SavedQueriesList = (props) => {
<ManageSavedQueryRow key={name}
Name={queryName}
Query={query}
author={props.author}
/>
);
}
Expand All @@ -247,6 +295,7 @@ const SavedQueriesList = (props) => {
<th>Query Name</th>
<th>Fields</th>
<th>Filters</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
Expand Down
41 changes: 41 additions & 0 deletions modules/dqt/jsx/react.tabs.js
Expand Up @@ -11,6 +11,8 @@
import React, {Component, useState} from 'react';
import PropTypes from 'prop-types';
import StaticDataTable from '../../../jsx/StaticDataTable';
import swal from 'sweetalert2';

const {jStat} = require('jstat');

/**
Expand Down Expand Up @@ -1116,6 +1118,35 @@ class ManageSavedQueryRow extends Component {
super(props);
this.state = {};
}
/**
* @deleteclick
*/
deleteclick() {
let id = this.props.Query['_id'];
swal.fire({
title: 'Are you sure?',
text: 'You won\'t be able to revert this!',
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, delete it!',
}).then((result) => {
if (result.value) {
let deleteurl = loris.BaseURL +
'/AjaxHelper.php?Module=dqt&script=DeleteDoc.php&DocID='
+ encodeURIComponent(id);
fetch(deleteurl, {
cache: 'no-cache',
credentials: 'same-origin',
}).then((resp) => resp.json())
kongtiaowang marked this conversation as resolved.
Show resolved Hide resolved
.then(()=>{
location.reload();
swal.fire('delete Successful!', '', 'success');
});
}
});
}

/**
* Renders the React component.
Expand Down Expand Up @@ -1224,6 +1255,15 @@ class ManageSavedQueryRow extends Component {
{filters}
</div>
</td>
<td>
<div className={'tableNamesCell'}>
<button className='btn btn-danger'
onClick={()=> {this.deleteclick();}}
>
delete
</button>
</div>
</td>
</tr>
);
}
Expand Down Expand Up @@ -1296,6 +1336,7 @@ let ManageSavedQueriesTabPane = (props) => {
<th>Query Name</th>
<th>Fields</th>
<th>Filters</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
Expand Down
6 changes: 3 additions & 3 deletions modules/dqt/php/dqt_setup.class.inc
Expand Up @@ -164,12 +164,12 @@ class Dqt_Setup extends \NDB_Form implements RequestHandlerInterface
return $row['id'];
};

$usersavedNames = array_map($IDMapCallback, $usersaved);
$globalsavedNames = array_map($IDMapCallback, $globalsaved);

$usersavedNames = array_map($IDMapCallback, $usersaved);
$globalsavedNames = array_map($IDMapCallback, $globalsaved);
$data['savedqueries'] = [
'user' => $usersavedNames,
'shared' => $globalsavedNames,
'author' => $user->getUsername(),
];
$data['visits'] = \Utility::getVisitList();
// Note: StringStream since BinaryStream isn't in 23.0-release.
Expand Down
46 changes: 46 additions & 0 deletions tools/dqt_delete_saved_query.php
@@ -0,0 +1,46 @@
<?php
/**
* Data Querying Module
*
* PHP Version 7.4
*
* @category Data_Querying_Module
* @package Loris
* @author Loris Team <loris-dev@bic.mni.mcgill.ca>
* @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3
* @link https://www.github.com/aces/Loris/
*/
require_once __DIR__ . '/../vendor/autoload.php';
$client = new NDB_Client();
$client->makeCommandLine();
$client->initialize(__DIR__ . "/../project/config.xml");
header("Content-Type: application/json");
kongtiaowang marked this conversation as resolved.
Show resolved Hide resolved
$config = \NDB_Config::singleton();
$couchConfig = $config->getSetting('CouchDB');
$cdb = \NDB_Factory::singleton()->couchDB(
$couchConfig['dbName'],
$couchConfig['hostname'],
intval($couchConfig['port']),
$couchConfig['admin'],
$couchConfig['adminpass']
);
echo "Deleting a saved query in DQT.\n";

$user = readline("Please input the author of the saved query:");
$name = readline("Please input the name of the saved query:");
$global = readline("If the saved query is global then input 'y':");
if ($global) {
$docID = urlencode("global:".$user."_".$name);
} else {
$docID = urlencode($user."_".$name);
}
$results = $cdb->deleteDoc(
$docID
);


if (json_encode($results) == "true") {
echo $name." has been deleted in DQT.\n";
} else {
echo "There is no query named ".$name." in DQT.\n";
};