set_validate_func
// muutettu 23.3.2006 klo 13:00 / Jukka Mäntylä
// - get_count() choicegroupeille, antaa valittujen määrän
// - php-tagimerkintä ihan alkuun
// - get_name ja get_label formcontrolliin
// muutettu 23.3.2006 klo 14:00 / Jukka Mäntylä
// - get_label lisätty grouppeihin
// muutettu 23.3.2006 klo 14:14 / Tommi Lahtonen
// - lisätty get_name grouppeihin
// muutettu 23.3.2006 klo 15:15 / Jukka Mäntylä
// - laitettu oletusarvoja selectgroupin konstruktoriin
// - vaihdettu selectgroupin konstruktorin parametrien järjestystä
// muutettu 25.3.2006 klo 15:30 / Jukka Mäntylä
// - requestista asettaminen toimii nyt vaikka namessa olisi pisteitä
// tai välilyöntejä, vastaavia alaviivallisia nameja ei saa käyttää
// muutettu 27.3.2006 klo 12:00 / Jukka Mäntylä
// - textboxin set_from_request-bugi hyväksyy nyt nollan
// muutettu 29.3.2006 klo 12:00 / Jukka Mäntylä
// - komponentit eivät aseta itseään requestista ilman eri käskyä
// - poistettu is_req-muuttuja ja is_set_from_req, nämä voidaan toteuttaa
// suoraan metodilla set_from_request. Groupit saavat default-arvon oletuksena.
// - get_choice_count on nyt public
// - choice ja choicegroup poistaa [ -merkin jälkeiset osat nameista
// muutettu 30.3.2006 klo 13:20 / Jukka Mäntylä
// - $this->str_strip_brackets lisätty grouppiin
// - CText:iin lisätty isset set_from_request-metodiin
// muutettu 5.4.2006 klo 23:45 / Jukka Mäntylä
// - lisätty submit-button-luokka
/*
CFormControl
Lomake-elementtien yhteiset ominaisuudet ja metodit, sisältää otsikot
esim. label + input/option
- set_validate_func - voidaan määrätä lomakkeen arvot tarkastava funktio
- set_from_request - toteuttava metodi, joka asettaa lomakkeen arvon $_REQUEST-muuttujasta
- get_string - metodi, joka palauttaa elementin ja labelin XHTML:nä
*/
abstract class CFormControl {
protected $name = "", $value = "", $label = "";
//protected $is_req = false; // onko oletusarvo saatu $_REQUEST-muuttujasta
protected $validate_func; // funktion nimi, jolle viedään tämän luokan olio
protected $display_errors = false;
// abstraktit funktiot, jotka pitää toteuttaa aliluokissa
abstract function set_from_request(); // palauttaa true jos onnistuu
abstract function get_string();
// tätä tarvitaan kun pässi PHP menee muuttelemaan nameja $_REQUEST:ssa
protected function str_replace_underscore($string) {
return str_replace(array("."," "),"_",$string);
}
protected function str_strip_brackets($string) {
$pos = strpos($string, "[");
if ($pos === false)
return $string;
else
return substr($string, 0, $pos);
}
// olion luonnin yhteydessä haetaan mahdollinen arvo $_REQUEST-muuttujasta
function __construct($name, $value, $label = "") {
$this->name = $this->str_strip_brackets($name);
$this->value = $value;
$this->label = $label;
}
// is_-funktiot
function is_valid() {
$error = $this->get_error();
if (empty($error))
return true;
return false;
}
// set-funktiot
function set_validate_func($func) {
if (is_callable($func))
$this->validate_func = $func;
}
function set_name($name) {
$this->name = $name;
}
function set_label($value) {
$this->label = $value;
}
function set_value($value) {
$this->value = $value;
}
function set_display_errors($bool) {
if (is_bool($bool))
$this->display_errors = $bool;
}
// get-funktiot
function get_label() {
return $this->label;
}
function get_name() {
return $this->name;
}
function get_req_name() {
return $this->str_replace_underscore($this->name);
}
function get_value() {
return $this->value;
}
// tarkistaa annetulla funktiolla onko syötteessä virheitä
function get_error() {
if (isset($this->validate_func)) {
$call_func =$this->validate_func;
return $call_func($this);
} else {
return "";
}
}
// voidaan käyttää echo-funktion kanssa
function __toString() {
return $this->get_string();
}
}
/*
CText
Tekstilaatikko
*/
class CText extends CFormControl {
private $size;
const type = "text";
function __construct($name, $value, $label, $size="25") {
parent::__construct($name, $value, $label);
if (is_numeric($size))
$this->size = $size;
}
function set_from_request() {
if (isset($_REQUEST[$this->get_req_name()])) {
if (strlen($_REQUEST[$this->get_req_name()]) > 0) {
$this->value = $_REQUEST[$this->get_req_name()];
return true;
}
}
return false;
}
function get_string() {
$string = "";
if (empty($this->label))
$string = "value)."\" name=\"$this->name\" size=\"$this->size\" maxlength=\"$this->size\" />";
else {
$id = uniqid("id_");
$string = "value)."\" name=\"$this->name\" size=\"$this->size\" maxlength=\"$this->size\" />";
}
if ($this->display_errors) {
if (!$this->is_valid())
$string = $string."".$this->get_error()."";
}
return $string;
}
}
/*
CSubmit
Lomakkeen lähetyspainike
*/
class CSubmit extends CFormControl {
const type = "submit";
function __construct($name, $value, $label = "") {
parent::__construct($name, $value, $label);
}
// ei aseta valueta, koska napin tekstiä ei ole järkeä muuttaa
// tarkistaa kuitenkin saako arvonsa requestista (=onko lähetetty?)
function set_from_request() {
return $this->is_submitted();
}
function is_submitted() {
if (isset($_REQUEST[$this->get_req_name()])) {
if (strlen($_REQUEST[$this->get_req_name()]) > 0) {
return true;
}
}
return false;
}
function get_string() {
$string = "";
if (empty($this->label))
$string = "value)."\" name=\"$this->name\" />";
else {
$id = uniqid("id_");
$string = "value)."\" name=\"$this->name\" />";
}
return $string;
}
}
/*
CRadio
Yksittäinen valintanappi
*/
class CRadio extends CFormControl {
private $checked = false;
const type = "radio";
function set_from_request() {
if (isset($_REQUEST[$this->get_req_name()])) {
if($_REQUEST[$this->get_req_name()] == $this->value) {
$this->set_checked();
return true;
}
else {
$this->set_unchecked();
}
}
return false;
}
function is_checked() {
return $this->checked;
}
function set_checked() {
$this->checked = true;
}
function set_unchecked() {
$this->checked = false;
}
function get_value() {
if ( $this->is_checked() ) {
return $this->value;
}
}
function get_string() {
if ($this->is_checked())
$add_checked = "checked=\"checked\"";
else
$add_checked = "";
$add_error = "";
if ($this->display_errors) {
if (!$this->is_valid())
$add_error = "".$this->get_error()."";
}
if (strlen($this->label) < 1)
return "value\" name=\"$this->name\" ".$add_checked." />".$add_error;
else {
$id = uniqid("id_");
return "value\" name=\"$this->name\" ".$add_checked." />".$add_error;
}
}
}
/*
CCheckbox
Yksittäinen valintalaatikko
*/
class CCheckbox extends CFormControl {
private $checked = false;
private $name_indexed = false;
const type = "checkbox";
function set_from_request() {
if (isset($_REQUEST[$this->get_req_name()])) {
$values = $_REQUEST[$this->get_req_name()];
foreach ($values as $value) {
if ( $value == $this->value ) {
$this->set_checked();
return true;
}
}
}
$this->set_unchecked();
return false;
}
function is_checked() {
return $this->checked;
}
function set_checked() {
$this->checked = true;
}
function set_unchecked() {
$this->checked = false;
}
function get_value() {
if ( $this->is_checked() ) {
return $this->value;
}
}
function get_string() {
if ($this->is_checked())
$add_checked = "checked=\"checked\"";
else
$add_checked = "";
$add_error = "";
if ($this->display_errors) {
if (!$this->is_valid())
$add_error = "".$this->get_error()."";
}
if (strlen($this->label) < 1)
return "value\" name=\"$this->name[]\" ".$add_checked." />".$add_error;
else {
$id = uniqid("id_");
return "value\" name=\"$this->name[]\" ".$add_checked." />".$add_error;
}
}
}
/*
COption
Yksittäinen valintalistan vaihtoehto
*/
class COption extends CRadio {
//perintä CRadio:sta: olennaisesti samanlainen kuin radio, vähän erilainen tulostus
function get_string() {
if ($this->is_checked())
$add_checked = "selected=\"selected\"";
else
$add_checked = "";
if (empty($this->label))
return "";
else {
return "";
}
}
}
class COptionMultiple extends CCheckbox {
function get_string() {
if ($this->is_checked())
$add_checked = "selected=\"selected\"";
else
$add_checked = "";
if (empty($this->label))
return "";
else {
return "";
}
}
}
/*
CChoiceGroup
Valintaryhmän yhteiset ominaisuudet ja metodit
esim. select, checkbox-ryhmä, radiobutton-ryhmä
- set_group_validate_func - voidaan määrätä ryhmän valintojen virheettömyys
- create_choice - metodi, jonka tehtävä on luoda ja palauttaa sopiva lomake-elementti
- get_string - palauttaa ryhmän XHTML:nä
*/
abstract class CChoiceGroup {
protected $choices = array();
protected $groupname = "";
protected $group_validate_func;
protected $display_errors = false;
protected function str_strip_brackets($string) {
$pos = strpos($string, "[");
if ($pos === false)
return $string;
else
return substr($string, 0, $pos);
}
// $choices = Array( [label1] => value1, [label2] => value2, ...)
// $default = array, indeksi tai ""
function __construct($groupname, $choices, $default = "") {
$this->groupname = $this->str_strip_brackets($groupname);
if (!empty($choices)) {
// luodaan kukin valinta
foreach ($choices as $label => $value) {
$this->choices[] = $this->create_choice($groupname, $value, $label);
}
/*
// etsitään onko jokin saanut requestista oletusarvon
$is_req = false;
foreach ($this->choices as $choice) {
if ($choice->is_set_from_req()) {
$is_req = true;
break;
}
}
// jos requestista ei ole löytynyt valintaa, niin asetetaan default-arvon mukaan
if (!$is_req) {
*/
// asetetaan oletusarvot
$choices_count = count($this->choices);
if (is_array($default)) {
foreach ($default as $index) {
if (0 <= $index && $index < $choices_count)
$this->choices[$index]->set_checked();
}
} else {
if (is_numeric($default)) {
$index = $default;
if (0 <= $index && $index < $choices_count)
$this->choices[$index]->set_checked();
}
}
}
}
// is_-funktiot
function is_valid() {
$error = $this->get_error();
if (empty($error))
return true;
return false;
}
// set-funktiot
function set_validate_func($func) {
if (is_callable($func))
$this->group_validate_func = $func;
}
function set_display_errors($bool) {
if (is_bool($bool))
$this->display_errors = $bool;
}
// jos ryhmästä yksikin saa arvonsa requestista niin palauttaa true
function set_from_request() {
$isreq = false;
foreach($this->choices as $choice) {
if ($choice->set_from_request())
$isreq = true;
}
return $isreq;
}
// tarkistaa onko ryhmässä virheitä
// palauttaa virheilmoituksen tai tyhjän merkkijonon
function get_error() {
if (isset($this->group_validate_func)) {
$call_func = $this->group_validate_func;
// antaa tarkastavalle funktiolle kaikki valinnat, voisi antaa myös iteraattorin
return $call_func($this);
} else {
return "";
}
}
function get_choice_count() {
return count($this->choices);
}
function get_value() {
return $this->get_chosen_val();
}
function get_name() {
return $this->groupname;
}
abstract function get_label();
abstract function get_count(); // palauttaa valittujen määrän
abstract function get_chosen_ind(); // palauttaa valittujen indeksit taulukossa
abstract function get_chosen_val(); // palauttaa valittujen arvot
abstract protected function create_choice($groupname, $value, $label);
abstract function get_string();
function __toString() {
return $this->get_string();
}
}
/*
CRadioGroup
Valintanappiryhmä
*/
class CRadioGroup extends CChoiceGroup {
// palauttaa valitun indeksinumeron, jos ei löydy niin palauttaa -1
function get_chosen_ind() {
for ($i=0;$i<$this->get_choice_count();$i++) {
if ($this->choices[$i]->is_checked()) {
return $i;
}
}
return -1;
}
function get_chosen_val() {
for ($i=0;$i<$this->get_choice_count();$i++) {
if ($this->choices[$i]->is_checked())
return $this->choices[$i]->get_value();
}
return false; // ei pitäisi tulla tällaista tilannetta
}
function get_label() {
for ($i=0;$i<$this->get_choice_count();$i++) {
if ($this->choices[$i]->is_checked())
return $this->choices[$i]->get_label();
}
return false; // ei pitäisi tulla tällaista tilannetta
}
function get_count() {
if ($this->get_chosen_ind() == -1)
return 0;
return 1;
}
protected function create_choice($groupname, $value, $label) {
return new CRadio($groupname, $value, $label);
}
function get_string() {
$napit = "";
for ($i=0;$ichoices);$i++) {
$button = $this->choices[$i];
$napit = $napit."".($button->get_string())."\n";
}
if ($this->display_errors) {
if (!$this->is_valid())
$napit = $napit."".$this->get_error()."";
}
return $napit;
}
}
/*
CRadioGroup
Valintalaatikkoryhmä
*/
class CCheckboxGroup extends CChoiceGroup {
function is_checked($choice_index) {
return $this->choices[$choice_index]->is_checked();
}
function get_chosen_ind() {
$indexes = array();
for($i=0;$i<$this->get_choice_count();$i++)
if ($this->is_checked($i))
$indexes[] = $i;
return $indexes;
}
function get_chosen_val() {
$values = array();
for($i=0;$i<$this->get_choice_count();$i++)
if ($this->is_checked($i))
$values[] = $this->choices[$i]->get_value();
return $values;
}
function get_label() {
$values = array();
for($i=0;$i<$this->get_choice_count();$i++)
if ($this->is_checked($i))
$values[] = $this->choices[$i]->get_label();
return $values;
}
function get_count() {
$sum = 0;
for($i=0;$i<$this->get_choice_count();$i++)
if ($this->is_checked($i))
$sum++;
return $sum;
}
protected function create_choice($groupname, $value, $label) {
return new CCheckbox($groupname, $value, $label);
}
function get_string() {
$napit = "";
for ($i=0;$ichoices);$i++) {
$button = $this->choices[$i];
$napit = $napit."".($button->get_string())."\n";
}
if ($this->display_errors) {
if (!$this->is_valid())
$napit = $napit."".$this->get_error()."";
}
return $napit;
}
}
/*
CSelectGroup
Valintalista (alasvetovalikko yhdellä valinnalla)
*/
class CSelectGroup extends CRadioGroup {
protected $label = "";
function __construct($groupname, $choices, $label = "", $default = "") {
parent::__construct($groupname, $choices, $default);
$this->label = $label;
}
protected function create_choice($groupname, $value, $label) {
return new COption($groupname, $value, $label);
}
// palauttaa select-elementin mahdollisine virheineen
function get_string() {
$id = uniqid("id_");
if (strlen($this->label) > 0)
$valinnat = "";
else
$valinnat = "";
$valinnat = $valinnat."\n";
if ($this->display_errors) {
if (!$this->is_valid())
$valinnat = $valinnat."".$this->get_error()."";
}
return $valinnat;
}
}
/*
CSelectMultipleGroup
Valintalista (alasvetovalikko monella valinnalla)
*/
class CSelectMultipleGroup extends CCheckboxGroup {
protected $label = "";
protected $size = 4;
function __construct($groupname, $choices, $label = "", $size = 4, $default = "") {
parent::__construct($groupname, $choices, $default);
$this->label = $label;
if (is_numeric($size))
$this->size = $size;
}
protected function create_choice($groupname, $value, $label) {
return new COptionMultiple($groupname, $value, $label);
}
// palauttaa select-elementin mahdollisine virheineen
function get_string() {
$id = uniqid("id_");
if (strlen($this->label) > 0)
$valinnat = "";
else
$valinnat = "";
$valinnat = $valinnat."\n";
if ($this->display_errors) {
if (!$this->is_valid())
$valinnat = $valinnat."".$this->get_error()."";
}
return $valinnat;
}
}
?>