QuickForm.php 79 KB

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