Skip to content

Commit

Permalink
Initial pagination commit and other miscellanea
Browse files Browse the repository at this point in the history
  • Loading branch information
Benn Oshrin committed Oct 13, 2021
1 parent 5aa9df2 commit be224b8
Show file tree
Hide file tree
Showing 10 changed files with 313 additions and 18 deletions.
42 changes: 36 additions & 6 deletions app/resources/locales/en_US/default.po
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,12 @@ msgstr "{0} not found"
msgid "registry.er.notprov"
msgstr "{0} not provided"

msgid "registry.er.pagenum.exceeded"
msgstr "Page number may not be larger than {0}"

msgid "registry.er.pagenum.nan"
msgstr "Page number must be an integer"

msgid "registry.er.perm"
msgstr "Permission Denied"

Expand Down Expand Up @@ -347,16 +353,19 @@ msgstr "API Users created in the COmanage CO are given full privileges to all Re
msgid "registry.in.api.key"
msgstr "This newly generated API Key cannot be recovered. If it is lost a new key must be generated."

### Menu Messages
msgid "registry.me.co.switch"
msgstr "Switch Collaboration"

msgid "registry.me.co.co_people"
msgstr "CO People"
msgid "registry.in.pagination.format"
msgstr "Page {{page}} of {{pages}}, Viewing {{start}}-{{end}} of {{count}}"

### Menu Messages
msgid "registry.me.co.configuration"
msgstr "Configuration"

msgid "registry.me.co.people"
msgstr "People"

msgid "registry.me.co.switch"
msgstr "Switch Collaboration"

### Operations (Commands)
msgid "registry.op.add.a"
msgstr "Add New {0}"
Expand Down Expand Up @@ -385,12 +394,33 @@ msgstr "Edit"
msgid "registry.op.edit.a"
msgstr "Edit {0}"

msgid "registry.op.first"
msgstr "First"

msgid "registry.op.go"
msgstr "Go"

msgid "registry.op.last"
msgstr "Last"

msgid "registry.op.login"
msgstr "Login"

msgid "registry.op.logout"
msgstr "Logout"

msgid "registry.op.next"
msgstr "Next"

msgid "registry.op.page.display"
msgstr "Display"

msgid "registry.op.page.goto"
msgstr "Go To Page"

msgid "registry.op.previous"
msgstr "Previous"

msgid "registry.op.save"
msgstr "Save"

Expand Down
6 changes: 6 additions & 0 deletions app/src/Controller/ApiUsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ class ApiUsersController extends StandardController {
]
];

public $pagination = [
'order' => [
'ApiUsers.username' => 'asc'
]
];

/**
* Generate a new API Key.
*
Expand Down
6 changes: 6 additions & 0 deletions app/src/Controller/CosController.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ class CosController extends StandardController {
]
];

public $pagination = [
'order' => [
'Cos.name' => 'asc'
]
];

/*
* XXX implement, also REST API
*
Expand Down
6 changes: 6 additions & 0 deletions app/src/Controller/CousController.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ class CousController extends StandardController {
]
];

public $pagination = [
'order' => [
'Cous.name' => 'asc'
]
];

/**
* Callback run prior to the request render.
*
Expand Down
31 changes: 23 additions & 8 deletions app/src/Controller/StandardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
use \Cake\Http\Exception\BadRequestException;

class StandardController extends AppController {
// Pagination defaults should be set in each controller
public $pagination = [];

/**
* Handle an add action for a Standard object.
*
Expand Down Expand Up @@ -298,11 +301,10 @@ public function edit($id) {
// query modifications via traits
$query = $table->findById($id);

// AssociationTrait
/*
if(method_exists($table, "getEditContains")) {
$query = $query->contain($table->getEditContains());
}*/
// QueryModificationTrait
if(method_exists($this->$modelsName, "getEditContains")) {
$query = $query->contain($this->$modelsName->getEditContains());
}

try {
// Pull the current record
Expand Down Expand Up @@ -478,7 +480,10 @@ public function index() {
$query->contain($table->getIndexContains());
}

$resultSet = $this->Paginator->paginate($query);
// The Cake documents describe $this->paginate (which worked in Cake 2),
// but it doesn't seem to work in Cake 4. So we just use $this->pagination
// ourselves here.
$resultSet = $this->Paginator->paginate($query, $this->pagination);

$this->set($tableName, $resultSet);
$this->set('vv_permission_set', $this->RegistryAuth->calculatePermissionsForResultSet($resultSet));
Expand Down Expand Up @@ -517,7 +522,12 @@ protected function populateAutoViewVars(object $obj=null) {
break;
// "auxiliary" and "select" do basically the same thing, but the former
// returns the full object and the latter just returns a hash suitable
// for a select
// for a select. "type" is a shorthand for "select" for type_id.
case 'type':
// Inject configuration
$avv['model'] = 'Types';
// We assume the model using type_id has a primary link of co_id
$avv['find'] = 'filterPrimaryLink';
case 'auxiliary':
// XXX add list as in match?
case 'select':
Expand Down Expand Up @@ -553,7 +563,7 @@ protected function populateAutoViewVars(object $obj=null) {
// to PrimaryLinkTrait and call it there?
if($v) {
$query = $query->find($avv['find'], [$linkFilter => $v]);
$query = $query->where([$linkFilter => $v]);
}
}
} else {
Expand All @@ -562,6 +572,11 @@ protected function populateAutoViewVars(object $obj=null) {
}
}

if(!empty($avv['where'])) {
// Filter on the specified clause (of the form [column=>value])
$query = $query->where($avv['where']);
}

$this->set($vvar, $query->toArray());
break;
default:
Expand Down
100 changes: 100 additions & 0 deletions app/src/Lib/Traits/QueryModificationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,43 @@
namespace App\Lib\Traits;

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

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

// Containable models for index actions
private $indexContains = null;

// Array of associated models to save during a patch
private $patchAssociated = [];

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

/**
* Obtain the set of associated models to copy during a duplicate.
*
* @since COmanage Registry v5.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.
*
* @since COmanage Registry v5.0.0
* @return array Array of associated models
*/

public function getEditContains() {
return $this->editContains;
}

/**
* Containable models for index actions.
*
Expand All @@ -44,6 +78,50 @@ public function getIndexContains() {
return $this->indexContains;
}

/**
* Obtain the set of associated models to save during a patch.
*
* @since COmanage Registry v5.0.0
* @return array Array of associated models
*/

public function getPatchAssociated() {
return $this->patchAssociated;
}

/**
* Obtain the set of associated models to pull during a view.
*
* @since COmanage Registry v5.0.0
* @return array Array of associated models
*/

public function getViewContains() {
return $this->viewContains;
}

/**
* Set the associated models to copy during a duplicate.
*
* @since COmanage Registry v5.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.
*
* @since COmanage Registry v5.0.0
* @param array $c Array of associated models
*/

public function setEditContains(array $c) {
$this->editContains = $c;
}

/**
* Set containable models for index actions.
*
Expand All @@ -54,4 +132,26 @@ public function getIndexContains() {
public function setIndexContains(array $contains) {
$this->indexContains = $contains;
}

/**
* Set the associated models to save during a patch.
*
* @since COmanage Registry v5.0.0
* @param array $a Array of associated models
*/

public function setPatchAssociated(array $a) {
$this->patchAssociated = $a;
}

/**
* Set the associated models to pull during a view.
*
* @since COmanage Registry v5.0.0
* @param array $c Array of associated models
*/

public function setViewContains(array $c) {
$this->viewContains = $c;
}
}
6 changes: 4 additions & 2 deletions app/templates/ApiUsers/columns.inc
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ if($vv_cur_co->id == 1) {

$indexColumns = [
'username' => [
'type' => 'link'
'type' => 'link',
'sortable' => true
],
'status' => [
'type' => 'enum',
'class' => 'SuspendableStatusEnum'
'class' => 'SuspendableStatusEnum',
'sortable' => true
],
'api_key' => [
'type' => 'boolean',
Expand Down
7 changes: 7 additions & 0 deletions app/templates/Standard/add-edit-view.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@

include(ROOT . DS . "templates" . DS . $modelsName . DS . $fieldsFile);

if(!empty($hidden)) {
// Inject any hidden variables set by the include file
foreach($hidden as $attr => $v) {
print $this->Form->hidden($attr, ['value' => $v]);
}
}

if($vv_action == 'add' || $vv_action == 'edit') {
// We don't want/need to output these for view actions

Expand Down
21 changes: 19 additions & 2 deletions app/templates/Standard/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,21 @@ function _column_key($modelsName, $c, $tz=null) {
<table id="<?= $tableName . '-table'; ?>">
<tr>
<?php foreach($indexColumns as $col => $cfg): ?>
<th><?= !empty($cfg['label']) ? $cfg['label'] : _column_key($modelsName, $col, $vv_tz); ?></th>
<th>
<?php
$label = !empty($cfg['label']) ? $cfg['label'] : _column_key($modelsName, $col, $vv_tz);

if(isset($cfg['sortable']) && $cfg['sortable']) {
if(is_string($cfg['sortable'])) {
print $this->Paginator->sort($cfg['sortable'], $label);
} else {
print $this->Paginator->sort($col, $label);
}
} else {
print $label;
}
?>
</th>
<?php endforeach; ?>
<th><?= __('registry.fd.action'); ?></th>
</tr>
Expand Down Expand Up @@ -268,4 +282,7 @@ function _column_key($modelsName, $c, $tz=null) {
</tr>
<?php endforeach; // $$tablename ?>
</table>
</div>
</div>

<?php
print $this->element("pagination");
Loading

0 comments on commit be224b8

Please sign in to comment.