QuickForm.php 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * Create, validate and process HTML forms
  5. *
  6. * PHP versions 4 and 5
  7. *
  8. * LICENSE: This source file is subject to version 3.01 of the PHP license
  9. * that is available through the world-wide-web at the following URI:
  10. * http://www.php.net/license/3_01.txt If you did not receive a copy of
  11. * the PHP License and are unable to obtain it through the web, please
  12. * send a note to license@php.net so we can mail you a copy immediately.
  13. *
  14. * @category HTML
  15. * @package HTML_QuickForm
  16. * @author Adam Daniel <adaniel1@eesus.jnj.com>
  17. * @author Bertrand Mansion <bmansion@mamasam.com>
  18. * @author Alexey Borzov <avb@php.net>
  19. * @copyright 2001-2009 The PHP Group
  20. * @license http://www.php.net/license/3_01.txt PHP License 3.01
  21. * @version CVS: $Id: QuickForm.php,v 1.166 2009/04/04 21:34:02 avb Exp $
  22. * @link http://pear.php.net/package/HTML_QuickForm
  23. */
  24. /**
  25. * Validation rules known to HTML_QuickForm
  26. * @see HTML_QuickForm::registerRule(), HTML_QuickForm::getRegisteredRules(),
  27. * HTML_QuickForm::isRuleRegistered()
  28. * @global array $GLOBALS['_HTML_QuickForm_registered_rules']
  29. */
  30. // {{{ error codes
  31. /**#@+
  32. * Error codes for HTML_QuickForm
  33. *
  34. * Codes are mapped to textual messages by errorMessage() method, if you add a
  35. * new code be sure to add a new message for it to errorMessage()
  36. *
  37. * @see HTML_QuickForm::errorMessage()
  38. */
  39. define('QUICKFORM_OK', 1);
  40. define('QUICKFORM_ERROR', -1);
  41. define('QUICKFORM_INVALID_RULE', -2);
  42. define('QUICKFORM_NONEXIST_ELEMENT', -3);
  43. define('QUICKFORM_INVALID_FILTER', -4);
  44. define('QUICKFORM_UNREGISTERED_ELEMENT', -5);
  45. define('QUICKFORM_INVALID_ELEMENT_NAME', -6);
  46. define('QUICKFORM_INVALID_PROCESS', -7);
  47. define('QUICKFORM_DEPRECATED', -8);
  48. define('QUICKFORM_INVALID_DATASOURCE', -9);
  49. /**#@-*/
  50. // }}}
  51. /**
  52. * Class HTML_QuickForm
  53. * Create, validate and process HTML forms
  54. *
  55. * @category HTML
  56. * @package HTML_QuickForm
  57. * @author Adam Daniel <adaniel1@eesus.jnj.com>
  58. * @author Bertrand Mansion <bmansion@mamasam.com>
  59. * @author Alexey Borzov <avb@php.net>
  60. * @version Release: 3.2.11
  61. */
  62. class HTML_QuickForm extends HTML_Common
  63. {
  64. const MAX_ELEMENT_ARGUMENT = 10;
  65. private $dateTimePickerLibraryAdded;
  66. /**
  67. * Array containing the form fields
  68. * @since 1.0
  69. * @var array
  70. * @access private
  71. */
  72. public $_elements = array();
  73. /**
  74. * Array containing element name to index map
  75. * @since 1.1
  76. * @var array
  77. * @access private
  78. */
  79. public $_elementIndex = array();
  80. /**
  81. * Array containing indexes of duplicate elements
  82. * @since 2.10
  83. * @var array
  84. * @access private
  85. */
  86. public $_duplicateIndex = array();
  87. /**
  88. * Array containing required field IDs
  89. * @since 1.0
  90. * @var array
  91. * @access private
  92. */
  93. public $_required = array();
  94. /**
  95. * Prefix message in javascript alert if error
  96. * @since 1.0
  97. * @var string
  98. * @access public
  99. */
  100. public $_jsPrefix = 'Invalid information entered.';
  101. /**
  102. * Postfix message in javascript alert if error
  103. * @since 1.0
  104. * @var string
  105. * @access public
  106. */
  107. public $_jsPostfix = 'Please correct these fields.';
  108. /**
  109. * Datasource object implementing the informal
  110. * datasource protocol
  111. * @since 3.3
  112. * @var object
  113. * @access private
  114. */
  115. public $_datasource;
  116. /**
  117. * Array of default form values
  118. * @since 2.0
  119. * @var array
  120. * @access private
  121. */
  122. public $_defaultValues = array();
  123. /**
  124. * Array of constant form values
  125. * @since 2.0
  126. * @var array
  127. * @access private
  128. */
  129. public $_constantValues = array();
  130. /**
  131. * Array of submitted form values
  132. * @since 1.0
  133. * @var array
  134. * @access private
  135. */
  136. public $_submitValues = array();
  137. /**
  138. * Array of submitted form files
  139. * @since 1.0
  140. * @var integer
  141. * @access public
  142. */
  143. public $_submitFiles = array();
  144. /**
  145. * Value for maxfilesize hidden element if form contains file input
  146. * @since 1.0
  147. * @var integer
  148. * @access public
  149. */
  150. public $_maxFileSize = 1048576; // 1 Mb = 1048576
  151. /**
  152. * Flag to know if all fields are frozen
  153. * @since 1.0
  154. * @var boolean
  155. * @access private
  156. */
  157. public $_freezeAll = false;
  158. /**
  159. * Array containing the form rules
  160. * @since 1.0
  161. * @var array
  162. * @access private
  163. */
  164. public $_rules = array();
  165. /**
  166. * Form rules, global variety
  167. * @var array
  168. * @access private
  169. */
  170. public $_formRules = array();
  171. /**
  172. * Array containing the validation errors
  173. * @since 1.0
  174. * @var array
  175. * @access private
  176. */
  177. public $_errors = array();
  178. /**
  179. * Note for required fields in the form
  180. * @var string
  181. * @since 1.0
  182. * @access private
  183. */
  184. public $_requiredNote = '<span style="font-size:80%; color:#ff0000;">*</span><span style="font-size:80%;"> denotes required field</span>';
  185. /**
  186. * Whether the form was submitted
  187. * @var boolean
  188. * @access private
  189. */
  190. public $_flagSubmitted = false;
  191. // }}}
  192. // {{{ constructor
  193. /**
  194. * Class constructor
  195. * @param string $formName Form's name.
  196. * @param string $method (optional)Form's method defaults to 'POST'
  197. * @param string $action (optional)Form's action
  198. * @param string $target (optional)Form's target defaults to '_self'
  199. * @param mixed $attributes (optional)Extra attributes for <form> tag
  200. * @param bool $trackSubmit (optional)Whether to track if the form was submitted by adding a special hidden field
  201. * @access public
  202. */
  203. public function __construct($formName='', $method='post', $action='', $target='', $attributes=null, $trackSubmit = false)
  204. {
  205. parent::__construct($attributes);
  206. $method = (strtoupper($method) == 'GET') ? 'get' : 'post';
  207. $action = ($action == '') ? api_get_self() : $action;
  208. $target = empty($target) ? array() : array('target' => $target);
  209. $form_id = $formName;
  210. if (isset($attributes['id']) && !empty($attributes['id'])) {
  211. $form_id = Security::remove_XSS($attributes['id']);
  212. }
  213. $attributes = array(
  214. 'action' => $action,
  215. 'method' => $method,
  216. 'name' => $formName,
  217. 'id' => $form_id
  218. ) + $target;
  219. $this->updateAttributes($attributes);
  220. if (!$trackSubmit || isset($_REQUEST['_qf__' . $formName])) {
  221. if (1 == get_magic_quotes_gpc()) {
  222. $this->_submitValues = $this->_recursiveFilter('stripslashes', 'get' == $method? $_GET: $_POST);
  223. foreach ($_FILES as $keyFirst => $valFirst) {
  224. foreach ($valFirst as $keySecond => $valSecond) {
  225. if ('name' == $keySecond) {
  226. $this->_submitFiles[$keyFirst][$keySecond] = $this->_recursiveFilter('stripslashes', $valSecond);
  227. } else {
  228. $this->_submitFiles[$keyFirst][$keySecond] = $valSecond;
  229. }
  230. }
  231. }
  232. } else {
  233. $this->_submitValues = 'get' == $method? $_GET: $_POST;
  234. $this->_submitFiles = $_FILES;
  235. }
  236. $this->_flagSubmitted = count($this->_submitValues) > 0 || count($this->_submitFiles) > 0;
  237. }
  238. if ($trackSubmit) {
  239. unset($this->_submitValues['_qf__' . $formName]);
  240. $this->addElement('hidden', '_qf__' . $formName, null);
  241. }
  242. if (preg_match('/^([0-9]+)([a-zA-Z]*)$/', ini_get('upload_max_filesize'), $matches)) {
  243. // see http://www.php.net/manual/en/faq.using.php#faq.using.shorthandbytes
  244. switch (strtoupper($matches['2'])) {
  245. case 'G':
  246. $this->_maxFileSize = $matches['1'] * 1073741824;
  247. break;
  248. case 'M':
  249. $this->_maxFileSize = $matches['1'] * 1048576;
  250. break;
  251. case 'K':
  252. $this->_maxFileSize = $matches['1'] * 1024;
  253. break;
  254. default:
  255. $this->_maxFileSize = $matches['1'];
  256. }
  257. }
  258. // $course_id = api_get_course_int_id();
  259. // //If I'm in a course replace the default max filesize with the course limits
  260. // if (!empty($course_id)) {
  261. // $free_course_quota = DocumentManager::get_course_quota() - DocumentManager::documents_total_space();
  262. // if (empty($this->_maxFileSize) || $free_course_quota <= $this->_maxFileSize) {
  263. // $this->_maxFileSize = intval($free_course_quota);
  264. // }
  265. // }
  266. } // end constructor
  267. // }}}
  268. // {{{ apiVersion()
  269. /**
  270. * Returns the current API version
  271. *
  272. * @since 1.0
  273. * @access public
  274. * @return float
  275. */
  276. function apiVersion()
  277. {
  278. return 3.2;
  279. } // end func apiVersion
  280. // }}}
  281. // {{{ registerElementType()
  282. /**
  283. * Registers a new element type
  284. *
  285. * @param string $typeName Name of element type
  286. * @param string $include Include path for element type
  287. * @param string $className Element class name
  288. * @since 1.0
  289. * @access public
  290. * @return void
  291. */
  292. function registerElementType($typeName, $include, $className)
  293. {
  294. $GLOBALS['HTML_QUICKFORM_ELEMENT_TYPES'][strtolower($typeName)] = array($include, $className);
  295. } // end func registerElementType
  296. // }}}
  297. // {{{ registerRule()
  298. /**
  299. * Registers a new validation rule
  300. *
  301. * @param string $ruleName Name of validation rule
  302. * @param string $type Either: 'regex', 'function' or 'rule' for an HTML_QuickForm_Rule object
  303. * @param string $data1 Name of function, regular expression or HTML_QuickForm_Rule classname
  304. * @param string $data2 Object parent of above function or HTML_QuickForm_Rule file path
  305. * @since 1.0
  306. * @access public
  307. * @return void
  308. */
  309. public static function registerRule($ruleName, $type, $data1, $data2 = null)
  310. {
  311. $registry =& HTML_QuickForm_RuleRegistry::singleton();
  312. $registry->registerRule($ruleName, $type, $data1, $data2);
  313. } // end func registerRule
  314. // }}}
  315. // {{{ elementExists()
  316. /**
  317. * Returns true if element is in the form
  318. *
  319. * @param string $element form name of element to check
  320. * @since 1.0
  321. * @access public
  322. * @return boolean
  323. */
  324. function elementExists($element=null)
  325. {
  326. return isset($this->_elementIndex[$element]);
  327. } // end func elementExists
  328. // }}}
  329. // {{{ setDatasource()
  330. /**
  331. * Sets a datasource object for this form object
  332. *
  333. * Datasource default and constant values will feed the QuickForm object if
  334. * the datasource implements defaultValues() and constantValues() methods.
  335. *
  336. * @param object $datasource datasource object implementing the informal datasource protocol
  337. * @param mixed $defaultsFilter string or array of filter(s) to apply to default values
  338. * @param mixed $constantsFilter string or array of filter(s) to apply to constants values
  339. * @since 3.3
  340. * @access public
  341. * @return void
  342. * @throws HTML_QuickForm_Error
  343. */
  344. function setDatasource(&$datasource, $defaultsFilter = null, $constantsFilter = null)
  345. {
  346. if (is_object($datasource)) {
  347. $this->_datasource =& $datasource;
  348. if (is_callable(array($datasource, 'defaultValues'))) {
  349. $this->setDefaults($datasource->defaultValues($this), $defaultsFilter);
  350. }
  351. if (is_callable(array($datasource, 'constantValues'))) {
  352. $this->setConstants($datasource->constantValues($this), $constantsFilter);
  353. }
  354. } else {
  355. return PEAR::raiseError(null, QUICKFORM_INVALID_DATASOURCE, null, E_USER_WARNING, "Datasource is not an object in QuickForm::setDatasource()", 'HTML_QuickForm_Error', true);
  356. }
  357. } // end func setDatasource
  358. // }}}
  359. // {{{ setDefaults()
  360. /**
  361. * Initializes default form values
  362. *
  363. * @param array $defaultValues values used to fill the form
  364. * @param mixed $filter (optional) filter(s) to apply to all default values
  365. * @since 1.0
  366. * @access public
  367. * @return void
  368. * @throws HTML_QuickForm_Error
  369. */
  370. function setDefaults($defaultValues = null, $filter = null)
  371. {
  372. if (is_array($defaultValues)) {
  373. if (isset($filter)) {
  374. if (is_array($filter) && (2 != count($filter) || !is_callable($filter))) {
  375. foreach ($filter as $val) {
  376. if (!is_callable($val)) {
  377. return PEAR::raiseError(null, QUICKFORM_INVALID_FILTER, null, E_USER_WARNING, "Callback function does not exist in QuickForm::setDefaults()", 'HTML_QuickForm_Error', true);
  378. } else {
  379. $defaultValues = $this->_recursiveFilter($val, $defaultValues);
  380. }
  381. }
  382. } elseif (!is_callable($filter)) {
  383. return PEAR::raiseError(null, QUICKFORM_INVALID_FILTER, null, E_USER_WARNING, "Callback function does not exist in QuickForm::setDefaults()", 'HTML_QuickForm_Error', true);
  384. } else {
  385. $defaultValues = $this->_recursiveFilter($filter, $defaultValues);
  386. }
  387. }
  388. $this->_defaultValues = HTML_QuickForm::arrayMerge($this->_defaultValues, $defaultValues);
  389. $this->_constantValues = $this->_defaultValues;
  390. foreach (array_keys($this->_elements) as $key) {
  391. $this->_elements[$key]->onQuickFormEvent('updateValue', null, $this);
  392. }
  393. }
  394. } // end func setDefaults
  395. // }}}
  396. // {{{ setConstants()
  397. /**
  398. * Initializes constant form values.
  399. * These values won't get overridden by POST or GET vars
  400. *
  401. * @param array $constantValues values used to fill the form
  402. * @param mixed $filter (optional) filter(s) to apply to all default values
  403. *
  404. * @since 2.0
  405. * @access public
  406. * @return void
  407. * @throws HTML_QuickForm_Error
  408. */
  409. function setConstants($constantValues = null, $filter = null)
  410. {
  411. if (is_array($constantValues)) {
  412. if (isset($filter)) {
  413. if (is_array($filter) && (2 != count($filter) || !is_callable($filter))) {
  414. foreach ($filter as $val) {
  415. if (!is_callable($val)) {
  416. return PEAR::raiseError(null, QUICKFORM_INVALID_FILTER, null, E_USER_WARNING, "Callback function does not exist in QuickForm::setConstants()", 'HTML_QuickForm_Error', true);
  417. } else {
  418. $constantValues = $this->_recursiveFilter($val, $constantValues);
  419. }
  420. }
  421. } elseif (!is_callable($filter)) {
  422. return PEAR::raiseError(null, QUICKFORM_INVALID_FILTER, null, E_USER_WARNING, "Callback function does not exist in QuickForm::setConstants()", 'HTML_QuickForm_Error', true);
  423. } else {
  424. $constantValues = $this->_recursiveFilter($filter, $constantValues);
  425. }
  426. }
  427. $this->_constantValues = HTML_QuickForm::arrayMerge($this->_constantValues, $constantValues);
  428. foreach (array_keys($this->_elements) as $key) {
  429. $this->_elements[$key]->onQuickFormEvent('updateValue', null, $this);
  430. }
  431. }
  432. } // end func setConstants
  433. // }}}
  434. // {{{ setMaxFileSize()
  435. /**
  436. * Sets the value of MAX_FILE_SIZE hidden element
  437. *
  438. * @param int $bytes Size in bytes
  439. * @since 3.0
  440. * @access public
  441. * @return void
  442. */
  443. function setMaxFileSize($bytes = 0)
  444. {
  445. if ($bytes > 0) {
  446. $this->_maxFileSize = $bytes;
  447. }
  448. if (!$this->elementExists('MAX_FILE_SIZE')) {
  449. $this->addElement('hidden', 'MAX_FILE_SIZE', $this->_maxFileSize);
  450. } else {
  451. $el =& $this->getElement('MAX_FILE_SIZE');
  452. $el->updateAttributes(array('value' => $this->_maxFileSize));
  453. }
  454. } // end func setMaxFileSize
  455. // }}}
  456. // {{{ getMaxFileSize()
  457. /**
  458. * Returns the value of MAX_FILE_SIZE hidden element
  459. *
  460. * @since 3.0
  461. * @access public
  462. * @return int max file size in bytes
  463. */
  464. function getMaxFileSize()
  465. {
  466. return $this->_maxFileSize;
  467. } // end func getMaxFileSize
  468. // }}}
  469. // {{{ &createElement()
  470. /**
  471. * Creates a new form element of the given type.
  472. *
  473. * This method accepts variable number of parameters, their
  474. * meaning and count depending on $elementType
  475. *
  476. * @param string $elementType type of element to add (text, textarea, file...)
  477. * @since 1.0
  478. * @access public
  479. * @return HTML_QuickForm_Element
  480. * @throws HTML_QuickForm_Error
  481. */
  482. public function &createElement($elementType)
  483. {
  484. $args = func_get_args();
  485. $element = HTML_QuickForm::_loadElement('createElement', $elementType, array_slice($args, 1));
  486. return $element;
  487. } // end func createElement
  488. // }}}
  489. // {{{ _loadElement()
  490. /**
  491. * Returns a form element of the given type
  492. *
  493. * @param string $event event to send to newly created element ('createElement' or 'addElement')
  494. * @param string $type element type
  495. * @param array $args arguments for event
  496. * @since 2.0
  497. * @access private
  498. * @return HTML_QuickForm_Element
  499. * @throws HTML_QuickForm_Error
  500. */
  501. public function &_loadElement($event, $type, $args)
  502. {
  503. $className = null;
  504. // Try if class exists
  505. if (!class_exists($type)) {
  506. // Try classic class name HTML_QuickForm_
  507. $className = 'HTML_QuickForm_'.$type;
  508. if (!class_exists($className)) {
  509. // Try classic class name HTML_QuickForm_ with strtolower
  510. $lowerType = strtolower($type);
  511. $className = 'HTML_QuickForm_'.$lowerType;
  512. if (!class_exists($className)) {
  513. // Try new class name CamelCase
  514. $className = underScoreToCamelCase($type);
  515. if (!class_exists($className)) {
  516. throw new \Exception("Class '$className' does not exist. ");
  517. }
  518. }
  519. }
  520. } else {
  521. $className = $type;
  522. }
  523. if (empty($className)) {
  524. throw new \Exception("Class '$className' does not exist. ");
  525. }
  526. for ($i = 0; $i < self::MAX_ELEMENT_ARGUMENT; $i++) {
  527. if (!isset($args[$i])) {
  528. $args[$i] = null;
  529. }
  530. }
  531. /** @var HTML_QuickForm_element $element */
  532. $element = new $className(
  533. $args[0],
  534. $args[1],
  535. $args[2],
  536. $args[3],
  537. $args[4],
  538. $args[5],
  539. $args[6],
  540. $args[7],
  541. $args[8],
  542. $args[9]
  543. );
  544. if ($event != 'createElement') {
  545. $err = $element->onQuickFormEvent($event, $args, $this);
  546. if ($err !== true) {
  547. return $err;
  548. }
  549. }
  550. return $element;
  551. }
  552. // }}}
  553. // {{{ addElement()
  554. /**
  555. * Adds an element into the form
  556. *
  557. * If $element is a string representing element type, then this
  558. * method accepts variable number of parameters, their meaning
  559. * and count depending on $element
  560. *
  561. * @param mixed $element element object or type of element to add (text, textarea, file...)
  562. * @since 1.0
  563. * @return HTML_QuickForm_element a reference to newly added element
  564. * @access public
  565. * @throws HTML_QuickForm_Error
  566. */
  567. public function &addElement($element)
  568. {
  569. if (is_object($element) && is_subclass_of($element, 'html_quickform_element')) {
  570. $elementObject = &$element;
  571. $elementObject->onQuickFormEvent('updateValue', null, $this);
  572. } else {
  573. $args = func_get_args();
  574. $elementObject =& $this->_loadElement('addElement', $element, array_slice($args, 1));
  575. if (PEAR::isError($elementObject)) {
  576. return $elementObject;
  577. }
  578. }
  579. $elementName = $elementObject->getName();
  580. // Add the element if it is not an incompatible duplicate
  581. if (!empty($elementName) &&
  582. isset($this->_elementIndex[$elementName])
  583. ) {
  584. if ($this->_elements[$this->_elementIndex[$elementName]]->getType() == $elementObject->getType()) {
  585. $this->_elements[] =& $elementObject;
  586. $elKeys = array_keys($this->_elements);
  587. $this->_duplicateIndex[$elementName][] = end($elKeys);
  588. } else {
  589. $error = PEAR::raiseError(
  590. null,
  591. QUICKFORM_INVALID_ELEMENT_NAME,
  592. null,
  593. E_USER_WARNING,
  594. "Element '$elementName' already exists in HTML_QuickForm::addElement()", 'HTML_QuickForm_Error',
  595. true
  596. );
  597. return $error;
  598. }
  599. } else {
  600. $this->_elements[] =& $elementObject;
  601. $elKeys = array_keys($this->_elements);
  602. $this->_elementIndex[$elementName] = end($elKeys);
  603. }
  604. if ($this->_freezeAll) {
  605. $elementObject->freeze();
  606. }
  607. return $elementObject;
  608. } // end func addElement
  609. // }}}
  610. // {{{ insertElementBefore()
  611. /**
  612. * Inserts a new element right before the other element
  613. *
  614. * Warning: it is not possible to check whether the $element is already
  615. * added to the form, therefore if you want to move the existing form
  616. * element to a new position, you'll have to use removeElement():
  617. * $form->insertElementBefore($form->removeElement('foo', false), 'bar');
  618. *
  619. * @access public
  620. * @since 3.2.4
  621. * @param HTML_QuickForm_element Element to insert
  622. * @param string Name of the element before which the new
  623. * one is inserted
  624. * @return HTML_QuickForm_element reference to inserted element
  625. * @throws HTML_QuickForm_Error
  626. */
  627. function &insertElementBefore(&$element, $nameAfter)
  628. {
  629. if (!empty($this->_duplicateIndex[$nameAfter])) {
  630. $error = PEAR::raiseError(null, QUICKFORM_INVALID_ELEMENT_NAME, null, E_USER_WARNING, 'Several elements named "' . $nameAfter . '" exist in HTML_QuickForm::insertElementBefore().', 'HTML_QuickForm_Error', true);
  631. return $error;
  632. } elseif (!$this->elementExists($nameAfter)) {
  633. $error = PEAR::raiseError(null, QUICKFORM_NONEXIST_ELEMENT, null, E_USER_WARNING, "Element '$nameAfter' does not exist in HTML_QuickForm::insertElementBefore()", 'HTML_QuickForm_Error', true);
  634. return $error;
  635. }
  636. $elementName = $element->getName();
  637. $targetIdx = $this->_elementIndex[$nameAfter];
  638. $duplicate = false;
  639. // Like in addElement(), check that it's not an incompatible duplicate
  640. if (!empty($elementName) && isset($this->_elementIndex[$elementName])) {
  641. if ($this->_elements[$this->_elementIndex[$elementName]]->getType() != $element->getType()) {
  642. $error = PEAR::raiseError(null, QUICKFORM_INVALID_ELEMENT_NAME, null, E_USER_WARNING, "Element '$elementName' already exists in HTML_QuickForm::insertElementBefore()", 'HTML_QuickForm_Error', true);
  643. return $error;
  644. }
  645. $duplicate = true;
  646. }
  647. // Move all the elements after added back one place, reindex _elementIndex and/or _duplicateIndex
  648. $elKeys = array_keys($this->_elements);
  649. for ($i = end($elKeys); $i >= $targetIdx; $i--) {
  650. if (isset($this->_elements[$i])) {
  651. $currentName = $this->_elements[$i]->getName();
  652. $this->_elements[$i + 1] =& $this->_elements[$i];
  653. if ($this->_elementIndex[$currentName] == $i) {
  654. $this->_elementIndex[$currentName] = $i + 1;
  655. } else {
  656. $dupIdx = array_search($i, $this->_duplicateIndex[$currentName]);
  657. $this->_duplicateIndex[$currentName][$dupIdx] = $i + 1;
  658. }
  659. unset($this->_elements[$i]);
  660. }
  661. }
  662. // Put the element in place finally
  663. $this->_elements[$targetIdx] =& $element;
  664. if (!$duplicate) {
  665. $this->_elementIndex[$elementName] = $targetIdx;
  666. } else {
  667. $this->_duplicateIndex[$elementName][] = $targetIdx;
  668. }
  669. $element->onQuickFormEvent('updateValue', null, $this);
  670. if ($this->_freezeAll) {
  671. $element->freeze();
  672. }
  673. // If not done, the elements will appear in reverse order
  674. ksort($this->_elements);
  675. return $element;
  676. }
  677. // }}}
  678. // {{{ addGroup()
  679. /**
  680. * Adds an element group
  681. * @param array $elements array of elements composing the group
  682. * @param string $name (optional)group name
  683. * @param string $groupLabel (optional)group label
  684. * @param string $separator (optional)string to separate elements
  685. * @param string $appendName (optional)specify whether the group name should be
  686. * used in the form element name ex: group[element]
  687. * @return HTML_QuickForm_group reference to a newly added group
  688. * @since 2.8
  689. * @access public
  690. * @throws HTML_QuickForm_Error
  691. */
  692. function &addGroup($elements, $name=null, $groupLabel='', $separator=null, $appendName = true)
  693. {
  694. static $anonGroups = 1;
  695. if (0 == strlen($name)) {
  696. $name = 'qf_group_' . $anonGroups++;
  697. $appendName = false;
  698. }
  699. $group = & $this->addElement('group', $name, $groupLabel, $elements, $separator, $appendName);
  700. return $group;
  701. } // end func addGroup
  702. // }}}
  703. // {{{ &getElement()
  704. /**
  705. * Returns a reference to the element
  706. *
  707. * @param string $element Element name
  708. * @since 2.0
  709. * @access public
  710. * @return HTML_QuickForm_element reference to element
  711. * @throws HTML_QuickForm_Error
  712. */
  713. function &getElement($element)
  714. {
  715. if (isset($this->_elementIndex[$element])) {
  716. return $this->_elements[$this->_elementIndex[$element]];
  717. } else {
  718. $error = PEAR::raiseError(null, QUICKFORM_NONEXIST_ELEMENT, null, E_USER_WARNING, "Element '$element' does not exist in HTML_QuickForm::getElement()", 'HTML_QuickForm_Error', true);
  719. return $error;
  720. }
  721. } // end func getElement
  722. // }}}
  723. // {{{ &getElementValue()
  724. /**
  725. * Returns the element's raw value
  726. *
  727. * This returns the value as submitted by the form (not filtered)
  728. * or set via setDefaults() or setConstants()
  729. *
  730. * @param string $element Element name
  731. * @since 2.0
  732. * @access public
  733. * @return mixed element value
  734. * @throws HTML_QuickForm_Error
  735. */
  736. function &getElementValue($element)
  737. {
  738. if (!isset($this->_elementIndex[$element])) {
  739. $error = PEAR::raiseError(null, QUICKFORM_NONEXIST_ELEMENT, null, E_USER_WARNING, "Element '$element' does not exist in HTML_QuickForm::getElementValue()", 'HTML_QuickForm_Error', true);
  740. return $error;
  741. }
  742. $value = $this->_elements[$this->_elementIndex[$element]]->getValue();
  743. if (isset($this->_duplicateIndex[$element])) {
  744. foreach ($this->_duplicateIndex[$element] as $index) {
  745. if (null !== ($v = $this->_elements[$index]->getValue())) {
  746. if (is_array($value)) {
  747. $value[] = $v;
  748. } else {
  749. $value = (null === $value)? $v: array($value, $v);
  750. }
  751. }
  752. }
  753. }
  754. return $value;
  755. } // end func getElementValue
  756. // }}}
  757. // {{{ getSubmitValue()
  758. /**
  759. * Returns the elements value after submit and filter
  760. *
  761. * @param string Element name
  762. * @since 2.0
  763. * @access public
  764. * @return mixed submitted element value or null if not set
  765. */
  766. function getSubmitValue($elementName)
  767. {
  768. $value = null;
  769. if (isset($this->_submitValues[$elementName]) || isset($this->_submitFiles[$elementName])) {
  770. $value = isset($this->_submitValues[$elementName])? $this->_submitValues[$elementName]: array();
  771. if (is_array($value) && isset($this->_submitFiles[$elementName])) {
  772. foreach ($this->_submitFiles[$elementName] as $k => $v) {
  773. $value = HTML_QuickForm::arrayMerge($value, $this->_reindexFiles($this->_submitFiles[$elementName][$k], $k));
  774. }
  775. }
  776. } elseif ('file' == $this->getElementType($elementName)) {
  777. return $this->getElementValue($elementName);
  778. } elseif (false !== ($pos = strpos($elementName, '['))) {
  779. $base = str_replace(
  780. array('\\', '\''), array('\\\\', '\\\''),
  781. substr($elementName, 0, $pos)
  782. );
  783. $idx = "['" . str_replace(
  784. array('\\', '\'', ']', '['), array('\\\\', '\\\'', '', "']['"),
  785. substr($elementName, $pos + 1, -1)
  786. ) . "']";
  787. if (isset($this->_submitValues[$base])) {
  788. $value = eval("return (isset(\$this->_submitValues['{$base}']{$idx})) ? \$this->_submitValues['{$base}']{$idx} : null;");
  789. }
  790. if ((is_array($value) || null === $value) && isset($this->_submitFiles[$base])) {
  791. $props = array('name', 'type', 'size', 'tmp_name', 'error');
  792. $code = "if (!isset(\$this->_submitFiles['{$base}']['name']{$idx})) {\n" .
  793. " return null;\n" .
  794. "} else {\n" .
  795. " \$v = array();\n";
  796. foreach ($props as $prop) {
  797. $code .= " \$v = HTML_QuickForm::arrayMerge(\$v, \$this->_reindexFiles(\$this->_submitFiles['{$base}']['{$prop}']{$idx}, '{$prop}'));\n";
  798. }
  799. $fileValue = eval($code . " return \$v;\n}\n");
  800. if (null !== $fileValue) {
  801. $value = null === $value? $fileValue: HTML_QuickForm::arrayMerge($value, $fileValue);
  802. }
  803. }
  804. }
  805. // This is only supposed to work for groups with appendName = false
  806. if (null === $value && 'group' == $this->getElementType($elementName)) {
  807. $group =& $this->getElement($elementName);
  808. $elements =& $group->getElements();
  809. foreach (array_keys($elements) as $key) {
  810. $name = $group->getElementName($key);
  811. // prevent endless recursion in case of radios and such
  812. if ($name != $elementName) {
  813. if (null !== ($v = $this->getSubmitValue($name))) {
  814. $value[$name] = $v;
  815. }
  816. }
  817. }
  818. }
  819. if ($this->getElementType($elementName) == 'date_range_picker') {
  820. /** @var DateRangePicker $element */
  821. $element = $this->getElement($elementName);
  822. $parsedDates = $element->parseDateRange($value);
  823. if (!$element->validateDates($parsedDates)) {
  824. $this->_errors[$elementName] = get_lang('CheckDates');
  825. }
  826. $this->_submitValues[$elementName.'_start'] = $parsedDates['start'];
  827. $this->_submitValues[$elementName.'_end'] = $parsedDates['end'];
  828. }
  829. return $value;
  830. }
  831. /**
  832. * A helper function to change the indexes in $_FILES array
  833. *
  834. * @param mixed Some value from the $_FILES array
  835. * @param string The key from the $_FILES array that should be appended
  836. * @return array
  837. */
  838. function _reindexFiles($value, $key)
  839. {
  840. if (!is_array($value)) {
  841. return array($key => $value);
  842. } else {
  843. $ret = array();
  844. foreach ($value as $k => $v) {
  845. $ret[$k] = $this->_reindexFiles($v, $key);
  846. }
  847. return $ret;
  848. }
  849. }
  850. // }}}
  851. // {{{ getElementError()
  852. /**
  853. * Returns error corresponding to validated element
  854. *
  855. * @param string $element Name of form element to check
  856. * @since 1.0
  857. * @access public
  858. * @return string error message corresponding to checked element
  859. */
  860. function getElementError($element)
  861. {
  862. if (isset($this->_errors[$element])) {
  863. return $this->_errors[$element];
  864. }
  865. } // end func getElementError
  866. // }}}
  867. // {{{ setElementError()
  868. /**
  869. * Set error message for a form element
  870. *
  871. * @param string $element Name of form element to set error for
  872. * @param string $message Error message, if empty then removes the current error message
  873. * @since 1.0
  874. * @access public
  875. * @return void
  876. */
  877. function setElementError($element, $message = null)
  878. {
  879. if (!empty($message)) {
  880. $this->_errors[$element] = $message;
  881. } else {
  882. unset($this->_errors[$element]);
  883. }
  884. } // end func setElementError
  885. // }}}
  886. // {{{ getElementType()
  887. /**
  888. * Returns the type of the given element
  889. *
  890. * @param string $element Name of form element
  891. * @since 1.1
  892. * @access public
  893. * @return string Type of the element, false if the element is not found
  894. */
  895. function getElementType($element)
  896. {
  897. if (isset($this->_elementIndex[$element])) {
  898. return $this->_elements[$this->_elementIndex[$element]]->getType();
  899. }
  900. return false;
  901. } // end func getElementType
  902. // }}}
  903. // {{{ updateElementAttr()
  904. /**
  905. * Updates Attributes for one or more elements
  906. *
  907. * @param mixed $elements Array of element names/objects or string of elements to be updated
  908. * @param mixed $attrs Array or sting of html attributes
  909. * @since 2.10
  910. * @access public
  911. * @return void
  912. */
  913. function updateElementAttr($elements, $attrs)
  914. {
  915. if (is_string($elements)) {
  916. $elements = split('[ ]?,[ ]?', $elements);
  917. }
  918. foreach (array_keys($elements) as $key) {
  919. if (is_object($elements[$key]) && is_a($elements[$key], 'HTML_QuickForm_element')) {
  920. $elements[$key]->updateAttributes($attrs);
  921. } elseif (isset($this->_elementIndex[$elements[$key]])) {
  922. $this->_elements[$this->_elementIndex[$elements[$key]]]->updateAttributes($attrs);
  923. if (isset($this->_duplicateIndex[$elements[$key]])) {
  924. foreach ($this->_duplicateIndex[$elements[$key]] as $index) {
  925. $this->_elements[$index]->updateAttributes($attrs);
  926. }
  927. }
  928. }
  929. }
  930. } // end func updateElementAttr
  931. // }}}
  932. // {{{ removeElement()
  933. /**
  934. * Removes an element
  935. *
  936. * The method "unlinks" an element from the form, returning the reference
  937. * to the element object. If several elements named $elementName exist,
  938. * it removes the first one, leaving the others intact.
  939. *
  940. * @param string $elementName The element name
  941. * @param boolean $removeRules True if rules for this element are to be removed too
  942. * @access public
  943. * @since 2.0
  944. * @return HTML_QuickForm_element a reference to the removed element
  945. * @throws HTML_QuickForm_Error
  946. */
  947. function &removeElement($elementName, $removeRules = true)
  948. {
  949. if (!isset($this->_elementIndex[$elementName])) {
  950. $error = PEAR::raiseError(null, QUICKFORM_NONEXIST_ELEMENT, null, E_USER_WARNING, "Element '$elementName' does not exist in HTML_QuickForm::removeElement()", 'HTML_QuickForm_Error', true);
  951. return $error;
  952. }
  953. $el =& $this->_elements[$this->_elementIndex[$elementName]];
  954. unset($this->_elements[$this->_elementIndex[$elementName]]);
  955. if (empty($this->_duplicateIndex[$elementName])) {
  956. unset($this->_elementIndex[$elementName]);
  957. } else {
  958. $this->_elementIndex[$elementName] = array_shift($this->_duplicateIndex[$elementName]);
  959. }
  960. if ($removeRules) {
  961. $this->_required = array_diff($this->_required, array($elementName));
  962. unset($this->_rules[$elementName], $this->_errors[$elementName]);
  963. if ('group' == $el->getType()) {
  964. foreach (array_keys($el->getElements()) as $key) {
  965. unset($this->_rules[$el->getElementName($key)]);
  966. }
  967. }
  968. }
  969. return $el;
  970. } // end func removeElement
  971. /**
  972. * Adds a validation rule for the given field
  973. *
  974. * If the element is in fact a group, it will be considered as a whole.
  975. * To validate grouped elements as separated entities,
  976. * use addGroupRule instead of addRule.
  977. *
  978. * @param string $element Form element name
  979. * @param string $message Message to display for invalid data
  980. * @param string $type Rule type, use getRegisteredRules() to get types
  981. * @param string $format (optional)Required for extra rule data
  982. * @param string $validation (optional)Where to perform validation: "server", "client"
  983. * @param boolean $reset Client-side validation: reset the form element to its original value if there is an error?
  984. * @param boolean $force Force the rule to be applied, even if the target form element does not exist
  985. * @since 1.0
  986. * @access public
  987. * @throws HTML_QuickForm_Error
  988. */
  989. function addRule(
  990. $element,
  991. $message,
  992. $type,
  993. $format = null,
  994. $validation = 'server',
  995. $reset = false,
  996. $force = false
  997. ) {
  998. if (!$force) {
  999. if (!is_array($element) && !$this->elementExists($element)) {
  1000. return PEAR::raiseError(
  1001. null,
  1002. QUICKFORM_NONEXIST_ELEMENT,
  1003. null,
  1004. E_USER_WARNING,
  1005. "Element '$element' does not exist in HTML_QuickForm::addRule()",
  1006. 'HTML_QuickForm_Error',
  1007. true
  1008. );
  1009. } elseif (is_array($element)) {
  1010. foreach ($element as $el) {
  1011. if (!$this->elementExists($el)) {
  1012. return PEAR::raiseError(
  1013. null,
  1014. QUICKFORM_NONEXIST_ELEMENT,
  1015. null,
  1016. E_USER_WARNING,
  1017. "Element '$el' does not exist in HTML_QuickForm::addRule()",
  1018. 'HTML_QuickForm_Error',
  1019. true
  1020. );
  1021. }
  1022. }
  1023. }
  1024. }
  1025. if (false === ($newName = $this->isRuleRegistered($type, true))) {
  1026. return PEAR::raiseError(
  1027. null,
  1028. QUICKFORM_INVALID_RULE,
  1029. null,
  1030. E_USER_WARNING,
  1031. "Rule '$type' is not registered in HTML_QuickForm::addRule()",
  1032. 'HTML_QuickForm_Error',
  1033. true
  1034. );
  1035. } elseif (is_string($newName)) {
  1036. $type = $newName;
  1037. }
  1038. if (is_array($element)) {
  1039. $dependent = $element;
  1040. $element = array_shift($dependent);
  1041. } else {
  1042. $dependent = null;
  1043. }
  1044. if ($type == 'required' || $type == 'uploadedfile') {
  1045. $this->_required[] = $element;
  1046. }
  1047. if (!isset($this->_rules[$element])) {
  1048. $this->_rules[$element] = array();
  1049. }
  1050. if ($validation == 'client') {
  1051. $this->updateAttributes(array('onsubmit' => 'try { var myValidator = validate_' . $this->_attributes['id'] . '; } catch(e) { return true; } return myValidator(this);'));
  1052. }
  1053. $this->_rules[$element][] = array(
  1054. 'type' => $type,
  1055. 'format' => $format,
  1056. 'message' => $message,
  1057. 'validation' => $validation,
  1058. 'reset' => $reset,
  1059. 'dependent' => $dependent
  1060. );
  1061. }
  1062. /**
  1063. * Adds a validation rule for the given group of elements
  1064. *
  1065. * Only groups with a name can be assigned a validation rule
  1066. * Use addGroupRule when you need to validate elements inside the group.
  1067. * Use addRule if you need to validate the group as a whole. In this case,
  1068. * the same rule will be applied to all elements in the group.
  1069. * Use addRule if you need to validate the group against a function.
  1070. *
  1071. * @param string $group Form group name
  1072. * @param mixed $arg1 Array for multiple elements or error message string for one element
  1073. * @param string $type (optional)Rule type use getRegisteredRules() to get types
  1074. * @param string $format (optional)Required for extra rule data
  1075. * @param int $howmany (optional)How many valid elements should be in the group
  1076. * @param string $validation (optional)Where to perform validation: "server", "client"
  1077. * @param bool $reset Client-side: whether to reset the element's value to its original state if validation failed.
  1078. * @since 2.5
  1079. * @access public
  1080. * @throws HTML_QuickForm_Error
  1081. */
  1082. function addGroupRule($group, $arg1, $type='', $format=null, $howmany=0, $validation = 'server', $reset = false)
  1083. {
  1084. if (!$this->elementExists($group)) {
  1085. return PEAR::raiseError(null, QUICKFORM_NONEXIST_ELEMENT, null, E_USER_WARNING, "Group '$group' does not exist in HTML_QuickForm::addGroupRule()", 'HTML_QuickForm_Error', true);
  1086. }
  1087. $groupObj =& $this->getElement($group);
  1088. if (is_array($arg1)) {
  1089. $required = 0;
  1090. foreach ($arg1 as $elementIndex => $rules) {
  1091. $elementName = $groupObj->getElementName($elementIndex);
  1092. foreach ($rules as $rule) {
  1093. $format = (isset($rule[2])) ? $rule[2] : null;
  1094. $validation = (isset($rule[3]) && 'client' == $rule[3])? 'client': 'server';
  1095. $reset = isset($rule[4]) && $rule[4];
  1096. $type = $rule[1];
  1097. if (false === ($newName = $this->isRuleRegistered($type, true))) {
  1098. return PEAR::raiseError(null, QUICKFORM_INVALID_RULE, null, E_USER_WARNING, "Rule '$type' is not registered in HTML_QuickForm::addGroupRule()", 'HTML_QuickForm_Error', true);
  1099. } elseif (is_string($newName)) {
  1100. $type = $newName;
  1101. }
  1102. $this->_rules[$elementName][] = array(
  1103. 'type' => $type,
  1104. 'format' => $format,
  1105. 'message' => $rule[0],
  1106. 'validation' => $validation,
  1107. 'reset' => $reset,
  1108. 'group' => $group,
  1109. );
  1110. if ('required' == $type || 'uploadedfile' == $type) {
  1111. $groupObj->_required[] = $elementName;
  1112. $this->_required[] = $elementName;
  1113. $required++;
  1114. }
  1115. if ('client' == $validation) {
  1116. $this->updateAttributes(array('onsubmit' => 'try { var myValidator = validate_' . $this->_attributes['id'] . '; } catch(e) { return true; } return myValidator(this);'));
  1117. }
  1118. }
  1119. }
  1120. if ($required > 0 && count($groupObj->getElements()) == $required) {
  1121. $this->_required[] = $group;
  1122. }
  1123. } elseif (is_string($arg1)) {
  1124. if (false === ($newName = $this->isRuleRegistered($type, true))) {
  1125. return PEAR::raiseError(null, QUICKFORM_INVALID_RULE, null, E_USER_WARNING, "Rule '$type' is not registered in HTML_QuickForm::addGroupRule()", 'HTML_QuickForm_Error', true);
  1126. } elseif (is_string($newName)) {
  1127. $type = $newName;
  1128. }
  1129. // addGroupRule() should also handle <select multiple>
  1130. if (is_a($groupObj, 'html_quickform_group')) {
  1131. // Radios need to be handled differently when required
  1132. if ($type == 'required' && $groupObj->getGroupType() == 'radio') {
  1133. $howmany = ($howmany == 0) ? 1 : $howmany;
  1134. } else {
  1135. $howmany = ($howmany == 0) ? count($groupObj->getElements()) : $howmany;
  1136. }
  1137. }
  1138. $this->_rules[$group][] = array(
  1139. 'type' => $type,
  1140. 'format' => $format,
  1141. 'message' => $arg1,
  1142. 'validation' => $validation,
  1143. 'howmany' => $howmany,
  1144. 'reset' => $reset,
  1145. );
  1146. if ($type == 'required') {
  1147. $this->_required[] = $group;
  1148. }
  1149. if ($validation == 'client') {
  1150. $this->updateAttributes(array('onsubmit' => 'try { var myValidator = validate_' . $this->_attributes['id'] . '; } catch(e) { return true; } return myValidator(this);'));
  1151. }
  1152. }
  1153. } // end func addGroupRule
  1154. // }}}
  1155. // {{{ addFormRule()
  1156. /**
  1157. * Adds a global validation rule
  1158. *
  1159. * This should be used when for a rule involving several fields or if
  1160. * you want to use some completely custom validation for your form.
  1161. * The rule function/method should return true in case of successful
  1162. * validation and array('element name' => 'error') when there were errors.
  1163. *
  1164. * @access public
  1165. * @param mixed Callback, either function name or array(&$object, 'method')
  1166. * @throws HTML_QuickForm_Error
  1167. */
  1168. function addFormRule($rule)
  1169. {
  1170. if (!is_callable($rule)) {
  1171. return PEAR::raiseError(null, QUICKFORM_INVALID_RULE, null, E_USER_WARNING, 'Callback function does not exist in HTML_QuickForm::addFormRule()', 'HTML_QuickForm_Error', true);
  1172. }
  1173. $this->_formRules[] = $rule;
  1174. }
  1175. // }}}
  1176. // {{{ applyFilter()
  1177. /**
  1178. * Applies a data filter for the given field(s)
  1179. *
  1180. * @param mixed $element Form element name or array of such names
  1181. * @param mixed $filter Callback, either function name or array(&$object, 'method')
  1182. * @since 2.0
  1183. * @access public
  1184. * @throws HTML_QuickForm_Error
  1185. */
  1186. function applyFilter($element, $filter)
  1187. {
  1188. if (!is_callable($filter)) {
  1189. return PEAR::raiseError(null, QUICKFORM_INVALID_FILTER, null, E_USER_WARNING, "Callback function does not exist in QuickForm::applyFilter()", 'HTML_QuickForm_Error', true);
  1190. }
  1191. if ($element == '__ALL__') {
  1192. $this->_submitValues = $this->_recursiveFilter($filter, $this->_submitValues);
  1193. } else {
  1194. if (!is_array($element)) {
  1195. $element = array($element);
  1196. }
  1197. foreach ($element as $elName) {
  1198. $value = $this->getSubmitValue($elName);
  1199. if (null !== $value) {
  1200. if (false === strpos($elName, '[')) {
  1201. $this->_submitValues[$elName] = $this->_recursiveFilter($filter, $value);
  1202. } else {
  1203. $idx = "['" . str_replace(
  1204. array('\\', '\'', ']', '['), array('\\\\', '\\\'', '', "']['"),
  1205. $elName
  1206. ) . "']";
  1207. eval("\$this->_submitValues{$idx} = \$this->_recursiveFilter(\$filter, \$value);");
  1208. }
  1209. }
  1210. }
  1211. }
  1212. } // end func applyFilter
  1213. // }}}
  1214. // {{{ _recursiveFilter()
  1215. /**
  1216. * Recursively apply a filter function
  1217. *
  1218. * @param string $filter filter to apply
  1219. * @param mixed $value submitted values
  1220. * @since 2.0
  1221. * @access private
  1222. * @return cleaned values
  1223. */
  1224. function _recursiveFilter($filter, $value)
  1225. {
  1226. if (is_array($value)) {
  1227. $cleanValues = array();
  1228. foreach ($value as $k => $v) {
  1229. $cleanValues[$k] = $this->_recursiveFilter($filter, $v);
  1230. }
  1231. return $cleanValues;
  1232. } else {
  1233. return call_user_func($filter, $value);
  1234. }
  1235. } // end func _recursiveFilter
  1236. // }}}
  1237. // {{{ arrayMerge()
  1238. /**
  1239. * Merges two arrays
  1240. *
  1241. * Merges two array like the PHP function array_merge but recursively.
  1242. * The main difference is that existing keys will not be renumbered
  1243. * if they are integers.
  1244. *
  1245. * @access public
  1246. * @param array $a original array
  1247. * @param array $b array which will be merged into first one
  1248. * @return array merged array
  1249. */
  1250. static function arrayMerge($a, $b)
  1251. {
  1252. foreach ($b as $k => $v) {
  1253. if (is_array($v)) {
  1254. if (isset($a[$k]) && !is_array($a[$k])) {
  1255. $a[$k] = $v;
  1256. } else {
  1257. if (!isset($a[$k])) {
  1258. $a[$k] = array();
  1259. }
  1260. $a[$k] = HTML_QuickForm::arrayMerge($a[$k], $v);
  1261. }
  1262. } else {
  1263. $a[$k] = $v;
  1264. }
  1265. }
  1266. return $a;
  1267. } // end func arrayMerge
  1268. // }}}
  1269. // {{{ isTypeRegistered()
  1270. /**
  1271. * Returns whether or not the form element type is supported
  1272. *
  1273. * @param string $type Form element type
  1274. * @since 1.0
  1275. * @access public
  1276. * @return boolean
  1277. */
  1278. static function isTypeRegistered($type)
  1279. {
  1280. return isset($GLOBALS['HTML_QUICKFORM_ELEMENT_TYPES'][strtolower($type)]);
  1281. } // end func isTypeRegistered
  1282. // }}}
  1283. // {{{ getRegisteredTypes()
  1284. /**
  1285. * Returns an array of registered element types
  1286. *
  1287. * @since 1.0
  1288. * @access public
  1289. * @return array
  1290. */
  1291. function getRegisteredTypes()
  1292. {
  1293. return array_keys($GLOBALS['HTML_QUICKFORM_ELEMENT_TYPES']);
  1294. } // end func getRegisteredTypes
  1295. // }}}
  1296. // {{{ isRuleRegistered()
  1297. /**
  1298. * Returns whether or not the given rule is supported
  1299. *
  1300. * @param string $name Validation rule name
  1301. * @param bool Whether to automatically register subclasses of HTML_QuickForm_Rule
  1302. * @since 1.0
  1303. * @access public
  1304. * @return mixed true if previously registered, false if not, new rule name if auto-registering worked
  1305. */
  1306. function isRuleRegistered($name, $autoRegister = false)
  1307. {
  1308. return true;
  1309. if (is_scalar($name) && isset($GLOBALS['_HTML_QuickForm_registered_rules'][$name])) {
  1310. return true;
  1311. } elseif (!$autoRegister) {
  1312. return false;
  1313. }
  1314. $ruleName = false;
  1315. if (is_object($name) && is_a($name, 'html_quickform_rule')) {
  1316. $ruleName = !empty($name->name)? $name->name: strtolower(get_class($name));
  1317. } elseif (is_string($name) && class_exists($name)) {
  1318. $parent = strtolower($name);
  1319. do {
  1320. if ('html_quickform_rule' == strtolower($parent)) {
  1321. $ruleName = strtolower($name);
  1322. break;
  1323. }
  1324. } while ($parent = get_parent_class($parent));
  1325. }
  1326. if ($ruleName) {
  1327. $registry =& HTML_QuickForm_RuleRegistry::singleton();
  1328. $registry->registerRule($ruleName, null, $name);
  1329. }
  1330. return $ruleName;
  1331. } // end func isRuleRegistered
  1332. // }}}
  1333. // {{{ getRegisteredRules()
  1334. /**
  1335. * Returns an array of registered validation rules
  1336. *
  1337. * @since 1.0
  1338. * @access public
  1339. * @return array
  1340. */
  1341. function getRegisteredRules()
  1342. {
  1343. return array_keys($GLOBALS['_HTML_QuickForm_registered_rules']);
  1344. } // end func getRegisteredRules
  1345. // }}}
  1346. // {{{ isElementRequired()
  1347. /**
  1348. * Returns whether or not the form element is required
  1349. *
  1350. * @param string $element Form element name
  1351. * @since 1.0
  1352. * @access public
  1353. * @return boolean
  1354. */
  1355. function isElementRequired($element)
  1356. {
  1357. return in_array($element, $this->_required, true);
  1358. } // end func isElementRequired
  1359. // }}}
  1360. // {{{ isElementFrozen()
  1361. /**
  1362. * Returns whether or not the form element is frozen
  1363. *
  1364. * @param string $element Form element name
  1365. * @since 1.0
  1366. * @access public
  1367. * @return boolean
  1368. */
  1369. function isElementFrozen($element)
  1370. {
  1371. if (isset($this->_elementIndex[$element])) {
  1372. return $this->_elements[$this->_elementIndex[$element]]->isFrozen();
  1373. }
  1374. return false;
  1375. } // end func isElementFrozen
  1376. // }}}
  1377. // {{{ setJsWarnings()
  1378. /**
  1379. * Sets JavaScript warning messages
  1380. *
  1381. * @param string $pref Prefix warning
  1382. * @param string $post Postfix warning
  1383. * @since 1.1
  1384. * @access public
  1385. * @return void
  1386. */
  1387. function setJsWarnings($pref, $post)
  1388. {
  1389. $this->_jsPrefix = $pref;
  1390. $this->_jsPostfix = $post;
  1391. } // end func setJsWarnings
  1392. // }}}
  1393. // {{{ setRequiredNote()
  1394. /**
  1395. * Sets required-note
  1396. *
  1397. * @param string $note Message indicating some elements are required
  1398. * @since 1.1
  1399. * @access public
  1400. * @return void
  1401. */
  1402. function setRequiredNote($note)
  1403. {
  1404. $this->_requiredNote = $note;
  1405. } // end func setRequiredNote
  1406. // }}}
  1407. // {{{ getRequiredNote()
  1408. /**
  1409. * Returns the required note
  1410. *
  1411. * @since 2.0
  1412. * @access public
  1413. * @return string
  1414. */
  1415. function getRequiredNote()
  1416. {
  1417. return $this->_requiredNote;
  1418. } // end func getRequiredNote
  1419. // }}}
  1420. // {{{ validate()
  1421. /**
  1422. * Performs the server side validation
  1423. * @access public
  1424. * @since 1.0
  1425. * @return boolean true if no error found
  1426. * @throws HTML_QuickForm_Error
  1427. */
  1428. function validate()
  1429. {
  1430. if (count($this->_rules) == 0 && count($this->_formRules) == 0 &&
  1431. $this->isSubmitted()) {
  1432. return (0 == count($this->_errors));
  1433. } elseif (!$this->isSubmitted()) {
  1434. return false;
  1435. }
  1436. $registry =& HTML_QuickForm_RuleRegistry::singleton();
  1437. foreach ($this->_rules as $target => $rules) {
  1438. $submitValue = $this->getSubmitValue($target);
  1439. foreach ($rules as $rule) {
  1440. if ((isset($rule['group']) && isset($this->_errors[$rule['group']])) ||
  1441. isset($this->_errors[$target])) {
  1442. continue 2;
  1443. }
  1444. // If element is not required and is empty, we shouldn't validate it
  1445. if (!$this->isElementRequired($target)) {
  1446. if (!isset($submitValue) || '' == $submitValue) {
  1447. continue 2;
  1448. // Fix for bug #3501: we shouldn't validate not uploaded files, either.
  1449. // Unfortunately, we can't just use $element->isUploadedFile() since
  1450. // the element in question can be buried in group. Thus this hack.
  1451. // See also bug #12014, we should only consider a file that has
  1452. // status UPLOAD_ERR_NO_FILE as not uploaded, in all other cases
  1453. // validation should be performed, so that e.g. 'maxfilesize' rule
  1454. // will display an error if status is UPLOAD_ERR_INI_SIZE
  1455. // or UPLOAD_ERR_FORM_SIZE
  1456. } elseif (is_array($submitValue)) {
  1457. if (false === ($pos = strpos($target, '['))) {
  1458. $isUpload = !empty($this->_submitFiles[$target]);
  1459. } else {
  1460. $base = str_replace(
  1461. array('\\', '\''), array('\\\\', '\\\''),
  1462. substr($target, 0, $pos)
  1463. );
  1464. $idx = "['" . str_replace(
  1465. array('\\', '\'', ']', '['), array('\\\\', '\\\'', '', "']['"),
  1466. substr($target, $pos + 1, -1)
  1467. ) . "']";
  1468. eval("\$isUpload = isset(\$this->_submitFiles['{$base}']['name']{$idx});");
  1469. }
  1470. if ($isUpload && (!isset($submitValue['error']) || UPLOAD_ERR_NO_FILE == $submitValue['error'])) {
  1471. continue 2;
  1472. }
  1473. }
  1474. }
  1475. if (isset($rule['dependent']) && is_array($rule['dependent'])) {
  1476. $values = array($submitValue);
  1477. foreach ($rule['dependent'] as $elName) {
  1478. $values[] = $this->getSubmitValue($elName);
  1479. }
  1480. $result = $registry->validate($rule['type'], $values, $rule['format'], true);
  1481. } elseif (is_array($submitValue) && !isset($rule['howmany'])) {
  1482. $result = $registry->validate($rule['type'], $submitValue, $rule['format'], true);
  1483. } else {
  1484. $result = $registry->validate($rule['type'], $submitValue, $rule['format'], false);
  1485. }
  1486. if (!$result || (!empty($rule['howmany']) && $rule['howmany'] > (int)$result)) {
  1487. if (isset($rule['group'])) {
  1488. $this->_errors[$rule['group']] = $rule['message'];
  1489. } else {
  1490. $this->_errors[$target] = $rule['message'];
  1491. }
  1492. }
  1493. }
  1494. }
  1495. // process the global rules now
  1496. foreach ($this->_formRules as $rule) {
  1497. if (true !== ($res = call_user_func($rule, $this->_submitValues, $this->_submitFiles))) {
  1498. if (is_array($res)) {
  1499. $this->_errors += $res;
  1500. } else {
  1501. return PEAR::raiseError(null, QUICKFORM_ERROR, null, E_USER_WARNING, 'Form rule callback returned invalid value in HTML_QuickForm::validate()', 'HTML_QuickForm_Error', true);
  1502. }
  1503. }
  1504. }
  1505. return (0 == count($this->_errors));
  1506. } // end func validate
  1507. // }}}
  1508. // {{{ freeze()
  1509. /**
  1510. * Displays elements without HTML input tags
  1511. *
  1512. * @param mixed $elementList array or string of element(s) to be frozen
  1513. * @since 1.0
  1514. * @access public
  1515. * @throws HTML_QuickForm_Error
  1516. */
  1517. function freeze($elementList=null)
  1518. {
  1519. if (!isset($elementList)) {
  1520. $this->_freezeAll = true;
  1521. $elementList = array();
  1522. } else {
  1523. if (!is_array($elementList)) {
  1524. $elementList = preg_split('/[ ]*,[ ]*/', $elementList);
  1525. }
  1526. $elementList = array_flip($elementList);
  1527. }
  1528. foreach (array_keys($this->_elements) as $key) {
  1529. $name = $this->_elements[$key]->getName();
  1530. if ($this->_freezeAll || isset($elementList[$name])) {
  1531. $this->_elements[$key]->freeze();
  1532. unset($elementList[$name]);
  1533. }
  1534. }
  1535. if (!empty($elementList)) {
  1536. return PEAR::raiseError(null, QUICKFORM_NONEXIST_ELEMENT, null, E_USER_WARNING, "Nonexistant element(s): '" . implode("', '", array_keys($elementList)) . "' in HTML_QuickForm::freeze()", 'HTML_QuickForm_Error', true);
  1537. }
  1538. return true;
  1539. } // end func freeze
  1540. // }}}
  1541. // {{{ isFrozen()
  1542. /**
  1543. * Returns whether or not the whole form is frozen
  1544. *
  1545. * @since 3.0
  1546. * @access public
  1547. * @return boolean
  1548. */
  1549. function isFrozen()
  1550. {
  1551. return $this->_freezeAll;
  1552. } // end func isFrozen
  1553. // }}}
  1554. // {{{ process()
  1555. /**
  1556. * Performs the form data processing
  1557. *
  1558. * @param mixed $callback Callback, either function name or array(&$object, 'method')
  1559. * @param bool $mergeFiles Whether uploaded files should be processed too
  1560. * @since 1.0
  1561. * @access public
  1562. * @throws HTML_QuickForm_Error
  1563. * @return mixed Whatever value the $callback function returns
  1564. */
  1565. function process($callback, $mergeFiles = true)
  1566. {
  1567. if (!is_callable($callback)) {
  1568. return PEAR::raiseError(null, QUICKFORM_INVALID_PROCESS, null, E_USER_WARNING, "Callback function does not exist in QuickForm::process()", 'HTML_QuickForm_Error', true);
  1569. }
  1570. $values = ($mergeFiles === true) ? HTML_QuickForm::arrayMerge($this->_submitValues, $this->_submitFiles) : $this->_submitValues;
  1571. return call_user_func($callback, $values);
  1572. } // end func process
  1573. // }}}
  1574. // {{{ accept()
  1575. /**
  1576. * Accepts a renderer
  1577. *
  1578. * @param object An HTML_QuickForm_Renderer object
  1579. * @since 3.0
  1580. * @access public
  1581. * @return void
  1582. */
  1583. function accept(&$renderer) {
  1584. $renderer->startForm($this);
  1585. foreach (array_keys($this->_elements) as $key) {
  1586. $element =& $this->_elements[$key];
  1587. $elementName = $element->getName();
  1588. $required = ($this->isElementRequired($elementName) && !$element->isFrozen());
  1589. $error = $this->getElementError($elementName);
  1590. $element->accept($renderer, $required, $error);
  1591. }
  1592. $renderer->finishForm($this);
  1593. } // end func accept
  1594. // }}}
  1595. // {{{ defaultRenderer()
  1596. /**
  1597. * Returns a reference to default renderer object
  1598. *
  1599. * @access public
  1600. * @since 3.0
  1601. * @return HTML_QuickForm_Renderer_Default
  1602. */
  1603. function &defaultRenderer() {
  1604. if (!isset($GLOBALS['_HTML_QuickForm_default_renderer'])) {
  1605. // Modified by Ivan Tcholakov, 16-MAR-2010. Suppressing a deprecation warning on PHP 5.3
  1606. //$GLOBALS['_HTML_QuickForm_default_renderer'] =& new HTML_QuickForm_Renderer_Default();
  1607. $GLOBALS['_HTML_QuickForm_default_renderer'] = new HTML_QuickForm_Renderer_Default();
  1608. }
  1609. return $GLOBALS['_HTML_QuickForm_default_renderer'];
  1610. } // end func defaultRenderer
  1611. // }}}
  1612. // {{{ toHtml ()
  1613. /**
  1614. * Returns an HTML version of the form
  1615. *
  1616. * @param string $in_data (optional) Any extra data to insert right
  1617. * before form is rendered. Useful when using templates.
  1618. *
  1619. * @return string Html version of the form
  1620. * @since 1.0
  1621. * @access public
  1622. */
  1623. public function toHtml($in_data = null)
  1624. {
  1625. if (!is_null($in_data)) {
  1626. $this->addElement('html', $in_data);
  1627. }
  1628. $renderer =& $this->defaultRenderer();
  1629. $this->accept($renderer);
  1630. return $renderer->toHtml();
  1631. } // end func toHtml
  1632. // }}}
  1633. // {{{ getValidationScript()
  1634. /**
  1635. * Returns the client side validation script
  1636. *
  1637. * @since 2.0
  1638. * @access public
  1639. * @return string Javascript to perform validation, empty string if no 'client' rules were added
  1640. */
  1641. function getValidationScript()
  1642. {
  1643. if (empty($this->_rules) || empty($this->_attributes['onsubmit'])) {
  1644. return '';
  1645. }
  1646. $registry =& HTML_QuickForm_RuleRegistry::singleton();
  1647. $test = array();
  1648. $js_escape = array(
  1649. "\r" => '\r',
  1650. "\n" => '\n',
  1651. "\t" => '\t',
  1652. "'" => "\\'",
  1653. '"' => '\"',
  1654. '\\' => '\\\\'
  1655. );
  1656. foreach ($this->_rules as $elementName => $rules) {
  1657. foreach ($rules as $rule) {
  1658. if ('client' == $rule['validation']) {
  1659. unset($element);
  1660. $dependent = isset($rule['dependent']) && is_array($rule['dependent']);
  1661. $rule['message'] = strtr($rule['message'], $js_escape);
  1662. if (isset($rule['group'])) {
  1663. $group =& $this->getElement($rule['group']);
  1664. // No JavaScript validation for frozen elements
  1665. if ($group->isFrozen()) {
  1666. continue 2;
  1667. }
  1668. $elements =& $group->getElements();
  1669. foreach (array_keys($elements) as $key) {
  1670. if ($elementName == $group->getElementName($key)) {
  1671. $element =& $elements[$key];
  1672. break;
  1673. }
  1674. }
  1675. } elseif ($dependent) {
  1676. $element = array();
  1677. $element[] =& $this->getElement($elementName);
  1678. foreach ($rule['dependent'] as $elName) {
  1679. $element[] =& $this->getElement($elName);
  1680. }
  1681. } else {
  1682. $element =& $this->getElement($elementName);
  1683. }
  1684. // No JavaScript validation for frozen elements
  1685. if (is_object($element) && $element->isFrozen()) {
  1686. continue 2;
  1687. } elseif (is_array($element)) {
  1688. foreach (array_keys($element) as $key) {
  1689. if ($element[$key]->isFrozen()) {
  1690. continue 3;
  1691. }
  1692. }
  1693. }
  1694. $test[] = $registry->getValidationScript($element, $elementName, $rule);
  1695. }
  1696. }
  1697. }
  1698. if (count($test) > 0) {
  1699. return
  1700. "\n<script type=\"text/javascript\">\n" .
  1701. "//<![CDATA[\n" .
  1702. "function validate_" . $this->_attributes['id'] . "(frm) {\n" .
  1703. " var value = '';\n" .
  1704. " var errFlag = new Array();\n" .
  1705. " var _qfGroups = {};\n" .
  1706. " _qfMsg = '';\n\n" .
  1707. join("\n", $test) .
  1708. "\n if (_qfMsg != '') {\n" .
  1709. " _qfMsg = '" . strtr($this->_jsPrefix, $js_escape) . "' + _qfMsg;\n" .
  1710. " _qfMsg = _qfMsg + '\\n" . strtr($this->_jsPostfix, $js_escape) . "';\n" .
  1711. " alert(_qfMsg);\n" .
  1712. " return false;\n" .
  1713. " }\n" .
  1714. " return true;\n" .
  1715. "}\n" .
  1716. "//]]>\n" .
  1717. "</script>";
  1718. }
  1719. return '';
  1720. } // end func getValidationScript
  1721. // }}}
  1722. // {{{ getSubmitValues()
  1723. /**
  1724. * Returns the values submitted by the form
  1725. *
  1726. * @since 2.0
  1727. * @access public
  1728. * @param bool Whether uploaded files should be returned too
  1729. * @return array
  1730. */
  1731. function getSubmitValues($mergeFiles = false)
  1732. {
  1733. return $mergeFiles? HTML_QuickForm::arrayMerge($this->_submitValues, $this->_submitFiles): $this->_submitValues;
  1734. } // end func getSubmitValues
  1735. // }}}
  1736. // {{{ toArray()
  1737. /**
  1738. * Returns the form's contents in an array.
  1739. *
  1740. * The description of the array structure is in HTML_QuickForm_Renderer_Array docs
  1741. *
  1742. * @since 2.0
  1743. * @access public
  1744. * @param bool Whether to collect hidden elements (passed to the Renderer's constructor)
  1745. * @return array of form contents
  1746. */
  1747. function toArray($collectHidden = false)
  1748. {
  1749. include_once 'HTML/QuickForm/Renderer/Array.php';
  1750. // Modified by Ivan Tcholakov, 16-MAR-2010. Suppressing a deprecation warning on PHP 5.3
  1751. //$renderer =& new HTML_QuickForm_Renderer_Array($collectHidden);
  1752. $renderer = new HTML_QuickForm_Renderer_Array($collectHidden);
  1753. //
  1754. $this->accept($renderer);
  1755. return $renderer->toArray();
  1756. } // end func toArray
  1757. // }}}
  1758. // {{{ exportValue()
  1759. /**
  1760. * Returns a 'safe' element's value
  1761. *
  1762. * This method first tries to find a cleaned-up submitted value,
  1763. * it will return a value set by setValue()/setDefaults()/setConstants()
  1764. * if submitted value does not exist for the given element.
  1765. *
  1766. * @param string Name of an element
  1767. * @access public
  1768. * @return mixed
  1769. * @throws HTML_QuickForm_Error
  1770. */
  1771. function exportValue($element)
  1772. {
  1773. if (!isset($this->_elementIndex[$element])) {
  1774. return PEAR::raiseError(null, QUICKFORM_NONEXIST_ELEMENT, null, E_USER_WARNING, "Element '$element' does not exist in HTML_QuickForm::getElementValue()", 'HTML_QuickForm_Error', true);
  1775. }
  1776. $value = $this->_elements[$this->_elementIndex[$element]]->exportValue($this->_submitValues, false);
  1777. if (isset($this->_duplicateIndex[$element])) {
  1778. foreach ($this->_duplicateIndex[$element] as $index) {
  1779. if (null !== ($v = $this->_elements[$index]->exportValue($this->_submitValues, false))) {
  1780. if (is_array($value)) {
  1781. $value[] = $v;
  1782. } else {
  1783. $value = (null === $value)? $v: array($value, $v);
  1784. }
  1785. }
  1786. }
  1787. }
  1788. return $value;
  1789. }
  1790. // }}}
  1791. // {{{ exportValues()
  1792. /**
  1793. * Returns 'safe' elements' values
  1794. *
  1795. * Unlike getSubmitValues(), this will return only the values
  1796. * corresponding to the elements present in the form.
  1797. *
  1798. * @param mixed Array/string of element names, whose values we want. If not set then return all elements.
  1799. * @access public
  1800. * @return array An assoc array of elements' values
  1801. * @throws HTML_QuickForm_Error
  1802. */
  1803. function exportValues($elementList = null)
  1804. {
  1805. $values = array();
  1806. if (null === $elementList) {
  1807. // iterate over all elements, calling their exportValue() methods
  1808. foreach (array_keys($this->_elements) as $key) {
  1809. $value = $this->_elements[$key]->exportValue($this->_submitValues, true);
  1810. if (is_array($value)) {
  1811. // This shit throws a bogus warning in PHP 4.3.x
  1812. $values = HTML_QuickForm::arrayMerge($values, $value);
  1813. }
  1814. }
  1815. } else {
  1816. if (!is_array($elementList)) {
  1817. $elementList = array_map('trim', explode(',', $elementList));
  1818. }
  1819. foreach ($elementList as $elementName) {
  1820. $value = $this->exportValue($elementName);
  1821. if (PEAR::isError($value)) {
  1822. return $value;
  1823. }
  1824. $values[$elementName] = $value;
  1825. }
  1826. }
  1827. return $values;
  1828. }
  1829. // }}}
  1830. // {{{ isSubmitted()
  1831. /**
  1832. * Tells whether the form was already submitted
  1833. *
  1834. * This is useful since the _submitFiles and _submitValues arrays
  1835. * may be completely empty after the trackSubmit value is removed.
  1836. *
  1837. * @access public
  1838. * @return bool
  1839. */
  1840. function isSubmitted()
  1841. {
  1842. return $this->_flagSubmitted;
  1843. }
  1844. // }}}
  1845. // {{{ isError()
  1846. /**
  1847. * Tell whether a result from a QuickForm method is an error (an instance of HTML_QuickForm_Error)
  1848. *
  1849. * @access public
  1850. * @param mixed result code
  1851. * @return bool whether $value is an error
  1852. * @static
  1853. */
  1854. function isError($value)
  1855. {
  1856. return (is_object($value) && is_a($value, 'html_quickform_error'));
  1857. } // end func isError
  1858. // }}}
  1859. // {{{ errorMessage()
  1860. /**
  1861. * Return a textual error message for an QuickForm error code
  1862. *
  1863. * @access public
  1864. * @param int error code
  1865. * @return string error message
  1866. * @static
  1867. */
  1868. function errorMessage($value)
  1869. {
  1870. // make the variable static so that it only has to do the defining on the first call
  1871. static $errorMessages;
  1872. // define the varies error messages
  1873. if (!isset($errorMessages)) {
  1874. $errorMessages = array(
  1875. QUICKFORM_OK => 'no error',
  1876. QUICKFORM_ERROR => 'unknown error',
  1877. QUICKFORM_INVALID_RULE => 'the rule does not exist as a registered rule',
  1878. QUICKFORM_NONEXIST_ELEMENT => 'nonexistent html element',
  1879. QUICKFORM_INVALID_FILTER => 'invalid filter',
  1880. QUICKFORM_UNREGISTERED_ELEMENT => 'unregistered element',
  1881. QUICKFORM_INVALID_ELEMENT_NAME => 'element already exists',
  1882. QUICKFORM_INVALID_PROCESS => 'process callback does not exist',
  1883. QUICKFORM_DEPRECATED => 'method is deprecated',
  1884. QUICKFORM_INVALID_DATASOURCE => 'datasource is not an object'
  1885. );
  1886. }
  1887. // If this is an error object, then grab the corresponding error code
  1888. if (HTML_QuickForm::isError($value)) {
  1889. $value = $value->getCode();
  1890. }
  1891. // return the textual error message corresponding to the code
  1892. return isset($errorMessages[$value]) ? $errorMessages[$value] : $errorMessages[QUICKFORM_ERROR];
  1893. } // end func errorMessage
  1894. /**
  1895. * @param HTML_QuickForm_element $element
  1896. */
  1897. public function setRequired(HTML_QuickForm_element $element)
  1898. {
  1899. $this->addRule(
  1900. $element->getName(),
  1901. get_lang('ThisFieldIsRequired'),
  1902. 'required'
  1903. );
  1904. }
  1905. }
  1906. /**
  1907. * Class for errors thrown by HTML_QuickForm package
  1908. *
  1909. * @category HTML
  1910. * @package HTML_QuickForm
  1911. * @author Adam Daniel <adaniel1@eesus.jnj.com>
  1912. * @author Bertrand Mansion <bmansion@mamasam.com>
  1913. * @version Release: 3.2.11
  1914. */
  1915. class HTML_QuickForm_Error extends PEAR_Error {
  1916. // {{{ properties
  1917. /**
  1918. * Prefix for all error messages
  1919. * @var string
  1920. */
  1921. var $error_message_prefix = 'QuickForm Error: ';
  1922. // }}}
  1923. // {{{ constructor
  1924. /**
  1925. * Creates a quickform error object, extending the PEAR_Error class
  1926. *
  1927. * @param int $code the error code
  1928. * @param int $mode the reaction to the error, either return, die or trigger/callback
  1929. * @param int $level intensity of the error (PHP error code)
  1930. * @param mixed $debuginfo any information that can inform user as to nature of the error
  1931. */
  1932. function HTML_QuickForm_Error($code = QUICKFORM_ERROR, $mode = PEAR_ERROR_RETURN,
  1933. $level = E_USER_NOTICE, $debuginfo = null)
  1934. {
  1935. if (is_int($code)) {
  1936. $this->PEAR_Error(HTML_QuickForm::errorMessage($code), $code, $mode, $level, $debuginfo);
  1937. } else {
  1938. $this->PEAR_Error("Invalid error code: $code", QUICKFORM_ERROR, $mode, $level, $debuginfo);
  1939. }
  1940. }
  1941. }