Skip to content

Commit

Permalink
Utility that calculates the view and breadcrump title
Browse files Browse the repository at this point in the history
  • Loading branch information
Ioannis committed Jan 24, 2024
1 parent a557a28 commit c980a40
Show file tree
Hide file tree
Showing 9 changed files with 183 additions and 101 deletions.
6 changes: 0 additions & 6 deletions app/resources/locales/en_US/operation.po
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,6 @@ msgstr "Edit"
msgid "edit.a"
msgstr "Edit {0}"

msgid "edit.ai"
msgstr "Edit {0}"

msgid "ExternalIdentitySourceRecords.retrieve"
msgstr "Retrieve from External Identity Source"

Expand Down Expand Up @@ -186,6 +183,3 @@ msgstr "View"
msgid "view.a"
msgstr "View {0}"

msgid "view.ai"
msgstr "View {0}"

7 changes: 6 additions & 1 deletion app/src/Controller/ApiUsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@

namespace App\Controller;

use App\Lib\Util\StringUtilities;

class ApiUsersController extends StandardController {
public $paginate = [
'order' => [
Expand Down Expand Up @@ -56,7 +58,10 @@ public function generate(string $id) {

// Let the view render, but tell it to use a different fields file
$this->set('vv_fields_inc', 'fields-generate.inc');
$this->set('vv_title', __d('operation', 'api.key.generate'));
[$title, , ] = StringUtilities::entityAndActionToTitle(null,
'api.key',
$this->request->getParam('action'));
$this->set('vv_title', $title);

$this->render('/Standard/add-edit-view');
}
Expand Down
32 changes: 16 additions & 16 deletions app/src/Controller/Component/BreadcrumbComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,20 @@ public function injectPrimaryLink(object $link, bool $index=true, string $linkLa

// Construct the get<Request Action>Contains function name
$requestAction = $this->getController()->getRequest()->getParam('action');
// In the case we are dealing with non-standard actions we need to fallback to a standard one
// in order to get access to the contain array. We will use the permissions to decide which
// action to fallback to
if(!in_array($requestAction, [
'index', 'view', 'delete', 'add', 'edit'
])) {
$permissionsArray = $this->getController()->RegistryAuth->calculatePermissionsForView($requestAction);
$id = $this->getController()->getRequest()->getParam('pass')[0];
if (isset($id)) {
$requestAction = ( isset($permissionsArray['edit']) && $permissionsArray['edit'] ) ? 'edit' : 'view';
} else {
$requestAction = 'index';
}
}
$containsList = "get" . ucfirst($requestAction) . "Contains";

$linkTable = TableRegistry::getTableLocator()->get($modelPath);
Expand Down Expand Up @@ -226,21 +240,7 @@ public function injectPrimaryLink(object $link, bool $index=true, string $linkLa
'edit';

// The action in the following injectParents dictates the action here
// XXX This is a duplicate from StandardController.
if(method_exists($linkTable, 'generateDisplayField')) {
// We don't use a trait for this since each table will implement different logic

$label = __d('operation', "{$breadcrumbAction}.ai", $linkTable->generateDisplayField($linkObj));
} else {
// Default view title is edit object display field
$field = $linkTable->getDisplayField();

if(!empty($obj->$field)) {
$label = __d('operation', "{$breadcrumbAction}.ai", $obj->$field);
} else {
$label = __d('operation', "{$breadcrumbAction}.ai", __d('controller', $modelsName, [1]));
}
}
[$title,,] = StringUtilities::entityAndActionToTitle($linkObj, $modelPath, $breadcrumbAction);

$this->injectParents[ $linkTable->getTable() . $linkObj->id ] = [
'target' => [
Expand All @@ -249,7 +249,7 @@ public function injectPrimaryLink(object $link, bool $index=true, string $linkLa
'action' => $breadcrumbAction,
$linkObj->id
],
'label' => $linkLabel ?? $label
'label' => $linkLabel ?? $title
];
}

Expand Down
33 changes: 26 additions & 7 deletions app/src/Controller/DashboardsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
namespace App\Controller;

// XXX not doing anything with Log yet
use App\Lib\Util\StringUtilities;
use Cake\Log\Log;
use Cake\ORM\TableRegistry;
use Cake\Utility\Hash;
Expand Down Expand Up @@ -66,8 +67,12 @@ public function initialize(): void {

public function artifacts() {
$cur_co = $this->getCO();

$this->set('vv_title', __d('menu', 'artifacts', [$cur_co->name]));

[$title, , ] = StringUtilities::entityAndActionToTitle($cur_co,
'artifacts',
$this->request->getParam('action'),
'menu');
$this->set('vv_title', $title);

// Construct the set of primary registry objects, which
// we want to order by the localized text string.
Expand Down Expand Up @@ -102,8 +107,12 @@ public function artifacts() {

public function configuration() {
$cur_co = $this->getCO();

$this->set('vv_title', __d('operation', 'dashboard.configuration', $cur_co->name));

[$title, , ] = StringUtilities::entityAndActionToTitle($cur_co,
'dashboard',
$this->request->getParam('action'),
);
$this->set('vv_title', $title);

// Construct the set of configuration items. For everything except CO Settings
// we want to order by the localized text string.
Expand Down Expand Up @@ -213,8 +222,12 @@ public function dashboard(?int $id=null) {

public function registries() {
$cur_co = $this->getCO();

$this->set('vv_title', __d('menu', 'registries', [$cur_co->name]));

[$title, , ] = StringUtilities::entityAndActionToTitle($cur_co,
'registries',
$this->request->getParam('action'),
'menu');
$this->set('vv_title', $title);

// Construct the set of primary registry objects, which
// we want to order by the localized text string.
Expand Down Expand Up @@ -431,6 +444,12 @@ public function search() {
}

$this->set('vv_results', $results);
$this->set('vv_title', __d('result', 'search.results'));
// XXX The action is search and the result is not a modelPath. In this use the pattern is reversed. We should
// probably reconsider the po naming for the result domain
[$title, , ] = StringUtilities::entityAndActionToTitle(null,
'search',
'results',
'result');
$this->set('vv_title', $title);
}
}
31 changes: 19 additions & 12 deletions app/src/Controller/ExternalIdentitySourcesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
namespace App\Controller;

// XXX not doing anything with Log yet
use App\Lib\Util\StringUtilities;
use Cake\Log\Log;
use Cake\ORM\TableRegistry;

Expand Down Expand Up @@ -109,15 +110,19 @@ public function retrieve(string $id) {

$this->set('vv_eis_record', $this->ExternalIdentitySources->retrieve((int)$id, $source_key));

$this->set('vv_external_identity_record', $this->ExternalIdentitySources
->ExtIdentitySourceRecords
->find()
->where(['ExtIdentitySourceRecords.source_key' => $source_key,
'ExtIdentitySourceRecords.external_identity_source_id' => $id])
->contain(['ExternalIdentities'])
->first());

$this->set('vv_title', __d('operation', 'view.a', [$source_key]));
$externalIdentityRecordObj = $this->ExternalIdentitySources
->ExtIdentitySourceRecords
->find()
->where(['ExtIdentitySourceRecords.source_key' => $source_key,
'ExtIdentitySourceRecords.external_identity_source_id' => $id])
->contain(['ExternalIdentities'])
->first();
$this->set('vv_external_identity_record', $externalIdentityRecordObj);

[$title, , ] = StringUtilities::entityAndActionToTitle($externalIdentityRecordObj,
StringUtilities::entityToClassName($externalIdentityRecordObj),
'view');
$this->set('vv_title', $title);
}
catch(\Exception $e) {
$this->Flash->error($e->getMessage());
Expand Down Expand Up @@ -152,7 +157,10 @@ public function search(string $id) {

$this->set('vv_search_attrs', $this->ExternalIdentitySources->searchableAttributes((int)$id));

$this->set('vv_title', __d('operation', 'ExternalIdentitySources.search'));
[$title, , ] = StringUtilities::entityAndActionToTitle(null,
$this->getName(),
$this->request->getParam('action'));
$this->set('vv_title', $title);
}

/**
Expand All @@ -167,9 +175,8 @@ public function sync(string $id) {
$source_key = $this->request->getQuery('source_key');

$this->ExternalIdentitySources->sync((int)$id, $source_key);
// XXX Sync does not have a view. We do not need to set a title. Yet need to fetch the updated Identity
$this->set('vv_eis_record', $this->ExternalIdentitySources->retrieve((int)$id, $source_key));

$this->set('vv_title', __d('operation', 'view.a', [$source_key]));

$this->Flash->success(__d('result', 'ExternalIdentitySources.synced'));
}
Expand Down
20 changes: 13 additions & 7 deletions app/src/Controller/ProvisioningTargetsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
namespace App\Controller;

// XXX not doing anything with Log yet
use App\Lib\Util\StringUtilities;
use Cake\Utility\Inflector;
use Cake\Log\Log;
use Cake\ORM\TableRegistry;

Expand Down Expand Up @@ -85,17 +87,21 @@ public function status() {
// PrimaryLinkTrait - Look up our primary link to see which object type we're
// working with, an also get our CO ID
$link = $this->getPrimaryLink(true);

if($link->attr == 'person_id') {
$statuses = $this->ProvisioningTargets->status(coId: $link->co_id, personId: (int)$link->value);
} elseif($link->attr == 'group_id') {
$statuses = $this->ProvisioningTargets->status(coId: $link->co_id, groupId: (int)$link->value);
}
// Use argument unpacking operator with names parameters in order to make the call more dynamic
$statusCalculateParams = [
'coId' => $link->co_id,
// Currently supported function parameters are personId, groupId
Inflector::variable($link->attr) => (int)$link->value
];
$statuses = $this->ProvisioningTargets->status(...$statusCalculateParams);

$this->set('vv_provisioning_statuses', $statuses);

if(!$this->request->is('restful')) {
$this->set('vv_title', __d('operation', 'provisioning.status'));
[$title, , ] = StringUtilities::entityAndActionToTitle(null,
'provisioning',
$this->request->getParam('action'));
$this->set('vv_title', $title);
}
}
}
76 changes: 25 additions & 51 deletions app/src/Controller/StandardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public function add() {
$table = $this->$modelsName;
// $tableName = models
$tableName = $table->getTable();
// Create an empty entity for FormHelper
$obj = $table->newEmptyEntity();

if($this->request->is('post')) {
try {
Expand Down Expand Up @@ -100,14 +102,10 @@ public function add() {

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

// Pass $obj as context so the view can render validation errors
$this->set('vv_obj', $obj);
} else {
// Create an empty entity for FormHelper

$this->set('vv_obj', $table->newEmptyEntity());
}

// Pass $obj as context so the view can render validation errors
$this->set('vv_obj', $obj);

// PrimaryLinkTrait, via AppController
$this->getPrimaryLink();
Expand All @@ -116,13 +114,10 @@ public function add() {
$this->populateAutoViewVars();

// Default title is add new object
$this->set('vv_title', __d('operation', 'add.a', __d('controller', $modelsName, [1])));

// Supertitle is normally the display name of the parent object when subnavigation exists.
// Set this here as the fallback default. This value is overriden in MVEAController to hold the
// name of the parent object, not the model name of the current object.
// TODO: set this to a better value for other kinds of child objects (e.g. Group member)
$this->set('vv_supertitle', __d('controller', $modelsName, [1]));
[$title, $supertitle, $subtitle] = StringUtilities::entityAndActionToTitle($obj, $modelsName, 'add');
$this->set('vv_title', $title);
$this->set('vv_supertitle', $supertitle);
$this->set('vv_subtitle', $subtitle);

// Let the view render
$this->render('/Standard/add-edit-view');
Expand Down Expand Up @@ -411,24 +406,13 @@ public function edit(string $id) {

// AutoViewVarsTrait
$this->populateAutoViewVars($obj);

if(method_exists($table, 'generateDisplayField')) {
// We don't use a trait for this since each table will implement different logic

$this->set('vv_title', __d('operation', 'edit.ai', $table->generateDisplayField($obj)));
$this->set('vv_supertitle', $table->generateDisplayField($obj));
// Pass the display field also into subtitle for dealing with External IDs
$this->set('vv_subtitle', $table->generateDisplayField($obj));
} else {
// Default view title is edit object display field
$field = $table->getDisplayField();

if(!empty($obj->$field)) {
$this->set('vv_title', __d('operation', 'edit.ai', $obj->$field));
} else {
$this->set('vv_title', __d('operation', 'edit.ai', __d('controller', $modelsName, [1])));
}
}

// Calculate and set title, supertitle and subtitle
[$title, $supertitle, $subtitle] = StringUtilities::entityAndActionToTitle($obj, $modelsName, 'edit');

$this->set('vv_title', $title);
$this->set('vv_supertitle', $supertitle);
$this->set('vv_subtitle', $subtitle);

// Let the view render
$this->render('/Standard/add-edit-view');
Expand Down Expand Up @@ -646,7 +630,8 @@ public function index() {
$this->set('vv_permission_set', $this->RegistryAuth->calculatePermissionsForResultSet($resultSet));

// Default index view title is model name
$this->set('vv_title', __d('controller', $modelsName, [99]));
[$title, , ] = StringUtilities::entityAndActionToTitle($resultSet, $modelsName, 'index');
$this->set('vv_title', $title);

// Let the view render
$this->render('/Standard/index');
Expand Down Expand Up @@ -915,24 +900,13 @@ public function view($id = null) {
// AutoViewVarsTrait
// We still used this in view() to map select values
$this->populateAutoViewVars($obj);

if(method_exists($table, 'generateDisplayField')) {
// We don't use a trait for this since each table will implement different logic

$this->set('vv_title', __d('operation', 'view.ai', $table->generateDisplayField($obj)));
$this->set('vv_supertitle', $table->generateDisplayField($obj));
// Pass the display field also into subtitle for dealing with External IDs
$this->set('vv_subtitle', $table->generateDisplayField($obj));
} else {
// Default view title is the object display field
$field = $table->getDisplayField();

if(!empty($obj->$field)) {
$this->set('vv_title', __d('operation', 'view.ai', $obj->$field));
} else {
$this->set('vv_title', __d('operation', 'view.ai', __d('controller', $modelsName, [1])));
}
}

// Calculate and set title, supertitle and subtitle
[$title, $supertitle, $subtitle] = StringUtilities::entityAndActionToTitle($obj, $modelsName, 'view');

$this->set('vv_title', $title);
$this->set('vv_supertitle', $supertitle);
$this->set('vv_subtitle', $subtitle);

// Let the view render
$this->render('/Standard/add-edit-view');
Expand Down
3 changes: 2 additions & 1 deletion app/src/Controller/StandardPluginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ public function beforeRender(\Cake\Event\EventInterface $event) {
// Override the title set in StandardController. Since that was set in edit()
// which is called before the rendering hooks, this title will take precedence.

$this->set('vv_title', __d('operation', 'configure.a', $parentObj->$parentDisplayField));
[$title, , ] = StringUtilities::entityAndActionToTitle($parentObj, $parentClassName, 'configure');
$this->set('vv_title', $title);
}

return parent::beforeRender($event);
Expand Down
Loading

0 comments on commit c980a40

Please sign in to comment.