Completed
Branch BETA-4.9-messages-queue-fixed (941081)
by
unknown
17:38 queued 10s
created

EEM_Datetime::__construct()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 35
Code Lines 29

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 35
rs 8.8571
cc 1
eloc 29
nc 1
nop 1
1
<?php if ( ! defined('EVENT_ESPRESSO_VERSION')) exit('No direct script access allowed');
2
/**
3
 * Event Espresso
4
 *
5
 * Event Registration and Management Plugin for WordPress
6
 *
7
 * @ package			Event Espresso
8
 * @ author				Seth Shoultes
9
 * @ copyright		(c) 2008-2011 Event Espresso  All Rights Reserved.
10
 * @ license			http://eventespresso.com/support/terms-conditions/   * see Plugin Licensing *
11
 * @ link					http://www.eventespresso.com
12
 * @ version		 	4.0
13
 *
14
 * ------------------------------------------------------------------------
15
 *
16
 *	Datetime Model
17
 *
18
 * @package			Event Espresso
19
 * @subpackage		includes/models/
20
 * @author				Brent Christensen
21
 *
22
 * ------------------------------------------------------------------------
23
 */
24
require_once ( EE_MODELS . 'EEM_Soft_Delete_Base.model.php' );
25
require_once ( EE_CLASSES . 'EE_Datetime.class.php' );
26
27
class EEM_Datetime extends EEM_Soft_Delete_Base {
28
29
  	// private instance of the EEM_Datetime object
30
	protected static $_instance = NULL;
31
32
	/**
33
	 *		private constructor to prevent direct creation
34
	 *		@Constructor
35
	 *		@access private
36
	 *		@param string $timezone string representing the timezone we want to set for returned Date Time Strings (and any incoming timezone data that gets saved).  Note this just sends the timezone info to the date time model field objects.  Default is NULL (and will be assumed using the set timezone in the 'timezone_string' wp option)
37
	 */
38
	protected function __construct( $timezone ) {
39
		$this->singular_item = __('Datetime','event_espresso');
40
		$this->plural_item = __('Datetimes','event_espresso');
41
42
		$this->_tables = array(
0 ignored issues
show
Documentation Bug introduced by
It seems like array('Datetime' => new ...p_datetime', 'DTT_ID')) of type array<string,object<EE_P...ct<EE_Primary_Table>"}> is incompatible with the declared type array<integer,object<EE_Table_Base>> of property $_tables.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
43
			'Datetime'=> new EE_Primary_Table('esp_datetime', 'DTT_ID')
44
		);
45
		$this->_fields = array(
0 ignored issues
show
Documentation Bug introduced by
It seems like array('Datetime' => arra...esso'), false, false))) of type array<string,array<strin...shed_Flag_Field>\"}>"}> is incompatible with the declared type array<integer,object<EE_Model_Field_Base>> of property $_fields.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
46
			'Datetime'=>array(
47
				'DTT_ID'=> new EE_Primary_Key_Int_Field('DTT_ID', __('Datetime ID','event_espresso')),
48
				'EVT_ID'=>new EE_Foreign_Key_Int_Field('EVT_ID', __('Event ID','event_espresso'), false, 0, 'Event'),
49
				'DTT_name' => new EE_Plain_Text_Field('DTT_name', __('Datetime Name', 'event_espresso'), false, ''),
50
				'DTT_description' => new EE_Full_HTML_Field('DTT_description', __('Description for Datetime', 'event_espresso'), false, ''),
51
				'DTT_EVT_start'=>new EE_Datetime_Field('DTT_EVT_start', __('Start time/date of Event','event_espresso'), false, time(), $timezone ),
52
				'DTT_EVT_end'=>new EE_Datetime_Field('DTT_EVT_end', __('End time/date of Event','event_espresso'), false, time(), $timezone ),
53
				'DTT_reg_limit'=>new EE_Infinite_Integer_Field('DTT_reg_limit', __('Registration Limit for this time','event_espresso'), true, EE_INF),
54
				'DTT_sold'=>new EE_Integer_Field('DTT_sold', __('How many sales for this Datetime that have occurred', 'event_espresso'), true, 0 ),
55
				'DTT_is_primary'=>new EE_Boolean_Field('DTT_is_primary', __("Flag indicating datetime is primary one for event", "event_espresso"), false,false),
56
				'DTT_order' => new EE_Integer_Field('DTT_order', __('The order in which the Datetime is displayed', 'event_espresso'), false, 0),
57
				'DTT_parent' => new EE_Integer_Field('DTT_parent', __('Indicates what DTT_ID is the parent of this DTT_ID'), true, 0 ),
58
				'DTT_deleted' => new EE_Trashed_Flag_Field('DTT_deleted', __('Flag indicating datetime is archived', 'event_espresso'), false, false ),
59
			));
60
		$this->_model_relations = array(
0 ignored issues
show
Documentation Bug introduced by
It seems like array('Ticket' => new \E...EE_Has_Many_Relation()) of type array<string,object<EE_H...E_Has_Many_Relation>"}> is incompatible with the declared type array<integer,object<EE_Model_Relation_Base>> of property $_model_relations.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
61
			'Ticket'=>new EE_HABTM_Relation('Datetime_Ticket'),
62
			'Event'=>new EE_Belongs_To_Relation(),
63
			'Checkin'=>new EE_Has_Many_Relation(),
64
		);
65
		$this->_model_chain_to_wp_user = 'Event';
66
		//this model is generally available for reading
67
		$this->_cap_restriction_generators[ EEM_Base::caps_read ] = new EE_Restriction_Generator_Event_Related_Public( 'Event' );
68
		$this->_cap_restriction_generators[ EEM_Base::caps_read_admin ] = new EE_Restriction_Generator_Event_Related_Protected( 'Event' );
69
		$this->_cap_restriction_generators[ EEM_Base::caps_edit ] = new EE_Restriction_Generator_Event_Related_Protected( 'Event' );
70
		$this->_cap_restriction_generators[ EEM_Base::caps_delete ] = new EE_Restriction_Generator_Event_Related_Protected( 'Event', EEM_Base::caps_edit );
71
		parent::__construct( $timezone );
72
	}
73
74
75
76
77
	/**
78
	*		create new blank datetime
79
	*
80
	* 		@access		public
81
	*		@return 		EE_Datetime[]		array on success, FALSE on fail
82
	*/
83
	public function create_new_blank_datetime() {
84
		$blank_datetime = EE_Datetime::new_instance(
85
			array(
86
				'DTT_EVT_start' => $this->current_time_for_query( 'DTT_EVT_start', true ) + (60 * 60 * 24 * 30),
87
				'DTT_EVT_end' => $this->current_time_for_query( 'DTT_EVT_end', true ) + (60 * 60 * 24 * 30),
88
				'DTT_order' => 1,
89
				'DTT_reg_limit' => EE_INF
90
			),
91
			$this->_timezone
92
		);
93
		$blank_datetime->set_start_time( $this->convert_datetime_for_query( 'DTT_EVT_start', '8am', 'ga', $this->_timezone ) );
0 ignored issues
show
Documentation introduced by
$this->convert_datetime_...'ga', $this->_timezone) is of type object<DateTime>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
94
		$blank_datetime->set_end_time( $this->convert_datetime_for_query( 'DTT_EVT_end', '5pm', 'ga', $this->_timezone ) );
0 ignored issues
show
Documentation introduced by
$this->convert_datetime_...'ga', $this->_timezone) is of type object<DateTime>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
95
		return array( $blank_datetime );
96
	}
97
98
99
100
101
102
	/**
103
	*		get event start date from db
104
	*
105
	* 		@access		public
106
	* 		@param		int 			$EVT_ID
107
	*		@return 		EE_Datetime[]		array on success, FALSE on fail
108
	*/
109
	public function get_all_event_dates( $EVT_ID = 0 ) {
110
		if ( ! $EVT_ID ) { // on add_new_event event_id gets set to 0
111
			return $this->create_new_blank_datetime();
112
		}
113
		$results =  $this->get_datetimes_for_event_ordered_by_DTT_order($EVT_ID);
114
115
		if ( empty( $results ) ) {
116
			return $this->create_new_blank_datetime();
117
		}
118
119
		return $results;
120
	}
121
122
123
124
125
126
	/**
127
	 * get all datetimes attached to an event ordered by the DTT_order field
128
	 * @public
129
	 * @param  int   			$EVT_ID event id
130
	 * @param boolean 			$include_expired
131
	 * @param boolean 			$include_deleted
132
	 * @param  int 				$limit If included then limit the count of results by
133
	 *                        	the given number
134
	 * @return EE_Datetime[]
135
	 */
136
	public function get_datetimes_for_event_ordered_by_DTT_order( $EVT_ID, $include_expired = TRUE, $include_deleted= TRUE, $limit = NULL  ) {
137
138
		//sanitize EVT_ID
139
		$EVT_ID = intval( $EVT_ID );
140
141
		$old_assumption = $this->get_assumption_concerning_values_already_prepared_by_model_object();
142
		$this->assume_values_already_prepared_by_model_object( EEM_Base::prepared_for_use_in_db );
143
		$where_params = array( 'Event.EVT_ID' => $EVT_ID );
144
145
		$query_params = ! empty( $limit ) ? array( $where_params, 'limit' => $limit, 'order_by' => array( 'DTT_order' => 'ASC' ), 'default_where_conditions' => 'none' ) : array( $where_params, 'order_by' => array( 'DTT_order' => 'ASC' ), 'default_where_conditions' => 'none' );
146
147
		if( ! $include_expired){
148
			$query_params[0]['DTT_EVT_end'] = array( '>=', current_time( 'mysql', TRUE ) );
149
		}
150
		if( $include_deleted){
151
			$query_params[0]['DTT_deleted'] = array( 'IN', array( TRUE, FALSE ));
152
		}
153
154
		$result = $this->get_all( $query_params );
155
		$this->assume_values_already_prepared_by_model_object( $old_assumption );
0 ignored issues
show
Documentation introduced by
$old_assumption is of type boolean, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
156
		return $result;
157
	}
158
159
160
161
162
	/**
163
	 * Gets the datetimes for the event (with the given limit), and orders them by "importance". By importance, we mean
164
	 * that the primary datetimes are most important (DEPRECATED FOR NOW), and then the earlier datetimes are the most important. Maybe we'll want
165
	 * this to take into account datetimes that haven't already passed, but we don't yet.
166
	 * @param int $EVT_ID
167
	 * @param int $limit
168
	 * @return EE_Datetime[]
169
	 */
170
	public function get_datetimes_for_event_ordered_by_importance( $EVT_ID = 0, $limit = NULL){
171
		return $this->get_all( array(array('Event.EVT_ID'=>$EVT_ID),
172
			'limit'=>$limit,
173
			'order_by'=>array('DTT_EVT_start'=>'ASC'),
174
			'default_where_conditions' => 'none'));
175
	}
176
177
178
179
	/**
180
	 *
181
	 * @param int $EVT_ID
182
	 * @param boolean $include_expired
183
	 * @param boolean $include_deleted
184
	 * @return EE_Datetime
185
	 */
186
	public function get_oldest_datetime_for_event($EVT_ID, $include_expired = false,$include_deleted = false){
187
		$results =  $this->get_datetimes_for_event_ordered_by_start_time($EVT_ID, $include_expired, $include_deleted, 1);
188
		if($results){
0 ignored issues
show
Bug Best Practice introduced by
The expression $results of type EE_Base_Class[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
189
			return array_shift($results);
190
		}else{
191
			return NULL;
192
		}
193
	}
194
195
196
197
	/**
198
	 * Gets the 'primary' datetime for an event.
199
	 * @param int  $EVT_ID
200
	 * @param bool $try_to_exclude_expired
201
	 * @param bool $try_to_exclude_deleted
202
	 * @return \EE_Datetime
203
	 */
204
	public function get_primary_datetime_for_event($EVT_ID,$try_to_exclude_expired = true, $try_to_exclude_deleted = true){
205
		if($try_to_exclude_expired){
206
			$non_expired = $this->get_oldest_datetime_for_event($EVT_ID, false,false);
207
			if($non_expired){
208
				return $non_expired;
209
			}
210
		}
211
		if($try_to_exclude_deleted){
212
			$expired_even = $this->get_oldest_datetime_for_event($EVT_ID, true);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $expired_even is correct as $this->get_oldest_dateti...or_event($EVT_ID, true) (which targets EEM_Datetime::get_oldest_datetime_for_event()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
213
			if($expired_even){
214
				return $expired_even;
215
			}
216
		}
217
		$deleted_even = $this->get_oldest_datetime_for_event($EVT_ID, true, true);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $deleted_even is correct as $this->get_oldest_dateti...nt($EVT_ID, true, true) (which targets EEM_Datetime::get_oldest_datetime_for_event()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
218
		return $deleted_even;
219
	}
220
221
222
223
	/**
224
	 * Gets ALL the datetimes for an event (including trashed ones, for now), ordered
225
	 * only by start date
226
	 * @param int $EVT_ID
227
	 * @param boolean $include_expired
228
	 * @param boolean $include_deleted
229
	 * @param int $limit
230
	 * @return EE_Datetime[]
231
	 */
232 View Code Duplication
	public function get_datetimes_for_event_ordered_by_start_time($EVT_ID, $include_expired = true, $include_deleted= true, $limit = NULL ){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
233
		//sanitize EVT_ID
234
		$EVT_ID = intval( $EVT_ID );
235
		$old_assumption = $this->get_assumption_concerning_values_already_prepared_by_model_object();
236
		$this->assume_values_already_prepared_by_model_object( EEM_Base::prepared_for_use_in_db );
237
		$query_params =array(array('Event.EVT_ID'=>$EVT_ID),'order_by'=>array('DTT_EVT_start'=>'asc'));
238
		if( ! $include_expired){
239
			$query_params[0]['DTT_EVT_end'] = array('>=',current_time('mysql', TRUE));
240
		}
241
		if( $include_deleted){
242
			$query_params[0]['DTT_deleted'] = array('IN',array(true,false));
243
		}
244
		if($limit){
0 ignored issues
show
Bug Best Practice introduced by
The expression $limit of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
245
			$query_params['limit'] = $limit;
246
		}
247
		$result = $this->get_all( $query_params );
248
		$this->assume_values_already_prepared_by_model_object( $old_assumption );
0 ignored issues
show
Documentation introduced by
$old_assumption is of type boolean, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
249
		return $result;
250
	}
251
252
		/**
253
	 * Gets ALL the datetimes for an ticket (including trashed ones, for now), ordered
254
	 * only by start date
255
	 * @param int $TKT_ID
256
	 * @param boolean $include_expired
257
	 * @param boolean $include_deleted
258
	 * @param int $limit
259
	 * @return EE_Datetime[]
260
	 */
261 View Code Duplication
	public function get_datetimes_for_ticket_ordered_by_start_time($TKT_ID, $include_expired = true, $include_deleted= true, $limit = NULL){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
262
		//sanitize TKT_ID
263
		$TKT_ID =  intval( $TKT_ID );
264
		$old_assumption = $this->get_assumption_concerning_values_already_prepared_by_model_object();
265
		$this->assume_values_already_prepared_by_model_object( EEM_Base::prepared_for_use_in_db );
266
		$query_params =array(array('Ticket.TKT_ID'=>$TKT_ID),'order_by'=>array('DTT_EVT_start'=>'asc'));
267
		if( ! $include_expired){
268
			$query_params[0]['DTT_EVT_end'] = array('>=',current_time('mysql', TRUE));
269
		}
270
		if( $include_deleted){
271
			$query_params[0]['DTT_deleted'] = array('IN',array(true,false));
272
		}
273
		if($limit){
0 ignored issues
show
Bug Best Practice introduced by
The expression $limit of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
274
			$query_params['limit'] = $limit;
275
		}
276
		$result = $this->get_all( $query_params );
277
		$this->assume_values_already_prepared_by_model_object( $old_assumption );
0 ignored issues
show
Documentation introduced by
$old_assumption is of type boolean, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
278
		return $result;
279
	}
280
281
282
283
284
	/**
285
	 * Gets all the datetimes for a ticket (including trashed ones, for now), ordered by the DTT_order for the datetimes.
286
	 * @param  int     $TKT_ID          ID of ticket to retrieve the datetimes for
287
	 * @param  boolean $include_expired whether to include expired datetimes or not
288
	 * @param  boolean $include_deleted whether to include trashed datetimes or not.
289
	 * @param  int|null  $limit         if null, no limit, if int then limit results by
290
	 *                                  that number
291
	 * @return EE_Datetime[]
292
	 */
293 View Code Duplication
	public function get_datetimes_for_ticket_ordered_by_DTT_order( $TKT_ID, $include_expired = true, $include_deleted = true, $limit = NULL ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
294
		//sanitize id.
295
		$TKT_ID =  intval( $TKT_ID );
296
		$old_assumption = $this->get_assumption_concerning_values_already_prepared_by_model_object();
297
		$this->assume_values_already_prepared_by_model_object( EEM_Base::prepared_for_use_in_db );
298
		$where_params = array( 'Ticket.TKT_ID' => $TKT_ID );
299
		$query_params = array( $where_params, 'order_by' => array( 'DTT_order' => 'ASC' ) );
300
		if( ! $include_expired){
301
			$query_params[0]['DTT_EVT_end'] = array('>=',current_time('mysql', TRUE));
302
		}
303
		if( $include_deleted){
304
			$query_params[0]['DTT_deleted'] = array('IN',array(true,false));
305
		}
306
		if($limit){
0 ignored issues
show
Bug Best Practice introduced by
The expression $limit of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
307
			$query_params['limit'] = $limit;
308
		}
309
		$result = $this->get_all( $query_params );
310
		$this->assume_values_already_prepared_by_model_object( $old_assumption );
0 ignored issues
show
Documentation introduced by
$old_assumption is of type boolean, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
311
		return $result;
312
	}
313
314
315
	/**
316
	 * Gets the most important datetime for a particular event (ie, the primary event usually. But if for some WACK
317
	 * reason it doesn't exist, we consider the earliest event the most important)
318
	 * @param int $EVT_ID
319
	 * @return EE_Datetime
320
	 */
321
	public function get_most_important_datetime_for_event($EVT_ID){
322
		$results = $this->get_datetimes_for_event_ordered_by_importance($EVT_ID, 1);
323
		if($results){
0 ignored issues
show
Bug Best Practice introduced by
The expression $results of type EE_Base_Class[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
324
			return array_shift($results);
325
		}else{
326
			return null;
327
		}
328
	}
329
330
331
332
	/**
333
	 * This returns a wpdb->results 		Array of all DTT month and years matching the incoming query params and grouped by month and year.
334
	 * @param  array $where_params 		Array of query_params as described in the comments for EEM_Base::get_all()
335
	 * @param  string $evt_active_status 		A string representing the evt active status to filter the months by.
336
	 * 		Can be:
337
	 * 			- '' = no filter
338
	 * 			- upcoming = Published events with at least one upcoming datetime.
339
	 * 			- expired = Events with all datetimes expired.
340
	 * 			- active = Events that are published and have at least one datetime that starts before now and ends after now.
341
	 * 			- inactive = Events that are either not published.
342
	 * @return wpdb results array
343
	 */
344
	public function get_dtt_months_and_years( $where_params, $evt_active_status = '' ) {
345
346
		switch ( $evt_active_status ) {
347
			case 'upcoming' :
348
					$where_params['Event.status'] = 'publish';
349
					//if there are already query_params matching DTT_EVT_start then we need to modify that to add them.
350
					if ( isset( $where_params['DTT_EVT_start'] ) ) {
351
						$where_params['DTT_EVT_start*****'] = $where_params['DTT_EVT_start'];
352
					}
353
					$where_params['DTT_EVT_start'] = array('>', $this->current_time_for_query( 'DTT_EVT_start' ) );
354
					break;
355
356
			case 'expired' :
357
				if ( isset( $where_params['Event.status'] ) ) unset( $where_params['Event.status'] );
358
				//get events to exclude
359
				$exclude_query[0] = array_merge( $where_params, array( 'DTT_EVT_end' => array( '>', $this->current_time_for_query( 'DTT_EVT_end' ) ) ) );
0 ignored issues
show
Coding Style Comprehensibility introduced by
$exclude_query was never initialized. Although not strictly required by PHP, it is generally a good practice to add $exclude_query = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
360
				//first get all events that have datetimes where its not expired.
361
				$event_ids = $this->_get_all_wpdb_results( $exclude_query, OBJECT_K, 'Datetime.EVT_ID' );
362
				$event_ids = array_keys( $event_ids );
363
364
				if ( isset( $where_params['DTT_EVT_end'] ) ) {
365
					$where_params['DTT_EVT_end****'] = $where_params['DTT_EVT_end'];
366
				}
367
				$where_params['DTT_EVT_end'] = array( '<', EEM_Datetime::instance()->current_time_for_query( 'DTT_EVT_end' ) );
368
				$where_params['Event.EVT_ID'] = array( 'NOT IN', $event_ids );
369
				break;
370
371
			case 'active' :
372
				$where_params['Event.status'] = 'publish';
373
				if ( isset( $where_params['DTT_EVT_start'] ) ) {
374
					$where_params['Datetime.DTT_EVT_start******'] = $where_params['DTT_EVT_start'];
375
				}
376
				if ( isset( $where_params['Datetime.DTT_EVT_end'] ) ) {
377
					$where_params['Datetime.DTT_EVT_end*****'] = $where_params['DTT_EVT_end'];
378
				}
379
				$where_params['DTT_EVT_start'] = array('<',  $this->current_time_for_query( 'DTT_EVT_start' ) );
380
				$where_params['DTT_EVT_end'] = array('>', $this->current_time_for_query( 'DTT_EVT_end' ) );
381
				break;
382
383
			case 'inactive' :
384
				if ( isset( $where_params['Event.status'] ) ) unset( $where_params['Event.status'] );
385
				if ( isset( $where_params['OR'] ) ) {
386
					$where_params['AND']['OR'] = $where_params['OR'];
387
				}
388
				if ( isset( $where_params['DTT_EVT_end'] ) ) {
389
					$where_params['AND']['DTT_EVT_end****'] = $where_params['DTT_EVT_end'];
390
					unset( $where_params['DTT_EVT_end'] );
391
				}
392
393
				if ( isset( $where_params['DTT_EVT_start'] ) ) {
394
					$where_params['AND']['DTT_EVT_start'] = $where_params['DTT_EVT_start'];
395
					unset( $where_params['DTT_EVT_start'] );
396
				}
397
				$where_params['AND']['Event.status'] = array( '!=', 'publish' );
398
				break;
399
		}
400
401
		$query_params[0] = $where_params;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$query_params was never initialized. Although not strictly required by PHP, it is generally a good practice to add $query_params = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
402
		$query_params['group_by'] = array('dtt_year', 'dtt_month');
403
		$query_params['order_by'] = array( 'DTT_EVT_start' => 'DESC' );
404
		$columns_to_select = array(
405
			'dtt_year' => array('YEAR(DTT_EVT_start)', '%s'),
406
			'dtt_month' => array('MONTHNAME(DTT_EVT_start)', '%s')
407
			);
408
		return $this->_get_all_wpdb_results( $query_params, OBJECT, $columns_to_select );
409
	}
410
411
	/**
412
	 * Updates the DTT_sold attribute on each datetime (based on the registrations
413
	 * for the tickets for each datetime)
414
	 * @param EE_Datetime[] $datetimes
415
	 */
416
	public function update_sold($datetimes){
417
		foreach($datetimes as $datetime){
418
			$datetime->update_sold();
419
		}
420
	}
421
422
423
424
	/**
425
	 * 	Gets the total number of tickets available at a particular datetime
426
	 * 	(does NOT take into account the datetime's spaces available)
427
	 *
428
	 * @param int   $DTT_ID
429
	 * @param array $query_params
430
	 * @return int of tickets available. If sold out, return less than 1. If infinite, returns EE_INF,  IF there are NO tickets attached to datetime then FALSE is returned.
431
	 */
432
	public function sum_tickets_currently_available_at_datetime( $DTT_ID, $query_params = array() ) {
433
		$datetime = $this->get_one_by_ID( $DTT_ID );
434
		if ( $datetime instanceof EE_Datetime ) {
435
			return $datetime->tickets_remaining( $query_params );
436
		}
437
		return 0;
438
	}
439
440
441
442
443
	/**
444
	 * This returns an array of counts of datetimes in the database for each Datetime status that can be queried.
445
	 *
446
	 * @param  array $stati_to_include        If included you can restrict the statuses we return counts for by including the stati
447
	 *                               you want counts for as values in the array.  An empty array returns counts for all valid
448
	 *                               stati.
449
	 * @param  array  $query_params  If included can be used to refine the conditions for returning the count (i.e. only for
450
	 *                               Datetimes connected to a specific event, or specific ticket.
451
	 *
452
	 * @return array  The value returned is an array indexed by Datetime Status and the values are the counts.  The stati used as index keys are:
453
	 *                EE_Datetime::active
454
	 *                EE_Datetime::upcoming
455
	 *                EE_Datetime::expired
456
	 */
457
	public function get_datetime_counts_by_status( $stati_to_include = array(), $query_params = array() ) {
458
		//only accept where conditions for this query.
459
		$_where = isset( $query_params[0] ) ? $query_params[0] : array();
460
		$status_query_args = array(
461
				EE_Datetime::active => array_merge(
462
						$_where,
463
						array( 'DTT_EVT_start' => array( '<', time() ), 'DTT_EVT_end' => array( '>', time() ) )
464
				),
465
				EE_Datetime::upcoming => array_merge(
466
						$_where,
467
						array( 'DTT_EVT_start' => array( '>', time() ) )
468
				),
469
				EE_Datetime::expired => array_merge(
470
						$_where,
471
						array( 'DTT_EVT_end' => array('<', time() ) )
472
				)
473
		);
474
475
		if ( ! empty( $stati_to_include ) ) {
476
			foreach( array_keys( $status_query_args ) as $status ) {
477
				if ( ! in_array( $status, $stati_to_include ) ) {
478
					unset( $status_query_args[$status] );
479
				}
480
			}
481
		}
482
483
		//loop through and query counts for each stati.
484
		$status_query_results = array();
485
		foreach( $status_query_args as $status => $status_where_conditions ) {
486
			$status_query_results[ $status ] = EEM_Datetime::count( array( $status_where_conditions ), 'DTT_ID', true );
487
		}
488
489
		return $status_query_results;
490
	}
491
492
493
	/**
494
	 * Returns the specific count for a given Datetime status matching any given query_params.
495
	 *
496
	 * @param string $status  Valid string representation for Datetime status requested. (Defaults to Active).
497
	 * @param array $query_params
498
	 * @return int
499
	 */
500
	public function get_datetime_count_for_status( $status = EE_Datetime::active, $query_params = array() ) {
501
		$count = $this->get_datetime_counts_by_status( array( $status ), $query_params );
502
		return ! empty( $count[$status] ) ? $count[$status] : 0;
503
	}
504
505
506
507
}
508
// End of file EEM_Datetime.model.php
509
// Location: /includes/models/EEM_Datetime.model.php
510