9c779179842f59dabef18ca1a208a913457fc4d5
[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 $mPassword      = false;
33     var $mColumns       = "";
34     var $mRows          = "";
35     var $class = 'TextArea';
36     var $mOnKeyPress    = "";
37
38         function __construct($name,$value="")
39         {
40                 $this->name = $name;
41                 $this->value = $value;
42         }
43         
44         function setColumns($value)     { $this->mColumns = $value;     }
45         function getColumns()   { return $this->mColumns; }
46
47         function setRows($value)        { $this->mRows = $value;        }
48         function getRows()      { return $this->mRows; }
49         
50         function toStringControl() {
51     
52             if ($this->id=="") $this->id = $this->mName;
53             
54             $js_maxlen = "";
55             
56                 $html = "\n\t<textarea";
57                 $html .= " name=\"$this->name\" id=\"$this->id\"";
58                 
59                 if ($this->mColumns!="")
60                   $html .= " cols=\"$this->mColumns\"";
61                   
62                 if ($this->mRows!="")
63                    $html .= " rows=\"$this->mRows\"";
64                    
65                 if ($this->max_length!="") {
66                         if ($this->mOnKeyPress) $this->mOnKeyPress .= ";";
67                         $this->mOnKeyPress .= "return validateMaxLenght_".$this->name."(this, event);";
68                         $js_maxlen = $this->getExtraScript();
69                         $html .= " maxlength=\"$this->max_length\"";
70                 }
71
72                 if ($this->style!="")
73                    $html .= " style=\"$this->style\"";
74
75                 if ($this->mOnKeyPress) {
76                         $html .= " onkeypress=\"$this->mOnKeyPress\"";
77                 }
78                         
79                 $html .= ">".htmlspecialchars($this->getValue())."</textarea>";
80                 if ($js_maxlen) $html = $js_maxlen."\n".$html;
81                 
82                 return $html;
83         }
84         
85         function getExtraScript() {
86                 $s = "<script>\n";
87                 $s .= "var isNS4 = (navigator.appName==\"Netscape\")?1:0;\n";
88                 $s .= "function validateMaxLenght_".$this->name."(element, event) {\n";
89                 $s .= "\tmaxlength=".$this->max_length.";\n";
90                 $s .= "\tvar iKey = (!isNS4?event.keyCode:event.which);\n";
91                 //$s .= "alert(iKey);";
92                 $s .= "\tvar re = new RegExp(\"".'\r\n'."\",\"g\");\n";
93                 $s .= "\tvar x = element.value.replace(re,\"\").length;\n";
94                 $s .= "\tif ((x>=maxlength) && ((iKey > 31 && iKey < 1200) || (iKey > 95 && iKey < 106)) && (iKey != 13)) {\n";
95                 $s .= "\t\treturn false;\n";
96                 $s .= "\t} else {\n";
97                 $s .= "\t\treturn true;\n";
98                 $s .= "\t}\n}\n";
99                 $s .= "</script>\n";
100                 return $s;
101         }
102 }