Skip to content

Commit

Permalink
CAKEPHP5 upgrade fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Ioannis committed Sep 12, 2025
1 parent b04aba0 commit 1f4ac6c
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 119 deletions.
26 changes: 12 additions & 14 deletions app/src/Controller/ApiV2Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ class ApiV2Controller extends AppController {
use \App\Lib\Traits\LabeledLogTrait;
use \App\Lib\Traits\IndexQueryTrait;

protected string $tableName = '';

protected \Cake\ORM\Table $table;

/**
* Perform Cake Controller initialization.
*
Expand Down Expand Up @@ -82,9 +86,7 @@ public function add() {
/** var string $modelsName */
$modelsName = $this->getName();
/** var Cake\ORM\Table $table */
$table = $this->fetchTable($modelsName);
// $tableName = models
$tableName = $this->tableName;
$table = $this->getCurrentTable();

$json = $this->request->getData(); // Parsed by BodyParserMiddleware

Expand All @@ -97,13 +99,13 @@ public function add() {

foreach($json[$modelsName] as $rec) {
try {
$obj = $this->$modelsName->newEntity($rec);
$obj = $table->newEntity($rec);

if($this->$modelsName->saveOrFail($obj)) {
if($table->saveOrFail($obj)) {
$results[] = ['id' => $obj->id];

// Trigger provisioning, letting errors bubble up (AR-GMR-5)
if(method_exists($this->$modelsName, "requestProvisioning")) {
if(method_exists($table, "requestProvisioning")) {
$this->llog('rule', "AR-GMR-5 Requesting provisioning for $modelsName " . $obj->id);
$table->requestProvisioning(id: $obj->id, context: ProvisioningContextEnum::Automatic);
}
Expand Down Expand Up @@ -186,7 +188,7 @@ public function delete($id) {
/** var string $modelsName */
$modelsName = $this->getName();
/** var Cake\ORM\Table $table */
$table = $this->fetchTable($modelsName);
$table = $this->getCurrentTable();
// $tableName = models
$tableName = $table->getTable();

Expand Down Expand Up @@ -234,10 +236,8 @@ protected function dispatchIndex(string $mode = 'default') {
throw new UnauthorizedException(__d('error', 'perm'));
}

// $modelsName = Models
$modelsName = $this->getName();
/** var Cake\ORM\Table $table */
$table = $this->fetchTable($modelsName);
$table = $this->getCurrentTable();

$reqParameters = [...$this->request->getQuery()];
$pickerMode = ($mode === 'picker');
Expand Down Expand Up @@ -271,7 +271,7 @@ public function edit($id) {
/** var string $modelsName */
$modelsName = $this->getName();
/** var Cake\ORM\Table $table */
$table = $this->fetchTable($modelsName);
$table = $this->getCurrentTable();
// $tableName = models
$tableName = $table->getTable();

Expand Down Expand Up @@ -390,10 +390,8 @@ public function index() {
*/

public function view($id = null) {
/** var string $modelsName */
$modelsName = $this->getName();
/** var Cake\ORM\Table $table */
$table = $this->fetchTable($modelsName);
$table = $this->getCurrentTable();
// $tableName = models
$tableName = $table->getTable();

Expand Down
91 changes: 44 additions & 47 deletions app/src/Controller/AppController.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
* @property \Cake\Controller\Component\FlashComponent $Flash
* @property \Cake\Controller\Component\FormProtectionComponent $FormProtection
*/
#[\AllowDynamicProperties]
class AppController extends Controller {
use \App\Lib\Traits\LabeledLogTrait;

Expand Down Expand Up @@ -221,9 +220,6 @@ public function beforeFilter(\Cake\Event\EventInterface $event) {
*/

public function beforeRender(\Cake\Event\EventInterface $event) {
/** var string $modelsName */
$modelsName = $this->getName();

// Views can also inspect the request object to determine the current
// controller and action, but it seems slightly easier to do it once here.
$this->set('vv_controller', $this->request->getParam('controller'));
Expand Down Expand Up @@ -269,6 +265,25 @@ public function getCOID(): ?int {
return $cur_co ? $cur_co->id : null;
}

/**
* Get the current Table instance for this controller, respecting plugin context.
*
* @since COmanage Registry v5.2.0
* @return \Cake\ORM\Table
*/
protected function getCurrentTable(): \Cake\ORM\Table
{
/** @var string $modelsName */
$modelsName = $this->getName();

$alias = $this->plugin !== null
? $this->plugin . '.' . $modelsName
: $modelsName;

return $this->fetchTable($alias);
}


/**
* @param string $potentialPrimaryLink
*
Expand All @@ -278,14 +293,11 @@ public function getCOID(): ?int {

protected function primaryLinkOnGet(string $potentialPrimaryLink): Object|bool
{
/** var string $modelsName */
$modelsName = $this->getName();

// If this action allows unkeyed, asserted primary link IDs, check the query
// string (e.g.: 'add' or 'index' allow matchgrid_id to be passed in)
$actionParam = $this->request->getParam('action');
$allowsUnkeyed = $this->$modelsName->allowUnkeyedPrimaryLink($actionParam);
$allowsLookup = $this->$modelsName->allowLookupPrimaryLink($actionParam);
$allowsUnkeyed = $this->getCurrentTable()->allowUnkeyedPrimaryLink($actionParam);
$allowsLookup = $this->getCurrentTable()->allowLookupPrimaryLink($actionParam);
$param = (int)$this->request->getParam('pass.0');

if($allowsUnkeyed) {
Expand All @@ -299,7 +311,7 @@ protected function primaryLinkOnGet(string $potentialPrimaryLink): Object|bool
return false;
}

return $this->$modelsName->findPrimaryLink($param);
return $this->getCurrentTable()->findPrimaryLink($param);
}

/**
Expand Down Expand Up @@ -345,20 +357,18 @@ protected function primaryLinkOnPost(string $potentialPrimaryLink): Object|bool

protected function primaryLinkOnPut(): Object|bool
{
/** var string $modelsName */
$modelsName = $this->getName();
$param = (int)$this->request->getParam('pass.0');

// Put = edit, so we should look up the parent ID via the object itself
if (
empty($param)
||
!$this->$modelsName->allowLookupPrimaryLink($this->request->getParam('action'))
!$this->getCurrentTable()->allowLookupPrimaryLink($this->request->getParam('action'))
) {
return false;
}

return $this->$modelsName->findPrimaryLink($param);
return $this->getCurrentTable()->findPrimaryLink($param);
}

/**
Expand All @@ -368,11 +378,9 @@ protected function primaryLinkOnPut(): Object|bool
*/
protected function populatedPrimaryLink(string $potentialPrimaryLink): Object
{
/** var string $modelsName */
$modelsName = $this->getName();
// $potentialPrimaryLink will be something like 'attribute_collector_id'
// $potentialPrimaryLinkTable will be something like 'CoreEnroller.AttributeCollectors'
$potentialPrimaryLinkTable = $this->$modelsName->getPrimaryLinkTableName($potentialPrimaryLink);
$potentialPrimaryLinkTable = $this->getCurrentTable()->getPrimaryLinkTableName($potentialPrimaryLink);

// For looking up values in records here, we want only the attribute
// itself and not the plugin name (used for hacky notation by
Expand Down Expand Up @@ -406,9 +414,8 @@ protected function populatedPrimaryLink(string $potentialPrimaryLink): Object

protected function primaryLinkLookup(): void
{
/** var string $modelsName */
$modelsName = $this->getName();
$availablePrimaryLinks = $this->$modelsName->getPrimaryLinks();
$table = $this->getCurrentTable();
$availablePrimaryLinks = $table->getPrimaryLinks();

// Iterate over all the potential primary links and pick the appropriate one
foreach($availablePrimaryLinks as $potentialPrimaryLink) {
Expand All @@ -429,7 +436,7 @@ protected function primaryLinkLookup(): void

// At the end we need to have a Primary Link
if(empty($this->cur_pl->value)
&& !$this->$modelsName->allowEmptyPrimaryLink($this->request->getParam('action'))
&& !$table->allowEmptyPrimaryLink($this->request->getParam('action'))
&& $this->request->getParam('action') != 'deleted') {
throw new \RuntimeException(__d('error', 'primary_link'));
}
Expand Down Expand Up @@ -478,13 +485,10 @@ public function getPrimaryLink(bool $lookup=false): Object
return $this->cur_pl;
}

/** var string $modelsName */
$modelsName = $this->getName();

$this->cur_pl = new \stdClass();

if(!(method_exists($this->$modelsName, 'getPrimaryLinks')
&& $this->$modelsName->getPrimaryLinks())
if(!(method_exists($this->getCurrentTable(), 'getPrimaryLinks')
&& $this->getCurrentTable()->getPrimaryLinks())
) {
return $this->cur_pl;
}
Expand All @@ -504,7 +508,7 @@ public function getPrimaryLink(bool $lookup=false): Object

// Look up the link value to find the related entity

$linkTableName = $this->$modelsName->getPrimaryLinkTableName($this->cur_pl->attr);
$linkTableName = $this->getCurrentTable()->getPrimaryLinkTableName($this->cur_pl->attr);
$linkTable = $this->getTableLocator()->get($linkTableName);

$this->set('vv_primary_link_model', $linkTableName);
Expand Down Expand Up @@ -540,12 +544,9 @@ public function getPrimaryLink(bool $lookup=false): Object
*/

protected function getRedirectGoal(string $action): ?string {
/** var string $modelsName */
$modelsName = $this->getName();

// PrimaryLinkTrait
if(method_exists($this->$modelsName, "getRedirectGoal")) {
return $this->$modelsName->getRedirectGoal($this->request->getParam('action'));
if(method_exists($this->getCurrentTable(), "getRedirectGoal")) {
return $this->getCurrentTable()->getRedirectGoal($this->request->getParam('action'));
}

return 'index';
Expand Down Expand Up @@ -674,11 +675,10 @@ protected function setCO() {
if($this->request->is('restful')
&& !empty($attrs['params']['model'])) {
$modelsName = \Cake\Utility\Inflector::camelize($attrs['params']['model']);
$this->$modelsName = TableRegistry::getTableLocator()->get($modelsName);
}

if(!method_exists($this->$modelsName, "requiresCO")
|| !$this->$modelsName->requiresCO()) {
if(!method_exists($this->getCurrentTable(), "requiresCO")
|| !$this->getCurrentTable()->requiresCO()) {
// Nothing to do, CO not required by this model/controller
return;
}
Expand All @@ -700,13 +700,13 @@ protected function setCO() {
}

if(!$coid
&& $this->$modelsName->allowUnkeyedCO($this->request->getParam('action'))
&& $this->getCurrentTable()->allowUnkeyedCO($this->request->getParam('action'))
&& !empty($this->request->getQuery('co_id'))) {
$coid = $this->request->getQuery('co_id');
}

if(!$coid
&& !$this->$modelsName->allowEmptyCO()
&& !$this->getCurrentTable()->allowEmptyCO()
&& !$this->request->is('restful')) {
// If we get this far without a CO ID, something went wrong.
throw new \RuntimeException(__d('error', 'coid'));
Expand All @@ -728,7 +728,7 @@ protected function setCO() {
throw new \InvalidArgumentException(__d('error', 'inactive', [__d('controller', 'Cos', [1]), $coid]));
}

if(!empty($modelsName) && !empty($this->$modelsName)) {
if(!empty($modelsName) && !empty($this->getCurrentTable())) {
// We store the CO ID in Configuration to facilitate its access from
// model contexts such as validation where passing the value via the
// Controller is not particularly feasible. Note that for API calls
Expand All @@ -737,9 +737,9 @@ protected function setCO() {

// This only works for the current model, not related models. For
// relatedmodels, we use the event listener approach below.
if(method_exists($this->$modelsName, "acceptsCoId")
&& $this->$modelsName->acceptsCoId()) {
$this->$modelsName->setCurCoId((int)$coid);
if(method_exists($this->getCurrentTable(), "acceptsCoId")
&& $this->getCurrentTable()->acceptsCoId()) {
$this->getCurrentTable()->setCurCoId((int)$coid);
}

// This doesn't work for the current model since it has already been
Expand All @@ -753,7 +753,7 @@ protected function setCO() {
// a use case to do so, though note it's possible a child associations
// wants the CO ID even though the parent doesn't.

foreach($this->$modelsName->associations()->getIterator() as $a) {
foreach($this->getCurrentTable()->associations()->getIterator() as $a) {
$aTable = $a->getTarget();

if(method_exists($aTable, "acceptsCoId")
Expand All @@ -773,9 +773,6 @@ protected function setCO() {
*/

protected function setTZ() {
/** var string $modelsName */
$modelsName = $this->getName();

// See if we've collected it from the browser in a previous page load. Otherwise,
// use the system default. If the user set a preferred timezone, we'll catch that below.

Expand All @@ -802,9 +799,9 @@ protected function setTZ() {

$this->set('vv_tz', $tz);

if($this->$modelsName->behaviors()->has('Timezone')) {
if($this->getCurrentTable()->behaviors()->has('Timezone')) {
// Tell TimezoneBehavior what the current timezone is
$this->$modelsName->setTimeZone($tz);
$this->getCurrentTable()->setTimeZone($tz);
}
}
}
14 changes: 8 additions & 6 deletions app/src/Controller/Component/BreadcrumbComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,10 @@ public function beforeRender(EventInterface $event) {

// Do we have a target model, and if so is it a configuration
// model (eg: ApiUsers) or an object model (eg: CoPeople)?
if(\is_object($controller->$modelsName)
&& method_exists($controller->$modelsName, "isConfigurationTable")
if(\is_object($controller->fetchTable($modelsName))
&& method_exists($controller->fetchTable($modelsName), "isConfigurationTable")
) {
$controller->set('vv_bc_configuration_link', $controller->$modelsName->isConfigurationTable());
$controller->set('vv_bc_configuration_link', $controller->fetchTable($modelsName)->isConfigurationTable());
} else {
$controller->set('vv_bc_configuration_link', false);
}
Expand Down Expand Up @@ -222,9 +222,11 @@ public function injectPrimaryLink(object $link, bool $index=true, string $linkLa
$linkAttr = $link->attr == $modelNameForeignKey ? 'id' : $link->attr;
$linkObj = $linkTable
->find()
->where(["$modelAlias.$linkAttr" => $link->value])
->contain($contain)
->firstOrFail();
->where(["$modelAlias.$linkAttr" => $link->value]);
if ($contain !== null) {
$linkObj = $linkObj->contain($contain);
}
$linkObj = $linkObj->firstOrFail();

if($index) {
// We need to determine the primary link of the parent, which might or might
Expand Down
6 changes: 4 additions & 2 deletions app/src/Controller/MVEAController.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class MVEAController extends StandardController {
public function beforeFilter(\Cake\Event\EventInterface $event) {
/** var string $modelsName */
$modelsName = $this->getName();
$table = $this->getCurrentTable();

if(!$this->request->is('restful') && $this->request->getParam('action') != 'deleted') {
// Provide additional hints to BreadcrumbsComponent. This needs to be here
Expand All @@ -62,7 +63,7 @@ public function beforeFilter(\Cake\Event\EventInterface $event) {
} else {
$parentModel = StringUtilities::foreignKeyToClassName($primaryLink->attr);

$parentPrimaryLink = $this->$modelsName->$parentModel->findPrimaryLink((int)$primaryLink->value);
$parentPrimaryLink = $table->$parentModel->findPrimaryLink((int)$primaryLink->value);

$this->Breadcrumb->injectPrimaryLink($parentPrimaryLink);
$this->Breadcrumb->injectPrimaryLink($primaryLink);
Expand Down Expand Up @@ -140,12 +141,13 @@ public function beforeFilter(\Cake\Event\EventInterface $event) {
public function beforeRender(\Cake\Event\EventInterface $event) {
/** var string $modelsName */
$modelsName = $this->getName();
$table = $this->getCurrentTable();
// field = model (or model_name)
$fieldName = Inflector::underscore(Inflector::singularize($modelsName));

if(!$this->request->is('restful') && $this->request->getParam('action') != 'deleted') {
// If there is a default type setting for this model, pass it to the view
if($this->$modelsName->getSchema()->hasColumn('type_id')) {
if($table->getSchema()->hasColumn('type_id')) {
$defaultTypeField = "default_" . $fieldName . "_type_id";

$CoSettings = TableRegistry::getTableLocator()->get('CoSettings');
Expand Down
Loading

0 comments on commit 1f4ac6c

Please sign in to comment.