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