mostly filebased Content Presentation System
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. namespace Controller;
  3. class Checkout {
  4. public $saksnummer;
  5. public $sak;
  6. public $DB = null;
  7. private $hydrated = false;
  8. function __construct($folder = null) {
  9. $f3 = \Base::instance();
  10. $saksnummer = $f3->get('SESSION.saksnummer');
  11. if ($saksnummer) {
  12. $this->saksnummer = $saksnummer;
  13. } else {
  14. $this->saksnummer = md5(sprintf("%s%f",$_SERVER['REMOTE_ADDR'],$_SERVER['REQUEST_TIME_FLOAT']));
  15. $f3->set('SESSION.saksnummer', $this->saksnummer);
  16. }
  17. if (is_object($folder)) {
  18. $folder = $f3->get('POST.datapath');
  19. }
  20. if (is_string($folder)) {
  21. $this->DB = new \DB\SQL(sprintf("sqlite:%sdatabase.sqlite",$folder));
  22. }
  23. }
  24. function hydrate_framework_variables() {
  25. if($this->hydrated) {
  26. return false;
  27. }
  28. $f3 = \Base::instance();
  29. $sak = new \DB\SQL\Mapper($this->DB,'saklist');
  30. $a = new \DB\SQL\Mapper($this->DB,'addresses');
  31. $contact = new \DB\SQL\Mapper($this->DB,'contacts');
  32. $sak->load(['id=?', $this->saksnummer]);
  33. $f3->mset([
  34. 'full_cart' => $this->html_cart(),
  35. 'contact' => $contact->load(['id=?',$sak->kContact]),
  36. 'shipping' => $a->load(['id=?',$sak->kShipping]),
  37. 'billing' => $sak->kBilling ? $a->load(['id=?',$sak->kBilling]) : $a
  38. ]);
  39. $this->hydrated = true;
  40. }
  41. function overview() {
  42. $f3 = \Base::instance();
  43. $tpl = \Template::instance();
  44. $this->hydrate_framework_variables();
  45. return $tpl->render('checkout_overview.htm',true);
  46. }
  47. function index() {
  48. $f3 = \Base::instance();
  49. $sak = new \DB\SQL\Mapper($this->DB,'saklist');
  50. if ($sak->load(['id=?',$this->saksnummer]) === false) {
  51. $sak->id = $this->saksnummer;
  52. $sak->save();
  53. }
  54. if ($sak->kContact === null) {
  55. $f3->reroute('/checkout/contact');
  56. }
  57. if ($sak->kShipping === null) {
  58. $f3->reroute('/checkout/shipping_address');
  59. }
  60. return $this->overview();
  61. }
  62. function save_address() {
  63. $f3 = \Base::instance();
  64. $a = new \DB\SQL\Mapper($this->DB,'addresses');
  65. $a->name = $f3->get('POST.name');
  66. $a->address1 = $f3->get('POST.address1');
  67. $a->address2 = $f3->get('POST.address2');
  68. $a->zip = $f3->get('POST.zip');
  69. $a->place = $f3->get('POST.town');
  70. $a->country = $f3->get('POST.country');
  71. $a->save();
  72. return $a->id;
  73. }
  74. function save_contact() {
  75. $f3 = \Base::instance();
  76. $a = new \DB\SQL\Mapper($this->DB,'contacts');
  77. //$a->name = $f3->get('POST.name');
  78. $a->email = $f3->get('POST.email');
  79. $a->save();
  80. return $a->id;
  81. }
  82. function place_order() {
  83. $info = new \DB\SQL\Mapper($this->DB,'info');
  84. for ($i=0;$i<9999;$i++) {
  85. $candidate = strval(sprintf('%s%04d',date('Ymd'),$i));
  86. if (!$info->load(['ordernumber=?',$candidate])) {
  87. $info->ordernumber = $candidate;
  88. $info->save();
  89. break;
  90. }
  91. }
  92. return $info->id;
  93. }
  94. function email_to_merchant() {
  95. $f3 = \Base::instance();
  96. $tpl = \Template::instance();
  97. $c = $f3->get('checkout_data.emailconfig');
  98. $subject = $f3->get('checkout_data.subject');
  99. $smtp = new \SMTP(
  100. $c['host'],
  101. $c['port'],
  102. $c['scheme'],
  103. $c['user'],
  104. $c['pass'],
  105. );
  106. $headers = [
  107. "MIME-Version"=>"1.0",
  108. "Content-type"=>"text/html",
  109. "From" => $c['from']
  110. ];
  111. $smtp->set('To', $c['admin']);
  112. $smtp->set('Subject',$subject);
  113. foreach ($headers as $k=>$v) {
  114. $smtp->set($k,$v);
  115. }
  116. $f3->set('order_summary', $this->overview());
  117. $f3->set('order_summary', $tpl->render('checkout_overview_kunde.htm',true));
  118. $f3->set('UI', $f3->get('UI').";".$f3->get('form_path'));
  119. $email = $tpl->render($f3->get('checkout_data.template'),true);
  120. if ($smtp->send($email)) {
  121. return true;
  122. } else {
  123. return false;
  124. }
  125. }
  126. function email_to_client($recipient) {
  127. $f3 = \Base::instance();
  128. $tpl = \Template::instance();
  129. $c = $f3->get('checkout_data.emailconfig');
  130. $subject = $f3->get('checkout_data.subject');
  131. $smtp = new \SMTP(
  132. $c['host'],
  133. $c['port'],
  134. $c['scheme'],
  135. $c['user'],
  136. $c['pass'],
  137. );
  138. $headers = [
  139. "MIME-Version"=>"1.0",
  140. "Content-type"=>"text/html",
  141. "From" => $c['from']
  142. ];
  143. $smtp->set('To', $recipient);
  144. $smtp->set('Subject',$subject);
  145. foreach ($headers as $k=>$v) {
  146. $smtp->set($k,$v);
  147. }
  148. $f3->set('order_summary', $this->overview());
  149. $f3->set('order_summary', $tpl->render('checkout_overview_kunde.htm',true));
  150. $f3->set('UI', $f3->get('UI').";".$f3->get('form_path'));
  151. $email = $tpl->render($f3->get('checkout_data.template'), true);
  152. if ($smtp->send($email)) {
  153. return true;
  154. } else {
  155. return false;
  156. }
  157. }
  158. function buy() {
  159. $f3 = \Base::instance();
  160. $sak = new \DB\SQL\Mapper($this->DB,'saklist');
  161. $info = new \DB\SQL\Mapper($this->DB,'info');
  162. $contact = new \DB\SQL\Mapper($this->DB,'contacts');
  163. $sak->load(['id=?',$this->saksnummer]);
  164. $datapath = $f3->get('POST.datapath');
  165. $sak->kInfo = $this->place_order();
  166. $sak->status = 1; // 1 := ordernumber is generated
  167. $sak->save();
  168. $info->load(['id=?',$sak->kInfo]);
  169. $contact->load(['id=?', $sak->kContact]);
  170. switch ($f3->get('POST.payment')) {
  171. case 'transfer':
  172. $email = new Email();
  173. $email->load_form_config($f3->get('POST.xss-token'));
  174. //var_dump($f3->get('private'));
  175. if ($this->email_to_merchant() &&
  176. $this->email_to_client($contact->email)) {
  177. $f3->set('SESSION',[]);
  178. $f3->reroute('/checkout/success');
  179. } else {
  180. $f3->reroute('/checkout/failure');
  181. }
  182. break;
  183. case 'paypal':
  184. break;
  185. }
  186. }
  187. function api(\Base $f3, $params) {
  188. $sak = new \DB\SQL\Mapper($this->DB,'saklist');
  189. if ($sak->load(['id=?',$this->saksnummer]) !== false) {
  190. switch ($params['method']) {
  191. case "contact":
  192. $sak->kContact = $this->save_contact();
  193. $sak->save();
  194. $f3->reroute('/checkout');
  195. break;
  196. case "shipping_address":
  197. $sak->kShipping = $this->save_address();
  198. $sak->save();
  199. $f3->reroute('/checkout');
  200. break;
  201. case "billing_address":
  202. $sak->kBilling = $this->save_address();
  203. $sak->save();
  204. $f3->reroute('/checkout');
  205. break;
  206. case "buy":
  207. $sak->status = $this->buy();
  208. // $sak->save();
  209. // $f3->reroute('/checkout/success');
  210. default:
  211. break;
  212. }
  213. }
  214. }
  215. function html_cart() {
  216. $t = \Template::instance();
  217. return $t->render("checkout.htm");
  218. }
  219. }