ui.htmleditorwidget.class.inc.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. // Copyright (C) 2010-2016 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 Romain Quetiez
  23. * @copyright Copyright (C) 2010-2016 Combodo SARL
  24. * @license http://opensource.org/licenses/AGPL-3.0
  25. */
  26. class UIHTMLEditorWidget
  27. {
  28. protected $m_iId;
  29. protected $m_oAttDef;
  30. protected $m_sAttCode;
  31. protected $m_sNameSuffix;
  32. protected $m_sFieldPrefix;
  33. protected $m_sHelpText;
  34. protected $m_sValidationField;
  35. protected $m_sValue;
  36. protected $m_sMandatory;
  37. public function __construct($iInputId, $oAttDef, $sNameSuffix, $sFieldPrefix, $sHelpText, $sValidationField, $sValue, $sMandatory)
  38. {
  39. $this->m_iId = $iInputId;
  40. $this->m_oAttDef = $oAttDef;
  41. $this->m_sAttCode = $oAttDef->GetCode();
  42. $this->m_sNameSuffix = $sNameSuffix;
  43. $this->m_sHelpText = $sHelpText;
  44. $this->m_sValidationField = $sValidationField;
  45. $this->m_sValue = $sValue;
  46. $this->m_sMandatory = $sMandatory;
  47. $this->m_sFieldPrefix = $sFieldPrefix;
  48. }
  49. /**
  50. * Get the HTML fragment corresponding to the HTML editor widget
  51. * @param WebPage $oP The web page used for all the output
  52. * @param Hash $aArgs Extra context arguments
  53. * @return string The HTML fragment to be inserted into the page
  54. */
  55. public function Display(WebPage $oPage, $aArgs = array())
  56. {
  57. $iId = $this->m_iId;
  58. $sCode = $this->m_sAttCode.$this->m_sNameSuffix;
  59. $sValue = $this->m_sValue;
  60. $sHelpText = $this->m_sHelpText;
  61. $sValidationField = $this->m_sValidationField;
  62. $sHtmlValue = "<div class=\"field_input_zone field_input_html\"><textarea class=\"htmlEditor\" title=\"$sHelpText\" name=\"attr_{$this->m_sFieldPrefix}{$sCode}\" rows=\"10\" cols=\"10\" id=\"$iId\">$sValue</textarea></div>$sValidationField";
  63. // Replace the text area with CKEditor
  64. // To change the default settings of the editor,
  65. // a) edit the file /js/ckeditor/config.js
  66. // b) or override some of the configuration settings, using the second parameter of ckeditor()
  67. $aConfig = array();
  68. $sLanguage = strtolower(trim(UserRights::GetUserLanguage()));
  69. $aConfig['language'] = $sLanguage;
  70. $aConfig['contentsLanguage'] = $sLanguage;
  71. $aConfig['extraPlugins'] = 'disabler';
  72. $sWidthSpec = addslashes(trim($this->m_oAttDef->GetWidth()));
  73. if ($sWidthSpec != '')
  74. {
  75. $aConfig['width'] = $sWidthSpec;
  76. }
  77. $sHeightSpec = addslashes(trim($this->m_oAttDef->GetHeight()));
  78. if ($sHeightSpec != '')
  79. {
  80. $aConfig['height'] = $sHeightSpec;
  81. }
  82. $sConfigJS = json_encode($aConfig);
  83. $oPage->add_ready_script("$('#$iId').ckeditor(function() { /* callback code */ }, $sConfigJS);"); // Transform $iId into a CKEdit
  84. // Please read...
  85. // ValidateCKEditField triggers a timer... calling itself indefinitely
  86. // This design was the quickest way to achieve the field validation (only checking if the field is blank)
  87. // because the ckeditor does not fire events like "change" or "keyup", etc.
  88. // See http://dev.ckeditor.com/ticket/900 => won't fix
  89. // 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
  90. // Could also be bound to 'instanceReady.ckeditor'
  91. $oPage->add_ready_script("$('#$iId').bind('validate', function(evt, sFormId) { return ValidateCKEditField('$iId', '', {$this->m_sMandatory}, sFormId, '') } );\n");
  92. $oPage->add_ready_script(
  93. <<<EOF
  94. $('#$iId').bind('update', function(evt){
  95. BlockField('cke_$iId', $('#$iId').attr('disabled'));
  96. //Delayed execution - ckeditor must be properly initialized before setting readonly
  97. var retryCount = 0;
  98. var oMe = $('#$iId');
  99. var delayedSetReadOnly = function () {
  100. if (oMe.data('ckeditorInstance').editable() == undefined && retryCount++ < 10) {
  101. setTimeout(delayedSetReadOnly, retryCount * 100); //Wait a while longer each iteration
  102. }
  103. else
  104. {
  105. oMe.data('ckeditorInstance').setReadOnly(oMe.prop('disabled'));
  106. }
  107. };
  108. setTimeout(delayedSetReadOnly, 50);
  109. });
  110. EOF
  111. );
  112. return $sHtmlValue;
  113. }
  114. }