/pulse2/web/modules/dyngroup/includes/request.php

https://github.com/mandriva-management-console/mmc · PHP · 210 lines · 152 code · 30 blank · 28 comment · 22 complexity · aac01cdbfdd803a1438cafa64a70e142 MD5 · raw file

  1. <?php
  2. /**
  3. * (c) 2004-2007 Linbox / Free&ALter Soft, http://linbox.com
  4. * (c) 2007-2009 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. function parse_request($str) {
  24. $req = new Request();
  25. $req->parse($str);
  26. return $req;
  27. }
  28. function parse_subrequest($str) {
  29. $sub = new SubRequest();
  30. $sub->parse($str);
  31. return $sub;
  32. }
  33. class Request {
  34. function Request() {
  35. $this->subs = array();
  36. $this->nextSubId = 1;
  37. }
  38. function isEmpty() {
  39. return (count($this->subs) == 0);
  40. }
  41. function addSub($sub) {
  42. $sub->id = $this->nextSubId++;
  43. $this->subs[$sub->id] = $sub;
  44. return $sub->id;
  45. }
  46. function editSub($sub) {
  47. $this->subs[$sub->id] = $sub;
  48. return $sub->id;
  49. }
  50. function removeSub($id) {
  51. unset($this->subs[$id]);
  52. }
  53. function getSub($id) {
  54. return $this->subs[$id];
  55. }
  56. function toS() {
  57. if (count($this->subs) == 0) {
  58. return 'EMPTY';
  59. }
  60. return implode('||', array_map('to_s', $this->subs));
  61. }
  62. function toURL() {
  63. return urlencode($this->toS());
  64. }
  65. function toRPC() {
  66. return array_map('to_rpc', $this->subs);
  67. }
  68. function countPart() {
  69. return count($this->subs);
  70. }
  71. function parse($str) {
  72. if ($str == 'EMPTY') {
  73. $this->subs = array();
  74. } else {
  75. $a_reqs = explode('||', $str);
  76. $this->subs = array();
  77. foreach ($a_reqs as $req) {
  78. $sub = parse_subrequest($req);
  79. $sub->id = $this->nextSubId++;
  80. $this->subs[$sub->id] = $sub;
  81. }
  82. }
  83. }
  84. function display() {
  85. $ret = array();
  86. foreach ($this->subs as $id => $sub) {
  87. $ret[] = $sub->display();
  88. }
  89. return $ret;
  90. }
  91. function displayReqListInfos($canbedeleted = false, $default_params = array()) {
  92. if (!$default_params['target']) {
  93. $default_params['target'] = 'creator';
  94. }
  95. if (!strlen($default_params['tab'])) {
  96. $default_params['tab'] = null;
  97. }
  98. $parameters = array();
  99. $parts = array();
  100. foreach ($this->subs as $id => $sub) {
  101. array_push($parts, $sub->display());
  102. $p = $default_params;
  103. $p['subedition'] = 1;
  104. #$p['delete'] = $id;
  105. $p['sub_id'] = $id;
  106. $p['request'] = null;
  107. array_push($parameters, $p);
  108. }
  109. $n = new ListInfos($parts, _T('Search part', 'dyngroup'), "&amp;id=" . $default_params['gid']);
  110. if ($canbedeleted) {
  111. $n->setParamInfo($parameters);
  112. $n->addActionItem(new ActionItem(_T("Edit", 'dyngroup'), $default_params['target_edit'], "edit", "params", null, null, $default_params['tab']));
  113. $n->addActionItem(new ActionItem(_T("Delete", 'dyngroup'), $default_params['target_del'], "delete", "params", null, null, $default_params['tab']));
  114. }
  115. $n->disableFirstColumnActionLink();
  116. $n->display();
  117. print "<br/>"; // or the previous/next will be on the next line...
  118. }
  119. }
  120. class SubRequest {
  121. function SubRequest($module = null, $criterion = null, $value = null, $value2 = null, $operator = null) {
  122. $this->sep_plural = array('>', '<');
  123. $this->module = $module;
  124. $this->crit = $criterion;
  125. $this->val = $value;
  126. $this->operator = (($operator == null) ? "=" : $operator);
  127. if ($value2) {
  128. $this->val = array($value, $value2);
  129. }
  130. $this->id = null;
  131. }
  132. function toS() {
  133. // Set the right comparison operator depending on the attribute of the subrequest
  134. // The operator has to follow the good syntax, else the QueryManager won't work
  135. $comparison_operator = "=="; // '==' is the comparison operator by default
  136. if ($this->operator != '=')
  137. $comparison_operator .= $this->operator;
  138. if (is_array($this->val)) {
  139. return $this->id . "==" . $this->module . "::" . $this->crit . $comparison_operator . $this->sep_plural[0] . implode(', ', $this->val) . $this->sep_plural[1];
  140. } else {
  141. return $this->id . "==" . $this->module . "::" . $this->crit . $comparison_operator . $this->val;
  142. }
  143. }
  144. function toURL() {
  145. return urlencode($this->toS());
  146. }
  147. function toRPC() {
  148. return array($this->id, $this->module, $this->crit, $this->val);
  149. }
  150. function display() {
  151. if (is_array($this->val)) {
  152. return sprintf(_T("%s) Search %s %s (%s) in module %s", "dyngroup"), $this->id, $this->operator, $this->crit, implode(', ', $this->val), $this->module);
  153. } else {
  154. return sprintf(_T("%s) Search %s %s %s in module %s", "dyngroup"), $this->id, $this->operator, $this->crit, $this->val, $this->module);
  155. }
  156. }
  157. function parse($str) {
  158. $a = explode('::', $str);
  159. $b = explode('==', $a[0]);
  160. if (!preg_match("#==>.+<$#", $a[1])) { // If the request isn't a double criterion request
  161. // Search the operator string in the second part of the request string
  162. preg_match("#==(<|>|!=)?#", $a[1], $comparison_operator);
  163. } else {
  164. $comparison_operator = array('==');
  165. }
  166. // Set the operator attribute of the instance : the last char of the matched string
  167. $this->operator = substr($comparison_operator[0], -1);
  168. // Explode the request with the operator to find the criterion and the value
  169. $c = explode($comparison_operator[0], $a[1]);
  170. $this->id = $b[0];
  171. $this->module = $b[1];
  172. $this->crit = $c[0];
  173. $this->val = explode(', ', rtrim(ltrim($c[1], $this->sep_plural[0]), $this->sep_plural[1]));
  174. #$this->val = explode(', ', $c[1]);
  175. if (is_array($this->val) && count($this->val) == 1) {
  176. $this->val = $this->val[0];
  177. }
  178. }
  179. }
  180. ?>