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: public function render($return = true) {
307: global $sess, $cfg;
308:
309: $tpl = new cTemplate();
310:
311: if ($this->submitjs != "") {
312: if (strlen($this->_acceptCharset) > 0) {
313: $tpl->set("s", "JSEXTRA", 'onsubmit="' . $this->submitjs
314: . '" accept-charset="' . $this->_acceptCharset . '"');
315: } else {
316: $tpl->set("s", "JSEXTRA", 'onsubmit="' . $this->submitjs . '"');
317: }
318: } else {
319: if (strlen($this->_acceptCharset) > 0) {
320: $tpl->set("s", "JSEXTRA", 'accept-charset="' . $this->_acceptCharset . '"');
321: } else {
322: $tpl->set("s", "JSEXTRA", '');
323: }
324: }
325:
326: $tpl->set("s", "FORMNAME", $this->formname);
327: $tpl->set("s", "METHOD", $this->formmethod);
328: $tpl->set("s", "ACTION", $this->formaction);
329:
330: $this->formvars[$sess->name] = $sess->id;
331:
332: $hidden = "";
333: if (is_array($this->formvars)) {
334: foreach ($this->formvars as $key => $value) {
335: $val = new cHTMLHiddenField($key, $value);
336: $hidden .= $val->render() . "\n";
337: }
338: }
339:
340: if (!array_key_exists("action", $this->formvars)) {
341: $val = new cHTMLHiddenField("", "");
342: $hidden .= $val->render() . "\n";
343: }
344:
345: $tpl->set("s", "HIDDEN_VALUES", $hidden);
346:
347: $tpl->set('s', 'ID', $this->tableid);
348:
349: $header = "";
350: if ($this->header != "") {
351: $tablerow = new cHTMLTableRow();
352: $tablehead = new cHTMLTableHead();
353: $tablehead->setAttribute("colspan", "2");
354: $tablehead->setAttribute("valign", "top");
355: $tablehead->setContent($this->header);
356: $tablerow->setContent($tablehead);
357: $header = $tablerow->render();
358: }
359:
360: $tpl->set('s', 'HEADER', $header);
361:
362: if (is_array($this->items)) {
363: foreach ($this->items as $key => $value) {
364: if ($this->itemType[$key] == 'subheader') {
365: $tablerow = new cHTMLTableRow();
366: $tabledata = new cHTMLTableData();
367: $tabledata->setAttribute("colspan", "2");
368: $tabledata->setAttribute("valign", "top");
369: $tabledata->setContent($this->captions[$key]);
370: $tablerow->setContent($tablehead);
371:
372: $tpl->set('d', 'SUBHEADER', $tablerow->render());
373: } else {
374: $tpl->set('d', 'SUBHEADER', '');
375: $tpl->set('d', 'CATNAME', $this->captions[$key]);
376: $tpl->set('d', 'CATFIELD', $value);
377: $tpl->set('d', 'ROWNAME', $this->rownames[$key]);
378:
379: $tpl->next();
380: }
381: }
382: }
383:
384: if ($this->cancelLink != "") {
385: $image = new cHTMLImage(cRegistry::getBackendUrl() . 'images/but_cancel.gif');
386: $link = new cHTMLLink($this->cancelLink);
387: $link->setContent($image);
388:
389: $tpl->set('s', 'CANCELLINK', $link->render());
390: } else {
391: $tpl->set('s', 'CANCELLINK', '');
392: }
393:
394: $custombuttons = "";
395:
396: foreach ($this->custom as $key => $value) {
397: if ($value["accesskey"] != "") {
398: $accesskey = $value["accesskey"];
399: } else {
400: $accesskey = "";
401: }
402:
403: $onclick = "";
404: if ($value["action"] !== false) {
405:
406: if ($value["confirmtitle"] != "") {
407: $action = 'document.forms["' . $this->formname . '"].elements["action"].value = "' . $value['action'] . '";';
408: $action .= 'document.forms["' . $this->formname . '"].submit()';
409:
410: $onclick = 'Con.showConfirmation("' . $value['confirmdescription'] . '", function() { ' . $action . ' });return false;';
411: } else {
412: $onclick = 'document.forms["' . $this->formname . '"].elements["action"].value = "' . $value['action'] . '";';
413: }
414: }
415:
416: if ($value["event"] != "") {
417: $onclick .= $value["event"];
418: }
419:
420: $button = new cHTMLFormElement("submit", "", "", "", "", "image_button");
421: $button->setAttribute("type", "image");
422: $button->setAttribute("src", $value["image"]);
423: $button->setAlt($value['description']);
424: $button->setAttribute("accesskey", $accesskey);
425: $button->setEvent("onclick", $onclick);
426: $custombuttons .= $button->render();
427: }
428:
429: $tpl->set('s', 'EXTRABUTTONS', $custombuttons);
430:
431: $tpl->set('s', 'ROWNAME', $this->id);
432:
433: $rendered = $tpl->generate(cRegistry::getBackendPath() . $cfg['path']['templates'] . $cfg['templates']['generic_table_form'], true);
434:
435: if ($return == true) {
436: return $rendered;
437: } else {
438: echo $rendered;
439: }
440: }
441: }
442: