@@ -645,7 +645,7 @@ |
||
| 645 | 645 | /** |
| 646 | 646 | * This returns an array of counts of datetimes in the database for each Datetime status that can be queried. |
| 647 | 647 | * |
| 648 | - * @param array $stati_to_include If included you can restrict the statuses we return counts for by including the |
|
| 648 | + * @param string[] $stati_to_include If included you can restrict the statuses we return counts for by including the |
|
| 649 | 649 | * stati you want counts for as values in the array. An empty array returns counts |
| 650 | 650 | * for all valid stati. |
| 651 | 651 | * @param array $query_params If included can be used to refine the conditions for returning the count (i.e. |
@@ -13,831 +13,831 @@ |
||
| 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 = [ |
|
| 39 | - 'Datetime' => new EE_Primary_Table('esp_datetime', 'DTT_ID'), |
|
| 40 | - ]; |
|
| 41 | - $this->_fields = [ |
|
| 42 | - 'Datetime' => [ |
|
| 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 = [ |
|
| 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 ] = |
|
| 135 | - new EE_Restriction_Generator_Event_Related_Public( |
|
| 136 | - $path_to_event_model |
|
| 137 | - ); |
|
| 138 | - $this->_cap_restriction_generators[ EEM_Base::caps_read_admin ] = |
|
| 139 | - new EE_Restriction_Generator_Event_Related_Protected( |
|
| 140 | - $path_to_event_model |
|
| 141 | - ); |
|
| 142 | - $this->_cap_restriction_generators[ EEM_Base::caps_edit ] = |
|
| 143 | - new EE_Restriction_Generator_Event_Related_Protected( |
|
| 144 | - $path_to_event_model |
|
| 145 | - ); |
|
| 146 | - $this->_cap_restriction_generators[ EEM_Base::caps_delete ] = |
|
| 147 | - new EE_Restriction_Generator_Event_Related_Protected( |
|
| 148 | - $path_to_event_model, |
|
| 149 | - EEM_Base::caps_edit |
|
| 150 | - ); |
|
| 151 | - parent::__construct($timezone); |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - |
|
| 155 | - /** |
|
| 156 | - * create new blank datetime |
|
| 157 | - * |
|
| 158 | - * @access public |
|
| 159 | - * @return EE_Datetime[] array on success, FALSE on fail |
|
| 160 | - * @throws EE_Error |
|
| 161 | - * @throws InvalidArgumentException |
|
| 162 | - * @throws InvalidDataTypeException |
|
| 163 | - * @throws ReflectionException |
|
| 164 | - * @throws InvalidInterfaceException |
|
| 165 | - */ |
|
| 166 | - public function create_new_blank_datetime() |
|
| 167 | - { |
|
| 168 | - // makes sure timezone is always set. |
|
| 169 | - $timezone_string = $this->get_timezone(); |
|
| 170 | - /** |
|
| 171 | - * Filters the initial start date for the new datetime. |
|
| 172 | - * Any time included in this value will be overridden later so use additional filters to modify the time. |
|
| 173 | - * |
|
| 174 | - * @param int $start_date Unixtimestamp representing now + 30 days in seconds. |
|
| 175 | - * @return int unixtimestamp |
|
| 176 | - */ |
|
| 177 | - $start_date = apply_filters( |
|
| 178 | - 'FHEE__EEM_Datetime__create_new_blank_datetime__start_date', |
|
| 179 | - $this->current_time_for_query('DTT_EVT_start', true) + MONTH_IN_SECONDS |
|
| 180 | - ); |
|
| 181 | - /** |
|
| 182 | - * Filters the initial end date for the new datetime. |
|
| 183 | - * Any time included in this value will be overridden later so use additional filters to modify the time. |
|
| 184 | - * |
|
| 185 | - * @param int $end_data Unixtimestamp representing now + 30 days in seconds. |
|
| 186 | - * @return int unixtimestamp |
|
| 187 | - */ |
|
| 188 | - $end_date = apply_filters( |
|
| 189 | - 'FHEE__EEM_Datetime__create_new_blank_datetime__end_date', |
|
| 190 | - $this->current_time_for_query('DTT_EVT_end', true) + MONTH_IN_SECONDS |
|
| 191 | - ); |
|
| 192 | - $blank_datetime = EE_Datetime::new_instance( |
|
| 193 | - [ |
|
| 194 | - 'DTT_EVT_start' => $start_date, |
|
| 195 | - 'DTT_EVT_end' => $end_date, |
|
| 196 | - 'DTT_order' => 1, |
|
| 197 | - 'DTT_reg_limit' => EE_INF, |
|
| 198 | - ], |
|
| 199 | - $timezone_string |
|
| 200 | - ); |
|
| 201 | - /** |
|
| 202 | - * Filters the initial start time and format for the new EE_Datetime instance. |
|
| 203 | - * |
|
| 204 | - * @param array $start_time An array having size 2. First element is the time, second element is the time |
|
| 205 | - * format. |
|
| 206 | - * @return array |
|
| 207 | - */ |
|
| 208 | - $start_time = apply_filters( |
|
| 209 | - 'FHEE__EEM_Datetime__create_new_blank_datetime__start_time', |
|
| 210 | - ['8am', 'ga'] |
|
| 211 | - ); |
|
| 212 | - /** |
|
| 213 | - * Filters the initial end time and format for the new EE_Datetime instance. |
|
| 214 | - * |
|
| 215 | - * @param array $end_time An array having size 2. First element is the time, second element is the time |
|
| 216 | - * format |
|
| 217 | - * @return array |
|
| 218 | - */ |
|
| 219 | - $end_time = apply_filters( |
|
| 220 | - 'FHEE__EEM_Datetime__create_new_blank_datetime__end_time', |
|
| 221 | - ['5pm', 'ga'] |
|
| 222 | - ); |
|
| 223 | - $this->validateStartAndEndTimeForBlankDate($start_time, $end_time); |
|
| 224 | - $blank_datetime->set_start_time( |
|
| 225 | - $this->convert_datetime_for_query( |
|
| 226 | - 'DTT_EVT_start', |
|
| 227 | - $start_time[0], |
|
| 228 | - $start_time[1], |
|
| 229 | - $timezone_string |
|
| 230 | - ) |
|
| 231 | - ); |
|
| 232 | - $blank_datetime->set_end_time( |
|
| 233 | - $this->convert_datetime_for_query( |
|
| 234 | - 'DTT_EVT_end', |
|
| 235 | - $end_time[0], |
|
| 236 | - $end_time[1], |
|
| 237 | - $timezone_string |
|
| 238 | - ) |
|
| 239 | - ); |
|
| 240 | - return [$blank_datetime]; |
|
| 241 | - } |
|
| 242 | - |
|
| 243 | - |
|
| 244 | - /** |
|
| 245 | - * Validates whether the start_time and end_time are in the expected format. |
|
| 246 | - * |
|
| 247 | - * @param array $start_time |
|
| 248 | - * @param array $end_time |
|
| 249 | - * @throws InvalidArgumentException |
|
| 250 | - * @throws InvalidDataTypeException |
|
| 251 | - */ |
|
| 252 | - private function validateStartAndEndTimeForBlankDate(array $start_time, array $end_time) |
|
| 253 | - { |
|
| 254 | - if (! is_array($start_time)) { |
|
| 255 | - throw new InvalidDataTypeException('start_time', $start_time, 'array'); |
|
| 256 | - } |
|
| 257 | - if (! is_array($end_time)) { |
|
| 258 | - throw new InvalidDataTypeException('end_time', $end_time, 'array'); |
|
| 259 | - } |
|
| 260 | - if (count($start_time) !== 2) { |
|
| 261 | - throw new InvalidArgumentException( |
|
| 262 | - sprintf( |
|
| 263 | - 'The variable %1$s is expected to be an array with two elements. The first item in the ' |
|
| 264 | - . 'array should be a valid time string, the second item in the array should be a valid time format', |
|
| 265 | - '$start_time' |
|
| 266 | - ) |
|
| 267 | - ); |
|
| 268 | - } |
|
| 269 | - if (count($end_time) !== 2) { |
|
| 270 | - throw new InvalidArgumentException( |
|
| 271 | - sprintf( |
|
| 272 | - 'The variable %1$s is expected to be an array with two elements. The first item in the ' |
|
| 273 | - . 'array should be a valid time string, the second item in the array should be a valid time format', |
|
| 274 | - '$end_time' |
|
| 275 | - ) |
|
| 276 | - ); |
|
| 277 | - } |
|
| 278 | - } |
|
| 279 | - |
|
| 280 | - |
|
| 281 | - /** |
|
| 282 | - * get event start date from db |
|
| 283 | - * |
|
| 284 | - * @access public |
|
| 285 | - * @param int $EVT_ID |
|
| 286 | - * @return EE_Datetime[] array on success, FALSE on fail |
|
| 287 | - * @throws EE_Error |
|
| 288 | - * @throws ReflectionException |
|
| 289 | - */ |
|
| 290 | - public function get_all_event_dates($EVT_ID = 0) |
|
| 291 | - { |
|
| 292 | - if (! $EVT_ID) { // on add_new_event event_id gets set to 0 |
|
| 293 | - return $this->create_new_blank_datetime(); |
|
| 294 | - } |
|
| 295 | - $results = $this->get_datetimes_for_event_ordered_by_DTT_order($EVT_ID); |
|
| 296 | - if (empty($results)) { |
|
| 297 | - return $this->create_new_blank_datetime(); |
|
| 298 | - } |
|
| 299 | - return $results; |
|
| 300 | - } |
|
| 301 | - |
|
| 302 | - |
|
| 303 | - /** |
|
| 304 | - * get all datetimes attached to an event ordered by the DTT_order field |
|
| 305 | - * |
|
| 306 | - * @public |
|
| 307 | - * @param int $EVT_ID event id |
|
| 308 | - * @param boolean $include_expired |
|
| 309 | - * @param boolean $include_deleted |
|
| 310 | - * @param int $limit If included then limit the count of results by |
|
| 311 | - * the given number |
|
| 312 | - * @return EE_Datetime[] |
|
| 313 | - * @throws EE_Error |
|
| 314 | - */ |
|
| 315 | - public function get_datetimes_for_event_ordered_by_DTT_order( |
|
| 316 | - $EVT_ID, |
|
| 317 | - $include_expired = true, |
|
| 318 | - $include_deleted = true, |
|
| 319 | - $limit = null |
|
| 320 | - ) { |
|
| 321 | - $prev_data_prep_value = $this->prepModelForQuery(); |
|
| 322 | - $where_params = ['Event.EVT_ID' => absint($EVT_ID)]; |
|
| 323 | - $query_params[0] = $this->addDefaultWhereParams($where_params, $include_deleted, $include_expired); |
|
| 324 | - $query_params = $this->addDefaultWhereConditions($query_params); |
|
| 325 | - $query_params = $this->addDefaultQueryParams($query_params, $limit, 'DTT_order'); |
|
| 326 | - return $this->getDatetimesAndRestoreModel($query_params, $prev_data_prep_value); |
|
| 327 | - } |
|
| 328 | - |
|
| 329 | - |
|
| 330 | - /** |
|
| 331 | - * Gets the datetimes for the event (with the given limit), and orders them by "importance". |
|
| 332 | - * By importance, we mean that the primary datetimes are most important (DEPRECATED FOR NOW), |
|
| 333 | - * and then the earlier datetimes are the most important. |
|
| 334 | - * Maybe we'll want this to take into account datetimes that haven't already passed, but we don't yet. |
|
| 335 | - * |
|
| 336 | - * @param int $EVT_ID |
|
| 337 | - * @param int $limit |
|
| 338 | - * @return EE_Datetime[]|EE_Base_Class[] |
|
| 339 | - * @throws EE_Error |
|
| 340 | - */ |
|
| 341 | - public function get_datetimes_for_event_ordered_by_importance($EVT_ID = 0, $limit = 0) |
|
| 342 | - { |
|
| 343 | - $query_params[0] = ['Event.EVT_ID' => absint($EVT_ID)]; |
|
| 344 | - $query_params = $this->addDefaultWhereConditions($query_params); |
|
| 345 | - $query_params = $this->addDefaultQueryParams($query_params, $limit); |
|
| 346 | - return $this->get_all($query_params); |
|
| 347 | - } |
|
| 348 | - |
|
| 349 | - |
|
| 350 | - /** |
|
| 351 | - * @param int $EVT_ID |
|
| 352 | - * @param boolean $include_expired |
|
| 353 | - * @param boolean $include_deleted |
|
| 354 | - * @return EE_Datetime |
|
| 355 | - * @throws EE_Error |
|
| 356 | - */ |
|
| 357 | - public function get_oldest_datetime_for_event($EVT_ID, $include_expired = false, $include_deleted = false) |
|
| 358 | - { |
|
| 359 | - $results = $this->get_datetimes_for_event_ordered_by_start_time( |
|
| 360 | - $EVT_ID, |
|
| 361 | - $include_expired, |
|
| 362 | - $include_deleted, |
|
| 363 | - 1 |
|
| 364 | - ); |
|
| 365 | - if ($results) { |
|
| 366 | - return array_shift($results); |
|
| 367 | - } |
|
| 368 | - return null; |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - |
|
| 372 | - /** |
|
| 373 | - * Gets the 'primary' datetime for an event. |
|
| 374 | - * |
|
| 375 | - * @param int $EVT_ID |
|
| 376 | - * @param bool $try_to_exclude_expired |
|
| 377 | - * @param bool $try_to_exclude_deleted |
|
| 378 | - * @return \EE_Datetime |
|
| 379 | - * @throws EE_Error |
|
| 380 | - */ |
|
| 381 | - public function get_primary_datetime_for_event( |
|
| 382 | - $EVT_ID, |
|
| 383 | - $try_to_exclude_expired = true, |
|
| 384 | - $try_to_exclude_deleted = true |
|
| 385 | - ) { |
|
| 386 | - if ($try_to_exclude_expired) { |
|
| 387 | - $non_expired = $this->get_oldest_datetime_for_event($EVT_ID, false, false); |
|
| 388 | - if ($non_expired) { |
|
| 389 | - return $non_expired; |
|
| 390 | - } |
|
| 391 | - } |
|
| 392 | - if ($try_to_exclude_deleted) { |
|
| 393 | - $expired_even = $this->get_oldest_datetime_for_event($EVT_ID, true); |
|
| 394 | - if ($expired_even) { |
|
| 395 | - return $expired_even; |
|
| 396 | - } |
|
| 397 | - } |
|
| 398 | - return $this->get_oldest_datetime_for_event($EVT_ID, true, true); |
|
| 399 | - } |
|
| 400 | - |
|
| 401 | - |
|
| 402 | - /** |
|
| 403 | - * Gets ALL the datetimes for an event (including trashed ones, for now), ordered |
|
| 404 | - * only by start date |
|
| 405 | - * |
|
| 406 | - * @param int $EVT_ID |
|
| 407 | - * @param boolean $include_expired |
|
| 408 | - * @param boolean $include_deleted |
|
| 409 | - * @param int $limit |
|
| 410 | - * @return EE_Datetime[] |
|
| 411 | - * @throws EE_Error |
|
| 412 | - */ |
|
| 413 | - public function get_datetimes_for_event_ordered_by_start_time( |
|
| 414 | - $EVT_ID, |
|
| 415 | - $include_expired = true, |
|
| 416 | - $include_deleted = true, |
|
| 417 | - $limit = null |
|
| 418 | - ) { |
|
| 419 | - $prev_data_prep_value = $this->prepModelForQuery(); |
|
| 420 | - $where_params = ['Event.EVT_ID' => absint($EVT_ID)]; |
|
| 421 | - $query_params[0] = $this->addDefaultWhereParams($where_params, $include_deleted, $include_expired); |
|
| 422 | - $query_params = $this->addDefaultWhereConditions( |
|
| 423 | - $query_params, |
|
| 424 | - EEM_Base::default_where_conditions_this_only |
|
| 425 | - ); |
|
| 426 | - $query_params = $this->addDefaultQueryParams($query_params, $limit, 'DTT_order'); |
|
| 427 | - return $this->getDatetimesAndRestoreModel($query_params, $prev_data_prep_value); |
|
| 428 | - } |
|
| 429 | - |
|
| 430 | - |
|
| 431 | - /** |
|
| 432 | - * Gets ALL the datetimes for an ticket (including trashed ones, for now), ordered |
|
| 433 | - * only by start date |
|
| 434 | - * |
|
| 435 | - * @param int $TKT_ID |
|
| 436 | - * @param boolean $include_expired |
|
| 437 | - * @param boolean $include_deleted |
|
| 438 | - * @param int $limit |
|
| 439 | - * @return EE_Datetime[] |
|
| 440 | - * @throws EE_Error |
|
| 441 | - */ |
|
| 442 | - public function get_datetimes_for_ticket_ordered_by_start_time( |
|
| 443 | - $TKT_ID, |
|
| 444 | - $include_expired = true, |
|
| 445 | - $include_deleted = true, |
|
| 446 | - $limit = null |
|
| 447 | - ) { |
|
| 448 | - $prev_data_prep_value = $this->prepModelForQuery(); |
|
| 449 | - $where_params = ['Ticket.TKT_ID' => absint($TKT_ID)]; |
|
| 450 | - $query_params[0] = $this->addDefaultWhereParams($where_params, $include_deleted, $include_expired); |
|
| 451 | - $query_params = $this->addDefaultQueryParams($query_params, $limit); |
|
| 452 | - return $this->getDatetimesAndRestoreModel($query_params, $prev_data_prep_value); |
|
| 453 | - } |
|
| 454 | - |
|
| 455 | - |
|
| 456 | - /** |
|
| 457 | - * Gets all the datetimes for a ticket (including trashed ones, for now), ordered by the DTT_order for the |
|
| 458 | - * datetimes. |
|
| 459 | - * |
|
| 460 | - * @param int $TKT_ID ID of ticket to retrieve the datetimes for |
|
| 461 | - * @param boolean $include_expired whether to include expired datetimes or not |
|
| 462 | - * @param boolean $include_deleted whether to include trashed datetimes or not. |
|
| 463 | - * @param int|null $limit if null, no limit, if int then limit results by |
|
| 464 | - * that number |
|
| 465 | - * @return EE_Datetime[] |
|
| 466 | - * @throws EE_Error |
|
| 467 | - */ |
|
| 468 | - public function get_datetimes_for_ticket_ordered_by_DTT_order( |
|
| 469 | - $TKT_ID, |
|
| 470 | - $include_expired = true, |
|
| 471 | - $include_deleted = true, |
|
| 472 | - $limit = null |
|
| 473 | - ) { |
|
| 474 | - $prev_data_prep_value = $this->prepModelForQuery(); |
|
| 475 | - $where_params = ['Ticket.TKT_ID' => absint($TKT_ID)]; |
|
| 476 | - $query_params[0] = $this->addDefaultWhereParams($where_params, $include_deleted, $include_expired); |
|
| 477 | - $query_params = $this->addDefaultQueryParams($query_params, $limit, 'DTT_order'); |
|
| 478 | - return $this->getDatetimesAndRestoreModel($query_params, $prev_data_prep_value); |
|
| 479 | - } |
|
| 480 | - |
|
| 481 | - |
|
| 482 | - /** |
|
| 483 | - * Gets the most important datetime for a particular event (ie, the primary event usually. But if for some WACK |
|
| 484 | - * reason it doesn't exist, we consider the earliest event the most important) |
|
| 485 | - * |
|
| 486 | - * @param int $EVT_ID |
|
| 487 | - * @return EE_Datetime |
|
| 488 | - * @throws EE_Error |
|
| 489 | - */ |
|
| 490 | - public function get_most_important_datetime_for_event($EVT_ID) |
|
| 491 | - { |
|
| 492 | - $results = $this->get_datetimes_for_event_ordered_by_importance($EVT_ID, 1); |
|
| 493 | - if ($results) { |
|
| 494 | - return array_shift($results); |
|
| 495 | - } |
|
| 496 | - return null; |
|
| 497 | - } |
|
| 498 | - |
|
| 499 | - |
|
| 500 | - /** |
|
| 501 | - * This returns a wpdb->results Array of all DTT month and years matching the incoming query params and |
|
| 502 | - * grouped by month and year. |
|
| 503 | - * |
|
| 504 | - * @param array $where_params @see |
|
| 505 | - * https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
| 506 | - * @param string $evt_active_status A string representing the evt active status to filter the months by. |
|
| 507 | - * Can be: |
|
| 508 | - * - '' = no filter |
|
| 509 | - * - upcoming = Published events with at least one upcoming datetime. |
|
| 510 | - * - expired = Events with all datetimes expired. |
|
| 511 | - * - active = Events that are published and have at least one datetime that |
|
| 512 | - * starts before now and ends after now. |
|
| 513 | - * - inactive = Events that are either not published. |
|
| 514 | - * @return EE_Base_Class[] |
|
| 515 | - * @throws EE_Error |
|
| 516 | - * @throws InvalidArgumentException |
|
| 517 | - * @throws InvalidArgumentException |
|
| 518 | - */ |
|
| 519 | - public function get_dtt_months_and_years(array $where_params, $evt_active_status = '') |
|
| 520 | - { |
|
| 521 | - $current_time_for_DTT_EVT_start = $this->current_time_for_query('DTT_EVT_start'); |
|
| 522 | - $current_time_for_DTT_EVT_end = $this->current_time_for_query('DTT_EVT_end'); |
|
| 523 | - switch ($evt_active_status) { |
|
| 524 | - case 'upcoming': |
|
| 525 | - $where_params['Event.status'] = 'publish'; |
|
| 526 | - // if there are already query_params matching DTT_EVT_start then we need to modify that to add them. |
|
| 527 | - if (isset($where_params['DTT_EVT_start'])) { |
|
| 528 | - $where_params['DTT_EVT_start*****'] = $where_params['DTT_EVT_start']; |
|
| 529 | - } |
|
| 530 | - $where_params['DTT_EVT_start'] = ['>', $current_time_for_DTT_EVT_start]; |
|
| 531 | - break; |
|
| 532 | - case 'expired': |
|
| 533 | - if (isset($where_params['Event.status'])) { |
|
| 534 | - unset($where_params['Event.status']); |
|
| 535 | - } |
|
| 536 | - // get events to exclude |
|
| 537 | - $exclude_query[0] = array_merge( |
|
| 538 | - $where_params, |
|
| 539 | - ['DTT_EVT_end' => ['>', $current_time_for_DTT_EVT_end]] |
|
| 540 | - ); |
|
| 541 | - // first get all events that have datetimes where its not expired. |
|
| 542 | - $event_ids = $this->_get_all_wpdb_results( |
|
| 543 | - $exclude_query, |
|
| 544 | - OBJECT_K, |
|
| 545 | - 'Datetime.EVT_ID' |
|
| 546 | - ); |
|
| 547 | - $event_ids = array_keys($event_ids); |
|
| 548 | - if (isset($where_params['DTT_EVT_end'])) { |
|
| 549 | - $where_params['DTT_EVT_end****'] = $where_params['DTT_EVT_end']; |
|
| 550 | - } |
|
| 551 | - $where_params['DTT_EVT_end'] = ['<', $current_time_for_DTT_EVT_end]; |
|
| 552 | - $where_params['Event.EVT_ID'] = ['NOT IN', $event_ids]; |
|
| 553 | - break; |
|
| 554 | - case 'active': |
|
| 555 | - $where_params['Event.status'] = 'publish'; |
|
| 556 | - if (isset($where_params['DTT_EVT_start'])) { |
|
| 557 | - $where_params['Datetime.DTT_EVT_start******'] = $where_params['DTT_EVT_start']; |
|
| 558 | - } |
|
| 559 | - if (isset($where_params['Datetime.DTT_EVT_end'])) { |
|
| 560 | - $where_params['Datetime.DTT_EVT_end*****'] = $where_params['DTT_EVT_end']; |
|
| 561 | - } |
|
| 562 | - $where_params['DTT_EVT_start'] = ['<', $current_time_for_DTT_EVT_start]; |
|
| 563 | - $where_params['DTT_EVT_end'] = ['>', $current_time_for_DTT_EVT_end]; |
|
| 564 | - break; |
|
| 565 | - case 'inactive': |
|
| 566 | - if (isset($where_params['Event.status'])) { |
|
| 567 | - unset($where_params['Event.status']); |
|
| 568 | - } |
|
| 569 | - if (isset($where_params['OR'])) { |
|
| 570 | - $where_params['AND']['OR'] = $where_params['OR']; |
|
| 571 | - } |
|
| 572 | - if (isset($where_params['DTT_EVT_end'])) { |
|
| 573 | - $where_params['AND']['DTT_EVT_end****'] = $where_params['DTT_EVT_end']; |
|
| 574 | - unset($where_params['DTT_EVT_end']); |
|
| 575 | - } |
|
| 576 | - if (isset($where_params['DTT_EVT_start'])) { |
|
| 577 | - $where_params['AND']['DTT_EVT_start'] = $where_params['DTT_EVT_start']; |
|
| 578 | - unset($where_params['DTT_EVT_start']); |
|
| 579 | - } |
|
| 580 | - $where_params['AND']['Event.status'] = ['!=', 'publish']; |
|
| 581 | - break; |
|
| 582 | - } |
|
| 583 | - $query_params[0] = $where_params; |
|
| 584 | - $query_params['group_by'] = ['dtt_year', 'dtt_month']; |
|
| 585 | - $query_params = $this->addOrderByQueryParams($query_params, 'DTT_EVT_start', 'DESC'); |
|
| 586 | - |
|
| 587 | - $query_interval = EEH_DTT_Helper::get_sql_query_interval_for_offset( |
|
| 588 | - $this->get_timezone(), |
|
| 589 | - 'DTT_EVT_start' |
|
| 590 | - ); |
|
| 591 | - $columns_to_select = [ |
|
| 592 | - 'dtt_year' => ['YEAR(' . $query_interval . ')', '%s'], |
|
| 593 | - 'dtt_month' => ['MONTHNAME(' . $query_interval . ')', '%s'], |
|
| 594 | - 'dtt_month_num' => ['MONTH(' . $query_interval . ')', '%s'], |
|
| 595 | - ]; |
|
| 596 | - return $this->_get_all_wpdb_results($query_params, OBJECT, $columns_to_select); |
|
| 597 | - } |
|
| 598 | - |
|
| 599 | - |
|
| 600 | - /** |
|
| 601 | - * Updates the DTT_sold attribute on each datetime (based on the registrations |
|
| 602 | - * for the tickets for each datetime) |
|
| 603 | - * |
|
| 604 | - * @param EE_Base_Class[]|EE_Datetime[] $datetimes |
|
| 605 | - * @throws EE_Error |
|
| 606 | - * @throws ReflectionException |
|
| 607 | - */ |
|
| 608 | - public function update_sold(array $datetimes) |
|
| 609 | - { |
|
| 610 | - EE_Error::doing_it_wrong( |
|
| 611 | - __FUNCTION__, |
|
| 612 | - esc_html__( |
|
| 613 | - 'Please use \EEM_Ticket::update_tickets_sold() instead which will in turn correctly update both the Ticket AND Datetime counts.', |
|
| 614 | - 'event_espresso' |
|
| 615 | - ), |
|
| 616 | - '4.9.32.rc.005' |
|
| 617 | - ); |
|
| 618 | - foreach ($datetimes as $datetime) { |
|
| 619 | - $datetime->update_sold(); |
|
| 620 | - } |
|
| 621 | - } |
|
| 622 | - |
|
| 623 | - |
|
| 624 | - /** |
|
| 625 | - * Gets the total number of tickets available at a particular datetime |
|
| 626 | - * (does NOT take into account the datetime's spaces available) |
|
| 627 | - * |
|
| 628 | - * @param int $DTT_ID |
|
| 629 | - * @param array $query_params |
|
| 630 | - * @return int of tickets available. If sold out, return less than 1. If infinite, returns EE_INF, IF there are NO |
|
| 631 | - * tickets attached to datetime then FALSE is returned. |
|
| 632 | - * @throws EE_Error |
|
| 633 | - * @throws ReflectionException |
|
| 634 | - */ |
|
| 635 | - public function sum_tickets_currently_available_at_datetime($DTT_ID, array $query_params = []) |
|
| 636 | - { |
|
| 637 | - $datetime = $this->get_one_by_ID($DTT_ID); |
|
| 638 | - if ($datetime instanceof EE_Datetime) { |
|
| 639 | - return $datetime->tickets_remaining($query_params); |
|
| 640 | - } |
|
| 641 | - return 0; |
|
| 642 | - } |
|
| 643 | - |
|
| 644 | - |
|
| 645 | - /** |
|
| 646 | - * This returns an array of counts of datetimes in the database for each Datetime status that can be queried. |
|
| 647 | - * |
|
| 648 | - * @param array $stati_to_include If included you can restrict the statuses we return counts for by including the |
|
| 649 | - * stati you want counts for as values in the array. An empty array returns counts |
|
| 650 | - * for all valid stati. |
|
| 651 | - * @param array $query_params If included can be used to refine the conditions for returning the count (i.e. |
|
| 652 | - * only for Datetimes connected to a specific event, or specific ticket. |
|
| 653 | - * @return array The value returned is an array indexed by Datetime Status and the values are the counts. The |
|
| 654 | - * @throws EE_Error |
|
| 655 | - * stati used as index keys are: EE_Datetime::active EE_Datetime::upcoming |
|
| 656 | - * EE_Datetime::expired |
|
| 657 | - */ |
|
| 658 | - public function get_datetime_counts_by_status(array $stati_to_include = [], array $query_params = []) |
|
| 659 | - { |
|
| 660 | - // only accept where conditions for this query. |
|
| 661 | - $_where = isset($query_params[0]) ? $query_params[0] : []; |
|
| 662 | - $status_query_args = [ |
|
| 663 | - EE_Datetime::active => array_merge( |
|
| 664 | - $_where, |
|
| 665 | - ['DTT_EVT_start' => ['<', time()], 'DTT_EVT_end' => ['>', time()]] |
|
| 666 | - ), |
|
| 667 | - EE_Datetime::upcoming => array_merge( |
|
| 668 | - $_where, |
|
| 669 | - ['DTT_EVT_start' => ['>', time()]] |
|
| 670 | - ), |
|
| 671 | - EE_Datetime::expired => array_merge( |
|
| 672 | - $_where, |
|
| 673 | - ['DTT_EVT_end' => ['<', time()]] |
|
| 674 | - ), |
|
| 675 | - ]; |
|
| 676 | - if (! empty($stati_to_include)) { |
|
| 677 | - foreach (array_keys($status_query_args) as $status) { |
|
| 678 | - if (! in_array($status, $stati_to_include, true)) { |
|
| 679 | - unset($status_query_args[ $status ]); |
|
| 680 | - } |
|
| 681 | - } |
|
| 682 | - } |
|
| 683 | - // loop through and query counts for each stati. |
|
| 684 | - $status_query_results = []; |
|
| 685 | - foreach ($status_query_args as $status => $status_where_conditions) { |
|
| 686 | - $status_query_results[ $status ] = EEM_Datetime::count( |
|
| 687 | - [$status_where_conditions], |
|
| 688 | - 'DTT_ID', |
|
| 689 | - true |
|
| 690 | - ); |
|
| 691 | - } |
|
| 692 | - return $status_query_results; |
|
| 693 | - } |
|
| 694 | - |
|
| 695 | - |
|
| 696 | - /** |
|
| 697 | - * Returns the specific count for a given Datetime status matching any given query_params. |
|
| 698 | - * |
|
| 699 | - * @param string $status Valid string representation for Datetime status requested. (Defaults to Active). |
|
| 700 | - * @param array $query_params |
|
| 701 | - * @return int |
|
| 702 | - * @throws EE_Error |
|
| 703 | - */ |
|
| 704 | - public function get_datetime_count_for_status($status = EE_Datetime::active, array $query_params = []) |
|
| 705 | - { |
|
| 706 | - $count = $this->get_datetime_counts_by_status([$status], $query_params); |
|
| 707 | - return ! empty($count[ $status ]) ? $count[ $status ] : 0; |
|
| 708 | - } |
|
| 709 | - |
|
| 710 | - |
|
| 711 | - /** |
|
| 712 | - * @return bool|int |
|
| 713 | - * @since $VID:$ |
|
| 714 | - */ |
|
| 715 | - private function prepModelForQuery() |
|
| 716 | - { |
|
| 717 | - $prev_data_prep_value = $this->get_assumption_concerning_values_already_prepared_by_model_object(); |
|
| 718 | - $this->assume_values_already_prepared_by_model_object(EEM_Base::prepared_for_use_in_db); |
|
| 719 | - return $prev_data_prep_value; |
|
| 720 | - } |
|
| 721 | - |
|
| 722 | - |
|
| 723 | - /** |
|
| 724 | - * @param array $query_params |
|
| 725 | - * @param bool|int $prev_data_prep_value |
|
| 726 | - * @return EE_Base_Class[]|EE_Datetime[] |
|
| 727 | - * @throws EE_Error |
|
| 728 | - * @since $VID:$ |
|
| 729 | - */ |
|
| 730 | - private function getDatetimesAndRestoreModel(array $query_params, $prev_data_prep_value) |
|
| 731 | - { |
|
| 732 | - $result = $this->get_all($query_params); |
|
| 733 | - $this->assume_values_already_prepared_by_model_object($prev_data_prep_value); |
|
| 734 | - return $result; |
|
| 735 | - } |
|
| 736 | - |
|
| 737 | - |
|
| 738 | - /** |
|
| 739 | - * @param array $query_params |
|
| 740 | - * @param int $limit |
|
| 741 | - * @param string $order_by |
|
| 742 | - * @param string $order |
|
| 743 | - * @return array |
|
| 744 | - * @since $VID:$ |
|
| 745 | - */ |
|
| 746 | - private function addDefaultQueryParams(array $query_params, $limit = 0, $order_by = 'DTT_EVT_start', $order = 'ASC') |
|
| 747 | - { |
|
| 748 | - $query_params = $this->addOrderByQueryParams($query_params, $order_by, $order); |
|
| 749 | - $query_params = $this->addLimitQueryParams($query_params, $limit); |
|
| 750 | - return $query_params; |
|
| 751 | - } |
|
| 752 | - |
|
| 753 | - |
|
| 754 | - /** |
|
| 755 | - * @param array $query_params |
|
| 756 | - * @param string $default_where_conditions |
|
| 757 | - * @return array |
|
| 758 | - * @since $VID:$ |
|
| 759 | - */ |
|
| 760 | - private function addDefaultWhereConditions( |
|
| 761 | - array $query_params, |
|
| 762 | - $default_where_conditions = EEM_Base::default_where_conditions_none |
|
| 763 | - ) { |
|
| 764 | - $query_params['default_where_conditions'] = $default_where_conditions; |
|
| 765 | - return $query_params; |
|
| 766 | - } |
|
| 767 | - |
|
| 768 | - |
|
| 769 | - /** |
|
| 770 | - * @param array $where_params |
|
| 771 | - * @param bool $include_deleted |
|
| 772 | - * @param bool $include_expired |
|
| 773 | - * @return array |
|
| 774 | - * @since $VID:$ |
|
| 775 | - */ |
|
| 776 | - private function addDefaultWhereParams(array $where_params, $include_deleted = true, $include_expired = true) |
|
| 777 | - { |
|
| 778 | - $where_params = $this->addExpiredWhereParams($where_params, $include_expired); |
|
| 779 | - $where_params = $this->addDeletedWhereParams($where_params, $include_deleted); |
|
| 780 | - return $where_params; |
|
| 781 | - } |
|
| 782 | - |
|
| 783 | - |
|
| 784 | - /** |
|
| 785 | - * @param array $where_params |
|
| 786 | - * @param bool $include_deleted |
|
| 787 | - * @return array |
|
| 788 | - * @since $VID:$ |
|
| 789 | - */ |
|
| 790 | - private function addDeletedWhereParams(array $where_params, $include_deleted = true) |
|
| 791 | - { |
|
| 792 | - $deleted = $include_deleted ? [true, false] : [false]; |
|
| 793 | - $where_params['DTT_deleted'] = ['IN', $deleted]; |
|
| 794 | - return $where_params; |
|
| 795 | - } |
|
| 796 | - |
|
| 797 | - |
|
| 798 | - /** |
|
| 799 | - * @param array $where_params |
|
| 800 | - * @param bool $include_expired |
|
| 801 | - * @return array |
|
| 802 | - * @since $VID:$ |
|
| 803 | - */ |
|
| 804 | - private function addExpiredWhereParams(array $where_params, $include_expired = true) |
|
| 805 | - { |
|
| 806 | - if (! $include_expired) { |
|
| 807 | - $where_params['DTT_EVT_end'] = ['>=', current_time('mysql', true)]; |
|
| 808 | - } |
|
| 809 | - return $where_params; |
|
| 810 | - } |
|
| 811 | - |
|
| 812 | - |
|
| 813 | - /** |
|
| 814 | - * @param array $query_params |
|
| 815 | - * @param int $limit |
|
| 816 | - * @return array |
|
| 817 | - * @since $VID:$ |
|
| 818 | - */ |
|
| 819 | - private function addLimitQueryParams(array $query_params, $limit = 0) |
|
| 820 | - { |
|
| 821 | - if ($limit) { |
|
| 822 | - $query_params['limit'] = $limit; |
|
| 823 | - } |
|
| 824 | - return $query_params; |
|
| 825 | - } |
|
| 826 | - |
|
| 827 | - |
|
| 828 | - /** |
|
| 829 | - * @param array $query_params |
|
| 830 | - * @param string $order_by |
|
| 831 | - * @param string $order |
|
| 832 | - * @return array |
|
| 833 | - * @since $VID:$ |
|
| 834 | - */ |
|
| 835 | - private function addOrderByQueryParams(array $query_params, $order_by = 'DTT_EVT_start', $order = 'ASC') |
|
| 836 | - { |
|
| 837 | - $order = $order === 'ASC' ? 'ASC' : 'DESC'; |
|
| 838 | - $valid_order_columns = ['DTT_ID', 'DTT_EVT_start', 'DTT_EVT_end', 'DTT_order']; |
|
| 839 | - $order_by = in_array($order_by, $valid_order_columns) ? $order_by : 'DTT_EVT_start'; |
|
| 840 | - $query_params['order_by'] = [$order_by => $order]; |
|
| 841 | - return $query_params; |
|
| 842 | - } |
|
| 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 = [ |
|
| 39 | + 'Datetime' => new EE_Primary_Table('esp_datetime', 'DTT_ID'), |
|
| 40 | + ]; |
|
| 41 | + $this->_fields = [ |
|
| 42 | + 'Datetime' => [ |
|
| 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 = [ |
|
| 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 ] = |
|
| 135 | + new EE_Restriction_Generator_Event_Related_Public( |
|
| 136 | + $path_to_event_model |
|
| 137 | + ); |
|
| 138 | + $this->_cap_restriction_generators[ EEM_Base::caps_read_admin ] = |
|
| 139 | + new EE_Restriction_Generator_Event_Related_Protected( |
|
| 140 | + $path_to_event_model |
|
| 141 | + ); |
|
| 142 | + $this->_cap_restriction_generators[ EEM_Base::caps_edit ] = |
|
| 143 | + new EE_Restriction_Generator_Event_Related_Protected( |
|
| 144 | + $path_to_event_model |
|
| 145 | + ); |
|
| 146 | + $this->_cap_restriction_generators[ EEM_Base::caps_delete ] = |
|
| 147 | + new EE_Restriction_Generator_Event_Related_Protected( |
|
| 148 | + $path_to_event_model, |
|
| 149 | + EEM_Base::caps_edit |
|
| 150 | + ); |
|
| 151 | + parent::__construct($timezone); |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + |
|
| 155 | + /** |
|
| 156 | + * create new blank datetime |
|
| 157 | + * |
|
| 158 | + * @access public |
|
| 159 | + * @return EE_Datetime[] array on success, FALSE on fail |
|
| 160 | + * @throws EE_Error |
|
| 161 | + * @throws InvalidArgumentException |
|
| 162 | + * @throws InvalidDataTypeException |
|
| 163 | + * @throws ReflectionException |
|
| 164 | + * @throws InvalidInterfaceException |
|
| 165 | + */ |
|
| 166 | + public function create_new_blank_datetime() |
|
| 167 | + { |
|
| 168 | + // makes sure timezone is always set. |
|
| 169 | + $timezone_string = $this->get_timezone(); |
|
| 170 | + /** |
|
| 171 | + * Filters the initial start date for the new datetime. |
|
| 172 | + * Any time included in this value will be overridden later so use additional filters to modify the time. |
|
| 173 | + * |
|
| 174 | + * @param int $start_date Unixtimestamp representing now + 30 days in seconds. |
|
| 175 | + * @return int unixtimestamp |
|
| 176 | + */ |
|
| 177 | + $start_date = apply_filters( |
|
| 178 | + 'FHEE__EEM_Datetime__create_new_blank_datetime__start_date', |
|
| 179 | + $this->current_time_for_query('DTT_EVT_start', true) + MONTH_IN_SECONDS |
|
| 180 | + ); |
|
| 181 | + /** |
|
| 182 | + * Filters the initial end date for the new datetime. |
|
| 183 | + * Any time included in this value will be overridden later so use additional filters to modify the time. |
|
| 184 | + * |
|
| 185 | + * @param int $end_data Unixtimestamp representing now + 30 days in seconds. |
|
| 186 | + * @return int unixtimestamp |
|
| 187 | + */ |
|
| 188 | + $end_date = apply_filters( |
|
| 189 | + 'FHEE__EEM_Datetime__create_new_blank_datetime__end_date', |
|
| 190 | + $this->current_time_for_query('DTT_EVT_end', true) + MONTH_IN_SECONDS |
|
| 191 | + ); |
|
| 192 | + $blank_datetime = EE_Datetime::new_instance( |
|
| 193 | + [ |
|
| 194 | + 'DTT_EVT_start' => $start_date, |
|
| 195 | + 'DTT_EVT_end' => $end_date, |
|
| 196 | + 'DTT_order' => 1, |
|
| 197 | + 'DTT_reg_limit' => EE_INF, |
|
| 198 | + ], |
|
| 199 | + $timezone_string |
|
| 200 | + ); |
|
| 201 | + /** |
|
| 202 | + * Filters the initial start time and format for the new EE_Datetime instance. |
|
| 203 | + * |
|
| 204 | + * @param array $start_time An array having size 2. First element is the time, second element is the time |
|
| 205 | + * format. |
|
| 206 | + * @return array |
|
| 207 | + */ |
|
| 208 | + $start_time = apply_filters( |
|
| 209 | + 'FHEE__EEM_Datetime__create_new_blank_datetime__start_time', |
|
| 210 | + ['8am', 'ga'] |
|
| 211 | + ); |
|
| 212 | + /** |
|
| 213 | + * Filters the initial end time and format for the new EE_Datetime instance. |
|
| 214 | + * |
|
| 215 | + * @param array $end_time An array having size 2. First element is the time, second element is the time |
|
| 216 | + * format |
|
| 217 | + * @return array |
|
| 218 | + */ |
|
| 219 | + $end_time = apply_filters( |
|
| 220 | + 'FHEE__EEM_Datetime__create_new_blank_datetime__end_time', |
|
| 221 | + ['5pm', 'ga'] |
|
| 222 | + ); |
|
| 223 | + $this->validateStartAndEndTimeForBlankDate($start_time, $end_time); |
|
| 224 | + $blank_datetime->set_start_time( |
|
| 225 | + $this->convert_datetime_for_query( |
|
| 226 | + 'DTT_EVT_start', |
|
| 227 | + $start_time[0], |
|
| 228 | + $start_time[1], |
|
| 229 | + $timezone_string |
|
| 230 | + ) |
|
| 231 | + ); |
|
| 232 | + $blank_datetime->set_end_time( |
|
| 233 | + $this->convert_datetime_for_query( |
|
| 234 | + 'DTT_EVT_end', |
|
| 235 | + $end_time[0], |
|
| 236 | + $end_time[1], |
|
| 237 | + $timezone_string |
|
| 238 | + ) |
|
| 239 | + ); |
|
| 240 | + return [$blank_datetime]; |
|
| 241 | + } |
|
| 242 | + |
|
| 243 | + |
|
| 244 | + /** |
|
| 245 | + * Validates whether the start_time and end_time are in the expected format. |
|
| 246 | + * |
|
| 247 | + * @param array $start_time |
|
| 248 | + * @param array $end_time |
|
| 249 | + * @throws InvalidArgumentException |
|
| 250 | + * @throws InvalidDataTypeException |
|
| 251 | + */ |
|
| 252 | + private function validateStartAndEndTimeForBlankDate(array $start_time, array $end_time) |
|
| 253 | + { |
|
| 254 | + if (! is_array($start_time)) { |
|
| 255 | + throw new InvalidDataTypeException('start_time', $start_time, 'array'); |
|
| 256 | + } |
|
| 257 | + if (! is_array($end_time)) { |
|
| 258 | + throw new InvalidDataTypeException('end_time', $end_time, 'array'); |
|
| 259 | + } |
|
| 260 | + if (count($start_time) !== 2) { |
|
| 261 | + throw new InvalidArgumentException( |
|
| 262 | + sprintf( |
|
| 263 | + 'The variable %1$s is expected to be an array with two elements. The first item in the ' |
|
| 264 | + . 'array should be a valid time string, the second item in the array should be a valid time format', |
|
| 265 | + '$start_time' |
|
| 266 | + ) |
|
| 267 | + ); |
|
| 268 | + } |
|
| 269 | + if (count($end_time) !== 2) { |
|
| 270 | + throw new InvalidArgumentException( |
|
| 271 | + sprintf( |
|
| 272 | + 'The variable %1$s is expected to be an array with two elements. The first item in the ' |
|
| 273 | + . 'array should be a valid time string, the second item in the array should be a valid time format', |
|
| 274 | + '$end_time' |
|
| 275 | + ) |
|
| 276 | + ); |
|
| 277 | + } |
|
| 278 | + } |
|
| 279 | + |
|
| 280 | + |
|
| 281 | + /** |
|
| 282 | + * get event start date from db |
|
| 283 | + * |
|
| 284 | + * @access public |
|
| 285 | + * @param int $EVT_ID |
|
| 286 | + * @return EE_Datetime[] array on success, FALSE on fail |
|
| 287 | + * @throws EE_Error |
|
| 288 | + * @throws ReflectionException |
|
| 289 | + */ |
|
| 290 | + public function get_all_event_dates($EVT_ID = 0) |
|
| 291 | + { |
|
| 292 | + if (! $EVT_ID) { // on add_new_event event_id gets set to 0 |
|
| 293 | + return $this->create_new_blank_datetime(); |
|
| 294 | + } |
|
| 295 | + $results = $this->get_datetimes_for_event_ordered_by_DTT_order($EVT_ID); |
|
| 296 | + if (empty($results)) { |
|
| 297 | + return $this->create_new_blank_datetime(); |
|
| 298 | + } |
|
| 299 | + return $results; |
|
| 300 | + } |
|
| 301 | + |
|
| 302 | + |
|
| 303 | + /** |
|
| 304 | + * get all datetimes attached to an event ordered by the DTT_order field |
|
| 305 | + * |
|
| 306 | + * @public |
|
| 307 | + * @param int $EVT_ID event id |
|
| 308 | + * @param boolean $include_expired |
|
| 309 | + * @param boolean $include_deleted |
|
| 310 | + * @param int $limit If included then limit the count of results by |
|
| 311 | + * the given number |
|
| 312 | + * @return EE_Datetime[] |
|
| 313 | + * @throws EE_Error |
|
| 314 | + */ |
|
| 315 | + public function get_datetimes_for_event_ordered_by_DTT_order( |
|
| 316 | + $EVT_ID, |
|
| 317 | + $include_expired = true, |
|
| 318 | + $include_deleted = true, |
|
| 319 | + $limit = null |
|
| 320 | + ) { |
|
| 321 | + $prev_data_prep_value = $this->prepModelForQuery(); |
|
| 322 | + $where_params = ['Event.EVT_ID' => absint($EVT_ID)]; |
|
| 323 | + $query_params[0] = $this->addDefaultWhereParams($where_params, $include_deleted, $include_expired); |
|
| 324 | + $query_params = $this->addDefaultWhereConditions($query_params); |
|
| 325 | + $query_params = $this->addDefaultQueryParams($query_params, $limit, 'DTT_order'); |
|
| 326 | + return $this->getDatetimesAndRestoreModel($query_params, $prev_data_prep_value); |
|
| 327 | + } |
|
| 328 | + |
|
| 329 | + |
|
| 330 | + /** |
|
| 331 | + * Gets the datetimes for the event (with the given limit), and orders them by "importance". |
|
| 332 | + * By importance, we mean that the primary datetimes are most important (DEPRECATED FOR NOW), |
|
| 333 | + * and then the earlier datetimes are the most important. |
|
| 334 | + * Maybe we'll want this to take into account datetimes that haven't already passed, but we don't yet. |
|
| 335 | + * |
|
| 336 | + * @param int $EVT_ID |
|
| 337 | + * @param int $limit |
|
| 338 | + * @return EE_Datetime[]|EE_Base_Class[] |
|
| 339 | + * @throws EE_Error |
|
| 340 | + */ |
|
| 341 | + public function get_datetimes_for_event_ordered_by_importance($EVT_ID = 0, $limit = 0) |
|
| 342 | + { |
|
| 343 | + $query_params[0] = ['Event.EVT_ID' => absint($EVT_ID)]; |
|
| 344 | + $query_params = $this->addDefaultWhereConditions($query_params); |
|
| 345 | + $query_params = $this->addDefaultQueryParams($query_params, $limit); |
|
| 346 | + return $this->get_all($query_params); |
|
| 347 | + } |
|
| 348 | + |
|
| 349 | + |
|
| 350 | + /** |
|
| 351 | + * @param int $EVT_ID |
|
| 352 | + * @param boolean $include_expired |
|
| 353 | + * @param boolean $include_deleted |
|
| 354 | + * @return EE_Datetime |
|
| 355 | + * @throws EE_Error |
|
| 356 | + */ |
|
| 357 | + public function get_oldest_datetime_for_event($EVT_ID, $include_expired = false, $include_deleted = false) |
|
| 358 | + { |
|
| 359 | + $results = $this->get_datetimes_for_event_ordered_by_start_time( |
|
| 360 | + $EVT_ID, |
|
| 361 | + $include_expired, |
|
| 362 | + $include_deleted, |
|
| 363 | + 1 |
|
| 364 | + ); |
|
| 365 | + if ($results) { |
|
| 366 | + return array_shift($results); |
|
| 367 | + } |
|
| 368 | + return null; |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + |
|
| 372 | + /** |
|
| 373 | + * Gets the 'primary' datetime for an event. |
|
| 374 | + * |
|
| 375 | + * @param int $EVT_ID |
|
| 376 | + * @param bool $try_to_exclude_expired |
|
| 377 | + * @param bool $try_to_exclude_deleted |
|
| 378 | + * @return \EE_Datetime |
|
| 379 | + * @throws EE_Error |
|
| 380 | + */ |
|
| 381 | + public function get_primary_datetime_for_event( |
|
| 382 | + $EVT_ID, |
|
| 383 | + $try_to_exclude_expired = true, |
|
| 384 | + $try_to_exclude_deleted = true |
|
| 385 | + ) { |
|
| 386 | + if ($try_to_exclude_expired) { |
|
| 387 | + $non_expired = $this->get_oldest_datetime_for_event($EVT_ID, false, false); |
|
| 388 | + if ($non_expired) { |
|
| 389 | + return $non_expired; |
|
| 390 | + } |
|
| 391 | + } |
|
| 392 | + if ($try_to_exclude_deleted) { |
|
| 393 | + $expired_even = $this->get_oldest_datetime_for_event($EVT_ID, true); |
|
| 394 | + if ($expired_even) { |
|
| 395 | + return $expired_even; |
|
| 396 | + } |
|
| 397 | + } |
|
| 398 | + return $this->get_oldest_datetime_for_event($EVT_ID, true, true); |
|
| 399 | + } |
|
| 400 | + |
|
| 401 | + |
|
| 402 | + /** |
|
| 403 | + * Gets ALL the datetimes for an event (including trashed ones, for now), ordered |
|
| 404 | + * only by start date |
|
| 405 | + * |
|
| 406 | + * @param int $EVT_ID |
|
| 407 | + * @param boolean $include_expired |
|
| 408 | + * @param boolean $include_deleted |
|
| 409 | + * @param int $limit |
|
| 410 | + * @return EE_Datetime[] |
|
| 411 | + * @throws EE_Error |
|
| 412 | + */ |
|
| 413 | + public function get_datetimes_for_event_ordered_by_start_time( |
|
| 414 | + $EVT_ID, |
|
| 415 | + $include_expired = true, |
|
| 416 | + $include_deleted = true, |
|
| 417 | + $limit = null |
|
| 418 | + ) { |
|
| 419 | + $prev_data_prep_value = $this->prepModelForQuery(); |
|
| 420 | + $where_params = ['Event.EVT_ID' => absint($EVT_ID)]; |
|
| 421 | + $query_params[0] = $this->addDefaultWhereParams($where_params, $include_deleted, $include_expired); |
|
| 422 | + $query_params = $this->addDefaultWhereConditions( |
|
| 423 | + $query_params, |
|
| 424 | + EEM_Base::default_where_conditions_this_only |
|
| 425 | + ); |
|
| 426 | + $query_params = $this->addDefaultQueryParams($query_params, $limit, 'DTT_order'); |
|
| 427 | + return $this->getDatetimesAndRestoreModel($query_params, $prev_data_prep_value); |
|
| 428 | + } |
|
| 429 | + |
|
| 430 | + |
|
| 431 | + /** |
|
| 432 | + * Gets ALL the datetimes for an ticket (including trashed ones, for now), ordered |
|
| 433 | + * only by start date |
|
| 434 | + * |
|
| 435 | + * @param int $TKT_ID |
|
| 436 | + * @param boolean $include_expired |
|
| 437 | + * @param boolean $include_deleted |
|
| 438 | + * @param int $limit |
|
| 439 | + * @return EE_Datetime[] |
|
| 440 | + * @throws EE_Error |
|
| 441 | + */ |
|
| 442 | + public function get_datetimes_for_ticket_ordered_by_start_time( |
|
| 443 | + $TKT_ID, |
|
| 444 | + $include_expired = true, |
|
| 445 | + $include_deleted = true, |
|
| 446 | + $limit = null |
|
| 447 | + ) { |
|
| 448 | + $prev_data_prep_value = $this->prepModelForQuery(); |
|
| 449 | + $where_params = ['Ticket.TKT_ID' => absint($TKT_ID)]; |
|
| 450 | + $query_params[0] = $this->addDefaultWhereParams($where_params, $include_deleted, $include_expired); |
|
| 451 | + $query_params = $this->addDefaultQueryParams($query_params, $limit); |
|
| 452 | + return $this->getDatetimesAndRestoreModel($query_params, $prev_data_prep_value); |
|
| 453 | + } |
|
| 454 | + |
|
| 455 | + |
|
| 456 | + /** |
|
| 457 | + * Gets all the datetimes for a ticket (including trashed ones, for now), ordered by the DTT_order for the |
|
| 458 | + * datetimes. |
|
| 459 | + * |
|
| 460 | + * @param int $TKT_ID ID of ticket to retrieve the datetimes for |
|
| 461 | + * @param boolean $include_expired whether to include expired datetimes or not |
|
| 462 | + * @param boolean $include_deleted whether to include trashed datetimes or not. |
|
| 463 | + * @param int|null $limit if null, no limit, if int then limit results by |
|
| 464 | + * that number |
|
| 465 | + * @return EE_Datetime[] |
|
| 466 | + * @throws EE_Error |
|
| 467 | + */ |
|
| 468 | + public function get_datetimes_for_ticket_ordered_by_DTT_order( |
|
| 469 | + $TKT_ID, |
|
| 470 | + $include_expired = true, |
|
| 471 | + $include_deleted = true, |
|
| 472 | + $limit = null |
|
| 473 | + ) { |
|
| 474 | + $prev_data_prep_value = $this->prepModelForQuery(); |
|
| 475 | + $where_params = ['Ticket.TKT_ID' => absint($TKT_ID)]; |
|
| 476 | + $query_params[0] = $this->addDefaultWhereParams($where_params, $include_deleted, $include_expired); |
|
| 477 | + $query_params = $this->addDefaultQueryParams($query_params, $limit, 'DTT_order'); |
|
| 478 | + return $this->getDatetimesAndRestoreModel($query_params, $prev_data_prep_value); |
|
| 479 | + } |
|
| 480 | + |
|
| 481 | + |
|
| 482 | + /** |
|
| 483 | + * Gets the most important datetime for a particular event (ie, the primary event usually. But if for some WACK |
|
| 484 | + * reason it doesn't exist, we consider the earliest event the most important) |
|
| 485 | + * |
|
| 486 | + * @param int $EVT_ID |
|
| 487 | + * @return EE_Datetime |
|
| 488 | + * @throws EE_Error |
|
| 489 | + */ |
|
| 490 | + public function get_most_important_datetime_for_event($EVT_ID) |
|
| 491 | + { |
|
| 492 | + $results = $this->get_datetimes_for_event_ordered_by_importance($EVT_ID, 1); |
|
| 493 | + if ($results) { |
|
| 494 | + return array_shift($results); |
|
| 495 | + } |
|
| 496 | + return null; |
|
| 497 | + } |
|
| 498 | + |
|
| 499 | + |
|
| 500 | + /** |
|
| 501 | + * This returns a wpdb->results Array of all DTT month and years matching the incoming query params and |
|
| 502 | + * grouped by month and year. |
|
| 503 | + * |
|
| 504 | + * @param array $where_params @see |
|
| 505 | + * https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
| 506 | + * @param string $evt_active_status A string representing the evt active status to filter the months by. |
|
| 507 | + * Can be: |
|
| 508 | + * - '' = no filter |
|
| 509 | + * - upcoming = Published events with at least one upcoming datetime. |
|
| 510 | + * - expired = Events with all datetimes expired. |
|
| 511 | + * - active = Events that are published and have at least one datetime that |
|
| 512 | + * starts before now and ends after now. |
|
| 513 | + * - inactive = Events that are either not published. |
|
| 514 | + * @return EE_Base_Class[] |
|
| 515 | + * @throws EE_Error |
|
| 516 | + * @throws InvalidArgumentException |
|
| 517 | + * @throws InvalidArgumentException |
|
| 518 | + */ |
|
| 519 | + public function get_dtt_months_and_years(array $where_params, $evt_active_status = '') |
|
| 520 | + { |
|
| 521 | + $current_time_for_DTT_EVT_start = $this->current_time_for_query('DTT_EVT_start'); |
|
| 522 | + $current_time_for_DTT_EVT_end = $this->current_time_for_query('DTT_EVT_end'); |
|
| 523 | + switch ($evt_active_status) { |
|
| 524 | + case 'upcoming': |
|
| 525 | + $where_params['Event.status'] = 'publish'; |
|
| 526 | + // if there are already query_params matching DTT_EVT_start then we need to modify that to add them. |
|
| 527 | + if (isset($where_params['DTT_EVT_start'])) { |
|
| 528 | + $where_params['DTT_EVT_start*****'] = $where_params['DTT_EVT_start']; |
|
| 529 | + } |
|
| 530 | + $where_params['DTT_EVT_start'] = ['>', $current_time_for_DTT_EVT_start]; |
|
| 531 | + break; |
|
| 532 | + case 'expired': |
|
| 533 | + if (isset($where_params['Event.status'])) { |
|
| 534 | + unset($where_params['Event.status']); |
|
| 535 | + } |
|
| 536 | + // get events to exclude |
|
| 537 | + $exclude_query[0] = array_merge( |
|
| 538 | + $where_params, |
|
| 539 | + ['DTT_EVT_end' => ['>', $current_time_for_DTT_EVT_end]] |
|
| 540 | + ); |
|
| 541 | + // first get all events that have datetimes where its not expired. |
|
| 542 | + $event_ids = $this->_get_all_wpdb_results( |
|
| 543 | + $exclude_query, |
|
| 544 | + OBJECT_K, |
|
| 545 | + 'Datetime.EVT_ID' |
|
| 546 | + ); |
|
| 547 | + $event_ids = array_keys($event_ids); |
|
| 548 | + if (isset($where_params['DTT_EVT_end'])) { |
|
| 549 | + $where_params['DTT_EVT_end****'] = $where_params['DTT_EVT_end']; |
|
| 550 | + } |
|
| 551 | + $where_params['DTT_EVT_end'] = ['<', $current_time_for_DTT_EVT_end]; |
|
| 552 | + $where_params['Event.EVT_ID'] = ['NOT IN', $event_ids]; |
|
| 553 | + break; |
|
| 554 | + case 'active': |
|
| 555 | + $where_params['Event.status'] = 'publish'; |
|
| 556 | + if (isset($where_params['DTT_EVT_start'])) { |
|
| 557 | + $where_params['Datetime.DTT_EVT_start******'] = $where_params['DTT_EVT_start']; |
|
| 558 | + } |
|
| 559 | + if (isset($where_params['Datetime.DTT_EVT_end'])) { |
|
| 560 | + $where_params['Datetime.DTT_EVT_end*****'] = $where_params['DTT_EVT_end']; |
|
| 561 | + } |
|
| 562 | + $where_params['DTT_EVT_start'] = ['<', $current_time_for_DTT_EVT_start]; |
|
| 563 | + $where_params['DTT_EVT_end'] = ['>', $current_time_for_DTT_EVT_end]; |
|
| 564 | + break; |
|
| 565 | + case 'inactive': |
|
| 566 | + if (isset($where_params['Event.status'])) { |
|
| 567 | + unset($where_params['Event.status']); |
|
| 568 | + } |
|
| 569 | + if (isset($where_params['OR'])) { |
|
| 570 | + $where_params['AND']['OR'] = $where_params['OR']; |
|
| 571 | + } |
|
| 572 | + if (isset($where_params['DTT_EVT_end'])) { |
|
| 573 | + $where_params['AND']['DTT_EVT_end****'] = $where_params['DTT_EVT_end']; |
|
| 574 | + unset($where_params['DTT_EVT_end']); |
|
| 575 | + } |
|
| 576 | + if (isset($where_params['DTT_EVT_start'])) { |
|
| 577 | + $where_params['AND']['DTT_EVT_start'] = $where_params['DTT_EVT_start']; |
|
| 578 | + unset($where_params['DTT_EVT_start']); |
|
| 579 | + } |
|
| 580 | + $where_params['AND']['Event.status'] = ['!=', 'publish']; |
|
| 581 | + break; |
|
| 582 | + } |
|
| 583 | + $query_params[0] = $where_params; |
|
| 584 | + $query_params['group_by'] = ['dtt_year', 'dtt_month']; |
|
| 585 | + $query_params = $this->addOrderByQueryParams($query_params, 'DTT_EVT_start', 'DESC'); |
|
| 586 | + |
|
| 587 | + $query_interval = EEH_DTT_Helper::get_sql_query_interval_for_offset( |
|
| 588 | + $this->get_timezone(), |
|
| 589 | + 'DTT_EVT_start' |
|
| 590 | + ); |
|
| 591 | + $columns_to_select = [ |
|
| 592 | + 'dtt_year' => ['YEAR(' . $query_interval . ')', '%s'], |
|
| 593 | + 'dtt_month' => ['MONTHNAME(' . $query_interval . ')', '%s'], |
|
| 594 | + 'dtt_month_num' => ['MONTH(' . $query_interval . ')', '%s'], |
|
| 595 | + ]; |
|
| 596 | + return $this->_get_all_wpdb_results($query_params, OBJECT, $columns_to_select); |
|
| 597 | + } |
|
| 598 | + |
|
| 599 | + |
|
| 600 | + /** |
|
| 601 | + * Updates the DTT_sold attribute on each datetime (based on the registrations |
|
| 602 | + * for the tickets for each datetime) |
|
| 603 | + * |
|
| 604 | + * @param EE_Base_Class[]|EE_Datetime[] $datetimes |
|
| 605 | + * @throws EE_Error |
|
| 606 | + * @throws ReflectionException |
|
| 607 | + */ |
|
| 608 | + public function update_sold(array $datetimes) |
|
| 609 | + { |
|
| 610 | + EE_Error::doing_it_wrong( |
|
| 611 | + __FUNCTION__, |
|
| 612 | + esc_html__( |
|
| 613 | + 'Please use \EEM_Ticket::update_tickets_sold() instead which will in turn correctly update both the Ticket AND Datetime counts.', |
|
| 614 | + 'event_espresso' |
|
| 615 | + ), |
|
| 616 | + '4.9.32.rc.005' |
|
| 617 | + ); |
|
| 618 | + foreach ($datetimes as $datetime) { |
|
| 619 | + $datetime->update_sold(); |
|
| 620 | + } |
|
| 621 | + } |
|
| 622 | + |
|
| 623 | + |
|
| 624 | + /** |
|
| 625 | + * Gets the total number of tickets available at a particular datetime |
|
| 626 | + * (does NOT take into account the datetime's spaces available) |
|
| 627 | + * |
|
| 628 | + * @param int $DTT_ID |
|
| 629 | + * @param array $query_params |
|
| 630 | + * @return int of tickets available. If sold out, return less than 1. If infinite, returns EE_INF, IF there are NO |
|
| 631 | + * tickets attached to datetime then FALSE is returned. |
|
| 632 | + * @throws EE_Error |
|
| 633 | + * @throws ReflectionException |
|
| 634 | + */ |
|
| 635 | + public function sum_tickets_currently_available_at_datetime($DTT_ID, array $query_params = []) |
|
| 636 | + { |
|
| 637 | + $datetime = $this->get_one_by_ID($DTT_ID); |
|
| 638 | + if ($datetime instanceof EE_Datetime) { |
|
| 639 | + return $datetime->tickets_remaining($query_params); |
|
| 640 | + } |
|
| 641 | + return 0; |
|
| 642 | + } |
|
| 643 | + |
|
| 644 | + |
|
| 645 | + /** |
|
| 646 | + * This returns an array of counts of datetimes in the database for each Datetime status that can be queried. |
|
| 647 | + * |
|
| 648 | + * @param array $stati_to_include If included you can restrict the statuses we return counts for by including the |
|
| 649 | + * stati you want counts for as values in the array. An empty array returns counts |
|
| 650 | + * for all valid stati. |
|
| 651 | + * @param array $query_params If included can be used to refine the conditions for returning the count (i.e. |
|
| 652 | + * only for Datetimes connected to a specific event, or specific ticket. |
|
| 653 | + * @return array The value returned is an array indexed by Datetime Status and the values are the counts. The |
|
| 654 | + * @throws EE_Error |
|
| 655 | + * stati used as index keys are: EE_Datetime::active EE_Datetime::upcoming |
|
| 656 | + * EE_Datetime::expired |
|
| 657 | + */ |
|
| 658 | + public function get_datetime_counts_by_status(array $stati_to_include = [], array $query_params = []) |
|
| 659 | + { |
|
| 660 | + // only accept where conditions for this query. |
|
| 661 | + $_where = isset($query_params[0]) ? $query_params[0] : []; |
|
| 662 | + $status_query_args = [ |
|
| 663 | + EE_Datetime::active => array_merge( |
|
| 664 | + $_where, |
|
| 665 | + ['DTT_EVT_start' => ['<', time()], 'DTT_EVT_end' => ['>', time()]] |
|
| 666 | + ), |
|
| 667 | + EE_Datetime::upcoming => array_merge( |
|
| 668 | + $_where, |
|
| 669 | + ['DTT_EVT_start' => ['>', time()]] |
|
| 670 | + ), |
|
| 671 | + EE_Datetime::expired => array_merge( |
|
| 672 | + $_where, |
|
| 673 | + ['DTT_EVT_end' => ['<', time()]] |
|
| 674 | + ), |
|
| 675 | + ]; |
|
| 676 | + if (! empty($stati_to_include)) { |
|
| 677 | + foreach (array_keys($status_query_args) as $status) { |
|
| 678 | + if (! in_array($status, $stati_to_include, true)) { |
|
| 679 | + unset($status_query_args[ $status ]); |
|
| 680 | + } |
|
| 681 | + } |
|
| 682 | + } |
|
| 683 | + // loop through and query counts for each stati. |
|
| 684 | + $status_query_results = []; |
|
| 685 | + foreach ($status_query_args as $status => $status_where_conditions) { |
|
| 686 | + $status_query_results[ $status ] = EEM_Datetime::count( |
|
| 687 | + [$status_where_conditions], |
|
| 688 | + 'DTT_ID', |
|
| 689 | + true |
|
| 690 | + ); |
|
| 691 | + } |
|
| 692 | + return $status_query_results; |
|
| 693 | + } |
|
| 694 | + |
|
| 695 | + |
|
| 696 | + /** |
|
| 697 | + * Returns the specific count for a given Datetime status matching any given query_params. |
|
| 698 | + * |
|
| 699 | + * @param string $status Valid string representation for Datetime status requested. (Defaults to Active). |
|
| 700 | + * @param array $query_params |
|
| 701 | + * @return int |
|
| 702 | + * @throws EE_Error |
|
| 703 | + */ |
|
| 704 | + public function get_datetime_count_for_status($status = EE_Datetime::active, array $query_params = []) |
|
| 705 | + { |
|
| 706 | + $count = $this->get_datetime_counts_by_status([$status], $query_params); |
|
| 707 | + return ! empty($count[ $status ]) ? $count[ $status ] : 0; |
|
| 708 | + } |
|
| 709 | + |
|
| 710 | + |
|
| 711 | + /** |
|
| 712 | + * @return bool|int |
|
| 713 | + * @since $VID:$ |
|
| 714 | + */ |
|
| 715 | + private function prepModelForQuery() |
|
| 716 | + { |
|
| 717 | + $prev_data_prep_value = $this->get_assumption_concerning_values_already_prepared_by_model_object(); |
|
| 718 | + $this->assume_values_already_prepared_by_model_object(EEM_Base::prepared_for_use_in_db); |
|
| 719 | + return $prev_data_prep_value; |
|
| 720 | + } |
|
| 721 | + |
|
| 722 | + |
|
| 723 | + /** |
|
| 724 | + * @param array $query_params |
|
| 725 | + * @param bool|int $prev_data_prep_value |
|
| 726 | + * @return EE_Base_Class[]|EE_Datetime[] |
|
| 727 | + * @throws EE_Error |
|
| 728 | + * @since $VID:$ |
|
| 729 | + */ |
|
| 730 | + private function getDatetimesAndRestoreModel(array $query_params, $prev_data_prep_value) |
|
| 731 | + { |
|
| 732 | + $result = $this->get_all($query_params); |
|
| 733 | + $this->assume_values_already_prepared_by_model_object($prev_data_prep_value); |
|
| 734 | + return $result; |
|
| 735 | + } |
|
| 736 | + |
|
| 737 | + |
|
| 738 | + /** |
|
| 739 | + * @param array $query_params |
|
| 740 | + * @param int $limit |
|
| 741 | + * @param string $order_by |
|
| 742 | + * @param string $order |
|
| 743 | + * @return array |
|
| 744 | + * @since $VID:$ |
|
| 745 | + */ |
|
| 746 | + private function addDefaultQueryParams(array $query_params, $limit = 0, $order_by = 'DTT_EVT_start', $order = 'ASC') |
|
| 747 | + { |
|
| 748 | + $query_params = $this->addOrderByQueryParams($query_params, $order_by, $order); |
|
| 749 | + $query_params = $this->addLimitQueryParams($query_params, $limit); |
|
| 750 | + return $query_params; |
|
| 751 | + } |
|
| 752 | + |
|
| 753 | + |
|
| 754 | + /** |
|
| 755 | + * @param array $query_params |
|
| 756 | + * @param string $default_where_conditions |
|
| 757 | + * @return array |
|
| 758 | + * @since $VID:$ |
|
| 759 | + */ |
|
| 760 | + private function addDefaultWhereConditions( |
|
| 761 | + array $query_params, |
|
| 762 | + $default_where_conditions = EEM_Base::default_where_conditions_none |
|
| 763 | + ) { |
|
| 764 | + $query_params['default_where_conditions'] = $default_where_conditions; |
|
| 765 | + return $query_params; |
|
| 766 | + } |
|
| 767 | + |
|
| 768 | + |
|
| 769 | + /** |
|
| 770 | + * @param array $where_params |
|
| 771 | + * @param bool $include_deleted |
|
| 772 | + * @param bool $include_expired |
|
| 773 | + * @return array |
|
| 774 | + * @since $VID:$ |
|
| 775 | + */ |
|
| 776 | + private function addDefaultWhereParams(array $where_params, $include_deleted = true, $include_expired = true) |
|
| 777 | + { |
|
| 778 | + $where_params = $this->addExpiredWhereParams($where_params, $include_expired); |
|
| 779 | + $where_params = $this->addDeletedWhereParams($where_params, $include_deleted); |
|
| 780 | + return $where_params; |
|
| 781 | + } |
|
| 782 | + |
|
| 783 | + |
|
| 784 | + /** |
|
| 785 | + * @param array $where_params |
|
| 786 | + * @param bool $include_deleted |
|
| 787 | + * @return array |
|
| 788 | + * @since $VID:$ |
|
| 789 | + */ |
|
| 790 | + private function addDeletedWhereParams(array $where_params, $include_deleted = true) |
|
| 791 | + { |
|
| 792 | + $deleted = $include_deleted ? [true, false] : [false]; |
|
| 793 | + $where_params['DTT_deleted'] = ['IN', $deleted]; |
|
| 794 | + return $where_params; |
|
| 795 | + } |
|
| 796 | + |
|
| 797 | + |
|
| 798 | + /** |
|
| 799 | + * @param array $where_params |
|
| 800 | + * @param bool $include_expired |
|
| 801 | + * @return array |
|
| 802 | + * @since $VID:$ |
|
| 803 | + */ |
|
| 804 | + private function addExpiredWhereParams(array $where_params, $include_expired = true) |
|
| 805 | + { |
|
| 806 | + if (! $include_expired) { |
|
| 807 | + $where_params['DTT_EVT_end'] = ['>=', current_time('mysql', true)]; |
|
| 808 | + } |
|
| 809 | + return $where_params; |
|
| 810 | + } |
|
| 811 | + |
|
| 812 | + |
|
| 813 | + /** |
|
| 814 | + * @param array $query_params |
|
| 815 | + * @param int $limit |
|
| 816 | + * @return array |
|
| 817 | + * @since $VID:$ |
|
| 818 | + */ |
|
| 819 | + private function addLimitQueryParams(array $query_params, $limit = 0) |
|
| 820 | + { |
|
| 821 | + if ($limit) { |
|
| 822 | + $query_params['limit'] = $limit; |
|
| 823 | + } |
|
| 824 | + return $query_params; |
|
| 825 | + } |
|
| 826 | + |
|
| 827 | + |
|
| 828 | + /** |
|
| 829 | + * @param array $query_params |
|
| 830 | + * @param string $order_by |
|
| 831 | + * @param string $order |
|
| 832 | + * @return array |
|
| 833 | + * @since $VID:$ |
|
| 834 | + */ |
|
| 835 | + private function addOrderByQueryParams(array $query_params, $order_by = 'DTT_EVT_start', $order = 'ASC') |
|
| 836 | + { |
|
| 837 | + $order = $order === 'ASC' ? 'ASC' : 'DESC'; |
|
| 838 | + $valid_order_columns = ['DTT_ID', 'DTT_EVT_start', 'DTT_EVT_end', 'DTT_order']; |
|
| 839 | + $order_by = in_array($order_by, $valid_order_columns) ? $order_by : 'DTT_EVT_start'; |
|
| 840 | + $query_params['order_by'] = [$order_by => $order]; |
|
| 841 | + return $query_params; |
|
| 842 | + } |
|
| 843 | 843 | } |
@@ -121,7 +121,7 @@ discard block |
||
| 121 | 121 | ), |
| 122 | 122 | ], |
| 123 | 123 | ]; |
| 124 | - $this->_model_relations = [ |
|
| 124 | + $this->_model_relations = [ |
|
| 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,19 +131,19 @@ 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 ] = |
|
| 134 | + $this->_cap_restriction_generators[EEM_Base::caps_read] = |
|
| 135 | 135 | new EE_Restriction_Generator_Event_Related_Public( |
| 136 | 136 | $path_to_event_model |
| 137 | 137 | ); |
| 138 | - $this->_cap_restriction_generators[ EEM_Base::caps_read_admin ] = |
|
| 138 | + $this->_cap_restriction_generators[EEM_Base::caps_read_admin] = |
|
| 139 | 139 | new EE_Restriction_Generator_Event_Related_Protected( |
| 140 | 140 | $path_to_event_model |
| 141 | 141 | ); |
| 142 | - $this->_cap_restriction_generators[ EEM_Base::caps_edit ] = |
|
| 142 | + $this->_cap_restriction_generators[EEM_Base::caps_edit] = |
|
| 143 | 143 | new EE_Restriction_Generator_Event_Related_Protected( |
| 144 | 144 | $path_to_event_model |
| 145 | 145 | ); |
| 146 | - $this->_cap_restriction_generators[ EEM_Base::caps_delete ] = |
|
| 146 | + $this->_cap_restriction_generators[EEM_Base::caps_delete] = |
|
| 147 | 147 | new EE_Restriction_Generator_Event_Related_Protected( |
| 148 | 148 | $path_to_event_model, |
| 149 | 149 | EEM_Base::caps_edit |
@@ -185,7 +185,7 @@ discard block |
||
| 185 | 185 | * @param int $end_data Unixtimestamp representing now + 30 days in seconds. |
| 186 | 186 | * @return int unixtimestamp |
| 187 | 187 | */ |
| 188 | - $end_date = apply_filters( |
|
| 188 | + $end_date = apply_filters( |
|
| 189 | 189 | 'FHEE__EEM_Datetime__create_new_blank_datetime__end_date', |
| 190 | 190 | $this->current_time_for_query('DTT_EVT_end', true) + MONTH_IN_SECONDS |
| 191 | 191 | ); |
@@ -251,10 +251,10 @@ discard block |
||
| 251 | 251 | */ |
| 252 | 252 | private function validateStartAndEndTimeForBlankDate(array $start_time, array $end_time) |
| 253 | 253 | { |
| 254 | - if (! is_array($start_time)) { |
|
| 254 | + if ( ! is_array($start_time)) { |
|
| 255 | 255 | throw new InvalidDataTypeException('start_time', $start_time, 'array'); |
| 256 | 256 | } |
| 257 | - if (! is_array($end_time)) { |
|
| 257 | + if ( ! is_array($end_time)) { |
|
| 258 | 258 | throw new InvalidDataTypeException('end_time', $end_time, 'array'); |
| 259 | 259 | } |
| 260 | 260 | if (count($start_time) !== 2) { |
@@ -289,7 +289,7 @@ discard block |
||
| 289 | 289 | */ |
| 290 | 290 | public function get_all_event_dates($EVT_ID = 0) |
| 291 | 291 | { |
| 292 | - if (! $EVT_ID) { // on add_new_event event_id gets set to 0 |
|
| 292 | + if ( ! $EVT_ID) { // on add_new_event event_id gets set to 0 |
|
| 293 | 293 | return $this->create_new_blank_datetime(); |
| 294 | 294 | } |
| 295 | 295 | $results = $this->get_datetimes_for_event_ordered_by_DTT_order($EVT_ID); |
@@ -423,7 +423,7 @@ discard block |
||
| 423 | 423 | $query_params, |
| 424 | 424 | EEM_Base::default_where_conditions_this_only |
| 425 | 425 | ); |
| 426 | - $query_params = $this->addDefaultQueryParams($query_params, $limit, 'DTT_order'); |
|
| 426 | + $query_params = $this->addDefaultQueryParams($query_params, $limit, 'DTT_order'); |
|
| 427 | 427 | return $this->getDatetimesAndRestoreModel($query_params, $prev_data_prep_value); |
| 428 | 428 | } |
| 429 | 429 | |
@@ -584,14 +584,14 @@ discard block |
||
| 584 | 584 | $query_params['group_by'] = ['dtt_year', 'dtt_month']; |
| 585 | 585 | $query_params = $this->addOrderByQueryParams($query_params, 'DTT_EVT_start', 'DESC'); |
| 586 | 586 | |
| 587 | - $query_interval = EEH_DTT_Helper::get_sql_query_interval_for_offset( |
|
| 587 | + $query_interval = EEH_DTT_Helper::get_sql_query_interval_for_offset( |
|
| 588 | 588 | $this->get_timezone(), |
| 589 | 589 | 'DTT_EVT_start' |
| 590 | 590 | ); |
| 591 | 591 | $columns_to_select = [ |
| 592 | - 'dtt_year' => ['YEAR(' . $query_interval . ')', '%s'], |
|
| 593 | - 'dtt_month' => ['MONTHNAME(' . $query_interval . ')', '%s'], |
|
| 594 | - 'dtt_month_num' => ['MONTH(' . $query_interval . ')', '%s'], |
|
| 592 | + 'dtt_year' => ['YEAR('.$query_interval.')', '%s'], |
|
| 593 | + 'dtt_month' => ['MONTHNAME('.$query_interval.')', '%s'], |
|
| 594 | + 'dtt_month_num' => ['MONTH('.$query_interval.')', '%s'], |
|
| 595 | 595 | ]; |
| 596 | 596 | return $this->_get_all_wpdb_results($query_params, OBJECT, $columns_to_select); |
| 597 | 597 | } |
@@ -673,17 +673,17 @@ discard block |
||
| 673 | 673 | ['DTT_EVT_end' => ['<', time()]] |
| 674 | 674 | ), |
| 675 | 675 | ]; |
| 676 | - if (! empty($stati_to_include)) { |
|
| 676 | + if ( ! empty($stati_to_include)) { |
|
| 677 | 677 | foreach (array_keys($status_query_args) as $status) { |
| 678 | - if (! in_array($status, $stati_to_include, true)) { |
|
| 679 | - unset($status_query_args[ $status ]); |
|
| 678 | + if ( ! in_array($status, $stati_to_include, true)) { |
|
| 679 | + unset($status_query_args[$status]); |
|
| 680 | 680 | } |
| 681 | 681 | } |
| 682 | 682 | } |
| 683 | 683 | // loop through and query counts for each stati. |
| 684 | 684 | $status_query_results = []; |
| 685 | 685 | foreach ($status_query_args as $status => $status_where_conditions) { |
| 686 | - $status_query_results[ $status ] = EEM_Datetime::count( |
|
| 686 | + $status_query_results[$status] = EEM_Datetime::count( |
|
| 687 | 687 | [$status_where_conditions], |
| 688 | 688 | 'DTT_ID', |
| 689 | 689 | true |
@@ -704,7 +704,7 @@ discard block |
||
| 704 | 704 | public function get_datetime_count_for_status($status = EE_Datetime::active, array $query_params = []) |
| 705 | 705 | { |
| 706 | 706 | $count = $this->get_datetime_counts_by_status([$status], $query_params); |
| 707 | - return ! empty($count[ $status ]) ? $count[ $status ] : 0; |
|
| 707 | + return ! empty($count[$status]) ? $count[$status] : 0; |
|
| 708 | 708 | } |
| 709 | 709 | |
| 710 | 710 | |
@@ -803,7 +803,7 @@ discard block |
||
| 803 | 803 | */ |
| 804 | 804 | private function addExpiredWhereParams(array $where_params, $include_expired = true) |
| 805 | 805 | { |
| 806 | - if (! $include_expired) { |
|
| 806 | + if ( ! $include_expired) { |
|
| 807 | 807 | $where_params['DTT_EVT_end'] = ['>=', current_time('mysql', true)]; |
| 808 | 808 | } |
| 809 | 809 | return $where_params; |