PageRenderTime 53ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/trunk/controllers/eventsbasket/controller.php

https://bitbucket.org/pooshonk/esw
PHP | 740 lines | 612 code | 95 blank | 33 comment | 54 complexity | ef6655110911fda61fa1a79f1170eb65 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. class Eventsbasketcontroller {
  3. private $basket;
  4. public function __construct( PeacockCarterFrameworkRegistry $registry, $directCall )
  5. {
  6. $this->registry = $registry;
  7. require_once( FRAMEWORK_PATH . 'models/eventsbasket/eventsbasket.php');
  8. $this->basket = new Eventsbasket( $this->registry );
  9. $this->basket->checkBasket();
  10. if( $directCall == true )
  11. {
  12. $this->registry->getObject('template')->getPage()->setTitle('Your basket');
  13. $urlBits = $this->registry->getURLBits();
  14. if( !isset( $urlBits[1] ) )
  15. {
  16. $this->viewBasket();
  17. }
  18. else
  19. {
  20. switch( $urlBits[1] )
  21. {
  22. case 'view':
  23. $this->viewBasket();
  24. break;
  25. case 'ping':
  26. $this->ping();
  27. break;
  28. case 'add-product':
  29. echo $this->addProduct( $urlBits[2], 1);
  30. break;
  31. case 'edit':
  32. $this->editBasket( intval( $urlBits[2] ) );
  33. break;
  34. case 'checkout':
  35. $this->checkout();
  36. break;
  37. case 'payment':
  38. $this->paymentPage( intval( $urlBits[2] ), false );
  39. break;
  40. case 'pay-offline':
  41. $this->paymentPage( intval( $urlBits[2] ), true );
  42. break;
  43. case 'remove':
  44. $this->removeEvent( intval( $urlBits[2] ) );
  45. break;
  46. case 'confirm-order':
  47. $this->confirmOrder( intval( $urlBits[2] ) );
  48. break;
  49. default:
  50. $this->viewBasket();
  51. break;
  52. }
  53. }
  54. }
  55. }
  56. private function ping()
  57. {
  58. require_once( FRAMEWORK_PATH . 'lib/payment/paypalevents.class.php' );
  59. $this->gateway = new Gateway( $this->registry );
  60. $this->gateway->processPingBack();
  61. exit();
  62. }
  63. public function smallBasket()
  64. {
  65. // set our embedded property
  66. $this->embedded = true;
  67. // check that the basket is not empty
  68. if( $this->basket->isEmpty() == false )
  69. {
  70. // basket isn't empty so use the basket template, and set the numBasketItems and basketCost template variables
  71. $this->registry->getObject('template')->addTemplateBit('eventsbasket', 'ebasket.tpl.php');
  72. $this->registry->getObject('template')->getPage()->addPPTag('ebasketCost', $this->basket->getTotal());
  73. //$this->registry->getObject('template')->getPage()->addPPTag('shippingCost', $this->basket->getShippingCost());
  74. }
  75. else
  76. {
  77. // basket is empty - so use the empty basket template
  78. $this->registry->getObject('template')->addTemplateBit('eventsbasket', 'ebasket-empty.tpl.php');
  79. }
  80. }
  81. private function viewBasket()
  82. {
  83. $path = 'events';
  84. require_once( FRAMEWORK_PATH . 'models/content.php');
  85. require_once( FRAMEWORK_PATH . 'models/page/model.php');
  86. $this->model = new Pagecontent( $this->registry, 0, $this->registry->getObject('db')->sanitizeData( $path ) );
  87. $this->registry->getObject('menubuilder')->buildMenu( $this->model->getID() );
  88. $url = $this->registry->buildURL(array('eventsbasket', 'checkout'), '', false, true );
  89. $this->registry->getObject('template')->getPage()->addTag( 'checkout_url', $url );
  90. //echo $url;
  91. if( $this->basket->isEmpty() )
  92. {
  93. $this->registry->getObject('template')->buildFromTemplates( 'header.tpl.php', 'eventsbasket/empty.tpl.php', 'footer.tpl.php');
  94. }
  95. else
  96. {
  97. $contents = $this->basket->getContents();
  98. $cache = $this->registry->getObject('db')->cacheData( $contents );
  99. $this->registry->getObject('template')->buildFromTemplates( 'header.tpl.php', 'eventsbasket/view.tpl.php', 'footer.tpl.php');
  100. $this->registry->getObject('template')->getPage()->addTag( 'basket', array( 'DATA', $cache ) );
  101. $this->registry->getObject('template')->getPage()->addTag( 'total', $this->basket->getTotal() );
  102. foreach( $contents as $content )
  103. {
  104. //discount amount > 0 insert template bit
  105. if( isset( $content['discount_amount'] ) && $content['discount_amount'] > 0 )
  106. {
  107. $this->registry->getObject('template')->addTemplateBit('nbd_discount' . $content['basket_id'], 'eventsbasket/discount.tpl.php', array( 'discount_percentage' => $content['discount_percentage'], 'discount_amount' => $content['discount_amount'], 'basket_subtotal' => $content['basket_subtotal'] ) );
  108. }
  109. }
  110. }
  111. }
  112. private function editBasket( $bid )
  113. {
  114. $path = 'events';
  115. require_once( FRAMEWORK_PATH . 'models/content.php');
  116. require_once( FRAMEWORK_PATH . 'models/page/model.php');
  117. $this->model = new Pagecontent( $this->registry, 0, $this->registry->getObject('db')->sanitizeData( $path ) );
  118. $this->registry->getObject('menubuilder')->buildMenu( $this->model->getID() );
  119. $session_id = session_id();
  120. $ip_address = $_SERVER ['REMOTE_ADDR'];
  121. $uid = $this->registry->getObject('authenticate')->getUserID();
  122. if( $this->registry->getObject('authenticate')->isLoggedIn() == true )
  123. {
  124. $sql = "SELECT a.*, a.ID as aid, b.event_session FROM eventbasket_attendees a, eventbasket b WHERE b.ID = {$bid} AND a.user_id={$uid} AND a.basket_id= b.ID";
  125. }
  126. else
  127. {
  128. $sql = "SELECT a.*, a.ID as aid, b.event_session FROM eventbasket_attendees a, eventbasket b WHERE b.ID = {$bid} AND a.session_id='{$session_id}' AND a.ip_address='{$ip_address}' AND a.basket_id= b.ID";
  129. }
  130. $this->registry->getObject('db')->executeQuery( $sql );
  131. if( $this->registry->getObject('db')->numRows( ) > 0 )
  132. {
  133. if( isset( $_POST ) && is_array($_POST) && count( $_POST ) > 0 )
  134. {
  135. $nums = 0;
  136. if( isset( $_POST['attendee'] ) && is_array($_POST['attendee']) && count( $_POST['attendee'] ) > 0 )
  137. {
  138. $current_attendees = array();
  139. while( $row = $this->registry->getObject('db')->getRows() )
  140. {
  141. $current_attendees[$row['aid']] = $row;
  142. }
  143. //delete removed attendees
  144. foreach( $current_attendees as $current_attendee )
  145. {
  146. if( !in_array( $current_attendee['ID'], array_keys( $_POST['attendee'] ) ) )
  147. {
  148. $aid = $this->registry->getObject('db')->sanitizeData( $current_attendee['ID'] );
  149. $sql = "DELETE FROM eventbasket_attendees WHERE ID = {$aid} AND basket_id = " . $bid;
  150. $this->registry->getObject('db')->executeQuery( $sql );
  151. }
  152. }
  153. //update remaining attendees
  154. foreach( $_POST['attendee'] as $attendee )
  155. {
  156. $update = array();
  157. $update['name'] = $this->registry->getObject('db')->sanitizeData( $attendee['name'] );
  158. $update['email'] = $this->registry->getObject('db')->sanitizeData( $attendee['email'] );
  159. $update['phone'] = $this->registry->getObject('db')->sanitizeData( $attendee['phone'] );
  160. $update['organisation'] = $this->registry->getObject('db')->sanitizeData( $attendee['organisation'] );
  161. $update['organisation_type'] = $this->registry->getObject('db')->sanitizeData( $attendee['organisation_type'] );
  162. $update['dietary'] = $this->registry->getObject('db')->sanitizeData( $attendee['dietary'] );
  163. $update['access'] = $this->registry->getObject('db')->sanitizeData( $attendee['access'] );
  164. $this->registry->getObject('db')->updateRecords( 'eventbasket_attendees', $update, 'ID=' . $this->registry->getObject('db')->sanitizeData( $attendee['ID'] ) );
  165. $nums++;
  166. if( isset( $attendee['workshop'] ) && is_array($attendee['workshop']) && count($attendee['workshop'] ) > 0 )
  167. {
  168. foreach( $attendee['workshop'] as $workshop => $preference )
  169. {
  170. $update = array();
  171. $update['preference'] = $this->registry->getObject('db')->sanitizeData( $preference );
  172. $this->registry->getObject('db')->updateRecords( 'eventbasket_attendees_workshops', $update, 'workshop_id = ' . $this->registry->getObject('db')->sanitizeData( $workshop ) . ' AND attendee_id = ' . $this->registry->getObject('db')->sanitizeData( $attendee['ID'] ) );
  173. }
  174. }
  175. }
  176. }
  177. else
  178. {
  179. $sql = "DELETE FROM eventbasket_attendees WHERE basket_id = " . $bid;
  180. $this->registry->getObject('db')->executeQuery( $sql );
  181. }
  182. $sql = "UPDATE eventbasket SET attendees = {$nums} WHERE ID =" . $bid;
  183. $this->registry->getObject('db')->executeQuery( $sql );
  184. $this->registry->redirectUser(array('eventsbasket'),'Booking updated','Thank you for updating your booking.',false);
  185. }
  186. else
  187. {
  188. $cache = $this->registry->getObject('db')->cacheQuery( $sql );
  189. $this->registry->getObject('template')->getPage()->addPPTag( 'attendees', array( 'SQL', $cache ) );
  190. while( $data = $this->registry->getObject('db')->getRows() )
  191. {
  192. $sid = $data['event_session'];
  193. //organisation types
  194. switch( $data['organisation_type'] )
  195. {
  196. case 'public':
  197. $this->registry->getObject('template')->getPage()->addPPTag( 'nbd_public' . $data['aid'], 'selected="selected"' );
  198. break;
  199. case 'private':
  200. $this->registry->getObject('template')->getPage()->addPPTag( 'nbd_private' . $data['aid'], 'selected="selected"' );
  201. break;
  202. case 'voluntary':
  203. $this->registry->getObject('template')->getPage()->addPPTag( 'nbd_voluntary' . $data['aid'], 'selected="selected"' );
  204. break;
  205. case 'social':
  206. $this->registry->getObject('template')->getPage()->addPPTag( 'nbd_social' . $data['aid'], 'selected="selected"' );
  207. break;
  208. case 'individual':
  209. $this->registry->getObject('template')->getPage()->addPPTag( 'nbd_individual' . $data['aid'], 'selected="selected"' );
  210. break;
  211. default;
  212. $this->registry->getObject('template')->getPage()->addPPTag( 'nbd_individual' . $data['aid'], 'selected="selected"' );
  213. break;
  214. }
  215. }
  216. //get the time slots and workshops for this session
  217. $sql = "SELECT s.ID as slot_id, DATE_FORMAT(s.time, '%H:%i') as time, s.heading, s.description FROM event_sessions_slots s WHERE s.session_id = {$sid} AND ( SELECT COUNT(*) FROM event_sessions_slots_workshops w WHERE s.ID = w.slot_id ) > 0 ORDER BY s.ID ASC";
  218. $this->registry->getObject('db')->executeQuery( $sql );
  219. if( $this->registry->getObject('db')->numRows() > 0 )
  220. {
  221. $slots = array();
  222. while( $data = $this->registry->getObject('db')->getRows() )
  223. {
  224. $slots[] = $data;
  225. }
  226. $cache = $this->registry->getObject('db')->cacheData( $slots );
  227. $this->registry->getObject('template')->getPage()->addTag( 'slots', array( 'DATA', $cache ) );
  228. //workshops
  229. $workshops = array();
  230. $sql = "SELECT ID as wid, slot_id, name as workshop FROM event_sessions_slots_workshops WHERE session_id = {$sid}";
  231. $this->registry->getObject('db')->executeQuery( $sql );
  232. while( $row = $this->registry->getObject('db')->getRows() )
  233. {
  234. if( in_array( $row['slot_id'], array_keys( $workshops ) ) )
  235. {
  236. $workshops[ $row['slot_id'] ][] = $row;
  237. }
  238. else
  239. {
  240. $workshops[ $row['slot_id'] ] = array();
  241. $workshops[ $row['slot_id'] ][] = $row;
  242. }
  243. }
  244. foreach( $workshops as $record => $details )
  245. {
  246. $cache = $this->registry->getObject('db')->cacheData( $details );
  247. $this->registry->getObject('template')->getPage()->addTag( 'workshops-' . $record, array( 'DATA', $cache ) );
  248. }
  249. //get attendee preferences
  250. $sql = "SELECT w.* FROM eventbasket_attendees_workshops w, eventbasket_attendees a WHERE w.attendee_id = a.ID AND a.basket_id = {$bid}";
  251. $this->registry->getObject('db')->executeQuery( $sql );
  252. while( $row = $this->registry->getObject('db')->getRows() )
  253. {
  254. $this->registry->getObject('template')->getPage()->addPPTag( $row['attendee_id'] . $row['workshop_id'], $row['preference'] );
  255. }
  256. $this->registry->getObject('template')->buildFromTemplates( 'header.tpl.php', 'eventsbasket/edit-book-with-workshops.tpl.php', 'footer.tpl.php');
  257. }
  258. else
  259. {
  260. $this->registry->getObject('template')->buildFromTemplates( 'header.tpl.php', 'eventsbasket/edit-book-without-workshops.tpl.php', 'footer.tpl.php');
  261. }
  262. $this->registry->getObject('template')->getPage()->addTag( 'bid', $bid );
  263. }
  264. }
  265. else
  266. {
  267. $this->registry->getObject('template')->buildFromTemplates( 'header.tpl.php', 'eventsbasket/noattendees.tpl.php', 'footer.tpl.php' );
  268. }
  269. }
  270. /**
  271. * Remove a course from the basket
  272. * @param int $bid the basket ID
  273. */
  274. private function removeEvent( $bid )
  275. {
  276. $session_id = session_id();
  277. $ip_address = $_SERVER ['REMOTE_ADDR'];
  278. if( $this->registry->getObject('authenticate')->isLoggedIn() == true )
  279. {
  280. $uid = $this->registry->getObject('authenticate')->getUserID();
  281. $sql = "DELETE FROM eventbasket WHERE user_id={$uid} AND ID=" . $bid;
  282. $this->registry->getObject('db')->executeQuery( $sql );
  283. $sql = "DELETE FROM eventbasket_attendees WHERE user_id={$uid} AND basket_id=" . $bid;
  284. $this->registry->getObject('db')->executeQuery( $sql );
  285. }
  286. else
  287. {
  288. $sql = "DELETE FROM eventbasket WHERE session_id='{$session_id}' AND ip_address='{$ip_address}' AND ID=" . $bid;
  289. $this->registry->getObject('db')->executeQuery( $sql );
  290. $sql = "DELETE FROM eventbasket_attendees WHERE session_id='{$session_id}' AND ip_address='{$ip_address}' AND basket_id=" . $bid;
  291. $this->registry->getObject('db')->executeQuery( $sql );
  292. }
  293. $this->registry->redirectUser( array('eventsbasket'), 'Event removed', 'The event has been removed from your basket', false );
  294. }
  295. private function checkout()
  296. {
  297. //find out if all the sessions in the basket can be paid for online. redirect accordingly
  298. $session_id = session_id();
  299. $ip_address = $_SERVER ['REMOTE_ADDR'];
  300. $path = 'events';
  301. require_once( FRAMEWORK_PATH . 'models/content.php');
  302. require_once( FRAMEWORK_PATH . 'models/page/model.php');
  303. $this->model = new Pagecontent( $this->registry, 0, $this->registry->getObject('db')->sanitizeData( $path ) );
  304. $this->registry->getObject('menubuilder')->buildMenu( $this->model->getID() );
  305. if( $this->basket->isEmpty() )
  306. {
  307. $this->registry->getObject('template')->buildFromTemplates( 'header.tpl.php', 'eventsbasket/empty.tpl.php', 'footer.tpl.php');
  308. }
  309. else
  310. {
  311. $contents = $this->basket->getContents();
  312. $sessions = count($contents);
  313. $payment = 0;
  314. $onlinePaymentCost = 0;
  315. $offlinePaymentCost = 0;
  316. foreach( $contents as $content )
  317. {
  318. $sql = "SELECT * FROM event_sessions WHERE pay_online = 1 AND ID =" . $content['event_session'];
  319. $this->registry->getObject('db')->executeQuery( $sql );
  320. if( $this->registry->getObject('db')->numRows() > 0 )
  321. {
  322. $payment = $payment + 1;
  323. $onlinePaymentCost += $content['basket_subtotal'];
  324. }
  325. else
  326. {
  327. $payment = $payment;
  328. $offlinePaymentCost += $content['basket_subtotal'];
  329. }
  330. }
  331. $this->registry->getObject('template')->getPage()->addTag('pay_online', number_format($onlinePaymentCost,2) );
  332. $this->registry->getObject('template')->getPage()->addTag('pay_offline', number_format($offlinePaymentCost, 2) );
  333. if( isset( $_POST ) && is_array( $_POST ) && count( $_POST ) > 0 )
  334. {
  335. // create the order
  336. $required = array( 'name' => 'Name', 'address' => 'Address', 'city' => 'City', 'county' =>'County', 'postcode' =>'Postcode', 'email' => 'Email address', 'phone' => 'Telephone number' );
  337. $errors = array();
  338. foreach( array_keys( $required ) as $r )
  339. {
  340. if( ! isset( $_POST[ $r ]) || $_POST[ $r ] == '' )
  341. {
  342. $processable = false;
  343. $error = array();
  344. $error['errora'] = $required[ $r ] . " is a required field";
  345. $errors[] = $error;
  346. }
  347. }
  348. if( empty( $errors ) )
  349. {
  350. $order = array();
  351. $order['user_id'] = $this->registry->getObject('authenticate')->getUserID();
  352. $order['session_id'] = $session_id;
  353. $order['ip_address'] = $ip_address;
  354. $order['payment'] = ( $payment == 0 ) ? 'offline' : 'online';
  355. $order['invoice_name'] = $this->registry->getObject('db')->sanitizeData( $_POST['name'] );
  356. $order['invoice_company'] = $this->registry->getObject('db')->sanitizeData( $_POST['organisation'] );
  357. $order['invoice_address'] = $this->registry->getObject('db')->sanitizeData( $_POST['address'] );
  358. $order['invoice_address_linetwo'] = $this->registry->getObject('db')->sanitizeData( $_POST['address_linetwo'] );
  359. $order['invoice_city'] = $this->registry->getObject('db')->sanitizeData( $_POST['city'] );
  360. $order['invoice_country'] = $this->registry->getObject('db')->sanitizeData( $_POST['county'] );
  361. $order['invoice_postcode'] = $this->registry->getObject('db')->sanitizeData( $_POST['postcode'] );
  362. $order['invoice_email'] = $this->registry->getObject('db')->sanitizeData( $_POST['email'] );
  363. $order['invoice_telephone'] = $this->registry->getObject('db')->sanitizeData( $_POST['phone'] );
  364. $order['cost'] = $onlinePaymentCost+$offlinePaymentCost;
  365. $order['online_cost'] = $onlinePaymentCost;
  366. $order['offline_cost'] = $offlinePaymentCost;
  367. $this->registry->getObject('db')->insertRecords( 'eventorders', $order );
  368. $order_id = $this->registry->getObject('db')->lastInsertID();
  369. $contents = $this->basket->getContents();
  370. foreach( $contents as $content )
  371. {
  372. //workshops
  373. $sql = "SELECT w.* FROM eventbasket_attendees_workshops w, eventbasket_attendees a WHERE w.attendee_id = a.ID AND a.basket_id = " . $content['basket_id'];
  374. $this->registry->getObject('db')->executeQuery( $sql );
  375. if( $this->registry->getObject('db')->numRows() > 0 )
  376. {
  377. $workshops = array();
  378. while( $row = $this->registry->getObject('db')->getRows() )
  379. {
  380. $workshops[] = $row;
  381. }
  382. }
  383. //attendees
  384. $sql = "SELECT * FROM eventbasket_attendees WHERE basket_id=" . $content['basket_id'];
  385. $this->registry->getObject('db')->executeQuery( $sql );
  386. $atts = array();
  387. while( $row = $this->registry->getObject('db')->getRows() )
  388. {
  389. $atts[] = $row;
  390. }
  391. $flag_check = false;
  392. foreach( $atts as $at )
  393. {
  394. $insert = array();
  395. $insert['session_ID'] = $this->registry->getObject('db')->sanitizeData( $content['event_session'] );
  396. $insert['name'] = $this->registry->getObject('db')->sanitizeData( $at['name'] );
  397. $insert['email'] = $this->registry->getObject('db')->sanitizeData( $at['email'] );
  398. $insert['phone'] = $this->registry->getObject('db')->sanitizeData( $at['phone'] );
  399. $insert['organisation'] = $this->registry->getObject('db')->sanitizeData( $at['organisation'] );
  400. $insert['organisation_type'] = $this->registry->getObject('db')->sanitizeData( $at['organisation_type'] );
  401. $insert['job_title'] = $this->registry->getObject('db')->sanitizeData( $at['job_title'] );
  402. $insert['dietary'] = $this->registry->getObject('db')->sanitizeData( $at['dietary'] );
  403. $insert['access'] = $this->registry->getObject('db')->sanitizeData( $at['access'] );
  404. $insert['cancelled'] = 0;
  405. $insert['order'] = $this->registry->getObject('db')->sanitizeData( $order_id );
  406. $this->registry->getObject('db')->insertRecords( 'event_session_attendees', $insert );
  407. $aid = $this->registry->getObject('db')->lastInsertID();
  408. if( isset( $workshops ) && is_array($workshops) && count( $workshops ) > 0 )
  409. {
  410. foreach( $workshops as $workshop )
  411. {
  412. if( $workshop['attendee_id'] == $at['ID'] )
  413. {
  414. $insert = array();
  415. $insert['attendee_id'] = $this->registry->getObject('db')->sanitizeData( $aid );
  416. $insert['workshop_id'] = $this->registry->getObject('db')->sanitizeData( $workshop['workshop_id'] );
  417. $insert['preference'] = $this->registry->getObject('db')->sanitizeData( $workshop['preference'] );
  418. $this->registry->getObject('db')->insertRecords( 'event_session_attendees_workshops', $insert );
  419. }
  420. }
  421. }
  422. }
  423. }
  424. //email confirmation that the order has been created
  425. $this->emailAdmin('invoice', $order_id, $order['cost'] );
  426. $ad = $_POST['address'] .', ' . $_POST['address_linetwo'] . ', ' . $_POST['city'] . ', ' . $_POST['county'] . ', ' . $_POST['postcode'];
  427. $this->emailCustomer('invoice', $order_id, $order['cost'], $_POST['email'], $_POST['name'], $ad, $_POST['telephone'], 'Invoice' );
  428. $this->emptyBasket();
  429. $this->registry->getObject('template')->buildFromTemplates( 'header.tpl.php', 'eventsbasket/placed-invoice.tpl.php', 'footer.tpl.php');
  430. $this->registry->redirectUser( array( 'eventsbasket', 'payment', $order_id ), 'Order placed', 'Thank you. Your order has been placed, we are now taking you to the payment details page', false );
  431. }
  432. else
  433. {
  434. // errors on submit
  435. $this->registry->getObject('template')->buildFromTemplates( 'header.tpl.php', 'eventsbasket/checkout.tpl.php', 'footer.tpl.php');
  436. $this->registry->getObject('template')->addTemplateBit( 'form_errors', 'eventsbasket/errors.tpl.php' );
  437. foreach( $_POST as $pkey => $pdata )
  438. {
  439. $this->registry->getObject('template')->getPage()->addTag( 'form_' . $pkey, $pdata );
  440. }
  441. $cache = $this->registry->getObject('db')->cacheData( $errors );
  442. $this->registry->getObject('template')->getPage()->addTag( 'errors', array( 'DATA', $cache ) );
  443. }
  444. }
  445. else
  446. {
  447. if( $this->registry->getObject('authenticate')->isLoggedIn() )
  448. {
  449. $sql = "SELECT u.*, e.* FROM users u LEFT JOIN users_extra e ON u.ID = e.user_id WHERE u.ID =" . $this->registry->getObject('authenticate')->getUserID();
  450. $this->registry->getObject('db')->executeQuery( $sql );
  451. $data = $this->registry->getObject('db')->getRows();
  452. $this->registry->getObject('template')->dataToTags( $data, 'form_' );
  453. }
  454. // confirm to place
  455. $this->registry->getObject('template')->buildFromTemplates( 'header.tpl.php', 'eventsbasket/checkout.tpl.php', 'footer.tpl.php');
  456. }
  457. }
  458. }
  459. private function paymentPage( $order, $request_pay_offline=false )
  460. {
  461. $path = 'events';
  462. require_once( FRAMEWORK_PATH . 'models/content.php');
  463. require_once( FRAMEWORK_PATH . 'models/page/model.php');
  464. $this->model = new Pagecontent( $this->registry, 0, $this->registry->getObject('db')->sanitizeData( $path ) );
  465. $this->registry->getObject('menubuilder')->buildMenu( $this->model->getID() );
  466. $sql = "SELECT * FROM eventorders WHERE cancelled = 0 AND ID = {$order} LIMIT 1";
  467. $this->registry->getObject('db')->executeQuery( $sql );
  468. if( $this->registry->getObject('db')->numRows() == 1 )
  469. {
  470. $data = $this->registry->getObject('db')->getRows();
  471. //print_r($order);
  472. $offline_cost = number_format($data['offline_cost'], 2);
  473. $online_cost = number_format($data['online_cost'], 2);
  474. $total_cost = number_format( ($offline_cost + $online_cost), 2 );
  475. $this->registry->getObject('template')->getPage()->addTag( 'online_cost', $online_cost );
  476. $this->registry->getObject('template')->getPage()->addTag( 'offline_cost', $offline_cost );
  477. $this->registry->getObject('template')->getPage()->addTag( 'reference', $order );
  478. if( $request_pay_offline==false )
  479. {
  480. if( $data['online_cost'] > 0 && $data['online_paid'] == 0 )
  481. {
  482. //there is an online cost and it hasn't been paid.
  483. $this->registry->getObject('template')->getPage()->addTag( 'confirm', '' );
  484. require_once( FRAMEWORK_PATH . 'lib/payment/paypal.class.php' );
  485. $this->gateway = new Gateway( $this->registry );
  486. $html = $this->gateway->paymentHTML();
  487. $this->registry->getObject('template')->getPage()->addTag( 'paypal', $html );
  488. $this->registry->getObject('template')->getPage()->addPPTag( 'total_cost', number_format($online_cost,2) );
  489. $this->registry->getObject('template')->getPage()->addPPTag( 'sitename', $this->registry->getSetting('sitename') );
  490. $this->registry->getObject('template')->getPage()->addPPTag( 'paypal_email_address',$this->registry->getSetting('store.paypal.email') );
  491. $notify_url = $this->registry->buildURL( array( 'eventsbasket', 'ping' ), '', false );
  492. $this->registry->getObject('template')->getPage()->addPPTag( 'notify_url', $notify_url );
  493. $thanks_url = $this->registry->buildURL( array( 'eventsbasket', 'ping-thanks' ), '', false );
  494. $this->registry->getObject('template')->getPage()->addPPTag( 'thanks_url', $thanks_url );
  495. $cancel_url = $this->registry->buildURL( array( 'eventsbasket', 'ping-cancel' ), '', false );
  496. $this->registry->getObject('template')->getPage()->addPPTag( 'cancel_url', $cancel_url );
  497. $this->registry->getObject('template')->addTemplateBit( 'content', 'eventsbasket/pay.tpl.php' );
  498. $this->registry->getObject('template')->getPage()->addTag( 'online_cost_message', ' This is the amount that you will be charged through Paypal.' );
  499. $this->registry->getObject('template')->getPage()->addTag( 'offline_cost_message', ' This amount will need to be paid separately offline by cheque or BACS (details will be emailed to you).' );
  500. $this->registry->getObject('template')->getPage()->addTag( 'confirm', '' );
  501. }
  502. elseif( ($data['offline_cost'] > 0 && $data['offline_paid'] == 0) && ($data['online_cost'] == 0 || $data['online_paid'] == 1) )
  503. {
  504. //there is an offline cost and no online cost or the online cost has already been paid for.
  505. $this->registry->getObject('template')->getPage()->addTag( 'paypal', '' );
  506. $this->registry->getObject('template')->getPage()->addTag( 'total_cost', $total_cost );
  507. $this->registry->getObject('template')->getPage()->addTag( 'online_cost_message', '' );
  508. $this->registry->getObject('template')->getPage()->addTag( 'offline_cost_message', '' );
  509. $this->registry->getObject('template')->addTemplateBit( 'content', 'eventsbasket/pay-only-offline.tpl.php' );
  510. $this->registry->getObject('template')->getPage()->addTag( 'confirm', 'Your order has been confirmed. Please arrange to pay the offline cost by cheque or BACS (details will be emailed to you). Thank you again.' );
  511. }
  512. else
  513. {
  514. //the order has been paid for in full.
  515. $this->registry->getObject('template')->addTemplateBit( 'content', 'eventsbasket/paid-in-full.tpl.php' );
  516. $this->registry->getObject('template')->getPage()->addTag( 'online', ($data['online_cost'] > 0) ? 'The amount you paid online was: �' . $data['online_cost'] : '' );
  517. $this->registry->getObject('template')->getPage()->addTag( 'offline', ($data['offline_cost'] > 0) ? 'The amount you paid offline was: �' . $data['offline_cost'] : '' );
  518. }
  519. }
  520. else
  521. {
  522. $update = array();
  523. $update['offline_cost'] = $this->registry->getObject('db')->sanitizeData( $total_cost );
  524. $update['online_cost'] = 0;
  525. $this->registry->getObject('db')->updateRecords( 'eventorders', $update, 'ID=' . $order );
  526. $this->registry->getObject('template')->getPage()->addTag( 'total_cost', $total_cost );
  527. $this->registry->getObject('template')->getPage()->addTag( 'offline_cost', $total_cost );
  528. $this->registry->getObject('template')->getPage()->addTag( 'offline_cost_message', ' This amount will need to be paid separately offline by cheque or BACS (details will be emailed to you).' );
  529. $this->registry->getObject('template')->addTemplateBit( 'content', 'eventsbasket/pay-offline.tpl.php' );
  530. $this->registry->getObject('template')->getPage()->addTag( 'confirm', 'Your order has been confirmed. Please arrange to pay the offline cost by cheque or BACS (details will be emailed to you). Thank you again.' );
  531. }
  532. $this->registry->getObject('template')->getPage()->addTag( 'order', $order );
  533. $this->registry->getObject('template')->buildFromTemplates( 'header.tpl.php', 'eventsbasket/payment.tpl.php', 'footer.tpl.php');
  534. }
  535. else
  536. {
  537. $this->registry->errorPage( 'Invalid order', 'Sorry, the order you selected was not found');
  538. }
  539. }
  540. private function emailCustomer( $type, $oid, $cost, $email, $na, $address, $phone, $payment )
  541. {
  542. $this->registry->getObject('mailout')->startFresh();
  543. $this->registry->getObject('mailout')->setTo( $email);
  544. $this->registry->getObject('mailout')->setSender( $this->registry->getSetting('adminEmailAddress'));
  545. $this->registry->getObject('mailout')->setFromName( $this->registry->getSetting('cms_name') );
  546. $this->registry->getObject('mailout')->setSubject( $this->registry->getSetting('sitename') . ' Event Booking Submission');
  547. $this->registry->getObject('mailout')->buildFromTemplates('customerorder-'. $type . '.tpl.php');
  548. //$tags = $this->values;
  549. $tags[ 'cost' ] = $cost;
  550. $tags[ 'order_id' ] = $oid;
  551. $tags[ 'name' ] = $na;
  552. $tags[ 'address' ] = $address;
  553. $tags[ 'phone' ] = $phone;
  554. $tags[ 'payment' ] = $payment;
  555. $tags[ 'bookings' ] = $this->buildDetailsForEmail();
  556. $tags[ 'email' ] = $email;
  557. $this->registry->getObject('mailout')->replaceTags( $tags );
  558. $this->registry->getObject('mailout')->setMethod('sendmail');
  559. $this->registry->getObject('mailout')->send();
  560. $this->registry->getObject('mailout')->startFresh();
  561. $this->registry->getObject('mailout')->setTo( "bookings@equalitysouthwest.org.uk" );
  562. $this->registry->getObject('mailout')->setSender( $this->registry->getSetting('adminEmailAddress'));
  563. $this->registry->getObject('mailout')->setFromName( $this->registry->getSetting('cms_name') );
  564. $this->registry->getObject('mailout')->setSubject( $this->registry->getSetting('sitename') . ' Event Booking Submission');
  565. $this->registry->getObject('mailout')->buildFromTemplates('customerorder-'. $type . '.tpl.php');
  566. //$tags = $this->values;
  567. $tags[ 'cost' ] = $cost;
  568. $tags[ 'order_id' ] = $oid;
  569. $tags[ 'name' ] = $na;
  570. $tags[ 'address' ] = $address;
  571. $tags[ 'phone' ] = $phone;
  572. $tags[ 'payment' ] = $payment;
  573. $tags[ 'bookings' ] = $this->buildDetailsForEmail();
  574. $tags[ 'email' ] = $email;
  575. $this->registry->getObject('mailout')->replaceTags( $tags );
  576. $this->registry->getObject('mailout')->setMethod('sendmail');
  577. $this->registry->getObject('mailout')->send();
  578. }
  579. private function buildDetailsForEmail()
  580. {
  581. $contents = $this->basket->getContents();
  582. $bookings_for_email = "";
  583. foreach( $contents as $content )
  584. {
  585. $bookings_for_email .= "Event: " . $content['event_name']."
  586. Attendees: ";
  587. $sql = "SELECT * FROM eventbasket_attendees WHERE basket_id=" . $content['basket_id'];
  588. $this->registry->getObject('db')->executeQuery( $sql );
  589. while( $row = $this->registry->getObject('db')->getRows() )
  590. {
  591. $bookings_for_email .= " " . $row['name'] ."; ";
  592. }
  593. $bookings_for_email .= "
  594. Date: ".$content['event_date_full']."
  595. Venue: " . $content['venue_name_full'] ."
  596. Time: " . $content['start_time'] ." - " . $content['end_time'] ."
  597. Price: ".$content['event_price']."
  598. ";
  599. }
  600. return $bookings_for_email;
  601. }
  602. private function emailAdmin( $type, $oid, $cost )
  603. {
  604. $this->registry->getObject('mailout')->startFresh();
  605. $this->registry->getObject('mailout')->setTo( $this->registry->getSetting('adminEmailAddress') );
  606. $this->registry->getObject('mailout')->setSender( $this->registry->getSetting('adminEmailAddress') );
  607. $this->registry->getObject('mailout')->setFromName( $this->registry->getSetting('cms_name') );
  608. $this->registry->getObject('mailout')->setSubject( $this->registry->getSetting('sitename') . ' Event Booking Submission');
  609. $this->registry->getObject('mailout')->buildFromTemplates('neworder-'. $type . '.tpl.php');
  610. //$tags = $this->values;
  611. $tags[ 'cost' ] = $cost;
  612. $tags[ 'order_id' ] = $oid;
  613. $this->registry->getObject('mailout')->replaceTags( $tags );
  614. $this->registry->getObject('mailout')->setMethod('sendmail');
  615. $this->registry->getObject('mailout')->send();
  616. $this->registry->getObject('mailout')->startFresh();
  617. $this->registry->getObject('mailout')->setTo( "bookings@equalitysouthwest.org.uk" );
  618. $this->registry->getObject('mailout')->setSender( $this->registry->getSetting('adminEmailAddress') );
  619. $this->registry->getObject('mailout')->setFromName( $this->registry->getSetting('cms_name') );
  620. $this->registry->getObject('mailout')->setSubject( $this->registry->getSetting('sitename') . ' Event Booking Submission');
  621. $this->registry->getObject('mailout')->buildFromTemplates('neworder-'. $type . '.tpl.php');
  622. //$tags = $this->values;
  623. $tags[ 'cost' ] = $cost;
  624. $tags[ 'order_id' ] = $oid;
  625. $this->registry->getObject('mailout')->replaceTags( $tags );
  626. $this->registry->getObject('mailout')->setMethod('sendmail');
  627. $this->registry->getObject('mailout')->send();
  628. }
  629. private function emptyBasket()
  630. {
  631. $contents = $this->basket->getContents();
  632. $bookings_for_email = "";
  633. foreach( $contents as $content )
  634. {
  635. // remove the basket stuff!
  636. $sql = "DELETE FROM eventbasket WHERE ID=" . $content['basket_id'];
  637. $this->registry->getObject('db')->executeQuery( $sql );
  638. $sql = "DELETE w.* FROM eventbasket_attendees_workshops w, eventbasket_attendees a WHERE a.ID = w.attendee_id AND a.basket_id=" . $content['basket_id'];
  639. $this->registry->getObject('db')->executeQuery( $sql );
  640. $sql = "DELETE FROM eventbasket_attendees WHERE basket_id=" . $content['basket_id'];
  641. $this->registry->getObject('db')->executeQuery( $sql );
  642. }
  643. }
  644. }
  645. ?>