Skip to content

Commit

Permalink
slection notification
Browse files Browse the repository at this point in the history
cleanup
  • Loading branch information
fiste788 committed Jan 12, 2018
1 parent f18ab78 commit d84ab35
Show file tree
Hide file tree
Showing 519 changed files with 115 additions and 707 deletions.
26 changes: 13 additions & 13 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/Controller/Api/SelectionsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ function (Event $event) {
$event->getSubject()->entity->active = true;
}
);

$this->Crud->execute();
}
}
18 changes: 0 additions & 18 deletions src/Model/Entity/Selection.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

use Cake\ORM\Entity;
use Cake\ORM\Table;
use Cake\ORM\TableRegistry;

/**
* Selection Entity.
Expand Down Expand Up @@ -49,21 +48,4 @@ public function toTransfert(Table $transfertsTable)

return $transfert;
}

public function isMemberAlreadySelected()
{
$team = TableRegistry::get('Teams')->get($this->team_id);
$selection = TableRegistry::get('Selections')
->find()
->innerJoinWith('Teams')
->where(
[
'team_id !=' => $this->team_id,
'new_member_id' => $this->new_member_id,
'Teams.championship_id' => $team->championship_id
]
);

return !$selection->isEmpty();
}
}
6 changes: 3 additions & 3 deletions src/Model/Table/LineupsTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,10 @@ public function buildRules(RulesChecker $rules)
$rules->add($rules->existsIn(['team_id'], 'Teams'));
$rules->add(
function (\App\Model\Entity\Lineup $entity, $options) {
TableRegistry::get('Lineups')->loadInto($entity, ['Matchdays']);
$matchday = TableRegistry::get('Matchdays')->get($entity->matchday_id);
$matchdays = TableRegistry::get('Matchdays')
->find()
->where(['season_id' => $entity->matchday->season_id])
->where(['season_id' => $matchday->season_id])
->count();

return TableRegistry::get('Lineups')->find()
Expand All @@ -184,7 +184,7 @@ function (\App\Model\Entity\Lineup $entity, $options) {
->where([
'jolly' => true,
'team_id' => $entity->team_id,
'Matchdays.number ' . ($entity->matchday->number <= $matchdays / 2 ? '<=' : '>') => $matchdays / 2
'Matchdays.number ' . ($matchday->number <= $matchdays / 2 ? '<=' : '>') => $matchdays / 2
])
->isEmpty();
},
Expand Down
9 changes: 3 additions & 6 deletions src/Model/Table/ScoresTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,9 @@ function ($q) use ($championshipId) {
*/
public function findRanking($championshipId)
{
$query = $this->find(
'all',
[
'contain' => ['Teams']
]
)->matching(
$query = $this->find()
->contain(['Teams'])
->matching(
'Teams',
function ($q) use ($championshipId) {
return $q->where(['Teams.championship_id' => $championshipId]);
Expand Down
130 changes: 95 additions & 35 deletions src/Model/Table/SelectionsTable.php
Original file line number Diff line number Diff line change
@@ -1,32 +1,36 @@
<?php

namespace App\Model\Table;

use App\Model\Entity\Event;
use App\Model\Entity\Selection;
use ArrayObject;
use Cake\Core\Configure;
use Cake\Datasource\EntityInterface;
use Cake\Event\Event as CakeEvent;
use Cake\ORM\Association\BelongsTo;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\ORM\TableRegistry;
use Cake\Validation\Validator;
use Minishlink\WebPush\WebPush;

/**
* Selections Model
*
* @property \App\Model\Table\TeamsTable|\Cake\ORM\Association\BelongsTo $Teams
* @property TeamsTable|BelongsTo $Teams
* @property BelongsTo $Members
* @property BelongsTo $Members
* @property \App\Model\Table\MatchdaysTable|\Cake\ORM\Association\BelongsTo $Matchdays
* @property \App\Model\Table\MembersTable|\Cake\ORM\Association\BelongsTo $NewMembers
* @property \App\Model\Table\MembersTable|\Cake\ORM\Association\BelongsTo $OldMembers
* @method \App\Model\Entity\Selection get($primaryKey, $options = [])
* @method \App\Model\Entity\Selection newEntity($data = null, array $options = [])
* @method \App\Model\Entity\Selection[] newEntities(array $data, array $options = [])
* @method \App\Model\Entity\Selection|bool save(\Cake\Datasource\EntityInterface $entity, $options = [])
* @method \App\Model\Entity\Selection patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = [])
* @method \App\Model\Entity\Selection[] patchEntities($entities, array $data, array $options = [])
* @method \App\Model\Entity\Selection findOrCreate($search, callable $callback = null, $options = [])
* @property MatchdaysTable|BelongsTo $Matchdays
* @property MembersTable|BelongsTo $NewMembers
* @property MembersTable|BelongsTo $OldMembers
* @method Selection get($primaryKey, $options = [])
* @method Selection newEntity($data = null, array $options = [])
* @method Selection[] newEntities(array $data, array $options = [])
* @method Selection|bool save(EntityInterface $entity, $options = [])
* @method Selection patchEntity(EntityInterface $entity, array $data, array $options = [])
* @method Selection[] patchEntities($entities, array $data, array $options = [])
* @method Selection findOrCreate($search, callable $callback = null, $options = [])
*/
class SelectionsTable extends Table
{
Expand All @@ -48,31 +52,31 @@ public function initialize(array $config)
$this->belongsTo(
'Teams',
[
'foreignKey' => 'team_id',
'joinType' => 'INNER'
'foreignKey' => 'team_id',
'joinType' => 'INNER'
]
);
$this->belongsTo(
'Matchdays',
[
'foreignKey' => 'matchday_id',
'joinType' => 'INNER'
'foreignKey' => 'matchday_id',
'joinType' => 'INNER'
]
);
$this->belongsTo(
'NewMembers',
[
'className' => 'Members',
'foreignKey' => 'old_member_id',
'propertyName' => 'old_member'
'className' => 'Members',
'foreignKey' => 'old_member_id',
'propertyName' => 'old_member'
]
);
$this->belongsTo(
'OldMembers',
[
'className' => 'Members',
'foreignKey' => 'new_member_id',
'propertyName' => 'new_member'
'className' => 'Members',
'foreignKey' => 'new_member_id',
'propertyName' => 'new_member'
]
);
}
Expand Down Expand Up @@ -112,29 +116,38 @@ public function buildRules(RulesChecker $rules)
$rules->add($rules->existsIn(['old_member_id'], 'OldMembers'));
$rules->add($rules->existsIn(['new_member_id'], 'NewMembers'));
$rules->add(
function (\App\Model\Entity\Selection $entity, $options) {
if ($entity->isMemberAlreadySelected()) {
$ranking = TableRegistry::get('Scores')->findRankingByChampionshipId($entity->team->championship_id);
function (Selection $entity, $options) {
$selection = $this->findAlreadySelectedMember($entity);
if ($selection != null) {
$ranking = TableRegistry::get('Scores')->findRanking($selection->team->championship_id);
$rank = \Cake\Utility\Hash::extract($ranking->toArray(), '{n}.team_id');
if (array_search($entity->team_id, $rank) > array_search($selection->team->id, $rank)) {
$selection->active = false;
$this->save($selection);
$this->notifyLostMember($selection);

return $ranking[$entity->team_id] < $ranking[$this->team_id];
return true;
} else {
return false;
}
}

return true;
},
return true;
},
'NewMemberIsSelectable',
['errorField' => 'new_member', 'message' => 'Un altro utente ha già selezionato il giocatore']
);
$rules->add(
function (\App\Model\Entity\Selection $entity, $options) use ($that) {
$championship = TableRegistry::get('Championships')->find()->innerJoinWith(
function (Selection $entity, $options) use ($that) {
$championship = TableRegistry::get('Championships')->find()->innerJoinWith(
'Teams',
function ($q) use ($entity) {
return $q->where(['Teams.id' => $entity->team_id]);
}
return $q->where(['Teams.id' => $entity->team_id]);
}
)->first();

return $that->find()->where(['team_id' => $entity->team_id, 'processed' => false])->count() < $championship->number_selections;
},
return $that->find()->where(['team_id' => $entity->team_id, 'processed' => false])->count() < $championship->number_selections;
},
'TeamReachedMaximum',
['errorField' => 'new_member', 'message' => 'Hai raggiunto il limite di cambi selezione']
);
Expand All @@ -153,7 +166,7 @@ public function afterSave(CakeEvent $event, EntityInterface $entity, ArrayObject
}
}

public function beforeSave(CakeEvent $event, \App\Model\Entity\Selection $entity, ArrayObject $options)
public function beforeSave(CakeEvent $event, Selection $entity, ArrayObject $options)
{
if ($entity->dirty('processed') && $entity->processed) {
$membersTeamsTable = TableRegistry::get('MembersTeams');
Expand All @@ -163,7 +176,7 @@ public function beforeSave(CakeEvent $event, \App\Model\Entity\Selection $entity
[
'team_id' => $entity->team_id,
'member_id' => $entity->old_member_id
]
]
)
->first();
$memberTeam->member_id = $entity->new_member_id;
Expand All @@ -172,4 +185,51 @@ public function beforeSave(CakeEvent $event, \App\Model\Entity\Selection $entity
$transfertsTable->save($transfert);
}
}

/**
*
* @param Selection $selection
*/
public function notifyLostMember(Selection $selection)
{
\Cake\Log\Log::debug('notifico utente');
$selection = $this->loadInto($selection, ['Teams.Users.Subscriptions', 'NewMembers.Players']);
$webPush = new WebPush(Configure::read('WebPush'));
foreach ($selection->team->user->subscriptions as $subscription) {
$message = WebPushMessage::create(Configure::read('WebPushMessage.default'))
->title('Un altra squadra ti ha soffiato un giocatore selezionato')
->body('Hai perso il giocatore ' . $selection->new_member->player->surname . ' ' . $selection->new_member->player->name)
->tag('lost-player-' . $selection->id);
$webPush->sendNotification(
$subscription->endpoint,
json_encode($message),
$subscription->public_key,
$subscription->auth_token
);
}
}

/**
*
* @param Selection $selection
* @return Selection
*/
public function findAlreadySelectedMember($selection)
{
$team = TableRegistry::get('Teams')->get($selection->team_id);
return $this->find()
->contain(['Teams'])
->matching(
'Teams',
function ($q) use ($team) {
return $q->where(['Teams.championship_id' => $team->championship_id]);
}
)
->where(
[
'team_id !=' => $selection->team_id,
'new_member_id' => $selection->new_member_id
]
)->first();
}
}
Binary file modified webroot/img/Clubs/1/background/1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added webroot/img/Clubs/1/background/1280w/1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added webroot/img/Clubs/1/background/600w/1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified webroot/img/Clubs/10/background/10.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added webroot/img/Clubs/2/background/1280w/2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified webroot/img/Clubs/2/background/2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added webroot/img/Clubs/2/background/600w/2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added webroot/img/Clubs/31/background/1280w/31.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified webroot/img/Clubs/31/background/31.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added webroot/img/Clubs/31/background/600w/31.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed webroot/img/ajax-loader.gif
Binary file not shown.
Binary file removed webroot/img/apple-touch-icon-precomposed.png
Binary file not shown.
Binary file removed webroot/img/backup.png
Binary file not shown.
Binary file removed webroot/img/bg-content-rounded.png
Binary file not shown.
Binary file removed webroot/img/bg-content.png
Binary file not shown.
Binary file removed webroot/img/bg-md.jpg
Binary file not shown.
Binary file removed webroot/img/bg-md.png
Binary file not shown.
Binary file removed webroot/img/bg-page.png
Binary file not shown.
Binary file removed webroot/img/bg-row.png
Binary file not shown.
Binary file removed webroot/img/bg.jpg
Binary file not shown.
Binary file removed webroot/img/bg2.jpg
Binary file not shown.
Binary file removed webroot/img/bg3.jpg
Binary file not shown.
Binary file removed webroot/img/bg4.jpg
Binary file not shown.
Binary file removed webroot/img/calendario.png
Binary file not shown.
Binary file removed webroot/img/contatti.png
Binary file not shown.
Binary file removed webroot/img/download.png
Diff not rendered.
Binary file removed webroot/img/emoticons/Baring_teeth_smiley.png
Diff not rendered.
Binary file removed webroot/img/emoticons/Cigarette.png
Diff not rendered.
Binary file removed webroot/img/emoticons/Dont_tell_anyone.png
Diff not rendered.
Binary file removed webroot/img/emoticons/Fingerscrossed.png
Diff not rendered.
Binary file removed webroot/img/emoticons/Hi_five.png
Diff not rendered.
Binary file removed webroot/img/emoticons/Sarcastic_smiley.png
Diff not rendered.
Binary file removed webroot/img/emoticons/Soccer_ball.png
Diff not rendered.
Binary file removed webroot/img/emoticons/Thinking_smiley.png
Diff not rendered.
Binary file removed webroot/img/emoticons/andy.png
Diff not rendered.
Binary file removed webroot/img/emoticons/angel.png
Diff not rendered.
Binary file removed webroot/img/emoticons/angry.png
Diff not rendered.
Binary file removed webroot/img/emoticons/beer.png
Diff not rendered.
Binary file removed webroot/img/emoticons/blushing.png
Diff not rendered.
Binary file removed webroot/img/emoticons/brheart.png
Diff not rendered.
Binary file removed webroot/img/emoticons/coffee.png
Diff not rendered.
Binary file removed webroot/img/emoticons/computer.png
Diff not rendered.
Binary file removed webroot/img/emoticons/confused.png
Diff not rendered.
Binary file removed webroot/img/emoticons/coolglasses.png
Diff not rendered.
Binary file removed webroot/img/emoticons/cry.png
Diff not rendered.
Binary file removed webroot/img/emoticons/cuffs.png
Diff not rendered.
Binary file removed webroot/img/emoticons/devil.png
Diff not rendered.
Binary file removed webroot/img/emoticons/drink.png
Diff not rendered.
Binary file removed webroot/img/emoticons/emblem-favorite.png
Diff not rendered.
Binary file removed webroot/img/emoticons/face-glasses.png
Diff not rendered.
Binary file removed webroot/img/emoticons/grin.png
Diff not rendered.
Binary file removed webroot/img/emoticons/kiss.png
Diff not rendered.
Binary file removed webroot/img/emoticons/lamp.png
Diff not rendered.
Binary file removed webroot/img/emoticons/mail.png
Diff not rendered.
Binary file removed webroot/img/emoticons/sick.png
Diff not rendered.
Binary file removed webroot/img/emoticons/sleeping.png
Diff not rendered.
Binary file removed webroot/img/emoticons/smile.png
Diff not rendered.
Binary file removed webroot/img/emoticons/tongue.png
Diff not rendered.
Binary file removed webroot/img/emoticons/unhappy.png
Diff not rendered.
Binary file removed webroot/img/emoticons/wink.png
Diff not rendered.
Binary file removed webroot/img/erba.jpg
Diff not rendered.
Binary file removed webroot/img/erba2.jpg
Diff not rendered.
Binary file removed webroot/img/eventi.png
Diff not rendered.
Binary file removed webroot/img/fancybox/blank.gif
Diff not rendered.
Binary file removed webroot/img/fancybox/fancybox_loading.gif
Diff not rendered.
Binary file removed webroot/img/fancybox/fancybox_overlay.png
Diff not rendered.
Binary file removed webroot/img/fancybox/fancybox_sprite.png
Diff not rendered.
Binary file removed webroot/img/fancybox/helpers/fancybox_buttons.png
Diff not rendered.
Loading

0 comments on commit d84ab35

Please sign in to comment.