/*
{"params":{"targetId":"integer"}}
*/
function getTagByAlias($params){
/*
Check necessary param consistency
*/
if ( !isset($params['targetId']) ) {
try {
$erLogData = new ErLogData(
'API_INTERNAL_PARAM_TRANSMISSION_ERROR',
57,
get_class($e),
__FILE__,
__CLASS__,
__FUNCTION__,
__LINE__,
date('Y-m-d H:i:s')
);
$erLog = ErLogFactory::create($erLogData, $e);
$response = json_encode(array('response' => array('error' => ['message' => $erLog->_user_message, 'code' => $erlog->_code])), JSON_UNESCAPED_UNICODE);
throw $erLog;
}
catch (ErLog $ee) {
ErLog::full_log_v1($ee);
return $response;
}
}
if ( !isset($params['tagAlias']) ) {
try {
$erLogData = new ErLogData(
'API_INTERNAL_PARAM_TRANSMISSION_ERROR',
57,
get_class($e),
__FILE__,
__CLASS__,
__FUNCTION__,
__LINE__,
date('Y-m-d H:i:s')
);
$erLog = ErLogFactory::create($erLogData, $e);
$response = json_encode(array('response' => array('error' => ['message' => $erLog->_user_message, 'code' => $erlog->_code])), JSON_UNESCAPED_UNICODE);
throw $erLog;
}
catch (ErLog $ee) {
ErLog::full_log_v1($ee);
return $response;
}
}
if ( !isset($params['getFullArticles']) ) {
try {
$erLogData = new ErLogData(
'API_INTERNAL_PARAM_TRANSMISSION_ERROR',
57,
get_class($e),
__FILE__,
__CLASS__,
__FUNCTION__,
__LINE__,
date('Y-m-d H:i:s')
);
$erLog = ErLogFactory::create($erLogData, $e);
$response = json_encode(array('response' => array('error' => ['message' => $erLog->_user_message, 'code' => $erlog->_code])), JSON_UNESCAPED_UNICODE);
throw $erLog;
}
catch (ErLog $ee) {
ErLog::full_log_v1($ee);
return $response;
}
}
if ($params['tagAlias']=='all-tags') {
$whereClause = '';
} else {
$params['tagAlias'] = "'" . $params['tagAlias'] . "'";
$whereClause = 'AND at.alias =' . $params['tagAlias'];
}
if ($params['getFullArticles']==0) {
$fullClause = '';
} else {
$fullClause = '
,article.creation_date as created,
article.html_text as content,
article.main_picture as main_picture,
article.feed_picture as feed_picture,
article.title as title,
article.subtitle as subtitle,
article."alias" as "slug",
article.creation_date as published,
article.author_id as author_id
';
}
/*
Language - setting param by default
*/
if (!isset($params['language']) ) {
$params['language'] = 1;
}
if ( !is_int($params['language']) ) {
try {
$erLogData = new ErLogData(
'LANGUAGE IS NOT INT',
58,
get_class($e),
__FILE__,
__CLASS__,
__FUNCTION__,
__LINE__,
date('Y-m-d H:i:s')
);
$erLog = ErLogFactory::create($erLogData, $e);
$response = json_encode(array('response' => array('error' => ['message' => $erLog->_user_message, 'code' => $erlog->_code])), JSON_UNESCAPED_UNICODE);
throw $erLog;
}
catch (ErLog $ee) {
ErLog::full_log_v1($ee);
return $response;
}
}
if (is_file('../Credentials/db_credentials.php')){
include '../Credentials/db_credentials.php';
}
else {
exit("No ../Credentials/db_credentials.php credentials available");
}
$credentials =
[
'host' => $host,
'db' => $db,
'user' => $user,
'pass' => $pass,
];
/*
Create a new article
*/
try {
$queryString =
"
SELECT
to_json((a)) as tag_info
FROM
(
SELECT
to_json(\"array_agg\"(b)) as tag_data,
(SELECT publication_targets.title FROM publication_targets WHERE publication_targets.\"id\" = ".$params['targetId'].") as publication_target_title,
(SELECT publication_targets.\"id\" FROM publication_targets WHERE publication_targets.\"id\" = ".$params['targetId'].") as publication_target_id
FROM
(
SELECT
raw_data.*,
(SELECT to_json(array_agg(f)) FROM (
SELECT
article_list.*,
(SELECT to_json(array_agg(e)) FROM (
SELECT
author.\"id\" as author_id,
author.alias as author_alias,
author.contact_json as author_contacts,
author.description as author_description,
author.header_picture as main_picture
FROM
author
WHERE
author.\"id\" = article_list.article_author_id
) e) as author_info
FROM
(
SELECT DISTINCT
article.\"id\" as article_id,
article.author_id as article_author_id
".$fullClause."
FROM
article_tags
JOIN article_publication ON article_tags.\"id\" = raw_data.tag_id AND article_tags.\"id\" = article_publication.tag_id AND article_publication.is_actual = 't' AND article_publication.language_id = ".$params['language']."
JOIN article ON article.\"id\" = article_publication.article_id
) as article_list
) f) as article_list,
(
SELECT
count(article.\"id\") as article_count
FROM
article_tags
JOIN article_publication ON article_tags.\"id\" = raw_data.tag_id AND article_tags.\"id\" = article_publication.tag_id AND article_publication.is_actual = 't' AND article_publication.language_id = ".$params['language']."
JOIN article ON article.\"id\" = article_publication.article_id
) as article_count,
(SELECT to_json(array_agg(f)) FROM (
SELECT DISTINCT
article.author_id as author_id,
author.alias as author_alias,
author.contact_json::TEXT as author_contacts,
author.description as author_description,
author.header_picture as main_picture
FROM
article_tags
JOIN article_publication ON article_tags.\"id\" = raw_data.tag_id AND article_tags.\"id\" = article_publication.tag_id AND article_publication.is_actual = 't' AND article_publication.language_id = ".$params['language']."
JOIN article ON article.\"id\" = article_publication.article_id
JOIN author ON author.id = article.author_id
) f) as author_list,
array_to_json(
array[
json_build_object (
'type',
'rel',
'key',
'canonical',
'content',
'spark-in.me/tag/'||raw_data.tag_alias
),
json_build_object (
'type',
'name',
'key',
'title',
'content',
raw_data.tag_title
),
json_build_object (
'type',
'name',
'key',
'description',
'content',
raw_data.tag_description
),
json_build_object (
'type',
'property',
'key',
'og:site_name',
'content',
'Spark in me'
),
json_build_object (
'type',
'property',
'key',
'og:title',
'content',
raw_data.tag_title
),
json_build_object (
'type',
'property',
'key',
'og:url',
'content',
'spark-in.me/tag/'||raw_data.tag_alias
),
json_build_object (
'type',
'property',
'key',
'og:description',
'content',
raw_data.tag_description
)
]
) as tag_meta
FROM
(
SELECT DISTINCT
\"at\".\"id\" as tag_id,
at.\"alias\" as tag_alias,
at.title as tag_title,
at.header_picture as main_picture,
at.description as tag_description,
att.title as att_title,
att.colour as att_colour,
att.description as att_description,
att.sort_order as att_sort_order
FROM
article_tags at
JOIN article_tag_types att ON at.tag_type_id = att.id
JOIN article_publication ap ON at.\"id\" = ap.tag_id AND ap.language_id = ".$params['language']."
JOIN publication_targets pt ON pt.\"id\" = ap.target_id
WHERE 1=1
AND pt.\"id\" = ".$params['targetId']."
".$whereClause."
) raw_data
ORDER BY
7 DESC
) b
) a
";
} catch (Exception $e){
try {
$erLogData = new ErLogData(
'API_QUERY_CONSTRUCTION_ERROR',
59,
get_class($e),
__FILE__,
__CLASS__,
__FUNCTION__,
__LINE__,
date('Y-m-d H:i:s')
);
$erLog = ErLogFactory::create($erLogData, $e);
$response = json_encode(array('response' => array('error' => ['message' => $erLog->_user_message, 'code' => $erlog->_code])), JSON_UNESCAPED_UNICODE);
throw $erLog;
}
catch (ErLog $ee) {
ErLog::full_log_v1($ee);
return $response;
}
}
$result = queryWrapper ($credentials, $queryString);
return $result;
}