Permalink
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
match/app/src/Model/Table/MatchgridSettingsTable.php
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

…story Records (CO-1682)
263 lines (219 sloc)
7.27 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* COmanage Match Matchgrid Settings Table | |
* | |
* Portions licensed to the University Corporation for Advanced Internet | |
* Development, Inc. ("UCAID") under one or more contributor license agreements. | |
* See the NOTICE file distributed with this work for additional information | |
* regarding copyright ownership. | |
* | |
* UCAID licenses this file to you under the Apache License, Version 2.0 | |
* (the "License"); you may not use this file except in compliance with the | |
* License. You may obtain a copy of the License at: | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
* | |
* @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) | |
*/ | |
declare(strict_types = 1); | |
namespace App\Model\Table; | |
use Cake\ORM\Query; | |
use Cake\ORM\Table; | |
use Cake\ORM\TableRegistry; | |
use Cake\Validation\Validator; | |
use \App\Lib\Enum\ReferenceIdEnum; | |
class MatchgridSettingsTable extends Table { | |
use \App\Lib\Traits\AutoViewVarsTrait; | |
use \App\Lib\Traits\MatchgridLinkTrait; | |
use \App\Lib\Traits\PrimaryLinkTrait; | |
// Default values for each setting | |
protected $defaultSettings = [ | |
'notification_email' => null, | |
'referenceid_method' => ReferenceIdEnum::UUID, | |
'referenceid_start' => 1, | |
'referenceid_prefix' => "", | |
]; | |
/** | |
* Perform Cake Model initialization. | |
* | |
* @since COmanage Match v1.0.0 | |
* @param array $config Configuration options passed to constructor | |
*/ | |
public function initialize(array $config): void { | |
// Timestamp behavior handles created/modified updates | |
$this->addBehavior('Timestamp'); | |
// Define associations | |
$this->belongsTo('Matchgrids'); | |
$this->belongsTo('ResolutionNotificationEndpoints', ['className' => 'Endpoints']) | |
->setForeignKey('resolution_notification_endpoint_id') | |
->setProperty('resolution_notification_endpoint'); | |
$this->setDisplayField('matchgrid_id'); | |
$this->setPrimaryLink('matchgrid_id'); | |
$this->setRequiresMatchgrid(true); | |
$this->setAutoViewVars([ | |
'referenceidMethods' => [ | |
'type' => 'enum', | |
'class' => 'ReferenceIdEnum' | |
], | |
'resolutionNotificationEndpoints' => [ | |
'type' => 'select', | |
'model' => 'Endpoints', | |
'find' => 'filterPrimaryLink', | |
'order' => ['description' => 'ASC'] | |
] | |
]); | |
} | |
/** | |
* Get the MatchgridSetting row ID for a given Matchgrid, creating it if not | |
* already present. | |
* | |
* @since COmanage Match v1.0.0 | |
* @param int $matchgridId Matchgrid ID | |
* @return int MatchgridSetting ID | |
*/ | |
public function getIdForMatchgrid(int $matchgridId) { | |
try { | |
$obj = $this->find()->where(["matchgrid_id" => $matchgridId])->firstOrFail(); | |
} | |
catch(\Cake\Datasource\Exception\RecordNotFoundException $e) { | |
// No current Matchgrid Settings object for this Matchgrid, create one | |
$obj = $this->newEntity(["matchgrid_id" => $matchgridId]); | |
$this->save($obj); | |
} | |
return $obj->id; | |
} | |
/** | |
* Get the Matchgrid notification email. | |
* | |
* @since COmanage Match v1.0.0 | |
* @param int $matchgridId Matchgrid ID | |
*/ | |
public function getNotificationEmail(int $matchgridId) { | |
return $this->lookupValue($matchgridId, 'notification_email'); | |
} | |
/** | |
* Get the Notification Endpoint for the specified Matchgrid. | |
* | |
* @since COmanage Match v1.1.0 | |
* @param int $matchgridId Matchgrid ID | |
* @return Endpoint Endpoint Entity | |
*/ | |
public function getNotificationEndpoint(int $matchgridId) { | |
$endpointId = $this->lookupValue($matchgridId, 'resolution_notification_endpoint_id'); | |
if($endpointId) { | |
$Endpoints = TableRegistry::getTableLocator()->get('Endpoints'); | |
// throws Cake\Datasource\Exception\RecordNotFoundException | |
return $Endpoints->get($endpointId); | |
} | |
return null; | |
} | |
/** | |
* Get the Matchgrid Reference ID prefix. | |
* | |
* @since COmanage Match v1.0.0 | |
* @param int $matchgridId Matchgrid ID | |
*/ | |
public function getReferenceIdPrefix(int $matchgridId) { | |
return $this->lookupValue($matchgridId, 'referenceid_prefix'); | |
} | |
/** | |
* Get the Matchgrid Reference ID start. | |
* | |
* @since COmanage Match v1.0.0 | |
* @param int $matchgridId Matchgrid ID | |
*/ | |
public function getReferenceIdStart(int $matchgridId) { | |
return $this->lookupValue($matchgridId, 'referenceid_start'); | |
} | |
/** | |
* Get the Matchgrid Reference ID method. | |
* | |
* @since COmanage Match v1.0.0 | |
* @param int $matchgridId Matchgrid ID | |
*/ | |
public function getReferenceIdMethod(int $matchgridId) { | |
return $this->lookupValue($matchgridId, 'referenceid_method'); | |
} | |
/** | |
* Obtain a single Matchgrid setting. | |
* | |
* @since COmanage Match v1.0.0 | |
* @param int $matchgridId Matchgrid ID | |
* @param string $field Field name to retrieve, corresponding to column/field name | |
*/ | |
protected function lookupValue(int $matchgridId, string $field) { | |
$value = null; | |
try { | |
$settings = $this->find()->where(["matchgrid_id" => $matchgridId])->firstOrFail(); | |
if(isset($settings->$field)) { | |
return $settings->$field; | |
} | |
} | |
catch(\Cake\Datasource\Exception\RecordNotFoundException $e) { | |
// No settings for this Matchgrid, use default value | |
} | |
// Let other exceptions pass up the stack | |
return $this->defaultSettings[$field]; | |
} | |
/** | |
* Set validation rules. | |
* | |
* @since COmanage Match v1.0.0 | |
* @param Validator $validator Validator | |
* @return \Cake\Validation\Validator Validator | |
*/ | |
public function validationDefault(Validator $validator): Validator { | |
$validator->add( | |
'matchgrid_id', | |
'content', | |
[ 'rule' => 'isInteger' ] | |
); | |
$validator->notBlank('matchgrid_id'); | |
$validator->add( | |
'referenceid_method', | |
'content', | |
[ 'rule' => [ 'inList', [ | |
ReferenceIdEnum::Sequence, | |
ReferenceIdEnum::UUID | |
] ] ] | |
); | |
$validator->notEmptyString('referenceid_method'); | |
$validator->add( | |
'referenceid_start', | |
'content', | |
[ 'rule' => [ 'range', 1, PHP_INT_MAX ], | |
'message' => __('match.er.val.range', [1, PHP_INT_MAX]) ] | |
); | |
$validator->allowEmptyString('referenceid_start'); | |
$validator->add( | |
'referenceid_prefix', | |
'content', | |
[ 'rule' => [ 'maxLength', 32 ], | |
'message' => __('match.er.val.length', [32]) ] | |
); | |
$validator->allowEmptyString('referenceid_prefix'); | |
$validator->add( | |
'notification_email', | |
'length', | |
[ 'rule' => [ 'maxLength', 80 ], | |
'message' => __('match.er.val.length', [80]) ] | |
); | |
$validator->add( | |
'notification_email', | |
'content', | |
[ 'rule' => 'email', | |
'message' => __('match.er.val.email') ] | |
); | |
$validator->allowEmptyString('notification_email'); | |
return $validator; | |
} | |
} |