PageRenderTime 38ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/inc/patForms/Parser/Html.php

https://github.com/chregu/fluxcms
PHP | 250 lines | 155 code | 27 blank | 68 comment | 18 complexity | 41d4397901a4a6c8038ab710d2d1659a MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, Apache-2.0, LGPL-2.1
  1. <?PHP
  2. /**
  3. * patForms parser that reads plain HTML files
  4. * and creates a from from these
  5. *
  6. * $Id$
  7. *
  8. * @author Stephan Schmidt <s.schmidt@metrix.de>
  9. * @package patForms
  10. * @subpackage Parser
  11. * @license LGPL
  12. * @copyright PHP Application Tools <http://www.php-tools.net>
  13. */
  14. /**
  15. * patForms parser that reads plain HTML files
  16. * and creates a from from these
  17. *
  18. * @author Stephan Schmidt <s.schmidt@metrix.de>
  19. * @package patForms
  20. * @subpackage Parser
  21. * @license LGPL
  22. * @copyright PHP Application Tools <http://www.php-tools.net>
  23. * @version 1.0
  24. */
  25. class patForms_Parser_Html extends patForms_Parser
  26. {
  27. /**
  28. * extract all form elements from the HTML page
  29. *
  30. * @access public
  31. * @param string html content
  32. * @return boolean
  33. * @todo extract select fields
  34. * @todo extract form information
  35. */
  36. function parseString( $string )
  37. {
  38. // set default attributes fo the form
  39. $this->_formAttributes = array(
  40. '__default' => array(
  41. 'name' => 'form'
  42. )
  43. );
  44. /**
  45. * transform simple input fields
  46. */
  47. $regexp = "/<input ([^>]+)\/?>/";
  48. $matches = array();
  49. if( preg_match_all( $regexp, $string, $matches ) )
  50. {
  51. for( $i = 0; $i < count( $matches[1] ); $i++ )
  52. {
  53. $atts = trim( $matches[1][$i] );
  54. if( substr( $atts, -1 ) == '/' )
  55. $atts = substr( $atts, 0, -1 );
  56. $atts = $this->_parseAttributes( $atts );
  57. array_change_key_case( $atts, CASE_LOWER );
  58. switch( strtolower( $atts['type'] ) )
  59. {
  60. case 'password':
  61. $atts['type'] = 'password';
  62. break;
  63. case 'radio':
  64. unset( $atts['type'] );
  65. $elName = 'Radio';
  66. break;
  67. case 'checkbox':
  68. unset( $atts['type'] );
  69. $elName = 'Switch';
  70. break;
  71. case 'text':
  72. unset( $atts['type'] );
  73. $elName = 'String';
  74. break;
  75. case 'hidden':
  76. unset( $atts['type'] );
  77. $elName = 'Hidden';
  78. break;
  79. case 'file':
  80. unset( $atts['type'] );
  81. $elName = 'File';
  82. break;
  83. default:
  84. continue 2;
  85. break;
  86. }
  87. if( isset( $atts['patforms:type'] ) )
  88. {
  89. $elName = $atts['patforms:type'];
  90. unset( $atts['patforms:type'] );
  91. }
  92. $this->addElementDefinition( $atts['name'], $elName, $atts );
  93. $pl = $this->_getPlaceholderForElement( $atts['name'] );
  94. $string = str_replace( $matches[0][$i], $pl, $string );
  95. }
  96. }
  97. /**
  98. * transform textarea fields
  99. */
  100. $regexp = "/<textarea ([^>]+)\/?>([^<]*)<\/textarea>/sm";
  101. $matches = array();
  102. if( preg_match_all( $regexp, $string, $matches ) )
  103. {
  104. for( $i = 0; $i < count( $matches[1] ); $i++ )
  105. {
  106. $atts = trim( $matches[1][$i] );
  107. $atts = $this->_parseAttributes( $atts );
  108. array_change_key_case( $atts, CASE_LOWER );
  109. /**
  110. * input => string
  111. */
  112. $elName = 'Text';
  113. $atts['default'] = $matches[2][$i];
  114. $this->addElementDefinition( $atts['name'], $elName, $atts );
  115. $pl = $this->_getPlaceholderForElement( $atts['name'] );
  116. $string = str_replace( $matches[0][$i], $pl, $string );
  117. }
  118. }
  119. /**
  120. * transform select fields
  121. */
  122. $regexp = "/<select ([^>]+)\/?>(.*)<\/select>/smU";
  123. $matches = array();
  124. if( preg_match_all( $regexp, $string, $matches ) )
  125. {
  126. for( $i = 0; $i < count( $matches[1] ); $i++ )
  127. {
  128. $atts = trim( $matches[1][$i] );
  129. $atts = $this->_parseAttributes( $atts );
  130. array_change_key_case( $atts, CASE_LOWER );
  131. /**
  132. * input => string
  133. */
  134. $elName = 'Enum';
  135. if( isset( $atts['multiple'] ) )
  136. {
  137. $elName = 'Set';
  138. unset( $atts['multiple'] );
  139. }
  140. $matches2 = array();
  141. $regexp = "/<option ([^>]+)\/?>([^<]*)(<\/option>)?/sm";
  142. $options = array();
  143. if( preg_match_all( $regexp, $matches[2][$i], $matches2 ) )
  144. {
  145. for( $j = 0; $j < count( $matches2[0] ); $j++ )
  146. {
  147. $opt = $this->_parseAttributes( $matches2[1][$j] );
  148. $opt['label'] = trim( $matches2[2][$j] );
  149. array_push( $options, $opt );
  150. }
  151. }
  152. $atts['values'] = $options;
  153. $this->addElementDefinition( $atts['name'], $elName, $atts );
  154. $pl = $this->_getPlaceholderForElement( $atts['name'] );
  155. $string = str_replace( $matches[0][$i], $pl, $string );
  156. }
  157. }
  158. $this->_html = $string;
  159. return true;
  160. }
  161. /**
  162. * write a patForms template after extracting the elements from the
  163. * template.
  164. *
  165. * This allows you to later add additional information.
  166. *
  167. * @access public
  168. * @param string filename
  169. * @param string namespace
  170. * @return boolean
  171. */
  172. function writeFormTemplate( $file, $ns = 'patForms' )
  173. {
  174. $patForms = &$this->getForm();
  175. $cnt = count( $patForms->elements );
  176. for( $i=0; $i < $cnt; $i++ )
  177. {
  178. // first, serialize the element as this also initializes the attribute collection.
  179. $serialized = trim( $patForms->elements[$i]->toXML( $ns ) );
  180. if( $serialized === false )
  181. {
  182. patErrorManager::raiseWarning(
  183. PATFORMS_PARSER_ERROR_ELEMENT_NOT_SERIALIZEABLE,
  184. "Element '".get_class( $patForms->elements[$i] )."' could not return serialized data."
  185. );
  186. continue;
  187. }
  188. $serializedElements[($patForms->elements[$i]->getName())] = $serialized;
  189. }
  190. $html = $this->getHTML();
  191. foreach( $serializedElements as $name => $element )
  192. {
  193. $varName = sprintf( "{PATFORMS_ELEMENT_%s}", strtoupper( $name ) );
  194. $html = str_replace( $varName, $element, $html );
  195. }
  196. $this->_writeToFile( $this->_adjustFilename( $file ), $html );
  197. return true;
  198. }
  199. /**
  200. * parse an attribute string and build an array
  201. *
  202. * @access private
  203. * @param string attribute string
  204. * @param array attribute array
  205. */
  206. function _parseAttributes( $string )
  207. {
  208. $string = trim( $string );
  209. // Check for trailing slash, if tag was an empty XML Tag
  210. if( substr( $string, -1 ) == "/" )
  211. $string = trim( substr( $string, 0, strlen( $string )-1 ) );
  212. $pairs = explode( ' ', $string );
  213. for ( $i = 0; $i < count($pairs); $i++ )
  214. {
  215. $pair = explode( '=', trim( str_replace( '"', '', $pairs[$i] ) ) );
  216. if( count( $pair ) == 1 )
  217. $pair[1] = 'yes';
  218. $attributes[strtolower( $pair[0]) ] = $pair[1];
  219. }
  220. return $attributes;
  221. }
  222. }
  223. ?>