Ongoing refactoring of Checkbox class.
[timetracker.git] / WEB-INF / lib / form / TextArea.class.php
1 <?php
2 // +----------------------------------------------------------------------+
3 // | Anuko Time Tracker
4 // +----------------------------------------------------------------------+
5 // | Copyright (c) Anuko International Ltd. (https://www.anuko.com)
6 // +----------------------------------------------------------------------+
7 // | LIBERAL FREEWARE LICENSE: This source code document may be used
8 // | by anyone for any purpose, and freely redistributed alone or in
9 // | combination with other software, provided that the license is obeyed.
10 // |
11 // | There are only two ways to violate the license:
12 // |
13 // | 1. To redistribute this code in source form, with the copyright
14 // |    notice or license removed or altered. (Distributing in compiled
15 // |    forms without embedded copyright notices is permitted).
16 // |
17 // | 2. To redistribute modified versions of this code in *any* form
18 // |    that bears insufficient indications that the modifications are
19 // |    not the work of the original author(s).
20 // |
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
23 // |
24 // +----------------------------------------------------------------------+
25 // | Contributors:
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
28
29 import('form.FormElement');
30         
31 class TextArea extends FormElement {
32     var $mColumns       = "";
33     var $mRows          = "";
34     var $mOnKeyPress    = "";
35
36   function __construct($name, $value = '')
37   {
38     $this->class = 'TextArea';
39     $this->name = $name;
40     $this->value = $value;
41   }
42         
43         function setColumns($value)     { $this->mColumns = $value;     }
44         function getColumns()   { return $this->mColumns; }
45
46         function setRows($value)        { $this->mRows = $value;        }
47         function getRows()      { return $this->mRows; }
48         
49         function getHtml() {
50     
51             if ($this->id=="") $this->id = $this->mName;
52             
53             $js_maxlen = "";
54             
55                 $html = "\n\t<textarea";
56                 $html .= " name=\"$this->name\" id=\"$this->id\"";
57                 
58                 if ($this->mColumns!="")
59                   $html .= " cols=\"$this->mColumns\"";
60                   
61                 if ($this->mRows!="")
62                    $html .= " rows=\"$this->mRows\"";
63                    
64                 if ($this->max_length!="") {
65                         if ($this->mOnKeyPress) $this->mOnKeyPress .= ";";
66                         $this->mOnKeyPress .= "return validateMaxLenght_".$this->name."(this, event);";
67                         $js_maxlen = $this->getExtraScript();
68                         $html .= " maxlength=\"$this->max_length\"";
69                 }
70
71                 if ($this->style!="")
72                    $html .= " style=\"$this->style\"";
73
74                 if ($this->mOnKeyPress) {
75                         $html .= " onkeypress=\"$this->mOnKeyPress\"";
76                 }
77                         
78                 $html .= ">".htmlspecialchars($this->getValue())."</textarea>";
79                 if ($js_maxlen) $html = $js_maxlen."\n".$html;
80                 
81                 return $html;
82         }
83         
84         function getExtraScript() {
85                 $s = "<script>\n";
86                 $s .= "var isNS4 = (navigator.appName==\"Netscape\")?1:0;\n";
87                 $s .= "function validateMaxLenght_".$this->name."(element, event) {\n";
88                 $s .= "\tmaxlength=".$this->max_length.";\n";
89                 $s .= "\tvar iKey = (!isNS4?event.keyCode:event.which);\n";
90                 //$s .= "alert(iKey);";
91                 $s .= "\tvar re = new RegExp(\"".'\r\n'."\",\"g\");\n";
92                 $s .= "\tvar x = element.value.replace(re,\"\").length;\n";
93                 $s .= "\tif ((x>=maxlength) && ((iKey > 31 && iKey < 1200) || (iKey > 95 && iKey < 106)) && (iKey != 13)) {\n";
94                 $s .= "\t\treturn false;\n";
95                 $s .= "\t} else {\n";
96                 $s .= "\t\treturn true;\n";
97                 $s .= "\t}\n}\n";
98                 $s .= "</script>\n";
99                 return $s;
100         }
101 }