ui.htmleditorwidget.class.inc.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. // Copyright (C) 2010-2015 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. /**
  19. * Class UIHTMLEditorWidget
  20. * UI wdiget for displaying and editing one-way encrypted passwords
  21. *
  22. * @author Phil Eddies
  23. * @author Romain Quetiez
  24. * @copyright Copyright (C) 2010-2015 Combodo SARL
  25. * @license http://opensource.org/licenses/AGPL-3.0
  26. */
  27. class UIHTMLEditorWidget
  28. {
  29. protected $m_iId;
  30. protected $m_oAttDef;
  31. protected $m_sAttCode;
  32. protected $m_sNameSuffix;
  33. protected $m_sFieldPrefix;
  34. protected $m_sHelpText;
  35. protected $m_sValidationField;
  36. protected $m_sValue;
  37. protected $m_sMandatory;
  38. public function __construct($iInputId, $oAttDef, $sNameSuffix, $sFieldPrefix, $sHelpText, $sValidationField, $sValue, $sMandatory)
  39. {
  40. $this->m_iId = $iInputId;
  41. $this->m_oAttDef = $oAttDef;
  42. $this->m_sAttCode = $oAttDef->GetCode();
  43. $this->m_sNameSuffix = $sNameSuffix;
  44. $this->m_sHelpText = $sHelpText;
  45. $this->m_sValidationField = $sValidationField;
  46. $this->m_sValue = $sValue;
  47. $this->m_sMandatory = $sMandatory;
  48. $this->m_sFieldPrefix = $sFieldPrefix;
  49. }
  50. /**
  51. * Get the HTML fragment corresponding to the HTML editor widget
  52. * @param WebPage $oP The web page used for all the output
  53. * @param Hash $aArgs Extra context arguments
  54. * @return string The HTML fragment to be inserted into the page
  55. */
  56. public function Display(WebPage $oPage, $aArgs = array())
  57. {
  58. $iId = $this->m_iId;
  59. $sCode = $this->m_sAttCode.$this->m_sNameSuffix;
  60. $sValue = $this->m_sValue;
  61. $sHelpText = $this->m_sHelpText;
  62. $sValidationField = $this->m_sValidationField;
  63. $sHtmlValue = "<table><tr><td><textarea class=\"htmlEditor\" title=\"$sHelpText\" name=\"attr_{$this->m_sFieldPrefix}{$sCode}\" rows=\"10\" cols=\"10\" id=\"$iId\">$sValue</textarea></td><td>$sValidationField</td></tr></table>";
  64. // Replace the text area with CKEditor
  65. // To change the default settings of the editor,
  66. // a) edit the file /js/ckeditor/config.js
  67. // b) or override some of the configuration settings, using the second parameter of ckeditor()
  68. $aConfig = array();
  69. $sLanguage = strtolower(trim(UserRights::GetUserLanguage()));
  70. $aConfig['language'] = $sLanguage;
  71. $aConfig['contentsLanguage'] = $sLanguage;
  72. $aConfig['extraPlugins'] = 'disabler';
  73. $sWidthSpec = addslashes(trim($this->m_oAttDef->GetWidth()));
  74. if ($sWidthSpec != '')
  75. {
  76. $aConfig['width'] = $sWidthSpec;
  77. }
  78. $sHeightSpec = addslashes(trim($this->m_oAttDef->GetHeight()));
  79. if ($sHeightSpec != '')
  80. {
  81. $aConfig['height'] = $sHeightSpec;
  82. }
  83. $sConfigJS = json_encode($aConfig);
  84. $oPage->add_ready_script("$('#$iId').ckeditor(function() { /* callback code */ }, $sConfigJS);"); // Transform $iId into a CKEdit
  85. // Please read...
  86. // ValidateCKEditField triggers a timer... calling itself indefinitely
  87. // This design was the quickest way to achieve the field validation (only checking if the field is blank)
  88. // because the ckeditor does not fire events like "change" or "keyup", etc.
  89. // See http://dev.ckeditor.com/ticket/900 => won't fix
  90. // The most relevant solution would be to implement a plugin to CKEdit, and handle the internal events like: setData, insertHtml, insertElement, loadSnapshot, key, afterUndo, afterRedo
  91. // Could also be bound to 'instanceReady.ckeditor'
  92. $oPage->add_ready_script("$('#$iId').bind('validate', function(evt, sFormId) { return ValidateCKEditField('$iId', '', {$this->m_sMandatory}, sFormId, '') } );\n");
  93. $oPage->add_ready_script("$('#$iId').bind('update', function() { BlockField('cke_$iId', $('#$iId').attr('disabled')); $(this).data('ckeditorInstance').setReadOnly($(this).prop('disabled')); } );\n");
  94. return $sHtmlValue;
  95. }
  96. }
  97. ?>