Skip to content

Commit

Permalink
Save pronouns.Save Urls.Improvements (#295)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ioannis authored Feb 13, 2025
1 parent 635d4f9 commit 70d2f2d
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public function finalize(int $id, \App\Model\Entity\Petition $petition): bool
})->toArray();
$personAttributes = array_keys($personAttributes);

// Get all the fields/values required to build the PrersonRole
// Get all the fields/values required to build the PersonRole
$fieldsForPerson = $attributesCollection->filter(function($attr, $key) use ($personAttributes) {
return in_array($attr['enrollment_attribute']['attribute'], $personAttributes);
})->toArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,8 @@

namespace CoreEnroller\Model\Table;

use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\Event\EventInterface;
use Cake\ORM\Table;
use Cake\ORM\TableRegistry;
use Cake\Utility\Hash;
use Cake\Validation\Validator;
use \App\Lib\Enum\GroupTypeEnum;
use \App\Lib\Enum\SuspendableStatusEnum;
Expand Down Expand Up @@ -70,7 +67,7 @@ public function initialize(array $config): void {
$this->belongsTo('AttributeTypes')
->setClassName('Types')
->setForeignKey('attribute_type')
->setProperty('attribute_type');
->setProperty('type');


$this->hasMany('CoreEnroller.PetitionAttributes')
Expand Down
10 changes: 2 additions & 8 deletions app/src/Model/Table/AddressesTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@

namespace App\Model\Table;

use Cake\Collection\Collection;
use Cake\Utility\Hash;
use \App\Lib\Enum\LanguageEnum;
use \Cake\ORM\Table;
use \Cake\ORM\TableRegistry;
Expand Down Expand Up @@ -297,12 +295,7 @@ public function getPermittedFields(): array {
*/
public function saveAttributes(int $personId, ?int $roleId, string $parentModel, array $fields): bool
{
$fieldType = Hash::extract($fields, '{n}.enrollment_attribute.attribute_type.id');
$fieldTypeId = (new Collection($fieldType))->first();

$address = [
'type_id' => $fieldTypeId
];
$address = [];

if($parentModel === 'Person') {
if(empty($personId)) {
Expand All @@ -318,6 +311,7 @@ public function saveAttributes(int $personId, ?int $roleId, string $parentModel,

foreach($fields as $fld) {
$address[$fld->column_name] = $fld->value;
$address['type_id'] = $fld->enrollment_attribute->attribute_type;
}

$this->saveOrFail($this->newEntity($address));
Expand Down
8 changes: 1 addition & 7 deletions app/src/Model/Table/EmailAddressesTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,13 @@

namespace App\Model\Table;

use Cake\Collection\Collection;
use Cake\Datasource\EntityInterface;
use Cake\Event\EventInterface;
use Cake\ORM\Table;
use Cake\ORM\TableRegistry;
use Cake\Utility\Hash;
use Cake\Validation\Validator;
use \App\Lib\Enum\ActionEnum;
use \App\Lib\Enum\ProvisioningContextEnum;
use \App\Model\Entity\EmailAddress;

class EmailAddressesTable extends Table {
use \App\Lib\Traits\AutoViewVarsTrait;
Expand Down Expand Up @@ -391,15 +388,12 @@ public function validationDefault(Validator $validator): Validator {
*/
public function saveAttributes(int $personId, ?int $roleId, string $parentModel, array $fields): bool
{
$fieldType = Hash::extract($fields, '{n}.enrollment_attribute.attribute_type.id');
$fieldTypeId = (new Collection($fieldType))->first();

foreach ($fields as $idx => $field) {
// Check if this has already been saved
$email = [
'person_id' => $personId,
'mail' => $field->value,
'type_id' => $fieldTypeId
'type_id' => $field->enrollment_attribute->attribute_type,
];

$this->saveOrFail($this->newEntity($email));
Expand Down
6 changes: 2 additions & 4 deletions app/src/Model/Table/NamesTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -476,12 +476,8 @@ public function validationDefault(Validator $validator): Validator {
*/
public function saveAttributes(int $personId, ?int $roleId, string $parentModel, array $fields): bool
{
$fieldType = Hash::extract($fields, '{n}.enrollment_attribute.attribute_type.id');
$fieldTypeId = (new Collection($fieldType))->first();

$name = [
'person_id' => $personId,
'type_id' => $fieldTypeId
];

// Save the primary name
Expand All @@ -491,6 +487,8 @@ public function saveAttributes(int $personId, ?int $roleId, string $parentModel,

foreach($fields as $fld) {
$name[$fld->column_name] = $fld->value;
$name['type_id'] = $fld->enrollment_attribute->attribute_type;
$name['language'] = $fld->enrollment_attribute->attribute_language;
}

$this->saveOrFail($this->newEntity($name));
Expand Down
14 changes: 13 additions & 1 deletion app/src/Model/Table/PronounsTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@

namespace App\Model\Table;

use App\Lib\Enum\LanguageEnum;
use Cake\Datasource\EntityInterface;
use Cake\ORM\Table;
use Cake\Validation\Validator;
use App\Lib\Enum\LanguageEnum;

class PronounsTable extends Table {
use \App\Lib\Traits\AutoViewVarsTrait;
Expand Down Expand Up @@ -188,6 +189,17 @@ public function validationDefault(Validator $validator): Validator {
*/
public function saveAttributes(int $personId, ?int $roleId, string $parentModel, array $fields): bool
{
foreach ($fields as $idx => $field) {
// Check if this has already been saved
$pronoun = [
'person_id' => $personId,
'pronouns' => $field->value,
'language' => $field->enrollment_attribute->attribute_language,
'type_id' => $field->enrollment_attribute->attribute_type,
];

$this->saveOrFail($this->newEntity($pronoun));
}
return true;
}
}
8 changes: 2 additions & 6 deletions app/src/Model/Table/TelephoneNumbersTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,12 +235,7 @@ public function validationDefault(Validator $validator): Validator {
*/
public function saveAttributes(int $personId, ?int $roleId, string $parentModel, array $fields): bool
{
$fieldType = Hash::extract($fields, '{n}.enrollment_attribute.attribute_type.id');
$fieldTypeId = (new Collection($fieldType))->first();

$telephone = [
'type_id' => $fieldTypeId
];
$telephone = [];

if($parentModel === 'Person') {
if(empty($personId)) {
Expand All @@ -256,6 +251,7 @@ public function saveAttributes(int $personId, ?int $roleId, string $parentModel,

foreach($fields as $fld) {
$telephone[$fld->column_name] = $fld->value;
$telephone['type_id'] = $fld->enrollment_attribute->attribute_type;
}

$this->saveOrFail($this->newEntity($telephone));
Expand Down
15 changes: 14 additions & 1 deletion app/src/Model/Table/UrlsTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@

namespace App\Model\Table;

use Cake\Collection\Collection;
use Cake\Datasource\EntityInterface;
use Cake\ORM\Table;
use Cake\Utility\Hash;
use Cake\Validation\Validator;

class UrlsTable extends Table {
Expand Down Expand Up @@ -195,7 +198,7 @@ public function validationDefault(Validator $validator): Validator {
/**
* Save attributes for a given person and optionally their role.
*
* @since COmanage Registry v5.0.0
* @since COmanage Registry v5.1.0
* @param int $personId Identifier for the person
* @param int|null $roleId Identifier for the role (nullable)
* @param string $parentModel Name of the parent model
Expand All @@ -204,6 +207,16 @@ public function validationDefault(Validator $validator): Validator {
*/
public function saveAttributes(int $personId, ?int $roleId, string $parentModel, array $fields): bool
{
foreach ($fields as $idx => $field) {
// Check if this has already been saved
$url = [
'person_id' => $personId,
'url' => $field->value,
'type_id' => $field->enrollment_attribute->attribute_type,
];

$this->saveOrFail($this->newEntity($url));
}
return true;
}
}

0 comments on commit 70d2f2d

Please sign in to comment.