PageRenderTime 71ms CodeModel.GetById 17ms RepoModel.GetById 2ms app.codeStats 0ms

/wp-content/plugins/events-manager/classes/em-tickets.php

https://bitbucket.org/antonyravel/cape-resorts
PHP | 238 lines | 160 code | 14 blank | 64 comment | 29 complexity | 4144aa789f93558ff021184599714ee9 MD5 | raw file
  1. <?php
  2. /**
  3. * Deals with the ticket info for an event
  4. * @author marcus
  5. *
  6. */
  7. class EM_Tickets extends EM_Object implements Iterator{
  8. /**
  9. * Array of EM_Ticket objects for a specific event
  10. * @var array
  11. */
  12. var $tickets = array();
  13. /**
  14. * @var int
  15. */
  16. var $event_id;
  17. /**
  18. * @var EM_Booking
  19. */
  20. var $booking;
  21. var $spaces;
  22. /**
  23. * Creates an EM_Tickets instance
  24. * @param mixed $event
  25. */
  26. function EM_Tickets( $object = false ){
  27. global $wpdb;
  28. if( is_numeric($object) || (is_object($object) && in_array(get_class($object), array("EM_Event","EM_Booking"))) ){
  29. $this->event_id = (is_object($object)) ? $object->event_id:$object;
  30. if( is_object($object) && get_class($object) == 'EM_Booking' ){
  31. $sql = "SELECT * FROM ". EM_TICKETS_TABLE ." WHERE ticket_id IN (SELECT ticket_id FROM ".EM_TICKETS_BOOKINGS_TABLE." WHERE booking_id='{$object->booking_id}') ORDER BY ".get_option('dbem_bookings_tickets_orderby');
  32. }else{
  33. $sql = "SELECT * FROM ". EM_TICKETS_TABLE ." WHERE event_id ='{$this->event_id}' ORDER BY ".get_option('dbem_bookings_tickets_orderby');
  34. }
  35. $tickets = $wpdb->get_results($sql, ARRAY_A);
  36. foreach ($tickets as $ticket){
  37. $EM_Ticket = new EM_Ticket($ticket);
  38. $EM_Ticket->event_id = $this->event_id;
  39. $this->tickets[$EM_Ticket->ticket_id] = $EM_Ticket;
  40. }
  41. }elseif( is_array($object) ){ //expecting an array of EM_Ticket objects or ticket db array
  42. if( is_object(current($object)) && get_class(current($object)) == 'EM_Ticket' ){
  43. foreach($object as $EM_Ticket){
  44. $this->tickets[$EM_Ticket->ticket_id] = $EM_Ticket;
  45. }
  46. }else{
  47. foreach($object as $ticket){
  48. $EM_Ticket = new EM_Ticket($ticket);
  49. $EM_Ticket->event_id = $this->event_id;
  50. $this->tickets[$EM_Ticket->ticket_id] = $EM_Ticket;
  51. }
  52. }
  53. }
  54. do_action('em_tickets', $this, $object);
  55. }
  56. /**
  57. * @return EM_Event
  58. */
  59. function get_event(){
  60. global $EM_Event;
  61. if( is_object($EM_Event) && $EM_Event->event_id == $this->event_id ){
  62. return $EM_Event;
  63. }else{
  64. return new EM_Event($this->event_id);
  65. }
  66. }
  67. /**
  68. * does this ticket exist?
  69. * @return bool
  70. */
  71. function has_ticket($ticket_id){
  72. foreach( $this->tickets as $EM_Ticket){
  73. if($EM_Ticket->ticket_id == $ticket_id){
  74. return apply_filters('em_tickets_has_ticket',true, $EM_Ticket, $this);
  75. }
  76. }
  77. return apply_filters('em_tickets_has_ticket',false, false,$this);
  78. }
  79. /**
  80. * Get the first EM_Ticket object in this instance. Returns false if no tickets available.
  81. * @return EM_Ticket
  82. */
  83. function get_first(){
  84. if( count($this->tickets) > 0 ){
  85. foreach($this->tickets as $EM_Ticket){
  86. return $EM_Ticket;
  87. }
  88. }else{
  89. return false;
  90. }
  91. }
  92. /**
  93. * Delete tickets in thie object
  94. * @return boolean
  95. */
  96. function delete(){
  97. global $wpdb;
  98. //get all the ticket ids
  99. $result = false;
  100. $ticket_ids = array();
  101. foreach( $this->tickets as $EM_Ticket ){
  102. $ticket_ids[] = $EM_Ticket->ticket_id;
  103. }
  104. //check that tickets don't have bookings
  105. if(count($ticket_ids) > 0){
  106. $bookings = $wpdb->get_var("SELECT COUNT(*) FROM ". EM_TICKETS_BOOKINGS_TABLE." WHERE ticket_id IN (".implode(',',$ticket_ids).")");
  107. if( $bookings > 0 ){
  108. $result = false;
  109. $this->add_error(__('You cannot delete tickets if there are any bookings associated with them. Please delete these bookings first.','dbem'));
  110. }else{
  111. $result = $wpdb->query("DELETE FROM ".EM_TICKETS_TABLE." WHERE event_id IN (".implode(',',$ticket_ids).")");
  112. }
  113. }
  114. return ($result !== false);
  115. }
  116. /**
  117. * Retrieve multiple ticket info via POST
  118. * @return boolean
  119. */
  120. function get_post(){
  121. //Build Event Array
  122. do_action('em_tickets_get_post_pre', $this);
  123. $this->tickets = array(); //clean current tickets out
  124. if( !empty($_POST['em_tickets']) && is_array($_POST['em_tickets']) ){
  125. //get all ticket data and create objects
  126. global $allowedposttags;
  127. foreach($_POST['em_tickets'] as $row => $ticket_data){
  128. if( $row > 0 ){
  129. $EM_Ticket = new EM_Ticket();
  130. $EM_Ticket->get_post($ticket_data);
  131. $this->tickets[] = $EM_Ticket;
  132. }
  133. }
  134. }else{
  135. //we create a blank standard ticket
  136. $EM_Ticket = new EM_Ticket(array(
  137. 'event_id' => $this->event_id,
  138. 'ticket_name' => __('Standard','dbem')
  139. ));
  140. $this->tickets[] = $EM_Ticket;
  141. }
  142. return apply_filters('em_tickets_get_post', count($this->errors) == 0, $this);
  143. }
  144. /**
  145. * Go through the tickets in this object and validate them
  146. */
  147. function validate(){
  148. $this->errors = array();
  149. foreach($this->tickets as $EM_Ticket){
  150. if( !$EM_Ticket->validate() ){
  151. $this->add_error($EM_Ticket->get_errors());
  152. }
  153. }
  154. return apply_filters('em_tickets_validate', count($this->errors) == 0, $this);
  155. }
  156. /**
  157. * Save tickets into DB
  158. */
  159. function save(){
  160. $errors = array();
  161. foreach( $this->tickets as $EM_Ticket ){
  162. /* @var $EM_Ticket EM_Ticket */
  163. $EM_Ticket->event_id = $this->event_id; //pass on saved event_data
  164. $errors[] = $EM_Ticket->save();
  165. }
  166. return apply_filters('em_tickets_save', !in_array(false, $errors), $this);
  167. }
  168. /**
  169. * Goes through each ticket and populates it with the bookings made
  170. */
  171. function get_ticket_bookings(){
  172. foreach( $this->tickets as $EM_Ticket ){
  173. $EM_Ticket->get_bookings();
  174. }
  175. }
  176. /**
  177. * Get the total number of spaces this event has. This will show the lower value of event global spaces limit or total ticket spaces. Setting $force_refresh to true will recheck spaces, even if previously done so.
  178. * @param boolean $force_refresh
  179. * @return int
  180. */
  181. function get_spaces( $force_refresh=false ){
  182. $spaces = 0;
  183. if($force_refresh || $this->spaces == 0){
  184. foreach( $this->tickets as $EM_Ticket ){
  185. /* @var $EM_Ticket EM_Ticket */
  186. $spaces += $EM_Ticket->get_spaces();
  187. }
  188. $this->spaces = $spaces;
  189. }
  190. return apply_filters('em_booking_get_spaces',$this->spaces,$this);
  191. }
  192. /**
  193. * Returns the collumns used in ticket public pricing tables/forms
  194. * @param unknown_type $EM_Event
  195. */
  196. function get_ticket_collumns($EM_Event = false){
  197. if( !$EM_Event ) $EM_Event = $this->get_event();
  198. $collumns = array( 'type' => __('Ticket Type','dbem'), 'price' => __('Price','dbem'), 'spaces' => __('Spaces','dbem'));
  199. if( $EM_Event->is_free() ) unset($collumns['price']); //add event price
  200. return apply_filters('em_booking_form_tickets_cols', $collumns, $EM_Event );
  201. }
  202. //Iterator Implementation
  203. public function rewind(){
  204. reset($this->tickets);
  205. }
  206. public function current(){
  207. $var = current($this->tickets);
  208. return $var;
  209. }
  210. public function key(){
  211. $var = key($this->tickets);
  212. return $var;
  213. }
  214. public function next(){
  215. $var = next($this->tickets);
  216. return $var;
  217. }
  218. public function valid(){
  219. $key = key($this->tickets);
  220. $var = ($key !== NULL && $key !== FALSE);
  221. return $var;
  222. }
  223. }
  224. ?>