PageRenderTime 55ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/core/web/includes/FormGenerator.php

https://github.com/vmasilva/mmc
PHP | 1156 lines | 857 code | 160 blank | 139 comment | 117 complexity | 18d7aac174abd6b5c13876368f5e05d3 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * (c) 2004-2007 Linbox / Free&ALter Soft, http://linbox.com
  4. * (c) 2007-2008 Mandriva, http://www.mandriva.com
  5. *
  6. * $Id$
  7. *
  8. * This file is part of Mandriva Management Console (MMC).
  9. *
  10. * MMC is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * MMC is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with MMC; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. /***********************************************************************
  25. * Form generator class
  26. ***********************************************************************/
  27. function displayErrorCss($name) {
  28. global $formErrorArray;
  29. if (isset($formErrorArray[$name]) && ($formErrorArray[$name]==1)) {
  30. print ' style="color: #C00; text-align:right;"';
  31. }
  32. }
  33. /**
  34. * astract class template
  35. */
  36. class AbstractTpl extends HtmlElement {
  37. var $name;
  38. /**
  39. * display abstract Element
  40. * $arrParam accept ["value"]
  41. */
  42. function display($arrParam) {
  43. }
  44. /**
  45. * Read Only display function
  46. */
  47. function displayRo($arrParam) {
  48. print $arrParam["value"];
  49. print '<input type="hidden" value="'.$arrParam["value"].'" name="'.$this->name.'" />';
  50. }
  51. function displayHide($arrParam) {
  52. print '<div style="color: #C00;">' . _("unavailable") . '</div>';
  53. print '<input type="hidden" value="'.$arrParam["value"].'" name="'.$this->name.'" />';
  54. }
  55. }
  56. class TextareaTpl extends AbstractTpl {
  57. var $name;
  58. var $rows;
  59. var $cols;
  60. function TextareaTpl($name) {
  61. $this->name = $name;
  62. $this->rows = 3;
  63. $this->cols = 21;
  64. }
  65. function setRows($value) {
  66. $this->rows = $value;
  67. }
  68. function setCols($value) {
  69. $this->cols = $value;
  70. }
  71. function display($arrParam) {
  72. if (!isset($arrParam['disabled'])) {
  73. $arrParam['disabled'] = '';
  74. }
  75. echo '<textarea name="'.$this->name.'" id="'.$this->name.'" class="textfield" rows="' . $this->rows . '" cols="'.$this->cols.'" '.$arrParam["disabled"].' />';
  76. if (isset($arrParam["value"])) {
  77. echo $arrParam["value"];
  78. }
  79. echo '</textarea>';
  80. }
  81. }
  82. class FileTpl extends AbstractTpl {
  83. function FileTpl($name) {
  84. $this->name=$name;
  85. }
  86. function display($arrParam) {
  87. print '<input name="'.$this->name.'" id="'.$this->name.'" type="file" class="textfield" size="23" />';
  88. }
  89. function displayRo($arrParam) {
  90. }
  91. }
  92. class RadioTpl extends AbstractTpl {
  93. var $name;
  94. var $choices;
  95. var $choiceVal;
  96. var $choiceWidget;
  97. var $selected;
  98. function RadioTpl($name) {
  99. $this->name = $name;
  100. }
  101. function setChoices($arrChoices) {
  102. $this->choices = $arrChoices;
  103. }
  104. function setValues($arrValues) {
  105. $this->choiceVal = $arrValues;
  106. }
  107. function setWidgets($arrWidgets) {
  108. $this->choiceWidget = $arrWidgets;
  109. }
  110. function setSelected($selected) {
  111. $this->selected = $selected;
  112. }
  113. function display($arrParam) {
  114. if (!isset($this->choiceVal)) {
  115. $this->choiceVal = $this->choices;
  116. }
  117. if (!isset($this->selected)) {
  118. $this->selected = $this->choiceVal[0];
  119. }
  120. if (isset($this->choiceWidget)) {
  121. print '<table cellspacing="0" style="border: none; margin: 0px;">'."\n";
  122. }
  123. foreach ($this->choiceVal as $key => $value) {
  124. if (isset($this->choiceWidget)) {
  125. if ($key == 0) {
  126. print '<tr><td style="border-top: none;">';
  127. } else {
  128. print '<tr><td>';
  129. }
  130. } else {
  131. if ($key > 0) {
  132. print '<br/>'."\n";
  133. }
  134. }
  135. if ($this->selected == $value) {
  136. $selected = "checked";
  137. } else {
  138. $selected = "";
  139. }
  140. print '<input name="'.$this->name.'" value="'.$this->choiceVal[$key].'" id="'.$this->name.'" type="radio" '.$selected.'>'.$this->choices[$key];
  141. if (isset($this->choiceWidget)) {
  142. if ($key == 0) {
  143. print '</td><td style="border-top: none;">';
  144. } else {
  145. print '</td><td>';
  146. }
  147. $widget = $this->choiceWidget[$key][0];
  148. $wParam = $this->choiceWidget[$key][1];
  149. $widget->display($wParam);
  150. print '</td></tr>'."\n";
  151. }
  152. }
  153. if (isset($this->choiceWidget)) {
  154. print '</table>'."\n";
  155. }
  156. }
  157. }
  158. class ImageTpl extends AbstractTpl {
  159. function ImageTpl($name) {
  160. $this->name = $name;
  161. }
  162. function display($arrParam) {
  163. if ($arrParam['value'])
  164. $img = "data:image/jpeg;base64," . base64_encode($arrParam['value']->scalar);
  165. else
  166. $img = "img/users/icn_users_large.gif";
  167. echo '
  168. <img src=' . $img .' style="border-width: 1px; border-style: solid" />
  169. </td>
  170. </tr>
  171. <tr>
  172. <td>&nbsp;</td>
  173. <td><input name="photofilename" type="file" size="23" />';
  174. if ($arrParam["action"] == "edit")
  175. echo '<input name="deletephoto" type="submit" value="' . _("Delete photo") . '"/>';
  176. }
  177. function displayRo($arrParam) {
  178. if ($arrParam['value'])
  179. $img = "data:image/jpeg;base64," . base64_encode($arrParam['value']->scalar);
  180. else
  181. $img = "img/users/icn_users_large.gif";
  182. echo '<img src=' . $img .' style="border-width: 1px; border-style: solid" />';
  183. }
  184. }
  185. /**
  186. * Checkbox input template
  187. */
  188. class CheckboxTpl extends AbstractTpl{
  189. function CheckboxTpl($name, $rightlabel = null, $jsFunc = null) {
  190. $this->name = $name;
  191. $this->rightlabel = $rightlabel;
  192. $this->jsFunc = $jsFunc;
  193. }
  194. /**
  195. * display input Element
  196. * $arrParam accept ["value"] to corresponding value
  197. */
  198. function display($arrParam = array()) {
  199. if (empty($arrParam)) $arrParam = $this->options;
  200. if (!isset($arrParam['extraArg'])) {
  201. $arrParam["extraArg"] = '';
  202. }
  203. print '<input '.$arrParam["value"].' name="'.$this->name.'" id="'.$this->name.'" type="checkbox" class="checkbox" '.$arrParam["extraArg"];
  204. if($this->jsFunc) {
  205. print " onchange=\"" . $this->jsFunc . "(); return false;\"";
  206. }
  207. print ' />';
  208. if (isset($this->rightlabel)) print $this->rightlabel . "\n<br/>\n";
  209. }
  210. function displayRo($arrParam) {
  211. if ($arrParam["value"]=="checked") {
  212. $value="on";
  213. print _("yes");
  214. } else {
  215. $value = "";
  216. print _("no");
  217. }
  218. print '<input type="hidden" value="'.$value.'" name="'.$this->name.'" />';
  219. }
  220. function displayHide($arrParam) {
  221. if ($arrParam["value"] == "checked") {
  222. $value = "on";
  223. } else {
  224. $value = "off";
  225. }
  226. print '<div style="color: #C00;">' . _("unavailable") . '</div>';
  227. print '<input type="hidden" value="'.$value.'" name="'.$this->name.'" />';
  228. }
  229. function check($checked) {
  230. if($checked)
  231. $this->options["value"] = "checked";
  232. else
  233. $this->options["value"] = "";
  234. }
  235. }
  236. /**
  237. * simple input template
  238. */
  239. class InputTpl extends AbstractTpl{
  240. function InputTpl($name, $regexp = '/.+/') {
  241. $this->name = $name;
  242. $this->regexp = $regexp;
  243. $this->fieldType = "text";
  244. $this->size = '23';
  245. }
  246. function setSize($size) {
  247. $this->size = $size;
  248. }
  249. /**
  250. * display input Element
  251. * $arrParam accept ["value"] to corresponding value
  252. */
  253. function display($arrParam) {
  254. if ($arrParam=='') {
  255. $arrParam = $_POST[$this->name];
  256. }
  257. if (!isset($arrParam['disabled'])) {
  258. $arrParam['disabled'] = '';
  259. }
  260. print '<span id="container_input_'.$this->name.'"><input name="'.$this->name.'" id="'.$this->name.'" type="' . $this->fieldType . '" class="textfield" size="'.$this->size.'" value="'.$arrParam["value"].'" '.$arrParam["disabled"].' autocomplete="off" /></span>';
  261. print '<script type="text/javascript">
  262. $(\''.$this->name.'\').validate = function() {';
  263. if (!isset($arrParam["required"]))
  264. /* If a value is not required, and the input field is empty, that's ok */
  265. print '
  266. if ($(\''.$this->name.'\').value == \'\') { //if is empty (hidden value)
  267. return true
  268. }';
  269. if (false) print 'alert("' . $this->name . '");'; // Used for debug only
  270. print '
  271. var rege = '.$this->regexp.';
  272. if ((rege.exec($(\''.$this->name.'\').value))!=null) {
  273. $(\''.$this->name.'\').setStyle({backgroundColor: \'\'});
  274. return true;
  275. } else {
  276. $(\''.$this->name.'\').setStyle({backgroundColor: \'pink\'});
  277. new Element.scrollTo(\'container_input_'.$this->name.'\');
  278. return 0;
  279. }
  280. };';
  281. if (isset($arrParam["onchange"])) {
  282. print '$(\''.$this->name.'\').onchange = function() {' . $arrParam["onchange"] . '};';
  283. }
  284. print '</script>';
  285. }
  286. }
  287. /**
  288. * password input template
  289. */
  290. class PasswordTpl extends InputTpl{
  291. function PasswordTpl($name, $regexp = '/.+/') {
  292. $this->InputTpl($name, $regexp);
  293. $this->fieldType = "password";
  294. }
  295. }
  296. /**
  297. * Input with IA5 string check. Lots of LDAP fields only accept IA5 strings.
  298. */
  299. class IA5InputTpl extends InputTpl {
  300. function IA5InputTpl($name) {
  301. $this->InputTpl($name, '/^[\x00-\x7f]*$/');
  302. }
  303. }
  304. /**
  305. * Input with IP address check
  306. */
  307. class IPInputTpl extends InputTpl {
  308. function IPInputTpl($name) {
  309. $this->InputTpl($name, '/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/');
  310. }
  311. }
  312. /**
  313. * Input with MAC address check
  314. */
  315. class MACInputTpl extends InputTpl {
  316. function MACInputTpl($name) {
  317. $this->InputTpl($name, '/^([0-9a-f]{2}:){5}[0-9a-f]{2}$/i');
  318. }
  319. }
  320. /**
  321. * Input with a check for a valid DNS domain
  322. * We allow up to 10 dots in domain name ! Should be enough ...
  323. */
  324. class DomainInputTpl extends InputTpl {
  325. function DomainInputTpl($name) {
  326. $this->InputTpl($name, '/^([a-z0-9][a-z0-9-]*[a-z0-9]\.){0,10}[a-z0-9][a-z0-9-]*[a-z0-9]$/');
  327. }
  328. }
  329. class MailInputTpl extends InputTpl {
  330. function MailInputTpl($name) {
  331. $this->InputTpl($name, '/^([A-Za-z0-9._%+\-]+@[A-Za-z0-9.\-]+\.[A-Za-z0-9]{2,}){0,1}$/');
  332. }
  333. }
  334. /**
  335. * Input with a check for a valid numeric value
  336. */
  337. class NumericInputTpl extends InputTpl {
  338. function NumericInputTpl($name) {
  339. $this->InputTpl($name, '/^[0-9]*$/');
  340. }
  341. }
  342. /**
  343. * simple add label with Hidden field
  344. */
  345. class HiddenTpl extends AbstractTpl{
  346. function HiddenTpl($name) {
  347. $this->name=$name;
  348. }
  349. /**
  350. * display input Element
  351. * $arrParam accept ["value"] to corresponding value
  352. */
  353. function display($arrParam = array()) {
  354. if (empty($arrParam)) $arrParam = $this->options;
  355. /* FIXME: ??? */
  356. if (($arrParam == '') && isset($_POST[$this->name])) {
  357. $arrParam = $_POST[$this->name];
  358. }
  359. if (!isset($arrParam["hide"])) print $arrParam['value'];
  360. print '<input type="hidden" value="'.$arrParam["value"].'" name="'.$this->name.'"/>';
  361. }
  362. }
  363. /**
  364. * date input template
  365. */
  366. class DateTpl extends InputTpl {
  367. function DateTpl($name) {
  368. $this->name = $name;
  369. }
  370. function display($arrParam) {
  371. print '<div id="div'.$this->name.'">';
  372. print '<table cellspacing="0">';
  373. $i = 0;
  374. foreach (array('year'=>array(_('Year: '), 4), 'month'=>array(_('Month: '), 2), 'day'=>array(_('Day: '), 2),
  375. 'hour'=>array(_('Hour: '), 2), 'min'=>array(_('Min.: '), 2), 'sec'=>array(_('Sec.: '), 2)) as $elem=>$a_params) {
  376. $e = new InputTpl($this->name.'_'.$elem); //, array('value'=>$arrParam[$elem]));
  377. $e->setSize($a_params[1]);
  378. print $a_params[0];
  379. $e->display(array('value'=>$arrParam[$elem], 'onchange'=>'
  380. var elem = document.getElementById("'.$this->name.'");
  381. var date = elem.value;
  382. var part = '.$i.';
  383. var value = document.getElementById("'.$this->name.'_'.$elem.'").value;
  384. var newdate = changePartDate(date, part, value);
  385. elem.value = newdate;
  386. '));
  387. $i += 1;
  388. }
  389. print '<input name="'.$this->name.'" id="'.$this->name.'" type="hidden" value="0000/00/00/00/00/00"/>';
  390. print '</table>';
  391. print '</div>';
  392. print '<script type="text/javascript">
  393. function changePartDate(date, part, value) {
  394. var re = new RegExp("/", "g");
  395. var adate = date.split(re);
  396. adate[part] = value;
  397. return adate.join("/");
  398. }
  399. </script>';
  400. }
  401. }
  402. /**
  403. * date input template
  404. */
  405. class DynamicDateTpl extends InputTpl {
  406. function DynamicDateTpl($name) {
  407. $this->name = $name;
  408. $this->size = "";
  409. $this->fieldType = "";
  410. }
  411. function display($arrParam) {
  412. $e = new InputTpl($this->name);
  413. if (!isset($GLOBALS["__JSCALENDAR_SOURCED__"])) { // to avoid double-sourcing
  414. $GLOBALS["__JSCALENDAR_SOURCED__"] = 1;
  415. print '
  416. <script type="text/javascript" src="graph/jscalendar/js/calendar_stripped.js"></script>
  417. <script type="text/javascript" src="graph/jscalendar/js/calendar-setup_stripped.js"></script>
  418. <style type="text/css">@import url("graph/jscalendar/css/calendar-blue.css");</style>
  419. <script type="text/javascript" src="graph/jscalendar/lang/calendar-en.js"></script>
  420. ';
  421. if (isset($_REQUEST["lang"])) { // EN calendar always read, as the next one may not exists
  422. $extention = substr($_REQUEST["lang"], 0, 2); // transpose LANG, f.ex. fr_FR => fr
  423. print '
  424. <script type="text/javascript" src="graph/jscalendar/lang/calendar-'.$extention.'.js"></script>
  425. ';
  426. }
  427. }
  428. print '
  429. <span id="container_input_'.$this->name.'">
  430. <input name="'.$this->name.'" id="'.$this->name.'" type="' . $this->fieldType . '" class="textfield" size="'.$this->size.'" value="'.$arrParam["value"].'" readonly=1 />
  431. <input
  432. type="image"
  433. style="vertical-align: bottom;"
  434. src="graph/jscalendar/img/calendar.png"
  435. id="'.$this->name.'_button"
  436. />
  437. ';
  438. // ugly gettext workaround
  439. if (isset($arrParam["ask_for_now"])) {
  440. print _("or <a href='#'");
  441. print " onclick='javascript:document.getElementById(\"".$this->name."\").";
  442. print _("value=\"now\";'>now</a>");
  443. } elseif (isset($arrParam["ask_for_never"])) {
  444. print _("or <a href='#'");
  445. print " onclick='javascript:document.getElementById(\"".$this->name."\").";
  446. print _("value=\"never\";'>never</a>");
  447. }
  448. print '
  449. </span>';
  450. print '
  451. <script type="text/javascript">
  452. Calendar.setup({
  453. inputField : "'.$this->name.'",
  454. ifFormat : "%Y-%m-%d %H:%M:00", // format of the input field
  455. showsTime : true,
  456. timeFormat : "24",
  457. button : "'.$this->name.'_button",
  458. firstDay : 1,
  459. weekNumbers : false
  460. });
  461. </script>';
  462. }
  463. }
  464. class MultipleInputTpl extends AbstractTpl {
  465. function MultipleInputTpl($name,$desc='', $new=false) {
  466. $this->name = $name;
  467. /*
  468. stripslashes is needed, because some characters may be backslashed
  469. when adding/removing an input field.
  470. */
  471. $this->desc = stripslashes($desc);
  472. $this->regexp = '/.*/';
  473. $this->new = $new;
  474. $this->tooltip = False;
  475. }
  476. function setRegexp($regexp) {
  477. $this->regexp = $regexp;
  478. }
  479. function display($arrParam) {
  480. print '<div id="'.$this->name.'">';
  481. print '<table cellspacing="0">';
  482. foreach ($arrParam as $key => $param) {
  483. $test = new DeletableTrFormElement($this->desc,
  484. new InputTpl($this->name.'['.$key.']',$this->regexp),
  485. array('key' => $key,
  486. 'name' => $this->name,
  487. 'new' => $this->new,
  488. "tooltip" => $this->tooltip
  489. )
  490. );
  491. $test->setCssError($this->name . $key);
  492. $test->display(array("value" => $param));
  493. }
  494. print '<tr><td width="40%" style="text-align:right;">';
  495. if (count($arrParam) == 0) {
  496. //if we got a tooltip, we show it
  497. if ($this->tooltip) {
  498. print "<a href=\"#\" class=\"tooltip\">".$this->desc."<span>".$this->tooltip."</span></a>";
  499. } else {
  500. print $this->desc;
  501. }
  502. }
  503. print '</td><td>';
  504. print '<input name="b'.$this->name.'" type="submit" class="btnPrimary" value="'._("Add").'" onclick="
  505. new Ajax.Updater(\''.$this->name.'\',\'includes/FormGenerator/MultipleInput.tpl.php\',
  506. { evalScripts: true, parameters: Form.serialize($(\'edit\'))+\'&amp;minputname='.$this->name.'&amp;desc='.urlencode($this->desc) . '&amp;regexp='.rawurlencode($this->regexp).'\' }); return false;"/>';
  507. print '</td></tr>';
  508. print '</table>';
  509. print '</div>';
  510. }
  511. function displayRo($arrParam) {
  512. print '<div id="'.$this->name.'">';
  513. print '<table>';
  514. foreach ($arrParam as $key => $param) {
  515. $test = new DeletableTrFormElement($this->desc,
  516. new InputTpl($this->name.'['.$key.']',$this->regexp),
  517. array('key'=>$key,
  518. 'name'=> $this->name)
  519. );
  520. $test->setCssError($this->name.$key);
  521. $test->displayRo(array("value"=>$param));
  522. }
  523. if (count($arrParam) == 0) {
  524. print '<tr><td width="40%" style="text-align:right;">';
  525. print $this->desc;
  526. print '</td><td>';
  527. print '</td></tr>';
  528. }
  529. print '</table>';
  530. print '</div>';
  531. }
  532. function displayHide($arrParam) {
  533. print '<div id="'.$this->name.'">';
  534. print '<table>';
  535. print '<tr><td width="40%" style="text-align:right;">'.$this->desc.'</td>';
  536. print '<td style="color: rgb(204, 0, 0);">' . _('unavailable') . '</td></tr>';
  537. print '</table>';
  538. print '<div style="display:none">';
  539. print '<table>';
  540. foreach ($arrParam as $key => $param) {
  541. $test = new DeletableTrFormElement($this->desc,
  542. new InputTpl($this->name.'['.$key.']',$this->regexp),
  543. array('key'=>$key, 'name'=> $this->name)
  544. );
  545. $test->setCssError($this->name.$key);
  546. $test->displayHide(array("value"=>$param));
  547. }
  548. if (count($arrParam) == 0) {
  549. print '<tr><td width="40%" style="text-align:right;">';
  550. print $this->desc;
  551. print '</td><td>';
  552. print '</td></tr>';
  553. }
  554. print '</table>';
  555. print '</div>';
  556. print '</div>';
  557. }
  558. }
  559. class MembersTpl extends AbstractTpl {
  560. function MembersTpl($name) {
  561. $this->name = $name;
  562. $this->titleLeft = "";
  563. $this->titleRight = "";
  564. }
  565. function setTitle($titleLeft, $titleRight) {
  566. $this->titleLeft = $titleLeft;
  567. $this->titleRight = $titleRight;
  568. }
  569. function display($arrParam) {
  570. if(is_array($arrParam['member']))
  571. $this->member = $arrParam['member'];
  572. else {
  573. echo 'MembersTpl: member is not an array.';
  574. return 1;
  575. }
  576. if(is_array($arrParam['available']))
  577. $this->available = $arrParam['available'];
  578. else {
  579. echo 'MembersTpl: available is not an array.';
  580. return 1;
  581. }
  582. echo '
  583. <table class="membersTpl">
  584. <tr>
  585. <td class="membersTplMembers">
  586. <h4>' . $this->titleLeft . '</h4>';
  587. if ($this->member) {
  588. foreach ($this->member as $id=>$name)
  589. echo '<input type="hidden" name="old_' . $this->name .'[]" value="' . $name . '" />';
  590. }
  591. else {
  592. echo '<input type="hidden" name="old_' . $this->name .'[]" value="" />';
  593. }
  594. echo '
  595. <select multiple size="15" class="list" name="' . $this->name .'[]" id="' . $this->name .'">';
  596. foreach ($this->member as $id=>$name)
  597. echo '<option value="' . $id . '">' . $name . '</option>';
  598. echo '
  599. </select>
  600. </td>
  601. <td class="membersTplSwitchs">
  602. <a href="#" onclick="switch_' . $this->name .'(\'available_'.$this->name.'\', \''.$this->name.'\'); event.returnValue=false; return false;">
  603. <img style="padding: 5px;" src="img/common/icn_arrowleft.gif" value="<--" />
  604. </a>
  605. <br/>
  606. <a href="#" onclick="switch_' . $this->name .'(\''.$this->name.'\', \'available_'.$this->name.'\'); event.returnValue=false; return false;">
  607. <img style="padding: 5px;" src="img/common/icn_arrowright.gif" value = "-->" />
  608. </a>
  609. </td>
  610. <td class="membersTplAvailable">
  611. <h4>' . $this->titleRight . '</h4>
  612. <select multiple size="15" class="list" name="available_' . $this->name .'[]" id="available_' . $this->name .'">';
  613. foreach ($this->available as $id=>$name)
  614. echo '<option value="' . $id . '">' . $name . '</option>';
  615. echo '
  616. </select>
  617. </td>
  618. </tr>
  619. </table>
  620. <script type="text/javascript">
  621. switch_' . $this->name .' = function(from, to) {
  622. var emptyOption = new Element("option", { value: "" }).update("");
  623. var toAdd = $$("#"+from+" option").findAll(function(e) {
  624. return e.selected;
  625. });
  626. var len = toAdd.length;
  627. for(var i=0; i<len; i++) {
  628. try {
  629. // If the first option is empty
  630. // and we are about to add one, remove it
  631. if ($(to).options[0] && $(to).options[0].value == "") {
  632. $(to).options.remove($(to).options[0]);
  633. }
  634. $(to).options.add(toAdd[i]);
  635. // always add an empty option if the select box
  636. // is empty in order to have
  637. // the select box in the $POST array
  638. if ($(from).options.length == 0) {
  639. $(from).options.add(emptyOption);
  640. }
  641. }
  642. catch(ex) {
  643. // For IE
  644. if ($(to).options[0] && $(to).options[0].value == "") {
  645. $(to).options.removeChild($(to).options[0]);
  646. }
  647. $(to).appendChild(toAdd[i]);
  648. if ($(from).options.length == 0) {
  649. $(from).options.appendChild(emptyOption);
  650. }
  651. }
  652. }
  653. };
  654. </script>';
  655. }
  656. function displayRo($arrParam) {
  657. if(is_array($arrParam['member']))
  658. $this->member = $arrParam['member'];
  659. else {
  660. echo 'MembersTpl: member is not an array.';
  661. return 1;
  662. }
  663. echo '<ul class="roACL">';
  664. foreach ($this->member as $id => $name) {
  665. echo '<input type="hidden" name="old_' . $this->name .'[]" value="' . $name . '" />';
  666. echo '<input type="hidden" name="' . $this->name .'[]" value="' . $name . '" />';
  667. echo '<li>' . $name . '</li>';
  668. }
  669. echo '</ul>';
  670. }
  671. function displayHide($arrParam) {
  672. if(is_array($arrParam['member']))
  673. $this->member = $arrParam['member'];
  674. else {
  675. echo 'MembersTpl: member is not an array.';
  676. return 1;
  677. }
  678. foreach ($this->member as $id => $name) {
  679. echo '<input type="hidden" name="old_' . $this->name .'[]" value="' . $name . '" />';
  680. echo '<input type="hidden" name="' . $this->name .'[]" value="' . $name . '" />';
  681. }
  682. echo '<div style="color: #C00;">' . _("unavailable") . '</div>';
  683. }
  684. }
  685. /**
  686. * display select html tags with specified
  687. * entry, autoselect.
  688. */
  689. class SelectItem extends AbstractTpl{
  690. var $elements; /**< list of all elements*/
  691. var $elementsVal; /**< list of elements values*/
  692. var $selected; /**< element who are selected*/
  693. var $id; /**< id for css property*/
  694. /**
  695. *constructor
  696. */
  697. function SelectItem($idElt, $jsFunc = null, $style = null) {
  698. $this->id=$idElt;
  699. $this->name=$idElt;
  700. $this->jsFunc = $jsFunc;
  701. $this->style = $style;
  702. $this->jsFuncParams = null;
  703. }
  704. function setJsFuncParams($params) {
  705. $this->jsFuncParams = $params;
  706. }
  707. function setElements($elt) {
  708. $this->elements= $elt;
  709. }
  710. function setElementsVal($elt) {
  711. $this->elementsVal= $elt;
  712. }
  713. function setSelected($elemnt) {
  714. $this->selected= $elemnt;
  715. }
  716. /**
  717. * $paramArray can be "null"
  718. */
  719. function displayContent($paramArray = null) {
  720. print $this->content_to_string($paramArray);
  721. }
  722. function content_to_string($paramArray = null) {
  723. if (!isset($this->elementsVal)) {
  724. $this->elementsVal = $this->elements;
  725. }
  726. // if value... set it
  727. if (isset($paramArray["value"])) {
  728. $this->setSelected($paramArray["value"]);
  729. }
  730. $ret = '';
  731. foreach ($this->elements as $key => $item) {
  732. if ($this->elementsVal[$key] == $this->selected) {
  733. $selected='selected="selected"';
  734. } else {
  735. $selected="";
  736. }
  737. $ret .= "\t<option value=\"".$this->elementsVal[$key]."\" $selected>$item</option>\n";
  738. }
  739. return $ret;
  740. }
  741. function display($paramArray = null) {
  742. print $this->to_string($paramArray);
  743. }
  744. function to_string($paramArray = null) {
  745. $ret = "<select";
  746. if ($this->style) {
  747. $ret .= " class=\"".$this->style."\"";
  748. }
  749. if ($this->jsFunc) {
  750. $ret .= " onchange=\"".$this->jsFunc."(";
  751. if ($this->jsFuncParams) {
  752. $ret .= implode(", ", $this->jsFuncParams);
  753. }
  754. $ret .= "); return false;\"";
  755. }
  756. $ret .= " name=\"".$this->id."\" id=\"".$this->id."\">\n";
  757. $ret .= $this->content_to_string($paramArray);
  758. $ret .= "</select>";
  759. return $ret;
  760. }
  761. }
  762. /**
  763. * Simple Form Template encapsulator
  764. *
  765. */
  766. class FormElement extends HtmlElement {
  767. var $template;
  768. var $desc;
  769. var $cssErrorName;
  770. var $tooltip;
  771. function FormElement($desc, $tpl, $extraInfo = array()) {
  772. $this->desc = $desc;
  773. $this->template = &$tpl;
  774. foreach ($extraInfo as $key => $value) {
  775. $this->template->$key = $value;
  776. }
  777. }
  778. function setCssError($name) {
  779. $this->cssErrorName=$name;
  780. }
  781. /**
  782. * display input Element
  783. * $arrParam accept ["value"] to corresponding value
  784. */
  785. function display($arrParam = array()) {
  786. if (empty($arrParam)) $arrParam = $this->options;
  787. $existACL=existAclAttr($this->template->name);
  788. //if not
  789. if (!$existACL) {
  790. $aclattrright="rw";
  791. $isAclattrright=true;
  792. } else {
  793. $aclattrright=(getAclAttr($this->template->name));
  794. $isAclattrright=$aclattrright!='';
  795. }
  796. //if correct acl and exist acl
  797. if ($isAclattrright) {
  798. //if read only
  799. if ($aclattrright=="ro") {
  800. $this->template->displayRo($arrParam);
  801. //if all right
  802. } else if ($aclattrright=="rw") {
  803. $this->template->display($arrParam);
  804. }
  805. //if no right at all
  806. } else {
  807. $this->template->displayHide($arrParam);
  808. }
  809. }
  810. function displayRo($arrParam) {
  811. $this->template->displayRo($arrParam);
  812. }
  813. function displayHide($arrParam) {
  814. $this->template->displayHide($arrParam);
  815. }
  816. }
  817. /**
  818. * display a tr html tag in a form
  819. * using corresponding template
  820. */
  821. class DeletableTrFormElement extends FormElement{
  822. var $template;
  823. var $desc;
  824. var $cssErrorName;
  825. function DeletableTrFormElement($desc,$tpl,$extraInfo = array()) {
  826. $this->desc=$desc;
  827. $this->template=&$tpl;
  828. foreach ($extraInfo as $key => $value) {
  829. $this->$key = $value;
  830. }
  831. }
  832. /**
  833. * display input Element
  834. * $arrParam accept ["value"] to corresponding value
  835. */
  836. function display($arrParam = array()) {
  837. if (empty($arrParam)) $arrParam = $this->options;
  838. if ($this->key==0) {
  839. $desc = $this->desc;
  840. } else {
  841. $desc = '';
  842. }
  843. // set hidden form with old_value for each DeletableTrFormElement field
  844. // set a random old_value if some field has been created
  845. if($this->new) {
  846. $old_value = uniqid();
  847. }
  848. else if(isset($arrParam["value"])) {
  849. $old_value = $arrParam["value"];
  850. }
  851. else {
  852. $old_value = "";
  853. }
  854. if(is_object($this->template)) {
  855. $field_name = $this->template->name;
  856. }
  857. else if(is_array($this->template)) {
  858. $field_name = $this->template["name"];
  859. }
  860. else {
  861. $field_name = "";
  862. }
  863. if ($field_name) {
  864. print '<input type="hidden" name="old_'.$field_name.'" value="'.$old_value.'" />';
  865. }
  866. print '<tr><td width="40%" ';
  867. print displayErrorCss($this->cssErrorName);
  868. print 'style = "text-align: right;">';
  869. //if we got a tooltip, we show it
  870. if ($this->tooltip) {
  871. print "<a href=\"#\" class=\"tooltip\">".$desc."<span>".$this->tooltip."</span></a>";
  872. } else {
  873. print $desc;
  874. }
  875. print '</td><td>';
  876. // reald field display
  877. parent::display($arrParam);
  878. print '<input name="bdel" type="submit" class="btnSecondary" value="'._("Delete").'" onclick="
  879. new Ajax.Updater(\''.$this->name.'\',\'includes/FormGenerator/MultipleInput.tpl.php\',
  880. { evalScripts: true, parameters: Form.serialize($(\'edit\'))+\'&amp;minputname='.$this->name.'&amp;del='.$this->key.'&amp;desc='.urlencode($this->desc) . '&amp;regexp='.rawurlencode($this->template->regexp) . '\' }); return false;"/>';
  881. print '</td></tr>';
  882. }
  883. function displayRo($arrParam) {
  884. if ($this->key==0) {
  885. $desc = $this->desc;
  886. }
  887. print '<tr><td width="40%" ';
  888. print displayErrorCss($this->cssErrorName);
  889. print 'style = "text-align: right;">';
  890. //if we got a tooltip, we show it
  891. if ($this->tooltip) {
  892. print "<a href=\"#\" class=\"tooltip\">".$desc."<span>".$this->tooltip."</span></a>";
  893. } else {
  894. print $desc;
  895. }
  896. print '</td><td>';
  897. parent::displayRo($arrParam);
  898. print '</td></tr>';
  899. }
  900. }
  901. /**
  902. * display a tr html tag in a form
  903. * using corresponding template
  904. */
  905. class TrFormElement extends FormElement {
  906. var $template;
  907. var $desc;
  908. var $cssErrorName;
  909. function TrFormElement($desc, $tpl, $extraInfo = array()) {
  910. $this->desc=$desc;
  911. $this->template=&$tpl;
  912. $this->tooltip = False;
  913. $this->firstColWidth = "40%";
  914. $this->style = null; /* css style */
  915. $this->class = null; /* html class for the tr element */
  916. foreach ($extraInfo as $key => $value) {
  917. $this->$key = $value;
  918. }
  919. }
  920. /**
  921. * display input Element
  922. * $arrParam accept ["value"] to corresponding value
  923. */
  924. function display($arrParam = array()) {
  925. if (empty($arrParam)) $arrParam = $this->options;
  926. if (!isset($this->cssErrorName)) $this->cssErrorName = $this->template->name;
  927. printf('<tr');
  928. if ($this->class !== null)
  929. printf(' class="%s"', $this->class);
  930. if ($this->style !== null)
  931. printf(' style="%s"', $this->style);
  932. printf('><td class="label" width="%s" ', $this->firstColWidth);
  933. print displayErrorCss($this->cssErrorName);
  934. print 'style = "text-align: right;">';
  935. //if we got a tooltip, we show it
  936. if ($this->tooltip) {
  937. print "<a href=\"#\" class=\"tooltip\">".$this->desc."<span>".$this->tooltip."</span></a>";
  938. } else {
  939. print $this->desc;
  940. }
  941. print '</td><td>';
  942. // set hidden form with old_value for each TrFormElement field
  943. if(isset($arrParam["value"])) {
  944. // if checkbox
  945. if ($arrParam["value"] == "checked")
  946. $old_value = "on";
  947. else
  948. $old_value = $arrParam["value"];
  949. }
  950. else {
  951. $old_value = "";
  952. }
  953. if(is_object($this->template)) {
  954. $field_name = $this->template->name;
  955. }
  956. else if(is_array($this->template)) {
  957. $field_name = $this->template["name"];
  958. }
  959. else {
  960. $field_name = "";
  961. }
  962. if ($field_name && is_string($old_value)) {
  963. print '<input type="hidden" name="old_'.$field_name.'" value="'.$old_value.'" />';
  964. }
  965. // display real field
  966. parent::display($arrParam);
  967. if (isset($arrParam["extra"])) {
  968. print "&nbsp;" . $arrParam["extra"];
  969. }
  970. print "</td></tr>";
  971. }
  972. function displayRo($arrParam) {
  973. printf('<tr><td width="%s" ', $this->firstColWidth);
  974. print displayErrorCss($this->cssErrorName);
  975. print 'style = "text-align: right;">';
  976. //if we got a tooltip, we show it
  977. if ($this->tooltip) {
  978. print "<a href=\"#\" class=\"tooltip\">".$this->desc."<span>".$this->tooltip."</span></a>";
  979. } else {
  980. print $this->desc;
  981. }
  982. print '</td><td>';
  983. parent::displayRo($arrParam);
  984. print '</td></tr>';
  985. }
  986. function setClass($className) {
  987. $this->class = $className;
  988. }
  989. function getClass() {
  990. return $this->class;
  991. }
  992. function setStyle($style) {
  993. $this->style = $style;
  994. }
  995. }
  996. ?>