ArticleTopic::GetArticlesWithTopic PHP Method

GetArticlesWithTopic() public static method

Get the Articles that have the given Topic.
public static GetArticlesWithTopic ( integer $p_topicId ) : array
$p_topicId integer
return array
    public static function GetArticlesWithTopic($p_topicId)
    {
        global $g_ado_db;
        $articleIds = array();
        $queryStr = "SELECT NrArticle FROM ArticleTopics WHERE Topicid = {$p_topicId}";
        $rows = $g_ado_db->GetAll($queryStr);
        if (is_array($rows)) {
            foreach ($rows as $row) {
                $articleIds[] = $row['NrArticle'];
            }
        }
        $queryStr = 'SELECT DISTINCT(ArticleType) FROM TopicFields';
        $rows = $g_ado_db->GetAll($queryStr);
        foreach ($rows as $row) {
            $queryStr = "SELECT FieldName FROM TopicFields WHERE ArticleType = '" . $row['ArticleType'] . "'";
            $rows2 = $g_ado_db->GetAll($queryStr);
            if (!is_array($rows2) || sizeof($rows2) == 0) {
                continue;
            }
            $columns = '';
            foreach ($rows2 as $row2) {
                $columns .= " OR F" . $row2['FieldName'] . " = {$p_topicId}";
            }
            $columns = substr($columns, 3);
            $queryStr = "SELECT DISTINCT(NrArticle) FROM X" . $row['ArticleType'] . " WHERE {$columns}";
            $rows2 = $g_ado_db->GetAll($queryStr);
            if (!is_array($rows2)) {
                continue;
            }
            foreach ($rows2 as $row2) {
                foreach ($row2 as $fieldName => $value) {
                    $articleIds[] = $value;
                }
            }
        }
        if (sizeof($articleIds) == 0) {
            return null;
        }
        $articleIds = array_unique($articleIds);
        $tmpArticle = new Article();
        $columnNames = implode(',', $tmpArticle->getColumnNames(true));
        $queryStr = "SELECT {$columnNames} FROM Articles WHERE Number IN (" . implode(', ', $articleIds) . ")";
        return DbObjectArray::Create('Article', $queryStr);
    }

Usage Example

Example #1
0
}
if (!$g_user->hasPermission('ManageTopics')) {
    camp_html_display_error(getGS("You do not have the right to delete topics."));
    exit;
}
$f_confirmed = Input::Get('f_confirmed', 'int', 0);
$f_topic_language_id = Input::Get('f_topic_language_id', 'int', 0);
$f_topic_delete_id = Input::Get('f_topic_delete_id', 'int', 0);
$errorMsgs = array();
$doDelete = true;
$deleteTopic = new Topic($f_topic_delete_id);
if ($deleteTopic->hasSubtopics()) {
    $doDelete = false;
    $errorMsgs[] = getGS('This topic has subtopics, therefore it cannot be deleted.');
}
$numArticles = count(ArticleTopic::GetArticlesWithTopic($f_topic_delete_id));
if ($numArticles > 0) {
    $doDelete = false;
    $errorMsgs[] = getGS('There are $1 articles using the topic.', $numArticles);
}
if ($f_confirmed == 1) {
    // get a list of subtopics
    $deleteTopics = $deleteTopic->getSubtopics();
    // detach all subtopics from all articles
    foreach ($deleteTopics as $topic) {
        ArticleTopic::RemoveTopicFromArticles($topic->getTopicId());
    }
    // delete all subtopics
    foreach ($deleteTopics as $topic) {
        $topic->delete($f_topic_language_id);
    }