WCAG_rendering.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. include_once ('../inc/global.inc.php');
  22. include_once (api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php');
  23. /**
  24. * helper for WCAG rendering.
  25. *
  26. * @author Patrick Vandermaesen <pvandermaesen@noctis.be>
  27. * @version 1.0
  28. */
  29. class WCAG_Rendering {
  30. function editor_header() {
  31. return '<div id="WCAG-editor"><div class="title">'.get_lang('WCAGEditor').'</div><div class="body">';
  32. }
  33. function editor_footer() {
  34. return '</div></div>';
  35. }
  36. function prepareXHTML() {
  37. $text = $_POST['text'];
  38. $text = WCAG_Rendering::text_to_HTML ( $text );
  39. $imageFile = $_POST['imagefile'];
  40. $imageLabel = $_POST['imageLabel'];
  41. $link = $_POST['link'];
  42. $linkLabel = $_POST['linkLabel'];
  43. if (strlen($linkLabel) == 0) {
  44. $linkLabel = $link;
  45. }
  46. $home_top='<div id="WCAG-home"><img src="'.$imageFile.'" alt="'.$imageLabel.'" />'.'<p>'.$text.'</p>';
  47. if (strlen($link) > 0) {
  48. $home_top = $home_top.'<a href="'.$link.'">'.$linkLabel.'</a>';
  49. }
  50. $home_top=$home_top."<div style=\"clear:both;\"><span></span></div></div>";
  51. return $home_top;
  52. }
  53. /**
  54. * this method validate the content of current request (from WCAG editor).
  55. * this function return the error msg.
  56. */
  57. function request_validation() {
  58. $imageFile = $_POST['imagefile'];
  59. $imageLabel = $_POST['imageLabel'];
  60. if ((strlen($imageFile) > 0) and (strlen($imageLabel) == 0)) {
  61. return get_lang('errorNoLabel');
  62. }
  63. return '';
  64. }
  65. /**
  66. * Converter Plaintext to (x)HTML
  67. */
  68. function text_to_HTML ($Text)
  69. {
  70. $t = $Text;
  71. $t = stripslashes($t);
  72. $t = str_replace(">", "&gt;", $t);
  73. $t = str_replace("<", "&lt;", $t);
  74. $t = preg_replace("/(\015\012)|(\015)|(\012)/", "<br />\n", $t);
  75. $t = str_replace(" ", " &nbsp;", $t);
  76. return $t;
  77. }
  78. function HTML_to_text ($xhtml) {
  79. // convert HTML to text.
  80. $text = str_replace("<br />", "", $xhtml);
  81. $text = str_replace("<br/>", "", $text);
  82. $text = str_replace("&nbsp;", " ", $text);
  83. return $text;
  84. }
  85. function extract_data ($xhtml) {
  86. $text = $xhtml;
  87. if (stripos($xhtml, '<p>')) {
  88. $startP = stripos ($xhtml, "<p>");
  89. $endP = stripos ($xhtml, "</p>");
  90. $text = substr ($xhtml, $startP+3, $endP-$startP-3 );
  91. }
  92. // convert HTML to text.
  93. $text = WCAG_Rendering::HTML_to_text($text);
  94. $url='';
  95. if (stripos($xhtml, '<img')) {
  96. $startImgURL = stripos ($xhtml, "src=\"");
  97. $endImgURL = stripos ($xhtml, "\" ");
  98. $url = substr ($xhtml, $startImgURL+5, $endImgURL-$startImgURL-5 );
  99. $subxhtml = substr ($xhtml, $endImgURL+2, $startP);
  100. $startImgLabel = stripos ($subxhtml, "alt=\"");
  101. $endImgLabel = stripos ($subxhtml, "\" ");
  102. $label = substr ($subxhtml, $startImgLabel+5, $endImgLabel-$startImgLabel-5 );
  103. }
  104. $subxhtml = substr ($xhtml, $endImgURL+2, $startP);
  105. $startImgLabel = stripos ($subxhtml, "alt=\"");
  106. $endImgLabel = stripos ($subxhtml, "\" ");
  107. $label = substr ($subxhtml, $startImgLabel+5, $endImgLabel-$startImgLabel-5 );
  108. $subxhtml = substr ($xhtml, $endP+2, 9999999999);
  109. $link="";
  110. $linkLabel="";
  111. if (stripos($subxhtml, '<a href')) {
  112. $startLinkURL = stripos ($subxhtml, "ref=\"");
  113. $endLinkURL = stripos ($subxhtml, "\">");
  114. $link = substr ($subxhtml, $startLinkURL+5, $endLinkURL-$startLinkURL-5 );
  115. $endLinkLabel = stripos ($subxhtml, "</a>");
  116. $linkLabel = substr ( $subxhtml, $endLinkURL+2, $endLinkLabel-$endLinkURL-2 );
  117. }
  118. $values = array("text"=>$text,
  119. "imagefile"=>$url,
  120. "imageLabel"=>$label,
  121. "link"=>$link,
  122. "linkLabel"=>$linkLabel);
  123. return $values;
  124. }
  125. /**
  126. * add a form for set WCAG content (replace FCK)
  127. * @version 1.1
  128. */
  129. function &prepare_admin_form( $xhtml, &$form )
  130. {
  131. $values = WCAG_Rendering::extract_data($xhtml);
  132. if ($form == null) {
  133. $form = new FormValidator('waiForm');
  134. }
  135. $form->addElement('textarea','text',get_lang('WCAGContent'));
  136. $file =& $form->addElement('text','imagefile',get_lang('WCAGImage'));
  137. $form->addElement('text','imageLabel',get_lang('WCAGLabel'));
  138. $form->addElement('text','link',get_lang('WCAGLink'));
  139. $form->addElement('text','linkLabel',get_lang('WCAGLinkLabel'));
  140. $form->setDefaults($values);
  141. $renderer =& $form->defaultRenderer();
  142. $element_template = '<!-- BEGIN required --><span class="form_required">*</span> <!-- END required -->{label}<br />
  143. <!-- BEGIN error --><span class="form_error">{error}</span><br /><!-- END error --> {element}<br />';
  144. $renderer->setElementTemplate($element_template);
  145. return $form;
  146. }
  147. function &create_xhtml($xhtml) {
  148. $values = WCAG_Rendering::extract_data($xhtml);
  149. $xhtml = WCAG_Rendering::editor_header();
  150. $xhtml .= get_lang('WCAGContent').'<br />';
  151. $xhtml .= '<textarea name="text">'.$values['text'].'</textarea>';
  152. $xhtml .= get_lang('WCAGImage').'<br />';
  153. $xhtml .= '<input type="text" name="imagefile" value="'.$values['imagefile'].'"/>';
  154. $xhtml .= '<br />';
  155. $xhtml .= '<a href="#" onclick="OpenFileBrowser (\''.api_get_path(WEB_PATH) . 'main/inc/lib/fckeditor/'.'editor/plugins/ImageManagerStandalone/genericManager.php?uploadPath=/\');">'.get_lang('SelectPicture').'</a>';
  156. $xhtml .= '<br />';
  157. $xhtml .= get_lang('WCAGLabel').'<br />';
  158. $xhtml .= '<input type="text" name="imageLabel" value="'.$values['imageLabel'].'"/>';
  159. $xhtml .= get_lang('WCAGLink').'<br />';
  160. $xhtml .= '<input type="text" name="link" value="'.$values['link'].'"/>';
  161. $xhtml .= get_lang('WCAGLinkLabel').'<br />';
  162. $xhtml .= '<input type="text" name="linkLabel" value="'.$values['linkLabel'].'"/>';
  163. $xhtml .= WCAG_Rendering::editor_footer();;
  164. return $xhtml;
  165. }
  166. } // end class WAI_Rendering
  167. ?>