WCAG_rendering.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4.0 |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license, |
  9. // | that is bundled with this package in the file LICENSE, and is |
  10. // | available at through the world-wide-web at |
  11. // | http://www.php.net/license/2_02.txt. |
  12. // | If you did not receive a copy of the PHP license and are unable to |
  13. // | obtain it through the world-wide-web, please send a note to |
  14. // | license@php.net so we can mail you a copy immediately. |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Adam Daniel <adaniel1@eesus.jnj.com> |
  17. // | Bertrand Mansion <bmansion@mamasam.com> |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // $Id: wai_rendering.php,v 1.0 2006/10/07 20:12:17 avb Exp $
  21. /**
  22. * @package chamilo.include
  23. */
  24. /**
  25. * Code
  26. */
  27. include_once ('../inc/global.inc.php');
  28. include_once (api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php');
  29. /**
  30. * helper for WCAG rendering.
  31. *
  32. * @author Patrick Vandermaesen <pvandermaesen@noctis.be>
  33. * @version 1.0
  34. * @package chamilo.include
  35. */
  36. class WCAG_Rendering {
  37. function editor_header() {
  38. return '<div id="WCAG-editor"><div class="title">'.get_lang('WCAGEditor').'</div><div class="body">';
  39. }
  40. function editor_footer() {
  41. return '</div></div>';
  42. }
  43. function prepareXHTML() {
  44. $text = $_POST['text'];
  45. $text = WCAG_Rendering::text_to_HTML ( $text );
  46. $imageFile = $_POST['imagefile'];
  47. $imageLabel = $_POST['imageLabel'];
  48. $link = $_POST['link'];
  49. $linkLabel = $_POST['linkLabel'];
  50. if (strlen($linkLabel) == 0) {
  51. $linkLabel = $link;
  52. }
  53. $home_top='<div id="WCAG-home"><img src="'.$imageFile.'" alt="'.$imageLabel.'" />'.'<p>'.$text.'</p>';
  54. if (strlen($link) > 0) {
  55. $home_top = $home_top.'<a href="'.$link.'">'.$linkLabel.'</a>';
  56. }
  57. $home_top=$home_top."<div style=\"clear:both;\"><span></span></div></div>";
  58. return $home_top;
  59. }
  60. /**
  61. * this method validate the content of current request (from WCAG editor).
  62. * this function return the error msg.
  63. */
  64. function request_validation() {
  65. $imageFile = $_POST['imagefile'];
  66. $imageLabel = $_POST['imageLabel'];
  67. if ((strlen($imageFile) > 0) and (strlen($imageLabel) == 0)) {
  68. return get_lang('ErrorNoLabel');
  69. }
  70. return '';
  71. }
  72. /**
  73. * Converter Plaintext to (x)HTML
  74. */
  75. function text_to_HTML ($Text)
  76. {
  77. $t = $Text;
  78. $t = stripslashes($t);
  79. $t = str_replace(">", "&gt;", $t);
  80. $t = str_replace("<", "&lt;", $t);
  81. $t = preg_replace("/(\015\012)|(\015)|(\012)/", "<br />\n", $t);
  82. $t = str_replace(" ", " &nbsp;", $t);
  83. return $t;
  84. }
  85. function HTML_to_text ($xhtml) {
  86. // convert HTML to text.
  87. $text = str_replace("<br />", "", $xhtml);
  88. $text = str_replace("<br/>", "", $text);
  89. $text = str_replace("&nbsp;", " ", $text);
  90. return $text;
  91. }
  92. function extract_data ($xhtml) {
  93. $text = $xhtml;
  94. if (stripos($xhtml, '<p>')) {
  95. $startP = stripos ($xhtml, "<p>");
  96. $endP = stripos ($xhtml, "</p>");
  97. $text = substr ($xhtml, $startP+3, $endP-$startP-3 );
  98. }
  99. // convert HTML to text.
  100. $text = WCAG_Rendering::HTML_to_text($text);
  101. $url='';
  102. if (stripos($xhtml, '<img')) {
  103. $startImgURL = stripos ($xhtml, "src=\"");
  104. $endImgURL = stripos ($xhtml, "\" ");
  105. $url = substr ($xhtml, $startImgURL+5, $endImgURL-$startImgURL-5 );
  106. $subxhtml = substr ($xhtml, $endImgURL+2, $startP);
  107. $startImgLabel = stripos ($subxhtml, "alt=\"");
  108. $endImgLabel = stripos ($subxhtml, "\" ");
  109. $label = substr ($subxhtml, $startImgLabel+5, $endImgLabel-$startImgLabel-5 );
  110. }
  111. $subxhtml = substr ($xhtml, $endImgURL+2, $startP);
  112. $startImgLabel = stripos ($subxhtml, "alt=\"");
  113. $endImgLabel = stripos ($subxhtml, "\" ");
  114. $label = substr ($subxhtml, $startImgLabel+5, $endImgLabel-$startImgLabel-5 );
  115. $subxhtml = substr ($xhtml, $endP+2, 9999999999);
  116. $link="";
  117. $linkLabel="";
  118. if (stripos($subxhtml, '<a href')) {
  119. $startLinkURL = stripos ($subxhtml, "ref=\"");
  120. $endLinkURL = stripos ($subxhtml, "\">");
  121. $link = substr ($subxhtml, $startLinkURL+5, $endLinkURL-$startLinkURL-5 );
  122. $endLinkLabel = stripos ($subxhtml, "</a>");
  123. $linkLabel = substr ( $subxhtml, $endLinkURL+2, $endLinkLabel-$endLinkURL-2 );
  124. }
  125. $values = array("text"=>$text,
  126. "imagefile"=>$url,
  127. "imageLabel"=>$label,
  128. "link"=>$link,
  129. "linkLabel"=>$linkLabel);
  130. return $values;
  131. }
  132. /**
  133. * add a form for set WCAG content (replace FCK)
  134. * @version 1.1
  135. */
  136. function &prepare_admin_form( $xhtml, &$form )
  137. {
  138. $values = WCAG_Rendering::extract_data($xhtml);
  139. if ($form == null) {
  140. $form = new FormValidator('waiForm');
  141. }
  142. $form->addElement('textarea','text',get_lang('WCAGContent'));
  143. $file =& $form->addElement('text','imagefile',get_lang('WCAGImage'));
  144. $form->addElement('text','imageLabel',get_lang('WCAGLabel'));
  145. $form->addElement('text','link',get_lang('WCAGLink'));
  146. $form->addElement('text','linkLabel',get_lang('WCAGLinkLabel'));
  147. $form->setDefaults($values);
  148. $renderer =& $form->defaultRenderer();
  149. $element_template = '<!-- BEGIN required --><span class="form_required">*</span> <!-- END required -->{label}<br />
  150. <!-- BEGIN error --><span class="form_error">{error}</span><br /><!-- END error --> {element}<br />';
  151. $renderer->setElementTemplate($element_template);
  152. return $form;
  153. }
  154. function &create_xhtml($xhtml) {
  155. $values = WCAG_Rendering::extract_data($xhtml);
  156. $xhtml = WCAG_Rendering::editor_header();
  157. $xhtml .= get_lang('WCAGContent').'<br />';
  158. $xhtml .= '<textarea name="text">'.$values['text'].'</textarea>';
  159. $xhtml .= get_lang('WCAGImage').'<br />';
  160. $xhtml .= '<input type="text" name="imagefile" value="'.$values['imagefile'].'"/>';
  161. $xhtml .= '<br />';
  162. $xhtml .= '<a href="#" onclick="OpenFileBrowser (\''.api_get_path(WEB_PATH) . 'main/inc/lib/fckeditor/'.'editor/plugins/ImageManagerStandalone/genericManager.php?uploadPath=/\');">'.get_lang('SelectPicture').'</a>';
  163. $xhtml .= '<br />';
  164. $xhtml .= get_lang('WCAGLabel').'<br />';
  165. $xhtml .= '<input type="text" name="imageLabel" value="'.$values['imageLabel'].'"/>';
  166. $xhtml .= get_lang('WCAGLink').'<br />';
  167. $xhtml .= '<input type="text" name="link" value="'.$values['link'].'"/>';
  168. $xhtml .= get_lang('WCAGLinkLabel').'<br />';
  169. $xhtml .= '<input type="text" name="linkLabel" value="'.$values['linkLabel'].'"/>';
  170. $xhtml .= WCAG_Rendering::editor_footer();;
  171. return $xhtml;
  172. }
  173. } // end class WAI_Rendering