<?php
if (!empty($LIVE_URL['REQUEST'])) { // if valid request
require $LIVE_URL['DIR'].'/livewhale.php'; // load LiveWhale
$request=array_shift($LIVE_URL['REQUEST']); // get command name
switch($request) {
case 'tags':
$gid=array_shift($LIVE_URL['REQUEST']); // get secondary group id
if (!empty($gid)) {
// Starred Tags in a specific group id
$key='events_tags_'.$gid;
$tags = $_LW->getVariable($key);
if (empty($tags)) { // if tags not cached
$tags=[];
foreach($_LW->dbo->query('select', 'livewhale_tags.id,livewhale_tags.title', 'livewhale_tags', 'livewhale_tags.is_starred IS NOT NULL AND livewhale_tags.gid='.(int)$gid, 'livewhale_tags.title ASC')
->groupBy('livewhale_tags.id')->run() as $res2) { // fetch tags
$tags[]=[
'id'=>$res2['id'],
'title'=>trim($res2['title'])
];
};
$_LW->setVariable($key, $tags, 300); // cache for 5min
};
echo json_encode($tags);
} else {
// Starred Tags in Newsroom group
$key='events_tags';
$tags = $_LW->getVariable($key);
if (empty($tags)) { // if tags not cached
$tags=[];
foreach($_LW->dbo->query('select', 'livewhale_tags.id,livewhale_tags.title', 'livewhale_tags', 'livewhale_tags.is_starred IS NOT NULL AND livewhale_tags.gid=2', 'livewhale_tags.title ASC')
->groupBy('livewhale_tags.id')->run() as $res2) { // fetch tags
$tags[]=[
'id'=>$res2['id'],
'title'=>trim($res2['title'])
];
};
$_LW->setVariable($key, $tags, 300); // cache for 5min
};
echo json_encode($tags);
}
break;
case 'all_tags':
// All Tags
$key='events_all_tags';
$tags = $_LW->getVariable($key);
if (empty($tags)) { // if tags not cached
$tags=[];
foreach($_LW->dbo->query('select', 'livewhale_tags.id,livewhale_tags.title', 'livewhale_tags', '', 'livewhale_tags.title ASC')
->groupBy('livewhale_tags.id')->run() as $res2) { // fetch tags
$tags[]=[
'id'=>$res2['id'],
'title'=>trim($res2['title'])
];
};
$_LW->setVariable($key, $tags, 300); // cache for 5min
};
echo json_encode($tags);
break;
case 'categories':
// Starred Event Types
$key='events_categories';
$events_categories = $_LW->getVariable($key);
if (empty($events_categories)) { // if events_categories not cached
$events_categories=[];
foreach($_LW->dbo->query('select', 'livewhale_events_categories.id,livewhale_events_categories.title', 'livewhale_events_categories', '', 'livewhale_events_categories.title ASC')
->groupBy('livewhale_events_categories.title')->run() as $res2) { // fetch categories
$events_categories[]=[
'id'=>$res2['id'],
'title'=>trim($res2['title'])
];
};
$_LW->setVariable($key, $events_categories, 300); // cache for 5min
};
echo json_encode($events_categories);
break;
case 'audiences':
$key='audiences';
$audiences = $_LW->getVariable($key);
if (empty($audiences)) { // if audiences not cached
$audiences=[];
foreach($_LW->CONFIG['CUSTOM_FIELDS']['global'] as $custom_field) {
if ($custom_field['name'] == 'audience') {
$audiences = $custom_field['options'];
break;
}
}
$_LW->setVariable($key, $audiences, 300); // cache for 5min
};
echo json_encode($audiences);
break;
case 'campuses':
$key='campuses';
$campuses = $_LW->getVariable($key);
if (empty($campuses)) { // if campuses not cached
$campuses=[];
foreach($_LW->CONFIG['CUSTOM_FIELDS']['global'] as $custom_field) {
if ($custom_field['name'] == 'iu_campus') {
$campuses = $custom_field['options'];
break;
}
}
$_LW->setVariable($key, $campuses, 300); // cache for 5min
};
echo json_encode($campuses);
break;
//IU Addition - Added an API end point for Tags to include Group ID the tags belong to
case 'group_tags':
// Group Starred Tags
$gid = isset($_GET['gid']) ? $_GET['gid'] : null;
$starred = isset($_GET['starred']);
$key="taxonomy_group_tags+$gid";
$tags = $_LW->getVariable($key);
if (empty($tags)) { // if tags not cached
$tags=[];
$where = [];
if($starred) $where[] = 'livewhale_tags.is_starred IS NOT NULL';
if($gid) $where[] = "livewhale_tags.gid =$gid";
$where = count($where) ? implode(' AND ', $where) : 1;
foreach($_LW->dbo->query('select', 'livewhale_tags.id,livewhale_tags.gid,livewhale_tags.title', 'livewhale_tags', $where, 'livewhale_tags.title ASC')
->groupBy('livewhale_tags.id')->run() as $res2) { // fetch tags
$tags[]=[
'id'=>$res2['id'],
'gid'=>$res2['gid'],
'name'=>trim($res2['title'])
];
};
$_LW->setVariable($key, $tags, 300); // cache for 5min
};
echo json_encode($tags);
break;
};
};
exit;
?>