1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13:
14:
15: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
16:
17: 18: 19: 20: 21: 22:
23: class cGuiTableForm {
24:
25: 26: 27: 28: 29:
30: private $_acceptCharset = '';
31:
32: 33: 34: 35:
36: public $items = array();
37:
38: 39: 40: 41:
42: public $captions = array();
43:
44: 45: 46: 47:
48: public $id = 0;
49:
50: 51: 52: 53:
54: public $rownames = array();
55:
56: 57: 58: 59:
60: public $itemType = array();
61:
62: 63: 64: 65:
66: public $formname;
67:
68: 69: 70: 71:
72: public $formmethod;
73:
74: 75: 76: 77:
78: public $formaction;
79:
80: 81: 82: 83:
84: public $formvars = array();
85:
86: 87: 88: 89:
90: public $tableid = "";
91:
92: 93: 94: 95:
96: public ;
97:
98: 99: 100: 101:
102: public $cancelLink;
103:
104: 105: 106: 107:
108: public $submitjs;
109:
110: 111: 112: 113:
114: public $custom = array();
115:
116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127:
128: public function __construct($name, $action = 'main.php', $method = 'post') {
129:
130:
131: if ($action == '') {
132: $action = 'main.php';
133: }
134:
135:
136: $this->formname = $name;
137: $this->formaction = $action;
138: $this->formmethod = $method;
139:
140: $this->setActionButton('submit', cRegistry::getBackendUrl() . 'images/but_ok.gif', i18n('Save changes'), 's');
141: }
142:
143: 144: 145: 146: 147:
148: public function setVar($name, $value) {
149: $this->formvars[$name] = $value;
150: }
151:
152: 153: 154: 155: 156: 157: 158:
159: public function add($caption, $item, $rowname = "") {
160:
161:
162: if (is_array($item)) {
163: $temp = "";
164: foreach ($item as $value) {
165: if (is_object($value) && method_exists($value, "render")) {
166: $temp .= $value->render();
167: } else {
168: $temp .= $value;
169: }
170: }
171: $item = $temp;
172: }
173:
174:
175: if (is_object($item) && method_exists($item, "render")) {
176: $item = $item->render();
177: }
178:
179:
180: $this->id++;
181:
182:
183: if ($caption == "") {
184: $caption = " ";
185: }
186: if ($item == "") {
187: $item = " ";
188: }
189: if ($rowname == "") {
190: $rowname = $this->id;
191: }
192:
193: $this->captions[$this->id] = $caption;
194: $this->items[$this->id] = $item;
195: $this->rownames[$this->id] = $rowname;
196: }
197:
198: 199: 200: 201: 202:
203: public function addCancel($link) {
204: $this->cancelLink = $link;
205: }
206:
207: 208: 209: 210: 211: 212:
213: public function ($header) {
214: $this->header = $header;
215: }
216:
217: 218: 219: 220:
221: public function ($header) {
222: $this->id++;
223: $this->items[$this->id] = '';
224: $this->captions[$this->id] = $header;
225: $this->itemType[$this->id] = 'subheader';
226: }
227:
228: 229: 230: 231:
232: public function setSubmitJS($js) {
233: $this->submitjs = $js;
234: }
235:
236: 237: 238: 239: 240:
241: public function setAcceptCharset($charset) {
242: $this->_acceptCharset = $charset;
243: }
244:
245: 246: 247: 248: 249:
250: public function setActionEvent($id, $event) {
251: $this->custom[$id]["event"] = $event;
252: }
253:
254: 255: 256: 257: 258: 259: 260: 261:
262: public function setActionButton($id, $image, $description = "", $accesskey = false, $action = false) {
263: $this->custom[$id]["image"] = $image;
264: $this->custom[$id]["type"] = "actionsetter";
265: $this->custom[$id]["action"] = $action;
266: $this->custom[$id]["description"] = $description;
267: $this->custom[$id]["accesskey"] = $accesskey;
268: $this->custom[$id]["event"] = "";
269: }
270:
271: 272: 273: 274: 275: 276:
277: public function setConfirm($id, $title, $description) {
278: $this->custom[$id]["confirmtitle"] = $title;
279: $this->custom[$id]["confirmdescription"] = $description;
280: }
281:
282: 283: 284: 285:
286: public function setTableID($tableid) {
287: $this->tableid = $tableid;
288: }
289:
290: 291: 292: 293:
294: public function unsetActionButton($id) {
295: unset($this->custom[$id]);
296: }
297:
298: 299: 300: 301: 302: 303: 304: 305: 306: 307:
308: public function render($return = true) {
309: global $sess, $cfg;
310:
311: $tpl = new cTemplate();
312:
313: if ($this->submitjs != "") {
314: if (cString::getStringLength($this->_acceptCharset) > 0) {
315: $tpl->set("s", "JSEXTRA", 'onsubmit="' . $this->submitjs
316: . '" accept-charset="' . $this->_acceptCharset . '"');
317: } else {
318: $tpl->set("s", "JSEXTRA", 'onsubmit="' . $this->submitjs . '"');
319: }
320: } else {
321: if (cString::getStringLength($this->_acceptCharset) > 0) {
322: $tpl->set("s", "JSEXTRA", 'accept-charset="' . $this->_acceptCharset . '"');
323: } else {
324: $tpl->set("s", "JSEXTRA", '');
325: }
326: }
327:
328: $tpl->set("s", "FORMNAME", $this->formname);
329: $tpl->set("s", "METHOD", $this->formmethod);
330: $tpl->set("s", "ACTION", $this->formaction);
331:
332: $this->formvars[$sess->name] = $sess->id;
333:
334: $hidden = "";
335: if (is_array($this->formvars)) {
336: foreach ($this->formvars as $key => $value) {
337: $val = new cHTMLHiddenField($key, $value);
338: $hidden .= $val->render() . "\n";
339: }
340: }
341:
342: if (!array_key_exists("action", $this->formvars)) {
343: $val = new cHTMLHiddenField("", "");
344: $hidden .= $val->render() . "\n";
345: }
346:
347: $tpl->set("s", "HIDDEN_VALUES", $hidden);
348:
349: $tpl->set('s', 'ID', $this->tableid);
350:
351: $header = "";
352: if ($this->header != "") {
353: $tablerow = new cHTMLTableRow();
354: $tablehead = new cHTMLTableHead();
355: $tablehead->setAttribute("colspan", "2");
356: $tablehead->setAttribute("valign", "top");
357: $tablehead->setContent($this->header);
358: $tablerow->setContent($tablehead);
359: $header = $tablerow->render();
360: }
361:
362: $tpl->set('s', 'HEADER', $header);
363:
364: if (is_array($this->items)) {
365: foreach ($this->items as $key => $value) {
366: if (!empty($this->itemType[$key]) && $this->itemType[$key] == 'subheader') {
367: $tablerow = new cHTMLTableRow();
368: $tabledata = new cHTMLTableData();
369: $tabledata->setAttribute("colspan", "2");
370: $tabledata->setAttribute("valign", "top");
371: $tabledata->setContent($this->captions[$key]);
372: $tablerow->setContent($tablehead);
373:
374: $tpl->set('d', 'SUBHEADER', $tablerow->render());
375: } else {
376: $tpl->set('d', 'SUBHEADER', '');
377: $tpl->set('d', 'CATNAME', $this->captions[$key]);
378: $tpl->set('d', 'CATFIELD', $value);
379: $tpl->set('d', 'ROWNAME', $this->rownames[$key]);
380:
381: $tpl->next();
382: }
383: }
384: }
385:
386: if ($this->cancelLink != "") {
387: $image = new cHTMLImage(cRegistry::getBackendUrl() . 'images/but_cancel.gif');
388: $link = new cHTMLLink($this->cancelLink);
389: $link->setContent($image);
390:
391: $tpl->set('s', 'CANCELLINK', $link->render());
392: } else {
393: $tpl->set('s', 'CANCELLINK', '');
394: }
395:
396: $custombuttons = "";
397:
398: foreach ($this->custom as $key => $value) {
399: if ($value["accesskey"] != "") {
400: $accesskey = $value["accesskey"];
401: } else {
402: $accesskey = "";
403: }
404:
405: $onclick = "";
406: if ($value["action"] !== false) {
407:
408: if ($value["confirmtitle"] != "") {
409: $action = 'document.forms["' . $this->formname . '"].elements["action"].value = "' . $value['action'] . '";';
410: $action .= 'document.forms["' . $this->formname . '"].submit()';
411:
412: $onclick = 'Con.showConfirmation("' . $value['confirmdescription'] . '", function() { ' . $action . ' });return false;';
413: } else {
414: $onclick = 'document.forms["' . $this->formname . '"].elements["action"].value = "' . $value['action'] . '";';
415: }
416: }
417:
418: if ($value["event"] != "") {
419: $onclick .= $value["event"];
420: }
421:
422: $button = new cHTMLFormElement("submit", "", "", "", "", "image_button");
423: $button->setAttribute("type", "image");
424: $button->setAttribute("src", $value["image"]);
425: $button->setAlt($value['description']);
426: $button->setAttribute("accesskey", $accesskey);
427: $button->setEvent("onclick", $onclick);
428: $custombuttons .= $button->render();
429: }
430:
431: $tpl->set('s', 'EXTRABUTTONS', $custombuttons);
432:
433: $tpl->set('s', 'ROWNAME', $this->id);
434:
435: $rendered = $tpl->generate(cRegistry::getBackendPath() . $cfg['path']['templates'] . $cfg['templates']['generic_table_form'], true);
436:
437: if ($return == true) {
438: return $rendered;
439: } else {
440: echo $rendered;
441: }
442: }
443: }
444: