.
* ---------------------------------------------------------------------
*/
if (!defined('GLPI_ROOT')) {
die("Sorry. You can't access this file directly");
}
/// Class KnowbaseItem_Comment
/// since version 9.2
class KnowbaseItem_Comment extends CommonDBTM {
static function getTypeName($nb = 0) {
return _n('Comment', 'Comments', $nb);
}
function getTabNameForItem(CommonGLPI $item, $withtemplate = 0) {
if (!$item->canUpdateItem()) {
return '';
}
$nb = 0;
if ($_SESSION['glpishow_count_on_tabs']) {
$where = [];
if ($item->getType() == KnowbaseItem::getType()) {
$where = [
'knowbaseitems_id' => $item->getID(),
'language' => null
];
} else {
$where = [
'knowbaseitems_id' => $item->fields['knowbaseitems_id'],
'language' => $item->fields['language']
];
}
$nb = countElementsInTable(
'glpi_knowbaseitems_comments',
$where
);
}
return self::createTabEntry(self::getTypeName($nb), $nb);
}
static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtemplate = 0) {
self::showForItem($item, $withtemplate);
return true;
}
/**
* Show linked items of a knowbase item
*
* @param $item CommonDBTM object
* @param $withtemplate integer withtemplate param (default 0)
**/
static function showForItem(CommonDBTM $item, $withtemplate = 0) {
global $CFG_GLPI;
// Total Number of comments
if ($item->getType() == KnowbaseItem::getType()) {
$where = [
'knowbaseitems_id' => $item->getID(),
'language' => null
];
} else {
$where = [
'knowbaseitems_id' => $item->fields['knowbaseitems_id'],
'language' => $item->fields['language']
];
}
$kbitem_id = $where['knowbaseitems_id'];
$kbitem = new KnowbaseItem();
$kbitem->getFromDB($kbitem_id);
$number = countElementsInTable(
'glpi_knowbaseitems_comments',
$where
);
$cancomment = $kbitem->canComment();
if ($cancomment) {
echo "
";
$lang = null;
if ($item->getType() == KnowbaseItemTranslation::getType()) {
$lang = $item->fields['language'];
}
echo self::getCommentForm($kbitem_id, $lang);
echo "
";
}
// No comments in database
if ($number < 1) {
$no_txt = __('No comments');
echo "";
echo "
";
echo "| $no_txt |
";
echo "
";
echo "
";
return;
}
// Output events
echo "";
}
/**
* Gat all comments for specified KB entry
*
* @param integer $kbitem_id KB entry ID
* @param string $lang Requested language
* @param integer $parent Parent ID (defaults to 0)
*
* @return array
*/
static public function getCommentsForKbItem($kbitem_id, $lang, $parent = null) {
global $DB;
$where = [
'knowbaseitems_id' => $kbitem_id,
'language' => $lang,
'parent_comment_id' => $parent
];
$db_comments = $DB->request(
'glpi_knowbaseitems_comments',
$where + ['ORDER' => 'id ASC']
);
$comments = [];
foreach ($db_comments as $db_comment) {
$db_comment['answers'] = self::getCommentsForKbItem($kbitem_id, $lang, $db_comment['id']);
$comments[] = $db_comment;
}
return $comments;
}
/**
* Display comments
*
* @param array $comments Comments
* @param boolean $cancomment Whether user can comment or not
* @param integer $level Current level, defaults to 0
*
* @return string
*/
static public function displayComments($comments, $cancomment, $level = 0) {
$html = '';
foreach ($comments as $comment) {
$user = new User();
$user->getFromDB($comment['users_id']);
$html .= "";
}
return $html;
}
/**
* Get comment form
*
* @param integer $kbitem_id Knowbase item ID
* @param string $lang Related item language
* @param false|integer $edit Comment id to edit, or false
* @param false|integer $answer Comment id to answer to, or false
* @return string
*/
static public function getCommentForm($kbitem_id, $lang = null, $edit = false, $answer = false) {
$rand = mt_rand();
$content = '';
if ($edit !== false) {
$comment = new KnowbaseItem_Comment();
$comment->getFromDB($edit);
$content = $comment->fields['comment'];
}
$html = '';
$html .= "
"; $comments = self::getCommentsForKbItem($where['knowbaseitems_id'], $where['language']); $html = self::displayComments($comments, $cancomment); echo $html; echo "
"; echo ""; echo "