@@ -13,755 +13,755 @@ |
||
| 13 | 13 | class EEM_Datetime extends EEM_Soft_Delete_Base |
| 14 | 14 | { |
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * @var EEM_Datetime $_instance |
|
| 18 | - */ |
|
| 19 | - protected static $_instance; |
|
| 20 | - |
|
| 21 | - |
|
| 22 | - /** |
|
| 23 | - * private constructor to prevent direct creation |
|
| 24 | - * |
|
| 25 | - * @param string $timezone A string representing the timezone we want to set for returned Date Time Strings |
|
| 26 | - * (and any incoming timezone data that gets saved). |
|
| 27 | - * Note this just sends the timezone info to the date time model field objects. |
|
| 28 | - * Default is NULL |
|
| 29 | - * (and will be assumed using the set timezone in the 'timezone_string' wp option) |
|
| 30 | - * @throws EE_Error |
|
| 31 | - * @throws InvalidArgumentException |
|
| 32 | - * @throws InvalidArgumentException |
|
| 33 | - */ |
|
| 34 | - protected function __construct($timezone) |
|
| 35 | - { |
|
| 36 | - $this->singular_item = esc_html__('Datetime', 'event_espresso'); |
|
| 37 | - $this->plural_item = esc_html__('Datetimes', 'event_espresso'); |
|
| 38 | - $this->_tables = array( |
|
| 39 | - 'Datetime' => new EE_Primary_Table('esp_datetime', 'DTT_ID'), |
|
| 40 | - ); |
|
| 41 | - $this->_fields = array( |
|
| 42 | - 'Datetime' => array( |
|
| 43 | - 'DTT_ID' => new EE_Primary_Key_Int_Field( |
|
| 44 | - 'DTT_ID', |
|
| 45 | - esc_html__('Datetime ID', 'event_espresso') |
|
| 46 | - ), |
|
| 47 | - 'EVT_ID' => new EE_Foreign_Key_Int_Field( |
|
| 48 | - 'EVT_ID', |
|
| 49 | - esc_html__('Event ID', 'event_espresso'), |
|
| 50 | - false, |
|
| 51 | - 0, |
|
| 52 | - 'Event' |
|
| 53 | - ), |
|
| 54 | - 'DTT_name' => new EE_Plain_Text_Field( |
|
| 55 | - 'DTT_name', |
|
| 56 | - esc_html__('Datetime Name', 'event_espresso'), |
|
| 57 | - false, |
|
| 58 | - '' |
|
| 59 | - ), |
|
| 60 | - 'DTT_description' => new EE_Post_Content_Field( |
|
| 61 | - 'DTT_description', |
|
| 62 | - esc_html__('Description for Datetime', 'event_espresso'), |
|
| 63 | - false, |
|
| 64 | - '' |
|
| 65 | - ), |
|
| 66 | - 'DTT_EVT_start' => new EE_Datetime_Field( |
|
| 67 | - 'DTT_EVT_start', |
|
| 68 | - esc_html__('Start time/date of Event', 'event_espresso'), |
|
| 69 | - false, |
|
| 70 | - EE_Datetime_Field::now, |
|
| 71 | - $timezone |
|
| 72 | - ), |
|
| 73 | - 'DTT_EVT_end' => new EE_Datetime_Field( |
|
| 74 | - 'DTT_EVT_end', |
|
| 75 | - esc_html__('End time/date of Event', 'event_espresso'), |
|
| 76 | - false, |
|
| 77 | - EE_Datetime_Field::now, |
|
| 78 | - $timezone |
|
| 79 | - ), |
|
| 80 | - 'DTT_reg_limit' => new EE_Infinite_Integer_Field( |
|
| 81 | - 'DTT_reg_limit', |
|
| 82 | - esc_html__('Registration Limit for this time', 'event_espresso'), |
|
| 83 | - true, |
|
| 84 | - EE_INF |
|
| 85 | - ), |
|
| 86 | - 'DTT_sold' => new EE_Integer_Field( |
|
| 87 | - 'DTT_sold', |
|
| 88 | - esc_html__('How many sales for this Datetime that have occurred', 'event_espresso'), |
|
| 89 | - true, |
|
| 90 | - 0 |
|
| 91 | - ), |
|
| 92 | - 'DTT_reserved' => new EE_Integer_Field( |
|
| 93 | - 'DTT_reserved', |
|
| 94 | - esc_html__('Quantity of tickets reserved, but not yet fully purchased', 'event_espresso'), |
|
| 95 | - false, |
|
| 96 | - 0 |
|
| 97 | - ), |
|
| 98 | - 'DTT_is_primary' => new EE_Boolean_Field( |
|
| 99 | - 'DTT_is_primary', |
|
| 100 | - esc_html__('Flag indicating datetime is primary one for event', 'event_espresso'), |
|
| 101 | - false, |
|
| 102 | - false |
|
| 103 | - ), |
|
| 104 | - 'DTT_order' => new EE_Integer_Field( |
|
| 105 | - 'DTT_order', |
|
| 106 | - esc_html__('The order in which the Datetime is displayed', 'event_espresso'), |
|
| 107 | - false, |
|
| 108 | - 0 |
|
| 109 | - ), |
|
| 110 | - 'DTT_parent' => new EE_Integer_Field( |
|
| 111 | - 'DTT_parent', |
|
| 112 | - esc_html__('Indicates what DTT_ID is the parent of this DTT_ID', 'event_espresso'), |
|
| 113 | - true, |
|
| 114 | - 0 |
|
| 115 | - ), |
|
| 116 | - 'DTT_deleted' => new EE_Trashed_Flag_Field( |
|
| 117 | - 'DTT_deleted', |
|
| 118 | - esc_html__('Flag indicating datetime is archived', 'event_espresso'), |
|
| 119 | - false, |
|
| 120 | - false |
|
| 121 | - ), |
|
| 122 | - ), |
|
| 123 | - ); |
|
| 124 | - $this->_model_relations = array( |
|
| 125 | - 'Ticket' => new EE_HABTM_Relation('Datetime_Ticket'), |
|
| 126 | - 'Event' => new EE_Belongs_To_Relation(), |
|
| 127 | - 'Checkin' => new EE_Has_Many_Relation(), |
|
| 128 | - 'Datetime_Ticket' => new EE_Has_Many_Relation(), |
|
| 129 | - ); |
|
| 130 | - $path_to_event_model = 'Event'; |
|
| 131 | - $this->model_chain_to_password = $path_to_event_model; |
|
| 132 | - $this->_model_chain_to_wp_user = $path_to_event_model; |
|
| 133 | - // this model is generally available for reading |
|
| 134 | - $this->_cap_restriction_generators[ EEM_Base::caps_read ] = new EE_Restriction_Generator_Event_Related_Public( |
|
| 135 | - $path_to_event_model |
|
| 136 | - ); |
|
| 137 | - $this->_cap_restriction_generators[ EEM_Base::caps_read_admin ] = new EE_Restriction_Generator_Event_Related_Protected( |
|
| 138 | - $path_to_event_model |
|
| 139 | - ); |
|
| 140 | - $this->_cap_restriction_generators[ EEM_Base::caps_edit ] = new EE_Restriction_Generator_Event_Related_Protected( |
|
| 141 | - $path_to_event_model |
|
| 142 | - ); |
|
| 143 | - $this->_cap_restriction_generators[ EEM_Base::caps_delete ] = new EE_Restriction_Generator_Event_Related_Protected( |
|
| 144 | - $path_to_event_model, |
|
| 145 | - EEM_Base::caps_edit |
|
| 146 | - ); |
|
| 147 | - parent::__construct($timezone); |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - |
|
| 151 | - /** |
|
| 152 | - * create new blank datetime |
|
| 153 | - * |
|
| 154 | - * @access public |
|
| 155 | - * @return EE_Datetime[] array on success, FALSE on fail |
|
| 156 | - * @throws EE_Error |
|
| 157 | - * @throws InvalidArgumentException |
|
| 158 | - * @throws InvalidDataTypeException |
|
| 159 | - * @throws ReflectionException |
|
| 160 | - * @throws InvalidInterfaceException |
|
| 161 | - */ |
|
| 162 | - public function create_new_blank_datetime() |
|
| 163 | - { |
|
| 164 | - // makes sure timezone is always set. |
|
| 165 | - $timezone_string = $this->get_timezone(); |
|
| 166 | - /** |
|
| 167 | - * Filters the initial start date for the new datetime. |
|
| 168 | - * Any time included in this value will be overridden later so use additional filters to modify the time. |
|
| 169 | - * |
|
| 170 | - * @param int $start_date Unixtimestamp representing now + 30 days in seconds. |
|
| 171 | - * @return int unixtimestamp |
|
| 172 | - */ |
|
| 173 | - $start_date = apply_filters( |
|
| 174 | - 'FHEE__EEM_Datetime__create_new_blank_datetime__start_date', |
|
| 175 | - $this->current_time_for_query('DTT_EVT_start', true) + MONTH_IN_SECONDS |
|
| 176 | - ); |
|
| 177 | - /** |
|
| 178 | - * Filters the initial end date for the new datetime. |
|
| 179 | - * Any time included in this value will be overridden later so use additional filters to modify the time. |
|
| 180 | - * |
|
| 181 | - * @param int $end_data Unixtimestamp representing now + 30 days in seconds. |
|
| 182 | - * @return int unixtimestamp |
|
| 183 | - */ |
|
| 184 | - $end_date = apply_filters( |
|
| 185 | - 'FHEE__EEM_Datetime__create_new_blank_datetime__end_date', |
|
| 186 | - $this->current_time_for_query('DTT_EVT_end', true) + MONTH_IN_SECONDS |
|
| 187 | - ); |
|
| 188 | - $blank_datetime = EE_Datetime::new_instance( |
|
| 189 | - array( |
|
| 190 | - 'DTT_EVT_start' => $start_date, |
|
| 191 | - 'DTT_EVT_end' => $end_date, |
|
| 192 | - 'DTT_order' => 1, |
|
| 193 | - 'DTT_reg_limit' => EE_INF, |
|
| 194 | - ), |
|
| 195 | - $timezone_string |
|
| 196 | - ); |
|
| 197 | - /** |
|
| 198 | - * Filters the initial start time and format for the new EE_Datetime instance. |
|
| 199 | - * |
|
| 200 | - * @param array $start_time An array having size 2. First element is the time, second element is the time |
|
| 201 | - * format. |
|
| 202 | - * @return array |
|
| 203 | - */ |
|
| 204 | - $start_time = apply_filters( |
|
| 205 | - 'FHEE__EEM_Datetime__create_new_blank_datetime__start_time', |
|
| 206 | - ['8am', 'ga'] |
|
| 207 | - ); |
|
| 208 | - /** |
|
| 209 | - * Filters the initial end time and format for the new EE_Datetime instance. |
|
| 210 | - * |
|
| 211 | - * @param array $end_time An array having size 2. First element is the time, second element is the time |
|
| 212 | - * format |
|
| 213 | - * @return array |
|
| 214 | - */ |
|
| 215 | - $end_time = apply_filters( |
|
| 216 | - 'FHEE__EEM_Datetime__create_new_blank_datetime__end_time', |
|
| 217 | - ['5pm', 'ga'] |
|
| 218 | - ); |
|
| 219 | - $this->validateStartAndEndTimeForBlankDate($start_time, $end_time); |
|
| 220 | - $blank_datetime->set_start_time( |
|
| 221 | - $this->convert_datetime_for_query( |
|
| 222 | - 'DTT_EVT_start', |
|
| 223 | - $start_time[0], |
|
| 224 | - $start_time[1], |
|
| 225 | - $timezone_string |
|
| 226 | - ) |
|
| 227 | - ); |
|
| 228 | - $blank_datetime->set_end_time( |
|
| 229 | - $this->convert_datetime_for_query( |
|
| 230 | - 'DTT_EVT_end', |
|
| 231 | - $end_time[0], |
|
| 232 | - $end_time[1], |
|
| 233 | - $timezone_string |
|
| 234 | - ) |
|
| 235 | - ); |
|
| 236 | - return array($blank_datetime); |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - |
|
| 240 | - /** |
|
| 241 | - * Validates whether the start_time and end_time are in the expected format. |
|
| 242 | - * @param array $start_time |
|
| 243 | - * @param array $end_time |
|
| 244 | - * @throws InvalidArgumentException |
|
| 245 | - * @throws InvalidDataTypeException |
|
| 246 | - */ |
|
| 247 | - private function validateStartAndEndTimeForBlankDate($start_time, $end_time) |
|
| 248 | - { |
|
| 249 | - if (! is_array($start_time)) { |
|
| 250 | - throw new InvalidDataTypeException('start_time', $start_time, 'array'); |
|
| 251 | - } |
|
| 252 | - if (! is_array($end_time)) { |
|
| 253 | - throw new InvalidDataTypeException('end_time', $end_time, 'array'); |
|
| 254 | - } |
|
| 255 | - if (count($start_time) !== 2) { |
|
| 256 | - throw new InvalidArgumentException( |
|
| 257 | - sprintf( |
|
| 258 | - 'The variable %1$s is expected to be an array with two elements. The first item in the ' |
|
| 259 | - . 'array should be a valid time string, the second item in the array should be a valid time format', |
|
| 260 | - '$start_time' |
|
| 261 | - ) |
|
| 262 | - ); |
|
| 263 | - } |
|
| 264 | - if (count($end_time) !== 2) { |
|
| 265 | - throw new InvalidArgumentException( |
|
| 266 | - sprintf( |
|
| 267 | - 'The variable %1$s is expected to be an array with two elements. The first item in the ' |
|
| 268 | - . 'array should be a valid time string, the second item in the array should be a valid time format', |
|
| 269 | - '$end_time' |
|
| 270 | - ) |
|
| 271 | - ); |
|
| 272 | - } |
|
| 273 | - } |
|
| 274 | - |
|
| 275 | - |
|
| 276 | - /** |
|
| 277 | - * get event start date from db |
|
| 278 | - * |
|
| 279 | - * @access public |
|
| 280 | - * @param int $EVT_ID |
|
| 281 | - * @return EE_Datetime[] array on success, FALSE on fail |
|
| 282 | - * @throws EE_Error |
|
| 283 | - */ |
|
| 284 | - public function get_all_event_dates($EVT_ID = 0) |
|
| 285 | - { |
|
| 286 | - if (! $EVT_ID) { // on add_new_event event_id gets set to 0 |
|
| 287 | - return $this->create_new_blank_datetime(); |
|
| 288 | - } |
|
| 289 | - $results = $this->get_datetimes_for_event_ordered_by_DTT_order($EVT_ID); |
|
| 290 | - if (empty($results)) { |
|
| 291 | - return $this->create_new_blank_datetime(); |
|
| 292 | - } |
|
| 293 | - return $results; |
|
| 294 | - } |
|
| 295 | - |
|
| 296 | - |
|
| 297 | - /** |
|
| 298 | - * get all datetimes attached to an event ordered by the DTT_order field |
|
| 299 | - * |
|
| 300 | - * @public |
|
| 301 | - * @param int $EVT_ID event id |
|
| 302 | - * @param boolean $include_expired |
|
| 303 | - * @param boolean $include_deleted |
|
| 304 | - * @param int $limit If included then limit the count of results by |
|
| 305 | - * the given number |
|
| 306 | - * @return EE_Datetime[] |
|
| 307 | - * @throws EE_Error |
|
| 308 | - */ |
|
| 309 | - public function get_datetimes_for_event_ordered_by_DTT_order( |
|
| 310 | - $EVT_ID, |
|
| 311 | - $include_expired = true, |
|
| 312 | - $include_deleted = true, |
|
| 313 | - $limit = null |
|
| 314 | - ) { |
|
| 315 | - // sanitize EVT_ID |
|
| 316 | - $EVT_ID = absint($EVT_ID); |
|
| 317 | - $old_assumption = $this->get_assumption_concerning_values_already_prepared_by_model_object(); |
|
| 318 | - $this->assume_values_already_prepared_by_model_object(EEM_Base::prepared_for_use_in_db); |
|
| 319 | - $where_params = array('Event.EVT_ID' => $EVT_ID); |
|
| 320 | - $query_params = ! empty($limit) |
|
| 321 | - ? array( |
|
| 322 | - $where_params, |
|
| 323 | - 'limit' => $limit, |
|
| 324 | - 'order_by' => array('DTT_order' => 'ASC'), |
|
| 325 | - 'default_where_conditions' => 'none', |
|
| 326 | - ) |
|
| 327 | - : array( |
|
| 328 | - $where_params, |
|
| 329 | - 'order_by' => array('DTT_order' => 'ASC'), |
|
| 330 | - 'default_where_conditions' => 'none', |
|
| 331 | - ); |
|
| 332 | - if (! $include_expired) { |
|
| 333 | - $query_params[0]['DTT_EVT_end'] = array('>=', current_time('mysql', true)); |
|
| 334 | - } |
|
| 335 | - if ($include_deleted) { |
|
| 336 | - $query_params[0]['DTT_deleted'] = array('IN', array(true, false)); |
|
| 337 | - } |
|
| 338 | - /** @var EE_Datetime[] $result */ |
|
| 339 | - $result = $this->get_all($query_params); |
|
| 340 | - $this->assume_values_already_prepared_by_model_object($old_assumption); |
|
| 341 | - return $result; |
|
| 342 | - } |
|
| 343 | - |
|
| 344 | - |
|
| 345 | - /** |
|
| 346 | - * Gets the datetimes for the event (with the given limit), and orders them by "importance". |
|
| 347 | - * By importance, we mean that the primary datetimes are most important (DEPRECATED FOR NOW), |
|
| 348 | - * and then the earlier datetimes are the most important. |
|
| 349 | - * Maybe we'll want this to take into account datetimes that haven't already passed, but we don't yet. |
|
| 350 | - * |
|
| 351 | - * @param int $EVT_ID |
|
| 352 | - * @param int $limit |
|
| 353 | - * @return EE_Datetime[]|EE_Base_Class[] |
|
| 354 | - * @throws EE_Error |
|
| 355 | - */ |
|
| 356 | - public function get_datetimes_for_event_ordered_by_importance($EVT_ID = 0, $limit = null) |
|
| 357 | - { |
|
| 358 | - return $this->get_all( |
|
| 359 | - array( |
|
| 360 | - array('Event.EVT_ID' => $EVT_ID), |
|
| 361 | - 'limit' => $limit, |
|
| 362 | - 'order_by' => array('DTT_EVT_start' => 'ASC'), |
|
| 363 | - 'default_where_conditions' => 'none', |
|
| 364 | - ) |
|
| 365 | - ); |
|
| 366 | - } |
|
| 367 | - |
|
| 368 | - |
|
| 369 | - /** |
|
| 370 | - * @param int $EVT_ID |
|
| 371 | - * @param boolean $include_expired |
|
| 372 | - * @param boolean $include_deleted |
|
| 373 | - * @return EE_Datetime |
|
| 374 | - * @throws EE_Error |
|
| 375 | - */ |
|
| 376 | - public function get_oldest_datetime_for_event($EVT_ID, $include_expired = false, $include_deleted = false) |
|
| 377 | - { |
|
| 378 | - $results = $this->get_datetimes_for_event_ordered_by_start_time( |
|
| 379 | - $EVT_ID, |
|
| 380 | - $include_expired, |
|
| 381 | - $include_deleted, |
|
| 382 | - 1 |
|
| 383 | - ); |
|
| 384 | - if ($results) { |
|
| 385 | - return array_shift($results); |
|
| 386 | - } |
|
| 387 | - return null; |
|
| 388 | - } |
|
| 389 | - |
|
| 390 | - |
|
| 391 | - /** |
|
| 392 | - * Gets the 'primary' datetime for an event. |
|
| 393 | - * |
|
| 394 | - * @param int $EVT_ID |
|
| 395 | - * @param bool $try_to_exclude_expired |
|
| 396 | - * @param bool $try_to_exclude_deleted |
|
| 397 | - * @return \EE_Datetime |
|
| 398 | - * @throws EE_Error |
|
| 399 | - */ |
|
| 400 | - public function get_primary_datetime_for_event( |
|
| 401 | - $EVT_ID, |
|
| 402 | - $try_to_exclude_expired = true, |
|
| 403 | - $try_to_exclude_deleted = true |
|
| 404 | - ) { |
|
| 405 | - if ($try_to_exclude_expired) { |
|
| 406 | - $non_expired = $this->get_oldest_datetime_for_event($EVT_ID, false, false); |
|
| 407 | - if ($non_expired) { |
|
| 408 | - return $non_expired; |
|
| 409 | - } |
|
| 410 | - } |
|
| 411 | - if ($try_to_exclude_deleted) { |
|
| 412 | - $expired_even = $this->get_oldest_datetime_for_event($EVT_ID, true); |
|
| 413 | - if ($expired_even) { |
|
| 414 | - return $expired_even; |
|
| 415 | - } |
|
| 416 | - } |
|
| 417 | - return $this->get_oldest_datetime_for_event($EVT_ID, true, true); |
|
| 418 | - } |
|
| 419 | - |
|
| 420 | - |
|
| 421 | - /** |
|
| 422 | - * Gets ALL the datetimes for an event (including trashed ones, for now), ordered |
|
| 423 | - * only by start date |
|
| 424 | - * |
|
| 425 | - * @param int $EVT_ID |
|
| 426 | - * @param boolean $include_expired |
|
| 427 | - * @param boolean $include_deleted |
|
| 428 | - * @param int $limit |
|
| 429 | - * @return EE_Datetime[] |
|
| 430 | - * @throws EE_Error |
|
| 431 | - */ |
|
| 432 | - public function get_datetimes_for_event_ordered_by_start_time( |
|
| 433 | - $EVT_ID, |
|
| 434 | - $include_expired = true, |
|
| 435 | - $include_deleted = true, |
|
| 436 | - $limit = null |
|
| 437 | - ) { |
|
| 438 | - // sanitize EVT_ID |
|
| 439 | - $EVT_ID = absint($EVT_ID); |
|
| 440 | - $old_assumption = $this->get_assumption_concerning_values_already_prepared_by_model_object(); |
|
| 441 | - $this->assume_values_already_prepared_by_model_object(EEM_Base::prepared_for_use_in_db); |
|
| 442 | - $query_params = array( |
|
| 443 | - array( |
|
| 444 | - 'Event.EVT_ID' => $EVT_ID |
|
| 445 | - ), |
|
| 446 | - 'order_by' => array( |
|
| 447 | - 'DTT_EVT_start' => 'asc' |
|
| 448 | - ), |
|
| 449 | - 'default_where_conditions' => EEM_Base::default_where_conditions_this_only |
|
| 450 | - ); |
|
| 451 | - if (! $include_expired) { |
|
| 452 | - $query_params[0]['DTT_EVT_end'] = array('>=', current_time('mysql', true)); |
|
| 453 | - } |
|
| 454 | - if ($include_deleted) { |
|
| 455 | - $query_params[0]['DTT_deleted'] = array('IN', array(true, false)); |
|
| 456 | - } |
|
| 457 | - if ($limit) { |
|
| 458 | - $query_params['limit'] = $limit; |
|
| 459 | - } |
|
| 460 | - /** @var EE_Datetime[] $result */ |
|
| 461 | - $result = $this->get_all($query_params); |
|
| 462 | - $this->assume_values_already_prepared_by_model_object($old_assumption); |
|
| 463 | - return $result; |
|
| 464 | - } |
|
| 465 | - |
|
| 466 | - |
|
| 467 | - /** |
|
| 468 | - * Gets ALL the datetimes for an ticket (including trashed ones, for now), ordered |
|
| 469 | - * only by start date |
|
| 470 | - * |
|
| 471 | - * @param int $TKT_ID |
|
| 472 | - * @param boolean $include_expired |
|
| 473 | - * @param boolean $include_deleted |
|
| 474 | - * @param int $limit |
|
| 475 | - * @return EE_Datetime[] |
|
| 476 | - * @throws EE_Error |
|
| 477 | - */ |
|
| 478 | - public function get_datetimes_for_ticket_ordered_by_start_time( |
|
| 479 | - $TKT_ID, |
|
| 480 | - $include_expired = true, |
|
| 481 | - $include_deleted = true, |
|
| 482 | - $limit = null |
|
| 483 | - ) { |
|
| 484 | - // sanitize TKT_ID |
|
| 485 | - $TKT_ID = absint($TKT_ID); |
|
| 486 | - $old_assumption = $this->get_assumption_concerning_values_already_prepared_by_model_object(); |
|
| 487 | - $this->assume_values_already_prepared_by_model_object(EEM_Base::prepared_for_use_in_db); |
|
| 488 | - $query_params = array(array('Ticket.TKT_ID' => $TKT_ID), 'order_by' => array('DTT_EVT_start' => 'asc')); |
|
| 489 | - if (! $include_expired) { |
|
| 490 | - $query_params[0]['DTT_EVT_end'] = array('>=', current_time('mysql', true)); |
|
| 491 | - } |
|
| 492 | - if ($include_deleted) { |
|
| 493 | - $query_params[0]['DTT_deleted'] = array('IN', array(true, false)); |
|
| 494 | - } |
|
| 495 | - if ($limit) { |
|
| 496 | - $query_params['limit'] = $limit; |
|
| 497 | - } |
|
| 498 | - /** @var EE_Datetime[] $result */ |
|
| 499 | - $result = $this->get_all($query_params); |
|
| 500 | - $this->assume_values_already_prepared_by_model_object($old_assumption); |
|
| 501 | - return $result; |
|
| 502 | - } |
|
| 503 | - |
|
| 504 | - |
|
| 505 | - /** |
|
| 506 | - * Gets all the datetimes for a ticket (including trashed ones, for now), ordered by the DTT_order for the |
|
| 507 | - * datetimes. |
|
| 508 | - * |
|
| 509 | - * @param int $TKT_ID ID of ticket to retrieve the datetimes for |
|
| 510 | - * @param boolean $include_expired whether to include expired datetimes or not |
|
| 511 | - * @param boolean $include_deleted whether to include trashed datetimes or not. |
|
| 512 | - * @param int|null $limit if null, no limit, if int then limit results by |
|
| 513 | - * that number |
|
| 514 | - * @return EE_Datetime[] |
|
| 515 | - * @throws EE_Error |
|
| 516 | - */ |
|
| 517 | - public function get_datetimes_for_ticket_ordered_by_DTT_order( |
|
| 518 | - $TKT_ID, |
|
| 519 | - $include_expired = true, |
|
| 520 | - $include_deleted = true, |
|
| 521 | - $limit = null |
|
| 522 | - ) { |
|
| 523 | - // sanitize id. |
|
| 524 | - $TKT_ID = absint($TKT_ID); |
|
| 525 | - $old_assumption = $this->get_assumption_concerning_values_already_prepared_by_model_object(); |
|
| 526 | - $this->assume_values_already_prepared_by_model_object(EEM_Base::prepared_for_use_in_db); |
|
| 527 | - $where_params = array('Ticket.TKT_ID' => $TKT_ID); |
|
| 528 | - $query_params = array($where_params, 'order_by' => array('DTT_order' => 'ASC')); |
|
| 529 | - if (! $include_expired) { |
|
| 530 | - $query_params[0]['DTT_EVT_end'] = array('>=', current_time('mysql', true)); |
|
| 531 | - } |
|
| 532 | - if ($include_deleted) { |
|
| 533 | - $query_params[0]['DTT_deleted'] = array('IN', array(true, false)); |
|
| 534 | - } |
|
| 535 | - if ($limit) { |
|
| 536 | - $query_params['limit'] = $limit; |
|
| 537 | - } |
|
| 538 | - /** @var EE_Datetime[] $result */ |
|
| 539 | - $result = $this->get_all($query_params); |
|
| 540 | - $this->assume_values_already_prepared_by_model_object($old_assumption); |
|
| 541 | - return $result; |
|
| 542 | - } |
|
| 543 | - |
|
| 544 | - |
|
| 545 | - /** |
|
| 546 | - * Gets the most important datetime for a particular event (ie, the primary event usually. But if for some WACK |
|
| 547 | - * reason it doesn't exist, we consider the earliest event the most important) |
|
| 548 | - * |
|
| 549 | - * @param int $EVT_ID |
|
| 550 | - * @return EE_Datetime |
|
| 551 | - * @throws EE_Error |
|
| 552 | - */ |
|
| 553 | - public function get_most_important_datetime_for_event($EVT_ID) |
|
| 554 | - { |
|
| 555 | - $results = $this->get_datetimes_for_event_ordered_by_importance($EVT_ID, 1); |
|
| 556 | - if ($results) { |
|
| 557 | - return array_shift($results); |
|
| 558 | - } |
|
| 559 | - return null; |
|
| 560 | - } |
|
| 561 | - |
|
| 562 | - |
|
| 563 | - /** |
|
| 564 | - * This returns a wpdb->results Array of all DTT month and years matching the incoming query params and |
|
| 565 | - * grouped by month and year. |
|
| 566 | - * |
|
| 567 | - * @param array $where_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
| 568 | - * @param string $evt_active_status A string representing the evt active status to filter the months by. |
|
| 569 | - * Can be: |
|
| 570 | - * - '' = no filter |
|
| 571 | - * - upcoming = Published events with at least one upcoming datetime. |
|
| 572 | - * - expired = Events with all datetimes expired. |
|
| 573 | - * - active = Events that are published and have at least one datetime that |
|
| 574 | - * starts before now and ends after now. |
|
| 575 | - * - inactive = Events that are either not published. |
|
| 576 | - * @return EE_Base_Class[] |
|
| 577 | - * @throws EE_Error |
|
| 578 | - * @throws InvalidArgumentException |
|
| 579 | - * @throws InvalidArgumentException |
|
| 580 | - */ |
|
| 581 | - public function get_dtt_months_and_years($where_params, $evt_active_status = '') |
|
| 582 | - { |
|
| 583 | - $current_time_for_DTT_EVT_start = $this->current_time_for_query('DTT_EVT_start'); |
|
| 584 | - $current_time_for_DTT_EVT_end = $this->current_time_for_query('DTT_EVT_end'); |
|
| 585 | - switch ($evt_active_status) { |
|
| 586 | - case 'upcoming': |
|
| 587 | - $where_params['Event.status'] = 'publish'; |
|
| 588 | - // if there are already query_params matching DTT_EVT_start then we need to modify that to add them. |
|
| 589 | - if (isset($where_params['DTT_EVT_start'])) { |
|
| 590 | - $where_params['DTT_EVT_start*****'] = $where_params['DTT_EVT_start']; |
|
| 591 | - } |
|
| 592 | - $where_params['DTT_EVT_start'] = array('>', $current_time_for_DTT_EVT_start); |
|
| 593 | - break; |
|
| 594 | - case 'expired': |
|
| 595 | - if (isset($where_params['Event.status'])) { |
|
| 596 | - unset($where_params['Event.status']); |
|
| 597 | - } |
|
| 598 | - // get events to exclude |
|
| 599 | - $exclude_query[0] = array_merge( |
|
| 600 | - $where_params, |
|
| 601 | - array('DTT_EVT_end' => array('>', $current_time_for_DTT_EVT_end)) |
|
| 602 | - ); |
|
| 603 | - // first get all events that have datetimes where its not expired. |
|
| 604 | - $event_ids = $this->_get_all_wpdb_results( |
|
| 605 | - $exclude_query, |
|
| 606 | - OBJECT_K, |
|
| 607 | - 'Datetime.EVT_ID' |
|
| 608 | - ); |
|
| 609 | - $event_ids = array_keys($event_ids); |
|
| 610 | - if (isset($where_params['DTT_EVT_end'])) { |
|
| 611 | - $where_params['DTT_EVT_end****'] = $where_params['DTT_EVT_end']; |
|
| 612 | - } |
|
| 613 | - $where_params['DTT_EVT_end'] = array('<', $current_time_for_DTT_EVT_end); |
|
| 614 | - $where_params['Event.EVT_ID'] = array('NOT IN', $event_ids); |
|
| 615 | - break; |
|
| 616 | - case 'active': |
|
| 617 | - $where_params['Event.status'] = 'publish'; |
|
| 618 | - if (isset($where_params['DTT_EVT_start'])) { |
|
| 619 | - $where_params['Datetime.DTT_EVT_start******'] = $where_params['DTT_EVT_start']; |
|
| 620 | - } |
|
| 621 | - if (isset($where_params['Datetime.DTT_EVT_end'])) { |
|
| 622 | - $where_params['Datetime.DTT_EVT_end*****'] = $where_params['DTT_EVT_end']; |
|
| 623 | - } |
|
| 624 | - $where_params['DTT_EVT_start'] = array('<', $current_time_for_DTT_EVT_start); |
|
| 625 | - $where_params['DTT_EVT_end'] = array('>', $current_time_for_DTT_EVT_end); |
|
| 626 | - break; |
|
| 627 | - case 'inactive': |
|
| 628 | - if (isset($where_params['Event.status'])) { |
|
| 629 | - unset($where_params['Event.status']); |
|
| 630 | - } |
|
| 631 | - if (isset($where_params['OR'])) { |
|
| 632 | - $where_params['AND']['OR'] = $where_params['OR']; |
|
| 633 | - } |
|
| 634 | - if (isset($where_params['DTT_EVT_end'])) { |
|
| 635 | - $where_params['AND']['DTT_EVT_end****'] = $where_params['DTT_EVT_end']; |
|
| 636 | - unset($where_params['DTT_EVT_end']); |
|
| 637 | - } |
|
| 638 | - if (isset($where_params['DTT_EVT_start'])) { |
|
| 639 | - $where_params['AND']['DTT_EVT_start'] = $where_params['DTT_EVT_start']; |
|
| 640 | - unset($where_params['DTT_EVT_start']); |
|
| 641 | - } |
|
| 642 | - $where_params['AND']['Event.status'] = array('!=', 'publish'); |
|
| 643 | - break; |
|
| 644 | - } |
|
| 645 | - $query_params[0] = $where_params; |
|
| 646 | - $query_params['group_by'] = array('dtt_year', 'dtt_month'); |
|
| 647 | - $query_params['order_by'] = array('DTT_EVT_start' => 'DESC'); |
|
| 648 | - $query_interval = EEH_DTT_Helper::get_sql_query_interval_for_offset( |
|
| 649 | - $this->get_timezone(), |
|
| 650 | - 'DTT_EVT_start' |
|
| 651 | - ); |
|
| 652 | - $columns_to_select = array( |
|
| 653 | - 'dtt_year' => array('YEAR(' . $query_interval . ')', '%s'), |
|
| 654 | - 'dtt_month' => array('MONTHNAME(' . $query_interval . ')', '%s'), |
|
| 655 | - 'dtt_month_num' => array('MONTH(' . $query_interval . ')', '%s'), |
|
| 656 | - ); |
|
| 657 | - return $this->_get_all_wpdb_results($query_params, OBJECT, $columns_to_select); |
|
| 658 | - } |
|
| 659 | - |
|
| 660 | - |
|
| 661 | - /** |
|
| 662 | - * Updates the DTT_sold attribute on each datetime (based on the registrations |
|
| 663 | - * for the tickets for each datetime) |
|
| 664 | - * |
|
| 665 | - * @param EE_Base_Class[]|EE_Datetime[] $datetimes |
|
| 666 | - * @throws EE_Error |
|
| 667 | - */ |
|
| 668 | - public function update_sold($datetimes) |
|
| 669 | - { |
|
| 670 | - EE_Error::doing_it_wrong( |
|
| 671 | - __FUNCTION__, |
|
| 672 | - esc_html__( |
|
| 673 | - 'Please use \EEM_Ticket::update_tickets_sold() instead which will in turn correctly update both the Ticket AND Datetime counts.', |
|
| 674 | - 'event_espresso' |
|
| 675 | - ), |
|
| 676 | - '4.9.32.rc.005' |
|
| 677 | - ); |
|
| 678 | - foreach ($datetimes as $datetime) { |
|
| 679 | - $datetime->update_sold(); |
|
| 680 | - } |
|
| 681 | - } |
|
| 682 | - |
|
| 683 | - |
|
| 684 | - /** |
|
| 685 | - * Gets the total number of tickets available at a particular datetime |
|
| 686 | - * (does NOT take into account the datetime's spaces available) |
|
| 687 | - * |
|
| 688 | - * @param int $DTT_ID |
|
| 689 | - * @param array $query_params |
|
| 690 | - * @return int of tickets available. If sold out, return less than 1. If infinite, returns EE_INF, IF there are NO |
|
| 691 | - * tickets attached to datetime then FALSE is returned. |
|
| 692 | - */ |
|
| 693 | - public function sum_tickets_currently_available_at_datetime($DTT_ID, array $query_params = array()) |
|
| 694 | - { |
|
| 695 | - $datetime = $this->get_one_by_ID($DTT_ID); |
|
| 696 | - if ($datetime instanceof EE_Datetime) { |
|
| 697 | - return $datetime->tickets_remaining($query_params); |
|
| 698 | - } |
|
| 699 | - return 0; |
|
| 700 | - } |
|
| 701 | - |
|
| 702 | - |
|
| 703 | - /** |
|
| 704 | - * This returns an array of counts of datetimes in the database for each Datetime status that can be queried. |
|
| 705 | - * |
|
| 706 | - * @param array $stati_to_include If included you can restrict the statuses we return counts for by including the |
|
| 707 | - * stati you want counts for as values in the array. An empty array returns counts |
|
| 708 | - * for all valid stati. |
|
| 709 | - * @param array $query_params If included can be used to refine the conditions for returning the count (i.e. |
|
| 710 | - * only for Datetimes connected to a specific event, or specific ticket. |
|
| 711 | - * @return array The value returned is an array indexed by Datetime Status and the values are the counts. The |
|
| 712 | - * @throws EE_Error |
|
| 713 | - * stati used as index keys are: EE_Datetime::active EE_Datetime::upcoming |
|
| 714 | - * EE_Datetime::expired |
|
| 715 | - */ |
|
| 716 | - public function get_datetime_counts_by_status(array $stati_to_include = array(), array $query_params = array()) |
|
| 717 | - { |
|
| 718 | - // only accept where conditions for this query. |
|
| 719 | - $_where = isset($query_params[0]) ? $query_params[0] : array(); |
|
| 720 | - $status_query_args = array( |
|
| 721 | - EE_Datetime::active => array_merge( |
|
| 722 | - $_where, |
|
| 723 | - array('DTT_EVT_start' => array('<', time()), 'DTT_EVT_end' => array('>', time())) |
|
| 724 | - ), |
|
| 725 | - EE_Datetime::upcoming => array_merge( |
|
| 726 | - $_where, |
|
| 727 | - array('DTT_EVT_start' => array('>', time())) |
|
| 728 | - ), |
|
| 729 | - EE_Datetime::expired => array_merge( |
|
| 730 | - $_where, |
|
| 731 | - array('DTT_EVT_end' => array('<', time())) |
|
| 732 | - ), |
|
| 733 | - ); |
|
| 734 | - if (! empty($stati_to_include)) { |
|
| 735 | - foreach (array_keys($status_query_args) as $status) { |
|
| 736 | - if (! in_array($status, $stati_to_include, true)) { |
|
| 737 | - unset($status_query_args[ $status ]); |
|
| 738 | - } |
|
| 739 | - } |
|
| 740 | - } |
|
| 741 | - // loop through and query counts for each stati. |
|
| 742 | - $status_query_results = array(); |
|
| 743 | - foreach ($status_query_args as $status => $status_where_conditions) { |
|
| 744 | - $status_query_results[ $status ] = EEM_Datetime::count( |
|
| 745 | - array($status_where_conditions), |
|
| 746 | - 'DTT_ID', |
|
| 747 | - true |
|
| 748 | - ); |
|
| 749 | - } |
|
| 750 | - return $status_query_results; |
|
| 751 | - } |
|
| 752 | - |
|
| 753 | - |
|
| 754 | - /** |
|
| 755 | - * Returns the specific count for a given Datetime status matching any given query_params. |
|
| 756 | - * |
|
| 757 | - * @param string $status Valid string representation for Datetime status requested. (Defaults to Active). |
|
| 758 | - * @param array $query_params |
|
| 759 | - * @return int |
|
| 760 | - * @throws EE_Error |
|
| 761 | - */ |
|
| 762 | - public function get_datetime_count_for_status($status = EE_Datetime::active, array $query_params = array()) |
|
| 763 | - { |
|
| 764 | - $count = $this->get_datetime_counts_by_status(array($status), $query_params); |
|
| 765 | - return ! empty($count[ $status ]) ? $count[ $status ] : 0; |
|
| 766 | - } |
|
| 16 | + /** |
|
| 17 | + * @var EEM_Datetime $_instance |
|
| 18 | + */ |
|
| 19 | + protected static $_instance; |
|
| 20 | + |
|
| 21 | + |
|
| 22 | + /** |
|
| 23 | + * private constructor to prevent direct creation |
|
| 24 | + * |
|
| 25 | + * @param string $timezone A string representing the timezone we want to set for returned Date Time Strings |
|
| 26 | + * (and any incoming timezone data that gets saved). |
|
| 27 | + * Note this just sends the timezone info to the date time model field objects. |
|
| 28 | + * Default is NULL |
|
| 29 | + * (and will be assumed using the set timezone in the 'timezone_string' wp option) |
|
| 30 | + * @throws EE_Error |
|
| 31 | + * @throws InvalidArgumentException |
|
| 32 | + * @throws InvalidArgumentException |
|
| 33 | + */ |
|
| 34 | + protected function __construct($timezone) |
|
| 35 | + { |
|
| 36 | + $this->singular_item = esc_html__('Datetime', 'event_espresso'); |
|
| 37 | + $this->plural_item = esc_html__('Datetimes', 'event_espresso'); |
|
| 38 | + $this->_tables = array( |
|
| 39 | + 'Datetime' => new EE_Primary_Table('esp_datetime', 'DTT_ID'), |
|
| 40 | + ); |
|
| 41 | + $this->_fields = array( |
|
| 42 | + 'Datetime' => array( |
|
| 43 | + 'DTT_ID' => new EE_Primary_Key_Int_Field( |
|
| 44 | + 'DTT_ID', |
|
| 45 | + esc_html__('Datetime ID', 'event_espresso') |
|
| 46 | + ), |
|
| 47 | + 'EVT_ID' => new EE_Foreign_Key_Int_Field( |
|
| 48 | + 'EVT_ID', |
|
| 49 | + esc_html__('Event ID', 'event_espresso'), |
|
| 50 | + false, |
|
| 51 | + 0, |
|
| 52 | + 'Event' |
|
| 53 | + ), |
|
| 54 | + 'DTT_name' => new EE_Plain_Text_Field( |
|
| 55 | + 'DTT_name', |
|
| 56 | + esc_html__('Datetime Name', 'event_espresso'), |
|
| 57 | + false, |
|
| 58 | + '' |
|
| 59 | + ), |
|
| 60 | + 'DTT_description' => new EE_Post_Content_Field( |
|
| 61 | + 'DTT_description', |
|
| 62 | + esc_html__('Description for Datetime', 'event_espresso'), |
|
| 63 | + false, |
|
| 64 | + '' |
|
| 65 | + ), |
|
| 66 | + 'DTT_EVT_start' => new EE_Datetime_Field( |
|
| 67 | + 'DTT_EVT_start', |
|
| 68 | + esc_html__('Start time/date of Event', 'event_espresso'), |
|
| 69 | + false, |
|
| 70 | + EE_Datetime_Field::now, |
|
| 71 | + $timezone |
|
| 72 | + ), |
|
| 73 | + 'DTT_EVT_end' => new EE_Datetime_Field( |
|
| 74 | + 'DTT_EVT_end', |
|
| 75 | + esc_html__('End time/date of Event', 'event_espresso'), |
|
| 76 | + false, |
|
| 77 | + EE_Datetime_Field::now, |
|
| 78 | + $timezone |
|
| 79 | + ), |
|
| 80 | + 'DTT_reg_limit' => new EE_Infinite_Integer_Field( |
|
| 81 | + 'DTT_reg_limit', |
|
| 82 | + esc_html__('Registration Limit for this time', 'event_espresso'), |
|
| 83 | + true, |
|
| 84 | + EE_INF |
|
| 85 | + ), |
|
| 86 | + 'DTT_sold' => new EE_Integer_Field( |
|
| 87 | + 'DTT_sold', |
|
| 88 | + esc_html__('How many sales for this Datetime that have occurred', 'event_espresso'), |
|
| 89 | + true, |
|
| 90 | + 0 |
|
| 91 | + ), |
|
| 92 | + 'DTT_reserved' => new EE_Integer_Field( |
|
| 93 | + 'DTT_reserved', |
|
| 94 | + esc_html__('Quantity of tickets reserved, but not yet fully purchased', 'event_espresso'), |
|
| 95 | + false, |
|
| 96 | + 0 |
|
| 97 | + ), |
|
| 98 | + 'DTT_is_primary' => new EE_Boolean_Field( |
|
| 99 | + 'DTT_is_primary', |
|
| 100 | + esc_html__('Flag indicating datetime is primary one for event', 'event_espresso'), |
|
| 101 | + false, |
|
| 102 | + false |
|
| 103 | + ), |
|
| 104 | + 'DTT_order' => new EE_Integer_Field( |
|
| 105 | + 'DTT_order', |
|
| 106 | + esc_html__('The order in which the Datetime is displayed', 'event_espresso'), |
|
| 107 | + false, |
|
| 108 | + 0 |
|
| 109 | + ), |
|
| 110 | + 'DTT_parent' => new EE_Integer_Field( |
|
| 111 | + 'DTT_parent', |
|
| 112 | + esc_html__('Indicates what DTT_ID is the parent of this DTT_ID', 'event_espresso'), |
|
| 113 | + true, |
|
| 114 | + 0 |
|
| 115 | + ), |
|
| 116 | + 'DTT_deleted' => new EE_Trashed_Flag_Field( |
|
| 117 | + 'DTT_deleted', |
|
| 118 | + esc_html__('Flag indicating datetime is archived', 'event_espresso'), |
|
| 119 | + false, |
|
| 120 | + false |
|
| 121 | + ), |
|
| 122 | + ), |
|
| 123 | + ); |
|
| 124 | + $this->_model_relations = array( |
|
| 125 | + 'Ticket' => new EE_HABTM_Relation('Datetime_Ticket'), |
|
| 126 | + 'Event' => new EE_Belongs_To_Relation(), |
|
| 127 | + 'Checkin' => new EE_Has_Many_Relation(), |
|
| 128 | + 'Datetime_Ticket' => new EE_Has_Many_Relation(), |
|
| 129 | + ); |
|
| 130 | + $path_to_event_model = 'Event'; |
|
| 131 | + $this->model_chain_to_password = $path_to_event_model; |
|
| 132 | + $this->_model_chain_to_wp_user = $path_to_event_model; |
|
| 133 | + // this model is generally available for reading |
|
| 134 | + $this->_cap_restriction_generators[ EEM_Base::caps_read ] = new EE_Restriction_Generator_Event_Related_Public( |
|
| 135 | + $path_to_event_model |
|
| 136 | + ); |
|
| 137 | + $this->_cap_restriction_generators[ EEM_Base::caps_read_admin ] = new EE_Restriction_Generator_Event_Related_Protected( |
|
| 138 | + $path_to_event_model |
|
| 139 | + ); |
|
| 140 | + $this->_cap_restriction_generators[ EEM_Base::caps_edit ] = new EE_Restriction_Generator_Event_Related_Protected( |
|
| 141 | + $path_to_event_model |
|
| 142 | + ); |
|
| 143 | + $this->_cap_restriction_generators[ EEM_Base::caps_delete ] = new EE_Restriction_Generator_Event_Related_Protected( |
|
| 144 | + $path_to_event_model, |
|
| 145 | + EEM_Base::caps_edit |
|
| 146 | + ); |
|
| 147 | + parent::__construct($timezone); |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + |
|
| 151 | + /** |
|
| 152 | + * create new blank datetime |
|
| 153 | + * |
|
| 154 | + * @access public |
|
| 155 | + * @return EE_Datetime[] array on success, FALSE on fail |
|
| 156 | + * @throws EE_Error |
|
| 157 | + * @throws InvalidArgumentException |
|
| 158 | + * @throws InvalidDataTypeException |
|
| 159 | + * @throws ReflectionException |
|
| 160 | + * @throws InvalidInterfaceException |
|
| 161 | + */ |
|
| 162 | + public function create_new_blank_datetime() |
|
| 163 | + { |
|
| 164 | + // makes sure timezone is always set. |
|
| 165 | + $timezone_string = $this->get_timezone(); |
|
| 166 | + /** |
|
| 167 | + * Filters the initial start date for the new datetime. |
|
| 168 | + * Any time included in this value will be overridden later so use additional filters to modify the time. |
|
| 169 | + * |
|
| 170 | + * @param int $start_date Unixtimestamp representing now + 30 days in seconds. |
|
| 171 | + * @return int unixtimestamp |
|
| 172 | + */ |
|
| 173 | + $start_date = apply_filters( |
|
| 174 | + 'FHEE__EEM_Datetime__create_new_blank_datetime__start_date', |
|
| 175 | + $this->current_time_for_query('DTT_EVT_start', true) + MONTH_IN_SECONDS |
|
| 176 | + ); |
|
| 177 | + /** |
|
| 178 | + * Filters the initial end date for the new datetime. |
|
| 179 | + * Any time included in this value will be overridden later so use additional filters to modify the time. |
|
| 180 | + * |
|
| 181 | + * @param int $end_data Unixtimestamp representing now + 30 days in seconds. |
|
| 182 | + * @return int unixtimestamp |
|
| 183 | + */ |
|
| 184 | + $end_date = apply_filters( |
|
| 185 | + 'FHEE__EEM_Datetime__create_new_blank_datetime__end_date', |
|
| 186 | + $this->current_time_for_query('DTT_EVT_end', true) + MONTH_IN_SECONDS |
|
| 187 | + ); |
|
| 188 | + $blank_datetime = EE_Datetime::new_instance( |
|
| 189 | + array( |
|
| 190 | + 'DTT_EVT_start' => $start_date, |
|
| 191 | + 'DTT_EVT_end' => $end_date, |
|
| 192 | + 'DTT_order' => 1, |
|
| 193 | + 'DTT_reg_limit' => EE_INF, |
|
| 194 | + ), |
|
| 195 | + $timezone_string |
|
| 196 | + ); |
|
| 197 | + /** |
|
| 198 | + * Filters the initial start time and format for the new EE_Datetime instance. |
|
| 199 | + * |
|
| 200 | + * @param array $start_time An array having size 2. First element is the time, second element is the time |
|
| 201 | + * format. |
|
| 202 | + * @return array |
|
| 203 | + */ |
|
| 204 | + $start_time = apply_filters( |
|
| 205 | + 'FHEE__EEM_Datetime__create_new_blank_datetime__start_time', |
|
| 206 | + ['8am', 'ga'] |
|
| 207 | + ); |
|
| 208 | + /** |
|
| 209 | + * Filters the initial end time and format for the new EE_Datetime instance. |
|
| 210 | + * |
|
| 211 | + * @param array $end_time An array having size 2. First element is the time, second element is the time |
|
| 212 | + * format |
|
| 213 | + * @return array |
|
| 214 | + */ |
|
| 215 | + $end_time = apply_filters( |
|
| 216 | + 'FHEE__EEM_Datetime__create_new_blank_datetime__end_time', |
|
| 217 | + ['5pm', 'ga'] |
|
| 218 | + ); |
|
| 219 | + $this->validateStartAndEndTimeForBlankDate($start_time, $end_time); |
|
| 220 | + $blank_datetime->set_start_time( |
|
| 221 | + $this->convert_datetime_for_query( |
|
| 222 | + 'DTT_EVT_start', |
|
| 223 | + $start_time[0], |
|
| 224 | + $start_time[1], |
|
| 225 | + $timezone_string |
|
| 226 | + ) |
|
| 227 | + ); |
|
| 228 | + $blank_datetime->set_end_time( |
|
| 229 | + $this->convert_datetime_for_query( |
|
| 230 | + 'DTT_EVT_end', |
|
| 231 | + $end_time[0], |
|
| 232 | + $end_time[1], |
|
| 233 | + $timezone_string |
|
| 234 | + ) |
|
| 235 | + ); |
|
| 236 | + return array($blank_datetime); |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + |
|
| 240 | + /** |
|
| 241 | + * Validates whether the start_time and end_time are in the expected format. |
|
| 242 | + * @param array $start_time |
|
| 243 | + * @param array $end_time |
|
| 244 | + * @throws InvalidArgumentException |
|
| 245 | + * @throws InvalidDataTypeException |
|
| 246 | + */ |
|
| 247 | + private function validateStartAndEndTimeForBlankDate($start_time, $end_time) |
|
| 248 | + { |
|
| 249 | + if (! is_array($start_time)) { |
|
| 250 | + throw new InvalidDataTypeException('start_time', $start_time, 'array'); |
|
| 251 | + } |
|
| 252 | + if (! is_array($end_time)) { |
|
| 253 | + throw new InvalidDataTypeException('end_time', $end_time, 'array'); |
|
| 254 | + } |
|
| 255 | + if (count($start_time) !== 2) { |
|
| 256 | + throw new InvalidArgumentException( |
|
| 257 | + sprintf( |
|
| 258 | + 'The variable %1$s is expected to be an array with two elements. The first item in the ' |
|
| 259 | + . 'array should be a valid time string, the second item in the array should be a valid time format', |
|
| 260 | + '$start_time' |
|
| 261 | + ) |
|
| 262 | + ); |
|
| 263 | + } |
|
| 264 | + if (count($end_time) !== 2) { |
|
| 265 | + throw new InvalidArgumentException( |
|
| 266 | + sprintf( |
|
| 267 | + 'The variable %1$s is expected to be an array with two elements. The first item in the ' |
|
| 268 | + . 'array should be a valid time string, the second item in the array should be a valid time format', |
|
| 269 | + '$end_time' |
|
| 270 | + ) |
|
| 271 | + ); |
|
| 272 | + } |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + |
|
| 276 | + /** |
|
| 277 | + * get event start date from db |
|
| 278 | + * |
|
| 279 | + * @access public |
|
| 280 | + * @param int $EVT_ID |
|
| 281 | + * @return EE_Datetime[] array on success, FALSE on fail |
|
| 282 | + * @throws EE_Error |
|
| 283 | + */ |
|
| 284 | + public function get_all_event_dates($EVT_ID = 0) |
|
| 285 | + { |
|
| 286 | + if (! $EVT_ID) { // on add_new_event event_id gets set to 0 |
|
| 287 | + return $this->create_new_blank_datetime(); |
|
| 288 | + } |
|
| 289 | + $results = $this->get_datetimes_for_event_ordered_by_DTT_order($EVT_ID); |
|
| 290 | + if (empty($results)) { |
|
| 291 | + return $this->create_new_blank_datetime(); |
|
| 292 | + } |
|
| 293 | + return $results; |
|
| 294 | + } |
|
| 295 | + |
|
| 296 | + |
|
| 297 | + /** |
|
| 298 | + * get all datetimes attached to an event ordered by the DTT_order field |
|
| 299 | + * |
|
| 300 | + * @public |
|
| 301 | + * @param int $EVT_ID event id |
|
| 302 | + * @param boolean $include_expired |
|
| 303 | + * @param boolean $include_deleted |
|
| 304 | + * @param int $limit If included then limit the count of results by |
|
| 305 | + * the given number |
|
| 306 | + * @return EE_Datetime[] |
|
| 307 | + * @throws EE_Error |
|
| 308 | + */ |
|
| 309 | + public function get_datetimes_for_event_ordered_by_DTT_order( |
|
| 310 | + $EVT_ID, |
|
| 311 | + $include_expired = true, |
|
| 312 | + $include_deleted = true, |
|
| 313 | + $limit = null |
|
| 314 | + ) { |
|
| 315 | + // sanitize EVT_ID |
|
| 316 | + $EVT_ID = absint($EVT_ID); |
|
| 317 | + $old_assumption = $this->get_assumption_concerning_values_already_prepared_by_model_object(); |
|
| 318 | + $this->assume_values_already_prepared_by_model_object(EEM_Base::prepared_for_use_in_db); |
|
| 319 | + $where_params = array('Event.EVT_ID' => $EVT_ID); |
|
| 320 | + $query_params = ! empty($limit) |
|
| 321 | + ? array( |
|
| 322 | + $where_params, |
|
| 323 | + 'limit' => $limit, |
|
| 324 | + 'order_by' => array('DTT_order' => 'ASC'), |
|
| 325 | + 'default_where_conditions' => 'none', |
|
| 326 | + ) |
|
| 327 | + : array( |
|
| 328 | + $where_params, |
|
| 329 | + 'order_by' => array('DTT_order' => 'ASC'), |
|
| 330 | + 'default_where_conditions' => 'none', |
|
| 331 | + ); |
|
| 332 | + if (! $include_expired) { |
|
| 333 | + $query_params[0]['DTT_EVT_end'] = array('>=', current_time('mysql', true)); |
|
| 334 | + } |
|
| 335 | + if ($include_deleted) { |
|
| 336 | + $query_params[0]['DTT_deleted'] = array('IN', array(true, false)); |
|
| 337 | + } |
|
| 338 | + /** @var EE_Datetime[] $result */ |
|
| 339 | + $result = $this->get_all($query_params); |
|
| 340 | + $this->assume_values_already_prepared_by_model_object($old_assumption); |
|
| 341 | + return $result; |
|
| 342 | + } |
|
| 343 | + |
|
| 344 | + |
|
| 345 | + /** |
|
| 346 | + * Gets the datetimes for the event (with the given limit), and orders them by "importance". |
|
| 347 | + * By importance, we mean that the primary datetimes are most important (DEPRECATED FOR NOW), |
|
| 348 | + * and then the earlier datetimes are the most important. |
|
| 349 | + * Maybe we'll want this to take into account datetimes that haven't already passed, but we don't yet. |
|
| 350 | + * |
|
| 351 | + * @param int $EVT_ID |
|
| 352 | + * @param int $limit |
|
| 353 | + * @return EE_Datetime[]|EE_Base_Class[] |
|
| 354 | + * @throws EE_Error |
|
| 355 | + */ |
|
| 356 | + public function get_datetimes_for_event_ordered_by_importance($EVT_ID = 0, $limit = null) |
|
| 357 | + { |
|
| 358 | + return $this->get_all( |
|
| 359 | + array( |
|
| 360 | + array('Event.EVT_ID' => $EVT_ID), |
|
| 361 | + 'limit' => $limit, |
|
| 362 | + 'order_by' => array('DTT_EVT_start' => 'ASC'), |
|
| 363 | + 'default_where_conditions' => 'none', |
|
| 364 | + ) |
|
| 365 | + ); |
|
| 366 | + } |
|
| 367 | + |
|
| 368 | + |
|
| 369 | + /** |
|
| 370 | + * @param int $EVT_ID |
|
| 371 | + * @param boolean $include_expired |
|
| 372 | + * @param boolean $include_deleted |
|
| 373 | + * @return EE_Datetime |
|
| 374 | + * @throws EE_Error |
|
| 375 | + */ |
|
| 376 | + public function get_oldest_datetime_for_event($EVT_ID, $include_expired = false, $include_deleted = false) |
|
| 377 | + { |
|
| 378 | + $results = $this->get_datetimes_for_event_ordered_by_start_time( |
|
| 379 | + $EVT_ID, |
|
| 380 | + $include_expired, |
|
| 381 | + $include_deleted, |
|
| 382 | + 1 |
|
| 383 | + ); |
|
| 384 | + if ($results) { |
|
| 385 | + return array_shift($results); |
|
| 386 | + } |
|
| 387 | + return null; |
|
| 388 | + } |
|
| 389 | + |
|
| 390 | + |
|
| 391 | + /** |
|
| 392 | + * Gets the 'primary' datetime for an event. |
|
| 393 | + * |
|
| 394 | + * @param int $EVT_ID |
|
| 395 | + * @param bool $try_to_exclude_expired |
|
| 396 | + * @param bool $try_to_exclude_deleted |
|
| 397 | + * @return \EE_Datetime |
|
| 398 | + * @throws EE_Error |
|
| 399 | + */ |
|
| 400 | + public function get_primary_datetime_for_event( |
|
| 401 | + $EVT_ID, |
|
| 402 | + $try_to_exclude_expired = true, |
|
| 403 | + $try_to_exclude_deleted = true |
|
| 404 | + ) { |
|
| 405 | + if ($try_to_exclude_expired) { |
|
| 406 | + $non_expired = $this->get_oldest_datetime_for_event($EVT_ID, false, false); |
|
| 407 | + if ($non_expired) { |
|
| 408 | + return $non_expired; |
|
| 409 | + } |
|
| 410 | + } |
|
| 411 | + if ($try_to_exclude_deleted) { |
|
| 412 | + $expired_even = $this->get_oldest_datetime_for_event($EVT_ID, true); |
|
| 413 | + if ($expired_even) { |
|
| 414 | + return $expired_even; |
|
| 415 | + } |
|
| 416 | + } |
|
| 417 | + return $this->get_oldest_datetime_for_event($EVT_ID, true, true); |
|
| 418 | + } |
|
| 419 | + |
|
| 420 | + |
|
| 421 | + /** |
|
| 422 | + * Gets ALL the datetimes for an event (including trashed ones, for now), ordered |
|
| 423 | + * only by start date |
|
| 424 | + * |
|
| 425 | + * @param int $EVT_ID |
|
| 426 | + * @param boolean $include_expired |
|
| 427 | + * @param boolean $include_deleted |
|
| 428 | + * @param int $limit |
|
| 429 | + * @return EE_Datetime[] |
|
| 430 | + * @throws EE_Error |
|
| 431 | + */ |
|
| 432 | + public function get_datetimes_for_event_ordered_by_start_time( |
|
| 433 | + $EVT_ID, |
|
| 434 | + $include_expired = true, |
|
| 435 | + $include_deleted = true, |
|
| 436 | + $limit = null |
|
| 437 | + ) { |
|
| 438 | + // sanitize EVT_ID |
|
| 439 | + $EVT_ID = absint($EVT_ID); |
|
| 440 | + $old_assumption = $this->get_assumption_concerning_values_already_prepared_by_model_object(); |
|
| 441 | + $this->assume_values_already_prepared_by_model_object(EEM_Base::prepared_for_use_in_db); |
|
| 442 | + $query_params = array( |
|
| 443 | + array( |
|
| 444 | + 'Event.EVT_ID' => $EVT_ID |
|
| 445 | + ), |
|
| 446 | + 'order_by' => array( |
|
| 447 | + 'DTT_EVT_start' => 'asc' |
|
| 448 | + ), |
|
| 449 | + 'default_where_conditions' => EEM_Base::default_where_conditions_this_only |
|
| 450 | + ); |
|
| 451 | + if (! $include_expired) { |
|
| 452 | + $query_params[0]['DTT_EVT_end'] = array('>=', current_time('mysql', true)); |
|
| 453 | + } |
|
| 454 | + if ($include_deleted) { |
|
| 455 | + $query_params[0]['DTT_deleted'] = array('IN', array(true, false)); |
|
| 456 | + } |
|
| 457 | + if ($limit) { |
|
| 458 | + $query_params['limit'] = $limit; |
|
| 459 | + } |
|
| 460 | + /** @var EE_Datetime[] $result */ |
|
| 461 | + $result = $this->get_all($query_params); |
|
| 462 | + $this->assume_values_already_prepared_by_model_object($old_assumption); |
|
| 463 | + return $result; |
|
| 464 | + } |
|
| 465 | + |
|
| 466 | + |
|
| 467 | + /** |
|
| 468 | + * Gets ALL the datetimes for an ticket (including trashed ones, for now), ordered |
|
| 469 | + * only by start date |
|
| 470 | + * |
|
| 471 | + * @param int $TKT_ID |
|
| 472 | + * @param boolean $include_expired |
|
| 473 | + * @param boolean $include_deleted |
|
| 474 | + * @param int $limit |
|
| 475 | + * @return EE_Datetime[] |
|
| 476 | + * @throws EE_Error |
|
| 477 | + */ |
|
| 478 | + public function get_datetimes_for_ticket_ordered_by_start_time( |
|
| 479 | + $TKT_ID, |
|
| 480 | + $include_expired = true, |
|
| 481 | + $include_deleted = true, |
|
| 482 | + $limit = null |
|
| 483 | + ) { |
|
| 484 | + // sanitize TKT_ID |
|
| 485 | + $TKT_ID = absint($TKT_ID); |
|
| 486 | + $old_assumption = $this->get_assumption_concerning_values_already_prepared_by_model_object(); |
|
| 487 | + $this->assume_values_already_prepared_by_model_object(EEM_Base::prepared_for_use_in_db); |
|
| 488 | + $query_params = array(array('Ticket.TKT_ID' => $TKT_ID), 'order_by' => array('DTT_EVT_start' => 'asc')); |
|
| 489 | + if (! $include_expired) { |
|
| 490 | + $query_params[0]['DTT_EVT_end'] = array('>=', current_time('mysql', true)); |
|
| 491 | + } |
|
| 492 | + if ($include_deleted) { |
|
| 493 | + $query_params[0]['DTT_deleted'] = array('IN', array(true, false)); |
|
| 494 | + } |
|
| 495 | + if ($limit) { |
|
| 496 | + $query_params['limit'] = $limit; |
|
| 497 | + } |
|
| 498 | + /** @var EE_Datetime[] $result */ |
|
| 499 | + $result = $this->get_all($query_params); |
|
| 500 | + $this->assume_values_already_prepared_by_model_object($old_assumption); |
|
| 501 | + return $result; |
|
| 502 | + } |
|
| 503 | + |
|
| 504 | + |
|
| 505 | + /** |
|
| 506 | + * Gets all the datetimes for a ticket (including trashed ones, for now), ordered by the DTT_order for the |
|
| 507 | + * datetimes. |
|
| 508 | + * |
|
| 509 | + * @param int $TKT_ID ID of ticket to retrieve the datetimes for |
|
| 510 | + * @param boolean $include_expired whether to include expired datetimes or not |
|
| 511 | + * @param boolean $include_deleted whether to include trashed datetimes or not. |
|
| 512 | + * @param int|null $limit if null, no limit, if int then limit results by |
|
| 513 | + * that number |
|
| 514 | + * @return EE_Datetime[] |
|
| 515 | + * @throws EE_Error |
|
| 516 | + */ |
|
| 517 | + public function get_datetimes_for_ticket_ordered_by_DTT_order( |
|
| 518 | + $TKT_ID, |
|
| 519 | + $include_expired = true, |
|
| 520 | + $include_deleted = true, |
|
| 521 | + $limit = null |
|
| 522 | + ) { |
|
| 523 | + // sanitize id. |
|
| 524 | + $TKT_ID = absint($TKT_ID); |
|
| 525 | + $old_assumption = $this->get_assumption_concerning_values_already_prepared_by_model_object(); |
|
| 526 | + $this->assume_values_already_prepared_by_model_object(EEM_Base::prepared_for_use_in_db); |
|
| 527 | + $where_params = array('Ticket.TKT_ID' => $TKT_ID); |
|
| 528 | + $query_params = array($where_params, 'order_by' => array('DTT_order' => 'ASC')); |
|
| 529 | + if (! $include_expired) { |
|
| 530 | + $query_params[0]['DTT_EVT_end'] = array('>=', current_time('mysql', true)); |
|
| 531 | + } |
|
| 532 | + if ($include_deleted) { |
|
| 533 | + $query_params[0]['DTT_deleted'] = array('IN', array(true, false)); |
|
| 534 | + } |
|
| 535 | + if ($limit) { |
|
| 536 | + $query_params['limit'] = $limit; |
|
| 537 | + } |
|
| 538 | + /** @var EE_Datetime[] $result */ |
|
| 539 | + $result = $this->get_all($query_params); |
|
| 540 | + $this->assume_values_already_prepared_by_model_object($old_assumption); |
|
| 541 | + return $result; |
|
| 542 | + } |
|
| 543 | + |
|
| 544 | + |
|
| 545 | + /** |
|
| 546 | + * Gets the most important datetime for a particular event (ie, the primary event usually. But if for some WACK |
|
| 547 | + * reason it doesn't exist, we consider the earliest event the most important) |
|
| 548 | + * |
|
| 549 | + * @param int $EVT_ID |
|
| 550 | + * @return EE_Datetime |
|
| 551 | + * @throws EE_Error |
|
| 552 | + */ |
|
| 553 | + public function get_most_important_datetime_for_event($EVT_ID) |
|
| 554 | + { |
|
| 555 | + $results = $this->get_datetimes_for_event_ordered_by_importance($EVT_ID, 1); |
|
| 556 | + if ($results) { |
|
| 557 | + return array_shift($results); |
|
| 558 | + } |
|
| 559 | + return null; |
|
| 560 | + } |
|
| 561 | + |
|
| 562 | + |
|
| 563 | + /** |
|
| 564 | + * This returns a wpdb->results Array of all DTT month and years matching the incoming query params and |
|
| 565 | + * grouped by month and year. |
|
| 566 | + * |
|
| 567 | + * @param array $where_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
| 568 | + * @param string $evt_active_status A string representing the evt active status to filter the months by. |
|
| 569 | + * Can be: |
|
| 570 | + * - '' = no filter |
|
| 571 | + * - upcoming = Published events with at least one upcoming datetime. |
|
| 572 | + * - expired = Events with all datetimes expired. |
|
| 573 | + * - active = Events that are published and have at least one datetime that |
|
| 574 | + * starts before now and ends after now. |
|
| 575 | + * - inactive = Events that are either not published. |
|
| 576 | + * @return EE_Base_Class[] |
|
| 577 | + * @throws EE_Error |
|
| 578 | + * @throws InvalidArgumentException |
|
| 579 | + * @throws InvalidArgumentException |
|
| 580 | + */ |
|
| 581 | + public function get_dtt_months_and_years($where_params, $evt_active_status = '') |
|
| 582 | + { |
|
| 583 | + $current_time_for_DTT_EVT_start = $this->current_time_for_query('DTT_EVT_start'); |
|
| 584 | + $current_time_for_DTT_EVT_end = $this->current_time_for_query('DTT_EVT_end'); |
|
| 585 | + switch ($evt_active_status) { |
|
| 586 | + case 'upcoming': |
|
| 587 | + $where_params['Event.status'] = 'publish'; |
|
| 588 | + // if there are already query_params matching DTT_EVT_start then we need to modify that to add them. |
|
| 589 | + if (isset($where_params['DTT_EVT_start'])) { |
|
| 590 | + $where_params['DTT_EVT_start*****'] = $where_params['DTT_EVT_start']; |
|
| 591 | + } |
|
| 592 | + $where_params['DTT_EVT_start'] = array('>', $current_time_for_DTT_EVT_start); |
|
| 593 | + break; |
|
| 594 | + case 'expired': |
|
| 595 | + if (isset($where_params['Event.status'])) { |
|
| 596 | + unset($where_params['Event.status']); |
|
| 597 | + } |
|
| 598 | + // get events to exclude |
|
| 599 | + $exclude_query[0] = array_merge( |
|
| 600 | + $where_params, |
|
| 601 | + array('DTT_EVT_end' => array('>', $current_time_for_DTT_EVT_end)) |
|
| 602 | + ); |
|
| 603 | + // first get all events that have datetimes where its not expired. |
|
| 604 | + $event_ids = $this->_get_all_wpdb_results( |
|
| 605 | + $exclude_query, |
|
| 606 | + OBJECT_K, |
|
| 607 | + 'Datetime.EVT_ID' |
|
| 608 | + ); |
|
| 609 | + $event_ids = array_keys($event_ids); |
|
| 610 | + if (isset($where_params['DTT_EVT_end'])) { |
|
| 611 | + $where_params['DTT_EVT_end****'] = $where_params['DTT_EVT_end']; |
|
| 612 | + } |
|
| 613 | + $where_params['DTT_EVT_end'] = array('<', $current_time_for_DTT_EVT_end); |
|
| 614 | + $where_params['Event.EVT_ID'] = array('NOT IN', $event_ids); |
|
| 615 | + break; |
|
| 616 | + case 'active': |
|
| 617 | + $where_params['Event.status'] = 'publish'; |
|
| 618 | + if (isset($where_params['DTT_EVT_start'])) { |
|
| 619 | + $where_params['Datetime.DTT_EVT_start******'] = $where_params['DTT_EVT_start']; |
|
| 620 | + } |
|
| 621 | + if (isset($where_params['Datetime.DTT_EVT_end'])) { |
|
| 622 | + $where_params['Datetime.DTT_EVT_end*****'] = $where_params['DTT_EVT_end']; |
|
| 623 | + } |
|
| 624 | + $where_params['DTT_EVT_start'] = array('<', $current_time_for_DTT_EVT_start); |
|
| 625 | + $where_params['DTT_EVT_end'] = array('>', $current_time_for_DTT_EVT_end); |
|
| 626 | + break; |
|
| 627 | + case 'inactive': |
|
| 628 | + if (isset($where_params['Event.status'])) { |
|
| 629 | + unset($where_params['Event.status']); |
|
| 630 | + } |
|
| 631 | + if (isset($where_params['OR'])) { |
|
| 632 | + $where_params['AND']['OR'] = $where_params['OR']; |
|
| 633 | + } |
|
| 634 | + if (isset($where_params['DTT_EVT_end'])) { |
|
| 635 | + $where_params['AND']['DTT_EVT_end****'] = $where_params['DTT_EVT_end']; |
|
| 636 | + unset($where_params['DTT_EVT_end']); |
|
| 637 | + } |
|
| 638 | + if (isset($where_params['DTT_EVT_start'])) { |
|
| 639 | + $where_params['AND']['DTT_EVT_start'] = $where_params['DTT_EVT_start']; |
|
| 640 | + unset($where_params['DTT_EVT_start']); |
|
| 641 | + } |
|
| 642 | + $where_params['AND']['Event.status'] = array('!=', 'publish'); |
|
| 643 | + break; |
|
| 644 | + } |
|
| 645 | + $query_params[0] = $where_params; |
|
| 646 | + $query_params['group_by'] = array('dtt_year', 'dtt_month'); |
|
| 647 | + $query_params['order_by'] = array('DTT_EVT_start' => 'DESC'); |
|
| 648 | + $query_interval = EEH_DTT_Helper::get_sql_query_interval_for_offset( |
|
| 649 | + $this->get_timezone(), |
|
| 650 | + 'DTT_EVT_start' |
|
| 651 | + ); |
|
| 652 | + $columns_to_select = array( |
|
| 653 | + 'dtt_year' => array('YEAR(' . $query_interval . ')', '%s'), |
|
| 654 | + 'dtt_month' => array('MONTHNAME(' . $query_interval . ')', '%s'), |
|
| 655 | + 'dtt_month_num' => array('MONTH(' . $query_interval . ')', '%s'), |
|
| 656 | + ); |
|
| 657 | + return $this->_get_all_wpdb_results($query_params, OBJECT, $columns_to_select); |
|
| 658 | + } |
|
| 659 | + |
|
| 660 | + |
|
| 661 | + /** |
|
| 662 | + * Updates the DTT_sold attribute on each datetime (based on the registrations |
|
| 663 | + * for the tickets for each datetime) |
|
| 664 | + * |
|
| 665 | + * @param EE_Base_Class[]|EE_Datetime[] $datetimes |
|
| 666 | + * @throws EE_Error |
|
| 667 | + */ |
|
| 668 | + public function update_sold($datetimes) |
|
| 669 | + { |
|
| 670 | + EE_Error::doing_it_wrong( |
|
| 671 | + __FUNCTION__, |
|
| 672 | + esc_html__( |
|
| 673 | + 'Please use \EEM_Ticket::update_tickets_sold() instead which will in turn correctly update both the Ticket AND Datetime counts.', |
|
| 674 | + 'event_espresso' |
|
| 675 | + ), |
|
| 676 | + '4.9.32.rc.005' |
|
| 677 | + ); |
|
| 678 | + foreach ($datetimes as $datetime) { |
|
| 679 | + $datetime->update_sold(); |
|
| 680 | + } |
|
| 681 | + } |
|
| 682 | + |
|
| 683 | + |
|
| 684 | + /** |
|
| 685 | + * Gets the total number of tickets available at a particular datetime |
|
| 686 | + * (does NOT take into account the datetime's spaces available) |
|
| 687 | + * |
|
| 688 | + * @param int $DTT_ID |
|
| 689 | + * @param array $query_params |
|
| 690 | + * @return int of tickets available. If sold out, return less than 1. If infinite, returns EE_INF, IF there are NO |
|
| 691 | + * tickets attached to datetime then FALSE is returned. |
|
| 692 | + */ |
|
| 693 | + public function sum_tickets_currently_available_at_datetime($DTT_ID, array $query_params = array()) |
|
| 694 | + { |
|
| 695 | + $datetime = $this->get_one_by_ID($DTT_ID); |
|
| 696 | + if ($datetime instanceof EE_Datetime) { |
|
| 697 | + return $datetime->tickets_remaining($query_params); |
|
| 698 | + } |
|
| 699 | + return 0; |
|
| 700 | + } |
|
| 701 | + |
|
| 702 | + |
|
| 703 | + /** |
|
| 704 | + * This returns an array of counts of datetimes in the database for each Datetime status that can be queried. |
|
| 705 | + * |
|
| 706 | + * @param array $stati_to_include If included you can restrict the statuses we return counts for by including the |
|
| 707 | + * stati you want counts for as values in the array. An empty array returns counts |
|
| 708 | + * for all valid stati. |
|
| 709 | + * @param array $query_params If included can be used to refine the conditions for returning the count (i.e. |
|
| 710 | + * only for Datetimes connected to a specific event, or specific ticket. |
|
| 711 | + * @return array The value returned is an array indexed by Datetime Status and the values are the counts. The |
|
| 712 | + * @throws EE_Error |
|
| 713 | + * stati used as index keys are: EE_Datetime::active EE_Datetime::upcoming |
|
| 714 | + * EE_Datetime::expired |
|
| 715 | + */ |
|
| 716 | + public function get_datetime_counts_by_status(array $stati_to_include = array(), array $query_params = array()) |
|
| 717 | + { |
|
| 718 | + // only accept where conditions for this query. |
|
| 719 | + $_where = isset($query_params[0]) ? $query_params[0] : array(); |
|
| 720 | + $status_query_args = array( |
|
| 721 | + EE_Datetime::active => array_merge( |
|
| 722 | + $_where, |
|
| 723 | + array('DTT_EVT_start' => array('<', time()), 'DTT_EVT_end' => array('>', time())) |
|
| 724 | + ), |
|
| 725 | + EE_Datetime::upcoming => array_merge( |
|
| 726 | + $_where, |
|
| 727 | + array('DTT_EVT_start' => array('>', time())) |
|
| 728 | + ), |
|
| 729 | + EE_Datetime::expired => array_merge( |
|
| 730 | + $_where, |
|
| 731 | + array('DTT_EVT_end' => array('<', time())) |
|
| 732 | + ), |
|
| 733 | + ); |
|
| 734 | + if (! empty($stati_to_include)) { |
|
| 735 | + foreach (array_keys($status_query_args) as $status) { |
|
| 736 | + if (! in_array($status, $stati_to_include, true)) { |
|
| 737 | + unset($status_query_args[ $status ]); |
|
| 738 | + } |
|
| 739 | + } |
|
| 740 | + } |
|
| 741 | + // loop through and query counts for each stati. |
|
| 742 | + $status_query_results = array(); |
|
| 743 | + foreach ($status_query_args as $status => $status_where_conditions) { |
|
| 744 | + $status_query_results[ $status ] = EEM_Datetime::count( |
|
| 745 | + array($status_where_conditions), |
|
| 746 | + 'DTT_ID', |
|
| 747 | + true |
|
| 748 | + ); |
|
| 749 | + } |
|
| 750 | + return $status_query_results; |
|
| 751 | + } |
|
| 752 | + |
|
| 753 | + |
|
| 754 | + /** |
|
| 755 | + * Returns the specific count for a given Datetime status matching any given query_params. |
|
| 756 | + * |
|
| 757 | + * @param string $status Valid string representation for Datetime status requested. (Defaults to Active). |
|
| 758 | + * @param array $query_params |
|
| 759 | + * @return int |
|
| 760 | + * @throws EE_Error |
|
| 761 | + */ |
|
| 762 | + public function get_datetime_count_for_status($status = EE_Datetime::active, array $query_params = array()) |
|
| 763 | + { |
|
| 764 | + $count = $this->get_datetime_counts_by_status(array($status), $query_params); |
|
| 765 | + return ! empty($count[ $status ]) ? $count[ $status ] : 0; |
|
| 766 | + } |
|
| 767 | 767 | } |
@@ -121,7 +121,7 @@ discard block |
||
| 121 | 121 | ), |
| 122 | 122 | ), |
| 123 | 123 | ); |
| 124 | - $this->_model_relations = array( |
|
| 124 | + $this->_model_relations = array( |
|
| 125 | 125 | 'Ticket' => new EE_HABTM_Relation('Datetime_Ticket'), |
| 126 | 126 | 'Event' => new EE_Belongs_To_Relation(), |
| 127 | 127 | 'Checkin' => new EE_Has_Many_Relation(), |
@@ -131,16 +131,16 @@ discard block |
||
| 131 | 131 | $this->model_chain_to_password = $path_to_event_model; |
| 132 | 132 | $this->_model_chain_to_wp_user = $path_to_event_model; |
| 133 | 133 | // this model is generally available for reading |
| 134 | - $this->_cap_restriction_generators[ EEM_Base::caps_read ] = new EE_Restriction_Generator_Event_Related_Public( |
|
| 134 | + $this->_cap_restriction_generators[EEM_Base::caps_read] = new EE_Restriction_Generator_Event_Related_Public( |
|
| 135 | 135 | $path_to_event_model |
| 136 | 136 | ); |
| 137 | - $this->_cap_restriction_generators[ EEM_Base::caps_read_admin ] = new EE_Restriction_Generator_Event_Related_Protected( |
|
| 137 | + $this->_cap_restriction_generators[EEM_Base::caps_read_admin] = new EE_Restriction_Generator_Event_Related_Protected( |
|
| 138 | 138 | $path_to_event_model |
| 139 | 139 | ); |
| 140 | - $this->_cap_restriction_generators[ EEM_Base::caps_edit ] = new EE_Restriction_Generator_Event_Related_Protected( |
|
| 140 | + $this->_cap_restriction_generators[EEM_Base::caps_edit] = new EE_Restriction_Generator_Event_Related_Protected( |
|
| 141 | 141 | $path_to_event_model |
| 142 | 142 | ); |
| 143 | - $this->_cap_restriction_generators[ EEM_Base::caps_delete ] = new EE_Restriction_Generator_Event_Related_Protected( |
|
| 143 | + $this->_cap_restriction_generators[EEM_Base::caps_delete] = new EE_Restriction_Generator_Event_Related_Protected( |
|
| 144 | 144 | $path_to_event_model, |
| 145 | 145 | EEM_Base::caps_edit |
| 146 | 146 | ); |
@@ -246,10 +246,10 @@ discard block |
||
| 246 | 246 | */ |
| 247 | 247 | private function validateStartAndEndTimeForBlankDate($start_time, $end_time) |
| 248 | 248 | { |
| 249 | - if (! is_array($start_time)) { |
|
| 249 | + if ( ! is_array($start_time)) { |
|
| 250 | 250 | throw new InvalidDataTypeException('start_time', $start_time, 'array'); |
| 251 | 251 | } |
| 252 | - if (! is_array($end_time)) { |
|
| 252 | + if ( ! is_array($end_time)) { |
|
| 253 | 253 | throw new InvalidDataTypeException('end_time', $end_time, 'array'); |
| 254 | 254 | } |
| 255 | 255 | if (count($start_time) !== 2) { |
@@ -283,7 +283,7 @@ discard block |
||
| 283 | 283 | */ |
| 284 | 284 | public function get_all_event_dates($EVT_ID = 0) |
| 285 | 285 | { |
| 286 | - if (! $EVT_ID) { // on add_new_event event_id gets set to 0 |
|
| 286 | + if ( ! $EVT_ID) { // on add_new_event event_id gets set to 0 |
|
| 287 | 287 | return $this->create_new_blank_datetime(); |
| 288 | 288 | } |
| 289 | 289 | $results = $this->get_datetimes_for_event_ordered_by_DTT_order($EVT_ID); |
@@ -329,7 +329,7 @@ discard block |
||
| 329 | 329 | 'order_by' => array('DTT_order' => 'ASC'), |
| 330 | 330 | 'default_where_conditions' => 'none', |
| 331 | 331 | ); |
| 332 | - if (! $include_expired) { |
|
| 332 | + if ( ! $include_expired) { |
|
| 333 | 333 | $query_params[0]['DTT_EVT_end'] = array('>=', current_time('mysql', true)); |
| 334 | 334 | } |
| 335 | 335 | if ($include_deleted) { |
@@ -448,7 +448,7 @@ discard block |
||
| 448 | 448 | ), |
| 449 | 449 | 'default_where_conditions' => EEM_Base::default_where_conditions_this_only |
| 450 | 450 | ); |
| 451 | - if (! $include_expired) { |
|
| 451 | + if ( ! $include_expired) { |
|
| 452 | 452 | $query_params[0]['DTT_EVT_end'] = array('>=', current_time('mysql', true)); |
| 453 | 453 | } |
| 454 | 454 | if ($include_deleted) { |
@@ -486,7 +486,7 @@ discard block |
||
| 486 | 486 | $old_assumption = $this->get_assumption_concerning_values_already_prepared_by_model_object(); |
| 487 | 487 | $this->assume_values_already_prepared_by_model_object(EEM_Base::prepared_for_use_in_db); |
| 488 | 488 | $query_params = array(array('Ticket.TKT_ID' => $TKT_ID), 'order_by' => array('DTT_EVT_start' => 'asc')); |
| 489 | - if (! $include_expired) { |
|
| 489 | + if ( ! $include_expired) { |
|
| 490 | 490 | $query_params[0]['DTT_EVT_end'] = array('>=', current_time('mysql', true)); |
| 491 | 491 | } |
| 492 | 492 | if ($include_deleted) { |
@@ -526,7 +526,7 @@ discard block |
||
| 526 | 526 | $this->assume_values_already_prepared_by_model_object(EEM_Base::prepared_for_use_in_db); |
| 527 | 527 | $where_params = array('Ticket.TKT_ID' => $TKT_ID); |
| 528 | 528 | $query_params = array($where_params, 'order_by' => array('DTT_order' => 'ASC')); |
| 529 | - if (! $include_expired) { |
|
| 529 | + if ( ! $include_expired) { |
|
| 530 | 530 | $query_params[0]['DTT_EVT_end'] = array('>=', current_time('mysql', true)); |
| 531 | 531 | } |
| 532 | 532 | if ($include_deleted) { |
@@ -649,10 +649,10 @@ discard block |
||
| 649 | 649 | $this->get_timezone(), |
| 650 | 650 | 'DTT_EVT_start' |
| 651 | 651 | ); |
| 652 | - $columns_to_select = array( |
|
| 653 | - 'dtt_year' => array('YEAR(' . $query_interval . ')', '%s'), |
|
| 654 | - 'dtt_month' => array('MONTHNAME(' . $query_interval . ')', '%s'), |
|
| 655 | - 'dtt_month_num' => array('MONTH(' . $query_interval . ')', '%s'), |
|
| 652 | + $columns_to_select = array( |
|
| 653 | + 'dtt_year' => array('YEAR('.$query_interval.')', '%s'), |
|
| 654 | + 'dtt_month' => array('MONTHNAME('.$query_interval.')', '%s'), |
|
| 655 | + 'dtt_month_num' => array('MONTH('.$query_interval.')', '%s'), |
|
| 656 | 656 | ); |
| 657 | 657 | return $this->_get_all_wpdb_results($query_params, OBJECT, $columns_to_select); |
| 658 | 658 | } |
@@ -731,17 +731,17 @@ discard block |
||
| 731 | 731 | array('DTT_EVT_end' => array('<', time())) |
| 732 | 732 | ), |
| 733 | 733 | ); |
| 734 | - if (! empty($stati_to_include)) { |
|
| 734 | + if ( ! empty($stati_to_include)) { |
|
| 735 | 735 | foreach (array_keys($status_query_args) as $status) { |
| 736 | - if (! in_array($status, $stati_to_include, true)) { |
|
| 737 | - unset($status_query_args[ $status ]); |
|
| 736 | + if ( ! in_array($status, $stati_to_include, true)) { |
|
| 737 | + unset($status_query_args[$status]); |
|
| 738 | 738 | } |
| 739 | 739 | } |
| 740 | 740 | } |
| 741 | 741 | // loop through and query counts for each stati. |
| 742 | 742 | $status_query_results = array(); |
| 743 | 743 | foreach ($status_query_args as $status => $status_where_conditions) { |
| 744 | - $status_query_results[ $status ] = EEM_Datetime::count( |
|
| 744 | + $status_query_results[$status] = EEM_Datetime::count( |
|
| 745 | 745 | array($status_where_conditions), |
| 746 | 746 | 'DTT_ID', |
| 747 | 747 | true |
@@ -762,6 +762,6 @@ discard block |
||
| 762 | 762 | public function get_datetime_count_for_status($status = EE_Datetime::active, array $query_params = array()) |
| 763 | 763 | { |
| 764 | 764 | $count = $this->get_datetime_counts_by_status(array($status), $query_params); |
| 765 | - return ! empty($count[ $status ]) ? $count[ $status ] : 0; |
|
| 765 | + return ! empty($count[$status]) ? $count[$status] : 0; |
|
| 766 | 766 | } |
| 767 | 767 | } |