File: //home/awaldron/_phpassport/api.pennhillspassport.com/v1/events.php
<?php
//**********************************************************************************************************//
// Method: POST
// Endpoint: events/group
function post_v1_events_group( $args, $body, $uid, $admin_level, $db ) {
$rules = [
'minDate' => 'req',
'maxDate' => 'req',
];
validate_body( $body, $rules );
if (empty($body['chIds'])) {
$body['chIds'] = [];
}
$res = qSet( $db, "SELECT e.id AS evId, e.title AS evTitle, e.description AS evDesc, e.url AS evURL,
c.id AS chId, c.name AS chName, c.slug AS chSlug, c.avatar AS chAvatar, c.color_light AS chColorLight,
c.color_dark AS chColorDark, COUNT(ed.event_id) AS num_dates
FROM events as e
LEFT JOIN event_dates as ed ON e.id = ed.event_id AND ed.date_start >= '".$body['minDate']."' AND ed.date_start <= '".$body['maxDate']."'
LEFT JOIN channels as c ON e.channel_id = c.id
LEFT JOIN users as u ON u.user_id = e.user_id
WHERE e.approved=1 AND e.hidden = 0 " .
(count($body['chIds']) > 0 ? "AND e.channel_id IN (".implode(',',$body['chIds']).")" : "") . "
GROUP BY e.id
ORDER BY e.title ASC");
$data['records'] = $res;
$data['results'] = count($res);
response(200, ['success'=>true, 'data'=>$data, 'al'=>$admin_level] );
}
//**********************************************************************************************************//
// Method: GET
// Endpoint: events/group
function get_v1_events_edit( $args, $body, $uid, $admin_level, $db ) {
$res = qSet( $db, "SELECT ed.id AS dateId, ed.description as dateDescription, ed.date_start AS dateStart, ed.date_end as dateEnd, ed.all_day as dateAllDay, ed.views as edViews, ed.shares as edShares,
e.id AS evId, e.title AS evTitle, e.description AS evDesc, e.url AS evURL,
l.id AS locId, l.name AS locName, l.add1 AS locAdd1, l.add2 AS locAdd2,
l.city AS locCity, l.state AS locState, l.zip AS locZip,
c.id AS chId, c.name AS chName, c.slug AS chSlug, c.avatar AS chAvatar, c.color_light AS chColorLight, c.color_dark AS chColorDark,
u.lname as uLname, u.fname as uFname
FROM event_dates as ed, locations as l, events as e, channels as c, users as u
WHERE ed.location_id=l.id AND ed.event_id=e.id
AND e.channel_id=c.id
AND ed.user_id = u.user_id
AND ed.event_id=".$args[0]."
AND e.id=ed.event_id
ORDER BY ed.date_start ASC");
response(200, ['success'=>true, 'data'=>$res, 'al'=>$admin_level] );
}
//**********************************************************************************************************//
// Method: POST
// Endpoint: events/list
function post_v1_events_list( $args, $body, $uid, $admin_level, $db ) {
$rules = [
'minDate' => 'req',
'maxDate' => 'req',
];
validate_body( $body, $rules );
if (count($body['chIds']) === 0 ) {
$data['records'] = array();
$data['results'] = 0;
response(200, ['success'=>true, 'data'=>$data, 'chIds'=>$body['chIds'], 'al'=>$admin_level] );
}
else {
$res = qSet( $db, "SELECT ed.id AS dateId, ed.date_start AS dateStart, ed.date_end as dateEnd, ed.all_day as dateAllDay, ed.views as edViews, ed.shares as edShares,
e.id AS evId, e.title AS evTitle, e.description AS evDesc, e.url AS evURL,
l.id AS locId, l.name AS locName, l.add1 AS locAdd1, l.add2 AS locAdd2,
l.city AS locCity, l.state AS locState, l.zip AS locZip, l.lat AS locLat, l.lng AS locLng,
c.id AS chId, c.name AS chName, c.slug AS chSlug, c.avatar AS chAvatar, c.color_light AS chColorLight, c.color_dark AS chColorDark,
u.lname as uLname, u.fname as uFname
FROM event_dates as ed, locations as l, events as e, channels as c, users as u
WHERE ed.date_start >= '".$body['minDate']."'
AND ed.date_start <= '".$body['maxDate']."'
AND ed.location_id=l.id AND ed.event_id=e.id
AND e.channel_id=c.id
AND e.user_id = u.user_id
AND e.approved=1 " .
(count($body['chIds']) > 0 ? "AND e.channel_id IN (".implode(',',$body['chIds']).")" : "") .
" ORDER BY ed.date_start ASC");
//(count($body['chIds']) > 0 ? "AND e.channel_id IN (".implode(',',$body['chIds']).")" : "") .
$data['records'] = $res;
$data['results'] = count($res);
response(200, ['success'=>true, 'data'=>$data, 'chIds'=>$body['chIds'], 'al'=>$admin_level] );
}
}
//**********************************************************************************************************//
// Method: POST
// Endpoint: events/info
function post_v1_events_info( $args, $body, $uid, $admin_level, $db )
{
$rules = [
'evTitle' => 'req',
'evDescription' => 'req',
'chId' => 'req',
'userId' => 'req',
];
validate_body($body, $rules);
try {
$ins = "INSERT INTO events (`title`, `description`, `url`, `channel_id`, `user_id`, `approved`) VALUES (";
$ins .= "'".addslashes($body['evTitle'])."', ";
$ins .= "'".addslashes($body['evDescription'])."', ";
$ins .= "'".$body['evUrl']."', ";
$ins .= $body['chId'].", ";
$ins .= $body['userId'].", ";
//$ins .= $admin_level > 0 ? "1" : "0";
$ins .= "1)"; // approved
$db->query( $ins );
$eventId = qVal( $db, "SELECT MAX(id) FROM events");
response(200, ['success'=>true, 'eventId'=>$eventId] );
}
catch (Exception $e) {
response(200, ['success'=>false, 'errors'=>[$e->getMessage()]] );
}
}
//**********************************************************************************************************//
// Method: POST
// Endpoint: events/info
function put_v1_events_info( $args, $body, $uid, $admin_level, $db )
{
$rules = [
'evId' => 'req',
'evTitle' => 'req',
'evDescription' => 'req',
'evURL' => 'req',
'chId' => 'req',
'userId' => 'req'
];
validate_body($body, $rules);
if ($admin_level >= 100 || $uid === $body['userId']) {
try {
$upd = "UPDATE events SET ";
$upd .= "title='" . addslashes($body['evTitle']) . "', ";
$upd .= "description='" . addslashes($body['evDescription']) . "', ";
$upd .= "url='" . $body['evURL'] . "', ";
$upd .= "channel_id=".$body['chId'] . " ";
$upd .= "WHERE id=".$body['evId'];
$db->query( $upd );
response(200, ['success'=>true] );
} catch (Exception $e) {
response(200, ['success' => false, 'errors' => [$e->getMessage()]]);
}
}
else {
response(200, ['success' => false, 'errors' => ['Unauthorized']]);
}
}
//**********************************************************************************************************//
// Method: POST
// Endpoint: events/date
function post_v1_events_date( $args, $body, $uid, $admin_level, $db )
{
$rules = [
'evId' => 'req',
'dateStart' => 'req',
'dateEnd' => 'req',
'locId' => 'req',
'userId' => 'req',
'evDescription' => 'req',
];
validate_body($body, $rules);
try {
$ins = "INSERT INTO event_dates (`user_id`, `event_id`, `description`, `date_start`, `date_end`, `location_id`) VALUES (";
$ins .= "'".$body['userId']."', ";
$ins .= "'".$body['evId']."', ";
$ins .= "'" . addslashes($body['evDescription']) . "', ";
$ins .= "'".$body['dateStart']."', ";
$ins .= "'".$body['dateEnd']."', ";
$ins .= $body['locId'];
$ins .= ")";
$db->query( $ins );
response(200, ['success'=>true] );
}
catch (Exception $e) {
response(200, ['success'=>false, 'errors'=>[$e->getMessage()]] );
}
}
//**********************************************************************************************************//
// Method: PUT
// Endpoint: events/info
function put_v1_events_date( $args, $body, $uid, $admin_level, $db )
{
$rules = [
'edId' => 'req',
'evId' => 'req',
'dateStart' => 'req',
'dateEnd' => 'req',
'locId' => 'req',
'userId' => 'req',
'evDescription' => 'req',
];
validate_body($body, $rules);
if ($admin_level >= 100 || $uid === $body['userId']) {
try {
$upd = "UPDATE event_dates SET ";
$upd .= "user_id='" . $body['userId'] . "', ";
$upd .= "event_id='" . $body['evId'] . "', ";
$upd .= "date_start='" . $body['dateStart'] . "', ";
$upd .= "date_end='" . $body['dateEnd'] . "', ";
$upd .= "location_id=".$body['locId'] . ", ";
$upd .= "description='" . addslashes($body['evDescription']) . "' ";
$upd .= "WHERE id=".$body['edId'];
$db->query( $upd );
response(200, ['success'=>true] );
} catch (Exception $e) {
response(200, ['success' => false, 'errors' => [$e->getMessage()]]);
}
}
else {
response(200, ['success' => false, 'errors' => ['Unauthorized']]);
}
}
//**********************************************************************************************************//
// Method: DELETE
// Endpoint: date
// Params: id
function delete_v1_events_date( $args, $body, $uid, $is_admin, $db ) {
try {
// CHECK IF USER IS ADMIN OF CHANNEL
$channel_id = qVal( $db, "SELECT ev.channel_id FROM events as ev, event_dates as ed WHERE ed.event_id=ev.id AND ed.id=".$args[0]);
if (!$channel_id) {
response(200, ['success'=>false, 'errors'=>['Event Date ID not found.']] );
}
$is_admin = qVal( $db, "SELECT id FROM channel_admins WHERE user_id=".$uid." AND channel_id=".$channel_id);
if ($is_admin) {
$db->query( "DELETE FROM event_dates WHERE id=".$args[0] );
response(200, ['success'=>true]);
}
else {
response(200, ['success'=>false, 'errors'=>['User not authorized to delete this event date.']] );
}
}
catch (Exception $e) {
response(200, ['success'=>false, 'errors'=>[$e->getMessage()]] );
}
}
//**********************************************************************************************************//
// Method: DELETE
// Endpoint: info
// Params: id
function delete_v1_events_info( $args, $body, $uid, $is_admin, $db ) {
try {
// CHECK IF USER IS ADMIN OF CHANNEL
$channel_id = qVal( $db, "SELECT ev.channel_id FROM events as ev WHERE ev.id=".$args[0]);
if (!$channel_id) {
response(200, ['success'=>false, 'errors'=>['Event ID not found.']] );
}
$is_admin = qVal( $db, "SELECT id FROM channel_admins WHERE user_id=".$uid." AND channel_id=".$channel_id);
if ($is_admin) {
$db->query( "DELETE FROM events WHERE id=".$args[0] );
response(200, ['success'=>true]);
}
else {
response(200, ['success'=>false, 'errors'=>['User not authorized to delete this event.']] );
}
}
catch (Exception $e) {
response(200, ['success'=>false, 'errors'=>[$e->getMessage()]] );
}
}