PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/inc/bx/plugins/basket.php

https://github.com/chregu/fluxcms
PHP | 350 lines | 245 code | 82 blank | 23 comment | 67 complexity | 7b008a74daaf78a27b04766d67dfdfe3 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, Apache-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * Simple Basket Plugin
  4. *
  5. * <plugin type="basket">
  6. * <parameter name="basketname" value="kursbestellung"/>
  7. * <parameter name="baskethandlerclass" value="bx_helpers_novabasket"/>
  8. * </plugin>
  9. *
  10. *
  11. */
  12. class bx_plugins_basket extends bx_plugin {
  13. static private $instance = array();
  14. private $basketname='';
  15. private $baskethandler='';
  16. private $storage = null;
  17. private $idprefx = '';
  18. protected function __construct($mode) {
  19. @session_start();
  20. if (!isset($_SESSION['basket'])) {
  21. $_SESSION['basket'] = array();
  22. }
  23. $this->idprefx = get_class($this)."_";
  24. $this->storage =& $_SESSION['basket'];
  25. }
  26. public static function getInstance($mode='') {
  27. if (!isset(bx_plugins_basket::$instance[$mode])) {
  28. bx_plugins_basket::$instance[$mode] = new bx_plugins_basket($mode);
  29. }
  30. return bx_plugins_basket::$instance[$mode];
  31. }
  32. public function getIdByRequest($path, $name = NULL, $ext =NULL) {
  33. return $this->idprefx.$name.$ext;
  34. }
  35. public function isRealResource($path, $id) {
  36. return false;
  37. }
  38. public function getContentById($path, $id) {
  39. $this->path = $path;
  40. $this->id = $id;
  41. $id = str_replace($this->idprefx, "", $id);
  42. $this->basketname = $this->getParameter($path, 'basketname');
  43. $handlerClassName = $this->getParameter($path, 'baskethandlerclass');
  44. $this->baskethandler = new $handlerClassName();
  45. //post
  46. if ($_SERVER["REQUEST_METHOD"] == 'POST' && is_object($this->baskethandler) && method_exists($this->baskethandler, 'postRequest')) {
  47. $this->baskethandler->postRequest($this->basketname, $this, $this->storage);
  48. }
  49. $command = $this->getCommand($path, $id);
  50. if ($command && !empty($command) && $command!=".") {
  51. $id = str_replace("$command/", "", $id);
  52. $handler = $command."Handler";
  53. if (method_exists($this, $handler)) {
  54. $this->$handler($path, $id);
  55. }
  56. }
  57. $domdoc= new DomDocument();
  58. if (isset($this->storage[$this->basketname])) {
  59. $db2xml = new XML_db2xml(null, $this->basketname);
  60. $domdoc->loadXML('<basket/>');
  61. if (isset($this->storage[$this->basketname]['basket'])) {
  62. ksort($this->storage[$this->basketname]['basket']);
  63. foreach($this->storage[$this->basketname]['basket'] as $idfield => $opts) {
  64. // var_export($idfield.": ".$opts." <br/>");
  65. $e = $domdoc->createElement('entry');
  66. if ($e) {
  67. $e->setAttribute('idfield', $idfield);
  68. if (method_exists($this->baskethandler, 'getItemInfo')) {
  69. $dbprms = $this->baskethandler->getItemInfo($idfield, $this->basketname);
  70. if (is_array($dbprms)) {
  71. $opts = array_merge($opts, $dbprms);
  72. }
  73. }
  74. if (!empty($opts)) {
  75. bx_helpers_xml::array2Dom($opts, $domdoc, $e);
  76. }
  77. }
  78. $domdoc->documentElement->appendChild($e);
  79. }
  80. if (isset($this->storage[$this->basketname]['user'])) {
  81. foreach($this->storage[$this->basketname]['user'] as $idfield => $opts) {
  82. //var_export($idfield.": ".$opts."<br/>");
  83. $u = $domdoc->createElement('user');
  84. if ($u) {
  85. $u->setAttribute('idfield', $idfield);
  86. if ($opts && sizeof($opts) > 0) {
  87. //var_export($idfield.": ".$opts." <br/>");
  88. bx_helpers_xml::array2Dom($opts, $domdoc, $u);
  89. }
  90. }
  91. $domdoc->documentElement->appendChild($u);
  92. }
  93. }
  94. //Shippingadresse
  95. if (isset($this->storage[$this->basketname]['shipping'])) {
  96. foreach($this->storage[$this->basketname]['shipping'] as $idfield => $opts) {
  97. //var_export($idfield.": ".$opts."<br/>");
  98. $u = $domdoc->createElement('shipping');
  99. if ($u) {
  100. $u->setAttribute('idfield', $idfield);
  101. if ($opts && sizeof($opts) > 0) {
  102. //var_export($idfield.": ".$opts." <br/>");
  103. bx_helpers_xml::array2Dom($opts, $domdoc, $u);
  104. }
  105. }
  106. $domdoc->documentElement->appendChild($u);
  107. }
  108. }
  109. //Billsadresse
  110. if (isset($this->storage[$this->basketname]['bill'])) {
  111. foreach($this->storage[$this->basketname]['bill'] as $idfield => $opts) {
  112. //var_export($idfield.": ".$opts."<br/>");
  113. $u = $domdoc->createElement('bill');
  114. if ($u) {
  115. $u->setAttribute('idfield', $idfield);
  116. if ($opts && sizeof($opts) > 0) {
  117. //var_export($idfield.": ".$opts." <br/>");
  118. bx_helpers_xml::array2Dom($opts, $domdoc, $u);
  119. }
  120. }
  121. $domdoc->documentElement->appendChild($u);
  122. }
  123. }
  124. //
  125. if (isset($this->storage[$this->basketname]['costs'])) {
  126. foreach($this->storage[$this->basketname]['costs'] as $idfield => $opts) {
  127. $u = $domdoc->createElement('costs');
  128. if ($u) {
  129. $u->setAttribute('idfield', $idfield);
  130. if ($opts && sizeof($opts) > 0) {
  131. //var_export($idfield.": ".$opts." <br/>");
  132. bx_helpers_xml::array2Dom($opts, $domdoc, $u);
  133. }
  134. }
  135. $domdoc->documentElement->appendChild($u);
  136. }
  137. }
  138. }
  139. /*FIXME: Not really what we want ;) */
  140. if (isset($this->storage[$this->basketname]['checkout'])) {
  141. $c = $domdoc->createElement('checkout');
  142. if ($c) {
  143. bx_helpers_xml::array2Dom($this->storage[$this->basketname]['checkout'], $domdoc, $c);
  144. $domdoc->documentElement->appendChild($c);
  145. }
  146. }
  147. }
  148. return $domdoc;
  149. }
  150. public function &getBasket($basketname) {
  151. $bn = ($basketname!=null) ? $basketname : $this->basketname;
  152. if (isset($this->storage[$bn])) {
  153. return $this->storage[$bn];
  154. }
  155. }
  156. public function clearBasket($basketname) {
  157. if (isset($this->storage[$basketname])) {
  158. unset($this->storage[$basketname]);
  159. }
  160. }
  161. private function addHandler($path, $id) {
  162. $key = dirname($id);
  163. $prms = array();
  164. if (!isset($this->storage[$this->basketname])) {
  165. $this->storage[$this->basketname] = array();
  166. if (!isset($this->storage[$this->basketname]['basket'])) {
  167. $this->storage[$this->basketname]['basket'] = array();
  168. }
  169. }
  170. if (isset($_REQUEST[$this->basketname])) {
  171. $prms = $_REQUEST[$this->basketname];
  172. }
  173. if (method_exists($this->baskethandler, 'addHandler')) {
  174. $this->baskethandler->addHandler($path, $id, $this->basketname, $this->storage, $prms);
  175. } else {
  176. $this->storage[$this->basketname]['basket'][$key] = $prms;
  177. }
  178. }
  179. private function deleteHandler($path, $id) {
  180. $key = dirname($id);
  181. unset($this->storage[$this->basketname]['basket'][$key]);
  182. }
  183. private function removeHandler($path, $id) {
  184. if (isset($_REQUEST[$this->basketname]['remove'])) {
  185. foreach($_REQUEST[$this->basketname]['remove'] as $idfield=>$value) {
  186. if (isset($this->storage[$this->basketname]['basket'][$idfield])) {
  187. unset($this->storage[$this->basketname]['basket'][$idfield]);
  188. }
  189. }
  190. }
  191. }
  192. private function userInfoHandler($path, $id) {
  193. $prms=array();
  194. if (isset($_REQUEST[$this->basketname])) {
  195. $prms = $_REQUEST[$this->basketname];
  196. }
  197. if (is_object($this->baskethandler) && method_exists($this->baskethandler, 'userInfoHandler')) {
  198. $this->baskethandler->userInfoHandler($path, $id, $this->basketname, $this->storage, $prms);
  199. }
  200. }
  201. private function overviewHandler($path, $id) {
  202. $prms=array();
  203. if (isset($_REQUEST[$this->basketname])) {
  204. $prms = $_REQUEST[$this->basketname];
  205. }
  206. if (is_object($this->baskethandler) && method_exists($this->baskethandler, 'overviewHandler')) {
  207. $this->baskethandler->overviewHandler($path, $id, $this->basketname, $this->storage, $this->getParameterAll($path,"helper"));
  208. }
  209. }
  210. private function checkloginHandler($path, $id) {
  211. $prms=array();
  212. if (isset($_REQUEST[$this->basketname])) {
  213. $prms = $_REQUEST[$this->basketname];
  214. }
  215. if (is_object($this->baskethandler) && method_exists($this->baskethandler, 'checkloginHandler')) {
  216. $this->baskethandler->checkloginHandler($path, $id, $this->basketname, $this->storage, $prms);
  217. }
  218. }
  219. private function checkoutHandler($path, $id) {
  220. $this->storage[$this->basketname]['checkout'] = $this->getPayload();
  221. if (is_object($this->baskethandler) && method_exists($this->baskethandler, 'checkoutHandler')) {
  222. $this->baskethandler->checkoutHandler($path, $id, $this->getBasket($this->basketname));
  223. }
  224. }
  225. private function orderHandler($path, $id) {
  226. if (is_object($this->baskethandler) && method_exists($this->baskethandler, 'orderHandler')) {
  227. $prms =
  228. $this->baskethandler->orderHandler($path, $id, $this->basketname, $this->storage, $this->getParameterAll($path,"helper"));
  229. }
  230. }
  231. private function newloginHandler($path, $id) {
  232. if (is_object($this->baskethandler) && method_exists($this->baskethandler, 'newloginHandler')) {
  233. $this->baskethandler->newloginHandler($this->basketname, $this->storage);
  234. }
  235. }
  236. private function finishHandler($path, $id) {
  237. if (is_object($this->baskethandler) && method_exists($this->baskethandler, 'finishHandler')) {
  238. $this->baskethandler->finishHandler($path, $id, $this->getBasket($this->basketname));
  239. }
  240. }
  241. private function getCommand($path, $id) {
  242. $cmdArr = explode("/", dirname($id));
  243. $command = array_shift($cmdArr);
  244. if ($command == "." or empty($command)) {
  245. if (isset($_REQUEST[$this->basketname]['command'])) {
  246. return $_REQUEST[$this->basketname]['command'];
  247. } elseif ($this->checkBasketRequest() && isset($_REQUEST['command'])) {
  248. return $_REQUEST['command'];
  249. }
  250. }
  251. return $command;
  252. }
  253. private function getPayload() {
  254. if (isset($_REQUEST[$this->basketname])) {
  255. return $_REQUEST[$this->basketname];
  256. } elseif ($this->checkBasketRequest()) {
  257. return $GLOBALS["_".$_SERVER['REQUEST_METHOD']];
  258. }
  259. return null;
  260. }
  261. private function checkBasketRequest() {
  262. return (isset($_REQUEST['basketname']) && $_REQUEST['basketname'] == $this->basketname);
  263. }
  264. public function getLastModifiedById($path, $id) {
  265. return time();
  266. }
  267. }
  268. ?>