ui.htmleditorwidget.class.inc.php 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. // Copyright (C) 2010-2012 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. * @copyright Copyright (C) 2010-2012 Combodo SARL
  24. * @license http://opensource.org/licenses/AGPL-3.0
  25. */
  26. class UIHTMLEditorWidget
  27. {
  28. protected $m_iId;
  29. protected $m_sAttCode;
  30. protected $m_sNameSuffix;
  31. protected $m_sFieldPrefix;
  32. protected $m_sHelpText;
  33. protected $m_sValidationField;
  34. protected $m_sValue;
  35. protected $m_sMandatory;
  36. public function __construct($iInputId, $sAttCode, $sNameSuffix, $sFieldPrefix, $sHelpText, $sValidationField, $sValue, $sMandatory)
  37. {
  38. $this->m_iId = $iInputId;
  39. $this->m_sAttCode = $sAttCode;
  40. $this->m_sNameSuffix = $sNameSuffix;
  41. $this->m_sHelpText = $sHelpText;
  42. $this->m_sValidationField = $sValidationField;
  43. $this->m_sValue = $sValue;
  44. $this->m_sMandatory = $sMandatory;
  45. $this->m_sFieldPrefix = $sFieldPrefix;
  46. }
  47. /**
  48. * Get the HTML fragment corresponding to the HTML editor widget
  49. * @param WebPage $oP The web page used for all the output
  50. * @param Hash $aArgs Extra context arguments
  51. * @return string The HTML fragment to be inserted into the page
  52. */
  53. public function Display(WebPage $oPage, $aArgs = array())
  54. {
  55. $iId = $this->m_iId;
  56. $sCode = $this->m_sAttCode.$this->m_sNameSuffix;
  57. $sValue = $this->m_sValue;
  58. $sHelpText = $this->m_sHelpText;
  59. $sValidationField = $this->m_sValidationField;
  60. $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>";
  61. // Replace the text area with CKEditor
  62. // To change the default settings of the editor,
  63. // a) edit the file /js/ckeditor/config.js
  64. // b) or override some of the configuration settings, using the second parameter of ckeditor()
  65. $sLanguage = strtolower(trim(UserRights::GetUserLanguage()));
  66. $oPage->add_ready_script("$('#$iId').ckeditor(function() { /* callback code */ }, { language : '$sLanguage' , contentsLanguage : '$sLanguage', extraPlugins: 'disabler' });"); // Transform $iId into a CKEdit
  67. // Please read...
  68. // ValidateCKEditField triggers a timer... calling itself indefinitely
  69. // This design was the quickest way to achieve the field validation (only checking if the field is blank)
  70. // because the ckeditor does not fire events like "change" or "keyup", etc.
  71. // See http://dev.ckeditor.com/ticket/900 => won't fix
  72. // 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
  73. // Could also be bound to 'instanceReady.ckeditor'
  74. $oPage->add_ready_script("$('#$iId').bind('validate', function(evt, sFormId) { return ValidateCKEditField('$iId', '', {$this->m_sMandatory}, sFormId, '') } );\n");
  75. $oPage->add_ready_script("$('#$iId').bind('update', function() { BlockField('cke_$iId', $('#$iId').attr('disabled')); } );\n");
  76. return $sHtmlValue;
  77. }
  78. }
  79. ?>