Source for file form-defs.php

Documentation is available at form-defs.php

  1. <?php
  2. /* ******************************************************************** */
  3. /* CATALYST PHP Source Code */
  4. /* -------------------------------------------------------------------- */
  5. /* This program is free software; you can redistribute it and/or modify */
  6. /* it under the terms of the GNU General Public License as published by */
  7. /* the Free Software Foundation; either version 2 of the License, or */
  8. /* (at your option) any later version. */
  9. /* */
  10. /* This program is distributed in the hope that it will be useful, */
  11. /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
  12. /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
  13. /* GNU General Public License for more details. */
  14. /* */
  15. /* You should have received a copy of the GNU General Public License */
  16. /* along with this program; if not, write to: */
  17. /* The Free Software Foundation, Inc., 59 Temple Place, Suite 330, */
  18. /* Boston, MA 02111-1307 USA */
  19. /* -------------------------------------------------------------------- */
  20. /* */
  21. /* Filename: form-defs.php */
  22. /* Author: Paul Waite */
  23. /* Description: Definitions for data visualization elements by way */
  24. /* of HTML forms. */
  25. /* */
  26. /* ******************************************************************** */
  27. /** @package form */
  28. include_once("data-defs.php");
  29. /** Include date and time functions */
  30. ("datetime-defs.php");
  31. /** Include button creation classes */
  32. ("button-defs.php");
  33. /** The HTML object classes are required for forms */
  34. ("html-defs.php");
  35.  
  36. // ----------------------------------------------------------------------
  37.  
  38. /** Standard width for a textual form element */
  39. ("STD_WIDTH", 16);
  40.  
  41. // Field attributes. We generally implement a display-only
  42. // field as a hidden field on the HTML form, with plain
  43. // text showing the value. This way we get around some
  44. // deficiencies in some browsers which don't implement the
  45. // READONLY attribute properly!
  46.  
  47.  
  48.  
  49. /** Form field is readonly/display only */
  50. ("DISPLAY_ONLY", false);
  51. /** Form element is editable (ie. not readonly) */
  52. ("EDITABLE", true);
  53.  
  54. // Various form display element types. These define
  55. // how the data looks on the HTML form.
  56.  
  57.  
  58.  
  59. /** General text field */
  60. ("F_TEXT", 1 );
  61. /** Text field containing image URL */
  62. ("F_IMAGE", 2 );
  63. /** Date/time field */
  64. ("F_DATETIME", 3 );
  65. /** Dropdown menu or listbox field */
  66. ("F_COMBO", 4 );
  67. /** Textarea memo field */
  68. ("F_MEMO", 5 );
  69. /** Tickbox or checkbox */
  70. ("F_CHECKBOX", 6 );
  71. /** Radio button */
  72. ("F_RADIO", 7 );
  73.  
  74. // Case forcing options for text fields
  75. /** Do not force case */
  76. ("NONE", 0 );
  77. /** Force text to uppercase */
  78. ("UPPER", 1 );
  79. /** Force text to lowercase */
  80. ("LOWER", 2 );
  81.  
  82. // Flag for Multi-select in combo fields..
  83. /** Combo-field is multiple-select */
  84. ("MULTISELECT", true);
  85. /** Combo-field is single-select */
  86. ("SINGLESELECT", false);
  87.  
  88. // ----------------------------------------------------------------------
  89. /**
  90. * Form class
  91. * A container object for form elements. This is for when we have a
  92. * classical form which is just a set of form elements for filling
  93. * in, rather than a mixture of page layout and elements. It is
  94. * rendered in a vanilla table with each field stacked vertically
  95. * to the right of its label..
  96. * @package form
  97. */
  98. class form extends HTMLObject {
  99. // Public
  100. /** Action attribute */
  101.  
  102. var $action = "";
  103. /** Method attribute */
  104.  
  105. var $method = "post";
  106. /** Encoding type */
  107.  
  108. var $enctype = "";
  109. /** Accept-charset attributes - comma-delimited list */
  110.  
  111. var $accept_charsets = "";
  112. /** Script to call on form submit */
  113.  
  114. var $onsubmit;
  115. /** Whether to render labels for fields */
  116.  
  117. var $showlabels = true;
  118. /** Justification mode for labels "right" or "left" */
  119.  
  120. var $labeljustify = "right";
  121. /** Whether to append colon ":" to labels */
  122.  
  123. var $labelcolon = true;
  124. /** Optional class/style for labels */
  125.  
  126. var $labelcss;
  127. /** Width taken by element (RHS) side of the form in % */
  128.  
  129. var $elewidth_pct = 65;
  130. /** Whether to force forma elements to be read-only */
  131.  
  132. var $force_readonly = false;
  133.  
  134. // Private
  135. /** Type of form 'normal' or 'subform' */
  136.  
  137. var $type = "normal";
  138. /** Elements array. Contains form elements
  139. @access private */
  140. var $elements;
  141. // ....................................................................
  142. /**
  143. * Constructor
  144. * Create a form object. Sets basic form attributes.
  145. * @param string $name The name of the form
  146. * @param string $title The title/banner for the form
  147. * @param string $action Action attribute, ie. the script to POST to
  148. * @param string $method Method attribute, POST or GET
  149. * @param string $enctype Encoding type
  150. */
  151. function form($name="", $title="", $action="", $method="post", $enctype="", $css="", $charsets="") {
  152. global $SCRIPT_NAME;
  153. if ($action == "") {
  154. $action = $SCRIPT_NAME;
  155. }
  156. $this->setname($name);
  157. $this->settitle($title);
  158. $this->action = $action;
  159. $this->method = $method;
  160. $this->enctype = $enctype;
  161. // A form can be of two basic types:
  162. // 'normal' : we render between <form></form> tags
  163. // 'subform' : we omit these tags; part of another form
  164. $this->type = "normal";
  165. $this->setcss($css);
  166. $this->accept_charsets = $charsets;
  167. } // form
  168. // ....................................................................
  169. /**
  170. * Set the proportion of the form taken up by the form field elements
  171. * as opposed to the labels. This is specified as an integer which
  172. * is the proportion as a percentage. Eg: 70 would be '70%'.
  173. * @param integer $pctwidth Percentage width of form for form fields
  174. */
  175. function set_fieldwidth_pct($pctwidth=65) {
  176. $this->elewidth_pct = $pctwidth;
  177. } // set_fieldwidth_pct
  178. // ....................................................................
  179. /** Force all of the contained fields to be rendered read-only */
  180.  
  181. function force_readonly($mode=true) {
  182. $this->force_readonly = $mode;
  183. } // force_readonly
  184. // ....................................................................
  185. /**
  186. * Adds a form element object to the form. This is usually an object
  187. * you have previously created eg. with new form_textfield(...) etc.
  188. * @param object $element The form element object to add
  189. */
  190. function add($element) {
  191. $this->elements[] = $element;
  192. } // add
  193. // ....................................................................
  194. /**
  195. * Add form separator row
  196. * Adds a separator row to the form
  197. * If no heading is given then this produces a ruled line across
  198. * the form. If a heading is given then this text is put in a row
  199. * just below the ruled line. Feel free to add in HTML formatting
  200. * using normal HTML tags like <b></b> etc.
  201. * @param string $heading Optional separator title/heading text
  202. * @param string $css Style/class to apply (not rendered at present)
  203. */
  204. function add_separator($heading="", $css="") {
  205. $f = new form_labelfield($heading, "", $css);
  206. $f->type = "separator";
  207. $this->add($f);
  208. } // add_separator
  209. // ....................................................................
  210. /**
  211. * Add text to the form
  212. * Adds text content to the form spanning both columns. The text is
  213. * added literally, so you can add in effects like <em>..</em> etc.
  214. * @param string $text Text to display in the form
  215. * @param string $css Style/class to apply (NB: not rendered at present)
  216. */
  217. function add_text($text, $css="") {
  218. $f = new form_labelfield($text, "", $css);
  219. $f->type = "textcontent";
  220. $this->add($f);
  221. } // add_text
  222. // ....................................................................
  223. /**
  224. * Add annotation to the form
  225. * Adds text content to the form in the field column. The text is
  226. * added literally, so you can add in effects like <em>..</em> etc.
  227. * @param string $text Text to display in the form
  228. * @param string $css Style/class to apply (NB: not rendered at present)
  229. */
  230. function add_annotation($text, $css="") {
  231. $f = new form_labelfield($text, "", $css);
  232. $f->type = "annotation";
  233. $this->add($f);
  234. } // add_annotation
  235. // ....................................................................
  236. /**
  237. * Add file upload fields
  238. * Special function to add repeated fileupload fields to the form.
  239. * These share the same naming, but with square brackets "[]" appended.
  240. * @param object $element The form element object to add
  241. * @param integer $count The number of elements to add
  242. * @param boolean $labels Whether we have individual field labels
  243. */
  244. function add_fileuploadfields($element, $count=1, $labels=false) {
  245. // If multiple file upload fields, then make sure that we
  246. // have the [] postfixed onto the name first..
  247. if ($count > 1) {
  248. $name = $element->name;
  249. $len = strlen($name);
  250. if ($len <= 2) {
  251. $name .= "[]";
  252. }
  253. else {
  254. $last2chars = substr($name, $len - 2, 2);
  255. if ($last2chars != "[]") {
  256. $name .= "[]";
  257. }
  258. }
  259. $element->name = $name;
  260. }
  261. // First element gets the hidden field, and all the
  262. // rest therefore must have it suppressed..
  263. $element->include_maxsize = true;
  264. for ($ix=0; $ix < $count; $ix++) {
  265. if ($ix == 1) {
  266. $element->include_maxsize = false;
  267. }
  268. if ($ix > 0) {
  269. if (isset($labels[$ix])) {
  270. $element->label = $labels[$ix];
  271. }
  272. else $element->label = "";
  273. }
  274. $this->add($element);
  275. }
  276. } // add_fileuploadfields
  277. // ....................................................................
  278. /**
  279. * Add a button
  280. * Adds a button element to the form.
  281. * @param object $button The button element object to add
  282. */
  283. function add_button($button) {
  284. $this->add($button);
  285. } // add_button
  286. // ....................................................................
  287. /**
  288. * Set onsubmit script
  289. * Defines the onsubmit script to call when the user
  290. * submits the form.
  291. * @param string $script The script to execute on form submit
  292. */
  293. function set_onsubmit($script) {
  294. $this->onsubmit = $script;
  295. } // set_onsubmit
  296. // ....................................................................
  297. /**
  298. * Set the form type
  299. * This can be 'normal' or 'subform'. Use the subform variant when you
  300. * already have <form> tags being provided by other means. Then when
  301. * you render the subform only the fields will be returned.
  302. * @param string $type The form type 'normal' or 'subform'
  303. */
  304. function set_type($type="normal") {
  305. $this->type = $type;
  306. } // set_type
  307. // ....................................................................
  308. /**
  309. * Check if form contains file upload field
  310. * Check all elements for presence of file upload field. This is
  311. * mainly for internal use.
  312. * @return boolean True if form contains a file upload field
  313. */
  314. function contains_fileuploadfield() {
  315. $res = false;
  316. if (isset($this->elements)) {
  317. foreach ($this->elements as $element) {
  318. if ($element->type == "file") {
  319. $res = true;
  320. break;
  321. }
  322. }
  323. }
  324. return $res;
  325. } // contains_fileuploadfield
  326. // ....................................................................
  327. /**
  328. * This renders the form as HTML, including the form tags and every
  329. * form element in the form.
  330. * @return string The form as HTML.
  331. */
  332. function html() {
  333. $s = "";
  334. $formtitle = $this->title;
  335. $this->title = strip_tags($this->title);
  336. if (isset($this->elements)) {
  337. switch ($this->type) {
  338. case "normal":
  339. // If we have a file upload field, then set the encoding,
  340. // unless it has already been manually set..
  341. if ($this->enctype == "" && $this->contains_fileuploadfield()) {
  342. $this->enctype = "multipart/form-data";
  343. }
  344. $s .= "<form";
  345. $s .= $this->attributes();
  346. $s .= " action=\"" . $this->action . "\"";
  347. $s .= " method=\"" . $this->method . "\"";
  348. if ($this->enctype != "") {
  349. $s .= " enctype=\"" . $this->enctype . "\"";
  350. }
  351. if ($this->accept_charsets != "") {
  352. $s .= " accept-charset=\"" . $this->accept_charsets . "\"";
  353. }
  354. if (isset($this->onsubmit)) {
  355. $s .= " onsubmit=\"" . $this->onsubmit . "\"";
  356. }
  357. $s .= ">\n";
  358. break;
  359. case "subform":
  360. break;
  361. } // switch
  362.  
  363. $fmtable = new table($this->name);
  364. $fmtable->setpadding(3);
  365. $fmtable->inherit_attributes($this);
  366.  
  367. // Optional form title area..
  368. if ($formtitle != "") {
  369. $fmtable->thead();
  370. $fmtable->tr();
  371. $fmtable->td($formtitle);
  372. if ($this->showlabels) {
  373. $fmtable->td_colspan(2);
  374. }
  375. }
  376.  
  377. // Render any elements..
  378. $fmtable->tbody();
  379. $rowcnt = 0;
  380. foreach ($this->elements as $element) {
  381. if (isset($element->type)) {
  382. switch ($element->type) {
  383. // Subform of this form..
  384. case "subform":
  385. $element->inherit_attributes($this);
  386. $element->labeljustify = $this->labeljustify;
  387. $element->labelcolon = $this->labelcolon;
  388. $element->force_readonly = $this->force_readonly;
  389. $fmtable->tr();
  390. $rowcnt += 1;
  391. $fmtable->td($element->render());
  392. $fmtable->td_alignment("", "top");
  393. if ($this->showlabels) {
  394. $fmtable->td_colspan(2);
  395. }
  396. break;
  397. // Table separator line..
  398. case "separator":
  399. $fmtable->tr();
  400. $rowcnt += 1;
  401. $fmtable->td("<hr>");
  402. if ($this->showlabels) {
  403. $fmtable->td_colspan(2);
  404. }
  405. if ($element->label != "") {
  406. $fmtable->tr();
  407. $rowcnt += 1;
  408. $fmtable->td("<b>" . $element->label . "</b>");
  409. if (isset($this->labelcss)) {
  410. $fmtable->td_css($this->labelcss);
  411. }
  412. if ($this->showlabels) {
  413. $fmtable->td_colspan(2);
  414. }
  415. }
  416. break;
  417. // Text done as a labelfield..
  418. case "textcontent":
  419. $fmtable->tr();
  420. $rowcnt += 1;
  421. $fmtable->td($element->label);
  422. if (isset($this->labelcss)) {
  423. $fmtable->td_css($this->labelcss);
  424. }
  425. if ($this->showlabels) {
  426. $fmtable->td_colspan(2);
  427. }
  428. break;
  429. // Annotation done as a labelfield..
  430. case "annotation":
  431. $fmtable->tr();
  432. $rowcnt += 1;
  433. $fmtable->td();
  434. $fmtable->td($element->label);
  435. if (isset($this->labelcss)) {
  436. $fmtable->td_css($this->labelcss);
  437. }
  438. break;
  439. // Defer hidden fields 'til later
  440. case "hidden":
  441. $hiddenfields[] = $element;
  442. break;
  443. // Defer image buttons 'til later
  444. case "image":
  445. $buttons[] = $element;
  446. break;
  447. // Standard form field - label: field
  448. default:
  449. if ($this->force_readonly) {
  450. $element->set_displayonly();
  451. }
  452. if ($element->label != "") {
  453. $label = $element->label;
  454. if ($this->labelcolon) {
  455. $label .= ":";
  456. }
  457. }
  458. else $label = "";
  459. $fmtable->tr();
  460. $rowcnt += 1;
  461. if ($this->showlabels) {
  462. $fmtable->td($label);
  463. switch ($element->type) {
  464. case "textarea":
  465. $fmtable->td_alignment($this->labeljustify, "top");
  466. break;
  467. case "select":
  468. if ($element->multiselect) {
  469. $fmtable->td_alignment($this->labeljustify, "top");
  470. }
  471. else {
  472. $fmtable->td_alignment($this->labeljustify);
  473. }
  474. break;
  475. default:
  476. $fmtable->td_alignment($this->labeljustify);
  477. } // switch
  478. if (isset($this->labelcss)) {
  479. $fmtable->td_css($this->labelcss);
  480. }
  481. }
  482. $fmtable->td($element->render());
  483. if (($element->type == "label" || $element->type == "displayonly")
  484. && isset($this->labelcss)) {
  485. $fmtable->td_css($this->labelcss);
  486. }
  487. $fmtable->td_alignment("", "top");
  488. } // switch
  489. }
  490. else {
  491. // Save buttons 'til later..
  492. if (is_subclass_of($element, "button_object")) {
  493. $buttons[] = $element;
  494. }
  495. // Otherwise render it in the right-hand cell..
  496. else {
  497. $fmtable->tr();
  498. $rowcnt += 1;
  499. if ($this->showlabels) {
  500. $fmtable->td("&nbsp;");
  501. }
  502. $fmtable->td($element->render());
  503. $fmtable->td_alignment("", "top");
  504. }
  505. }
  506. } // foreach
  507.  
  508. // Render any buttons in one cell..
  509. if (isset($buttons)) {
  510. $allbtns = "";
  511. foreach ($buttons as $button) {
  512. $allbtns .= $button->render() . "&nbsp;";
  513. }
  514. $fmtable->tr();
  515. $rowcnt += 1;
  516. if ($this->showlabels) {
  517. $fmtable->td("&nbsp;");
  518. }
  519. $fmtable->td($allbtns);
  520. }
  521.  
  522. // Render the form table content..
  523. if ($rowcnt == 0) {
  524. $fmtable->tr();
  525. $fmtable->td("");
  526. }
  527.  
  528. // Apply width profile..
  529. $fmtable->set_width_profile((100 - $this->elewidth_pct) . "%," . $this->elewidth_pct . "%");
  530.  
  531. // Render it as HTML..
  532. $fmtable->inherit_attributes($this);
  533. $s .= $fmtable->html();
  534.  
  535. // Render any hidden fields..
  536. if (isset($hiddenfields)) {
  537. foreach ($hiddenfields as $hidden) {
  538. $s .= $hidden->render();
  539. }
  540. }
  541. // End the form if required..
  542. switch ($this->type) {
  543. case "normal":
  544. $s .= "</form>\n";
  545. break;
  546. case "subform":
  547. break;
  548. } // switch
  549. }
  550. // Return the finished form..
  551. return $s;
  552. } // html
  553.  
  554. } // form class
  555. // ----------------------------------------------------------------------
  556.  
  557. /**
  558. * SubForm class
  559. * The sub-form is a special case of the form class. The difference is
  560. * that it is expected to be part of an existing form, and therefore
  561. * is rendered without the form tags.
  562. * @package form
  563. */
  564. class subform extends form {
  565. // ....................................................................
  566. /**
  567. * Create a subform object. Sets basic form attributes.
  568. * @param string $name The name of the form
  569. * @param string $title The title/banner for the form
  570. * @param string $action Action attribute, ie. the script to POST to
  571. * @param string $method Method attribute, POST or GET
  572. * @param string $enctype Encoding type
  573. */
  574. function subform($name="", $title="", $action="", $method="post", $enctype="") {
  575. $this->form($name, $title, $action, $method, $enctype);
  576. $this->type = "subform";
  577. }
  578. } // subform class
  579. // ----------------------------------------------------------------------
  580.  
  581. /**
  582. * multipart_form class
  583. * The multipart_form is just a standard form, but with the enctype
  584. * pre-set to the "multipart/form-data" setting required for file
  585. * multiparts of binary form data.
  586. * @package form
  587. */
  588. class multipart_form extends form {
  589. /**
  590. * Create an multipart_form object. Sets basic form attributes.
  591. * @param string $name The name of the form
  592. * @param string $title The title/banner for the form
  593. * @param string $action Action attribute, ie. the script to POST to
  594. * @param string $method Method attribute, POST or GET
  595. * @param string $enctype Encoding type
  596. */
  597. function multipart_form($name="", $title="", $action="", $method="post") {
  598. $this->form($name, $title, $action, $method, "multipart/form-data");
  599. }
  600. } // multipart_form class
  601. // ----------------------------------------------------------------------
  602.  
  603. /**
  604. * Form Field class
  605. * Abstract field class, for deriving various form field types.
  606. * This class holds the basic properties and methods which are
  607. * inherited by all form fields.
  608. * @package form
  609. */
  610. class form_field extends HTMLObject {
  611. /** Type of field eg: text, hidden, checkbox etc. */
  612.  
  613. var $type = "";
  614. /** Value of the field */
  615.  
  616. var $value;
  617. /** Label to put against the field. NB: this is not displayed unless you make it so. */
  618.  
  619. var $label;
  620. /** True if field is NOT readonly */
  621.  
  622. var $editable = EDITABLE;
  623. /** True if field is disabled */
  624.  
  625. var $disabled = false;
  626. /** If false, disable autocomplete on this field. Mainly used with
  627. password fields to stop browsers saving passwords insecurely. */
  628. var $autocomplete = true;
  629. // ....................................................................
  630. /**
  631. * Constructor
  632. * Create a field object. Sets basic field attributes.
  633. * @param string $name The name of the field
  634. * @param string $label The label which can be displayed alongside the field
  635. * @param string $value The value of the field
  636. * @param boolean $editable Editability: EDITABLE or DISPLAY_ONLY (true or false)
  637. * @param string $style Style to apply to the field
  638. */
  639. function form_field($name="", $label="", $value="", $editable=EDITABLE, $css="") {
  640. $this->setname($name);
  641. $this->label = $label;
  642. $this->value = $value;
  643. $this->editable = $editable;
  644. $this->setcss($css);
  645. }
  646. /** Set the field to be display-only (non-editable) */
  647.  
  648. function set_displayonly() {
  649. $this->editable = false;
  650. }
  651. /** Set the label associated with the field
  652. * @param string $label The label to set for this field
  653. */
  654. function setlabel($label) {
  655. $this->label = $label;
  656. }
  657. /** Set the value of the field
  658. * @param mixed $value The value to set for this field
  659. */
  660. function setvalue($value) {
  661. if (!is_array($value)) $value = trim($value);
  662. $this->value = $value;
  663. }
  664. /**
  665. * Returns current element as a hidden field.
  666. * @return string HTML for hidden field version of current field.
  667. */
  668. function as_hiddenfield() {
  669. $hid = new form_hiddenfield($this->name, $this->value);
  670. return $hid->render();
  671. }
  672. /**
  673. * Returns current element as text, with associated hidden field.
  674. * @return string HTML for hidden field version of current field.
  675. */
  676. function as_displayonly() {
  677. $s = $this->as_hiddenfield();
  678. if ( isset($this->value) ) $s .= "<strong>$this->value</strong>";
  679. return $s;
  680. }
  681. /**
  682. * Disable the autocomplete form functionality of the browser for
  683. * this field. Browsers have, mainly for password fields, been offering
  684. * auto-completion of contents. This function will stop this from being
  685. * done, in browsers which obey the "autocomplete='off'" attribute.
  686. * By default autocomplete is "true" for every field. The attribute is
  687. * only ever rendered when autocomplete is "false".
  688. */
  689. function disable_autocomplete() {
  690. $this->autocomplete = false;
  691. }
  692. /**
  693. * Render common field properties
  694. * This method renders the common field properties. Used in the
  695. * descendant classes to generate the property tags.
  696. * $return string Common attribute property tags for current field
  697. * @access private
  698. */
  699. function field_attributes() {
  700. global $RESPONSE;
  701. $html = "";
  702. // Add in standard HTML attributes..
  703. $html .= $this->attributes();
  704. // Add in our field-specific attributes..
  705. if ($this->disabled) $html .= " disabled";
  706. // Readonly behaviour depends on type..
  707. if (!$this->editable) {
  708. switch ($this->type) {
  709. // These elements support the readonly property..
  710. case "text":
  711. case "password":
  712. case "textarea":
  713. $html .= " readonly";
  714. break;
  715. case "hidden":
  716. break;
  717. // All the rest have to be disabled. They also
  718. // render hidden fields, to allow value submit.
  719. // See each relevant element class.
  720. default:
  721. if (!$this->disabled) {
  722. $html .= " disabled";
  723. }
  724. } // switch
  725. }
  726. // Disable autocomplete if flagged..
  727. if (!$this->autocomplete) {
  728. $html .= " autocomplete=\"off\"";
  729. }
  730. return $html;
  731. }
  732. } // form_field class
  733. // ----------------------------------------------------------------------
  734.  
  735. /**
  736. * Label Field class
  737. * This is just to display a bare value where the form element would
  738. * normally be, and where we dont want to use a form_textfield in
  739. * non-editable mode because we don't want the hidden field..
  740. * @package form
  741. */
  742. class form_labelfield extends form_field {
  743. /**
  744. * Constructor
  745. * Create a label field object. Sets basic field attributes.
  746. * @param string $label The label which can be displayed alongside the field
  747. * @param string $value The value of the field
  748. * @param string $css Optional style of class to apply
  749. */
  750. function form_labelfield($label="", $value="", $css="") {
  751. $this->form_field("nullname", $label, $value);
  752. $this->setcss($css);
  753. $this->type = "label";
  754. }
  755. // ....................................................................
  756. /**
  757. * This renders the field as HTML.
  758. * @see render()
  759. * @return string The field as HTML.
  760. */
  761. function html($name="") {
  762. if ($name != "") $this->name = $name;
  763. return $this->value;
  764. }
  765. } // form_labelfield class
  766. // ----------------------------------------------------------------------
  767.  
  768. /**
  769. * Hidden Field class
  770. * This class generates a hidden field.
  771. * @package form
  772. */
  773. class form_hiddenfield extends form_field {
  774. /**
  775. * Constructor
  776. * Create a hidden field object. Sets basic field attributes.
  777. * @param string $name The name of the field
  778. * @param string $value The value of the field
  779. */
  780. function form_hiddenfield($name="", $value="") {
  781. $this->form_field($name, "", $value);
  782. $this->type = "hidden";
  783. }
  784. // ....................................................................
  785. /**
  786. * This renders the field as HTML.
  787. * @see render()
  788. * @return string The field as HTML.
  789. */
  790. function html($name="") {
  791. if ($name != "") $this->name = $name;
  792. $html = "<input";
  793. $html .= " type=\"$this->type\"";
  794. $html .= $this->field_attributes();
  795. if (isset($this->value)) {
  796. $html .= " value=" . quoted_valuestring($this->value);
  797. }
  798. $html .= ">";
  799. return $html;
  800. }
  801. } // form_hiddenfield class
  802. // ----------------------------------------------------------------------
  803.  
  804. /**
  805. * Button Field class
  806. * This virtual class generates a standard form button field. This is
  807. * a virtual class, used to provide a basis for real buttons. Do not
  808. * instantiate this as an object - use the descendants instead.
  809. * @package form
  810. */
  811. class form_buttonfield extends form_field {
  812. // ....................................................................
  813. /**
  814. * Constructor
  815. * Create a field object. Sets basic field attributes.
  816. * @param string $name The name of the button
  817. * @param string $value The value of the button
  818. * @param string $css CSS class or style to apply to the button
  819. */
  820. function form_buttonfield($name="", $value="", $css="") {
  821. $this->form_field($name, "", $value);
  822. $this->setcss($css);
  823. }
  824. // ....................................................................
  825. /**
  826. * Renders the form button as HTML.
  827. * @see render()
  828. * @return string HTML rendering of button
  829. */
  830. function html($name="") {
  831. if ($name != "") $this->name = $name;
  832. $html = "<input";
  833. $html .= " type=\"$this->type\"";
  834. $html .= $this->field_attributes();
  835. if (isset($this->value)) $html .= " value=\"$this->value\"";
  836. $html .= ">";
  837. return $html;
  838. }
  839. } // form_buttonfield class
  840. // ----------------------------------------------------------------------
  841.  
  842. /**
  843. * Submit Button class
  844. * This class generates a standard form submit button field.
  845. * @package form
  846. */
  847. class form_submitbutton extends form_buttonfield {
  848. // ....................................................................
  849. /**
  850. * Constructor
  851. * Create a field object. Sets basic field attributes.
  852. * @param string $name The name of the button
  853. * @param string $value The value of the button
  854. * @param string $css CSS class or style to apply to the button
  855. */
  856. function form_submitbutton($name="", $value="", $css="") {
  857. $this->form_buttonfield($name, $value, $css);
  858. $this->type = "submit";
  859. }
  860. } // form_submitbutton class
  861. // ----------------------------------------------------------------------
  862.  
  863. /**
  864. * Reset Button class
  865. * This class generates a standard form reset button field.
  866. * @package form
  867. */
  868. class form_resetbutton extends form_buttonfield {
  869. // ....................................................................
  870. /**
  871. * Constructor
  872. * Create a field object. Sets basic field attributes.
  873. * @param string $name The name of the button
  874. * @param string $value The value of the button
  875. * @param string $css CSS class or style to apply to the button
  876. */
  877. function form_resetbutton($name="", $value="", $css="") {
  878. $this->form_buttonfield($name, $value, $css);
  879. $this->type = "reset";
  880. }
  881. } // form_resetbutton class
  882. // ----------------------------------------------------------------------
  883.  
  884. /**
  885. * Image Button class
  886. * This class generates a standard form image button field.
  887. * @package form
  888. */
  889. class form_imagebutton extends form_buttonfield {
  890. /** Popup confirmation text to display on button click
  891. @access private */
  892. var $confirm_text = "";
  893. /** Whether to allow form submit on-click
  894. @access private */
  895. var $onclick_form_submit = false;
  896. // ....................................................................
  897. /**
  898. * Constructor
  899. * Create a field object. Sets basic field attributes.
  900. * @param string $name The name of the button
  901. * @param string $value The value of the button
  902. * @param string $css CSS class or style to apply to the button
  903. */
  904. function form_imagebutton($name="", $value="", $css="", $src="", $tooltip="", $width="", $height="", $border=0) {
  905. $this->form_buttonfield($name, $value, $css);
  906. $this->type = "image";
  907. if ($name != "") $this->setalt($name);
  908. if ($src != "") {
  909. $this->setimage($src, $tooltip, $width, $height, $border);
  910. }
  911. }
  912. // ....................................................................
  913. /**
  914. * Sets the image to display for this button.
  915. * Usually these details are specified in the initial instantiation
  916. * @param string $src URL or path to image for the button
  917. * @param string $tooltip Button image tooltip (title tag) content
  918. * @param integer $width Button image width (px)
  919. * @param integer $height Button image height (px)
  920. * @param string $border Size of border around image
  921. */
  922. function setimage($src, $tooltip="", $width="", $height="", $border=0) {
  923. $this->setsrc($src);
  924. $this->settitle($tooltip);
  925. if ($width != "" || $height != "") {
  926. $this->setwidth ( (($width != "") ? $width : 0) );
  927. $this->setheight( (($height != "") ? $height : 0) );
  928. }
  929. else {
  930. if (extension_loaded("gd") && file_exists($src)) {
  931. $szinfo = getimagesize($this->src);
  932. $this->setwidth ( $szinfo[0] );
  933. $this->setheight( $szinfo[1] );
  934. }
  935. }
  936. $this->setborder($border);
  937. }
  938. // ....................................................................
  939. /**
  940. * Set the confirmation text to popup on click using Javascript.
  941. * @param string $conf Text to display in the confirmation popup.
  942. */
  943. function set_confirm_text($conf) {
  944. $this->confirm_text = $conf;
  945. }
  946. // ....................................................................
  947. /**
  948. * This renders the image button as HTML. If we have onclick then we render
  949. * this as a simple image with a javascript URL rather than a INPUT
  950. * form element of type "image". This is done to cope with Netscape's
  951. * lack of an onclick event handler for INPUT TYPE=IMAGE, and we
  952. * don't use BUTTON since that's only HTML4.
  953. * @return string HTML rendering of button
  954. */
  955. function html($name="") {
  956. global $RESPONSE;
  957. if ($name != "") $this->name = $name;
  958. // Set name to image file if not given..
  959. if ($this->name == "" && $this->src != "") {
  960. $fbits = explode(".", basename($this->src));
  961. $this->name = $fbits[0];
  962. }
  963. // Set tooltip to name if not given..
  964. if ($this->title == "") {
  965. $this->settitle($this->name);
  966. }
  967. // Set alt to name if not given..
  968. if ($this->alt == "") {
  969. $this->setalt($this->name);
  970. }
  971. // Lame Netscape doesn't support onclick, so use HREF surrogate..
  972. if ($this->onclick && isset($RESPONSE) && $RESPONSE->browser == BROWSER_NETSCAPE) {
  973. $html = "<a href=\"javascript:$this->onclick\"";
  974. if ($this->tooltip != "") {
  975. $html .= " onmouseover=\"window.status='$this->tooltip'; return true;\"";
  976. $html .= " onmouseout=\"window.status=''; return true;\"";
  977. }
  978. $html .= ">";
  979. $myimg = new img($this->src, $this->name, $this->tooltip, $this->width, $this->height);
  980. $myimg->setborder($this->border);
  981. if ($this->class != "") $myimg->setclass($this->class);
  982. if ($this->style != "") $myimg->setstyle($this->style);
  983. $html .= $myimg->render();
  984. $html .= "</a>";
  985. }
  986. else {
  987. // This will act as a submit button by default, and
  988. // will return its value with the form..
  989.  
  990. // Onclick/confirmation popup option..
  991. if (!$this->onclick && $this->confirm_text != "") {
  992. $this->onclick="return confirm('$this->confirm_text');";
  993. }
  994. if ($this->onclick) {
  995. // Append return false if form submit not allowed. Since the onclick
  996. // event is defined, we assume that the user's Javascript handler will
  997. // submit the form if required, unless this flag has been set true..
  998. if (!$this->onclick_form_submit) {
  999. if ($this->onclick != "" && substr($this->onclick, -1) != ";") {
  1000. $this->onclick .= ";";
  1001. }
  1002. $this->onclick .= "return false;";
  1003. }
  1004. }
  1005. // Dimensions via style setting..
  1006. if (!isset($RESPONSE) || $RESPONSE->browser != BROWSER_NETSCAPE) {
  1007. if ($this->width) $this->setstyle("width:$this->width" . "px;");
  1008. if ($this->height) $this->setstyle("height:$this->height" . "px;");
  1009. }
  1010. // Render the HTML..
  1011. $html = "<input";
  1012. $html .= " type=\"$this->type\"";
  1013. $html .= $this->field_attributes();
  1014. $html .= ">";
  1015. }
  1016. return $html;
  1017. }
  1018.  
  1019. } // form_imagebutton class
  1020. // ----------------------------------------------------------------------
  1021.  
  1022. /**
  1023. * Text Field class. This class generates a text field.
  1024. * @package form
  1025. */
  1026. class form_textfield extends form_field {
  1027. /** Format specifier (WML)
  1028. @access private */
  1029. var $format;
  1030. /** Maximum user-enterable chars for field
  1031. @access private */
  1032. var $maxlength;
  1033. // ....................................................................
  1034. /**
  1035. * Constructor
  1036. * Create a field object. Sets basic field attributes.
  1037. * @param string $name The name of the field
  1038. * @param string $label The label which can be displayed alongside the field
  1039. * @param string $value The value of the field
  1040. * @param boolean $editable Editability: EDITABLE or DISPLAY_ONLY (true or false)
  1041. * @param string $css CSS class or style to apply to the button
  1042. * @param integer $width Width of element in characters
  1043. */
  1044. function form_textfield($name="", $label="", $value="", $editable=EDITABLE, $css="", $width=STD_WIDTH) {
  1045. $this->form_field($name, $label, $value, $editable, $css);
  1046. $this->size = $width;
  1047. $this->type = "text";
  1048. $this->format = "*m";
  1049. } // form_textfield
  1050. // ....................................................................
  1051. /**
  1052. * Set the field width (usually in characters).
  1053. * @param integer $width Width of element in characters
  1054. * @param integer $maxlength Maximum no. of characters in field
  1055. */
  1056. function set_width($width, $maxlength=false) {
  1057. $this->size = $width;
  1058. if ($maxlength) {
  1059. $this->maxlength = $maxlength;
  1060. }
  1061. } // set_width
  1062. // ....................................................................
  1063. /**
  1064. * Set field format (WML)
  1065. * @param string $format WML format specifier.
  1066. */
  1067. function set_format($format) {
  1068. $this->format = $format;
  1069. } // set_format
  1070. // ....................................................................
  1071. /**
  1072. * This renders the field as WML.
  1073. * @return string The field as WML.
  1074. */
  1075. function wml($name="") {
  1076. $wml = "";
  1077. if ($name != "") $this->name = $name;
  1078. if ($this->label != "") $wml .= $this->label . ": ";
  1079. $wml .= "<input type=\"$this->type\"";
  1080. $wml .= " name=\"$this->name\"";
  1081. if ($this->label != "") $wml .= " title=\"$this->label\"";
  1082. if ($this->value != "") $wml .= " value=\"$this->value\"";
  1083. $wml .= " size=\"$this->size\"";
  1084. if ($this->format != "") $wml .= " format=\"$this->format\"";
  1085. if (isset($this->tabindex)) $wml .= " tabindex=\"$this->tabindex\"";
  1086. if (isset($this->maxlength)) $wml .= " maxlength=\"$this->maxlength\"";
  1087. $wml .= " />";
  1088. return $wml;
  1089. } // wml
  1090. // ....................................................................
  1091. /**
  1092. * This renders the field as HTML.
  1093. * @return string The field as HTML.
  1094. */
  1095. function html($name="") {
  1096. if ($name != "") $this->name = $name;
  1097. $html = "<input";
  1098. $html .= " type=\"$this->type\"";
  1099. $html .= $this->field_attributes();
  1100. if (isset($this->value)) $html .= " value=" . quoted_valuestring($this->value);
  1101. if (isset($this->maxlength)) $html .= " maxlength=\"$this->maxlength\"";
  1102. $html .= ">";
  1103. return $html;
  1104. } // html
  1105.  
  1106. } // form_textfield class
  1107. // ----------------------------------------------------------------------
  1108.  
  1109. /**
  1110. * Displayonly Field class
  1111. * Extends the textfield class. This class renders a textfield as
  1112. * text rather than a textbox control. The form value is still
  1113. * submitted, but as a hidden field in the form..
  1114. * @package form
  1115. */
  1116. class form_displayonlyfield extends form_textfield {
  1117. /**
  1118. * Constructor
  1119. * Create a disply-only field object. Sets basic field attributes.
  1120. * @param string $name The name of the field
  1121. * @param string $label The label which can be displayed alongside the field
  1122. * @param string $value The value of the field
  1123. */
  1124. function form_displayonlyfield($name="", $label="", $value="") {
  1125. $this->form_textfield($name, $label, $value);
  1126. $this->type = "displayonly";
  1127. }
  1128. // ....................................................................
  1129. /**
  1130. * Use render() to render this element in your page.
  1131. * This renders the field as HTML.
  1132. * @return string The field as HTML.
  1133. */
  1134. function html() {
  1135. return $this->as_displayonly();
  1136. }
  1137. } // form_displayonlyfield
  1138. // ----------------------------------------------------------------------
  1139.  
  1140. /**
  1141. * Password Field class
  1142. * Password field element. Same as textfield, but masks user input.
  1143. * @package form
  1144. */
  1145. class form_passwordfield extends form_textfield {
  1146. // ....................................................................
  1147. /**
  1148. * Constructor
  1149. * Create a field object. Sets basic field attributes.
  1150. * @param string $name The name of the field
  1151. * @param string $label The label which can be displayed alongside the field
  1152. * @param string $value The value of the field
  1153. * @param boolean $editable Editability: EDITABLE or DISPLAY_ONLY (true or false)
  1154. * @param string $css CSS class or style to apply to the button
  1155. * @param integer $width Width of element in characters
  1156. */
  1157. function form_passwordfield($name="", $label="", $value="", $editable=EDITABLE, $css="", $width=STD_WIDTH) {
  1158. $this->form_textfield($name, $label, $value, $editable, $css, $width);
  1159. $this->type = "password";
  1160. }
  1161. } // form_passwordfield class
  1162. // ----------------------------------------------------------------------
  1163.  
  1164. /**
  1165. * File Upload Field class
  1166. * A field for uploading files to the webserver. If used with a 'form'
  1167. * object, the form will be automatically rendered with the proper
  1168. * encoding type by setting the 'enctype' in the form tag.
  1169. * Example:
  1170. * $upField = new form_fileuploadfield("userfile", "Upload file", "", 500000);
  1171. * $upform = new form("upload_frm", "File Upload", "upload.php");
  1172. * $upform->add($upField);
  1173. * $upform->add_button(new submit_button("Upload", "Upload"));
  1174. * echo $upform->render();
  1175. * @package form
  1176. */
  1177. class form_fileuploadfield extends form_field {
  1178. /** Maximum file suze in bytes
  1179. @access private */
  1180. var $max_file_size = 16384;
  1181. /** Whether to include the max size in the form
  1182. @access private */
  1183. var $include_maxsize = false;
  1184. // ....................................................................
  1185. /**
  1186. * Constructor
  1187. * Create a field object. Sets basic field attributes.
  1188. * @param string $name The name of the field
  1189. * @param string $label The label which can be displayed alongside the field
  1190. * @param string $value The value of the field, ie. the filename/path
  1191. * @param integer $maxsize Maximum size of upload file in bytes
  1192. * @param boolean $incl_maxsize True if we include the MAX_FILE_SIZE hidden field when rendering
  1193. */
  1194. function form_fileuploadfield($name="", $label="", $value="", $maxsize=16384, $incl_maxsize=false) {
  1195. $this->form_field($name, $label, $value);
  1196. $this->type = "file";
  1197. $this->max_file_size = $maxsize;
  1198. $this->include_maxsize = $incl_maxsize;
  1199. }
  1200. // ....................................................................
  1201. /**
  1202. * This renders the field as HTML.
  1203. * @return string The field as HTML.
  1204. */
  1205. function html($name="") {
  1206. if ($name != "") $this->name = $name;
  1207. $html = "<input";
  1208. $html .= " type=\"$this->type\"";
  1209. $html .= $this->field_attributes();
  1210. if (isset($this->value)) $html .= " value=\"$this->value\"";
  1211. $html .= ">";
  1212. // Optional hidden field with maximum filesize setting..
  1213. if ($this->include_maxsize) {
  1214. $hid = new form_hiddenfield("MAX_FILE_SIZE", $this->max_file_size);
  1215. $html .= $hid->render();
  1216. }
  1217. return $html;
  1218. } // html
  1219.  
  1220. } // form_fileuploadfield class
  1221. // ----------------------------------------------------------------------
  1222.  
  1223. /**
  1224. * Combo Field class
  1225. * A field for producing combo boxes (dropdown select menus) or
  1226. * multi-line list-boxes, either of which may be single-select or
  1227. * multiple select.
  1228. * @package form
  1229. */
  1230. class form_combofield extends form_textfield {
  1231. // Public
  1232. /** Boolean, multiple item select */
  1233.  
  1234. var $multiselect;
  1235.  
  1236. // Private
  1237. /** Items to display, assoc. array
  1238. @access private */
  1239. var $itemlist;
  1240. // ....................................................................
  1241. /**
  1242. * Constructor
  1243. * Create a field object. Sets basic field attributes.
  1244. * @param string $name The name of the field
  1245. * @param string $label The label which can be displayed alongside the field
  1246. * @param string $value The value of the field
  1247. * @param boolean $editable Editability: EDITABLE or DISPLAY_ONLY (true or false)
  1248. * @param string $css CSS class or style to apply to the button
  1249. * @param integer $lines Number of lines to show in the drop-down combo box/listbox
  1250. * @param boolean $multi True if the combo is multiple-select, else false
  1251. */
  1252. function form_combofield($name="", $label="", $value="", $editable=EDITABLE, $css="", $lines=1, $multi=SINGLESELECT) {
  1253. $this->form_textfield($name, $label, $value, $editable, $css, STD_WIDTH);
  1254. $this->size = $lines;
  1255. $this->multiselect = $multi;
  1256. $this->itemlist = new dat_keyvalues();
  1257. $this->type = "select";
  1258. } // form_combofield
  1259. // ....................................................................
  1260. /**
  1261. * Set select field width. Note that selects can only have width set
  1262. * using a style, since they normally auto-set the width to the max
  1263. * length of their options display strings.
  1264. * @param integer $width Width of select element in pixels
  1265. */
  1266. function set_width($widthpx) {
  1267. if (!strstr($widthpx, ":")) $widthpx = "width:" . $widthpx . "px;";
  1268. $this->setstyle($widthpx);
  1269. } // width
  1270. // ....................................................................
  1271. /**
  1272. * Set select field size in lines.
  1273. * @param integer $lines Number of lines to display in the select element.
  1274. */
  1275. function set_size($lines) {
  1276. $this->size = $lines;
  1277. } // set_size
  1278. // ....................................................................
  1279. /**
  1280. * Add a data item
  1281. * Adds a key=>value pair into the combo options collection.
  1282. * @param string $key The key to use (the field 'value' or ID)
  1283. * @param string $value The value to assign to the key (displayed value)
  1284. */
  1285. function additem($key, $value="???") {
  1286. if ($value == "???") $value = $key;
  1287. $this->itemlist->additem($key, $value);
  1288. } // additem
  1289. // ....................................................................
  1290. /** Clears any existing items */
  1291.  
  1292. function clearitems() {
  1293. $this->itemlist->clearitems();
  1294. } // clearitems
  1295. // ....................................................................
  1296. /**
  1297. * Add ready-made data
  1298. * Use a ready-made, piping hot source of data. This should be a
  1299. * normal Key/Value pair associative array..
  1300. * @param array $data The array of key=>value pairs to add
  1301. */
  1302. function ovenready_data($data) {
  1303. $this->itemlist->data = $data;
  1304. } // ovenready_data
  1305. // ....................................................................
  1306. /**
  1307. * Add ready-made data. Populates the combo field data from a pre-run query.
  1308. * @param resource $query An Axyl query object, pre-executed, with data
  1309. * @param string $keyfield The name of the keyfield in the data
  1310. * @param string $displayfields The names of displayfields, delimited by "|"
  1311. */
  1312. function add_querydata($query, $keyfield, $displayfields) {
  1313. $this->itemlist->add_querydata($query, $keyfield, $displayfields);
  1314. } // add_querydata
  1315. // ....................................................................
  1316. /**
  1317. * This renders the field as WML.
  1318. * @return string The field as WML.
  1319. */
  1320. function wml($name="") {
  1321. $wml = "";
  1322. if ($name != "") $this->name = $name;
  1323. $selix = $this->itemlist->getitemindex($this->value);
  1324. if ($this->editable) {
  1325. $wml .= "<select name='$this->name'";
  1326. if ($selix > 0) $wml .= " ivalue='$selix'";
  1327. $wml .= ">";
  1328. // Options list
  1329. if ($this->itemlist->hasdata()) {
  1330. foreach ($this->itemlist->data as $key => $displayvalue) {
  1331. $wml .= "<option value=" . $key . ">";
  1332. $wml .= $displayvalue . "</option>";
  1333. }
  1334. }
  1335. $wml .= "</select>";
  1336. } else {
  1337. $this->value = $this->itemlist->getitem($this->value);
  1338. $f = new form_textfield($this->name, $this->label, $this->value, DISPLAY_ONLY, $this->style, $this->size);
  1339. $wml = $f->wml();
  1340. }
  1341. return $wml;
  1342. } // wml
  1343. // ....................................................................
  1344. /**
  1345. * This renders the field as HTML.
  1346. * @return string The field as HTML.
  1347. */
  1348. function html($name="") {
  1349. $html = "";
  1350. if ($name != "") $this->name = $name;
  1351.  
  1352. // Standard SELECT in html..
  1353. $saved_name = $this->name;
  1354. if ($this->multiselect) $this->name .= "[]";
  1355. $html .= "<select";
  1356. $html .= $this->field_attributes();
  1357. if ($this->multiselect) $html .= " multiple";
  1358. $html .= ">";
  1359. $this->name = $saved_name;
  1360.  
  1361. // HTML Options list
  1362. if ($this->itemlist->hasdata()) {
  1363. if (!is_array($this->value) && stristr($this->value, "regex=")) {
  1364. // The value associated with this combo/select is a
  1365. // regular expression to match the relevant key..
  1366. $regbits = explode("=", $this->value);
  1367. $regexp = $regbits[1];
  1368. }
  1369. else $regexp = "";
  1370.  
  1371. $selvalues = "";
  1372. foreach ($this->itemlist->data as $key => $displayvalue) {
  1373. $selected = "";
  1374. if ($regexp != "") {
  1375. // Do regular expression match test..
  1376. if (!empty($regexp) && !empty($key)) {
  1377. if (ereg($regexp, $key)) {
  1378. $selected = "selected ";
  1379. $this->value = $key;
  1380. }
  1381. }
  1382. }
  1383. else {
  1384. // Might be in an array if multiselect..
  1385. if (is_array($this->value)) {
  1386. if (count($this->value) > 0) {
  1387. foreach($this->value as $elem) {
  1388. if ($key == $elem) {
  1389. $selected = "selected ";
  1390. break;
  1391. }
  1392. }
  1393. }
  1394. else {
  1395. // An empty array should match a nullstring..
  1396. if ($key == "") {
  1397. $selected = "selected ";
  1398. }
  1399. }
  1400. }
  1401. else {
  1402. // Do normal simple equality test. Take care over the
  1403. // sometime equivalence of 0 and nullstrings..
  1404. if ($this->value === "") {
  1405. if ($key === "") {
  1406. $selected = "selected ";
  1407. }
  1408. }
  1409. else {
  1410. if ($key == $this->value) {
  1411. $selected = "selected ";
  1412. }
  1413. }
  1414. }
  1415. }
  1416. // Render the select option..
  1417. $html .= "<option " . $selected . "value=\"$key\">$displayvalue";
  1418.  
  1419. // Remember selected values..
  1420. if ($selected != "") {
  1421. if ($selvalues != "") {
  1422. $selvalues .= ",";
  1423. }
  1424. $selvalues .= $key;
  1425. }
  1426. }
  1427. }
  1428. $html .= "</select>";
  1429.  
  1430. if (!$this->editable && !$this->disabled) {
  1431. // Not editable, so render it as a hidden field
  1432. // for the data, and plain text for the user..
  1433. // Implement as hidden field value, plus visible
  1434. // normal text display-value..
  1435. $name = $this->name;
  1436. if ($this->multiselect) $name .= "[]";
  1437. $hidf = new form_hiddenfield($name, $selvalues);
  1438. $html .= $hidf->render();
  1439. }
  1440. return $html;
  1441. } // html
  1442.  
  1443. } // form_combofield class
  1444. // ----------------------------------------------------------------------
  1445.  
  1446. /**
  1447. * Jumpmenu Field class
  1448. * A special case of combofield where we define key/value data made up
  1449. * of display-value and URL pairs.
  1450. * @package form
  1451. */
  1452. class form_jumpmenu extends form_combofield {
  1453. // ....................................................................
  1454. /**
  1455. * Constructor
  1456. * Create a field object. Sets basic field attributes.
  1457. * @param string $name The name of the field
  1458. * @param string $label The label which can be displayed alongside the field
  1459. * @param string $value The value of the field
  1460. * @param boolean $editable Editability: EDITABLE or DISPLAY_ONLY (true or false)
  1461. * @param string $css CSS class or style to apply to the button
  1462. * @param integer $lines Number of lines to show in the drop-down combo box/listbox
  1463. */
  1464. function form_jumpmenu($name="", $label="", $value="", $editable=EDITABLE, $css="", $lines=1) {
  1465. $this->form_combofield($name, $label, $value, $editable, $css, $lines);
  1466. } // form_jumpmenu
  1467. // ....................................................................
  1468. /**
  1469. * This renders the field as WML.
  1470. * @return string The field as WML.
  1471. */
  1472. function wml($name="") {
  1473. $wml = "";
  1474. if ($name != "") $this->name = $name;
  1475. if ($this->editable) {
  1476. $wml .= "<select name=\"$this->name\"";
  1477. if ($this->label != "") $wml .= " title=\"" . $this->label . "\"";
  1478. if ($this->value != "") $wml .= " ivalue=\"" . $this->value . "\"";
  1479. $wml .= ">";
  1480. // Options list
  1481. if ($this->itemlist->hasdata()) {
  1482. $jix = 1;
  1483. foreach ($this->itemlist->data as $displayvalue => $jumpurl) {
  1484. if (function_exists("href_rewrite")) {
  1485. $jumpurl = href_rewrite($jumpurl);
  1486. }
  1487. $wml .= "<option value=\"$jix\"";
  1488. $wml .= " onpick=\"$jumpurl\"";
  1489. $wml .= ">";
  1490. $wml .= $displayvalue . "</option>";
  1491. $jix += 1;
  1492. }
  1493. }
  1494. $wml .= "</select>";
  1495. }
  1496. else {
  1497. $this->value = $this->itemlist->getitem($this->value);
  1498. $f = new form_textfield(
  1499. $this->name,
  1500. $this->label,
  1501. $this->value,
  1502. DISPLAY_ONLY,
  1503. $this->style,
  1504. $this->width
  1505. );
  1506. if (isset($this->class) && $this->class != "") {
  1507. $f->setcss($this->class);
  1508. }
  1509. $wml = $f->wml();
  1510. }
  1511. return $wml;
  1512. } // wml
  1513. // ....................................................................
  1514. /**
  1515. * This renders the field as HTML.
  1516. * @return string The field as HTML.
  1517. */
  1518. function html($name="") {
  1519. $html = "";
  1520. if ($name != "") $this->name = $name;
  1521. // Standard jumpmenu in html..
  1522. $html .= "<select";
  1523. $html .= $this->field_attributes();
  1524. $html .= ">";
  1525.  
  1526. // HTML Options list
  1527. if ($this->itemlist->hasdata()) {
  1528. if (stristr($this->value, "regex=")) {
  1529. // The value associated with this combo/select is a
  1530. // regular expression to match the relevant key..
  1531. $regbits = explode("=", $this->value);
  1532. $regexp = $regbits[1];
  1533. }
  1534. else $regexp = "";
  1535.  
  1536. foreach ($this->itemlist->data as $displayvalue => $jumpurl) {
  1537. $html .= "<option";
  1538. if ($regexp != "") {
  1539. // Do regular expression match test..
  1540. if (!empty($regexp) && !empty($displayvalue)) {
  1541. if (ereg($regexp, $displayvalue)) {
  1542. $html .= " selected";
  1543. $this->value = $displayvalue;
  1544. }
  1545. }
  1546. }
  1547. else {
  1548. // Do normal equality test..
  1549. if ($displayvalue == $this->value) $html .= " selected";
  1550. }
  1551. $html .= " value=\"$displayvalue\"";
  1552. $html .= " onclick=\"javascript:document.location.href='$jumpurl'\"";
  1553. $html .= ">";
  1554. $html .= $displayvalue;
  1555. }
  1556. }
  1557. $html .= "</select>";
  1558.  
  1559. if (!$this->editable) {
  1560. // Not editable, so render it as a hidden field
  1561. // for the data, and plain text for the user..
  1562. // Implement as hidden field value, plus visible
  1563. // normal text display-value..
  1564. $hidf = new form_hiddenfield($this->name, $this->value);
  1565. $html .= $hidf->render();
  1566. $html .= "<strong>$this->itemlist->getitem($this->value)</strong>";
  1567. }
  1568. return $html;
  1569. } // html
  1570.  
  1571. } // form_jumpmenu class
  1572. // ----------------------------------------------------------------------
  1573.  
  1574. /**
  1575. * Memo Field class
  1576. * A field which renders a textarea form element. These are used to
  1577. * allow people to input large tracts of text by typing or copy/paste.
  1578. * @package form
  1579. */
  1580. class form_memofield extends form_textfield {
  1581. /** Number of rows in the text box */
  1582.  
  1583. var $rows;
  1584. /** Wrapping mode: 'virtual', 'physical', or 'off'
  1585. @access private */
  1586. var $wrapmode;
  1587. // ....................................................................
  1588. /**
  1589. * Constructor
  1590. * Create a field object. Sets basic field attributes.
  1591. * @param string $name The name of the field
  1592. * @param string $label The label which can be displayed alongside the field
  1593. * @param string $value The value of the field
  1594. * @param boolean $editable Editability: EDITABLE or DISPLAY_ONLY (true or false)
  1595. * @param string $css CSS class or style to apply to the button
  1596. * @param integer $width Width of the memo field in characters
  1597. * @param integer $rows Height of the memo field in lines
  1598. * @param string $wrapmode Wrapping mode: 'virtual', 'physical', or 'off'
  1599. */
  1600. function form_memofield($name="", $label="", $value="", $editable=EDITABLE, $css="", $width=STD_WIDTH, $rows=5, $wrapmode="virtual") {
  1601. $this->form_textfield($name, $label, $value, $editable, $css, $width);
  1602. $this->rows = $rows;
  1603. $this->type = "textarea";
  1604. $this->wrapmode = $wrapmode;
  1605. } // form_memofield
  1606. // ....................................................................
  1607. /**
  1608. * Set the wrap mode for the textarea.
  1609. * @param string $mode Wrapping mode: 'virtual', 'physical', or 'off'
  1610. */
  1611. function set_wrapmode($mode="virtual") {
  1612. $this->wrapmode = $mode;
  1613. } // set_wrapmode
  1614. // ....................................................................
  1615. /**
  1616. * This renders the field as HTML.
  1617. * @see render()
  1618. * @return string The field as HTML.
  1619. */
  1620. function html($name="") {
  1621. global $RESPONSE;
  1622. // Figure out a legal wrapmode..
  1623. if (isset($RESPONSE)) {
  1624. if ($RESPONSE->browser == BROWSER_MOZILLA || $RESPONSE->browser == BROWSER_NETSCAPE) {
  1625. if ($this->wrapmode == "physical") $this->wrapmode = "hard";
  1626. elseif ($this->wrapmode == "virtual") $this->wrapmode = "soft";
  1627. }
  1628. else {
  1629. if ($this->wrapmode == "hard") $this->wrapmode = "physical";
  1630. elseif ($this->wrapmode == "soft") $this->wrapmode = "virtual";
  1631. }
  1632. }
  1633.  
  1634. $html = "";
  1635. if ($name != "") $this->name = $name;
  1636. $html .= "<textarea";
  1637. $html .= $this->field_attributes();
  1638. if (isset($this->size)) $html .= " cols=\"$this->size\"";
  1639. if (isset($this->rows)) $html .= " rows=\"$this->rows\"";
  1640. if (isset($this->wrapmode)) $html .= " wrap=\"$this->wrapmode\"";
  1641. $html .= ">";
  1642. if (isset($this->value)) $html .= $this->value;
  1643. $html .= "</textarea>";
  1644. return $html;
  1645. } // html
  1646. // ....................................................................
  1647. /**
  1648. * Set the number of rows for this memo field.
  1649. * @param integer $rows The number of rows for this memo field.
  1650. */
  1651. function set_rows($rows) {
  1652. $this->rows = $rows;
  1653. } // set_rows
  1654.  
  1655. } // form_memofield class
  1656. // ----------------------------------------------------------------------
  1657.  
  1658. /**
  1659. * Checkbox Field class
  1660. * A field which renders a checkbox form element.
  1661. * @package form
  1662. */
  1663. class form_checkbox extends form_field {
  1664. /** True if checkbox is ticked/checked */
  1665.  
  1666. var $checked;
  1667. // ....................................................................
  1668. /**
  1669. * Constructor
  1670. * Create a field object. Sets basic field attributes.
  1671. * @param string $name The name of the field
  1672. * @param string $label The label which can be displayed alongside the field
  1673. * @param string $value The value of the field
  1674. * @param boolean $editable Editability: EDITABLE or DISPLAY_ONLY (true or false)
  1675. * @param string $css CSS class or style to apply to the button
  1676. * @param boolean $checked True if element should be checked/ticked
  1677. */
  1678. function form_checkbox($name="", $label="", $value="", $editable=EDITABLE, $css="", $checked=FALSE) {
  1679. $this->form_field($name, $label, $value, $editable, $css="");
  1680. $this->checked = $checked;
  1681. if ($value != "") $this->value = $value;
  1682. else $this->value = $name;
  1683. $this->type = "checkbox";
  1684. } // form_checkbox
  1685. // ....................................................................
  1686. /** Sets the checkbox to the checked state. */
  1687.  
  1688. function check() {
  1689. $this->checked = true;
  1690. } // check
  1691. // ....................................................................
  1692. /** Sets the checkbox to the unchecked state. */
  1693.  
  1694. function uncheck() {
  1695. $this->checked = false;
  1696. } // uncheck
  1697. // ....................................................................
  1698. /**
  1699. * This renders the field as HTML.
  1700. * @return string The field as HTML.
  1701. */
  1702. function html($name="") {
  1703. if ($name != "") $this->name = $name;
  1704. $html = "<input";
  1705. $html .= " type=\"checkbox\"";
  1706. $html .= $this->field_attributes();
  1707. if ($this->checked) $html .= " checked";
  1708. $html .= " value=\"$this->value\"";
  1709. $html .= ">";
  1710. // Only send the readonly field if its checked..
  1711. if (!$this->editable && $this->checked) {
  1712. $hidf = new form_hiddenfield($this->name, $this->value);
  1713. $html .= $hidf->render();
  1714. }
  1715. return $html;
  1716. } // html
  1717.  
  1718. } // form_checkbox class
  1719. // ----------------------------------------------------------------------
  1720.  
  1721. /**
  1722. * Radio Button Field class
  1723. * A field which renders a radio form element.
  1724. * @package form
  1725. */
  1726. class form_radiobutton extends form_field {
  1727. /** True if this element should be selected */
  1728.  
  1729. var $checked;
  1730. // ....................................................................
  1731. /**
  1732. * Constructor
  1733. * Create a field object. Sets basic field attributes.
  1734. * @param string $name The name of the field
  1735. * @param string $label The label which can be displayed alongside the field
  1736. * @param string $value The value of the field
  1737. * @param boolean $editable Editability: EDITABLE or DISPLAY_ONLY (true or false)
  1738. * @param string $css CSS class or style to apply to the button
  1739. * @param boolean $checked True if element should be checked/ticked
  1740. */
  1741. function form_radiobutton($name="", $label="", $value="", $editable=EDITABLE, $css="", $checked=FALSE) {
  1742. $this->form_field($name, $label, $value, $editable, $css="");
  1743. $this->checked = $checked;
  1744. if ($value != "") $this->value = $value;
  1745. else $this->value = $name;
  1746. $this->type = "radio";
  1747. } // form_radiobutton
  1748. // ....................................................................
  1749. /** Sets the checkbox to the checked state. */
  1750.  
  1751. function check() {
  1752. $this->checked = true;
  1753. } // check
  1754. // ....................................................................
  1755. /** Sets the checkbox to the unchecked state. */
  1756.  
  1757. function uncheck() {
  1758. $this->checked = false;
  1759. } // uncheck
  1760. // ....................................................................
  1761. /**
  1762. * This renders the field as HTML.
  1763. * @return string The field as HTML.
  1764. */
  1765. function html($name="") {
  1766. if ($name != "") $this->name = $name;
  1767. $html = "<input";
  1768. $html .= " type=\"radio\"";
  1769. $html .= $this->field_attributes();
  1770. if ($this->checked) $html .= " checked";
  1771. $html .= " value=\"$this->value\"";
  1772. $html .= ">";
  1773. if (!$this->editable) {
  1774. $hidf = new form_hiddenfield($this->name, $this->value);
  1775. $html .= $hidf->render();
  1776. }
  1777. return $html;
  1778. }
  1779. } // form_radiobutton class
  1780. // ----------------------------------------------------------------------
  1781.  
  1782. /**
  1783. * Radio Group Field class
  1784. * A field which groups a set of radio form elements together.
  1785. * This element actually extends the form_combofield, since it is basically
  1786. * a list of selectable options, like a single-select dropdown menu is.
  1787. * As such, you can add key->value options using the 'additem' method, just
  1788. * like for combo fields.
  1789. * @package form
  1790. */
  1791. class form_radiogroup extends form_combofield {
  1792. /** Group orientation: 'horizontal' or 'vertical' */
  1793.  
  1794. var $orientation = "horizontal";
  1795. /** Side to place label: 'right' or 'left' */
  1796.  
  1797. var $labelside = "right";
  1798. // ....................................................................
  1799. /**
  1800. * Constructor
  1801. * Create a field object. Sets basic field attributes.
  1802. * @param string $name The name of the field
  1803. * @param string $label The label which can be displayed alongside the field
  1804. * @param string $value The value of the field
  1805. * @param boolean $editable Editability: EDITABLE or DISPLAY_ONLY (true or false)
  1806. * @param string $style Style to apply to the field
  1807. * @param boolean $orientation Group orientation: 'horizontal' (default) or 'vertical'
  1808. */
  1809. function form_radiogroup($name="", $label="", $value="", $editable=EDITABLE, $css="", $orientation="horizontal") {
  1810. $this->form_combofield($name, $label, $value, $editable, $css);
  1811. $this->orientation = $orientation;
  1812. if ($value != "") $this->value = $value;
  1813. else $this->value = $name;
  1814. $this->type = "radiogroup";
  1815. } // form_radiogroup
  1816. // ....................................................................
  1817. /**
  1818. * This renders the field as HTML.
  1819. * @return string The field as HTML.
  1820. */
  1821. function html($name="") {
  1822. $html = "";
  1823. if ($name != "") $this->name = $name;
  1824. if ($this->itemlist->hasdata()) {
  1825. $Trg = new table("radiogroup");
  1826. $Trg->setwidth("");
  1827. $first = true;
  1828. while ( list($key, $displayvalue) = each($this->itemlist->data) ) {
  1829. $rad = new form_radiobutton($this->name, $displayvalue, $key);
  1830. if ($this->value == $key) $rad->check();
  1831. if ($this->labelside == "left") $radbtn = "$displayvalue&nbsp;" . $rad->render();
  1832. else $radbtn = $rad->render() . "&nbsp;$displayvalue";
  1833. if ($first) {
  1834. if (isset($this->style) && $this->style != "") $Trg->tbody($this->style);
  1835. elseif (isset($this->class) && $this->class != "") $Trg->tbody($this->class);
  1836. $Trg->tr();
  1837. $first = false;
  1838. }
  1839. elseif ($this->orientation == "vertical") {
  1840. $Trg->tr();
  1841. }
  1842. $Trg->td( $radbtn );
  1843. }
  1844. $html .= $Trg->render();
  1845. }
  1846. return $html;
  1847. } // html
  1848.  
  1849. } // form_radiogroup class
  1850. // ----------------------------------------------------------------------
  1851.  
  1852. /**
  1853. * Image Field class
  1854. * This is a hybrid field. It is basically a text field where the
  1855. * value is expected to be the location of an image, either a real path
  1856. * to a file or a URI.
  1857. * @package form
  1858. */
  1859. class form_imagefield extends form_textfield {
  1860. /** Prefix to image URL/path. This will be prefixed to the value. */
  1861.  
  1862. var $prefix;
  1863. // ....................................................................
  1864. /**
  1865. * Constructor
  1866. * Create a field object. Sets basic field attributes.
  1867. * @param string $name The name of the field
  1868. * @param string $label The label which can be displayed alongside the field
  1869. * @param string $value The value of the field
  1870. * @param boolean $editable Editability: EDITABLE or DISPLAY_ONLY (true or false)
  1871. * @param string $css CSS class or style to apply to the button
  1872. * @param integer $width Width of element in characters
  1873. * @param string $prefix Directory path prefix to apply to image file path
  1874. */
  1875. function form_imagefield($name="", $label="", $value="", $editable=EDITABLE, $css="", $width=STD_WIDTH, $prefix="") {
  1876. $this->form_textfield($name, $label, $value, $editable, $css, $width);
  1877. if ($prefix != "" && substr($prefix, -1) != "/") {
  1878. $prefix .= "/";
  1879. }
  1880. $this->prefix = $prefix;
  1881. } // form_imagefield
  1882. // ....................................................................
  1883. /**
  1884. * This renders the field as HTML.
  1885. * @return string The field as HTML.
  1886. */
  1887. function html($name="") {
  1888. $s = "<table border=0 cellpadding=1 cellspacing=0>";
  1889. $s .= "<tr><td colspan=2 valign=top>" . form_textfield::html() . "</td></tr>";
  1890. $s .= "<tr><td valign=top>";
  1891. if ($this->value != "") {
  1892. $imgurl = $this->prefix . $this->value;
  1893. $img = new image("img_" . $this->name, $imgurl, "Image at $imgurl");
  1894. $s .= $img->html();
  1895. }
  1896. $s .= "</td>";
  1897. $s .= "<td>";
  1898. if ($imgurl != "") {
  1899. if ($this->prefix != "") {
  1900. $s .= "Path: $this->prefix<br>";
  1901. }
  1902. $siz = getimagesize($imgurl);
  1903. if (isset($siz[0])) {
  1904. $s .= "Size: " . $siz[0] . " x " . $siz[1] . "<br>";
  1905. switch ($siz[2]) {
  1906. case 1: $type = "GIF"; break;
  1907. case 2: $type = "JPG"; break;
  1908. case 3: $type = "PNG"; break;
  1909. case 4: $type = "SWF"; break;
  1910. case 5: $type = "PSD"; break;
  1911. case 6: $type = "BMP"; break;
  1912. default: "??";
  1913. }
  1914. $s .= "Type: $type";
  1915. }
  1916. }
  1917. $s .= "</td></tr>";
  1918. $s .= "</table>\n";
  1919. return $s;
  1920. } // html
  1921.  
  1922. } // form_imagefield class
  1923. // ----------------------------------------------------------------------
  1924.  
  1925. /**
  1926. * Tandem Field class
  1927. * This is a rather bizarre hybrid field. It is basically a pair of text
  1928. * fields. The second field is initialised with the same properties as the
  1929. * first but can be individually styled etc. by accessing the relevant
  1930. * property in this class. The name given to the tandem field is the
  1931. * same as the parent field, but with the prefix "tandem_" applied.
  1932. * @package form
  1933. */
  1934. class form_tandemfield extends form_textfield {
  1935. /** The second field object in tandem with this one */
  1936.  
  1937. var $tandemfield;
  1938. /** Overall width in pixels */
  1939.  
  1940. var $tandemwidth;
  1941. /** True if main field should be suppressed */
  1942.  
  1943. var $summaryfield = false;
  1944. // ....................................................................
  1945. /**
  1946. * Constructor
  1947. * Create a field object. Sets basic field attributes.
  1948. * @param string $name The name of the field
  1949. * @param string $label The label which can be displayed alongside the field
  1950. * @param string $value The value of the field
  1951. * @param boolean $editable Editability: EDITABLE or DISPLAY_ONLY (true or false)
  1952. * @param string $css CSS class or style to apply to the button
  1953. * @param integer $width Width of element in characters
  1954. */
  1955. function form_tandemfield($name="", $label="", $value="", $editable=EDITABLE, $css="", $width=STD_WIDTH) {
  1956. $this->form_textfield($name, $label, $value, $editable, $css, $width);
  1957. $this->tandemfield = new form_textfield("tandem_$name", $label, $value, $editable, $css, $width);
  1958. } // form_tandemfield
  1959. // ....................................................................
  1960. /**
  1961. * Use render() to render this element in your page.
  1962. * This renders the field as HTML.
  1963. * @see render()
  1964. * @return string The field as HTML.
  1965. */
  1966. function html() {
  1967. $Tt = new table();
  1968. if (isset($this->tandemwidth)) {
  1969. $Tt->setwidth($this->tandemwidth);
  1970. }
  1971. $Tt->tr();
  1972. if ($this->summaryfield) {
  1973. $Tt->td( "&nbsp;" );
  1974. $Tt->td( $this->tandemfield->html(), "padding-left:5px" );
  1975. }
  1976. else {
  1977. $Tt->td( form_textfield::html(), "padding-right:5px" );
  1978. $Tt->td( $this->tandemfield->html(), "padding-left:5px" );
  1979. }
  1980. $Tt->set_width_profile("50%,50%");
  1981. return $Tt->render();
  1982. } // html
  1983.  
  1984. } // form_tandemfield class
  1985. // ----------------------------------------------------------------------
  1986.  
  1987. ?>

Documentation generated by phpDocumentor 1.3.0RC3