. * --------------------------------------------------------------------- */ 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 ""; echo "
$no_txt
"; echo "
"; return; } // Output events echo "
"; echo ""; echo ""; 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 .= "
  • "; $html .= "
    "; if ($level === 0) { $html .= '
    '; } $html .= "
    "; $html .= "
    ".Html::convDateTime($comment['date_creation'])."
    "; $html .= "
    "; $html .= "
    "; $html .= ""; $html .= "
    "; $html .= ""; $userdata = getUserName($user->getID(), 2); $html .= $user->getLink()." "; $html .= Html::showToolTip($userdata["comment"], ['link' => $userdata['link'], 'display' => false]); $html .= ""; $html .= "
    "; // h_user $html .= "
    "; //h_info $html .= "
    "; $html .= "
    "; if ($cancomment) { if (Session::getLoginUserID() == $comment['users_id']) { $html .= ""; } } $html .= "
    "; $html .= "

    {$comment['comment']}

    "; $html .= "
    "; $html .= "
    "; // displayed_content if ($cancomment) { $html .= ""; } $html .= "
    "; //end h_content $html .= "
    "; if (isset($comment['answers']) && count($comment['answers']) > 0) { $html .= ""; $html .= ""; $html .= ""; } $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 .= "
    "; $html .= ""; $form_title = ($edit === false ? __('New comment') : __('Edit comment')); $html .= ""; $html .= ""; $html .= "
    $form_title
     *"; $html .= ""; $html .= ""; $btn_text = _sx('button', 'Add'); $btn_name = 'add'; if ($edit !== false) { $btn_text = _sx('button', 'Edit'); $btn_name = 'edit'; } $html .= ""; if ($edit !== false || $answer !== false) { $html .= ""; } $html .= ""; if ($lang !== null) { $html .= ""; } if ($answer !== false) { $html .= ""; } if ($edit !== false) { $html .= ""; } $html .= "
    "; $html .= Html::closeForm(false); return $html; } function prepareInputForAdd($input) { if (!isset($input["users_id"])) { $input["users_id"] = 0; if ($uid = Session::getLoginUserID()) { $input["users_id"] = $uid; } } return $input; } }