Example #1
0
/**
 * generates proper text for confirmation question and deletes comments
 *
 *
 * @param mixed $comment_id (single or array)
 * @return string text for confirmation question or empty string after deletion
 */
function delete_comments($delete_comments_array = '')
{
    $text = '';
    $confirmed = false;
    if (!is_array($delete_comments_array)) {
        $delete_comments_array = array($delete_comments_array);
    }
    if (Request::submitted('yes') and Request::isPost()) {
        CSRFProtection::verifySecurityToken();
        $confirmed = true;
    }
    if ($confirmed) {
        foreach ($delete_comments_array as $comment_id) {
            $delete_comment = new StudipComment($comment_id);
            if (!$delete_comment->isNew()) {
                if (!is_object($news[$delete_comment->getValue("object_id")])) {
                    $news[$delete_comment->getValue("object_id")] = new StudipNews($delete_comment->getValue("object_id"));
                }
                // user has to have delete permission for news
                if ($news[$delete_comment->getValue("object_id")]->havePermission('delete')) {
                    $delete_comment->delete();
                    $delete_counter++;
                } else {
                    PageLayout::postMessage(MessageBox::error(_('Keine Berechtigung zum Löschen des Kommentars.')));
                }
            }
        }
        if ($delete_counter > 1) {
            PageLayout::postMessage(MessageBox::success(sprintf(_('%s Kommentare wurden gelöscht.'), $delete_counter)));
        } elseif ($delete_counter == 1) {
            PageLayout::postMessage(MessageBox::success(_('Kommentar wurde gelöscht.')));
        }
    } else {
        if (count($delete_comments_array) > 1) {
            $text = sprintf(_('Wollen Sie die %s Komentare jetzt löschen?'), count($delete_comments_array));
        } elseif (count($delete_comments_array) == 1) {
            $text = _('Wollen Sie den Kommentar jetzt löschen?');
        }
    }
    return $text;
}