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