Skip to content

Commit 8712657

Browse files
committed
Bugfixes
Fixed creation of date in IfmsaPerson entity which was throwing exceptions. Fixed nullptr dereference in events presenter.
1 parent d3d8e4b commit 8712657

File tree

6 files changed

+42
-10
lines changed

6 files changed

+42
-10
lines changed

app/forms/ContactPersonsFormsFactory.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use App\Model\Repository\Users;
1212
use App\Model\Repository\CpAssignedAfs;
1313
use App\Model\Repository\IfmsaPersons;
14+
use App\Helpers\Date\DateHelper;
1415

1516
/**
1617
* Class containing factory methods for forms mainly concerning contact persons
@@ -36,6 +37,8 @@ class ContactPersonsFormsFactory extends Nette\Object
3637
private $httpRequest;
3738
/** @var App\Users\MyAuthorizator */
3839
private $myAuthorizator;
40+
/** @var DateHelper */
41+
private $dateHelper;
3942

4043
/**
4144
* DI Constructor.
@@ -48,6 +51,7 @@ class ContactPersonsFormsFactory extends Nette\Object
4851
* @param IfmsaPersons $ifmsaPersons
4952
* @param Nette\Http\Request $httpRequest
5053
* @param App\Users\MyAuthorizator $myAuthorizator
54+
* @param DateHelper $dateHelper
5155
*/
5256
public function __construct(
5357
UserManager $userManager,
@@ -58,7 +62,8 @@ public function __construct(
5862
App\Helpers\IfmsaConnectionHelper $ifmsaConnectionHelper,
5963
IfmsaPersons $ifmsaPersons,
6064
Nette\Http\Request $httpRequest,
61-
App\Users\MyAuthorizator $myAuthorizator
65+
App\Users\MyAuthorizator $myAuthorizator,
66+
DateHelper $dateHelper
6267
) {
6368

6469
$this->user = $userManager->getCurrentUser();
@@ -70,6 +75,7 @@ public function __construct(
7075
$this->ifmsaPersons = $ifmsaPersons;
7176
$this->httpRequest = $httpRequest;
7277
$this->myAuthorizator = $myAuthorizator;
78+
$this->dateHelper = $dateHelper;
7379
}
7480

7581
public function createFindContactPersonForm($afNumber)
@@ -221,7 +227,7 @@ private function getCardAndUpdate($afNumber, $accm, & $personInfo, array & $card
221227
$person->surname = $personInfo['surname'];
222228
$person->email = $personInfo['email'];
223229
$person->photo = $personInfo['jpgPath'];
224-
$person->afArrival = new \DateTime($personInfo['arrivalDate']);
230+
$person->afArrival = $this->dateHelper->createDateOrDefault($personInfo['arrivalDate'])->typed;
225231
$this->ifmsaPersons->flush();
226232
} else {
227233
$person = new IfmsaPerson(
@@ -231,7 +237,7 @@ private function getCardAndUpdate($afNumber, $accm, & $personInfo, array & $card
231237
$personInfo['surname'],
232238
$personInfo['email'],
233239
$personInfo['jpgPath'],
234-
$personInfo['arrivalDate']
240+
$this->dateHelper->createDateOrDefault($personInfo['arrivalDate'])->typed
235241
);
236242
$this->ifmsaPersons->persist($person);
237243
}

app/helpers/date/DateHelper.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,21 @@ public function createFromDateOrLastDayOfMonth($text): DateTimeHolder
3939
$holder->textual = $holder->typed->format("j. n. Y");
4040
return $holder;
4141
}
42+
43+
/**
44+
* Create date from given string, if not possible create fallback date (0000-00-00 00:00:00)
45+
* @param string $text
46+
* @return \App\Helpers\Date\DateTimeHolder
47+
*/
48+
public function createDateOrDefault($text): DateTimeHolder {
49+
$holder = new DateTimeHolder;
50+
try {
51+
$date = new \DateTime($text);
52+
} catch (\Exception $e) {
53+
$date = new \DateTime("0000-00-00 00:00:00");
54+
}
55+
$holder->typed = $date;
56+
$holder->textual = $holder->typed->format("j. n. Y");
57+
return $holder;
58+
}
4259
}

app/model/entity/IfmsaPerson.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,18 +74,22 @@ public function __construct(
7474
$surname = '',
7575
$email = '',
7676
$photo = '',
77-
$afArrival = '0000-00-00 00:00:00',
77+
\DateTime $afArrival = null,
7878
$accommodation = '',
7979
$department = ''
8080
) {
81+
if (!$afArrival) {
82+
$afArrival = new \DateTime("0000-00-00 00:00:00");
83+
}
84+
8185
$this->afNumber = $afNumber;
8286
$this->confirmationNumber = $confirmationNumber;
8387
$this->firstname = $firstname;
8488
$this->surname = $surname;
8589
$this->email = $email;
8690
$this->photo = $photo;
87-
$this->afArrival = new \DateTime($afArrival);
8891
$this->accommodation = $accommodation;
8992
$this->department = $department;
93+
$this->afArrival = $afArrival;
9094
}
9195
}

app/model/repository/Users.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Model\Repository;
44

55
use Kdyby\Doctrine\EntityManager;
6+
use App\Exceptions\NotFoundException;
67
use App\Model\Entity\User;
78
use App\Model\Entity\Faculty;
89

app/presenters/EventsPresenter.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,10 +244,8 @@ public function actionModifyEvent($id)
244244
public function actionSignUp($id)
245245
{
246246
$event = $this->events->findOrThrow($id);
247-
$signed = $this->currentUser->getParticipatedEvent($event);
248247
if (!$this->isLoggedIn() ||
249-
!$this->user->isAllowed("Event", "sign") ||
250-
!$event || $signed ||
248+
!$this->user->isAllowed("Event", "sign") || !$event ||
251249
!$this->myAuthorizator->isAllowedEvent('signUp', $event)) {
252250
$this->error('Access Denied');
253251
}

app/presenters/IfmsaPresenter.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use App\Model\Repository\CpAssignedAfs;
99
use App\Model\Repository\IfmsaPersons;
1010
use App\Exceptions\IfmsaConnectionException;
11+
use App\Helpers\Date\DateHelper;
1112

1213
/**
1314
* Ifmsa presenter.
@@ -49,6 +50,11 @@ class IfmsaPresenter extends BasePresenter
4950
* @inject
5051
*/
5152
public $ifmsaPersons;
53+
/**
54+
* @var DateHelper
55+
* @inject
56+
*/
57+
public $dateHelper;
5258

5359
protected function startup()
5460
{
@@ -232,7 +238,7 @@ public function actionPerson($afNumber)
232238
$person->surname = $personInfo['surname'];
233239
$person->email = $personInfo['email'];
234240
$person->photo = $personInfo['jpgPath'];
235-
$person->afArrival = $personInfo['arrivalDate'];
241+
$person->afArrival = $this->dateHelper->createDateOrDefault($personInfo['arrivalDate'])->typed;
236242
} else {
237243
$person = new IfmsaPerson(
238244
$afNumber,
@@ -241,7 +247,7 @@ public function actionPerson($afNumber)
241247
$personInfo['surname'],
242248
$personInfo['email'],
243249
$personInfo['jpgPath'],
244-
$personInfo['arrivalDate']
250+
$this->dateHelper->createDateOrDefault($personInfo['arrivalDate'])->typed
245251
);
246252
$this->ifmsaPersons->persist($person);
247253
}

0 commit comments

Comments
 (0)