Skip to content

Commit

Permalink
events can have a different owner (#1190)
Browse files Browse the repository at this point in the history
* feat: events can have a different owner
  • Loading branch information
alyxbb authored and hitime1234 committed Nov 20, 2024
1 parent 8190ec2 commit 50e768d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
44 changes: 37 additions & 7 deletions src/Classes/ServiceAPI/MyRadio_Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,21 @@ public function setEndTime(int $endTime): void
$this->updateCacheObject();
}

/**
* @param int
*/
public function setHostId(int $hostId): void
{
self::$db->query('UPDATE public.events
SET hostid = $2
WHERE eventid = $1', [
$this->getID(),
$hostId
]);
$this->hostId = $hostId;
$this->updateCacheObject();
}

/**
* @return string
*/
Expand Down Expand Up @@ -184,7 +199,7 @@ public function getHost(): MyRadio_User
*/
public function update(array $data)
{
$requiredFields = ['title', 'description_html', 'start_time', 'end_time'];
$requiredFields = ['title', 'description_html', 'start_time', 'end_time', 'host'];
foreach ($requiredFields as $field) {
if (!(isset($data[$field]))) {
throw new MyRadioException("Missing $field", 400);
Expand All @@ -203,21 +218,24 @@ public function update(array $data)
SET title = $2,
description_html = $3,
start_time = $4,
end_time = $5
end_time = $5,
hostid = $6
WHERE eventid = $1',
[
$this->getID(),
$data['title'],
$data['description_html'],
CoreUtils::getTimestamp($data['start_time']),
CoreUtils::getTimestamp($data['end_time'])
CoreUtils::getTimestamp($data['end_time']),
$data['host']->getID()
]
);

$this->title = $data['title'];
$this->descriptionHtml = $data['description_html'];
$this->startTime = $data['start_time'];
$this->endTime = $data['end_time'];
$this->hostId = $data['host']->getID();

$this->updateCacheObject();
}
Expand Down Expand Up @@ -263,7 +281,7 @@ public static function getInRange($start, $end)
public static function create($data = [])
{
// Validate
$requiredFields = ['title', 'description_html', 'start_time', 'end_time'];
$requiredFields = ['title', 'description_html', 'start_time', 'end_time', 'host'];
foreach ($requiredFields as $field) {
if (!(isset($data[$field]))) {
throw new MyRadioException("Missing $field", 400);
Expand All @@ -277,15 +295,14 @@ public static function create($data = [])
}
}

$hostid = MyRadio_User::getCurrentOrSystemUser()->getID();

$sql = "INSERT INTO public.events (title, description_html, start_time, end_time, hostid)
VALUES ($1, $2, $3, $4, $5) RETURNING eventid";

$result = self::$db->fetchColumn($sql, [
$data['title'], $data['description_html'],
CoreUtils::getTimestamp($data['start_time']), CoreUtils::getTimestamp($data['end_time']),
$hostid
$data['host']->getID()
]);

return self::factory($result[0]);
Expand Down Expand Up @@ -352,6 +369,18 @@ public static function getForm()
'label' => 'End Time',
]
)
)
->addField(
new MyRadioFormField(
'host',
MyRadioFormField::TYPE_MEMBER,
[
'required' => true,
'label' => 'Organizer',
'explanation' => 'The person running the event',
]

)
);
}

Expand All @@ -365,7 +394,8 @@ public function getEditForm()
'title' => $this->getTitle(),
'description_html' => $this->getDescriptionHtml(),
'start_time' => date('d/m/Y H:i', $this->getStartTime()),
'end_time' => date('d/m/Y H:i', $this->getEndTime())
'end_time' => date('d/m/Y H:i', $this->getEndTime()),
'host' => MyRadio_User::getInstance($this->getHostId()),
]
);
}
Expand Down
1 change: 1 addition & 0 deletions src/Controllers/Events/editEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
} else {
// creating new
MyRadio_Event::getForm()
->setFieldValue('host', MyRadio_User::getInstance())
->render();
}
}

0 comments on commit 50e768d

Please sign in to comment.