PageRenderTime 49ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 1ms

/core/web/includes/PageGenerator.php

https://github.com/vmasilva/mmc
PHP | 2653 lines | 1868 code | 271 blank | 514 comment | 255 complexity | a01120f2c9a1486fc1cd1071a8463751 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-2010 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, see <http://www.gnu.org/licenses/>.
  22. */
  23. require ("FormGenerator.php");
  24. /**
  25. * return an uniqId (use full for javascript auto generation
  26. */
  27. function getUniqId() {
  28. global $__uniqID;
  29. $__uniqID++;
  30. return $__uniqID;
  31. }
  32. /**
  33. * can echo obj and string with the same function
  34. * similar to "echo" in PHP5
  35. */
  36. function echo_obj($obj) {
  37. if (is_object($obj)){
  38. echo $obj->__toString();
  39. }
  40. else if(is_bool($obj)) {
  41. if($obj)
  42. echo '<img src="img/common/icn_yes.gif" alt="yes" />';
  43. }
  44. else {
  45. echo ($obj);
  46. }
  47. }
  48. /**
  49. * class for action encapsulation
  50. * Abstract class for the moment
  51. * @see EditInPlace
  52. */
  53. class ActionEncapsulator {
  54. function ActionEncapsulator() {
  55. }
  56. function __toString() {
  57. return "default action encapsulator";
  58. }
  59. }
  60. /**
  61. * AutoGenerate an EditInPlace text
  62. * based on scriptaculous javascript
  63. */
  64. class EditInPlace extends ActionEncapsulator{
  65. var $origText;
  66. var $url;
  67. var $param;
  68. function EditInPlace($origText,$url,$param) {
  69. $this->origText = $origText;
  70. $this->url = $url;
  71. $this->param = $param;
  72. }
  73. function __toString() {
  74. $param = array();
  75. foreach ($this->param as $key=>$value) {
  76. $param[] = "$key=$value";
  77. }
  78. $urlparam = implode("&",$param);
  79. if ($this->origText== '') {
  80. $this->origText= "n/a";
  81. }
  82. $idx = getUniqId();
  83. $str = '';
  84. $str.= "<span id=\"id$idx\" class=\"editinplace\">".$this->origText."</span>";
  85. $str .= '<script type="text/javascript">';
  86. $str .= " new Ajax.InPlaceEditor($('id$idx'),'".$this->url."', {\n
  87. okButton: true, cancelLink: true, cancelText : '"._('Cancel')."',
  88. highlightcolor : '#FF9966',
  89. ajaxOptions: {method: 'get' },\n
  90. callback: function(form,value) {\n
  91. return '$urlparam&value='+value\n
  92. }\n
  93. });\n
  94. </script>\n";
  95. return $str;
  96. }
  97. }
  98. /**
  99. * class for action in various application
  100. */
  101. class ActionItem {
  102. var $desc;
  103. var $action;
  104. var $classCss;
  105. var $paramString;
  106. var $mod;
  107. /**
  108. * Constructor
  109. * @param $desc description
  110. * @param $action string include in the url
  111. * @param $classCss class for CSS like "supprimer" or other class define
  112. * in the CSS global.css
  113. * @param $paramString add "&$param=" at the very end of the url
  114. */
  115. function ActionItem($desc, $action, $classCss, $paramString, $module = null, $submod = null, $tab = null, $mod = false) {
  116. $this->desc=$desc;
  117. $this->action=$action;
  118. $this->classCss=$classCss;
  119. $this->paramString=$paramString;
  120. if ($module == null) $this->module = $_GET["module"];
  121. else $this->module = $module;
  122. if ($submod == null) $this->submod = $_GET["submod"];
  123. else $this->submod = $submod;
  124. $this->tab = $tab;
  125. $this->mod = $mod;
  126. $this->path = $this->module . "/" . $this->submod . "/" . $this->action;
  127. if ($this->tab != null) {
  128. $this->path .= "/" . $this->tab;
  129. }
  130. }
  131. /**
  132. * display a link for the action
  133. * @param add &$this->param=$param at the very end of the url
  134. * display "displayWithRight" if you have correct right
  135. */
  136. function display($param, $extraParams = array()) {
  137. if (hasCorrectAcl($this->module,$this->submod,$this->action)) {
  138. $this->displayWithRight($param, $extraParams);
  139. } else {
  140. $this->displayWithNoRight($param, $extraParams);
  141. }
  142. }
  143. /**
  144. * display function if you have correct right on this action
  145. */
  146. function displayWithRight($param, $extraParams = array()) {
  147. /* add special param for actionItem */
  148. if (is_array($extraParams)) {
  149. $extraParams['mod'] = $this->mod;
  150. }
  151. echo "<li class=\"".$this->classCss."\">";
  152. if (is_array($extraParams) & !empty($extraParams)) $urlChunk = $this->buildUrlChunk($extraParams);
  153. else $urlChunk = "&amp;" . $this->paramString."=" . rawurlencode($extraParams);
  154. echo "<a title=\"".$this->desc."\" href=\"" . urlStr($this->path) . $urlChunk . "\">&nbsp;</a>";
  155. echo "</li>";
  156. }
  157. /**
  158. * display function if you don't have the right for this action
  159. */
  160. function displayWithNoRight($param, $extraParams = array()) {
  161. echo "<li class=\"".$this->classCss."\" style=\"opacity: 0.30;\">";
  162. echo "<a title=\"".$this->desc."\" href=\"#\" onclick='return false;'>&nbsp;</a>";
  163. echo "</li>";
  164. }
  165. /**
  166. * transform $obj param in link for this action
  167. */
  168. function encapsulate($obj, $extraParams = Array()) {
  169. if (hasCorrectAcl($this->module,$this->submod,$this->action)) {
  170. if (is_array($extraParams) & !empty($extraParams)) {
  171. $urlChunk = $this->buildUrlChunk($extraParams);
  172. } else {
  173. $urlChunk = "&amp;" . $this->paramString."=" . rawurlencode($obj);
  174. }
  175. $str= "<a title=\"".$this->desc."\" href=\"main.php?module=".$this->module."&amp;submod=".$this->submod."&amp;action=".$this->action . $urlChunk ."\">";
  176. $str.= trim($obj);
  177. $str.= "</a>";
  178. return $str;
  179. } else {
  180. $str= "<a title=\"".$this->desc."\" href=\"#\">";
  181. $str.= "$obj";
  182. $str.=" </a>";
  183. return $str;
  184. }
  185. }
  186. /**
  187. * Build an URL chunk using a array of option => value
  188. */
  189. function buildUrlChunk($arr) {
  190. $urlChunk = "";
  191. foreach($arr as $option => $value) {
  192. $urlChunk .= "&amp;" . $option . "=" . urlencode($value);
  193. }
  194. return $urlChunk;
  195. }
  196. /**
  197. * display help (not use for the moment)
  198. */
  199. function strHelp() {
  200. $str = "";
  201. $str.= "<li class=\"".$this->classCss."\">";
  202. $str.= "<a title=\"".$this->desc."\" href=\"#\">";
  203. $str.= " </a>".$this->desc."</li>";
  204. return $str;
  205. }
  206. }
  207. /**
  208. * display action in a JavaScript popup
  209. *
  210. * @see ActionItem
  211. * @see showPopup (js)
  212. */
  213. class ActionPopupItem extends ActionItem {
  214. function ActionPopupItem($desc, $action, $classCss, $paramString, $module = null, $submod = null, $tab = null, $width = 300, $mod = false) {
  215. $this->ActionItem($desc, $action, $classCss, $paramString, $module, $submod, $tab, $mod);
  216. $this->setWidth($width);
  217. }
  218. /**
  219. * Set the JavaScript popup width.
  220. * The default width value is 300px.
  221. */
  222. function setWidth($width) {
  223. $this->width = $width;
  224. }
  225. function displayWithRight($param, $extraParams = array()) {
  226. /* Add special param for actionPopupItem */
  227. $extraParams['mod'] = $this->mod;
  228. if (is_array($extraParams) & !empty($extraParams)) {
  229. $urlChunk = $this->buildUrlChunk($extraParams);
  230. } else {
  231. $urlChunk = "&amp;" . $this->paramString."=" . rawurlencode($param);
  232. }
  233. echo "<li class=\"".$this->classCss."\">";
  234. echo "<a title=\"".$this->desc."\" href=\"main.php?module=".$this->module."&amp;submod=".$this->submod."&amp;action=" . $this->action . $urlChunk . "\"";
  235. echo " onclick=\"PopupWindow(event,'main.php?module=".$this->module."&amp;submod=".$this->submod."&amp;action=" .$this->action . $urlChunk . "', " . $this->width . "); return false;\">&nbsp;</a>";
  236. echo "</li>";
  237. }
  238. function encapsulate($obj, $extraParams = array()) {
  239. if (is_array($extraParams) & !empty($extraParams)) {
  240. $urlChunk = $this->buildUrlChunk($extraParams);
  241. } else {
  242. $urlChunk = "&amp;" . $this->paramString."=" . rawurlencode($obj);
  243. }
  244. $str= "<a title=\"".$this->desc."\" href=\"main.php?module=".$this->module."&amp;submod=".$this->submod."&amp;action=".$this->action . $urlChunk . "\" ";
  245. $str.= " onclick=\"showPopup(event,'main.php?module=".$this->module."&amp;submod=".$this->submod."&amp;action=".$this->action . $urlChunk . "', " . $this->width . "); return false;\">";
  246. $str.= "$obj";
  247. $str.=" </a>";
  248. return $str;
  249. }
  250. }
  251. class EmptyActionItem extends ActionItem {
  252. function EmptyActionItem() {
  253. }
  254. function display($param = null) {
  255. print '<li class="empty"><a href="#" onclick="return false;">&nbsp;</a></li>';
  256. }
  257. }
  258. /**
  259. * class who maintain array presentation of information
  260. */
  261. class ListInfos extends HtmlElement {
  262. var $arrInfo; /**< main list */
  263. var $extraInfo;
  264. var $paramInfo;
  265. var $name;
  266. var $arrAction; /**< list of possible action */
  267. var $end, $start;
  268. var $description; /**< list of description (not an obligation) */
  269. var $col_width; /**< Contains the columns width */
  270. var $tooltip; /**< Contains the tooltip for column label */
  271. /**
  272. * constructor
  273. * @param $tab must be an array of array
  274. */
  275. function ListInfos($tab, $description ="", $extranavbar = "") {
  276. $this->arrInfo=$tab;
  277. $this->arrAction=array();
  278. $this->description[] = $description;
  279. $this->extranavbar = $extranavbar;
  280. $this->initVar();
  281. $this->col_width = array();
  282. $this->tooltip = array();
  283. $this->tooltip[] = "";
  284. $this->firstColumnActionLink = True;
  285. $this->_addInfo = array();
  286. }
  287. function setAdditionalInfo($addinfo) {
  288. $this->_addInfo = $addinfo;
  289. }
  290. /**
  291. * Set the number of rows to display per ListInfos page.
  292. * It overrides the default value defined by $conf["global"]["maxperpage"].
  293. *
  294. * @param $value The number of rows
  295. */
  296. function setRowsPerPage($value) {
  297. $this->end = $value;
  298. }
  299. /**
  300. * add an ActionItem
  301. * @param $objActionItem object ActionItem
  302. */
  303. function addActionItem($objActionItem) {
  304. $this->arrAction[] = &$objActionItem;
  305. }
  306. /**
  307. * Add an array of ActionItem
  308. * Useful if all action items are not the same for each row of the list
  309. *
  310. */
  311. function addActionItemArray($objActionItemArray) {
  312. assert(is_array($objActionItemArray));
  313. $this->arrAction[] = &$objActionItemArray;
  314. }
  315. /**
  316. * add an array String to display
  317. * @param $arrString an Array String to display
  318. * @param description Table column name
  319. * @param width Table column width
  320. * @param tooltip Tooltip to display on the column name
  321. */
  322. function addExtraInfo($arrString, $description= "", $width="", $tooltip = "") {
  323. assert(is_array($arrString));
  324. $this->extraInfo[] = &$arrString;
  325. $this->description[] = $description;
  326. $this->col_width[] = $width;
  327. $this->tooltip[] = $tooltip;
  328. }
  329. /**
  330. * set parameters array for main action
  331. * @param $arrString an Array of string to be used as parameters for the main action
  332. */
  333. function setParamInfo($arrString) {
  334. assert(is_array($arrString));
  335. $this->paramInfo = $arrString;
  336. }
  337. /**
  338. * Set the left padding of the table header.
  339. * It will be set to 32 by default
  340. * @param $padding an integer
  341. */
  342. function setTableHeaderPadding($padding) {
  343. $this->first_elt_padding = $padding;
  344. }
  345. /**
  346. * Disable the link to the first available action in the table
  347. * This link is always done by default
  348. */
  349. function disableFirstColumnActionLink() {
  350. $this->firstColumnActionLink = False;
  351. }
  352. /**
  353. * init class' vars
  354. */
  355. function initVar() {
  356. $this->name="Elements";
  357. global $conf;
  358. $this->maxperpage = (isset($_REQUEST['maxperpage'])) ? $_REQUEST['maxperpage'] : $conf['global']['maxperpage'];
  359. if (!isset($_GET["start"])) {
  360. if (!isset($_POST["start"])) {
  361. $this->start = 0;
  362. if (count($this->arrInfo) > 0) {
  363. $this->end = $this->maxperpage - 1;
  364. } else {
  365. $this->end = 0;
  366. }
  367. }
  368. } else {
  369. $this->start = $_GET["start"];
  370. $this->end = $_GET["end"];
  371. }
  372. /* Set a basic navigation bar */
  373. $this->setNavBar(new SimpleNavBar($this->start, $this->end, count($this->arrInfo), $this->extranavbar));
  374. }
  375. /**
  376. * set the name of the array (for CSS)
  377. */
  378. function setName($name) {
  379. $this->name=$name;
  380. }
  381. /**
  382. * set the cssclass of a row
  383. */
  384. function setCssClass($name) {
  385. $this->cssClass=$name;
  386. }
  387. /**
  388. * set a cssclass for each row
  389. */
  390. function setCssClasses($a_names) {
  391. $this->cssClasses = $a_names;
  392. }
  393. /**
  394. * Set the ListInfos navigation bar
  395. */
  396. function setNavBar($navbar) {
  397. $this->navbar = $navbar;
  398. }
  399. /**
  400. *
  401. * Display the widget navigation bar if $navbar is True
  402. *
  403. * @param $navbar: if $navbar is true the navigation bar is displayed
  404. */
  405. function displayNavbar($navbar) {
  406. if ($navbar) $this->navbar->display();
  407. }
  408. /**
  409. * draw number of page etc...
  410. */
  411. function drawHeader($navbar = 1) {
  412. $this->displayNavbar($navbar);
  413. echo "<p class=\"listInfos\">";
  414. /*
  415. * Management of the numbers "start" and "end" to display depending on the maxperpage set in the selector
  416. * These numbers are more user-friendly and do not begin with 0
  417. */
  418. echo $this->name." <strong>".min($this->start + 1, count($this->arrInfo)) . "</strong>\n ";
  419. echo _("to")." <strong>".min($this->end + 1, count($this->arrInfo))."</strong>\n";
  420. printf (_(" - Total <b>%s </b>")."\n",count($this->arrInfo));
  421. /* Display page counter only when possible */
  422. if ($this->maxperpage >= ($this->end - $this->start)) {
  423. echo "("._("page")." ";
  424. printf("%.0f", ($this->end + 1) / $this->maxperpage);
  425. echo " / ";
  426. $pages = intval((count($this->arrInfo) / $this->maxperpage)) ;
  427. if ((count($this->arrInfo) % $this->maxperpage > 0) && (count($this->arrInfo) > $this->maxperpage))
  428. $pages++;
  429. else if ((count($this->arrInfo) > 0) && ($pages < 1))
  430. $pages = 1;
  431. else if ($pages < 0)
  432. $pages = 0;
  433. printf("%.0f", $pages);
  434. echo ")\n";
  435. }
  436. echo "</p>";
  437. }
  438. /**
  439. * display main action (first action
  440. */
  441. function drawMainAction($idx) {
  442. if (!empty($this->cssClass)) {
  443. echo "<td class=\"".$this->cssClass."\">";
  444. } else {
  445. echo "<td>";
  446. }
  447. if (is_a($this->arrAction[0], 'ActionItem')) {
  448. $firstAction = $this->arrAction[0];
  449. } else if (is_array($this->arrAction[0])) {
  450. $firstAction = $this->arrAction[0][$idx];
  451. }
  452. echo $firstAction->encapsulate($this->arrInfo[$idx], $this->paramInfo[$idx]);
  453. if (isset($this->_addInfo[$idx])) {
  454. print " " . $this->_addInfo[$idx];
  455. }
  456. echo "</td>";
  457. }
  458. function drawTable($navbar = 1) {
  459. echo "<table border=\"1\" cellspacing=\"0\" cellpadding=\"5\" class=\"listinfos\">\n";
  460. echo "<thead><tr>";
  461. $first = False;
  462. foreach ($this->description as $key => $desc) {
  463. if (isset($this->col_width[$key])) {
  464. $width_styl = 'width: '.$this->col_width[$key].';';
  465. } else {
  466. $width_styl = '';
  467. }
  468. if (!$first) {
  469. if (!isset($this->first_elt_padding)) {
  470. $this->first_elt_padding = 32;
  471. }
  472. echo "<td style=\"$width_styl\"><span style=\"color: #777; padding-left: ".$this->first_elt_padding."px;\">$desc</span></td>";
  473. $first = True;
  474. } else {
  475. /* Draw table header line */
  476. /* Add a tooltip to the column name if there is one set */
  477. if (!empty($this->tooltip[$key])) {
  478. $tooltipbegin = "<a href=\"#\" class=\"tooltip\">";
  479. $tooltipend = "<span>" . $this->tooltip[$key] . "</span></a>";
  480. } else {
  481. $tooltipbegin = "";
  482. $tooltipend = "";
  483. }
  484. echo "<td style=\"$width_styl\"><span style=\"color: #777;\">$tooltipbegin$desc$tooltipend</span></td>";
  485. }
  486. }
  487. if (count($this->arrAction)!=0) { //if we have actions
  488. if (!empty($this->col_width)) {
  489. $width_styl = $this->col_width[count($this->col_width) - 1];
  490. }
  491. $width_styl = isset($width_styl) ? sprintf('width: %s;', $width_styl) : '';
  492. echo "<td style=\"text-align: right; $width_styl\"><span style=\"color: #AAA;\" >Actions</span></td>";
  493. }
  494. echo "</tr></thead>";
  495. for ( $idx = $this->start; ($idx < count($this->arrInfo)) && ($idx <= $this->end); $idx++) {
  496. if (($this->start - $idx) % 2) {
  497. echo "<tr";
  498. if (!empty($this->cssClasses[$idx])) {
  499. echo " class=\"".$this->cssClasses[$idx]."\"";
  500. }
  501. echo ">";
  502. } else {
  503. echo "<tr class=\"alternate";
  504. if (!empty($this->cssClasses[$idx])) {
  505. echo " ".$this->cssClasses[$idx];
  506. }
  507. echo "\">";
  508. }
  509. //link to first action (if we have an action)
  510. if (count($this->arrAction) && $this->firstColumnActionLink) {
  511. $this->drawMainAction($idx);
  512. } else {
  513. if (!empty($this->cssClass)) {
  514. echo "<td class=\"".$this->cssClass."\">";
  515. } else {
  516. echo "<td>";
  517. }
  518. echo $this->arrInfo[$idx];
  519. echo "</td>";
  520. }
  521. if ($this->extraInfo)
  522. foreach ($this->extraInfo as $arrayTMP) {
  523. echo "<td>";
  524. if(trim($arrayTMP[$idx]) != "") {
  525. echo_obj($arrayTMP[$idx]);
  526. }
  527. else {
  528. echo "&nbsp;";
  529. }
  530. echo "</td>";
  531. }
  532. if (count($this->arrAction)!=0) {
  533. echo "<td class=\"action\">";
  534. echo "<ul class=\"action\">";
  535. foreach ($this->arrAction as $objActionItem) {
  536. if (is_a($objActionItem, 'ActionItem')) {
  537. $objActionItem->display($this->arrInfo[$idx], $this->paramInfo[$idx]);
  538. } else if (is_array($objActionItem)) {
  539. $obj = $objActionItem[$idx];
  540. $obj->display($this->arrInfo[$idx], $this->paramInfo[$idx]);
  541. }
  542. }
  543. echo "</ul>";
  544. echo "</td>";
  545. }
  546. echo "</tr>\n";
  547. }
  548. echo "</table>\n";
  549. $this->displayNavbar($navbar);
  550. if (false) {
  551. /* Code disabled because not used and make javavascript errors */
  552. print '<script type="text/javascript"><!--';
  553. print '$(\'help\').innerHTML=\'\''."\n";
  554. print '$(\'help\').innerHTML+=\'<ul>\''."\n";
  555. print '$(\'help\').innerHTML+=\'<li><h3>Aide contextuelle</h3></li>\''."\n";
  556. foreach ($this->arrAction as $objActionItem) {
  557. $content = $objActionItem->strHelp();
  558. print '$(\'help\').innerHTML+=\''.$content.'\';'."\n";
  559. }
  560. print '$(\'help\').innerHTML+=\'</ul>\''."\n";
  561. print '--></script>';
  562. }
  563. }
  564. function display($navbar = 1, $header =1 ) {
  565. if (!isset($this->paramInfo)) {
  566. $this->paramInfo = $this->arrInfo;
  567. }
  568. if ($header == 1) {
  569. $this->drawHeader($navbar);
  570. }
  571. $n = new NotifyWidget();
  572. if (isset($_SESSION['__notify'])) {
  573. $n->showJS();
  574. }
  575. $this->drawTable($navbar);
  576. }
  577. }
  578. /**
  579. * A modified version of Listinfos
  580. */
  581. class OptimizedListInfos extends ListInfos {
  582. /**
  583. * Allow to set another item count
  584. */
  585. function setItemCount($count) {
  586. $this->itemCount = $count;
  587. }
  588. function getItemCount() {
  589. return $this->itemCount;
  590. }
  591. /**
  592. * init class' vars
  593. */
  594. function initVar() {
  595. $this->name="Elements";
  596. global $conf;
  597. if (!isset($_GET["start"])) {
  598. if (!isset($_POST["start"])) {
  599. $this->start = 0;
  600. if (count($this->arrInfo) > 0) {
  601. $this->end = (isset($_REQUEST['maxperpage'])) ? ($_REQUEST['maxperpage'] - 1) : ($conf["global"]["maxperpage"] - 1);
  602. } else {
  603. $this->end = 0;
  604. }
  605. }
  606. } else {
  607. $this->start = $_GET["start"];
  608. $this->end = $_GET["end"];
  609. }
  610. $this->maxperpage = (isset($_REQUEST['maxperpage'])) ? $_REQUEST['maxperpage'] : $conf["global"]["maxperpage"];
  611. $this->setItemCount(count($this->arrInfo));
  612. $this->startreal = $this->start;
  613. $this->endreal = $this->end;
  614. }
  615. /**
  616. * draw number of page etc...
  617. */
  618. function drawHeader($navbar=1) {
  619. $count = $this->getItemCount();
  620. $this->displayNavbar($navbar);
  621. echo "<p class=\"listInfos\">";
  622. /*
  623. * Management of the numbers "start" and "end" to display depending on the maxperpage set in the selector
  624. * These numbers are more user-friendly and do not begin with 0
  625. */
  626. echo $this->name." <strong>".min($this->startreal + 1, $count) . "</strong>\n ";
  627. echo _("to")." <strong>".min($this->endreal + 1, $count)."</strong>\n";
  628. printf (_(" - Total <b>%s </b>")."\n", $count);
  629. /* Display page counter only when possible */
  630. if ($this->maxperpage >= ($this->endreal - $this->startreal)) {
  631. echo "("._("page")." ";
  632. printf("%.0f", ($this->endreal + 1) / $this->maxperpage);
  633. echo " / ";
  634. $pages = intval(($count / $this->maxperpage)) ;
  635. if (($count % $this->maxperpage > 0) && ($count > $this->maxperpage))
  636. $pages++;
  637. else if (($count > 0) && ($pages < 1))
  638. $pages = 1;
  639. else if ($pages < 0)
  640. $pages = 0;
  641. printf("%.0f", $pages);
  642. echo ")\n";
  643. }
  644. echo "</p>";
  645. }
  646. }
  647. /**
  648. * specific class for UserDisplay
  649. */
  650. class UserInfos extends OptimizedListInfos {
  651. var $css = array(); //css for first column
  652. function drawMainAction($idx) {
  653. echo "<td class=\"".$this->css[$idx]."\">";
  654. echo $this->arrAction[0]->encapsulate($this->arrInfo[$idx], $this->paramInfo[$idx]);
  655. echo "</td>";
  656. }
  657. }
  658. /**
  659. *
  660. * Display a previous/next navigation bar for ListInfos widget
  661. *
  662. */
  663. class SimpleNavBar extends HtmlElement {
  664. /**
  665. * @param $curstart: the first item index to display
  666. * @param $curent: the last item index
  667. * @param $itemcount: total number of item
  668. * @param $filter: the current list filter
  669. * @param $max: max quantity of elements in a page
  670. * @param $paginator: boolean which enable the selector of the number of results in a page
  671. */
  672. function SimpleNavBar($curstart, $curend, $itemcount, $extra = "", $max = "", $paginator = false) {
  673. global $conf;
  674. if(isset($max) && $max != "") {
  675. $this->max = $max;
  676. } else {
  677. $this->max = $conf["global"]["maxperpage"];
  678. }
  679. $this->curstart = $curstart;
  680. $this->curend = $curend;
  681. $this->itemcount = $itemcount;
  682. $this->extra = $extra;
  683. $this->paginator = $paginator;
  684. # number of pages
  685. $this->nbpages = ceil($this->itemcount / $this->max);
  686. # number of current page
  687. $this->curpage = floor(($this->curend + 1) / $this->max);
  688. }
  689. function display() {
  690. echo '<form method="post">';
  691. echo "<ul class=\"navList\">\n";
  692. if ($this->curstart == 0 || ($this->curstart - $this->max < 0))
  693. echo "<li class=\"previousListInactive\">"._("Previous")."</li>\n";
  694. else {
  695. $start = $this->curstart - $this->max;
  696. $end = $this->curstart - 1;
  697. echo "<li class=\"previousList\"><a href=\"".$_SERVER["SCRIPT_NAME"];
  698. /* FIXME: maybe we can get rid of $_GET["filter"] ? */
  699. printf("?module=%s&amp;submod=%s&amp;action=%s&amp;start=%d&amp;end=%d&amp;filter=%s%s", $_GET["module"],$_GET["submod"],$_GET["action"],$start, $end, $_GET["filter"], $this->extra);
  700. echo "\">"._("Previous")."</a></li>\n";
  701. }
  702. if($this->paginator) {
  703. // Display the maxperpage selector
  704. $this->displaySelectMax();
  705. }
  706. if (($this->curend + 1) >= $this->itemcount)
  707. echo "<li class=\"nextListInactive\">"._("Next")."</li>\n";
  708. else {
  709. $start = $this->curend + 1;
  710. $end = $this->curend + $this->max;
  711. echo "<li class=\"nextList\"><a href=\"".$_SERVER["SCRIPT_NAME"];
  712. printf("?module=%s&amp;submod=%s&amp;action=%s&amp;start=%d&amp;end=%d&amp;filter=%s%s", $_GET["module"],$_GET["submod"],$_GET["action"],$start, $end, $_GET["filter"], $this->extra);
  713. echo "\">"._("Next")."</a></li>\n";
  714. }
  715. if($this->paginator) {
  716. // Display a border at the left of the "Next" link
  717. $this->displayNextListBorder();
  718. }
  719. echo "</ul>\n";
  720. }
  721. /*
  722. * This function displays a selector to choose the maxperpage value
  723. * dynamically.
  724. * This is useful with AjaxNavBar
  725. * @param $jsfunc: optional javascript function which updates ListInfos
  726. */
  727. function displaySelectMax($jsfunc=null) {
  728. global $conf;
  729. echo '<span class="pagination">'._('Pagination').': ';
  730. if(isset($jsfunc)) {
  731. $start = $this->curstart;
  732. echo "<select id=\"maxperpage\" name=\"maxperpage\" onChange=\"updateMaxPerPage(this); return false;\" class=\"pagination\">";
  733. } else {
  734. echo "<select id=\"maxperpage\" name=\"maxperpage\" class=\"pagination\">";
  735. }
  736. /* Display the selector and each option of the array set in the config
  737. file */
  738. foreach($conf["global"]["pagination"] as $quantity) {
  739. $selected = '';
  740. if ($_REQUEST['maxperpage'] == $quantity)
  741. /* Set by default if already selected before */
  742. $selected = ' selected="selected"';
  743. echo "<option value=\"$quantity\"$selected>$quantity</option>";
  744. }
  745. echo "</select></span>";
  746. /*
  747. * Print the script which will launch an update of the ListInfos when
  748. * selectMax value changes.
  749. * It also synchronizes the value of the two selectors of the widget.
  750. * Then it calls the javascript function which do an AJAX update of
  751. * the ListInfos.
  752. */
  753. ?>
  754. <script type="javascript">
  755. updateMaxPerPage = function(elem) {
  756. // Get the selector element (the first of the page)
  757. var maxperpageElement = document.getElementById('maxperpage');
  758. if(maxperpageElement != undefined) {
  759. // Synchronize the value of the two selectors of the page :
  760. // the ListInfos widget print two Navbar, so the maxperpage value can be changed in both
  761. maxperpageElement.selectedIndex = elem.selectedIndex;
  762. var maxperpageValue = elem.value;
  763. // Evaluate the end depending on the maxperpage value selected
  764. var end = parseInt(maxperpageValue) + parseInt(<?php echo $start ?>) - 1;
  765. // Call the function to update the ListInfos
  766. <?php echo $jsfunc ?>('<?php echo $this->filter ?>', '<?php echo $start ?>', end);
  767. }
  768. return false;
  769. }
  770. </script>
  771. <?
  772. }
  773. /**
  774. * This function just print a script which add a border at the left of the "Next" link
  775. */
  776. function displayNextListBorder() {
  777. ?>
  778. <script type="javascript">
  779. var nextList = [];
  780. var nextListClass = "";
  781. if(document.getElementsByClassName("nextListInactive").length > 0) {
  782. nextListClass = "nextListInactive";
  783. }
  784. if(document.getElementsByClassName("nextList").length > 0) {
  785. nextListClass = "nextList";
  786. }
  787. if(nextListClass != "") {
  788. nextList = document.getElementsByClassName(nextListClass);
  789. nextList[0].style.borderLeft = "solid 1px #CCC";
  790. nextList[1].style.borderLeft = "solid 1px #CCC";
  791. }
  792. </script>
  793. <?
  794. }
  795. function displayGotoPageField() {
  796. echo '<script type="text/javascript">
  797. gotoPage = function(input) {
  798. page = input.value;
  799. if (page <= '.$this->nbpages.') {
  800. end = ('.$this->max.' * page);
  801. start = end - '.$this->max.';
  802. end -= 1;
  803. cur = ('.$this->curend.' + 1) / 10;
  804. '.$this->jsfunc.'("'.$this->filter.'", start, end, document.getElementById("maxperpage"));
  805. }
  806. }
  807. </script>';
  808. echo '<span class="pagination">'._("Go to page").': <input type="input" size="2" onchange="gotoPage(this)" /></span>';
  809. }
  810. function displayPagesNumbers() {
  811. # pages links
  812. # show all pages
  813. if ($this->nbpages <= 10) {
  814. if($this->nbpages > 1) {
  815. echo '<span class="pagination">';
  816. for($i=1; $i<=$this->nbpages; $i++) {
  817. echo $this->makePageLink($i);
  818. }
  819. echo '</span>';
  820. }
  821. }
  822. # show start/end pages and current page
  823. else {
  824. echo '<span class="pagination">';
  825. for($i=1; $i<=3; $i++) {
  826. echo $this->makePageLink($i);
  827. }
  828. if($this->curpage > 2 and $this->curpage < 5) {
  829. for($i=$this->curpage;$i<=$this->curpage+2;$i++) {
  830. if ($i > 3)
  831. echo $this->makePageLink($i);
  832. }
  833. }
  834. else if ($this->curpage > 4 and $this->curpage < $this->nbpages-3) {
  835. echo '.. ';
  836. for($i=$this->curpage-1;$i<=$this->curpage+1;$i++) {
  837. echo $this->makePageLink($i);
  838. }
  839. }
  840. echo '.. ';
  841. if ($this->curpage <= $this->nbpages-2 and $this->curpage >= $this->nbpages-3) {
  842. for($i=$this->nbpages-4; $i<=$this->nbpages-3; $i++) {
  843. echo $this->makePageLink($i);
  844. }
  845. }
  846. for($i=$this->nbpages-2; $i<=$this->nbpages; $i++) {
  847. echo $this->makePageLink($i);
  848. }
  849. echo '</span>';
  850. }
  851. }
  852. function makePageLink($page) {
  853. $end = ($this->max * $page);
  854. $start = $end - $this->max;
  855. $end -= 1;
  856. if ($page == $this->curpage) {
  857. return '<span>'.$page.'</span> ';
  858. }
  859. else {
  860. return '<a href="#" onclick="'.$this->jsfunc.'(\''.$this->filter.'\',\''.$start.'\',\''.$end.'\', document.getElementById(\'maxperpage\')); return false;">'.$page.'</a> ';
  861. }
  862. }
  863. }
  864. /**
  865. * Class which creates a SimpleNavBar with the paginator always enabled by
  866. * default
  867. */
  868. class SimplePaginator extends SimpleNavBar {
  869. /**
  870. * Just call the constructor of SimpleNavBar with "true" value for the
  871. * $paginator attribute
  872. *
  873. * @param $curstart: the first item index to display
  874. * @param $curent: the last item index
  875. * @param $itemcount: total number of item
  876. * @param $filter: the current list filter
  877. * @param $max: max quantity of elements in a page
  878. */
  879. function SimplePaginator($curstart, $curend, $itemcount, $extra = "", $max = "") {
  880. $this->SimpleNavBar($curstart, $curend, $itemcount, $extra, $max, true);
  881. }
  882. }
  883. /**
  884. * Display a previous/next navigation bar for ListInfos widget
  885. * The AjaxNavBar is useful when an Ajax Filter is set for a ListInfos widget
  886. */
  887. class AjaxNavBar extends SimpleNavBar {
  888. /**
  889. *
  890. * The AjaxNavBar start/end item are get from $_GET["start"] and
  891. * $_GET["end"]
  892. *
  893. * @param $itemcount: total number of item
  894. * @param $filter: the current list filter
  895. * @param $extra: extra URL parameter to pass the next/list button
  896. * @param $jsfunc: the name of the javascript function that applies the AJAX filter for the ListInfos widget
  897. * @param $max: the max number of elements to display in a page
  898. */
  899. function AjaxNavBar($itemcount, $filter, $jsfunc = "updateSearchParam", $max = "", $paginator = false) {
  900. global $conf;
  901. if (isset($_GET["start"])) {
  902. $curstart = $_GET["start"];
  903. $curend = $_GET["end"];
  904. } else {
  905. $curstart = 0;
  906. if ($itemcount > 0){
  907. if($max != "") {
  908. $curend = $max -1;
  909. } else {
  910. $curend = $conf["global"]["maxperpage"] - 1;
  911. }
  912. } else
  913. $curend = 0;
  914. }
  915. $this->SimpleNavBar($curstart, $curend, $itemcount, null, $max, $paginator);
  916. $this->filter = $filter;
  917. $this->jsfunc = $jsfunc;
  918. }
  919. function display() {
  920. echo '<form method="post">';
  921. echo "<ul class=\"navList\">\n";
  922. if($this->paginator) {
  923. // Display the maxperpage selector
  924. $this->displaySelectMax($this->jsfunc);
  925. }
  926. // show goto page field
  927. if ($this->nbpages > 20) {
  928. $this->displayGotoPageField();
  929. }
  930. # previous link
  931. if ($this->curstart == 0 || ($this->curstart - $this->max < 0))
  932. echo "<li class=\"previousListInactive\">" . _("Previous") . "</li>\n";
  933. else {
  934. $start = $this->curstart - $this->max;
  935. $end = $this->curstart - 1;
  936. echo "<li class=\"previousList\"><a href=\"#\" onClick=\"" . $this->jsfunc . "('" . $this->filter . "','$start','$end', document.getElementById('maxperpage')); return false;\">" . _("Previous") . "</a></li>\n";
  937. }
  938. // display pages numbers
  939. $this->displayPagesNumbers();
  940. # next link
  941. if (($this->curend + 1) >= $this->itemcount)
  942. echo "<li class=\"nextListInactive\">"._("Next")."</li>\n";
  943. else {
  944. $start = $this->curend + 1;
  945. $end = $this->curend + $this->max;
  946. echo "<li class=\"nextList\"><a href=\"#\" onClick=\"" . $this->jsfunc . "('" . $this->filter . "','$start','$end', document.getElementById('maxperpage')); return false;\">" . _("Next") . "</a></li>\n";
  947. }
  948. // Display a border at the left of the "Next" link
  949. if ($this->nbpages > 1) {
  950. $this->displayNextListBorder();
  951. }
  952. echo "</ul>\n";
  953. }
  954. }
  955. /**
  956. * Class which creates an AjaxNavBar with the paginator always enabled by
  957. * default
  958. */
  959. class AjaxPaginator extends AjaxNavBar {
  960. /**
  961. * Just call the constructor of AjaxNavBar with "true" value for the $paginator attribute
  962. *
  963. * @param $itemcount: total number of item
  964. * @param $filter: the current list filter
  965. * @param $jsfunc: the name of the javascript function that applies the AJAX filter for the ListInfos widget
  966. * @param $max: the max number of elements to display in a page
  967. */
  968. function AjaxPaginator($itemcount, $filter, $jsfunc = "updateSearchParam", $max = "") {
  969. $this->AjaxNavBar($itemcount, $filter, $jsfunc, $max, true);
  970. }
  971. }
  972. /**
  973. *
  974. * Create an AjaxFilter Form that updates a div according to an url output
  975. *
  976. */
  977. class AjaxFilter extends HtmlElement {
  978. /**
  979. * @param $url: URL called by the javascript updated. The URL gets the filter in $_GET["filter"]
  980. * @param $divid: div ID which is updated by the URL output
  981. * @param $formid: change the form id (usefull for multiple Ajaxfilter in one page)
  982. */
  983. function AjaxFilter($url, $divid = "container", $params = array(), $formid = "") {
  984. if (strpos($url, "?") === False)
  985. /* Add extra ? needed to build the URL */
  986. $this->url = $url . "?";
  987. else
  988. /* Add extra & needed to build the URL */
  989. $this->url = $url . "&";
  990. $this->divid = $divid;
  991. $this->formid = $formid;
  992. $this->refresh= 0;
  993. $this->params = '';
  994. foreach ($params as $k => $v) {
  995. $this->params .= "&".$k."=".$v;
  996. }
  997. // get the current module pages
  998. if (isset($_GET["module"]))
  999. $__module = $_GET["module"];
  1000. else
  1001. $__module = "default";
  1002. if (isset($_GET["submod"]))
  1003. $__submod = $_GET["submod"];
  1004. else
  1005. $__submod = "default";
  1006. if (isset($_GET["action"]))
  1007. $__action = $_GET["action"];
  1008. else
  1009. $__action = "default";
  1010. if (isset($_GET['tab']))
  1011. $__tab = $_GET['tab'];
  1012. else
  1013. $__tab = "default";
  1014. $extra = "";
  1015. foreach($_GET as $key => $value) {
  1016. if (!in_array($key, array('module', 'submod', 'tab', 'action', 'filter', 'start', 'end', 'maxperpage')))
  1017. $extra += $key + "_" + $value;
  1018. }
  1019. // then get our filter info
  1020. if(isset($_SESSION[$__module."_".$__submod."_".$__action."_".$__tab."_filter_".$extra])) {
  1021. $this->storedfilter = $_SESSION[$__module."_".$__submod."_".$__action."_".$__tab."_filter_".$extra];
  1022. }
  1023. if(isset($_SESSION[$__module."_".$__submod."_".$__action."_".$__tab."_maxperpage_".$extra])) {
  1024. $this->storedmax = $_SESSION[$__module."_".$__submod."_".$__action."_".$__tab."_maxperpage_".$extra];
  1025. }
  1026. if(isset($_SESSION[$__module."_".$__submod."_".$__action."_".$__tab."_start_".$extra])) {
  1027. $this->storedstart = $_SESSION[$__module."_".$__submod."_".$__action."_".$__tab."_start_".$extra];
  1028. }
  1029. if(isset($_SESSION[$__module."_".$__submod."_".$__action."_".$__tab."_end_".$extra])) {
  1030. $this->storedend = $_SESSION[$__module."_".$__submod."_".$__action."_".$__tab."_end_".$extra];
  1031. }
  1032. }
  1033. /**
  1034. * Allow the list to refresh
  1035. * @param $refresh: time in ms
  1036. */
  1037. function setRefresh($refresh) {
  1038. $this->refresh = $refresh;
  1039. }
  1040. function display() {
  1041. global $conf;
  1042. $root = $conf["global"]["root"];
  1043. $maxperpage = $conf["global"]["maxperpage"];
  1044. ?>
  1045. <form name="Form<?php echo $this->formid ?>" id="Form<?php echo $this->formid ?>" action="#" onsubmit="return false;">
  1046. <div id="loader<?php echo $this->formid ?>">
  1047. <img id="loadimg" src="<?php echo $root; ?>img/common/loader.gif" alt="loader" class="loader"/>
  1048. </div>
  1049. <div id="searchSpan<?php echo $this->formid ?>" class="searchbox" style="float: right;">
  1050. <img src="graph/search.gif" style="position:relative; top: 2px; float: left;" alt="search" /> <span class="searchfield"><input type="text" class="searchfieldreal" name="param" id="param<?php echo $this->formid ?>" onkeyup="pushSearch<?php echo $this->formid ?>(); return false;" />
  1051. <img src="graph/croix.gif" alt="suppression" style="position:relative; top : 3px;"
  1052. onclick="document.getElementById('param<?php echo $this->formid ?>').value =''; pushSearch<?php echo $this->formid ?>(); return false;" />
  1053. </span>
  1054. </div>
  1055. <script type="text/javascript">
  1056. <?
  1057. if(!$this->formid) {
  1058. ?>
  1059. document.getElementById('param<?php echo $this->formid ?>').focus();
  1060. <?
  1061. }
  1062. if(isset($this->storedfilter)) {
  1063. ?>
  1064. document.Form<?php echo $this->formid ?>.param.value = "<?php echo $this->storedfilter ?>";
  1065. <?
  1066. }
  1067. ?>
  1068. var refreshtimer<?php echo $this->formid ?> = null;
  1069. var refreshparamtimer<?php echo $this->formid ?> = null;
  1070. var refreshdelay<?php echo $this->formid ?> = <?php echo $this->refresh ?>;
  1071. var maxperpage = <?php echo $maxperpage ?>;
  1072. <?
  1073. if (isset($this->storedmax)) {
  1074. ?>
  1075. maxperpage = <?php echo $this->storedmax ?>;
  1076. <?
  1077. }
  1078. ?>
  1079. if(document.getElementById('maxperpage') != undefined)
  1080. maxperpage = document.getElementById('maxperpage').value;
  1081. /**
  1082. * Clear the timers set vith setTimeout
  1083. */
  1084. clearTimers<?php echo $this->formid ?> = function() {
  1085. if (refreshtimer<?php echo $this->formid ?> != null) {
  1086. clearTimeout(refreshtimer<?php echo $this->formid ?>);
  1087. }
  1088. if (refreshparamtimer<?php echo $this->formid ?> != null) {
  1089. clearTimeout(refreshparamtimer<?php echo $this->formid ?>);
  1090. }
  1091. }
  1092. /**
  1093. * Update div
  1094. */
  1095. <?php
  1096. $url = $this->url."filter='+document.Form".$this->formid.".param.value+'&maxperpage='+maxperpage+'".$this->params;
  1097. if (isset($this->storedstart) && isset($this->storedend)) {
  1098. $url .= "&start=".$this->storedstart."&end=".$this->storedend;
  1099. }
  1100. ?>
  1101. updateSearch<?php echo $this->formid ?> = function() {
  1102. new Ajax.Updater('<?php echo $this->divid; ?>',
  1103. '<?php echo $url ?>',
  1104. { asynchronous:true, evalScripts: true}
  1105. );
  1106. <?
  1107. if ($this->refresh) {
  1108. ?>
  1109. refreshtimer<?php echo $this->formid ?> = setTimeout("updateSearch<?php echo $this->formid ?>()", refreshdelay<?php echo $this->formid ?>)
  1110. <?
  1111. }
  1112. ?>
  1113. }
  1114. /**
  1115. * Update div when clicking previous / next
  1116. */
  1117. updateSearchParam<?php echo $this->formid ?> = function(filter, start, end, max) {
  1118. clearTimers<?php echo $this->formid ?>();
  1119. if(document.getElementById('maxperpage') != undefined)
  1120. maxperpage = document.getElementById('maxperpage').value;
  1121. new Ajax.Updater('<?php echo $this->divid; ?>','<?php echo $this->url; ?>filter='+filter+'&start='+start+'&end='+end+'&maxperpage='+maxperpage+'<?php echo $this->params ?>', { asynchronous:true, evalScripts: true});
  1122. <?
  1123. if ($this->refresh) {
  1124. ?>
  1125. refreshparamtimer<?php echo $this->formid ?> = setTimeout("updateSearchParam<?php echo $this->formid ?>('"+filter+"',"+start+","+end+","+maxperpage+")", refreshdelay<?php echo $this->formid ?>);
  1126. <?
  1127. }
  1128. ?>
  1129. }
  1130. /**
  1131. * wait 500ms and update search
  1132. */
  1133. pushSearch<?php echo $this->formid ?> = function() {
  1134. clearTimers<?php echo $this->formid ?>();
  1135. refreshtimer<?php echo $this->formid ?> = setTimeout("updateSearch<?php echo $this->formid ?>()", 500);
  1136. }
  1137. pushSearch<?php echo $this->formid ?>();
  1138. </script>
  1139. </form>
  1140. <?
  1141. }
  1142. function displayDivToUpdate() {
  1143. print '<div id="' . $this->divid . '"></div>' . "\n";
  1144. }
  1145. }
  1146. class NoLocationTpl extends AbstractTpl {
  1147. function NoLocationTpl($name) {
  1148. $this->name = $name;
  1149. $this->size = '13';
  1150. }
  1151. function display() {
  1152. print '<span class="error">' . _("No item available") . '</span>';
  1153. print '<input name="'.$this->name.'" id="'.$this->name.'" type="HIDDEN" size="'.$this->size.'" value="" class="searchfieldreal" />';
  1154. }
  1155. function setSelected($elemnt) {
  1156. }
  1157. }
  1158. class SingleLocationTpl extends AbstractTpl {
  1159. function SingleLocationTpl($name, $label) {
  1160. $this->name = $name;
  1161. $this->label = $label;
  1162. $this->value = null;
  1163. }
  1164. function setElementsVal($value) {
  1165. $this->value = array_values($value);
  1166. $this->value = $this->value[0];
  1167. }
  1168. function setSelected($elemnt) {
  1169. }
  1170. function display() {
  1171. print $this->label;
  1172. print '<input name="'.$this->name.'" id="'.$this->name.'" type="HIDDEN" value="'. $this->value .'" class="searchfieldreal" />';
  1173. }
  1174. }
  1175. class AjaxFilterLocation extends AjaxFilter {
  1176. function AjaxFilterLocation($url, $divid = "container", $paramname = 'location', $params = array()) {
  1177. $this->AjaxFilter($url, $divid, $params);
  1178. $this->location = new SelectItem($paramname, 'pushSearch', 'searchfieldreal noborder');
  1179. $this->paramname = $paramname;
  1180. }
  1181. function setElements($elt) {
  1182. if (count($elt) == 0) {
  1183. $this->location = new NoLocationTpl($this->paramname);
  1184. } else if (count($elt) == 1) {
  1185. $loc = array_values($elt);
  1186. $this->location = new SingleLocationTpl($this->paramname, $loc[0]);
  1187. } else {
  1188. $this->location->setElements($elt);
  1189. }
  1190. }
  1191. function setElementsVal($elt) {
  1192. if (count($elt) >= 1) {
  1193. $this->location->setElementsVal($elt);
  1194. }
  1195. }
  1196. function setSelected($elemnt) {
  1197. $this->location->setSelected($elemnt);
  1198. }
  1199. function display() {
  1200. global $conf;
  1201. $root = $conf["global"]["root"];
  1202. ?>
  1203. <form name="Form" id="Form" action="#" onsubmit="return false;">
  1204. <div id="loader"><img id="loadimg" src="<?php echo $root; ?>img/common/loader.gif" alt="loader" class="loader"/></div>
  1205. <div id="searchSpan" class="searchbox" style="float: right;">
  1206. <img src="graph/search.gif" style="position:relative; top: 2px; float: left;" alt="search" />
  1207. <span class="searchfield">
  1208. <?php
  1209. $this->location->display();
  1210. ?>
  1211. </span>&nbsp;
  1212. <span class="searchfield"><input type="text" class="searchfieldreal" name="param" id="param" onkeyup="pushSearch(); return false;" />
  1213. <img src="graph/croix.gif" alt="suppression" style="position:relative; top : 3px;"
  1214. onclick="document.getElementById('param').value =''; pushSearch(); return false;" />
  1215. </span>
  1216. </div>
  1217. <script type="text/javascript">
  1218. document.getElementById('param').focus();
  1219. <?
  1220. if(isset($this->storedfilter)) {
  1221. ?>
  1222. document.Form.param.value = "<?php echo $this->storedfilter ?>";
  1223. <?
  1224. }
  1225. ?>
  1226. var maxperpage = 10;
  1227. if(document.getElementById('maxperpage') != undefined)
  1228. maxperpage = document.getElementById('maxperpage').value;
  1229. /**
  1230. * update div with user
  1231. */
  1232. function updateSearch() {
  1233. launch--;
  1234. if (launch==0) {
  1235. new Ajax.Updater('<?php echo $this->divid; ?>','<?php echo $this->url; ?>filter='+document.Form.param.value+'<?php echo $this->params ?>&<?php echo $this->paramname ?>='+document.Form.<?php echo $this->paramname ?>.value+'&maxperpage='+maxperpage, { asynchronous:true, evalScripts: true});
  1236. }
  1237. }
  1238. /**
  1239. * provide navigation in ajax for user
  1240. */
  1241. function updateSearchParam(filt, start, end) {
  1242. var reg = new RegExp("##", "g");
  1243. var tableau = filt.split(reg);
  1244. var location = "";
  1245. var filter = "";
  1246. var reg1 = new RegExp(tableau[0]+"##", "g");
  1247. if (filt.match(reg1)) {
  1248. if (tableau[0] != undefined) {
  1249. filter = tableau[0];
  1250. }
  1251. if (tableau[1] != undefined) {
  1252. location = tableau[1];
  1253. }
  1254. } else if (tableau.length == 1) {
  1255. if (tableau[0] != undefined) {
  1256. location = tableau[0];
  1257. }
  1258. }
  1259. if(document.getElementById('maxperpage') != undefined)
  1260. {
  1261. maxperpage = document.getElementById('maxperpage').value;
  1262. }
  1263. new Ajax.Updater('<?php echo $this->divid; ?>','<?php echo $this->url; ?>filter='+filter+'<?php echo $this->params ?>&<?php echo $this->paramname ?>='+location+'&start='+start+'&end='+end+'&maxperpage='+maxperpage, { asynchronous:true, evalScripts: true});
  1264. }
  1265. /**
  1266. * wait 500ms and update search
  1267. */
  1268. function pushSearch() {
  1269. launch++;
  1270. setTimeout("updateSearch()",500);
  1271. }
  1272. pushSearch();
  1273. </script>
  1274. </form>
  1275. <?
  1276. }
  1277. }
  1278. class AjaxLocation extends AjaxFilterLocation {
  1279. function AjaxLocation($url, $divid = "container", $paramname = 'location', $params = array()) {
  1280. $this->AjaxFilterLocation($url, $divid, $paramname, $params);
  1281. $this->location = new SelectItem($paramname, 'pushSearchLocation', 'searchfieldreal noborder');
  1282. }
  1283. function display() {
  1284. global $conf;
  1285. $root = $conf["global"]["root"];
  1286. ?>
  1287. <form name="FormLocation" id="FormLocation" action="#">
  1288. <div id="Location">
  1289. <span id="searchSpan" class="searchbox">
  1290. <img src="graph/search.gif"/>
  1291. <span class="locationtext">&nbsp;<?php echo _("Select entity") ?>:&nbsp;</span>
  1292. <span class="locationfield">
  1293. <?php
  1294. $this->location->display();
  1295. ?>
  1296. </span>
  1297. </span>
  1298. <img id="loadimg" src="<?php echo $root; ?>img/common/loader.gif" alt="loader" />
  1299. </div>
  1300. <script type="text/javascript">
  1301. /**
  1302. * update div with user
  1303. */
  1304. function updateSearchLocation() {
  1305. launch--;
  1306. if (launch==0) {
  1307. new Ajax.Updater('<?php echo $this->divid; ?>','<?php echo $this->url; ?><?php echo $this->params ?>&<?php echo $this->paramname ?>='+document.FormLocation.<?php echo $this->paramname ?>.value, { asynchronous:true, evalScripts: true});
  1308. }
  1309. }
  1310. /**
  1311. * wait 500ms and update search
  1312. */
  1313. function pushSearchLocation() {
  1314. launch++;
  1315. setTimeout("updateSearchLocation()",500);
  1316. }
  1317. pushSearchLocation();
  1318. </script>
  1319. </form>
  1320. <?
  1321. }
  1322. }
  1323. /**
  1324. * side menu items class
  1325. * this class is required by SideMenu class
  1326. * each SideMenuItem is all necessary information to
  1327. * create a link.
  1328. *
  1329. * ex: create action "bar" in module "foo" with submodule "subfoo"
  1330. * new SideMenuItem("foobar example","foo","subfoo","bar");
  1331. */
  1332. class SideMenuItem {
  1333. var $text, $module, $submod, $action, $activebg, $inactivebg;
  1334. /**
  1335. * main constructor
  1336. * @param $text text for the link
  1337. * @param $module module for link
  1338. * @param $submod sub module for link
  1339. * @param $action action param for link /!\ use for class id too
  1340. * @param $activebg background image to use when menu is currently activated
  1341. * @param $inactivebg background image to use when menu is currently inactivated
  1342. */
  1343. function SideMenuItem($text,$module,$submod,$action, $activebg = "", $inactivebg = "") {
  1344. $this->text = $text;
  1345. $this->module = $module;
  1346. $this->submod = $submod;
  1347. $this->action = $action;
  1348. $this->cssId = $action;
  1349. $this->activebg = $activebg;
  1350. $this->inactivebg = $inactivebg;
  1351. }
  1352. /**
  1353. * @return a formated link like: main.php?module=base&submod=users&action=add
  1354. *
  1355. */
  1356. function getLink() {
  1357. return 'main.php?module='.$this->module.'&amp;submod='.$this->submod.'&amp;action='.$this->action;
  1358. }
  1359. /**
  1360. * display the SideMenuItem on the screen
  1361. */
  1362. function display() {
  1363. if (hasCorrectAcl($this->module, $this->submod, $this->action)) {
  1364. echo '<li id="'.$this->cssId.'">';
  1365. echo '<a href="'.$this->getLink().'">'.$this->text.'</a></li>'."\n";
  1366. }
  1367. }
  1368. /**
  1369. * Allows to set another CSS id then the default one which is the action
  1370. * string
  1371. *
  1372. * @param id: the CSS id to use
  1373. */
  1374. function setCssId($id) {
  1375. $this->cssId = $id;
  1376. }
  1377. /**
  1378. * Return the menu item CSS
  1379. *
  1380. * @param active: this menu item is active
  1381. */
  1382. function getCss($active = False) {
  1383. $ret = "";
  1384. if ($this->activebg != "" && $this->inactivebg != "") {
  1385. if ($active) $bgi = "background-image: url(" . $this->activebg . ");";
  1386. else $bgi = "background-image: url(" . $this->inactivebg . ");";
  1387. } else {
  1388. $bgi = "";
  1389. }
  1390. if ($active) {
  1391. $color = "#FFF";
  1392. $border = "#FFF";
  1393. } else {
  1394. $color = "#EEE";
  1395. $border = "#666";
  1396. }
  1397. $ret = "#sidebar ul." . $this->submod . " li#" . $this->cssId . " a {
  1398. background-color: " . $color . ";
  1399. color: #666;
  1400. border-right: 1px solid ". $border . ";"
  1401. . $bgi . "
  1402. }";
  1403. if (!$active)
  1404. $ret = $ret . "
  1405. #sidebar ul." . $this->submod . " li#" . $this->cssId ." a:hover {
  1406. color: #666;
  1407. background-color: #F7F7F7;"
  1408. . $bgi . "
  1409. }";
  1410. return $ret;
  1411. }
  1412. }
  1413. class SideMenuItemNoAclCheck extends SideMenuItem {
  1414. /**
  1415. * display the SideMenuItem on the screen
  1416. */
  1417. function display() {
  1418. echo '<li id="'.$this->cssId.'">';
  1419. echo '<a href="'.$this->getLink().'" target="_self">'.$this->text.'</a></li>'."\n";
  1420. }
  1421. }
  1422. /**
  1423. * SideMenu class
  1424. * this class display side menu item
  1425. * side menu is mmc's left menu, it regroups
  1426. * possible actions we can do in a spĂŠcific module
  1427. * like index/configuration/add machine/ add share in
  1428. * samba module
  1429. * this class require SideMenuItem
  1430. */
  1431. class SideMenu {
  1432. var $itemArray;
  1433. var $className;
  1434. var $backgroundImage;
  1435. var $activatedItem;
  1436. /**
  1437. * SideMenu default constructor
  1438. * initalize empty itemArray for SideMenuItem
  1439. */
  1440. function SideMenu() {
  1441. $this->itemArray = array();
  1442. $this->backgroundImage = null;
  1443. $this->activatedItem = null;
  1444. }
  1445. /**
  1446. * add a sideMenu Item into the SideMenu
  1447. * @param $objSideMenuItem object SideMenuItem
  1448. */
  1449. function addSideMenuItem($objSideMenuItem) {
  1450. $this->itemArray[]=&$objSideMenuItem;
  1451. }
  1452. /**
  1453. * CSS class
  1454. */
  1455. function setClass($class) {
  1456. $this->className=$class;
  1457. }
  1458. /**
  1459. * @return className for CSS
  1460. */
  1461. function getClass() {
  1462. return $this->className;
  1463. }
  1464. /**
  1465. * Set the sidemenu background image
  1466. */
  1467. function setBackgroundImage($bg) {
  1468. $this->backgroundImage = $bg;
  1469. }
  1470. /**
  1471. * Get the sidemenu background image
  1472. */
  1473. function getBackgroundImage() {
  1474. return $this->backgroundImage;
  1475. }
  1476. /**
  1477. * print the SideMenu and the sideMenuItem
  1478. */
  1479. function display() {
  1480. echo "<div id=\"sidebar\">\n";
  1481. echo "<ul class=\"".$this->className."\">\n";
  1482. foreach ($this->itemArray as $objSideMenuItem) {
  1483. $objSideMenuItem->display();
  1484. }
  1485. echo "</ul>\n";
  1486. echo "</div>\n";
  1487. }
  1488. /**
  1489. * @return return the Css content for a sidebar
  1490. * static method to get SideBarCss String
  1491. */
  1492. function getSideBarCss() {
  1493. $css = "";
  1494. foreach ($this->itemArray as $objSideMenuItem) {
  1495. $active = (($objSideMenuItem->submod == $_GET["submod"]) && (($objSideMenuItem->action == $_GET["action"]) || ($objSideMenuItem->action == $this->activatedItem)));
  1496. $css = $css . $objSideMenuItem->getCss($active);
  1497. }
  1498. if ($this->backgroundImage) {
  1499. $css .= "#sectionContainer { background-image: url(" . $this->backgroundImage . ") }";
  1500. }
  1501. return $css;
  1502. }
  1503. /**
  1504. * Force a menu item to be displayed as activated
  1505. * Useful for pages that don't have a dedicated tab
  1506. */
  1507. function forceActiveItem($item) {
  1508. $this->activatedItem = $item;
  1509. }
  1510. }
  1511. /**
  1512. * PageGenerator class
  1513. */
  1514. class PageGenerator {
  1515. var $sidemenu; /*< SideMenu Object */
  1516. var $content; /*< array who contains contents Objects */
  1517. /**
  1518. * Constructor
  1519. */
  1520. function PageGenerator($title = "") {
  1521. $content=array();
  1522. $this->title = $title;
  1523. $this->fixheight = True;
  1524. }
  1525. /**
  1526. * set the sideMenu object
  1527. */
  1528. function setSideMenu($objSideMenu) {
  1529. $this->sidemenu=$objSideMenu;
  1530. }
  1531. /**
  1532. * Set the page title
  1533. */
  1534. function setTitle($title) {
  1535. $this->title = $title;
  1536. }
  1537. /**
  1538. * display the whole page
  1539. */
  1540. function display() {
  1541. $this->displaySideMenu();
  1542. if ($this->title)
  1543. $this->displayTitle();
  1544. if ($this->fixheight) {
  1545. /* On IE, make the page have a minimal length, else the sidemenu may be cut */
  1546. print '<div class="fixheight"></div>';
  1547. }
  1548. }
  1549. function displayCss() {
  1550. echo'<style type="text/css">'."\n";
  1551. echo '<!--'."\n";
  1552. echo $this->sidemenu->getSideBarCss();
  1553. echo '-->'."\n";
  1554. echo '</style>'."\n\n";
  1555. }
  1556. /**
  1557. * display the side Menu
  1558. */
  1559. function displaySideMenu() {
  1560. $this->displayCss();
  1561. $this->sidemenu->display();
  1562. }
  1563. /**
  1564. * display the page title
  1565. */
  1566. function displayTitle() {
  1567. if (isset($this->title)) print "<h2>" . $this->title . "</h2>\n";
  1568. }
  1569. /**
  1570. * Sometimes, we don't want to add the fixheight div in the page
  1571. */
  1572. function setNoFixHeight() {
  1573. $this->fixheight = False;
  1574. }
  1575. }
  1576. /**
  1577. * Little wrapper that just include a PHP file as a HtmlElement
  1578. */
  1579. class DisplayFile extends HtmlElement {
  1580. function DisplayFile($file) {
  1581. $this->HtmlElement();
  1582. $this->file = $file;
  1583. }
  1584. function display() {
  1585. require($this->file);
  1586. }
  1587. }
  1588. /**
  1589. * Class for a tab content
  1590. */
  1591. class TabbedPage extends Div {
  1592. function TabbedPage($title, $file) {
  1593. $this->Div(array("class" => "tabdiv"));
  1594. $this->title = $title;
  1595. $this->add(new DisplayFile($file));
  1596. }
  1597. function displayTitle() {
  1598. return "<h2>" . $this->title . "</h2>\n";
  1599. }
  1600. function begin() {
  1601. $s = Div::begin();
  1602. $s .= $this->displayTitle();
  1603. return $s;
  1604. }
  1605. }
  1606. /**
  1607. * Class for tab displayed by TabSelector
  1608. */
  1609. class TabWidget extends HtmlElement {
  1610. function TabWidget($id, $title, $params = array()) {
  1611. $this->id = $id;
  1612. $this->title = $title;
  1613. $this->params = $params;
  1614. $this->active = False;
  1615. $this->last = False;
  1616. }
  1617. function getLink() {
  1618. return urlStr($_GET["module"] . "/" . $_GET["submod"] . "/" . $_GET["action"], array_merge(array("tab" => $this->id), $this->params));
  1619. }
  1620. function setActive($flag) {
  1621. $this->active = $flag;
  1622. }
  1623. function display() {
  1624. if ($this->active)
  1625. $klass = ' class="tabactive"';
  1626. else
  1627. $klass = "";
  1628. print '<li id="' . $this->id . '"' . $klass . '"> '
  1629. . '<a href="' . $this->getLink() . '">'
  1630. . $this->title . "</a></li>";
  1631. }
  1632. }
  1633. /**
  1634. * This class allow to create a page with a tab selector
  1635. */
  1636. class TabbedPageGenerator extends PageGenerator {
  1637. function TabbedPageGenerator() {
  1638. $this->PageGenerator();
  1639. $this->topfile = null;
  1640. $this->tabselector = new TabSelector();
  1641. $this->pages = array();
  1642. $this->firstTabActivated = False;
  1643. }
  1644. /**
  1645. * add a header above the tab selector
  1646. */
  1647. function addTop($title, $file) {
  1648. $this->title = $title;
  1649. $this->topfile = $file;
  1650. }
  1651. /**
  1652. * Add a new tab to a page
  1653. *
  1654. * @param name: the tab id
  1655. * @param title: the tab title in the tab selector
  1656. * @param pagetitle: the page title
  1657. * @param file: the file that renders the page
  1658. * @param params: an array of URL parameters
  1659. */
  1660. function addTab($id, $tabtitle, $pagetitle, $file, $params = array()) {
  1661. global $tabAclArray;
  1662. if (hasCorrectTabAcl($_GET["module"], $_GET["submod"], $_GET["action"], $id)) {
  1663. if (isset($_GET["tab"]) && $_GET["tab"] == $id) {
  1664. $this->tabselector->addActiveTab($id, $tabtitle, $params);
  1665. } else {
  1666. if (isset($_GET["tab"])) {
  1667. $this->tabselector->addTab($id, $tabtitle, $params);
  1668. } else {
  1669. if (!$this->firstTabActivated) {
  1670. $this->tabselector->addActiveTab($id, $tabtitle, $params);
  1671. $this->firstTabActivated = True;
  1672. } else {
  1673. $this->tabselector->addTab($id, $tabtitle, $params);
  1674. }
  1675. }
  1676. }
  1677. $this->pages[$id] = array($pagetitle, $file);
  1678. }
  1679. }
  1680. function display() {
  1681. $this->page = null;
  1682. $this->displaySideMenu();
  1683. $this->displayTitle();
  1684. if ($this->topfile) require($this->topfile);
  1685. $this->tabselector->display();
  1686. if (isset($_GET["tab"]) && isset($this->pages[$_GET["tab"]])) {
  1687. list($title, $file) = $this->pages[$_GET["tab"]];
  1688. $this->page = new TabbedPage($title, $file);
  1689. } else {
  1690. /* Get the first tab page */
  1691. $tab = $this->tabselector->getDefaultTab();
  1692. if ($tab != null) {
  1693. list($title, $file) = $this->pages[$tab->id];
  1694. $this->page = new TabbedPage($title, $file);
  1695. }
  1696. }
  1697. if ($this->page != null) $this->page->display();
  1698. }
  1699. }
  1700. /**
  1701. * Allow to draw a tab selector
  1702. */
  1703. class TabSelector extends HtmlContainer {
  1704. function TabSelector() {
  1705. $this->HtmlContainer();
  1706. $this->tabs = array();
  1707. $this->order = array();
  1708. }
  1709. function getDefaultTab() {
  1710. if (empty($this->elements))
  1711. return null;
  1712. else
  1713. return $this->elements[0];
  1714. }
  1715. function addActiveTab($name, $title, $params = array()) {
  1716. $tab = new TabWidget($name, $title, $params);
  1717. $tab->setActive(True);
  1718. $this->add($tab);
  1719. }
  1720. function addTab($name, $title, $params = array()) {
  1721. $this->add(new TabWidget($name, $title, $params));
  1722. }
  1723. function begin() {
  1724. return '<div class="tabselector"><ul>';
  1725. }
  1726. function end() {
  1727. return "</ul></div>";
  1728. }
  1729. }
  1730. /**
  1731. * display popup window if notify add in queue
  1732. *
  1733. */
  1734. class NotifyWidget {
  1735. /**
  1736. * default constructor
  1737. */
  1738. function NotifyWidget() {
  1739. }
  1740. /**
  1741. * Add a string in notify widget
  1742. * @param $str any HTML CODE
  1743. */
  1744. function add($str) {
  1745. if (!isset($_SESSION['__notify'])) {
  1746. $_SESSION['__notify'] = array();
  1747. }
  1748. $_SESSION['__notify'][] = $str;
  1749. }
  1750. /**
  1751. * set width size
  1752. * @param $size size in px
  1753. */
  1754. function setSize($size) {
  1755. $_SESSION['__notify_size'] = $size;
  1756. }
  1757. /**
  1758. * private internal function
  1759. */
  1760. function getSize() {
  1761. if (isset($_SESSION['__notify_size']) && $_SESSION['__notify_size']) {
  1762. return $_SESSION['__notify_size'];
  1763. } else {
  1764. return 300; //default value
  1765. }
  1766. }
  1767. /**
  1768. * @brief set level (change icon in widget)
  1769. * @param $level level must be beetween 0 and 5
  1770. * level must be beetween 0,5
  1771. * 0: info (default, blue info bubble)
  1772. * <= 1: error for the moment (red icon)
  1773. * 5 is critical
  1774. */
  1775. function setLevel($level) {
  1776. $_SESSION['__notify_level'] = $level;
  1777. }
  1778. /**
  1779. * private internal function
  1780. */
  1781. function getLevel() {
  1782. if (isset($_SESSION['__notify_level']) && $_SESSION['__notify_level']) {
  1783. return $_SESSION['__notify_level'];
  1784. } else {
  1785. return 0; //default level is 0
  1786. }
  1787. }
  1788. /**
  1789. * private internal function
  1790. */
  1791. function getImgLevel() {
  1792. if ($this->getLevel() != 0) {
  1793. return "img/common/icn_alert.gif";
  1794. }
  1795. return "img/common/big_icn_info.png";
  1796. }
  1797. /**
  1798. * private internal function
  1799. */
  1800. function get() {
  1801. $_SESSION['__notify'];
  1802. }
  1803. /**
  1804. * private internal function
  1805. */
  1806. function showJS() {
  1807. # if this function has already been launch, no need for a second launch
  1808. global $doneJS;
  1809. if ($doneJS) { return; }
  1810. $doneJS = True;
  1811. if (!isset($_SESSION['__notify'])) {
  1812. return;
  1813. }
  1814. echo "<script>\n";
  1815. echo "$('popup').style.width='".$this->getSize()."px';";
  1816. echo " showPopupCenter('includes/notify.php');";
  1817. echo "</script>\n";
  1818. }
  1819. /**
  1820. * private internal function
  1821. */
  1822. function display() {
  1823. # if this function has already been launch, no need for a second launch
  1824. global $doneDisplay;
  1825. if ($doneDisplay) { return; }
  1826. $doneDisplay = True;
  1827. if (!isset($_SESSION['__notify'])) {
  1828. return;
  1829. }
  1830. echo '<table style="border-width: 0 0 0 0; border-style: none;"><tr style="border-width: 0 0 0 0; border-style: none;"><td style="border-width: 0 0 0 0; border-style: none;">';
  1831. echo '<div id="popupicon" style="float: left;"><img src="'.$this->getImgLevel().'"></div>';
  1832. echo '</td><td style="vertical-align: center; border-width: 0 0 0 0; text-align: justify; width:100%;">';
  1833. foreach ($_SESSION['__notify'] as $info) {
  1834. echo "<p style=\"margin: auto;\">$info</p>";
  1835. }
  1836. echo '<div style="text-align: center; padding-top: 10px">';
  1837. echo '<input name="breset" type="reset" class="btnSecondary" onclick="getStyleObject(\'popup\').display=\'none\'; return false;" value="'._("Ok").'" />';
  1838. echo '</div>';
  1839. echo '</td></tr></table>';
  1840. $this->flush();
  1841. }
  1842. function flush() {
  1843. unset($_SESSION['__notify']);
  1844. unset ($_SESSION['__notify_size']);
  1845. unset ($_SESSION['__notify_level']);
  1846. }
  1847. }
  1848. /**
  1849. * display a popup window with a message for a successful operation
  1850. *
  1851. */
  1852. class NotifyWidgetSuccess extends NotifyWidget {
  1853. function NotifyWidgetSuccess($message) {
  1854. $this->flush();
  1855. $this->add("<div id=\"validCode\">$message</div>");
  1856. $this->setLevel(0);
  1857. $this->setSize(600);
  1858. }
  1859. }
  1860. /**
  1861. * display a popup window with a message for a failure
  1862. *
  1863. */
  1864. class NotifyWidgetFailure extends NotifyWidget {
  1865. function NotifyWidgetFailure($message) {
  1866. $this->flush();
  1867. $this->add("<div id=\"errorCode\">$message</div>");
  1868. $this->setLevel(4);
  1869. $this->setSize(600);
  1870. }
  1871. }
  1872. /**
  1873. * display a popup window with a message for a warning
  1874. *
  1875. */
  1876. class NotifyWidgetWarning extends NotifyWidget {
  1877. function NotifyWidgetWarning($message) {
  1878. $this->flush();
  1879. $this->add("<div id=\"warningCode\">$message</div>");
  1880. $this->setLevel(3);
  1881. $this->setSize(600);
  1882. }
  1883. }
  1884. /**
  1885. * Display a simple DIV with an error message
  1886. */
  1887. class ErrorMessage extends HtmlElement {
  1888. function ErrorMessage($msg) {
  1889. $this->msg = $msg;
  1890. }
  1891. function display() {
  1892. print '<div class="errorCode">' . $this->msg . '</div>';
  1893. }
  1894. }
  1895. /**
  1896. * Create an URL
  1897. *
  1898. * @param $link string accept format like "module/submod/action" or
  1899. * "module/submod/action/tab"
  1900. * @param $param assoc array with param to add in GET method
  1901. * @param $ampersandEncode bool defining if we want ampersand to be encoded in URL
  1902. */
  1903. function urlStr($link, $param = array(), $ampersandEncode = True) {
  1904. $arr = array();
  1905. $arr = explode ('/',$link);
  1906. if ($ampersandEncode) $amp = "&amp;";
  1907. else $amp = "&";
  1908. $enc_param = "";
  1909. foreach ($param as $key=>$value) {
  1910. $enc_param.= "$amp"."$key=$value";
  1911. }
  1912. if (count($arr) == 3) {
  1913. $ret = "main.php?module=".$arr[0]."$amp"."submod=".$arr[1]."$amp"."action=".$arr[2].$enc_param;
  1914. } else if (count($arr) == 4) {
  1915. $ret = "main.php?module=".$arr[0]."$amp"."submod=".$arr[1]."$amp"."action=".$arr[2]. "$amp" . "tab=" . $arr[3] . $enc_param;
  1916. } else {
  1917. die("Can't build URL");
  1918. }
  1919. return $ret;
  1920. }
  1921. function urlStrRedirect($link, $param = array()) {
  1922. return(urlStr($link, $param, False));
  1923. }
  1924. function findInSideBar($sidebar,$query) {
  1925. foreach ($sidebar['content'] as $arr) {
  1926. if (ereg($query,$arr['link'])) {
  1927. return $arr['text'];
  1928. }
  1929. }
  1930. }
  1931. function findFirstInSideBar($sidebar) {
  1932. return $sidebar['content'][0]['text'];
  1933. }
  1934. class HtmlElement {
  1935. var $options;
  1936. function HtmlElement() {
  1937. $this->options = array();
  1938. }
  1939. function setOptions($options) {
  1940. $this->options = $options;
  1941. }
  1942. function hasBeenPopped() {
  1943. return True;
  1944. }
  1945. function display() {
  1946. die("Must be implemented by the subclass");
  1947. }
  1948. }
  1949. class HtmlContainer {
  1950. var $elements;
  1951. var $index;
  1952. var $popped;
  1953. var $debug;
  1954. function HtmlContainer() {
  1955. $this->elements = array();
  1956. $this->popped = False;
  1957. $this->index = -1;
  1958. }
  1959. function begin() {
  1960. die("Must be implemented by the subclass");
  1961. }
  1962. function end() {
  1963. die("Must be implemented by the subclass");
  1964. }
  1965. function display() {
  1966. print "\n" . $this->begin() . "\n";
  1967. foreach($this->elements as $element) $element->display();
  1968. print "\n" . $this->end() . "\n";
  1969. }
  1970. function add($element, $options = array()) {
  1971. $element->setOptions($options);
  1972. $this->push($element);
  1973. }
  1974. function push($element) {
  1975. if ($this->index == -1) {
  1976. /* Add first element to container */
  1977. $this->index++;
  1978. $this->elements[$this->index] = $element;
  1979. //print "pushing " . $element->options["id"] . " into " . $this->options["id"] . "<br>";
  1980. } else {
  1981. if ($this->elements[$this->index]->hasBeenPopped()) {
  1982. /* All the contained elements have been popped, so add the new element in the list */
  1983. $this->index++;
  1984. $this->elements[$this->index] = $element;
  1985. //print "pushing " . $element->options["id"] . " into " . $this->options["id"] . "<br>";
  1986. } else {
  1987. /* Recursively push a new element into the container */
  1988. $this->elements[$this->index]->push($element);
  1989. }
  1990. }
  1991. }
  1992. function hasBeenPopped() {
  1993. if ($this->popped) $ret = True;
  1994. else if ($this->index == -1) $ret = False;
  1995. else $ret = False;
  1996. return $ret;
  1997. }
  1998. function pop() {
  1999. if (!$this->popped) {
  2000. if ($this->index == -1)
  2001. $this->popped = True;
  2002. else if ($this->elements[$this->index]->hasBeenPopped())
  2003. $this->popped = True;
  2004. else $this->elements[$this->index]->pop();
  2005. //if ($this->popped) print "popping " . $this->options["id"] . "<br>";
  2006. } else die("Nothing more to pop");
  2007. }
  2008. }
  2009. class Div extends HtmlContainer {
  2010. function Div($options = array()) {
  2011. $this->HtmlContainer();
  2012. $this->options = $options;
  2013. $this->display = True;
  2014. }
  2015. function begin() {
  2016. $str = "";
  2017. foreach($this->options as $key => $value) $str.= " $key=\"$value\"";
  2018. if (!$this->display) $displayStyle = ' style =" display: none;"';
  2019. else $displayStyle = "";
  2020. return "<div$str$displayStyle>";
  2021. }
  2022. function end () {
  2023. return "</div>";
  2024. }
  2025. function setVisibility($flag) {
  2026. $this->display = $flag;
  2027. }
  2028. }
  2029. class Form extends HtmlContainer {
  2030. function Form($options = array()) {
  2031. $this->HtmlContainer();
  2032. if (!isset($options["method"])) $options["method"] = "POST";
  2033. $this->options = $options;
  2034. $this->buttons = array();
  2035. $this->summary = NULL;
  2036. }
  2037. function begin() {
  2038. $str = "";
  2039. foreach($this->options as $key => $value) $str.= " $key=\"$value\"";
  2040. $ret = "<form$str>";
  2041. if (isset($this->summary)) {
  2042. $ret = "<p>" . $this->summary . "</p>\n" . $ret;
  2043. }
  2044. return $ret;
  2045. }
  2046. function end() {
  2047. $str = "";
  2048. foreach($this->buttons as $button) $str .= "\n$button\n";
  2049. $str .= "\n</form>\n";
  2050. return $str;
  2051. }
  2052. function addButton($name, $value, $klass = "btnPrimary", $extra = "", $type = "submit") {
  2053. $b = new Button();
  2054. $this->buttons[] = $b->getButtonString($name, $value, $klass, $extra, $type);
  2055. }
  2056. function addValidateButton($name) {
  2057. $b = new Button();
  2058. $this->buttons[] = $b->getValidateButtonString($name);
  2059. }
  2060. function addCancelButton($name) {
  2061. $b = new Button();
  2062. $this->buttons[] = $b->getCancelButtonString($name, "btnSecondary");
  2063. }
  2064. function addExpertButton($name, $value) {
  2065. $d = new DivExpertMode();
  2066. $b = new Button();
  2067. $this->buttons[] = $d->begin() . $b->getButtonString($name, $value) . $d->end();
  2068. }
  2069. function addSummary($msg) {
  2070. $this->summary = $msg;
  2071. }
  2072. function getButtonString($name, $value, $klass = "btnPrimary", $extra = "", $type = "submit") {
  2073. $b = new Button();
  2074. return $b->getButtonString($name, $value, $klass, $extra, $type);
  2075. }
  2076. function addOnClickButton($text, $url) {
  2077. $b = new Button();
  2078. $this->buttons[] = $b->getOnClickButton($text, $url);
  2079. }
  2080. }
  2081. class Button {
  2082. function Button($module = null, $submod = null, $action = null) { # TODO also verify ACL on tabs
  2083. if ($module == null) {
  2084. $this->module = $_GET["module"];
  2085. } else {
  2086. $this->module = $module;
  2087. }
  2088. if ($submod == null) {
  2089. $this->submod = $_GET["submod"];
  2090. } else {
  2091. $this->submod = $submod;
  2092. }
  2093. if ($action == null) {
  2094. $this->action = $_GET["action"];
  2095. } else {
  2096. $this->action = $action;
  2097. }
  2098. }
  2099. function getButtonString($name, $value, $klass = "btnPrimary", $extra = "", $type = "submit") {
  2100. if (hasCorrectAcl($this->module,$this->submod,$this->action)) {
  2101. return $this->getButtonStringWithRight($name, $value, $klass, $extra, $type);
  2102. } else {
  2103. return $this->getButtonStringWithNoRight($name, $value, $klass, $extra, $type);
  2104. }
  2105. }
  2106. function getButtonStringWithRight($name, $value, $klass = "btnPrimary", $extra = "", $type = "submit") {
  2107. return "<input type=\"$type\" name=\"$name\" value=\"$value\" class=\"$klass\" $extra />";
  2108. }
  2109. function getButtonStringWithNoRight($name, $value, $klass = "btnPrimary", $extra = "", $type = "submit") {
  2110. return "<input disabled type=\"$type\" name=\"$name\" value=\"$value\" class=\"btnDisabled\" $extra />";
  2111. }
  2112. function getValidateButtonString($name, $klass = "btnPrimary", $extra = "", $type = "submit") {
  2113. return $this->getButtonString($name, _("Confirm"));
  2114. }
  2115. function getCancelButtonString($name, $klass = "btnPrimary", $extra = "", $type = "submit") {
  2116. return $this->getButtonString($name, _("Cancel"));
  2117. }
  2118. function getOnClickButton($text, $url, $klass = "btnPrimary", $extra = "", $type = "button") {
  2119. return $this->getButtonString("onclick", $text, $klass, $extra = "onclick=\"location.href='" . $url . "';\"", $type);
  2120. }
  2121. }
  2122. class ValidatingForm extends Form {
  2123. function ValidatingForm($options = array()) {
  2124. $this->Form($options);
  2125. $this->options["id"] = "edit";
  2126. $this->options["onsubmit"] = "selectAll(); return validateForm();";
  2127. }
  2128. function end() {
  2129. $str = parent::end();
  2130. $str .= "
  2131. <script type=\"text/javascript\">
  2132. if(Form.findFirstElement(\"".$this->options["id"]."\")) {
  2133. Form.focusFirstElement(\"".$this->options["id"]."\");
  2134. }
  2135. </script>\n";
  2136. return $str;
  2137. }
  2138. }
  2139. /**
  2140. *
  2141. * Allow to easily build the little popup displayed when deleting a user for example
  2142. *
  2143. */
  2144. class PopupForm extends Form {
  2145. function PopupForm($title) {
  2146. $options = array("action" => $_SERVER["REQUEST_URI"]);
  2147. $this->Form($options);
  2148. $this->title = $title;
  2149. $this->text = array();
  2150. $this->ask = "";
  2151. }
  2152. function begin() {
  2153. $str = "<h2>" . $this->title . "</h2>\n";
  2154. $str .= parent::begin();
  2155. foreach($this->text as $text)
  2156. $str .= "<p>" . $text . "</p>";
  2157. return $str;
  2158. }
  2159. function end() {
  2160. $str = "<p>" . $this->ask . "</p>";
  2161. $str .= parent::end();
  2162. return $str;
  2163. }
  2164. function addText($msg) {
  2165. $this->text[] = $msg;
  2166. }
  2167. function setQuestion($msg) {
  2168. $this->ask = $ask;
  2169. }
  2170. function addValidateButtonWithFade($name) {
  2171. $this->buttons[] = $this->getButtonString($name, _("Confirm"), "btnPrimary", "onclick=\"new Effect.Fade('popup'); return true;\"" );
  2172. }
  2173. function addCancelButton($name) {
  2174. $this->buttons[] = $this->getButtonString($name, _("Cancel"), "btnSecondary", "onclick=\"new Effect.Fade('popup'); return false;\"" );
  2175. }
  2176. }
  2177. /**
  2178. *
  2179. * Allow to easily build the little popup, summon a new window
  2180. *
  2181. */
  2182. class PopupWindowForm extends PopupForm {
  2183. function PopupWindowForm($title) {
  2184. $options = array("action" => $_SERVER["REQUEST_URI"]);
  2185. $this->PopupForm($options);
  2186. $this->title = $title;
  2187. $this->text = array();
  2188. $this->ask = "";
  2189. $this->target_uri = "";
  2190. }
  2191. function addValidateButtonWithFade($name) {
  2192. $this->buttons[] = $this->getButtonString($name, _("Confirm"), "btnPrimary", "onclick=\"new Effect.Fade('popup'); window.open('".$this->target_uri."', '', 'toolbar=no, location=no, menubar=no, status=no, status=no, scrollbars=no, width=330, height=200'); return false;\"" );
  2193. }
  2194. }
  2195. class Table extends HtmlContainer {
  2196. function Table($options = array()) {
  2197. $this->HtmlContainer();
  2198. $this->lines = array();
  2199. $this->tr_style = '';
  2200. $this->td_style = '';
  2201. $this->options = $options;
  2202. if (isset($options['tr_style'])) { $this->tr_style = $options['tr_style']; }
  2203. if (isset($options['td_style'])) { $this->td_style = $options['td_style']; }
  2204. }
  2205. function setLines($lines) {
  2206. $this->lines = $lines;
  2207. }
  2208. function begin() {
  2209. return '<table cellspacing="0">';
  2210. }
  2211. function end() {
  2212. return "</table>";
  2213. }
  2214. function getContent() {
  2215. $str = '';
  2216. foreach ($this->lines as $line) {
  2217. $str .= sprintf("<tr%s><td%s>%s</td></tr>", $this->tr_style, $this->td_style, implode(sprintf('</td><td%s>', $this->td_style), $line));
  2218. }
  2219. return $str;
  2220. }
  2221. function displayTable($displayContent = False) {
  2222. print $this->begin();
  2223. if ($displayContent) {
  2224. print $this->getContent();
  2225. }
  2226. print $this->end();
  2227. }
  2228. }
  2229. class DivForModule extends Div {
  2230. function DivForModule($title, $color, $options = array()) {
  2231. $options["style"] = "background-color: " . $color;
  2232. $options["class"] = "formblock";
  2233. $this->Div($options);
  2234. $this->title = $title;
  2235. $this->color = $color;
  2236. }
  2237. function begin() {
  2238. print parent::begin();
  2239. print "<h3>" . $this->title . "</h3>";
  2240. }
  2241. }
  2242. class DivExpertMode extends Div {
  2243. function begin() {
  2244. $str = '<div id="expertMode" ';
  2245. if (isExpertMode()) {
  2246. $str .= ' style="display: inline;"';
  2247. } else {
  2248. $str .= ' style="display: none;"';
  2249. }
  2250. return $str . ' >';
  2251. }
  2252. }
  2253. class ModuleTitleElement extends HtmlElement{
  2254. function ModuleTitleElement($title){
  2255. $this->title=$title;
  2256. }
  2257. function display(){
  2258. print '<br><h1>'.$this->title.'</h1>';
  2259. }
  2260. }
  2261. class TitleElement extends HtmlElement {
  2262. function TitleElement($title, $level = 2){
  2263. $this->title=$title;
  2264. $this->level = $level;
  2265. }
  2266. function display(){
  2267. print '<br/><h'.$this->level.'>'.$this->title.'</h'.$this->level.'>';
  2268. }
  2269. }
  2270. class SelectElement extends HtmlElement{
  2271. function SelectElement($name, $nametab){
  2272. $this->name = $name;
  2273. $this->nametab = $nametab;
  2274. }
  2275. function display() {
  2276. print '<a href="javascript:void(0);" onclick="checkAll(\''.$this->name.'\',1);checkAll(\''.$this->nametab.'\',1);">'._("Select all").' </a> / ';
  2277. print '<a href="javascript:void(0);" onclick="checkAll(\''.$this->name.'\',0);checkAll(\''.$this->nametab.'\',0);">'._("Unselect all").'</a><br/>';
  2278. }
  2279. }
  2280. class TrTitleElement extends HtmlElement{
  2281. function TrTitleElement($arrtitles){
  2282. $this->titles=$arrtitles;
  2283. }
  2284. function display(){
  2285. $colsize=100/sizeof($this->titles);
  2286. print '<tr>';
  2287. foreach( $this->titles as $value ){
  2288. print '<td width="'.$colsize.'%"><b>'.$value.'</b></td>';
  2289. }
  2290. print '</tr>';
  2291. }
  2292. }
  2293. ?>