PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/inc/knowbaseitemtranslation.class.php

https://gitlab.com/OnBlox/OnBlox-Template
PHP | 337 lines | 301 code | 2 blank | 34 comment | 1 complexity | f67cbd686e10743bf8a4d753448e3a3a MD5 | raw file
  1. <?php
  2. /*
  3. * @version $Id$
  4. -------------------------------------------------------------------------
  5. GLPI - Gestionnaire Libre de Parc Informatique
  6. Copyright (C) 2015 Teclib'.
  7. http://glpi-project.org
  8. based on GLPI - Gestionnaire Libre de Parc Informatique
  9. Copyright (C) 2003-2014 by the INDEPNET Development Team.
  10. -------------------------------------------------------------------------
  11. LICENSE
  12. This file is part of GLPI.
  13. GLPI is free software; you can redistribute it and/or modify
  14. it under the terms of the GNU General Public License as published by
  15. the Free Software Foundation; either version 2 of the License, or
  16. (at your option) any later version.
  17. GLPI is distributed in the hope that it will be useful,
  18. but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. GNU General Public License for more details.
  21. You should have received a copy of the GNU General Public License
  22. along with GLPI. If not, see <http://www.gnu.org/licenses/>.
  23. --------------------------------------------------------------------------
  24. */
  25. /** @file
  26. * @brief
  27. */
  28. if (!defined('GLPI_ROOT')) {
  29. die("Sorry. You can't access directly to this file");
  30. }
  31. /**
  32. * KnowbaseItemTranslation Class
  33. *
  34. * @since version 0.85
  35. **/
  36. class KnowbaseItemTranslation extends CommonDBChild {
  37. static public $itemtype = 'KnowbaseItem';
  38. static public $items_id = 'knowbaseitems_id';
  39. public $dohistory = true;
  40. static $rightname = 'knowbase';
  41. static function getTypeName($nb = 0) {
  42. return _n('Translation', 'Translations', $nb);
  43. }
  44. function getForbiddenStandardMassiveAction() {
  45. $forbidden = parent::getForbiddenStandardMassiveAction();
  46. $forbidden[] = 'update';
  47. return $forbidden;
  48. }
  49. /**
  50. * @see CommonGLPI::getTabNameForItem()
  51. **/
  52. function getTabNameForItem(CommonGLPI $item, $withtemplate=0) {
  53. if (self::canBeTranslated($item)) {
  54. $nb = 0;
  55. if ($_SESSION['glpishow_count_on_tabs']) {
  56. $nb = self::getNumberOfTranslationsForItem($item);
  57. }
  58. return self::createTabEntry(self::getTypeName(Session::getPluralNumber()), $nb);
  59. }
  60. return '';
  61. }
  62. /**
  63. * @param $item CommonGLPI object
  64. * @param $tabnum (default 1)
  65. * @param $withtemplate (default 0)
  66. **/
  67. static function displayTabContentForItem(CommonGLPI $item, $tabnum=1, $withtemplate=0) {
  68. if (self::canBeTranslated($item)) {
  69. self::showTranslations($item);
  70. }
  71. return true;
  72. }
  73. /**
  74. * Display all translated field for an KnowbaseItem
  75. *
  76. * @param $item a KnowbaseItem item
  77. *
  78. * @return true;
  79. **/
  80. static function showTranslations(KnowbaseItem $item) {
  81. global $DB, $CFG_GLPI;
  82. $canedit = $item->can($item->getID(), UPDATE);
  83. $rand = mt_rand();
  84. if ($canedit) {
  85. echo "<div id='viewtranslation" . $item->getID() . "$rand'></div>\n";
  86. echo "<script type='text/javascript' >\n";
  87. echo "function addTranslation" . $item->getID() . "$rand() {\n";
  88. $params = array('type' => __CLASS__,
  89. 'parenttype' => get_class($item),
  90. 'knowbaseitems_id' => $item->fields['id'],
  91. 'id' => -1);
  92. Ajax::updateItemJsCode("viewtranslation" . $item->getID() . "$rand",
  93. $CFG_GLPI["root_doc"]."/ajax/viewsubitem.php",
  94. $params);
  95. echo "};";
  96. echo "</script>\n";
  97. echo "<div class='center'>".
  98. "<a class='vsubmit' href='javascript:addTranslation".$item->getID()."$rand();'>".
  99. __('Add a new translation')."</a></div><br>";
  100. }
  101. $obj = new self;
  102. $found = $obj->find("`knowbaseitems_id`='".$item->getID()."'", "`language` ASC");
  103. if (count($found) > 0) {
  104. if ($canedit) {
  105. Html::openMassiveActionsForm('mass'.__CLASS__.$rand);
  106. $massiveactionparams = array('container' => 'mass'.__CLASS__.$rand);
  107. Html::showMassiveActions($massiveactionparams);
  108. }
  109. echo "<div class='center'>";
  110. echo "<table class='tab_cadre_fixehov'><tr class='tab_bg_2'>";
  111. echo "<th colspan='4'>".__("List of translations")."</th></tr>";
  112. if ($canedit) {
  113. echo "<th width='10'>";
  114. Html::checkAllAsCheckbox('mass'.__CLASS__.$rand);
  115. echo "</th>";
  116. }
  117. echo "<th>".__("Language")."</th>";
  118. echo "<th>".__("Subject")."</th>";
  119. foreach ($found as $data) {
  120. echo "<tr class='tab_bg_1' ".($canedit ? "style='cursor:pointer'
  121. onClick=\"viewEditTranslation".$data['id']."$rand();\"" : '') .
  122. ">";
  123. if ($canedit) {
  124. echo "<td class='center'>";
  125. Html::showMassiveActionCheckBox(__CLASS__, $data["id"]);
  126. echo "</td>";
  127. }
  128. echo "<td>";
  129. if ($canedit) {
  130. echo "\n<script type='text/javascript' >\n";
  131. echo "function viewEditTranslation". $data["id"]."$rand() {\n";
  132. $params = array('type' => __CLASS__,
  133. 'parenttype' => get_class($item),
  134. 'knowbaseitems_id' => $item->getID(),
  135. 'id' => $data["id"]);
  136. Ajax::updateItemJsCode("viewtranslation" . $item->getID() . "$rand",
  137. $CFG_GLPI["root_doc"]."/ajax/viewsubitem.php",
  138. $params);
  139. echo "};";
  140. echo "</script>\n";
  141. }
  142. echo Dropdown::getLanguageName($data['language']);
  143. echo "</td><td>";
  144. echo $data["name"];
  145. if (isset($data['answer']) && !empty($data['answer'])) {
  146. echo "&nbsp;";
  147. Html::showToolTip(Toolbox::unclean_html_cross_side_scripting_deep($data['answer']));
  148. }
  149. echo "</td></tr>";
  150. }
  151. echo "</table>";
  152. if ($canedit) {
  153. $massiveactionparams['ontop'] = false;
  154. Html::showMassiveActions($massiveactionparams);
  155. Html::closeForm();
  156. }
  157. } else {
  158. echo "<table class='tab_cadre_fixe'><tr class='tab_bg_2'>";
  159. echo "<th class='b'>" . __("No translation found")."</th></tr></table>";
  160. }
  161. return true;
  162. }
  163. /**
  164. * Display translation form
  165. *
  166. * @param $ID field (default -1)
  167. * @param $options array
  168. */
  169. function showForm($ID=-1, $options=array()) {
  170. global $CFG_GLPI;
  171. if (isset($options['parent']) && !empty($options['parent'])) {
  172. $item = $options['parent'];
  173. }
  174. if ($ID > 0) {
  175. $this->check($ID, READ);
  176. } else {
  177. // Create item
  178. $options['itemtype'] = get_class($item);
  179. $options['knowbaseitems_id'] = $item->getID();
  180. $this->check(-1 , CREATE, $options);
  181. }
  182. Html::initEditorSystem('answer');
  183. $this->showFormHeader($options);
  184. echo "<tr class='tab_bg_1'>";
  185. echo "<td>".__('Language')."&nbsp;:</td>";
  186. echo "<td>";
  187. echo "<input type='hidden' name='knowbaseitems_id' value='".$item->getID()."'>";
  188. if ($ID > 0) {
  189. echo Dropdown::getLanguageName($this->fields['language']);
  190. } else {
  191. Dropdown::showLanguages("language",
  192. array('display_none' => false,
  193. 'value' => $_SESSION['glpilanguage'],
  194. 'used' => self::getAlreadyTranslatedForItem($item)));
  195. }
  196. echo "</td><td colspan='2'>&nbsp;</td></tr>";
  197. echo "<tr class='tab_bg_1'>";
  198. echo "<td>".__('Subject')."</td>";
  199. echo "<td colspan='3'>";
  200. echo "<textarea cols='100' rows='1' name='name'>".$this->fields["name"]."</textarea>";
  201. echo "</td></tr>\n";
  202. echo "<tr class='tab_bg_1'>";
  203. echo "<td>".__('Content')."</td>";
  204. echo "<td colspan='3'>";
  205. echo "<textarea cols='100' rows='30' id='answer' name='answer'>".$this->fields["answer"];
  206. echo "</textarea>";
  207. echo "</td></tr>\n";
  208. $this->showFormButtons($options);
  209. return true;
  210. }
  211. /**
  212. * Get a translation for a value
  213. *
  214. * @param $item item to translate
  215. * @param $field field to return (default 'name')
  216. *
  217. * @return the field translated if a translation is available, or the original field if not
  218. **/
  219. static function getTranslatedValue(KnowbaseItem $item, $field="name") {
  220. global $DB;
  221. $obj = new self;
  222. $found = $obj->find("`knowbaseitems_id` = '".$item->getID().
  223. "' AND `language` = '".$_SESSION['glpilanguage']."'");
  224. if ((count($found) > 0)
  225. && in_array($field, array('name', 'answer'))) {
  226. $first = array_shift($found);
  227. return $first[$field];
  228. }
  229. return $item->fields[$field];
  230. }
  231. /**
  232. * Is kb item translation functionnality active
  233. *
  234. * @return true if active, false if not
  235. **/
  236. static function isKbTranslationActive() {
  237. global $CFG_GLPI;
  238. return $CFG_GLPI['translate_kb'];
  239. }
  240. /**
  241. * Check if an item can be translated
  242. * It be translated if translation if globally on and item is an instance of CommonDropdown
  243. * or CommonTreeDropdown and if translation is enabled for this class
  244. *
  245. * @param item the item to check
  246. *
  247. * @return true if item can be translated, false otherwise
  248. **/
  249. static function canBeTranslated(CommonGLPI $item) {
  250. return (self::isKbTranslationActive()
  251. && $item instanceof KnowbaseItem);
  252. }
  253. /**
  254. * Return the number of translations for an item
  255. *
  256. * @param item
  257. *
  258. * @return the number of translations for this item
  259. **/
  260. static function getNumberOfTranslationsForItem($item) {
  261. return countElementsInTable(getTableForItemType(__CLASS__),
  262. "`knowbaseitems_id`='".$item->getID()."'");
  263. }
  264. /**
  265. * Get already translated languages for item
  266. *
  267. * @param item
  268. *
  269. * @return array of already translated languages
  270. **/
  271. static function getAlreadyTranslatedForItem($item) {
  272. global $DB;
  273. $tab = array();
  274. foreach ($DB->request(getTableForItemType(__CLASS__),
  275. "`knowbaseitems_id`='".$item->getID()."'") as $data) {
  276. $tab[$data['language']] = $data['language'];
  277. }
  278. return $tab;
  279. }
  280. }
  281. ?>