. * --------------------------------------------------------------------- */ if (!defined('GLPI_ROOT')) { die("Sorry. You can't access this file directly"); } /** * Notepad class * * @since 0.85 **/ class Notepad extends CommonDBChild { // From CommonDBChild static public $itemtype = 'itemtype'; static public $items_id = 'items_id'; public $dohistory = false; public $auto_message_on_action = false; // Link in message can't work' static public $logs_for_parent = true; static function getTypeName($nb = 0) { //TRANS: Always plural return _n('Note', 'Notes', $nb); } function getLogTypeID() { return [$this->fields['itemtype'], $this->fields['items_id']]; } function canCreateItem() { if (isset($this->fields['itemtype']) && ($item = getItemForItemtype($this->fields['itemtype']))) { return Session::haveRight($item::$rightname, UPDATENOTE); } return false; } function canUpdateItem() { if (isset($this->fields['itemtype']) && ($item = getItemForItemtype($this->fields['itemtype']))) { return Session::haveRight($item::$rightname, UPDATENOTE); } return false; } function prepareInputForAdd($input) { $input['users_id'] = Session::getLoginUserID(); $input['users_id_lastupdater'] = Session::getLoginUserID(); $input['date'] = $_SESSION['glpi_currenttime']; return $input; } function prepareInputForUpdate($input) { $input['users_id_lastupdater'] = Session::getLoginUserID(); return $input; } /** * Duplicate all notepads from a item template to his clone * * @deprecated 9.5 * @since 9.2 * * @param string $itemtype itemtype of the item * @param integer $oldid ID of the item to clone * @param integer $newid ID of the item cloned **/ static function cloneItem ($itemtype, $oldid, $newid) { global $DB; Toolbox::deprecated('Use clone'); $iterator = $DB->request([ 'FROM' => self::getTable(), 'WHERE' => [ 'items_id' => $oldid, 'itemtype' => $itemtype ] ]); while ($data = $iterator->next()) { $cd = new self(); unset($data['id']); $data['items_id'] = $newid; $data = Toolbox::addslashes_deep($data); $cd->add($data); } } function getTabNameForItem(CommonGLPI $item, $withtemplate = 0) { if (Session::haveRight($item::$rightname, READNOTE)) { $nb = 0; if ($_SESSION['glpishow_count_on_tabs']) { $nb = self::countForItem($item); } return self::createTabEntry(self::getTypeName(Session::getPluralNumber()), $nb); } return false; } /** * @param $item CommonGLPI object * @param $tabnum (default 1) * @param $withtemplate (default 0) **/ static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtemplate = 0) { static::showForItem($item, $withtemplate); } /** * @param $item CommonDBTM object * * @return number **/ static function countForItem(CommonDBTM $item) { return countElementsInTable('glpi_notepads', ['itemtype' => $item->getType(), 'items_id' => $item->getID()]); } /** * @param $item CommonDBTM object **/ static function getAllForItem(CommonDBTM $item) { global $DB; $data = []; $iterator = $DB->request([ 'SELECT' => [ 'glpi_notepads.*', 'glpi_users.picture' ], 'FROM' => self::getTable(), 'LEFT JOIN' => [ 'glpi_users' => [ 'ON' => [ self::getTable() => 'users_id_lastupdater', 'glpi_users' => 'id' ] ] ], 'WHERE' => [ 'itemtype' => $item->getType(), 'items_id' => $item->getID() ], 'ORDERBY' => 'date_mod DESC' ]); while ($note = $iterator->next()) { $data[] = $note; } return $data; } static public function rawSearchOptionsToAdd() { $tab = []; $name = _n('Note', 'Notes', Session::getPluralNumber()); $tab[] = [ 'id' => 'notepad', 'name' => $name ]; $tab[] = [ 'id' => '200', 'table' => 'glpi_notepads', 'field' => 'content', 'name' => $name, 'datatype' => 'text', 'joinparams' => [ 'jointype' => 'itemtype_item' ], 'forcegroupby' => true, 'splititems' => true, 'massiveaction' => false ]; $tab[] = [ 'id' => '201', 'table' => 'glpi_notepads', 'field' => 'date', 'name' => __('Creation date'), 'datatype' => 'datetime', 'joinparams' => [ 'jointype' => 'itemtype_item' ], 'forcegroupby' => true, 'massiveaction' => false ]; $tab[] = [ 'id' => '202', 'table' => 'glpi_users', 'field' => 'name', 'name' => __('Writer'), 'datatype' => 'dropdown', 'forcegroupby' => true, 'massiveaction' => false, 'joinparams' => [ 'beforejoin' => [ 'table' => 'glpi_notepads', 'joinparams' => [ 'jointype' => 'itemtype_item' ] ] ] ]; $tab[] = [ 'id' => '203', 'table' => 'glpi_notepads', 'field' => 'date_mod', 'name' => __('Last update'), 'datatype' => 'datetime', 'joinparams' => [ 'jointype' => 'itemtype_item' ], 'forcegroupby' => true, 'massiveaction' => false ]; $tab[] = [ 'id' => '204', 'table' => 'glpi_users', 'field' => 'name', 'linkfield' => 'users_id_lastupdater', 'name' => __('Last updater'), 'datatype' => 'dropdown', 'forcegroupby' => true, 'massiveaction' => false, 'joinparams' => [ 'beforejoin' => [ 'table' => 'glpi_notepads', 'joinparams' => [ 'jointype' => 'itemtype_item' ] ] ] ]; return $tab; } /** * Show notepads for an item * * @param $item CommonDBTM object * @param $withtemplate integer template or basic item (default 0) **/ static function showForItem(CommonDBTM $item, $withtemplate = 0) { if (!Session::haveRight($item::$rightname, READNOTE)) { return false; } $notes = static::getAllForItem($item); $rand = mt_rand(); $canedit = Session::haveRight($item::$rightname, UPDATENOTE); $showuserlink = 0; if (User::canView()) { $showuserlink = 1; } if ($canedit && !(!empty($withtemplate) && ($withtemplate == 2))) { echo "
"; echo "
"; echo "
"; echo Html::hidden('itemtype', ['value' => $item->getType()]); echo Html::hidden('items_id', ['value' => $item->getID()]); echo "
"; echo "
"; echo ""; echo "
"; echo "
"; // box notecontent echo "

"; echo Html::submit(_x('button', 'Add'), ['name' => 'add']); echo "
"; Html::closeForm(); echo "
"; // boxnote } if (count($notes)) { foreach ($notes as $note) { $id = 'note'.$note['id'].$rand; $classtoadd = ''; if ($canedit) { $classtoadd = " pointer"; } echo "
"; echo "
"; echo "\"".__s('Picture')."\""; echo "
"; // boxnoteleft echo "
"; echo "
"; $username = NOT_AVAILABLE; if ($note['users_id_lastupdater']) { $username = getUserName($note['users_id_lastupdater'], $showuserlink); } $update = sprintf(__('Last update by %1$s on %2$s'), $username, Html::convDateTime($note['date_mod'])); $username = NOT_AVAILABLE; if ($note['users_id']) { $username = getUserName($note['users_id'], $showuserlink); } $create = sprintf(__('Create by %1$s on %2$s'), $username, Html::convDateTime($note['date'])); printf(__('%1$s / %2$s'), $update, $create); echo "
"; // floatright echo "
"; $content = nl2br($note['content']); if (empty($content)) { $content = NOT_AVAILABLE; } echo $content.'
'; // boxnotetext echo "
"; // boxnotecontent echo "
"; if ($canedit) { Html::showSimpleForm(Toolbox::getItemTypeFormURL('Notepad'), ['purge' => 'purge'], _x('button', 'Delete permanently'), ['id' => $note['id']], 'fa-times-circle', '', __('Confirm the final deletion?')); } echo "
"; // boxnoteright echo "
"; // boxnote if ($canedit) { echo "
"; echo ""; echo "
"; echo "
"; echo Html::hidden('id', ['value' => $note['id']]); echo ""; echo "
"; // boxnotecontent echo "

"; echo Html::submit(_x('button', 'Update'), ['name' => 'update']); echo "
"; // boxnoteright Html::closeForm(); echo "
"; // boxnote } } } return true; } }