Skip to content

Commit

Permalink
Merge pull request #129 from anstiftung/issue-127-skills-with-session
Browse files Browse the repository at this point in the history
Issue 127 skills with session
  • Loading branch information
mrothauer authored Oct 16, 2023
2 parents f58be82 + 9c9588a commit b5c6674
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 29 deletions.
23 changes: 21 additions & 2 deletions plugins/Admin/src/Controller/KnowledgesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,38 @@ public function edit($uid)
$this->setReferer();
$this->setIsCurrentlyUpdated($uid);

$this->Skill = $this->getTableLocator()->get('Skills');

if (!empty($this->request->getData())) {

$this->Skill = $this->getTableLocator()->get('Skills');
$this->request = $this->Skill->addSkills($this->request, $this->loggedUser, 'Knowledges');
$associatedSkills = $this->request->getData('Knowledges.skills._ids');
$newSkills = $this->Skill->getNewSkillsFromRequest($associatedSkills);
$existingSkills = $this->Skill->getExistingSkillsFromRequest($associatedSkills);
$this->request->getSession()->write('newSkillsKnowledges', $newSkills);
$this->request = $this->request->withData('Knowledges.skills._ids', $existingSkills);

$patchedEntity = $this->Knowledge->getPatchedEntityForAdminEdit($knowledge, $this->request->getData());

if (!($patchedEntity->hasErrors())) {
$patchedEntity = $this->patchEntityWithCurrentlyUpdatedFields($patchedEntity);
$this->saveObject($patchedEntity);

$newSkills = $this->request->getSession()->read('newSkillsKnowledges');
if (!empty($newSkills)) {
// save new skills
$addedSkillIds = $this->Skill->addSkills($newSkills, $this->loggedUser->isAdmin(), $this->loggedUser->uid);
// save id associations to knowledge
$this->request = $this->request->withData('Knowledges.skills._ids', array_merge($this->request->getData('Knowledges.skills._ids'), $addedSkillIds));
$patchedEntity = $this->Knowledge->getPatchedEntityForAdminEdit($knowledge, $this->request->getData());
$this->saveObject($patchedEntity);
$this->request->getSession()->delete('newSkillsKnowledges');
}

} else {
$knowledge = $patchedEntity;
}
} else {
$this->request->getSession()->delete('newSkillsKnowledges');
}

$this->set('knowledge', $knowledge);
Expand Down
4 changes: 4 additions & 0 deletions plugins/Admin/templates/Knowledges/edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
echo '</div>';
echo '<div class="sc"></div>';

$this->element('addScript', ['script' =>
JS_NAMESPACE . ".Helper.addNewTagsToSelect2Multidropdown('select#knowledges-skills-ids', ".json_encode($this->request->getSession()->read('newSkillsKnowledges')).");
"]);

echo '<div class="skills-wrapper">';
echo '<b id="knowledges-skills" class="pseudo-field">Weitere Kenntnisse / Interessen</b>';
echo $this->Form->control('Knowledges.skills._ids', [
Expand Down
41 changes: 36 additions & 5 deletions src/Controller/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,15 @@ private function _profil($user, $isMyProfile, $isEditMode)
if (empty($this->request->getData())) {
$user->private_as_array = explode(',', $user->private);
$this->request = $this->request->withParsedBody($user);
$this->request->getSession()->delete('newSkillsProfile');
} else {

$associatedSkills = $this->request->getData('Users.skills._ids');
$newSkills = $this->Skill->getNewSkillsFromRequest($associatedSkills);
$existingSkills = $this->Skill->getExistingSkillsFromRequest($associatedSkills);
$this->request->getSession()->write('newSkillsProfile', $newSkills);
$this->request = $this->request->withData('Users.skills._ids', $existingSkills);

$addressString = trim($this->request->getData('Users.zip') . ', ' . $this->request->getData('Users.city') . ', ' . $this->request->getData('Users.country_code'));
$coordinates = $this->getLatLngFromGeoCodingService($addressString);
$this->request = $this->request->withData('Users.lat', $coordinates['lat']);
Expand All @@ -304,9 +311,6 @@ private function _profil($user, $isMyProfile, $isEditMode)
}
$this->request = $this->request->withData('Users.private', $private);

$this->Skill = $this->getTableLocator()->get('Skills');
$this->request = $this->Skill->addSkills($this->request, $this->loggedUser, 'Users');

$user = $this->User->patchEntity($user, $this->request->getData(), ['validate' => 'UserEdit' . ($this->isAdmin() ? 'Admin' : 'User')]);
if (!$user->hasErrors()) {
$this->User->save($user);
Expand All @@ -316,6 +320,17 @@ private function _profil($user, $isMyProfile, $isEditMode)
$this->Authentication->setIdentity($user);
}

$newSkills = $this->request->getSession()->read('newSkillsProfile');
if (!empty($newSkills)) {
// save new skills
$addedSkillIds = $this->Skill->addSkills($newSkills, $this->loggedUser->isAdmin(), $this->loggedUser->uid);
// save id associations to user
$this->request = $this->request->withData('Users.skills._ids', array_merge($this->request->getData('Users.skills._ids'), $addedSkillIds));
$user = $this->User->patchEntity($user, $this->request->getData());
$this->User->save($user);
$this->request->getSession()->delete('newSkillsProfile');
}

if ($isEditMode) {
$message = 'Das Profil wurde erfolgreich gespeichert.';
} else {
Expand Down Expand Up @@ -578,8 +593,11 @@ public function register($userGroup=GROUPS_REPAIRHELPER)

if (! empty($this->request->getData())) {

$this->Skill = $this->getTableLocator()->get('Skills');
$this->request = $this->Skill->addSkills($this->request, $this->loggedUser, 'Users');
$associatedSkills = $this->request->getData('Users.skills._ids');
$newSkills = $this->Skill->getNewSkillsFromRequest($associatedSkills);
$existingSkills = $this->Skill->getExistingSkillsFromRequest($associatedSkills);
$this->request->getSession()->write('newSkillsRegistration', $newSkills);
$this->request = $this->request->withData('Users.skills._ids', $existingSkills);

if ($this->request->getData('Users.i_want_to_receive_the_newsletter')) {
$this->loadComponent('CptNewsletter');
Expand Down Expand Up @@ -629,6 +647,17 @@ public function register($userGroup=GROUPS_REPAIRHELPER)
'data' => $user
]);

$newSkills = $this->request->getSession()->read('newSkillsRegistration');
if (!empty($newSkills)) {
// save new skills
$addedSkillIds = $this->Skill->addSkills($newSkills, false, $result->uid);
// save id associations to user
$this->request = $this->request->withData('Users.skills._ids', array_merge($this->request->getData('Users.skills._ids'), $addedSkillIds));
$userEntity = $this->User->patchEntity($userEntity, $this->request->getData());
$this->User->save($userEntity);
$this->request->getSession()->delete('newSkillsRegistration');
}

$email->setTo($user['Users']['email']);

if ($email->send()) {
Expand All @@ -641,6 +670,8 @@ public function register($userGroup=GROUPS_REPAIRHELPER)
$this->set('user', $user);
$this->render('register');
}
} else {
$this->request->getSession()->delete('newSkillsRegistration');
}

$this->set('user', $user);
Expand Down
46 changes: 28 additions & 18 deletions src/Model/Table/SkillsTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,38 +37,48 @@ public function validationDefault(Validator $validator): \Cake\Validation\Valida
return $validator;
}

public function addSkills($request, $loggedUser, $entity)
{
public function getNewSkillsFromRequest($associatedSkills) {
if (!is_array($associatedSkills)) {
return [];
}
$skills = array_filter($associatedSkills, function($value) {
return !is_numeric($value);
});
return $skills;
}

$skills = $request->getData($entity . '.skills._ids');
if (empty($skills)) {
return $request;
public function getExistingSkillsFromRequest($associatedSkills) {
if (!is_array($associatedSkills)) {
return [];
}
$skills = array_filter($associatedSkills, function($value) {
return is_numeric($value);
});
return $skills;
}

public function addSkills($newSkills, $isAdmin, $userUid)
{

$skillsToAdd = [];
foreach($skills as $key => $skill) {
if (!is_numeric($skill)) {
unset($skills[$key]);
$preparedSkill = strip_tags($skill);
$skillsToAdd[] = $this->newEntity([
'name' => $preparedSkill,
'status' => !empty($loggedUser) && $loggedUser->isAdmin() ? APP_ON : APP_OFF,
'owner' => !empty($loggedUser) ? $loggedUser->uid : 0,
]);
}
foreach($newSkills as $skill) {
$preparedSkill = strip_tags($skill);
$skillsToAdd[] = $this->newEntity([
'name' => $preparedSkill,
'status' => $isAdmin ? APP_ON : APP_OFF,
'owner' => $userUid,
]);
}
$request = $request->withData($entity . '.skills._ids', $skills);

$addedSkillIds = [];
if (!empty($skillsToAdd)) {
$addedSkills = $this->saveMany($skillsToAdd);
foreach($addedSkills as $addedSkill) {
$addedSkillIds[] = $addedSkill->id;
}
$request = $request->withData($entity . '.skills._ids', array_merge($skills, $addedSkillIds));
}

return $request;
return $addedSkillIds;

}

Expand Down
6 changes: 5 additions & 1 deletion templates/Users/profil.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,16 @@

echo '<div class="skills-wrapper">';
echo '<b id="users-skills" class="pseudo-field" data-private="true" >Weitere Kenntnisse / Interessen</b>';
$this->element('addScript', ['script' =>
JS_NAMESPACE . ".Helper.addNewTagsToSelect2Multidropdown('select#users-skills-ids', ".json_encode($this->request->getSession()->read('newSkillsProfile')).");
"]);

echo $this->Form->control('Users.skills._ids', [
'multiple' => 'select',
'data-tags' => true,
'data-token-separators' => "[',']",
'label' => false,
'options' => $skillsForDropdown
'options' => $skillsForDropdown,
]);
echo '</div>';

Expand Down
5 changes: 5 additions & 0 deletions templates/element/registrationForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@

echo '<div class="skills-wrapper">';
echo '<b>Weitere Kenntnisse / Interessen</b>';

$this->element('addScript', ['script' =>
JS_NAMESPACE . ".Helper.addNewTagsToSelect2Multidropdown('select#UserRegSkills" . $userGroup . "', ".json_encode($this->request->getSession()->read('newSkillsRegistration')).");
"]);

echo $this->Form->control('Users.skills._ids', [
'multiple' => 'select',
'data-tags' => true,
Expand Down
15 changes: 12 additions & 3 deletions tests/TestCase/Controller/UsersControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
use Cake\TestSuite\EmailTrait;
use Cake\TestSuite\IntegrationTestTrait;
use App\Test\TestCase\Traits\LoginTrait;
use AssetCompress\Factory;
use Cake\Datasource\FactoryLocator;
use Cake\ORM\Locator\TableLocator;

class UsersControllerTest extends AppTestCase
{
Expand All @@ -34,7 +37,10 @@ class UsersControllerTest extends AppTestCase
'_ids' => [87],
],
'skills' => [
'_ids' => [1, 'new skill'],
'_ids' => [
0 => 1,
1 => 'new skill',
],
],
];

Expand Down Expand Up @@ -88,8 +94,11 @@ public function testRegisterOrga()
$this->assertNotEquals($user->groups[0]->id, GROUPS_REPAIRHELPER);
$this->assertEquals($user->categories[0]->id, 87);
$this->assertEquals($user->skills[0]->id, 1);
$this->assertEquals($user->skills[1]->id, 2);
$this->assertEquals($user->skills[1]->name, 'new skill');

$skillsTable = $this->getTableLocator()->get('Skills');
$skills = $skillsTable->find('all')->toArray();
$this->assertCount(2, $skills);
$this->assertEquals(9, $skills[1]->owner);

$this->assertMailCount(1);
$this->assertMailSentTo($this->validUserData['email']);
Expand Down
15 changes: 15 additions & 0 deletions webroot/js/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,21 @@ MappedRepairEvents.Helper = {
$('#band-cookies-ok').addClass('button');
},

addNewTagsToSelect2Multidropdown : function(selector, newTags) {

if (newTags !== null && newTags.length == 0) {
return;
}

const selectElement = $(selector);

for (var i in newTags) {
var newOption = new Option(newTags[i], newTags[i], true, true);
selectElement.append(newOption);
}
selectElement.trigger('change');
},

beautifyDropdowns : function() {
$('select').not('.no-select2').select2();
},
Expand Down

0 comments on commit b5c6674

Please sign in to comment.