PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/fields/class.Field.php

https://github.com/reshadf/Library
PHP | 535 lines | 264 code | 36 blank | 235 comment | 48 complexity | 1a40257cc5f39ff41e217cfc5b5c80fa MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * class Field
  4. *
  5. * Class to create a field.
  6. * This class contains code which is used by all the other fields
  7. *
  8. * @author Teye Heimans
  9. * @package FormHandler
  10. * @subpackage Fields
  11. */
  12. class Field
  13. {
  14. var $_oForm; // object: the form where the field is located in
  15. var $_sName; // string: name of the field
  16. var $_sValidator; // string: callback function to validate the value of the field
  17. var $_mValue; // mixed: the value of the field
  18. var $_sError; // string: if the field is not valid, this var contains the error message
  19. var $_sExtra; // string: extra data which should be added into the HTML tag (like CSS or JS)
  20. var $_iTabIndex; // int: tabindex or null when no tabindex is set
  21. var $_sExtraAfter; // string: extra data which should be added AFTER the HTML tag
  22. var $_viewMode; // boolean: should we only display the value instead of the field ?
  23. /**
  24. * Field::Field()
  25. *
  26. * Public abstract constructor: Create a new field
  27. *
  28. * @param object $oForm: The form where the field is located on
  29. * @param string $sName: The name of the field
  30. * @return Field
  31. * @access public
  32. * @author Teye Heimans
  33. */
  34. function Field( &$oForm, $sName )
  35. {
  36. // save the form and nome of the field
  37. $this->_oForm = &$oForm;
  38. $this->_sName = $sName;
  39. // check if there are spaces in the fieldname
  40. if(strpos($sName,' ') !== false)
  41. {
  42. trigger_error('Warning: There are spaces in the field name "'.$sName.'"!', E_USER_WARNING );
  43. }
  44. // get the value of the field
  45. if( $oForm->isPosted() )
  46. {
  47. // make sure that the $_POST array is global
  48. if(!_global) global $_POST;
  49. // get the value if it exists in the $_POST array
  50. if( isset( $_POST[$sName] ) )
  51. {
  52. // is the posted value a string
  53. if( is_string( $_POST[$sName] ) )
  54. {
  55. // save the value...
  56. $this->setValue(
  57. get_magic_quotes_gpc() ? stripslashes($_POST[$sName]) : $_POST[$sName]
  58. );
  59. }
  60. // the posted value is an array
  61. else if( is_array( $_POST[$sName] ) )
  62. {
  63. // escape the incoming data if needed and pass it to the field
  64. $item = array();
  65. foreach ( $_POST[$sName] as $key => $value )
  66. {
  67. $item[$key] = get_magic_quotes_gpc() ? stripslashes($value) : $value;
  68. }
  69. $this->setValue($item);
  70. }
  71. }
  72. /*
  73. * When the form is posted but this field is not found in the $_POST array,
  74. * keep the data from the db
  75. * (This happens when the DISABLED attribute in the field's tag is used)
  76. * Problem is that datefield's are never in the post array (because
  77. * they have 3 fields: {name}_day, etc.). Because of this, the old value always
  78. * will be kept...
  79. *
  80. * see (dutch topics!):
  81. * http://www.formhandler.net/FH3/index.php?pg=12&id=1333#1333
  82. * http://www.formhandler.net/FH3/index.php?pg=12&id=1296#1296
  83. *
  84. * TODO!!
  85. */
  86. /*
  87. elseif ( $oForm->edit )
  88. {
  89. if( isset( $oForm->_dbData[$sName] ) )
  90. {
  91. $this->setValue( $oForm->_dbData[$sName] );
  92. }
  93. }*/
  94. }
  95. // The form is not posted, load database value if exists
  96. else if( isset( $oForm->edit) && $oForm -> edit )
  97. {
  98. // does a db value exists for this field ?
  99. if( isset( $oForm->_dbData[$sName] ) )
  100. {
  101. // load the value into the field
  102. $this->setValue( $oForm->_dbData[$sName] );
  103. }
  104. }
  105. // check if the user got another value for this field.
  106. if( isset($oForm ->_buffer[ $sName ] ) )
  107. {
  108. list( $bOverwrite, $sValue ) = $oForm->_buffer[ $sName ];
  109. // if the field does not exists in the database
  110. if($bOverwrite || (!isset($oForm->_dbData[$sName]) && !$oForm->isPosted() ))
  111. {
  112. $this->setValue( $sValue );
  113. }
  114. // remove the value from the buffer..
  115. unset( $oForm->_buffer[ $sName ] );
  116. }
  117. }
  118. /**
  119. * Field::isValid()
  120. *
  121. * Check if the value of the field is valid. If not,
  122. * set the error message and return false
  123. *
  124. * @return boolean: If the value of the field is valid
  125. * @author Teye Heimans
  126. * @access public
  127. * @since 11-04-2008 ADDED POSSIBILITY TO USE MULTIPLE VALIDATORS
  128. * @author Remco van Arkelen & Johan Wiegel
  129. */
  130. function isValid()
  131. {
  132. // done this function before... return the prefious value
  133. if( isset( $this->_isValid ) )
  134. {
  135. return $this->_isValid;
  136. }
  137. // field in view mode?
  138. if( $this -> getViewMode() )
  139. {
  140. return $this->_isValid = true;
  141. }
  142. // is a validator set?
  143. if(isset($this->_sValidator) && $this->_sValidator != null)
  144. {
  145. // if it's an array, it's a method
  146. if (!is_array($this->_sValidator))
  147. {
  148. // Is there an | , there are more validators
  149. if( strpos( $this->_sValidator, '|' ) > 0 )
  150. {
  151. $aValidator = explode( '|', $this->_sValidator );
  152. foreach( $aValidator AS $val )
  153. {
  154. // is the validator a user-specified function?
  155. if( function_exists($this->_sValidator) )
  156. {
  157. $value = $this->getValue();
  158. $v = is_string($value) ? trim( $value) : $value;
  159. $error = call_user_func( $this->_sValidator, $v, $this->_oForm );
  160. }
  161. else
  162. {
  163. $v = new Validator();
  164. // is this a defined function? translate it to the correct function
  165. if( defined( $val ) )
  166. {
  167. $aVal = get_defined_constants();
  168. $val = $aVal[ $val ];
  169. }
  170. if( is_object( $v ) && method_exists($v, $val ) )
  171. {
  172. // call the build in validator function
  173. $value = $this->getValue();
  174. if( is_string( $value) )
  175. $value = trim( $value );
  176. $error = $v->{$val}( $value );
  177. }
  178. else
  179. {
  180. trigger_error('Unknown validator: "'.$val.'" used in field "'.$this->_sName.'"');
  181. $error = false;
  182. }
  183. unset( $v );
  184. }
  185. // Stop processing validators if 1 fails.
  186. if( true !== $error )
  187. {
  188. break;
  189. }
  190. }
  191. }
  192. else
  193. {
  194. // is the validator a user-spicified function?
  195. if( function_exists($this->_sValidator) )
  196. {
  197. $value = $this->getValue();
  198. $v = is_string($value) ? trim( $value) : $value;
  199. $error = call_user_func( $this->_sValidator, $v, $this->_oForm );
  200. }
  201. else
  202. {
  203. $v = new Validator();
  204. if( is_object( $v ) && method_exists($v, $this->_sValidator) )
  205. {
  206. // call the build in validator function
  207. $value = $this->getValue();
  208. if( is_string( $value) )
  209. $value = trim( $value );
  210. $error = $v->{$this->_sValidator}( $value );
  211. }
  212. else
  213. {
  214. trigger_error('Unknown validator: "'.$this->_sValidator.'" used in field "'.$this->_sName.'"');
  215. $error = false;
  216. }
  217. unset( $v );
  218. }
  219. }
  220. }
  221. // method given
  222. else
  223. {
  224. if( method_exists( $this->_sValidator[0], $this->_sValidator[1] ) )
  225. {
  226. $value = $this->getValue();
  227. $value = (is_array ($value)) ? $value : trim ($value);
  228. $error = call_user_func(array(&$this->_sValidator[0], $this->_sValidator[1]), $value );
  229. }
  230. else
  231. {
  232. trigger_error(
  233. "Error, the validator method '".$this->_sValidator[1]."' does not exists ".
  234. "in object '".get_class($this->_sValidator[0])."'!",
  235. E_USER_ERROR
  236. );
  237. $error = false;
  238. }
  239. }
  240. // set the error message
  241. $this->_sError =
  242. is_string($error) ? $error :
  243. (!$error ? $this->_oForm->_text( 14 ) :
  244. (isset($this->_sError) ? $this->_sError : ''));
  245. }
  246. $this->_isValid = empty( $this->_sError );
  247. return $this->_isValid;
  248. }
  249. /**
  250. * Field::getValidator()
  251. *
  252. * Returns the validator fromm this field
  253. * Added in order to use ajax validation
  254. *
  255. * @return string
  256. * @access public
  257. * @author Johan Wiegel
  258. * @since 04-12-2008
  259. */
  260. function getValidator( )
  261. {
  262. return $this->_sValidator;
  263. }
  264. /**
  265. * Field::setValidator()
  266. *
  267. * Set the validator which is used to validate the value of the field
  268. * This can also be an array.
  269. * If you want to use a method to validate the value use it like this:
  270. * array(&$obj, 'NameOfTheMethod')
  271. *
  272. * @param string $sValidator: the name of the validator
  273. * @return void
  274. * @access public
  275. * @author Teye Heimans
  276. */
  277. function setValidator( $sValidator )
  278. {
  279. $this->_sValidator = $sValidator;
  280. /*
  281. if( $this->_oForm->_ajaxValidator === true )
  282. {
  283. echo 'JAJA';
  284. require_once( FH_INCLUDE_DIR . 'includes/class.AjaxValidator.php' );
  285. $oAjaxValidator = new AjaxValidator( $this );
  286. $oAjaxValidator->AjaxValidator( $this );
  287. }
  288. */
  289. }
  290. /**
  291. * Field::setTabIndex()
  292. *
  293. * Set the tabindex of the field
  294. *
  295. * @param int $iIndex
  296. * @return void
  297. * @author Teye Heimans
  298. * @access public
  299. */
  300. function setTabIndex( $iIndex )
  301. {
  302. $this->_iTabIndex = $iIndex;
  303. }
  304. /**
  305. * Field::setExtraAfter()
  306. *
  307. * Set some extra HTML, JS or something like that (to use after the html tag)
  308. *
  309. * @param strint $sExtra: the extra html to insert into the tag
  310. * @return void
  311. * @author Teye Heimans
  312. * @access public
  313. */
  314. function setExtraAfter( $sExtraAfter )
  315. {
  316. $this->_sExtraAfter = $sExtraAfter;
  317. }
  318. /**
  319. * Field::setError()
  320. *
  321. * Set a custom error
  322. *
  323. * @param string $sError: the error to set into the tag
  324. * @return void
  325. * @access public
  326. * @author Filippo Toso - filippotoso@libero.it
  327. */
  328. function setError( $sError )
  329. {
  330. $this->_sError = $sError;
  331. }
  332. /**
  333. * Field::getValue()
  334. *
  335. * Return the value of the field
  336. *
  337. * @return mixed: the value of the field
  338. * @access public
  339. * @author Teye Heimans
  340. */
  341. function getValue()
  342. {
  343. return isset( $this->_mValue ) ? $this->_mValue : '';
  344. }
  345. /**
  346. * Field::getError()
  347. *
  348. * Return the error of the field (if the field-value is not valid)
  349. *
  350. * @return string: the error message
  351. * @access public
  352. * @author Teye Heimans
  353. */
  354. function getError()
  355. {
  356. return isset( $this->_sError ) && strlen($this->_sError) > 0 ? sprintf( FH_ERROR_MASK, $this->_sName ,$this->_sError): '';
  357. }
  358. /**
  359. * Field::setValue()
  360. *
  361. * Set the value of the field
  362. *
  363. * @param mixed $mValue: The new value for the field
  364. * @return void
  365. * @access public
  366. * @author Teye Heimans
  367. */
  368. function setValue( $mValue )
  369. {
  370. $this->_mValue = $mValue;
  371. }
  372. /**
  373. * Field::setExtra()
  374. *
  375. * Set some extra CSS, JS or something like that (to use in the html tag)
  376. *
  377. * @param strint $sExtra: the extra html to insert into the tag
  378. * @return void
  379. * @access public
  380. * @author Teye Heimans
  381. */
  382. function setExtra( $sExtra )
  383. {
  384. $this->_sExtra = $sExtra;
  385. }
  386. /**
  387. * Field::getField()
  388. *
  389. * Return the HTML of the field.
  390. * This function HAS TO BE OVERWRITTEN by the child class!
  391. *
  392. * @return string: the html of the field
  393. * @access public
  394. * @author Teye Heimans
  395. */
  396. function getField()
  397. {
  398. trigger_error('Error, getField has not been overwritten!', E_USER_WARNING);
  399. return '';
  400. }
  401. /**
  402. * Field::getViewMode()
  403. *
  404. * Return if this field is set to view mode
  405. *
  406. * @return bool
  407. * @access public
  408. * @author Teye Heimans
  409. */
  410. function getViewMode()
  411. {
  412. return (isset( $this -> _viewMode) && $this -> _viewMode) ||
  413. (isset( $this -> _oForm -> _viewMode ) && $this -> _oForm -> _viewMode);
  414. }
  415. /**
  416. * Field::setViewMode()
  417. *
  418. * Enable or disable viewMode for this field
  419. *
  420. * @param boolean $mode
  421. * @return void
  422. * @access public
  423. * @author Teye Heimans
  424. */
  425. function setViewMode( $mode = true )
  426. {
  427. $this -> _viewMode = (bool) $mode;
  428. }
  429. /**
  430. * Field::_getViewValue()
  431. *
  432. * Return the value of the field
  433. *
  434. * @return mixed: the value of the field
  435. * @access private
  436. * @author Teye Heimans
  437. */
  438. function _getViewValue()
  439. {
  440. // edit form and posted ? then first get the database value!
  441. if( isset( $this -> _oForm -> edit ) && $this -> _oForm -> edit && $this -> _oForm -> isPosted() )
  442. {
  443. $this -> setValue( $this -> _oForm -> _dbData[ $this -> _sName ] );
  444. }
  445. // get the value for the field
  446. $val = $this->getValue();
  447. // implode arrays
  448. $save = is_array( $val) ? implode( ', ', $val) : $val;
  449. // are there mulitple options ?
  450. if( isset( $this->_aOptions ) )
  451. {
  452. // is the key returned while we should show the "label" to the user ?
  453. if( isset($this->_bUseArrayKeyAsValue) && $this->_bUseArrayKeyAsValue )
  454. {
  455. // is the value an array?
  456. if( is_array( $val) )
  457. {
  458. // save the labels instead of the index keys as view value
  459. foreach ( $val as $key => $value )
  460. {
  461. $val[$key] = $this->_aOptions[$value];
  462. }
  463. }
  464. // is there a "label" for this value ?
  465. else if( array_key_exists( $val, $this->_aOptions ) )
  466. {
  467. // get the "label" instead of the index
  468. $val = $this->_aOptions[$val];
  469. }
  470. }
  471. }
  472. // when the value is an array
  473. if( is_array($val) )
  474. {
  475. // is there only one item?
  476. if( sizeof($val) == 1 )
  477. {
  478. $result = $val[0];
  479. }
  480. else
  481. {
  482. // make a list of the selected items
  483. $result = "\t<ul>\n";
  484. foreach($val as $item )
  485. {
  486. $result .= "\t <li>".$item."</li>\n";
  487. }
  488. $result .= "\t</ul>\n";
  489. }
  490. }
  491. else
  492. {
  493. $result = $val;
  494. }
  495. // return the value
  496. return $result;
  497. }
  498. }
  499. ?>