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