Skip to content

Commit

Permalink
Add Suspended Confidence mode (CO-2114) and duplication of Rules and …
Browse files Browse the repository at this point in the history
…Attributes (CO-2191)
  • Loading branch information
Benn Oshrin committed Aug 26, 2021
1 parent fba23ec commit 74ba508
Show file tree
Hide file tree
Showing 10 changed files with 150 additions and 15 deletions.
1 change: 1 addition & 0 deletions app/src/Controller/AttributesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public function isAuthorized(Array $user) {
$p = [
'add' => $platformAdmin || $mgAdmin,
'delete' => $platformAdmin || $mgAdmin,
'duplicate' => $platformAdmin || $mgAdmin,
'edit' => $platformAdmin || $mgAdmin,
'index' => $platformAdmin || $mgAdmin,
'view' => false
Expand Down
1 change: 1 addition & 0 deletions app/src/Controller/RulesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public function isAuthorized(Array $user) {
$p = [
'add' => $platformAdmin || $mgAdmin,
'delete' => $platformAdmin || $mgAdmin,
'duplicate' => $platformAdmin || $mgAdmin,
'edit' => $platformAdmin || $mgAdmin,
'index' => $platformAdmin || $mgAdmin,
'view' => false
Expand Down
80 changes: 80 additions & 0 deletions app/src/Controller/StandardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,86 @@ public function delete($id) {
return $this->generateRedirect();
}

/**
* Handle a duplicate action for a Standard object.
*
* @since COmanage Match v1.0.0
* @param Integer $id Object ID
*/

public function duplicate($id) {
// $this->name = Models (ie: from ModelsTable)
$modelsName = $this->name;
$associatedModels = [];

$query = $this->$modelsName->findById($id);

// AssociationTrait
if(method_exists($this->$modelsName, "getDuplicateContains")) {
$query = $query->contain($this->$modelsName->getDuplicateContains());
$associatedModels = $this->$modelsName->getDuplicateContains();
}

// Make sure the requested object exists
try {
$obj = $query->firstOrFail();

// Create a new entity. We disable validation in case the validation rules
// changed after the original object was created.
$newObj = $this->$modelsName->newEntity($obj->toArray(), ['validate' => false]);

// The object ID is cleared by newEntity, but not the timestamps
unset($newObj->created);
unset($newObj->modified);

foreach($associatedModels as $am) {
$at = \Cake\Utility\Inflector::tableize($am);

if(!empty($newObj->$at)) {
foreach($newObj->$at as $ao) {
unset($ao->created);
unset($ao->modified);
}
}
}

// Edit the name of the parent object (but we don't rename related models)
if(!empty($newObj->name)) {
$newObj->name = __('match.fd.copy_of', [$newObj->name]);
}

if($this->$modelsName->save($newObj)) {
// Use the display field to generate the flash message

$field = $this->$modelsName->getDisplayField();

if(!empty($newObj->$field)) {
$this->Flash->success(__('match.rs.duplicated.a', [$newObj->$field]));
} else {
$this->Flash->success(__('match.rs.duplicated'));
}

// Redirect to edit view
$redirect = [
'action' => 'edit',
$newObj->id
];

return $this->redirect($redirect);
} else {
// It's hard to get a specific failure reason to render...
$this->Flash->error(__('match.er.duplicate'));
}
}
catch(\Exception $e) {
// findById throws Cake\Datasource\Exception\RecordNotFoundException

$this->Flash->error($e->getMessage());
}

return $this->generateRedirect();
}

/**
* Handle an edit action for a Standard object.
*
Expand Down
3 changes: 2 additions & 1 deletion app/src/Lib/Enum/ConfidenceModeEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @link http://www.internet2.edu/comanage COmanage Project
* @link https://www.internet2.edu/comanage COmanage Project
* @package match
* @since COmanage Match v1.0.0
* @license Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
Expand All @@ -32,4 +32,5 @@
class ConfidenceModeEnum extends StandardEnum {
const Canonical = 'C';
const Potential = 'P';
const Suspended = 'S';
}
31 changes: 28 additions & 3 deletions app/src/Lib/Traits/AssociationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,28 @@
namespace App\Lib\Traits;

trait AssociationTrait {
// Array of associated models to copy during a duplicate
private $duplicateContains = false;

// Array of associated models to pull during an edit
private $editContains = null;
private $editContains = false;

// Array of associated models to save during a patch
private $patchAssociated = null;
private $patchAssociated = false;

// Array of associated models to pull during a view
private $viewContains = null;
private $viewContains = false;

/**
* Obtain the set of associated models to copy during a duplicate.
*
* @since COmanage Match v1.0.0
* @return array Array of associated models
*/

public function getDuplicateContains() {
return $this->duplicateContains;
}

/**
* Obtain the set of associated models to pull during an edit.
Expand Down Expand Up @@ -72,6 +86,17 @@ public function getViewContains() {
return $this->viewContains;
}

/**
* Set the associated models to copy during a duplicate.
*
* @since COmanage Match v1.0.0
* @param array $c Array of associated models
*/

public function setDuplicateContains(array $c) {
$this->duplicateContains = $c;
}

/**
* Set the associated models to pull during an edit.
*
Expand Down
2 changes: 1 addition & 1 deletion app/src/Lib/Traits/PrimaryLinkTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ trait PrimaryLinkTrait {
private $unkeyedActions = ['add', 'index'];

// Actions where the primary link can be obtained by looking up the record ID
private $lookupActions = ['delete', 'edit', 'view'];
private $lookupActions = ['delete', 'duplicate', 'edit', 'view'];

/**
* Whether the primary link is permitted to be empty.
Expand Down
18 changes: 18 additions & 0 deletions app/src/Locale/en_US/default.po
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ msgstr "Canonical"
msgid "match.en.ConfidenceModeEnum.P"
msgstr "Potential"

msgid "match.en.ConfidenceModeEnum.S"
msgstr "Suspended"

msgid "match.en.PermissionEnum.A"
msgstr "Platform Administrator"

Expand Down Expand Up @@ -222,6 +225,9 @@ msgstr "Possibly failed to update database schema"
msgid "match.er.delete"
msgstr "Delete Failed"

msgid "match.er.duplicate"
msgstr "Duplicate Failed"

msgid "match.er.file"
msgstr "Cannot read file {0}"

Expand Down Expand Up @@ -351,6 +357,9 @@ msgstr "Case Sensitive"
msgid "match.fd.confidence_mode"
msgstr "Confidence Mode"

msgid "match.fd.copy_of"
msgstr "Copy of {0}"

msgid "match.fd.description"
msgstr "Description"

Expand Down Expand Up @@ -504,6 +513,9 @@ msgstr "Are you sure you wish to delete this record ({0})?"
msgid "match.op.display"
msgstr "Display"

msgid "match.op.duplicate"
msgstr "Duplicate"

msgid "match.op.edit"
msgstr "Edit"

Expand Down Expand Up @@ -583,6 +595,12 @@ msgstr "Deleted"
msgid "match.rs.deleted.a"
msgstr "{0} Deleted"

msgid "match.rs.duplicated"
msgstr "Duplicated"

msgid "match.rs.duplicated.a"
msgstr "{0} Duplicated"

msgid "match.rs.pending"
msgstr "{0,plural,=1{# Pending Match} other{# Pending Matches}}"

Expand Down
14 changes: 4 additions & 10 deletions app/src/Model/Table/RulesTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,13 @@ public function initialize(array $config) {

$this->setDisplayField('name');

$this->setEditContains(['RuleAttributes']);
// During a save, also save RuleAttributes
$this->setPatchAssociated(['RuleAttributes']);
// AssociationTrait
$this->setDuplicateContains(['RuleAttributes']);

$this->setPrimaryLink('matchgrid_id');
$this->setRequiresMatchgrid(true);

$this->setAutoViewVars([
'attributes' => [
'type' => 'auxiliary',
'model' => 'Attributes',
'find' => 'filterPrimaryLink',
'order' => ['name' => 'ASC']
],
'confidenceModes' => [
'type' => 'enum',
'class' => 'ConfidenceModeEnum'
Expand Down Expand Up @@ -113,7 +106,8 @@ public function validationDefault(Validator $validator) {
'content',
[ 'rule' => [ 'inList', [
ConfidenceModeEnum::Canonical,
ConfidenceModeEnum::Potential
ConfidenceModeEnum::Potential,
ConfidenceModeEnum::Suspended
] ] ]
);
$validator->notEmpty('confidence_mode');
Expand Down
7 changes: 7 additions & 0 deletions app/src/Template/Element/javascript.ctp
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,13 @@
text: true
});

$(".copybutton").button({
icons: {
primary: 'ui-icon-copy'
},
text: true
});

$(".deletebutton").button({
icons: {
primary: 'ui-icon-circle-close'
Expand Down
8 changes: 8 additions & 0 deletions app/src/Template/Standard/index.ctp
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,14 @@ function _column_key($modelsName, $c, $tz=null) {
);
}

if(isset($vv_permissions['duplicate']) && $vv_permissions['duplicate']) {
print $this->Html->link(
__($product.'.op.duplicate'),
array_merge(['action' => 'duplicate'], $linkArgs),
['class' => 'copybutton']
);
}

if($vv_permissions['delete']) {
// XXX this is throwing CSRF error even though delete button on edit-record page is working?
// probably because this is using Form helper, but we're outside of a form?
Expand Down

0 comments on commit 74ba508

Please sign in to comment.