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

https://github.com/sharpmachine/whiteantelopestudio.com · PHP · 242 lines · 164 code · 14 blank · 64 comment · 30 complexity · f329f5c8dc222234df3222908ed02159 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 __construct( $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. $ticket_data['event_id'] = $this->event_id;
  131. $EM_Ticket->get_post($ticket_data);
  132. $this->tickets[] = $EM_Ticket;
  133. }
  134. }
  135. }else{
  136. //we create a blank standard ticket
  137. $EM_Ticket = new EM_Ticket(array(
  138. 'event_id' => $this->event_id,
  139. 'ticket_name' => __('Standard','dbem')
  140. ));
  141. $this->tickets[] = $EM_Ticket;
  142. }
  143. return apply_filters('em_tickets_get_post', count($this->errors) == 0, $this);
  144. }
  145. /**
  146. * Go through the tickets in this object and validate them
  147. */
  148. function validate(){
  149. $this->errors = array();
  150. foreach($this->tickets as $EM_Ticket){
  151. if( !$EM_Ticket->validate() ){
  152. $this->add_error($EM_Ticket->get_errors());
  153. }
  154. }
  155. return apply_filters('em_tickets_validate', count($this->errors) == 0, $this);
  156. }
  157. /**
  158. * Save tickets into DB
  159. */
  160. function save(){
  161. $result = true;
  162. foreach( $this->tickets as $EM_Ticket ){
  163. /* @var $EM_Ticket EM_Ticket */
  164. $EM_Ticket->event_id = $this->event_id; //pass on saved event_data
  165. if( !$EM_Ticket->save() ){
  166. $result = false;
  167. $this->add_error($EM_Ticket->get_errors());
  168. }
  169. }
  170. return apply_filters('em_tickets_save', $result, $this);
  171. }
  172. /**
  173. * Goes through each ticket and populates it with the bookings made
  174. */
  175. function get_ticket_bookings(){
  176. foreach( $this->tickets as $EM_Ticket ){
  177. $EM_Ticket->get_bookings();
  178. }
  179. }
  180. /**
  181. * 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.
  182. * @param boolean $force_refresh
  183. * @return int
  184. */
  185. function get_spaces( $force_refresh=false ){
  186. $spaces = 0;
  187. if($force_refresh || $this->spaces == 0){
  188. foreach( $this->tickets as $EM_Ticket ){
  189. /* @var $EM_Ticket EM_Ticket */
  190. $spaces += $EM_Ticket->get_spaces();
  191. }
  192. $this->spaces = $spaces;
  193. }
  194. return apply_filters('em_booking_get_spaces',$this->spaces,$this);
  195. }
  196. /**
  197. * Returns the collumns used in ticket public pricing tables/forms
  198. * @param unknown_type $EM_Event
  199. */
  200. function get_ticket_collumns($EM_Event = false){
  201. if( !$EM_Event ) $EM_Event = $this->get_event();
  202. $collumns = array( 'type' => __('Ticket Type','dbem'), 'price' => __('Price','dbem'), 'spaces' => __('Spaces','dbem'));
  203. if( $EM_Event->is_free() ) unset($collumns['price']); //add event price
  204. return apply_filters('em_booking_form_tickets_cols', $collumns, $EM_Event );
  205. }
  206. //Iterator Implementation
  207. public function rewind(){
  208. reset($this->tickets);
  209. }
  210. public function current(){
  211. $var = current($this->tickets);
  212. return $var;
  213. }
  214. public function key(){
  215. $var = key($this->tickets);
  216. return $var;
  217. }
  218. public function next(){
  219. $var = next($this->tickets);
  220. return $var;
  221. }
  222. public function valid(){
  223. $key = key($this->tickets);
  224. $var = ($key !== NULL && $key !== FALSE);
  225. return $var;
  226. }
  227. }
  228. ?>