Completed
Branch BUG-10878-event-spaces-remaini... (17f80a)
by
unknown
13:41
created
espresso.php 1 patch
Indentation   +219 added lines, -219 removed lines patch added patch discarded remove patch
@@ -1,5 +1,5 @@  discard block
 block discarded – undo
1 1
 <?php if ( ! defined('ABSPATH')) {
2
-    exit('No direct script access allowed');
2
+	exit('No direct script access allowed');
3 3
 }
4 4
 /*
5 5
   Plugin Name:		Event Espresso
@@ -40,243 +40,243 @@  discard block
 block discarded – undo
40 40
  * @since            4.0
41 41
  */
42 42
 if (function_exists('espresso_version')) {
43
-    /**
44
-     *    espresso_duplicate_plugin_error
45
-     *    displays if more than one version of EE is activated at the same time
46
-     */
47
-    function espresso_duplicate_plugin_error()
48
-    {
49
-        ?>
43
+	/**
44
+	 *    espresso_duplicate_plugin_error
45
+	 *    displays if more than one version of EE is activated at the same time
46
+	 */
47
+	function espresso_duplicate_plugin_error()
48
+	{
49
+		?>
50 50
         <div class="error">
51 51
             <p>
52 52
                 <?php echo esc_html__(
53
-                        'Can not run multiple versions of Event Espresso! One version has been automatically deactivated. Please verify that you have the correct version you want still active.',
54
-                        'event_espresso'
55
-                ); ?>
53
+						'Can not run multiple versions of Event Espresso! One version has been automatically deactivated. Please verify that you have the correct version you want still active.',
54
+						'event_espresso'
55
+				); ?>
56 56
             </p>
57 57
         </div>
58 58
         <?php
59
-        espresso_deactivate_plugin(plugin_basename(__FILE__));
60
-    }
59
+		espresso_deactivate_plugin(plugin_basename(__FILE__));
60
+	}
61 61
 
62
-    add_action('admin_notices', 'espresso_duplicate_plugin_error', 1);
62
+	add_action('admin_notices', 'espresso_duplicate_plugin_error', 1);
63 63
 } else {
64
-    define('EE_MIN_PHP_VER_REQUIRED', '5.3.9');
65
-    if ( ! version_compare(PHP_VERSION, EE_MIN_PHP_VER_REQUIRED, '>=')) {
66
-        /**
67
-         * espresso_minimum_php_version_error
68
-         *
69
-         * @return void
70
-         */
71
-        function espresso_minimum_php_version_error()
72
-        {
73
-            ?>
64
+	define('EE_MIN_PHP_VER_REQUIRED', '5.3.9');
65
+	if ( ! version_compare(PHP_VERSION, EE_MIN_PHP_VER_REQUIRED, '>=')) {
66
+		/**
67
+		 * espresso_minimum_php_version_error
68
+		 *
69
+		 * @return void
70
+		 */
71
+		function espresso_minimum_php_version_error()
72
+		{
73
+			?>
74 74
             <div class="error">
75 75
                 <p>
76 76
                     <?php
77
-                    printf(
78
-                            esc_html__(
79
-                                    'We\'re sorry, but Event Espresso requires PHP version %1$s or greater in order to operate. You are currently running version %2$s.%3$sIn order to update your version of PHP, you will need to contact your current hosting provider.%3$sFor information on stable PHP versions, please go to %4$s.',
80
-                                    'event_espresso'
81
-                            ),
82
-                            EE_MIN_PHP_VER_REQUIRED,
83
-                            PHP_VERSION,
84
-                            '<br/>',
85
-                            '<a href="http://php.net/downloads.php">http://php.net/downloads.php</a>'
86
-                    );
87
-                    ?>
77
+					printf(
78
+							esc_html__(
79
+									'We\'re sorry, but Event Espresso requires PHP version %1$s or greater in order to operate. You are currently running version %2$s.%3$sIn order to update your version of PHP, you will need to contact your current hosting provider.%3$sFor information on stable PHP versions, please go to %4$s.',
80
+									'event_espresso'
81
+							),
82
+							EE_MIN_PHP_VER_REQUIRED,
83
+							PHP_VERSION,
84
+							'<br/>',
85
+							'<a href="http://php.net/downloads.php">http://php.net/downloads.php</a>'
86
+					);
87
+					?>
88 88
                 </p>
89 89
             </div>
90 90
             <?php
91
-            espresso_deactivate_plugin(plugin_basename(__FILE__));
92
-        }
91
+			espresso_deactivate_plugin(plugin_basename(__FILE__));
92
+		}
93 93
 
94
-        add_action('admin_notices', 'espresso_minimum_php_version_error', 1);
95
-    } else {
96
-        /**
97
-         * espresso_version
98
-         * Returns the plugin version
99
-         *
100
-         * @return string
101
-         */
102
-        function espresso_version()
103
-        {
104
-            return apply_filters('FHEE__espresso__espresso_version', '4.9.45.rc.009');
105
-        }
94
+		add_action('admin_notices', 'espresso_minimum_php_version_error', 1);
95
+	} else {
96
+		/**
97
+		 * espresso_version
98
+		 * Returns the plugin version
99
+		 *
100
+		 * @return string
101
+		 */
102
+		function espresso_version()
103
+		{
104
+			return apply_filters('FHEE__espresso__espresso_version', '4.9.45.rc.009');
105
+		}
106 106
 
107
-        // define versions
108
-        define('EVENT_ESPRESSO_VERSION', espresso_version());
109
-        define('EE_MIN_WP_VER_REQUIRED', '4.1');
110
-        define('EE_MIN_WP_VER_RECOMMENDED', '4.4.2');
111
-        define('EE_MIN_PHP_VER_RECOMMENDED', '5.4.44');
112
-        define('EVENT_ESPRESSO_MAIN_FILE', __FILE__);
113
-        //used to be DIRECTORY_SEPARATOR, but that caused issues on windows
114
-        if ( ! defined('DS')) {
115
-            define('DS', '/');
116
-        }
117
-        if ( ! defined('PS')) {
118
-            define('PS', PATH_SEPARATOR);
119
-        }
120
-        if ( ! defined('SP')) {
121
-            define('SP', ' ');
122
-        }
123
-        if ( ! defined('EENL')) {
124
-            define('EENL', "\n");
125
-        }
126
-        define('EE_SUPPORT_EMAIL', '[email protected]');
127
-        // define the plugin directory and URL
128
-        define('EE_PLUGIN_BASENAME', plugin_basename(EVENT_ESPRESSO_MAIN_FILE));
129
-        define('EE_PLUGIN_DIR_PATH', plugin_dir_path(EVENT_ESPRESSO_MAIN_FILE));
130
-        define('EE_PLUGIN_DIR_URL', plugin_dir_url(EVENT_ESPRESSO_MAIN_FILE));
131
-        // main root folder paths
132
-        define('EE_ADMIN_PAGES', EE_PLUGIN_DIR_PATH . 'admin_pages' . DS);
133
-        define('EE_CORE', EE_PLUGIN_DIR_PATH . 'core' . DS);
134
-        define('EE_MODULES', EE_PLUGIN_DIR_PATH . 'modules' . DS);
135
-        define('EE_PUBLIC', EE_PLUGIN_DIR_PATH . 'public' . DS);
136
-        define('EE_SHORTCODES', EE_PLUGIN_DIR_PATH . 'shortcodes' . DS);
137
-        define('EE_WIDGETS', EE_PLUGIN_DIR_PATH . 'widgets' . DS);
138
-        define('EE_PAYMENT_METHODS', EE_PLUGIN_DIR_PATH . 'payment_methods' . DS);
139
-        define('EE_CAFF_PATH', EE_PLUGIN_DIR_PATH . 'caffeinated' . DS);
140
-        // core system paths
141
-        define('EE_ADMIN', EE_CORE . 'admin' . DS);
142
-        define('EE_CPTS', EE_CORE . 'CPTs' . DS);
143
-        define('EE_CLASSES', EE_CORE . 'db_classes' . DS);
144
-        define('EE_INTERFACES', EE_CORE . 'interfaces' . DS);
145
-        define('EE_BUSINESS', EE_CORE . 'business' . DS);
146
-        define('EE_MODELS', EE_CORE . 'db_models' . DS);
147
-        define('EE_HELPERS', EE_CORE . 'helpers' . DS);
148
-        define('EE_LIBRARIES', EE_CORE . 'libraries' . DS);
149
-        define('EE_TEMPLATES', EE_CORE . 'templates' . DS);
150
-        define('EE_THIRD_PARTY', EE_CORE . 'third_party_libs' . DS);
151
-        define('EE_GLOBAL_ASSETS', EE_TEMPLATES . 'global_assets' . DS);
152
-        define('EE_FORM_SECTIONS', EE_LIBRARIES . 'form_sections' . DS);
153
-        // gateways
154
-        define('EE_GATEWAYS', EE_MODULES . 'gateways' . DS);
155
-        define('EE_GATEWAYS_URL', EE_PLUGIN_DIR_URL . 'modules' . DS . 'gateways' . DS);
156
-        // asset URL paths
157
-        define('EE_TEMPLATES_URL', EE_PLUGIN_DIR_URL . 'core' . DS . 'templates' . DS);
158
-        define('EE_GLOBAL_ASSETS_URL', EE_TEMPLATES_URL . 'global_assets' . DS);
159
-        define('EE_IMAGES_URL', EE_GLOBAL_ASSETS_URL . 'images' . DS);
160
-        define('EE_THIRD_PARTY_URL', EE_PLUGIN_DIR_URL . 'core' . DS . 'third_party_libs' . DS);
161
-        define('EE_HELPERS_ASSETS', EE_PLUGIN_DIR_URL . 'core/helpers/assets/');
162
-        define('EE_LIBRARIES_URL', EE_PLUGIN_DIR_URL . 'core/libraries/');
163
-        // define upload paths
164
-        $uploads = wp_upload_dir();
165
-        // define the uploads directory and URL
166
-        define('EVENT_ESPRESSO_UPLOAD_DIR', $uploads['basedir'] . DS . 'espresso' . DS);
167
-        define('EVENT_ESPRESSO_UPLOAD_URL', $uploads['baseurl'] . DS . 'espresso' . DS);
168
-        // define the templates directory and URL
169
-        define('EVENT_ESPRESSO_TEMPLATE_DIR', $uploads['basedir'] . DS . 'espresso' . DS . 'templates' . DS);
170
-        define('EVENT_ESPRESSO_TEMPLATE_URL', $uploads['baseurl'] . DS . 'espresso' . DS . 'templates' . DS);
171
-        // define the gateway directory and URL
172
-        define('EVENT_ESPRESSO_GATEWAY_DIR', $uploads['basedir'] . DS . 'espresso' . DS . 'gateways' . DS);
173
-        define('EVENT_ESPRESSO_GATEWAY_URL', $uploads['baseurl'] . DS . 'espresso' . DS . 'gateways' . DS);
174
-        // languages folder/path
175
-        define('EE_LANGUAGES_SAFE_LOC', '..' . DS . 'uploads' . DS . 'espresso' . DS . 'languages' . DS);
176
-        define('EE_LANGUAGES_SAFE_DIR', EVENT_ESPRESSO_UPLOAD_DIR . 'languages' . DS);
177
-        //check for dompdf fonts in uploads
178
-        if (file_exists(EVENT_ESPRESSO_UPLOAD_DIR . 'fonts' . DS)) {
179
-            define('DOMPDF_FONT_DIR', EVENT_ESPRESSO_UPLOAD_DIR . 'fonts' . DS);
180
-        }
181
-        //ajax constants
182
-        define(
183
-                'EE_FRONT_AJAX',
184
-                isset($_REQUEST['ee_front_ajax']) || isset($_REQUEST['data']['ee_front_ajax']) ? true : false
185
-        );
186
-        define(
187
-                'EE_ADMIN_AJAX',
188
-                isset($_REQUEST['ee_admin_ajax']) || isset($_REQUEST['data']['ee_admin_ajax']) ? true : false
189
-        );
190
-        //just a handy constant occasionally needed for finding values representing infinity in the DB
191
-        //you're better to use this than its straight value (currently -1) in case you ever
192
-        //want to change its default value! or find when -1 means infinity
193
-        define('EE_INF_IN_DB', -1);
194
-        define('EE_INF', INF > (float)PHP_INT_MAX ? INF : PHP_INT_MAX);
195
-        define('EE_DEBUG', false);
196
-        // for older WP versions
197
-        if ( ! defined('MONTH_IN_SECONDS')) {
198
-            define('MONTH_IN_SECONDS', DAY_IN_SECONDS * 30);
199
-        }
200
-        /**
201
-         *    espresso_plugin_activation
202
-         *    adds a wp-option to indicate that EE has been activated via the WP admin plugins page
203
-         */
204
-        function espresso_plugin_activation()
205
-        {
206
-            update_option('ee_espresso_activation', true);
207
-        }
107
+		// define versions
108
+		define('EVENT_ESPRESSO_VERSION', espresso_version());
109
+		define('EE_MIN_WP_VER_REQUIRED', '4.1');
110
+		define('EE_MIN_WP_VER_RECOMMENDED', '4.4.2');
111
+		define('EE_MIN_PHP_VER_RECOMMENDED', '5.4.44');
112
+		define('EVENT_ESPRESSO_MAIN_FILE', __FILE__);
113
+		//used to be DIRECTORY_SEPARATOR, but that caused issues on windows
114
+		if ( ! defined('DS')) {
115
+			define('DS', '/');
116
+		}
117
+		if ( ! defined('PS')) {
118
+			define('PS', PATH_SEPARATOR);
119
+		}
120
+		if ( ! defined('SP')) {
121
+			define('SP', ' ');
122
+		}
123
+		if ( ! defined('EENL')) {
124
+			define('EENL', "\n");
125
+		}
126
+		define('EE_SUPPORT_EMAIL', '[email protected]');
127
+		// define the plugin directory and URL
128
+		define('EE_PLUGIN_BASENAME', plugin_basename(EVENT_ESPRESSO_MAIN_FILE));
129
+		define('EE_PLUGIN_DIR_PATH', plugin_dir_path(EVENT_ESPRESSO_MAIN_FILE));
130
+		define('EE_PLUGIN_DIR_URL', plugin_dir_url(EVENT_ESPRESSO_MAIN_FILE));
131
+		// main root folder paths
132
+		define('EE_ADMIN_PAGES', EE_PLUGIN_DIR_PATH . 'admin_pages' . DS);
133
+		define('EE_CORE', EE_PLUGIN_DIR_PATH . 'core' . DS);
134
+		define('EE_MODULES', EE_PLUGIN_DIR_PATH . 'modules' . DS);
135
+		define('EE_PUBLIC', EE_PLUGIN_DIR_PATH . 'public' . DS);
136
+		define('EE_SHORTCODES', EE_PLUGIN_DIR_PATH . 'shortcodes' . DS);
137
+		define('EE_WIDGETS', EE_PLUGIN_DIR_PATH . 'widgets' . DS);
138
+		define('EE_PAYMENT_METHODS', EE_PLUGIN_DIR_PATH . 'payment_methods' . DS);
139
+		define('EE_CAFF_PATH', EE_PLUGIN_DIR_PATH . 'caffeinated' . DS);
140
+		// core system paths
141
+		define('EE_ADMIN', EE_CORE . 'admin' . DS);
142
+		define('EE_CPTS', EE_CORE . 'CPTs' . DS);
143
+		define('EE_CLASSES', EE_CORE . 'db_classes' . DS);
144
+		define('EE_INTERFACES', EE_CORE . 'interfaces' . DS);
145
+		define('EE_BUSINESS', EE_CORE . 'business' . DS);
146
+		define('EE_MODELS', EE_CORE . 'db_models' . DS);
147
+		define('EE_HELPERS', EE_CORE . 'helpers' . DS);
148
+		define('EE_LIBRARIES', EE_CORE . 'libraries' . DS);
149
+		define('EE_TEMPLATES', EE_CORE . 'templates' . DS);
150
+		define('EE_THIRD_PARTY', EE_CORE . 'third_party_libs' . DS);
151
+		define('EE_GLOBAL_ASSETS', EE_TEMPLATES . 'global_assets' . DS);
152
+		define('EE_FORM_SECTIONS', EE_LIBRARIES . 'form_sections' . DS);
153
+		// gateways
154
+		define('EE_GATEWAYS', EE_MODULES . 'gateways' . DS);
155
+		define('EE_GATEWAYS_URL', EE_PLUGIN_DIR_URL . 'modules' . DS . 'gateways' . DS);
156
+		// asset URL paths
157
+		define('EE_TEMPLATES_URL', EE_PLUGIN_DIR_URL . 'core' . DS . 'templates' . DS);
158
+		define('EE_GLOBAL_ASSETS_URL', EE_TEMPLATES_URL . 'global_assets' . DS);
159
+		define('EE_IMAGES_URL', EE_GLOBAL_ASSETS_URL . 'images' . DS);
160
+		define('EE_THIRD_PARTY_URL', EE_PLUGIN_DIR_URL . 'core' . DS . 'third_party_libs' . DS);
161
+		define('EE_HELPERS_ASSETS', EE_PLUGIN_DIR_URL . 'core/helpers/assets/');
162
+		define('EE_LIBRARIES_URL', EE_PLUGIN_DIR_URL . 'core/libraries/');
163
+		// define upload paths
164
+		$uploads = wp_upload_dir();
165
+		// define the uploads directory and URL
166
+		define('EVENT_ESPRESSO_UPLOAD_DIR', $uploads['basedir'] . DS . 'espresso' . DS);
167
+		define('EVENT_ESPRESSO_UPLOAD_URL', $uploads['baseurl'] . DS . 'espresso' . DS);
168
+		// define the templates directory and URL
169
+		define('EVENT_ESPRESSO_TEMPLATE_DIR', $uploads['basedir'] . DS . 'espresso' . DS . 'templates' . DS);
170
+		define('EVENT_ESPRESSO_TEMPLATE_URL', $uploads['baseurl'] . DS . 'espresso' . DS . 'templates' . DS);
171
+		// define the gateway directory and URL
172
+		define('EVENT_ESPRESSO_GATEWAY_DIR', $uploads['basedir'] . DS . 'espresso' . DS . 'gateways' . DS);
173
+		define('EVENT_ESPRESSO_GATEWAY_URL', $uploads['baseurl'] . DS . 'espresso' . DS . 'gateways' . DS);
174
+		// languages folder/path
175
+		define('EE_LANGUAGES_SAFE_LOC', '..' . DS . 'uploads' . DS . 'espresso' . DS . 'languages' . DS);
176
+		define('EE_LANGUAGES_SAFE_DIR', EVENT_ESPRESSO_UPLOAD_DIR . 'languages' . DS);
177
+		//check for dompdf fonts in uploads
178
+		if (file_exists(EVENT_ESPRESSO_UPLOAD_DIR . 'fonts' . DS)) {
179
+			define('DOMPDF_FONT_DIR', EVENT_ESPRESSO_UPLOAD_DIR . 'fonts' . DS);
180
+		}
181
+		//ajax constants
182
+		define(
183
+				'EE_FRONT_AJAX',
184
+				isset($_REQUEST['ee_front_ajax']) || isset($_REQUEST['data']['ee_front_ajax']) ? true : false
185
+		);
186
+		define(
187
+				'EE_ADMIN_AJAX',
188
+				isset($_REQUEST['ee_admin_ajax']) || isset($_REQUEST['data']['ee_admin_ajax']) ? true : false
189
+		);
190
+		//just a handy constant occasionally needed for finding values representing infinity in the DB
191
+		//you're better to use this than its straight value (currently -1) in case you ever
192
+		//want to change its default value! or find when -1 means infinity
193
+		define('EE_INF_IN_DB', -1);
194
+		define('EE_INF', INF > (float)PHP_INT_MAX ? INF : PHP_INT_MAX);
195
+		define('EE_DEBUG', false);
196
+		// for older WP versions
197
+		if ( ! defined('MONTH_IN_SECONDS')) {
198
+			define('MONTH_IN_SECONDS', DAY_IN_SECONDS * 30);
199
+		}
200
+		/**
201
+		 *    espresso_plugin_activation
202
+		 *    adds a wp-option to indicate that EE has been activated via the WP admin plugins page
203
+		 */
204
+		function espresso_plugin_activation()
205
+		{
206
+			update_option('ee_espresso_activation', true);
207
+		}
208 208
 
209
-        register_activation_hook(EVENT_ESPRESSO_MAIN_FILE, 'espresso_plugin_activation');
210
-        /**
211
-         *    espresso_load_error_handling
212
-         *    this function loads EE's class for handling exceptions and errors
213
-         */
214
-        function espresso_load_error_handling()
215
-        {
216
-            // load debugging tools
217
-            if (WP_DEBUG === true && is_readable(EE_HELPERS . 'EEH_Debug_Tools.helper.php')) {
218
-                require_once(EE_HELPERS . 'EEH_Debug_Tools.helper.php');
219
-                EEH_Debug_Tools::instance();
220
-            }
221
-            // load error handling
222
-            if (is_readable(EE_CORE . 'EE_Error.core.php')) {
223
-                require_once(EE_CORE . 'EE_Error.core.php');
224
-            } else {
225
-                wp_die(esc_html__('The EE_Error core class could not be loaded.', 'event_espresso'));
226
-            }
227
-        }
209
+		register_activation_hook(EVENT_ESPRESSO_MAIN_FILE, 'espresso_plugin_activation');
210
+		/**
211
+		 *    espresso_load_error_handling
212
+		 *    this function loads EE's class for handling exceptions and errors
213
+		 */
214
+		function espresso_load_error_handling()
215
+		{
216
+			// load debugging tools
217
+			if (WP_DEBUG === true && is_readable(EE_HELPERS . 'EEH_Debug_Tools.helper.php')) {
218
+				require_once(EE_HELPERS . 'EEH_Debug_Tools.helper.php');
219
+				EEH_Debug_Tools::instance();
220
+			}
221
+			// load error handling
222
+			if (is_readable(EE_CORE . 'EE_Error.core.php')) {
223
+				require_once(EE_CORE . 'EE_Error.core.php');
224
+			} else {
225
+				wp_die(esc_html__('The EE_Error core class could not be loaded.', 'event_espresso'));
226
+			}
227
+		}
228 228
 
229
-        /**
230
-         *    espresso_load_required
231
-         *    given a class name and path, this function will load that file or throw an exception
232
-         *
233
-         * @param    string $classname
234
-         * @param    string $full_path_to_file
235
-         * @throws    EE_Error
236
-         */
237
-        function espresso_load_required($classname, $full_path_to_file)
238
-        {
239
-            static $error_handling_loaded = false;
240
-            if ( ! $error_handling_loaded) {
241
-                espresso_load_error_handling();
242
-                $error_handling_loaded = true;
243
-            }
244
-            if (is_readable($full_path_to_file)) {
245
-                require_once($full_path_to_file);
246
-            } else {
247
-                throw new EE_Error (
248
-                        sprintf(
249
-                                esc_html__(
250
-                                        'The %s class file could not be located or is not readable due to file permissions.',
251
-                                        'event_espresso'
252
-                                ),
253
-                                $classname
254
-                        )
255
-                );
256
-            }
257
-        }
229
+		/**
230
+		 *    espresso_load_required
231
+		 *    given a class name and path, this function will load that file or throw an exception
232
+		 *
233
+		 * @param    string $classname
234
+		 * @param    string $full_path_to_file
235
+		 * @throws    EE_Error
236
+		 */
237
+		function espresso_load_required($classname, $full_path_to_file)
238
+		{
239
+			static $error_handling_loaded = false;
240
+			if ( ! $error_handling_loaded) {
241
+				espresso_load_error_handling();
242
+				$error_handling_loaded = true;
243
+			}
244
+			if (is_readable($full_path_to_file)) {
245
+				require_once($full_path_to_file);
246
+			} else {
247
+				throw new EE_Error (
248
+						sprintf(
249
+								esc_html__(
250
+										'The %s class file could not be located or is not readable due to file permissions.',
251
+										'event_espresso'
252
+								),
253
+								$classname
254
+						)
255
+				);
256
+			}
257
+		}
258 258
 
259
-        espresso_load_required('EEH_Base', EE_CORE . 'helpers' . DS . 'EEH_Base.helper.php');
260
-        espresso_load_required('EEH_File', EE_CORE . 'helpers' . DS . 'EEH_File.helper.php');
261
-        espresso_load_required('EE_Bootstrap', EE_CORE . 'EE_Bootstrap.core.php');
262
-        new EE_Bootstrap();
263
-    }
259
+		espresso_load_required('EEH_Base', EE_CORE . 'helpers' . DS . 'EEH_Base.helper.php');
260
+		espresso_load_required('EEH_File', EE_CORE . 'helpers' . DS . 'EEH_File.helper.php');
261
+		espresso_load_required('EE_Bootstrap', EE_CORE . 'EE_Bootstrap.core.php');
262
+		new EE_Bootstrap();
263
+	}
264 264
 }
265 265
 if ( ! function_exists('espresso_deactivate_plugin')) {
266
-    /**
267
-     *    deactivate_plugin
268
-     * usage:  espresso_deactivate_plugin( plugin_basename( __FILE__ ));
269
-     *
270
-     * @access public
271
-     * @param string $plugin_basename - the results of plugin_basename( __FILE__ ) for the plugin's main file
272
-     * @return    void
273
-     */
274
-    function espresso_deactivate_plugin($plugin_basename = '')
275
-    {
276
-        if ( ! function_exists('deactivate_plugins')) {
277
-            require_once(ABSPATH . 'wp-admin/includes/plugin.php');
278
-        }
279
-        unset($_GET['activate'], $_REQUEST['activate']);
280
-        deactivate_plugins($plugin_basename);
281
-    }
266
+	/**
267
+	 *    deactivate_plugin
268
+	 * usage:  espresso_deactivate_plugin( plugin_basename( __FILE__ ));
269
+	 *
270
+	 * @access public
271
+	 * @param string $plugin_basename - the results of plugin_basename( __FILE__ ) for the plugin's main file
272
+	 * @return    void
273
+	 */
274
+	function espresso_deactivate_plugin($plugin_basename = '')
275
+	{
276
+		if ( ! function_exists('deactivate_plugins')) {
277
+			require_once(ABSPATH . 'wp-admin/includes/plugin.php');
278
+		}
279
+		unset($_GET['activate'], $_REQUEST['activate']);
280
+		deactivate_plugins($plugin_basename);
281
+	}
282 282
 }
283 283
\ No newline at end of file
Please login to merge, or discard this patch.
core/db_classes/EE_Event.class.php 2 patches
Indentation   +1278 added lines, -1278 removed lines patch added patch discarded remove patch
@@ -4,7 +4,7 @@  discard block
 block discarded – undo
4 4
 
5 5
 
6 6
 if (!defined('EVENT_ESPRESSO_VERSION')) {
7
-    exit('No direct script access allowed');
7
+	exit('No direct script access allowed');
8 8
 }
9 9
 
10 10
 
@@ -18,1282 +18,1282 @@  discard block
 block discarded – undo
18 18
 class EE_Event extends EE_CPT_Base implements EEI_Line_Item_Object, EEI_Admin_Links, EEI_Has_Icon, EEI_Event
19 19
 {
20 20
 
21
-    /**
22
-     * cached value for the the logical active status for the event
23
-     *
24
-     * @see get_active_status()
25
-     * @var string
26
-     */
27
-    protected $_active_status = '';
28
-
29
-    /**
30
-     * This is just used for caching the Primary Datetime for the Event on initial retrieval
31
-     *
32
-     * @var EE_Datetime
33
-     */
34
-    protected $_Primary_Datetime;
35
-
36
-    /**
37
-     * @var EventSpacesCalculator $available_spaces_calculator
38
-     */
39
-    protected $available_spaces_calculator;
40
-
41
-
42
-    /**
43
-     * @param array $props_n_values incoming values
44
-     * @param string $timezone incoming timezone (if not set the timezone set for the website will be
45
-     *                                        used.)
46
-     * @param array $date_formats incoming date_formats in an array where the first value is the
47
-     *                                        date_format and the second value is the time format
48
-     * @return EE_Event
49
-     * @throws EE_Error
50
-     */
51
-    public static function new_instance($props_n_values = array(), $timezone = null, $date_formats = array())
52
-    {
53
-        $has_object = parent::_check_for_object($props_n_values, __CLASS__, $timezone, $date_formats);
54
-        return $has_object ? $has_object : new self($props_n_values, false, $timezone, $date_formats);
55
-    }
56
-
57
-
58
-    /**
59
-     * @param array $props_n_values incoming values from the database
60
-     * @param string $timezone incoming timezone as set by the model.  If not set the timezone for
61
-     *                                the website will be used.
62
-     * @return EE_Event
63
-     * @throws EE_Error
64
-     */
65
-    public static function new_instance_from_db($props_n_values = array(), $timezone = null)
66
-    {
67
-        return new self($props_n_values, true, $timezone);
68
-    }
69
-
70
-
71
-
72
-    /**
73
-     * @return EventSpacesCalculator
74
-     * @throws \EE_Error
75
-     */
76
-    public function getAvailableSpacesCalculator()
77
-    {
78
-        if(! $this->available_spaces_calculator instanceof EventSpacesCalculator){
79
-            $this->available_spaces_calculator = new EventSpacesCalculator($this);
80
-        }
81
-        return $this->available_spaces_calculator;
82
-    }
83
-
84
-
85
-
86
-    /**
87
-     * Overrides parent set() method so that all calls to set( 'status', $status ) can be routed to internal methods
88
-     *
89
-     * @param string $field_name
90
-     * @param mixed $field_value
91
-     * @param bool $use_default
92
-     * @throws EE_Error
93
-     */
94
-    public function set($field_name, $field_value, $use_default = false)
95
-    {
96
-        switch ($field_name) {
97
-            case 'status' :
98
-                $this->set_status($field_value, $use_default);
99
-                break;
100
-            default :
101
-                parent::set($field_name, $field_value, $use_default);
102
-        }
103
-    }
104
-
105
-
106
-    /**
107
-     *    set_status
108
-     * Checks if event status is being changed to SOLD OUT
109
-     * and updates event meta data with previous event status
110
-     * so that we can revert things if/when the event is no longer sold out
111
-     *
112
-     * @access public
113
-     * @param string $new_status
114
-     * @param bool $use_default
115
-     * @return void
116
-     * @throws EE_Error
117
-     */
118
-    public function set_status($new_status = null, $use_default = false)
119
-    {
120
-        // if nothing is set, and we aren't explicitly wanting to reset the status, then just leave
121
-        if (empty($new_status) && !$use_default) {
122
-            return;
123
-        }
124
-        // get current Event status
125
-        $old_status = $this->status();
126
-        // if status has changed
127
-        if ($old_status !== $new_status) {
128
-            // TO sold_out
129
-            if ($new_status === EEM_Event::sold_out) {
130
-                // save the previous event status so that we can revert if the event is no longer sold out
131
-                $this->add_post_meta('_previous_event_status', $old_status);
132
-                do_action('AHEE__EE_Event__set_status__to_sold_out', $this, $old_status, $new_status);
133
-                // OR FROM  sold_out
134
-            } else if ($old_status === EEM_Event::sold_out) {
135
-                $this->delete_post_meta('_previous_event_status');
136
-                do_action('AHEE__EE_Event__set_status__from_sold_out', $this, $old_status, $new_status);
137
-            }
138
-            // update status
139
-            parent::set('status', $new_status, $use_default);
140
-            do_action('AHEE__EE_Event__set_status__after_update', $this);
141
-            return;
142
-        }
143
-        // even though the old value matches the new value, it's still good to
144
-        // allow the parent set method to have a say
145
-        parent::set('status', $new_status, $use_default);
146
-    }
147
-
148
-
149
-    /**
150
-     * Gets all the datetimes for this event
151
-     *
152
-     * @param array $query_params like EEM_Base::get_all
153
-     * @return EE_Base_Class[]|EE_Datetime[]
154
-     * @throws EE_Error
155
-     */
156
-    public function datetimes($query_params = array())
157
-    {
158
-        return $this->get_many_related('Datetime', $query_params);
159
-    }
160
-
161
-
162
-    /**
163
-     * Gets all the datetimes for this event, ordered by DTT_EVT_start in ascending order
164
-     *
165
-     * @return EE_Base_Class[]|EE_Datetime[]
166
-     * @throws EE_Error
167
-     */
168
-    public function datetimes_in_chronological_order()
169
-    {
170
-        return $this->get_many_related('Datetime', array('order_by' => array('DTT_EVT_start' => 'ASC')));
171
-    }
172
-
173
-
174
-    /**
175
-     * Gets all the datetimes for this event, ordered by the DTT_order on the datetime.
176
-     * @darren, we should probably UNSET timezone on the EEM_Datetime model
177
-     * after running our query, so that this timezone isn't set for EVERY query
178
-     * on EEM_Datetime for the rest of the request, no?
179
-     *
180
-     * @param boolean $show_expired whether or not to include expired events
181
-     * @param boolean $show_deleted whether or not to include deleted events
182
-     * @param null $limit
183
-     * @return EE_Datetime[]
184
-     * @throws EE_Error
185
-     */
186
-    public function datetimes_ordered($show_expired = true, $show_deleted = false, $limit = null)
187
-    {
188
-        return EEM_Datetime::instance($this->_timezone)->get_datetimes_for_event_ordered_by_DTT_order(
189
-            $this->ID(),
190
-            $show_expired,
191
-            $show_deleted,
192
-            $limit
193
-        );
194
-    }
195
-
196
-
197
-    /**
198
-     * Returns one related datetime. Mostly only used by some legacy code.
199
-     *
200
-     * @return EE_Base_Class|EE_Datetime
201
-     * @throws EE_Error
202
-     */
203
-    public function first_datetime()
204
-    {
205
-        return $this->get_first_related('Datetime');
206
-    }
207
-
208
-
209
-    /**
210
-     * Returns the 'primary' datetime for the event
211
-     *
212
-     * @param bool $try_to_exclude_expired
213
-     * @param bool $try_to_exclude_deleted
214
-     * @return EE_Datetime
215
-     * @throws EE_Error
216
-     */
217
-    public function primary_datetime($try_to_exclude_expired = true, $try_to_exclude_deleted = true)
218
-    {
219
-        if (!empty ($this->_Primary_Datetime)) {
220
-            return $this->_Primary_Datetime;
221
-        }
222
-        $this->_Primary_Datetime = EEM_Datetime::instance($this->_timezone)->get_primary_datetime_for_event(
223
-            $this->ID(),
224
-            $try_to_exclude_expired,
225
-            $try_to_exclude_deleted
226
-        );
227
-        return $this->_Primary_Datetime;
228
-    }
229
-
230
-
231
-    /**
232
-     * Gets all the tickets available for purchase of this event
233
-     *
234
-     * @param array $query_params like EEM_Base::get_all
235
-     * @return EE_Base_Class[]|EE_Ticket[]
236
-     * @throws EE_Error
237
-     */
238
-    public function tickets($query_params = array())
239
-    {
240
-        //first get all datetimes
241
-        $datetimes = $this->datetimes_ordered();
242
-        if (!$datetimes) {
243
-            return array();
244
-        }
245
-        $datetime_ids = array();
246
-        foreach ($datetimes as $datetime) {
247
-            $datetime_ids[] = $datetime->ID();
248
-        }
249
-        $where_params = array('Datetime.DTT_ID' => array('IN', $datetime_ids));
250
-        //if incoming $query_params has where conditions let's merge but not override existing.
251
-        if (is_array($query_params) && isset($query_params[0])) {
252
-            $where_params = array_merge($query_params[0], $where_params);
253
-            unset($query_params[0]);
254
-        }
255
-        //now add $where_params to $query_params
256
-        $query_params[0] = $where_params;
257
-        return EEM_Ticket::instance()->get_all($query_params);
258
-    }
259
-
260
-
261
-    /**
262
-     * get all unexpired untrashed tickets
263
-     *
264
-     * @return EE_Ticket[]
265
-     * @throws EE_Error
266
-     */
267
-    public function active_tickets()
268
-    {
269
-        return $this->tickets(array(
270
-            array(
271
-                'TKT_end_date' => array('>=', EEM_Ticket::instance()->current_time_for_query('TKT_end_date')),
272
-                'TKT_deleted' => false,
273
-            ),
274
-        ));
275
-    }
276
-
277
-
278
-    /**
279
-     * @return bool
280
-     * @throws EE_Error
281
-     */
282
-    public function additional_limit()
283
-    {
284
-        return $this->get('EVT_additional_limit');
285
-    }
286
-
287
-
288
-    /**
289
-     * @return bool
290
-     * @throws EE_Error
291
-     */
292
-    public function allow_overflow()
293
-    {
294
-        return $this->get('EVT_allow_overflow');
295
-    }
296
-
297
-
298
-    /**
299
-     * @return bool
300
-     * @throws EE_Error
301
-     */
302
-    public function created()
303
-    {
304
-        return $this->get('EVT_created');
305
-    }
306
-
307
-
308
-    /**
309
-     * @return bool
310
-     * @throws EE_Error
311
-     */
312
-    public function description()
313
-    {
314
-        return $this->get('EVT_desc');
315
-    }
316
-
317
-
318
-    /**
319
-     * Runs do_shortcode and wpautop on the description
320
-     *
321
-     * @return string of html
322
-     * @throws EE_Error
323
-     */
324
-    public function description_filtered()
325
-    {
326
-        return $this->get_pretty('EVT_desc');
327
-    }
328
-
329
-
330
-    /**
331
-     * @return bool
332
-     * @throws EE_Error
333
-     */
334
-    public function display_description()
335
-    {
336
-        return $this->get('EVT_display_desc');
337
-    }
338
-
339
-
340
-    /**
341
-     * @return bool
342
-     * @throws EE_Error
343
-     */
344
-    public function display_ticket_selector()
345
-    {
346
-        return (bool)$this->get('EVT_display_ticket_selector');
347
-    }
348
-
349
-
350
-    /**
351
-     * @return bool
352
-     * @throws EE_Error
353
-     */
354
-    public function external_url()
355
-    {
356
-        return $this->get('EVT_external_URL');
357
-    }
358
-
359
-
360
-    /**
361
-     * @return bool
362
-     * @throws EE_Error
363
-     */
364
-    public function member_only()
365
-    {
366
-        return $this->get('EVT_member_only');
367
-    }
368
-
369
-
370
-    /**
371
-     * @return bool
372
-     * @throws EE_Error
373
-     */
374
-    public function phone()
375
-    {
376
-        return $this->get('EVT_phone');
377
-    }
378
-
379
-
380
-    /**
381
-     * @return bool
382
-     * @throws EE_Error
383
-     */
384
-    public function modified()
385
-    {
386
-        return $this->get('EVT_modified');
387
-    }
388
-
389
-
390
-    /**
391
-     * @return bool
392
-     * @throws EE_Error
393
-     */
394
-    public function name()
395
-    {
396
-        return $this->get('EVT_name');
397
-    }
398
-
399
-
400
-    /**
401
-     * @return bool
402
-     * @throws EE_Error
403
-     */
404
-    public function order()
405
-    {
406
-        return $this->get('EVT_order');
407
-    }
408
-
409
-
410
-    /**
411
-     * @return bool|string
412
-     * @throws EE_Error
413
-     */
414
-    public function default_registration_status()
415
-    {
416
-        $event_default_registration_status = $this->get('EVT_default_registration_status');
417
-        return !empty($event_default_registration_status)
418
-            ? $event_default_registration_status
419
-            : EE_Registry::instance()->CFG->registration->default_STS_ID;
420
-    }
421
-
422
-
423
-    /**
424
-     * @param int $num_words
425
-     * @param null $more
426
-     * @param bool $not_full_desc
427
-     * @return bool|string
428
-     * @throws EE_Error
429
-     */
430
-    public function short_description($num_words = 55, $more = null, $not_full_desc = false)
431
-    {
432
-        $short_desc = $this->get('EVT_short_desc');
433
-        if (!empty($short_desc) || $not_full_desc) {
434
-            return $short_desc;
435
-        }
436
-        $full_desc = $this->get('EVT_desc');
437
-        return wp_trim_words($full_desc, $num_words, $more);
438
-    }
439
-
440
-
441
-    /**
442
-     * @return bool
443
-     * @throws EE_Error
444
-     */
445
-    public function slug()
446
-    {
447
-        return $this->get('EVT_slug');
448
-    }
449
-
450
-
451
-    /**
452
-     * @return bool
453
-     * @throws EE_Error
454
-     */
455
-    public function timezone_string()
456
-    {
457
-        return $this->get('EVT_timezone_string');
458
-    }
459
-
460
-
461
-    /**
462
-     * @return bool
463
-     * @throws EE_Error
464
-     */
465
-    public function visible_on()
466
-    {
467
-        return $this->get('EVT_visible_on');
468
-    }
469
-
470
-
471
-    /**
472
-     * @return int
473
-     * @throws EE_Error
474
-     */
475
-    public function wp_user()
476
-    {
477
-        return $this->get('EVT_wp_user');
478
-    }
479
-
480
-
481
-    /**
482
-     * @return bool
483
-     * @throws EE_Error
484
-     */
485
-    public function donations()
486
-    {
487
-        return $this->get('EVT_donations');
488
-    }
489
-
490
-
491
-    /**
492
-     * @param $limit
493
-     * @throws EE_Error
494
-     */
495
-    public function set_additional_limit($limit)
496
-    {
497
-        $this->set('EVT_additional_limit', $limit);
498
-    }
499
-
500
-
501
-    /**
502
-     * @param $created
503
-     * @throws EE_Error
504
-     */
505
-    public function set_created($created)
506
-    {
507
-        $this->set('EVT_created', $created);
508
-    }
509
-
510
-
511
-    /**
512
-     * @param $desc
513
-     * @throws EE_Error
514
-     */
515
-    public function set_description($desc)
516
-    {
517
-        $this->set('EVT_desc', $desc);
518
-    }
519
-
520
-
521
-    /**
522
-     * @param $display_desc
523
-     * @throws EE_Error
524
-     */
525
-    public function set_display_description($display_desc)
526
-    {
527
-        $this->set('EVT_display_desc', $display_desc);
528
-    }
529
-
530
-
531
-    /**
532
-     * @param $display_ticket_selector
533
-     * @throws EE_Error
534
-     */
535
-    public function set_display_ticket_selector($display_ticket_selector)
536
-    {
537
-        $this->set('EVT_display_ticket_selector', $display_ticket_selector);
538
-    }
539
-
540
-
541
-    /**
542
-     * @param $external_url
543
-     * @throws EE_Error
544
-     */
545
-    public function set_external_url($external_url)
546
-    {
547
-        $this->set('EVT_external_URL', $external_url);
548
-    }
549
-
550
-
551
-    /**
552
-     * @param $member_only
553
-     * @throws EE_Error
554
-     */
555
-    public function set_member_only($member_only)
556
-    {
557
-        $this->set('EVT_member_only', $member_only);
558
-    }
559
-
560
-
561
-    /**
562
-     * @param $event_phone
563
-     * @throws EE_Error
564
-     */
565
-    public function set_event_phone($event_phone)
566
-    {
567
-        $this->set('EVT_phone', $event_phone);
568
-    }
569
-
570
-
571
-    /**
572
-     * @param $modified
573
-     * @throws EE_Error
574
-     */
575
-    public function set_modified($modified)
576
-    {
577
-        $this->set('EVT_modified', $modified);
578
-    }
579
-
580
-
581
-    /**
582
-     * @param $name
583
-     * @throws EE_Error
584
-     */
585
-    public function set_name($name)
586
-    {
587
-        $this->set('EVT_name', $name);
588
-    }
589
-
590
-
591
-    /**
592
-     * @param $order
593
-     * @throws EE_Error
594
-     */
595
-    public function set_order($order)
596
-    {
597
-        $this->set('EVT_order', $order);
598
-    }
599
-
600
-
601
-    /**
602
-     * @param $short_desc
603
-     * @throws EE_Error
604
-     */
605
-    public function set_short_description($short_desc)
606
-    {
607
-        $this->set('EVT_short_desc', $short_desc);
608
-    }
609
-
610
-
611
-    /**
612
-     * @param $slug
613
-     * @throws EE_Error
614
-     */
615
-    public function set_slug($slug)
616
-    {
617
-        $this->set('EVT_slug', $slug);
618
-    }
619
-
620
-
621
-    /**
622
-     * @param $timezone_string
623
-     * @throws EE_Error
624
-     */
625
-    public function set_timezone_string($timezone_string)
626
-    {
627
-        $this->set('EVT_timezone_string', $timezone_string);
628
-    }
629
-
630
-
631
-    /**
632
-     * @param $visible_on
633
-     * @throws EE_Error
634
-     */
635
-    public function set_visible_on($visible_on)
636
-    {
637
-        $this->set('EVT_visible_on', $visible_on);
638
-    }
639
-
640
-
641
-    /**
642
-     * @param $wp_user
643
-     * @throws EE_Error
644
-     */
645
-    public function set_wp_user($wp_user)
646
-    {
647
-        $this->set('EVT_wp_user', $wp_user);
648
-    }
649
-
650
-
651
-    /**
652
-     * @param $default_registration_status
653
-     * @throws EE_Error
654
-     */
655
-    public function set_default_registration_status($default_registration_status)
656
-    {
657
-        $this->set('EVT_default_registration_status', $default_registration_status);
658
-    }
659
-
660
-
661
-    /**
662
-     * @param $donations
663
-     * @throws EE_Error
664
-     */
665
-    public function set_donations($donations)
666
-    {
667
-        $this->set('EVT_donations', $donations);
668
-    }
669
-
670
-
671
-    /**
672
-     * Adds a venue to this event
673
-     *
674
-     * @param EE_Venue /int $venue_id_or_obj
675
-     * @return EE_Base_Class|EE_Venue
676
-     * @throws EE_Error
677
-     */
678
-    public function add_venue($venue_id_or_obj)
679
-    {
680
-        return $this->_add_relation_to($venue_id_or_obj, 'Venue');
681
-    }
682
-
683
-
684
-    /**
685
-     * Removes a venue from the event
686
-     *
687
-     * @param EE_Venue /int $venue_id_or_obj
688
-     * @return EE_Base_Class|EE_Venue
689
-     * @throws EE_Error
690
-     */
691
-    public function remove_venue($venue_id_or_obj)
692
-    {
693
-        return $this->_remove_relation_to($venue_id_or_obj, 'Venue');
694
-    }
695
-
696
-
697
-    /**
698
-     * Gets all the venues related ot the event. May provide additional $query_params if desired
699
-     *
700
-     * @param array $query_params like EEM_Base::get_all's $query_params
701
-     * @return EE_Base_Class[]|EE_Venue[]
702
-     * @throws EE_Error
703
-     */
704
-    public function venues($query_params = array())
705
-    {
706
-        return $this->get_many_related('Venue', $query_params);
707
-    }
708
-
709
-
710
-    /**
711
-     * check if event id is present and if event is published
712
-     *
713
-     * @access public
714
-     * @return boolean true yes, false no
715
-     * @throws EE_Error
716
-     */
717
-    private function _has_ID_and_is_published()
718
-    {
719
-        // first check if event id is present and not NULL,
720
-        // then check if this event is published (or any of the equivalent "published" statuses)
721
-        return
722
-            $this->ID() && $this->ID() !== null
723
-            && (
724
-                $this->status() === 'publish'
725
-                || $this->status() === EEM_Event::sold_out
726
-                || $this->status() === EEM_Event::postponed
727
-                || $this->status() === EEM_Event::cancelled
728
-            );
729
-    }
730
-
731
-
732
-    /**
733
-     * This simply compares the internal dates with NOW and determines if the event is upcoming or not.
734
-     *
735
-     * @access public
736
-     * @return boolean true yes, false no
737
-     * @throws EE_Error
738
-     */
739
-    public function is_upcoming()
740
-    {
741
-        // check if event id is present and if this event is published
742
-        if ($this->is_inactive()) {
743
-            return false;
744
-        }
745
-        // set initial value
746
-        $upcoming = false;
747
-        //next let's get all datetimes and loop through them
748
-        $datetimes = $this->datetimes_in_chronological_order();
749
-        foreach ($datetimes as $datetime) {
750
-            if ($datetime instanceof EE_Datetime) {
751
-                //if this dtt is expired then we continue cause one of the other datetimes might be upcoming.
752
-                if ($datetime->is_expired()) {
753
-                    continue;
754
-                }
755
-                //if this dtt is active then we return false.
756
-                if ($datetime->is_active()) {
757
-                    return false;
758
-                }
759
-                //otherwise let's check upcoming status
760
-                $upcoming = $datetime->is_upcoming();
761
-            }
762
-        }
763
-        return $upcoming;
764
-    }
765
-
766
-
767
-    /**
768
-     * @return bool
769
-     * @throws EE_Error
770
-     */
771
-    public function is_active()
772
-    {
773
-        // check if event id is present and if this event is published
774
-        if ($this->is_inactive()) {
775
-            return false;
776
-        }
777
-        // set initial value
778
-        $active = false;
779
-        //next let's get all datetimes and loop through them
780
-        $datetimes = $this->datetimes_in_chronological_order();
781
-        foreach ($datetimes as $datetime) {
782
-            if ($datetime instanceof EE_Datetime) {
783
-                //if this dtt is expired then we continue cause one of the other datetimes might be active.
784
-                if ($datetime->is_expired()) {
785
-                    continue;
786
-                }
787
-                //if this dtt is upcoming then we return false.
788
-                if ($datetime->is_upcoming()) {
789
-                    return false;
790
-                }
791
-                //otherwise let's check active status
792
-                $active = $datetime->is_active();
793
-            }
794
-        }
795
-        return $active;
796
-    }
797
-
798
-
799
-    /**
800
-     * @return bool
801
-     * @throws EE_Error
802
-     */
803
-    public function is_expired()
804
-    {
805
-        // check if event id is present and if this event is published
806
-        if ($this->is_inactive()) {
807
-            return false;
808
-        }
809
-        // set initial value
810
-        $expired = false;
811
-        //first let's get all datetimes and loop through them
812
-        $datetimes = $this->datetimes_in_chronological_order();
813
-        foreach ($datetimes as $datetime) {
814
-            if ($datetime instanceof EE_Datetime) {
815
-                //if this dtt is upcoming or active then we return false.
816
-                if ($datetime->is_upcoming() || $datetime->is_active()) {
817
-                    return false;
818
-                }
819
-                //otherwise let's check active status
820
-                $expired = $datetime->is_expired();
821
-            }
822
-        }
823
-        return $expired;
824
-    }
825
-
826
-
827
-    /**
828
-     * @return bool
829
-     * @throws EE_Error
830
-     */
831
-    public function is_inactive()
832
-    {
833
-        // check if event id is present and if this event is published
834
-        if ($this->_has_ID_and_is_published()) {
835
-            return false;
836
-        }
837
-        return true;
838
-    }
839
-
840
-
841
-    /**
842
-     * calculate spaces remaining based on "saleable" tickets
843
-     *
844
-     * @param array $tickets
845
-     * @param bool $filtered
846
-     * @return int|float
847
-     * @throws EE_Error
848
-     */
849
-    public function spaces_remaining($tickets = array(), $filtered = true)
850
-    {
851
-        $this->getAvailableSpacesCalculator()->setActiveTickets($tickets);
852
-        $spaces_remaining = $this->getAvailableSpacesCalculator()->spacesRemaining();
853
-        return $filtered
854
-            ? apply_filters(
855
-                'FHEE_EE_Event__spaces_remaining',
856
-                $spaces_remaining,
857
-                $this,
858
-                $tickets
859
-            )
860
-            : $spaces_remaining;
861
-    }
862
-
863
-
864
-    /**
865
-     *    perform_sold_out_status_check
866
-     *    checks all of this events's datetime  reg_limit - sold values to determine if ANY datetimes have spaces available...
867
-     *    if NOT, then the event status will get toggled to 'sold_out'
868
-     *
869
-     * @access public
870
-     * @return bool    return the ACTUAL sold out state.
871
-     * @throws EE_Error
872
-     */
873
-    public function perform_sold_out_status_check()
874
-    {
875
-        // get all unexpired untrashed tickets
876
-        $tickets = $this->active_tickets();
877
-        // if all the tickets are just expired, then don't update the event status to sold out
878
-        if (empty($tickets)) {
879
-            return true;
880
-        }
881
-        $spaces_remaining = $this->spaces_remaining($tickets);
882
-        if ($spaces_remaining < 1) {
883
-            $this->set_status(EEM_Event::sold_out);
884
-            $this->save();
885
-            $sold_out = true;
886
-        } else {
887
-            $sold_out = false;
888
-            // was event previously marked as sold out ?
889
-            if ($this->status() === EEM_Event::sold_out) {
890
-                // revert status to previous value, if it was set
891
-                $previous_event_status = $this->get_post_meta('_previous_event_status', true);
892
-                if ($previous_event_status) {
893
-                    $this->set_status($previous_event_status);
894
-                    $this->save();
895
-                }
896
-            }
897
-        }
898
-        do_action('AHEE__EE_Event__perform_sold_out_status_check__end', $this, $sold_out, $spaces_remaining, $tickets);
899
-        return $sold_out;
900
-    }
901
-
902
-
903
-
904
-    /**
905
-     * This returns the total remaining spaces for sale on this event.
906
-     *
907
-     * @uses EE_Event::total_available_spaces()
908
-     * @return float|int
909
-     * @throws EE_Error
910
-     */
911
-    public function spaces_remaining_for_sale()
912
-    {
913
-        return $this->total_available_spaces(true);
914
-    }
915
-
916
-
917
-
918
-    /**
919
-     * This returns the total spaces available for an event
920
-     * while considering all the qtys on the tickets and the reg limits
921
-     * on the datetimes attached to this event.
922
-     *
923
-     * @param   bool $consider_sold Whether to consider any tickets that have already sold in our calculation.
924
-     *                              If this is false, then we return the most tickets that could ever be sold
925
-     *                              for this event with the datetime and tickets setup on the event under optimal
926
-     *                              selling conditions.  Otherwise we return a live calculation of spaces available
927
-     *                              based on tickets sold.  Depending on setup and stage of sales, this
928
-     *                              may appear to equal remaining tickets.  However, the more tickets are
929
-     *                              sold out, the more accurate the "live" total is.
930
-     * @return  int|float  (Note: if EE_INF is returned its considered a float by PHP)
931
-     * @throws EE_Error
932
-     */
933
-    public function total_available_spaces($consider_sold = false)
934
-    {
935
-        $spaces_available = $consider_sold
936
-            ? $this->getAvailableSpacesCalculator()->spacesRemaining()
937
-            : $this->getAvailableSpacesCalculator()->totalSpacesAvailable();
938
-        return apply_filters(
939
-            'FHEE_EE_Event__total_available_spaces__spaces_available',
940
-            $spaces_available,
941
-            $this,
942
-            $this->getAvailableSpacesCalculator()->getDatetimes(),
943
-            $this->getAvailableSpacesCalculator()->getActiveTickets()
944
-        );
945
-    }
946
-
947
-
948
-    /**
949
-     * Checks if the event is set to sold out
950
-     *
951
-     * @param  bool $actual whether or not to perform calculations to not only figure the
952
-     *                      actual status but also to flip the status if necessary to sold
953
-     *                      out If false, we just check the existing status of the event
954
-     * @return boolean
955
-     * @throws EE_Error
956
-     */
957
-    public function is_sold_out($actual = false)
958
-    {
959
-        if (!$actual) {
960
-            return $this->status() === EEM_Event::sold_out;
961
-        }
962
-        return $this->perform_sold_out_status_check();
963
-    }
964
-
965
-
966
-    /**
967
-     * Checks if the event is marked as postponed
968
-     *
969
-     * @return boolean
970
-     */
971
-    public function is_postponed()
972
-    {
973
-        return $this->status() === EEM_Event::postponed;
974
-    }
975
-
976
-
977
-    /**
978
-     * Checks if the event is marked as cancelled
979
-     *
980
-     * @return boolean
981
-     */
982
-    public function is_cancelled()
983
-    {
984
-        return $this->status() === EEM_Event::cancelled;
985
-    }
986
-
987
-
988
-    /**
989
-     * Get the logical active status in a hierarchical order for all the datetimes.  Note
990
-     * Basically, we order the datetimes by EVT_start_date.  Then first test on whether the event is published.  If its
991
-     * NOT published then we test for whether its expired or not.  IF it IS published then we test first on whether an
992
-     * event has any active dates.  If no active dates then we check for any upcoming dates.  If no upcoming dates then
993
-     * the event is considered expired.
994
-     * NOTE: this method does NOT calculate whether the datetimes are sold out when event is published.  Sold Out is a status
995
-     * set on the EVENT when it is not published and thus is done
996
-     *
997
-     * @param bool $reset
998
-     * @return bool | string - based on EE_Datetime active constants or FALSE if error.
999
-     * @throws EE_Error
1000
-     */
1001
-    public function get_active_status($reset = false)
1002
-    {
1003
-        // if the active status has already been set, then just use that value (unless we are resetting it)
1004
-        if (!empty($this->_active_status) && !$reset) {
1005
-            return $this->_active_status;
1006
-        }
1007
-        //first check if event id is present on this object
1008
-        if (!$this->ID()) {
1009
-            return false;
1010
-        }
1011
-        $where_params_for_event = array(array('EVT_ID' => $this->ID()));
1012
-        //if event is published:
1013
-        if ($this->status() === 'publish') {
1014
-            //active?
1015
-            if (EEM_Datetime::instance()->get_datetime_count_for_status(EE_Datetime::active, $where_params_for_event) > 0) {
1016
-                $this->_active_status = EE_Datetime::active;
1017
-            } else {
1018
-                //upcoming?
1019
-                if (EEM_Datetime::instance()->get_datetime_count_for_status(EE_Datetime::upcoming, $where_params_for_event) > 0) {
1020
-                    $this->_active_status = EE_Datetime::upcoming;
1021
-                } else {
1022
-                    //expired?
1023
-                    if (
1024
-                        EEM_Datetime::instance()->get_datetime_count_for_status(EE_Datetime::expired, $where_params_for_event) > 0
1025
-                    ) {
1026
-                        $this->_active_status = EE_Datetime::expired;
1027
-                    } else {
1028
-                        //it would be odd if things make it this far because it basically means there are no datetime's
1029
-                        //attached to the event.  So in this case it will just be considered inactive.
1030
-                        $this->_active_status = EE_Datetime::inactive;
1031
-                    }
1032
-                }
1033
-            }
1034
-        } else {
1035
-            //the event is not published, so let's just set it's active status according to its' post status
1036
-            switch ($this->status()) {
1037
-                case EEM_Event::sold_out :
1038
-                    $this->_active_status = EE_Datetime::sold_out;
1039
-                    break;
1040
-                case EEM_Event::cancelled :
1041
-                    $this->_active_status = EE_Datetime::cancelled;
1042
-                    break;
1043
-                case EEM_Event::postponed :
1044
-                    $this->_active_status = EE_Datetime::postponed;
1045
-                    break;
1046
-                default :
1047
-                    $this->_active_status = EE_Datetime::inactive;
1048
-            }
1049
-        }
1050
-        return $this->_active_status;
1051
-    }
1052
-
1053
-
1054
-    /**
1055
-     *    pretty_active_status
1056
-     *
1057
-     * @access public
1058
-     * @param boolean $echo whether to return (FALSE), or echo out the result (TRUE)
1059
-     * @return mixed void|string
1060
-     * @throws EE_Error
1061
-     */
1062
-    public function pretty_active_status($echo = true)
1063
-    {
1064
-        $active_status = $this->get_active_status();
1065
-        $status = '<span class="ee-status event-active-status-'
1066
-            . $active_status
1067
-            . '">'
1068
-            . EEH_Template::pretty_status($active_status, false, 'sentence')
1069
-            . '</span>';
1070
-        if ($echo) {
1071
-            echo $status;
1072
-            return '';
1073
-        }
1074
-        return $status;
1075
-    }
1076
-
1077
-
1078
-    /**
1079
-     * @return bool|int
1080
-     * @throws EE_Error
1081
-     */
1082
-    public function get_number_of_tickets_sold()
1083
-    {
1084
-        $tkt_sold = 0;
1085
-        if (!$this->ID()) {
1086
-            return 0;
1087
-        }
1088
-        $datetimes = $this->datetimes();
1089
-        foreach ($datetimes as $datetime) {
1090
-            if ($datetime instanceof EE_Datetime) {
1091
-                $tkt_sold += $datetime->sold();
1092
-            }
1093
-        }
1094
-        return $tkt_sold;
1095
-    }
1096
-
1097
-
1098
-    /**
1099
-     * This just returns a count of all the registrations for this event
1100
-     *
1101
-     * @access  public
1102
-     * @return int
1103
-     * @throws EE_Error
1104
-     */
1105
-    public function get_count_of_all_registrations()
1106
-    {
1107
-        return EEM_Event::instance()->count_related($this, 'Registration');
1108
-    }
1109
-
1110
-
1111
-    /**
1112
-     * This returns the ticket with the earliest start time that is
1113
-     * available for this event (across all datetimes attached to the event)
1114
-     *
1115
-     * @return EE_Base_Class|EE_Ticket|null
1116
-     * @throws EE_Error
1117
-     */
1118
-    public function get_ticket_with_earliest_start_time()
1119
-    {
1120
-        $where['Datetime.EVT_ID'] = $this->ID();
1121
-        $query_params = array($where, 'order_by' => array('TKT_start_date' => 'ASC'));
1122
-        return EE_Registry::instance()->load_model('Ticket')->get_one($query_params);
1123
-    }
1124
-
1125
-
1126
-    /**
1127
-     * This returns the ticket with the latest end time that is available
1128
-     * for this event (across all datetimes attached to the event)
1129
-     *
1130
-     * @return EE_Base_Class|EE_Ticket|null
1131
-     * @throws EE_Error
1132
-     */
1133
-    public function get_ticket_with_latest_end_time()
1134
-    {
1135
-        $where['Datetime.EVT_ID'] = $this->ID();
1136
-        $query_params = array($where, 'order_by' => array('TKT_end_date' => 'DESC'));
1137
-        return EE_Registry::instance()->load_model('Ticket')->get_one($query_params);
1138
-    }
1139
-
1140
-
1141
-    /**
1142
-     * This returns whether there are any tickets on sale for this event.
1143
-     *
1144
-     * @return bool true = YES tickets on sale.
1145
-     * @throws EE_Error
1146
-     */
1147
-    public function tickets_on_sale()
1148
-    {
1149
-        $earliest_ticket = $this->get_ticket_with_earliest_start_time();
1150
-        $latest_ticket = $this->get_ticket_with_latest_end_time();
1151
-        if (!$latest_ticket instanceof EE_Ticket && !$earliest_ticket instanceof EE_Ticket) {
1152
-            return false;
1153
-        }
1154
-        //check on sale for these two tickets.
1155
-        if ($latest_ticket->is_on_sale() || $earliest_ticket->is_on_sale()) {
1156
-            return true;
1157
-        }
1158
-        return false;
1159
-    }
1160
-
1161
-
1162
-    /**
1163
-     * Gets the URL for viewing this event on the front-end. Overrides parent
1164
-     * to check for an external URL first
1165
-     *
1166
-     * @return string
1167
-     * @throws EE_Error
1168
-     */
1169
-    public function get_permalink()
1170
-    {
1171
-        if ($this->external_url()) {
1172
-            return $this->external_url();
1173
-        }
1174
-        return parent::get_permalink();
1175
-    }
1176
-
1177
-
1178
-    /**
1179
-     * Gets the first term for 'espresso_event_categories' we can find
1180
-     *
1181
-     * @param array $query_params like EEM_Base::get_all
1182
-     * @return EE_Base_Class|EE_Term|null
1183
-     * @throws EE_Error
1184
-     */
1185
-    public function first_event_category($query_params = array())
1186
-    {
1187
-        $query_params[0]['Term_Taxonomy.taxonomy'] = 'espresso_event_categories';
1188
-        $query_params[0]['Term_Taxonomy.Event.EVT_ID'] = $this->ID();
1189
-        return EEM_Term::instance()->get_one($query_params);
1190
-    }
1191
-
1192
-
1193
-    /**
1194
-     * Gets all terms for 'espresso_event_categories' we can find
1195
-     *
1196
-     * @param array $query_params
1197
-     * @return EE_Base_Class[]|EE_Term[]
1198
-     * @throws EE_Error
1199
-     */
1200
-    public function get_all_event_categories($query_params = array())
1201
-    {
1202
-        $query_params[0]['Term_Taxonomy.taxonomy'] = 'espresso_event_categories';
1203
-        $query_params[0]['Term_Taxonomy.Event.EVT_ID'] = $this->ID();
1204
-        return EEM_Term::instance()->get_all($query_params);
1205
-    }
1206
-
1207
-
1208
-    /**
1209
-     * Gets all the question groups, ordering them by QSG_order ascending
1210
-     *
1211
-     * @param array $query_params @see EEM_Base::get_all
1212
-     * @return EE_Base_Class[]|EE_Question_Group[]
1213
-     * @throws EE_Error
1214
-     */
1215
-    public function question_groups($query_params = array())
1216
-    {
1217
-        $query_params = !empty($query_params) ? $query_params : array('order_by' => array('QSG_order' => 'ASC'));
1218
-        return $this->get_many_related('Question_Group', $query_params);
1219
-    }
1220
-
1221
-
1222
-    /**
1223
-     * Implementation for EEI_Has_Icon interface method.
1224
-     *
1225
-     * @see EEI_Visual_Representation for comments
1226
-     * @return string
1227
-     */
1228
-    public function get_icon()
1229
-    {
1230
-        return '<span class="dashicons dashicons-flag"></span>';
1231
-    }
1232
-
1233
-
1234
-    /**
1235
-     * Implementation for EEI_Admin_Links interface method.
1236
-     *
1237
-     * @see EEI_Admin_Links for comments
1238
-     * @return string
1239
-     * @throws EE_Error
1240
-     */
1241
-    public function get_admin_details_link()
1242
-    {
1243
-        return $this->get_admin_edit_link();
1244
-    }
1245
-
1246
-
1247
-    /**
1248
-     * Implementation for EEI_Admin_Links interface method.
1249
-     *
1250
-     * @see EEI_Admin_Links for comments
1251
-     * @return string
1252
-     * @throws EE_Error
1253
-     */
1254
-    public function get_admin_edit_link()
1255
-    {
1256
-        return EEH_URL::add_query_args_and_nonce(array(
1257
-            'page' => 'espresso_events',
1258
-            'action' => 'edit',
1259
-            'post' => $this->ID(),
1260
-        ),
1261
-            admin_url('admin.php')
1262
-        );
1263
-    }
1264
-
1265
-
1266
-    /**
1267
-     * Implementation for EEI_Admin_Links interface method.
1268
-     *
1269
-     * @see EEI_Admin_Links for comments
1270
-     * @return string
1271
-     */
1272
-    public function get_admin_settings_link()
1273
-    {
1274
-        return EEH_URL::add_query_args_and_nonce(array(
1275
-            'page' => 'espresso_events',
1276
-            'action' => 'default_event_settings',
1277
-        ),
1278
-            admin_url('admin.php')
1279
-        );
1280
-    }
1281
-
1282
-
1283
-    /**
1284
-     * Implementation for EEI_Admin_Links interface method.
1285
-     *
1286
-     * @see EEI_Admin_Links for comments
1287
-     * @return string
1288
-     */
1289
-    public function get_admin_overview_link()
1290
-    {
1291
-        return EEH_URL::add_query_args_and_nonce(array(
1292
-            'page' => 'espresso_events',
1293
-            'action' => 'default',
1294
-        ),
1295
-            admin_url('admin.php')
1296
-        );
1297
-    }
21
+	/**
22
+	 * cached value for the the logical active status for the event
23
+	 *
24
+	 * @see get_active_status()
25
+	 * @var string
26
+	 */
27
+	protected $_active_status = '';
28
+
29
+	/**
30
+	 * This is just used for caching the Primary Datetime for the Event on initial retrieval
31
+	 *
32
+	 * @var EE_Datetime
33
+	 */
34
+	protected $_Primary_Datetime;
35
+
36
+	/**
37
+	 * @var EventSpacesCalculator $available_spaces_calculator
38
+	 */
39
+	protected $available_spaces_calculator;
40
+
41
+
42
+	/**
43
+	 * @param array $props_n_values incoming values
44
+	 * @param string $timezone incoming timezone (if not set the timezone set for the website will be
45
+	 *                                        used.)
46
+	 * @param array $date_formats incoming date_formats in an array where the first value is the
47
+	 *                                        date_format and the second value is the time format
48
+	 * @return EE_Event
49
+	 * @throws EE_Error
50
+	 */
51
+	public static function new_instance($props_n_values = array(), $timezone = null, $date_formats = array())
52
+	{
53
+		$has_object = parent::_check_for_object($props_n_values, __CLASS__, $timezone, $date_formats);
54
+		return $has_object ? $has_object : new self($props_n_values, false, $timezone, $date_formats);
55
+	}
56
+
57
+
58
+	/**
59
+	 * @param array $props_n_values incoming values from the database
60
+	 * @param string $timezone incoming timezone as set by the model.  If not set the timezone for
61
+	 *                                the website will be used.
62
+	 * @return EE_Event
63
+	 * @throws EE_Error
64
+	 */
65
+	public static function new_instance_from_db($props_n_values = array(), $timezone = null)
66
+	{
67
+		return new self($props_n_values, true, $timezone);
68
+	}
69
+
70
+
71
+
72
+	/**
73
+	 * @return EventSpacesCalculator
74
+	 * @throws \EE_Error
75
+	 */
76
+	public function getAvailableSpacesCalculator()
77
+	{
78
+		if(! $this->available_spaces_calculator instanceof EventSpacesCalculator){
79
+			$this->available_spaces_calculator = new EventSpacesCalculator($this);
80
+		}
81
+		return $this->available_spaces_calculator;
82
+	}
83
+
84
+
85
+
86
+	/**
87
+	 * Overrides parent set() method so that all calls to set( 'status', $status ) can be routed to internal methods
88
+	 *
89
+	 * @param string $field_name
90
+	 * @param mixed $field_value
91
+	 * @param bool $use_default
92
+	 * @throws EE_Error
93
+	 */
94
+	public function set($field_name, $field_value, $use_default = false)
95
+	{
96
+		switch ($field_name) {
97
+			case 'status' :
98
+				$this->set_status($field_value, $use_default);
99
+				break;
100
+			default :
101
+				parent::set($field_name, $field_value, $use_default);
102
+		}
103
+	}
104
+
105
+
106
+	/**
107
+	 *    set_status
108
+	 * Checks if event status is being changed to SOLD OUT
109
+	 * and updates event meta data with previous event status
110
+	 * so that we can revert things if/when the event is no longer sold out
111
+	 *
112
+	 * @access public
113
+	 * @param string $new_status
114
+	 * @param bool $use_default
115
+	 * @return void
116
+	 * @throws EE_Error
117
+	 */
118
+	public function set_status($new_status = null, $use_default = false)
119
+	{
120
+		// if nothing is set, and we aren't explicitly wanting to reset the status, then just leave
121
+		if (empty($new_status) && !$use_default) {
122
+			return;
123
+		}
124
+		// get current Event status
125
+		$old_status = $this->status();
126
+		// if status has changed
127
+		if ($old_status !== $new_status) {
128
+			// TO sold_out
129
+			if ($new_status === EEM_Event::sold_out) {
130
+				// save the previous event status so that we can revert if the event is no longer sold out
131
+				$this->add_post_meta('_previous_event_status', $old_status);
132
+				do_action('AHEE__EE_Event__set_status__to_sold_out', $this, $old_status, $new_status);
133
+				// OR FROM  sold_out
134
+			} else if ($old_status === EEM_Event::sold_out) {
135
+				$this->delete_post_meta('_previous_event_status');
136
+				do_action('AHEE__EE_Event__set_status__from_sold_out', $this, $old_status, $new_status);
137
+			}
138
+			// update status
139
+			parent::set('status', $new_status, $use_default);
140
+			do_action('AHEE__EE_Event__set_status__after_update', $this);
141
+			return;
142
+		}
143
+		// even though the old value matches the new value, it's still good to
144
+		// allow the parent set method to have a say
145
+		parent::set('status', $new_status, $use_default);
146
+	}
147
+
148
+
149
+	/**
150
+	 * Gets all the datetimes for this event
151
+	 *
152
+	 * @param array $query_params like EEM_Base::get_all
153
+	 * @return EE_Base_Class[]|EE_Datetime[]
154
+	 * @throws EE_Error
155
+	 */
156
+	public function datetimes($query_params = array())
157
+	{
158
+		return $this->get_many_related('Datetime', $query_params);
159
+	}
160
+
161
+
162
+	/**
163
+	 * Gets all the datetimes for this event, ordered by DTT_EVT_start in ascending order
164
+	 *
165
+	 * @return EE_Base_Class[]|EE_Datetime[]
166
+	 * @throws EE_Error
167
+	 */
168
+	public function datetimes_in_chronological_order()
169
+	{
170
+		return $this->get_many_related('Datetime', array('order_by' => array('DTT_EVT_start' => 'ASC')));
171
+	}
172
+
173
+
174
+	/**
175
+	 * Gets all the datetimes for this event, ordered by the DTT_order on the datetime.
176
+	 * @darren, we should probably UNSET timezone on the EEM_Datetime model
177
+	 * after running our query, so that this timezone isn't set for EVERY query
178
+	 * on EEM_Datetime for the rest of the request, no?
179
+	 *
180
+	 * @param boolean $show_expired whether or not to include expired events
181
+	 * @param boolean $show_deleted whether or not to include deleted events
182
+	 * @param null $limit
183
+	 * @return EE_Datetime[]
184
+	 * @throws EE_Error
185
+	 */
186
+	public function datetimes_ordered($show_expired = true, $show_deleted = false, $limit = null)
187
+	{
188
+		return EEM_Datetime::instance($this->_timezone)->get_datetimes_for_event_ordered_by_DTT_order(
189
+			$this->ID(),
190
+			$show_expired,
191
+			$show_deleted,
192
+			$limit
193
+		);
194
+	}
195
+
196
+
197
+	/**
198
+	 * Returns one related datetime. Mostly only used by some legacy code.
199
+	 *
200
+	 * @return EE_Base_Class|EE_Datetime
201
+	 * @throws EE_Error
202
+	 */
203
+	public function first_datetime()
204
+	{
205
+		return $this->get_first_related('Datetime');
206
+	}
207
+
208
+
209
+	/**
210
+	 * Returns the 'primary' datetime for the event
211
+	 *
212
+	 * @param bool $try_to_exclude_expired
213
+	 * @param bool $try_to_exclude_deleted
214
+	 * @return EE_Datetime
215
+	 * @throws EE_Error
216
+	 */
217
+	public function primary_datetime($try_to_exclude_expired = true, $try_to_exclude_deleted = true)
218
+	{
219
+		if (!empty ($this->_Primary_Datetime)) {
220
+			return $this->_Primary_Datetime;
221
+		}
222
+		$this->_Primary_Datetime = EEM_Datetime::instance($this->_timezone)->get_primary_datetime_for_event(
223
+			$this->ID(),
224
+			$try_to_exclude_expired,
225
+			$try_to_exclude_deleted
226
+		);
227
+		return $this->_Primary_Datetime;
228
+	}
229
+
230
+
231
+	/**
232
+	 * Gets all the tickets available for purchase of this event
233
+	 *
234
+	 * @param array $query_params like EEM_Base::get_all
235
+	 * @return EE_Base_Class[]|EE_Ticket[]
236
+	 * @throws EE_Error
237
+	 */
238
+	public function tickets($query_params = array())
239
+	{
240
+		//first get all datetimes
241
+		$datetimes = $this->datetimes_ordered();
242
+		if (!$datetimes) {
243
+			return array();
244
+		}
245
+		$datetime_ids = array();
246
+		foreach ($datetimes as $datetime) {
247
+			$datetime_ids[] = $datetime->ID();
248
+		}
249
+		$where_params = array('Datetime.DTT_ID' => array('IN', $datetime_ids));
250
+		//if incoming $query_params has where conditions let's merge but not override existing.
251
+		if (is_array($query_params) && isset($query_params[0])) {
252
+			$where_params = array_merge($query_params[0], $where_params);
253
+			unset($query_params[0]);
254
+		}
255
+		//now add $where_params to $query_params
256
+		$query_params[0] = $where_params;
257
+		return EEM_Ticket::instance()->get_all($query_params);
258
+	}
259
+
260
+
261
+	/**
262
+	 * get all unexpired untrashed tickets
263
+	 *
264
+	 * @return EE_Ticket[]
265
+	 * @throws EE_Error
266
+	 */
267
+	public function active_tickets()
268
+	{
269
+		return $this->tickets(array(
270
+			array(
271
+				'TKT_end_date' => array('>=', EEM_Ticket::instance()->current_time_for_query('TKT_end_date')),
272
+				'TKT_deleted' => false,
273
+			),
274
+		));
275
+	}
276
+
277
+
278
+	/**
279
+	 * @return bool
280
+	 * @throws EE_Error
281
+	 */
282
+	public function additional_limit()
283
+	{
284
+		return $this->get('EVT_additional_limit');
285
+	}
286
+
287
+
288
+	/**
289
+	 * @return bool
290
+	 * @throws EE_Error
291
+	 */
292
+	public function allow_overflow()
293
+	{
294
+		return $this->get('EVT_allow_overflow');
295
+	}
296
+
297
+
298
+	/**
299
+	 * @return bool
300
+	 * @throws EE_Error
301
+	 */
302
+	public function created()
303
+	{
304
+		return $this->get('EVT_created');
305
+	}
306
+
307
+
308
+	/**
309
+	 * @return bool
310
+	 * @throws EE_Error
311
+	 */
312
+	public function description()
313
+	{
314
+		return $this->get('EVT_desc');
315
+	}
316
+
317
+
318
+	/**
319
+	 * Runs do_shortcode and wpautop on the description
320
+	 *
321
+	 * @return string of html
322
+	 * @throws EE_Error
323
+	 */
324
+	public function description_filtered()
325
+	{
326
+		return $this->get_pretty('EVT_desc');
327
+	}
328
+
329
+
330
+	/**
331
+	 * @return bool
332
+	 * @throws EE_Error
333
+	 */
334
+	public function display_description()
335
+	{
336
+		return $this->get('EVT_display_desc');
337
+	}
338
+
339
+
340
+	/**
341
+	 * @return bool
342
+	 * @throws EE_Error
343
+	 */
344
+	public function display_ticket_selector()
345
+	{
346
+		return (bool)$this->get('EVT_display_ticket_selector');
347
+	}
348
+
349
+
350
+	/**
351
+	 * @return bool
352
+	 * @throws EE_Error
353
+	 */
354
+	public function external_url()
355
+	{
356
+		return $this->get('EVT_external_URL');
357
+	}
358
+
359
+
360
+	/**
361
+	 * @return bool
362
+	 * @throws EE_Error
363
+	 */
364
+	public function member_only()
365
+	{
366
+		return $this->get('EVT_member_only');
367
+	}
368
+
369
+
370
+	/**
371
+	 * @return bool
372
+	 * @throws EE_Error
373
+	 */
374
+	public function phone()
375
+	{
376
+		return $this->get('EVT_phone');
377
+	}
378
+
379
+
380
+	/**
381
+	 * @return bool
382
+	 * @throws EE_Error
383
+	 */
384
+	public function modified()
385
+	{
386
+		return $this->get('EVT_modified');
387
+	}
388
+
389
+
390
+	/**
391
+	 * @return bool
392
+	 * @throws EE_Error
393
+	 */
394
+	public function name()
395
+	{
396
+		return $this->get('EVT_name');
397
+	}
398
+
399
+
400
+	/**
401
+	 * @return bool
402
+	 * @throws EE_Error
403
+	 */
404
+	public function order()
405
+	{
406
+		return $this->get('EVT_order');
407
+	}
408
+
409
+
410
+	/**
411
+	 * @return bool|string
412
+	 * @throws EE_Error
413
+	 */
414
+	public function default_registration_status()
415
+	{
416
+		$event_default_registration_status = $this->get('EVT_default_registration_status');
417
+		return !empty($event_default_registration_status)
418
+			? $event_default_registration_status
419
+			: EE_Registry::instance()->CFG->registration->default_STS_ID;
420
+	}
421
+
422
+
423
+	/**
424
+	 * @param int $num_words
425
+	 * @param null $more
426
+	 * @param bool $not_full_desc
427
+	 * @return bool|string
428
+	 * @throws EE_Error
429
+	 */
430
+	public function short_description($num_words = 55, $more = null, $not_full_desc = false)
431
+	{
432
+		$short_desc = $this->get('EVT_short_desc');
433
+		if (!empty($short_desc) || $not_full_desc) {
434
+			return $short_desc;
435
+		}
436
+		$full_desc = $this->get('EVT_desc');
437
+		return wp_trim_words($full_desc, $num_words, $more);
438
+	}
439
+
440
+
441
+	/**
442
+	 * @return bool
443
+	 * @throws EE_Error
444
+	 */
445
+	public function slug()
446
+	{
447
+		return $this->get('EVT_slug');
448
+	}
449
+
450
+
451
+	/**
452
+	 * @return bool
453
+	 * @throws EE_Error
454
+	 */
455
+	public function timezone_string()
456
+	{
457
+		return $this->get('EVT_timezone_string');
458
+	}
459
+
460
+
461
+	/**
462
+	 * @return bool
463
+	 * @throws EE_Error
464
+	 */
465
+	public function visible_on()
466
+	{
467
+		return $this->get('EVT_visible_on');
468
+	}
469
+
470
+
471
+	/**
472
+	 * @return int
473
+	 * @throws EE_Error
474
+	 */
475
+	public function wp_user()
476
+	{
477
+		return $this->get('EVT_wp_user');
478
+	}
479
+
480
+
481
+	/**
482
+	 * @return bool
483
+	 * @throws EE_Error
484
+	 */
485
+	public function donations()
486
+	{
487
+		return $this->get('EVT_donations');
488
+	}
489
+
490
+
491
+	/**
492
+	 * @param $limit
493
+	 * @throws EE_Error
494
+	 */
495
+	public function set_additional_limit($limit)
496
+	{
497
+		$this->set('EVT_additional_limit', $limit);
498
+	}
499
+
500
+
501
+	/**
502
+	 * @param $created
503
+	 * @throws EE_Error
504
+	 */
505
+	public function set_created($created)
506
+	{
507
+		$this->set('EVT_created', $created);
508
+	}
509
+
510
+
511
+	/**
512
+	 * @param $desc
513
+	 * @throws EE_Error
514
+	 */
515
+	public function set_description($desc)
516
+	{
517
+		$this->set('EVT_desc', $desc);
518
+	}
519
+
520
+
521
+	/**
522
+	 * @param $display_desc
523
+	 * @throws EE_Error
524
+	 */
525
+	public function set_display_description($display_desc)
526
+	{
527
+		$this->set('EVT_display_desc', $display_desc);
528
+	}
529
+
530
+
531
+	/**
532
+	 * @param $display_ticket_selector
533
+	 * @throws EE_Error
534
+	 */
535
+	public function set_display_ticket_selector($display_ticket_selector)
536
+	{
537
+		$this->set('EVT_display_ticket_selector', $display_ticket_selector);
538
+	}
539
+
540
+
541
+	/**
542
+	 * @param $external_url
543
+	 * @throws EE_Error
544
+	 */
545
+	public function set_external_url($external_url)
546
+	{
547
+		$this->set('EVT_external_URL', $external_url);
548
+	}
549
+
550
+
551
+	/**
552
+	 * @param $member_only
553
+	 * @throws EE_Error
554
+	 */
555
+	public function set_member_only($member_only)
556
+	{
557
+		$this->set('EVT_member_only', $member_only);
558
+	}
559
+
560
+
561
+	/**
562
+	 * @param $event_phone
563
+	 * @throws EE_Error
564
+	 */
565
+	public function set_event_phone($event_phone)
566
+	{
567
+		$this->set('EVT_phone', $event_phone);
568
+	}
569
+
570
+
571
+	/**
572
+	 * @param $modified
573
+	 * @throws EE_Error
574
+	 */
575
+	public function set_modified($modified)
576
+	{
577
+		$this->set('EVT_modified', $modified);
578
+	}
579
+
580
+
581
+	/**
582
+	 * @param $name
583
+	 * @throws EE_Error
584
+	 */
585
+	public function set_name($name)
586
+	{
587
+		$this->set('EVT_name', $name);
588
+	}
589
+
590
+
591
+	/**
592
+	 * @param $order
593
+	 * @throws EE_Error
594
+	 */
595
+	public function set_order($order)
596
+	{
597
+		$this->set('EVT_order', $order);
598
+	}
599
+
600
+
601
+	/**
602
+	 * @param $short_desc
603
+	 * @throws EE_Error
604
+	 */
605
+	public function set_short_description($short_desc)
606
+	{
607
+		$this->set('EVT_short_desc', $short_desc);
608
+	}
609
+
610
+
611
+	/**
612
+	 * @param $slug
613
+	 * @throws EE_Error
614
+	 */
615
+	public function set_slug($slug)
616
+	{
617
+		$this->set('EVT_slug', $slug);
618
+	}
619
+
620
+
621
+	/**
622
+	 * @param $timezone_string
623
+	 * @throws EE_Error
624
+	 */
625
+	public function set_timezone_string($timezone_string)
626
+	{
627
+		$this->set('EVT_timezone_string', $timezone_string);
628
+	}
629
+
630
+
631
+	/**
632
+	 * @param $visible_on
633
+	 * @throws EE_Error
634
+	 */
635
+	public function set_visible_on($visible_on)
636
+	{
637
+		$this->set('EVT_visible_on', $visible_on);
638
+	}
639
+
640
+
641
+	/**
642
+	 * @param $wp_user
643
+	 * @throws EE_Error
644
+	 */
645
+	public function set_wp_user($wp_user)
646
+	{
647
+		$this->set('EVT_wp_user', $wp_user);
648
+	}
649
+
650
+
651
+	/**
652
+	 * @param $default_registration_status
653
+	 * @throws EE_Error
654
+	 */
655
+	public function set_default_registration_status($default_registration_status)
656
+	{
657
+		$this->set('EVT_default_registration_status', $default_registration_status);
658
+	}
659
+
660
+
661
+	/**
662
+	 * @param $donations
663
+	 * @throws EE_Error
664
+	 */
665
+	public function set_donations($donations)
666
+	{
667
+		$this->set('EVT_donations', $donations);
668
+	}
669
+
670
+
671
+	/**
672
+	 * Adds a venue to this event
673
+	 *
674
+	 * @param EE_Venue /int $venue_id_or_obj
675
+	 * @return EE_Base_Class|EE_Venue
676
+	 * @throws EE_Error
677
+	 */
678
+	public function add_venue($venue_id_or_obj)
679
+	{
680
+		return $this->_add_relation_to($venue_id_or_obj, 'Venue');
681
+	}
682
+
683
+
684
+	/**
685
+	 * Removes a venue from the event
686
+	 *
687
+	 * @param EE_Venue /int $venue_id_or_obj
688
+	 * @return EE_Base_Class|EE_Venue
689
+	 * @throws EE_Error
690
+	 */
691
+	public function remove_venue($venue_id_or_obj)
692
+	{
693
+		return $this->_remove_relation_to($venue_id_or_obj, 'Venue');
694
+	}
695
+
696
+
697
+	/**
698
+	 * Gets all the venues related ot the event. May provide additional $query_params if desired
699
+	 *
700
+	 * @param array $query_params like EEM_Base::get_all's $query_params
701
+	 * @return EE_Base_Class[]|EE_Venue[]
702
+	 * @throws EE_Error
703
+	 */
704
+	public function venues($query_params = array())
705
+	{
706
+		return $this->get_many_related('Venue', $query_params);
707
+	}
708
+
709
+
710
+	/**
711
+	 * check if event id is present and if event is published
712
+	 *
713
+	 * @access public
714
+	 * @return boolean true yes, false no
715
+	 * @throws EE_Error
716
+	 */
717
+	private function _has_ID_and_is_published()
718
+	{
719
+		// first check if event id is present and not NULL,
720
+		// then check if this event is published (or any of the equivalent "published" statuses)
721
+		return
722
+			$this->ID() && $this->ID() !== null
723
+			&& (
724
+				$this->status() === 'publish'
725
+				|| $this->status() === EEM_Event::sold_out
726
+				|| $this->status() === EEM_Event::postponed
727
+				|| $this->status() === EEM_Event::cancelled
728
+			);
729
+	}
730
+
731
+
732
+	/**
733
+	 * This simply compares the internal dates with NOW and determines if the event is upcoming or not.
734
+	 *
735
+	 * @access public
736
+	 * @return boolean true yes, false no
737
+	 * @throws EE_Error
738
+	 */
739
+	public function is_upcoming()
740
+	{
741
+		// check if event id is present and if this event is published
742
+		if ($this->is_inactive()) {
743
+			return false;
744
+		}
745
+		// set initial value
746
+		$upcoming = false;
747
+		//next let's get all datetimes and loop through them
748
+		$datetimes = $this->datetimes_in_chronological_order();
749
+		foreach ($datetimes as $datetime) {
750
+			if ($datetime instanceof EE_Datetime) {
751
+				//if this dtt is expired then we continue cause one of the other datetimes might be upcoming.
752
+				if ($datetime->is_expired()) {
753
+					continue;
754
+				}
755
+				//if this dtt is active then we return false.
756
+				if ($datetime->is_active()) {
757
+					return false;
758
+				}
759
+				//otherwise let's check upcoming status
760
+				$upcoming = $datetime->is_upcoming();
761
+			}
762
+		}
763
+		return $upcoming;
764
+	}
765
+
766
+
767
+	/**
768
+	 * @return bool
769
+	 * @throws EE_Error
770
+	 */
771
+	public function is_active()
772
+	{
773
+		// check if event id is present and if this event is published
774
+		if ($this->is_inactive()) {
775
+			return false;
776
+		}
777
+		// set initial value
778
+		$active = false;
779
+		//next let's get all datetimes and loop through them
780
+		$datetimes = $this->datetimes_in_chronological_order();
781
+		foreach ($datetimes as $datetime) {
782
+			if ($datetime instanceof EE_Datetime) {
783
+				//if this dtt is expired then we continue cause one of the other datetimes might be active.
784
+				if ($datetime->is_expired()) {
785
+					continue;
786
+				}
787
+				//if this dtt is upcoming then we return false.
788
+				if ($datetime->is_upcoming()) {
789
+					return false;
790
+				}
791
+				//otherwise let's check active status
792
+				$active = $datetime->is_active();
793
+			}
794
+		}
795
+		return $active;
796
+	}
797
+
798
+
799
+	/**
800
+	 * @return bool
801
+	 * @throws EE_Error
802
+	 */
803
+	public function is_expired()
804
+	{
805
+		// check if event id is present and if this event is published
806
+		if ($this->is_inactive()) {
807
+			return false;
808
+		}
809
+		// set initial value
810
+		$expired = false;
811
+		//first let's get all datetimes and loop through them
812
+		$datetimes = $this->datetimes_in_chronological_order();
813
+		foreach ($datetimes as $datetime) {
814
+			if ($datetime instanceof EE_Datetime) {
815
+				//if this dtt is upcoming or active then we return false.
816
+				if ($datetime->is_upcoming() || $datetime->is_active()) {
817
+					return false;
818
+				}
819
+				//otherwise let's check active status
820
+				$expired = $datetime->is_expired();
821
+			}
822
+		}
823
+		return $expired;
824
+	}
825
+
826
+
827
+	/**
828
+	 * @return bool
829
+	 * @throws EE_Error
830
+	 */
831
+	public function is_inactive()
832
+	{
833
+		// check if event id is present and if this event is published
834
+		if ($this->_has_ID_and_is_published()) {
835
+			return false;
836
+		}
837
+		return true;
838
+	}
839
+
840
+
841
+	/**
842
+	 * calculate spaces remaining based on "saleable" tickets
843
+	 *
844
+	 * @param array $tickets
845
+	 * @param bool $filtered
846
+	 * @return int|float
847
+	 * @throws EE_Error
848
+	 */
849
+	public function spaces_remaining($tickets = array(), $filtered = true)
850
+	{
851
+		$this->getAvailableSpacesCalculator()->setActiveTickets($tickets);
852
+		$spaces_remaining = $this->getAvailableSpacesCalculator()->spacesRemaining();
853
+		return $filtered
854
+			? apply_filters(
855
+				'FHEE_EE_Event__spaces_remaining',
856
+				$spaces_remaining,
857
+				$this,
858
+				$tickets
859
+			)
860
+			: $spaces_remaining;
861
+	}
862
+
863
+
864
+	/**
865
+	 *    perform_sold_out_status_check
866
+	 *    checks all of this events's datetime  reg_limit - sold values to determine if ANY datetimes have spaces available...
867
+	 *    if NOT, then the event status will get toggled to 'sold_out'
868
+	 *
869
+	 * @access public
870
+	 * @return bool    return the ACTUAL sold out state.
871
+	 * @throws EE_Error
872
+	 */
873
+	public function perform_sold_out_status_check()
874
+	{
875
+		// get all unexpired untrashed tickets
876
+		$tickets = $this->active_tickets();
877
+		// if all the tickets are just expired, then don't update the event status to sold out
878
+		if (empty($tickets)) {
879
+			return true;
880
+		}
881
+		$spaces_remaining = $this->spaces_remaining($tickets);
882
+		if ($spaces_remaining < 1) {
883
+			$this->set_status(EEM_Event::sold_out);
884
+			$this->save();
885
+			$sold_out = true;
886
+		} else {
887
+			$sold_out = false;
888
+			// was event previously marked as sold out ?
889
+			if ($this->status() === EEM_Event::sold_out) {
890
+				// revert status to previous value, if it was set
891
+				$previous_event_status = $this->get_post_meta('_previous_event_status', true);
892
+				if ($previous_event_status) {
893
+					$this->set_status($previous_event_status);
894
+					$this->save();
895
+				}
896
+			}
897
+		}
898
+		do_action('AHEE__EE_Event__perform_sold_out_status_check__end', $this, $sold_out, $spaces_remaining, $tickets);
899
+		return $sold_out;
900
+	}
901
+
902
+
903
+
904
+	/**
905
+	 * This returns the total remaining spaces for sale on this event.
906
+	 *
907
+	 * @uses EE_Event::total_available_spaces()
908
+	 * @return float|int
909
+	 * @throws EE_Error
910
+	 */
911
+	public function spaces_remaining_for_sale()
912
+	{
913
+		return $this->total_available_spaces(true);
914
+	}
915
+
916
+
917
+
918
+	/**
919
+	 * This returns the total spaces available for an event
920
+	 * while considering all the qtys on the tickets and the reg limits
921
+	 * on the datetimes attached to this event.
922
+	 *
923
+	 * @param   bool $consider_sold Whether to consider any tickets that have already sold in our calculation.
924
+	 *                              If this is false, then we return the most tickets that could ever be sold
925
+	 *                              for this event with the datetime and tickets setup on the event under optimal
926
+	 *                              selling conditions.  Otherwise we return a live calculation of spaces available
927
+	 *                              based on tickets sold.  Depending on setup and stage of sales, this
928
+	 *                              may appear to equal remaining tickets.  However, the more tickets are
929
+	 *                              sold out, the more accurate the "live" total is.
930
+	 * @return  int|float  (Note: if EE_INF is returned its considered a float by PHP)
931
+	 * @throws EE_Error
932
+	 */
933
+	public function total_available_spaces($consider_sold = false)
934
+	{
935
+		$spaces_available = $consider_sold
936
+			? $this->getAvailableSpacesCalculator()->spacesRemaining()
937
+			: $this->getAvailableSpacesCalculator()->totalSpacesAvailable();
938
+		return apply_filters(
939
+			'FHEE_EE_Event__total_available_spaces__spaces_available',
940
+			$spaces_available,
941
+			$this,
942
+			$this->getAvailableSpacesCalculator()->getDatetimes(),
943
+			$this->getAvailableSpacesCalculator()->getActiveTickets()
944
+		);
945
+	}
946
+
947
+
948
+	/**
949
+	 * Checks if the event is set to sold out
950
+	 *
951
+	 * @param  bool $actual whether or not to perform calculations to not only figure the
952
+	 *                      actual status but also to flip the status if necessary to sold
953
+	 *                      out If false, we just check the existing status of the event
954
+	 * @return boolean
955
+	 * @throws EE_Error
956
+	 */
957
+	public function is_sold_out($actual = false)
958
+	{
959
+		if (!$actual) {
960
+			return $this->status() === EEM_Event::sold_out;
961
+		}
962
+		return $this->perform_sold_out_status_check();
963
+	}
964
+
965
+
966
+	/**
967
+	 * Checks if the event is marked as postponed
968
+	 *
969
+	 * @return boolean
970
+	 */
971
+	public function is_postponed()
972
+	{
973
+		return $this->status() === EEM_Event::postponed;
974
+	}
975
+
976
+
977
+	/**
978
+	 * Checks if the event is marked as cancelled
979
+	 *
980
+	 * @return boolean
981
+	 */
982
+	public function is_cancelled()
983
+	{
984
+		return $this->status() === EEM_Event::cancelled;
985
+	}
986
+
987
+
988
+	/**
989
+	 * Get the logical active status in a hierarchical order for all the datetimes.  Note
990
+	 * Basically, we order the datetimes by EVT_start_date.  Then first test on whether the event is published.  If its
991
+	 * NOT published then we test for whether its expired or not.  IF it IS published then we test first on whether an
992
+	 * event has any active dates.  If no active dates then we check for any upcoming dates.  If no upcoming dates then
993
+	 * the event is considered expired.
994
+	 * NOTE: this method does NOT calculate whether the datetimes are sold out when event is published.  Sold Out is a status
995
+	 * set on the EVENT when it is not published and thus is done
996
+	 *
997
+	 * @param bool $reset
998
+	 * @return bool | string - based on EE_Datetime active constants or FALSE if error.
999
+	 * @throws EE_Error
1000
+	 */
1001
+	public function get_active_status($reset = false)
1002
+	{
1003
+		// if the active status has already been set, then just use that value (unless we are resetting it)
1004
+		if (!empty($this->_active_status) && !$reset) {
1005
+			return $this->_active_status;
1006
+		}
1007
+		//first check if event id is present on this object
1008
+		if (!$this->ID()) {
1009
+			return false;
1010
+		}
1011
+		$where_params_for_event = array(array('EVT_ID' => $this->ID()));
1012
+		//if event is published:
1013
+		if ($this->status() === 'publish') {
1014
+			//active?
1015
+			if (EEM_Datetime::instance()->get_datetime_count_for_status(EE_Datetime::active, $where_params_for_event) > 0) {
1016
+				$this->_active_status = EE_Datetime::active;
1017
+			} else {
1018
+				//upcoming?
1019
+				if (EEM_Datetime::instance()->get_datetime_count_for_status(EE_Datetime::upcoming, $where_params_for_event) > 0) {
1020
+					$this->_active_status = EE_Datetime::upcoming;
1021
+				} else {
1022
+					//expired?
1023
+					if (
1024
+						EEM_Datetime::instance()->get_datetime_count_for_status(EE_Datetime::expired, $where_params_for_event) > 0
1025
+					) {
1026
+						$this->_active_status = EE_Datetime::expired;
1027
+					} else {
1028
+						//it would be odd if things make it this far because it basically means there are no datetime's
1029
+						//attached to the event.  So in this case it will just be considered inactive.
1030
+						$this->_active_status = EE_Datetime::inactive;
1031
+					}
1032
+				}
1033
+			}
1034
+		} else {
1035
+			//the event is not published, so let's just set it's active status according to its' post status
1036
+			switch ($this->status()) {
1037
+				case EEM_Event::sold_out :
1038
+					$this->_active_status = EE_Datetime::sold_out;
1039
+					break;
1040
+				case EEM_Event::cancelled :
1041
+					$this->_active_status = EE_Datetime::cancelled;
1042
+					break;
1043
+				case EEM_Event::postponed :
1044
+					$this->_active_status = EE_Datetime::postponed;
1045
+					break;
1046
+				default :
1047
+					$this->_active_status = EE_Datetime::inactive;
1048
+			}
1049
+		}
1050
+		return $this->_active_status;
1051
+	}
1052
+
1053
+
1054
+	/**
1055
+	 *    pretty_active_status
1056
+	 *
1057
+	 * @access public
1058
+	 * @param boolean $echo whether to return (FALSE), or echo out the result (TRUE)
1059
+	 * @return mixed void|string
1060
+	 * @throws EE_Error
1061
+	 */
1062
+	public function pretty_active_status($echo = true)
1063
+	{
1064
+		$active_status = $this->get_active_status();
1065
+		$status = '<span class="ee-status event-active-status-'
1066
+			. $active_status
1067
+			. '">'
1068
+			. EEH_Template::pretty_status($active_status, false, 'sentence')
1069
+			. '</span>';
1070
+		if ($echo) {
1071
+			echo $status;
1072
+			return '';
1073
+		}
1074
+		return $status;
1075
+	}
1076
+
1077
+
1078
+	/**
1079
+	 * @return bool|int
1080
+	 * @throws EE_Error
1081
+	 */
1082
+	public function get_number_of_tickets_sold()
1083
+	{
1084
+		$tkt_sold = 0;
1085
+		if (!$this->ID()) {
1086
+			return 0;
1087
+		}
1088
+		$datetimes = $this->datetimes();
1089
+		foreach ($datetimes as $datetime) {
1090
+			if ($datetime instanceof EE_Datetime) {
1091
+				$tkt_sold += $datetime->sold();
1092
+			}
1093
+		}
1094
+		return $tkt_sold;
1095
+	}
1096
+
1097
+
1098
+	/**
1099
+	 * This just returns a count of all the registrations for this event
1100
+	 *
1101
+	 * @access  public
1102
+	 * @return int
1103
+	 * @throws EE_Error
1104
+	 */
1105
+	public function get_count_of_all_registrations()
1106
+	{
1107
+		return EEM_Event::instance()->count_related($this, 'Registration');
1108
+	}
1109
+
1110
+
1111
+	/**
1112
+	 * This returns the ticket with the earliest start time that is
1113
+	 * available for this event (across all datetimes attached to the event)
1114
+	 *
1115
+	 * @return EE_Base_Class|EE_Ticket|null
1116
+	 * @throws EE_Error
1117
+	 */
1118
+	public function get_ticket_with_earliest_start_time()
1119
+	{
1120
+		$where['Datetime.EVT_ID'] = $this->ID();
1121
+		$query_params = array($where, 'order_by' => array('TKT_start_date' => 'ASC'));
1122
+		return EE_Registry::instance()->load_model('Ticket')->get_one($query_params);
1123
+	}
1124
+
1125
+
1126
+	/**
1127
+	 * This returns the ticket with the latest end time that is available
1128
+	 * for this event (across all datetimes attached to the event)
1129
+	 *
1130
+	 * @return EE_Base_Class|EE_Ticket|null
1131
+	 * @throws EE_Error
1132
+	 */
1133
+	public function get_ticket_with_latest_end_time()
1134
+	{
1135
+		$where['Datetime.EVT_ID'] = $this->ID();
1136
+		$query_params = array($where, 'order_by' => array('TKT_end_date' => 'DESC'));
1137
+		return EE_Registry::instance()->load_model('Ticket')->get_one($query_params);
1138
+	}
1139
+
1140
+
1141
+	/**
1142
+	 * This returns whether there are any tickets on sale for this event.
1143
+	 *
1144
+	 * @return bool true = YES tickets on sale.
1145
+	 * @throws EE_Error
1146
+	 */
1147
+	public function tickets_on_sale()
1148
+	{
1149
+		$earliest_ticket = $this->get_ticket_with_earliest_start_time();
1150
+		$latest_ticket = $this->get_ticket_with_latest_end_time();
1151
+		if (!$latest_ticket instanceof EE_Ticket && !$earliest_ticket instanceof EE_Ticket) {
1152
+			return false;
1153
+		}
1154
+		//check on sale for these two tickets.
1155
+		if ($latest_ticket->is_on_sale() || $earliest_ticket->is_on_sale()) {
1156
+			return true;
1157
+		}
1158
+		return false;
1159
+	}
1160
+
1161
+
1162
+	/**
1163
+	 * Gets the URL for viewing this event on the front-end. Overrides parent
1164
+	 * to check for an external URL first
1165
+	 *
1166
+	 * @return string
1167
+	 * @throws EE_Error
1168
+	 */
1169
+	public function get_permalink()
1170
+	{
1171
+		if ($this->external_url()) {
1172
+			return $this->external_url();
1173
+		}
1174
+		return parent::get_permalink();
1175
+	}
1176
+
1177
+
1178
+	/**
1179
+	 * Gets the first term for 'espresso_event_categories' we can find
1180
+	 *
1181
+	 * @param array $query_params like EEM_Base::get_all
1182
+	 * @return EE_Base_Class|EE_Term|null
1183
+	 * @throws EE_Error
1184
+	 */
1185
+	public function first_event_category($query_params = array())
1186
+	{
1187
+		$query_params[0]['Term_Taxonomy.taxonomy'] = 'espresso_event_categories';
1188
+		$query_params[0]['Term_Taxonomy.Event.EVT_ID'] = $this->ID();
1189
+		return EEM_Term::instance()->get_one($query_params);
1190
+	}
1191
+
1192
+
1193
+	/**
1194
+	 * Gets all terms for 'espresso_event_categories' we can find
1195
+	 *
1196
+	 * @param array $query_params
1197
+	 * @return EE_Base_Class[]|EE_Term[]
1198
+	 * @throws EE_Error
1199
+	 */
1200
+	public function get_all_event_categories($query_params = array())
1201
+	{
1202
+		$query_params[0]['Term_Taxonomy.taxonomy'] = 'espresso_event_categories';
1203
+		$query_params[0]['Term_Taxonomy.Event.EVT_ID'] = $this->ID();
1204
+		return EEM_Term::instance()->get_all($query_params);
1205
+	}
1206
+
1207
+
1208
+	/**
1209
+	 * Gets all the question groups, ordering them by QSG_order ascending
1210
+	 *
1211
+	 * @param array $query_params @see EEM_Base::get_all
1212
+	 * @return EE_Base_Class[]|EE_Question_Group[]
1213
+	 * @throws EE_Error
1214
+	 */
1215
+	public function question_groups($query_params = array())
1216
+	{
1217
+		$query_params = !empty($query_params) ? $query_params : array('order_by' => array('QSG_order' => 'ASC'));
1218
+		return $this->get_many_related('Question_Group', $query_params);
1219
+	}
1220
+
1221
+
1222
+	/**
1223
+	 * Implementation for EEI_Has_Icon interface method.
1224
+	 *
1225
+	 * @see EEI_Visual_Representation for comments
1226
+	 * @return string
1227
+	 */
1228
+	public function get_icon()
1229
+	{
1230
+		return '<span class="dashicons dashicons-flag"></span>';
1231
+	}
1232
+
1233
+
1234
+	/**
1235
+	 * Implementation for EEI_Admin_Links interface method.
1236
+	 *
1237
+	 * @see EEI_Admin_Links for comments
1238
+	 * @return string
1239
+	 * @throws EE_Error
1240
+	 */
1241
+	public function get_admin_details_link()
1242
+	{
1243
+		return $this->get_admin_edit_link();
1244
+	}
1245
+
1246
+
1247
+	/**
1248
+	 * Implementation for EEI_Admin_Links interface method.
1249
+	 *
1250
+	 * @see EEI_Admin_Links for comments
1251
+	 * @return string
1252
+	 * @throws EE_Error
1253
+	 */
1254
+	public function get_admin_edit_link()
1255
+	{
1256
+		return EEH_URL::add_query_args_and_nonce(array(
1257
+			'page' => 'espresso_events',
1258
+			'action' => 'edit',
1259
+			'post' => $this->ID(),
1260
+		),
1261
+			admin_url('admin.php')
1262
+		);
1263
+	}
1264
+
1265
+
1266
+	/**
1267
+	 * Implementation for EEI_Admin_Links interface method.
1268
+	 *
1269
+	 * @see EEI_Admin_Links for comments
1270
+	 * @return string
1271
+	 */
1272
+	public function get_admin_settings_link()
1273
+	{
1274
+		return EEH_URL::add_query_args_and_nonce(array(
1275
+			'page' => 'espresso_events',
1276
+			'action' => 'default_event_settings',
1277
+		),
1278
+			admin_url('admin.php')
1279
+		);
1280
+	}
1281
+
1282
+
1283
+	/**
1284
+	 * Implementation for EEI_Admin_Links interface method.
1285
+	 *
1286
+	 * @see EEI_Admin_Links for comments
1287
+	 * @return string
1288
+	 */
1289
+	public function get_admin_overview_link()
1290
+	{
1291
+		return EEH_URL::add_query_args_and_nonce(array(
1292
+			'page' => 'espresso_events',
1293
+			'action' => 'default',
1294
+		),
1295
+			admin_url('admin.php')
1296
+		);
1297
+	}
1298 1298
 
1299 1299
 }
Please login to merge, or discard this patch.
Spacing   +14 added lines, -14 removed lines patch added patch discarded remove patch
@@ -3,7 +3,7 @@  discard block
 block discarded – undo
3 3
 use EventEspresso\core\domain\services\event\EventSpacesCalculator;
4 4
 
5 5
 
6
-if (!defined('EVENT_ESPRESSO_VERSION')) {
6
+if ( ! defined('EVENT_ESPRESSO_VERSION')) {
7 7
     exit('No direct script access allowed');
8 8
 }
9 9
 
@@ -75,7 +75,7 @@  discard block
 block discarded – undo
75 75
      */
76 76
     public function getAvailableSpacesCalculator()
77 77
     {
78
-        if(! $this->available_spaces_calculator instanceof EventSpacesCalculator){
78
+        if ( ! $this->available_spaces_calculator instanceof EventSpacesCalculator) {
79 79
             $this->available_spaces_calculator = new EventSpacesCalculator($this);
80 80
         }
81 81
         return $this->available_spaces_calculator;
@@ -118,7 +118,7 @@  discard block
 block discarded – undo
118 118
     public function set_status($new_status = null, $use_default = false)
119 119
     {
120 120
         // if nothing is set, and we aren't explicitly wanting to reset the status, then just leave
121
-        if (empty($new_status) && !$use_default) {
121
+        if (empty($new_status) && ! $use_default) {
122 122
             return;
123 123
         }
124 124
         // get current Event status
@@ -216,7 +216,7 @@  discard block
 block discarded – undo
216 216
      */
217 217
     public function primary_datetime($try_to_exclude_expired = true, $try_to_exclude_deleted = true)
218 218
     {
219
-        if (!empty ($this->_Primary_Datetime)) {
219
+        if ( ! empty ($this->_Primary_Datetime)) {
220 220
             return $this->_Primary_Datetime;
221 221
         }
222 222
         $this->_Primary_Datetime = EEM_Datetime::instance($this->_timezone)->get_primary_datetime_for_event(
@@ -239,7 +239,7 @@  discard block
 block discarded – undo
239 239
     {
240 240
         //first get all datetimes
241 241
         $datetimes = $this->datetimes_ordered();
242
-        if (!$datetimes) {
242
+        if ( ! $datetimes) {
243 243
             return array();
244 244
         }
245 245
         $datetime_ids = array();
@@ -343,7 +343,7 @@  discard block
 block discarded – undo
343 343
      */
344 344
     public function display_ticket_selector()
345 345
     {
346
-        return (bool)$this->get('EVT_display_ticket_selector');
346
+        return (bool) $this->get('EVT_display_ticket_selector');
347 347
     }
348 348
 
349 349
 
@@ -414,7 +414,7 @@  discard block
 block discarded – undo
414 414
     public function default_registration_status()
415 415
     {
416 416
         $event_default_registration_status = $this->get('EVT_default_registration_status');
417
-        return !empty($event_default_registration_status)
417
+        return ! empty($event_default_registration_status)
418 418
             ? $event_default_registration_status
419 419
             : EE_Registry::instance()->CFG->registration->default_STS_ID;
420 420
     }
@@ -430,7 +430,7 @@  discard block
 block discarded – undo
430 430
     public function short_description($num_words = 55, $more = null, $not_full_desc = false)
431 431
     {
432 432
         $short_desc = $this->get('EVT_short_desc');
433
-        if (!empty($short_desc) || $not_full_desc) {
433
+        if ( ! empty($short_desc) || $not_full_desc) {
434 434
             return $short_desc;
435 435
         }
436 436
         $full_desc = $this->get('EVT_desc');
@@ -956,7 +956,7 @@  discard block
 block discarded – undo
956 956
      */
957 957
     public function is_sold_out($actual = false)
958 958
     {
959
-        if (!$actual) {
959
+        if ( ! $actual) {
960 960
             return $this->status() === EEM_Event::sold_out;
961 961
         }
962 962
         return $this->perform_sold_out_status_check();
@@ -1001,11 +1001,11 @@  discard block
 block discarded – undo
1001 1001
     public function get_active_status($reset = false)
1002 1002
     {
1003 1003
         // if the active status has already been set, then just use that value (unless we are resetting it)
1004
-        if (!empty($this->_active_status) && !$reset) {
1004
+        if ( ! empty($this->_active_status) && ! $reset) {
1005 1005
             return $this->_active_status;
1006 1006
         }
1007 1007
         //first check if event id is present on this object
1008
-        if (!$this->ID()) {
1008
+        if ( ! $this->ID()) {
1009 1009
             return false;
1010 1010
         }
1011 1011
         $where_params_for_event = array(array('EVT_ID' => $this->ID()));
@@ -1082,7 +1082,7 @@  discard block
 block discarded – undo
1082 1082
     public function get_number_of_tickets_sold()
1083 1083
     {
1084 1084
         $tkt_sold = 0;
1085
-        if (!$this->ID()) {
1085
+        if ( ! $this->ID()) {
1086 1086
             return 0;
1087 1087
         }
1088 1088
         $datetimes = $this->datetimes();
@@ -1148,7 +1148,7 @@  discard block
 block discarded – undo
1148 1148
     {
1149 1149
         $earliest_ticket = $this->get_ticket_with_earliest_start_time();
1150 1150
         $latest_ticket = $this->get_ticket_with_latest_end_time();
1151
-        if (!$latest_ticket instanceof EE_Ticket && !$earliest_ticket instanceof EE_Ticket) {
1151
+        if ( ! $latest_ticket instanceof EE_Ticket && ! $earliest_ticket instanceof EE_Ticket) {
1152 1152
             return false;
1153 1153
         }
1154 1154
         //check on sale for these two tickets.
@@ -1214,7 +1214,7 @@  discard block
 block discarded – undo
1214 1214
      */
1215 1215
     public function question_groups($query_params = array())
1216 1216
     {
1217
-        $query_params = !empty($query_params) ? $query_params : array('order_by' => array('QSG_order' => 'ASC'));
1217
+        $query_params = ! empty($query_params) ? $query_params : array('order_by' => array('QSG_order' => 'ASC'));
1218 1218
         return $this->get_many_related('Question_Group', $query_params);
1219 1219
     }
1220 1220
 
Please login to merge, or discard this patch.
core/domain/services/event/EventSpacesCalculator.php 2 patches
Indentation   +439 added lines, -439 removed lines patch added patch discarded remove patch
@@ -23,445 +23,445 @@
 block discarded – undo
23 23
 class EventSpacesCalculator
24 24
 {
25 25
 
26
-    /**
27
-     * @var EE_Event $event
28
-     */
29
-    private $event;
30
-
31
-    /**
32
-     * @var array $datetime_query_params
33
-     */
34
-    private $datetime_query_params;
35
-
36
-    /**
37
-     * @var EE_Ticket[] $active_tickets
38
-     */
39
-    private $active_tickets = array();
40
-
41
-    /**
42
-     * @var EE_Datetime[] $datetimes
43
-     */
44
-    private $datetimes = array();
45
-
46
-    /**
47
-     * Array of Ticket IDs grouped by Datetime
48
-     *
49
-     * @var array $datetimes
50
-     */
51
-    private $datetime_tickets = array();
52
-
53
-    /**
54
-     * Max spaces for each Datetime (reg limit - previous sold)
55
-     *
56
-     * @var array $datetime_spaces
57
-     */
58
-    private $datetime_spaces = array();
59
-
60
-    /**
61
-     * Array of Datetime IDs grouped by Ticket
62
-     *
63
-     * @var array $ticket_datetimes
64
-     */
65
-    private $ticket_datetimes = array();
66
-
67
-    /**
68
-     * maximum ticket quantities for each ticket (adjusted for reg limit)
69
-     *
70
-     * @var array $ticket_quantities
71
-     */
72
-    private $ticket_quantities = array();
73
-
74
-    /**
75
-     * total quantity of sold and reserved for each ticket
76
-     *
77
-     * @var array $tickets_sold
78
-     */
79
-    private $tickets_sold = array();
80
-
81
-    /**
82
-     * total spaces available across all datetimes
83
-     *
84
-     * @var array $total_spaces
85
-     */
86
-    private $total_spaces = array();
87
-
88
-    /**
89
-     * @var boolean $debug
90
-     */
91
-    private $debug = false;
92
-
93
-
94
-
95
-    /**
96
-     * EventSpacesCalculator constructor.
97
-     *
98
-     * @param EE_Event $event
99
-     * @param array    $datetime_query_params
100
-     * @throws EE_Error
101
-     */
102
-    public function __construct(EE_Event $event, array $datetime_query_params = array())
103
-    {
104
-        $this->event = $event;
105
-        $this->datetime_query_params = $datetime_query_params + array('order_by' => array('DTT_reg_limit' => 'ASC'));
106
-    }
107
-
108
-
109
-
110
-    /**
111
-     * @return EE_Ticket[]
112
-     * @throws EE_Error
113
-     */
114
-    public function getActiveTickets()
115
-    {
116
-        if(empty($this->active_tickets)) {
117
-            $this->active_tickets = $this->event->tickets(
118
-                array(
119
-                    array(
120
-                        'TKT_end_date' => array('>=', EEM_Ticket::instance()->current_time_for_query('TKT_end_date')),
121
-                        'TKT_deleted'  => false,
122
-                    ),
123
-                    'order_by' => array('TKT_qty' => 'ASC')
124
-                )
125
-            );
126
-        }
127
-        return $this->active_tickets;
128
-    }
129
-
130
-
131
-
132
-    /**
133
-     * @param EE_Ticket[] $active_tickets
134
-     * @throws EE_Error
135
-     */
136
-    public function setActiveTickets(array $active_tickets)
137
-    {
138
-        // sort incoming array by ticket quantity (asc)
139
-        usort(
140
-            $active_tickets,
141
-            function (EE_Ticket $a, EE_Ticket $b) {
142
-                if ($a->qty() === $b->qty()) {
143
-                    return 0;
144
-                }
145
-                return ($a->qty() < $b->qty()) ? -1 : 1;
146
-            }
147
-        );
148
-        $this->active_tickets = $active_tickets;
149
-    }
150
-
151
-
152
-
153
-    /**
154
-     * @return EE_Datetime[]
155
-     */
156
-    public function getDatetimes()
157
-    {
158
-        return $this->datetimes;
159
-    }
160
-
161
-
162
-
163
-    /**
164
-     * @param EE_Datetime $datetime
165
-     * @throws EE_Error
166
-     *
167
-     */
168
-    public function setDatetime(EE_Datetime $datetime)
169
-    {
170
-        $this->datetimes[$datetime->ID()] = $datetime;
171
-    }
172
-
173
-
174
-
175
-    /**
176
-     * calculate spaces remaining based on "saleable" tickets
177
-     *
178
-     * @return int|float
179
-     * @throws EE_Error
180
-     */
181
-    public function spacesRemaining()
182
-    {
183
-        $this->initialize();
184
-        return $this->calculate();
185
-    }
186
-
187
-
188
-
189
-    /**
190
-     * calculates total available spaces for an event with no regard for sold tickets
191
-     *
192
-     * @return int|float
193
-     * @throws EE_Error
194
-     */
195
-    public function totalSpacesAvailable()
196
-    {
197
-        $this->initialize();
198
-        return $this->calculate(false);
199
-    }
200
-
201
-
202
-
203
-    /**
204
-     * Loops through the active tickets for the event
205
-     * and builds a series of data arrays that will be used for calculating
206
-     * the total maximum available spaces, as well as the spaces remaining.
207
-     * Because ticket quantities affect datetime spaces and vice versa,
208
-     * we need to be constantly updating these data arrays as things change,
209
-     * which is the entire reason for their existence.
210
-     *
211
-     * @throws EE_Error
212
-     */
213
-    private function initialize()
214
-    {
215
-        if ($this->debug) {
216
-            echo "\n\n" . __LINE__ . ') ' . strtoupper(__METHOD__) . '()';
217
-        }
218
-        $this->datetime_tickets = array();
219
-        $this->datetime_spaces = array();
220
-        $this->ticket_datetimes = array();
221
-        $this->ticket_quantities = array();
222
-        $this->tickets_sold = array();
223
-        $this->total_spaces = array();
224
-        $active_tickets = $this->getActiveTickets();
225
-        if (! empty($active_tickets)) {
226
-            foreach ($active_tickets as $ticket) {
227
-                if (! $ticket instanceof EE_Ticket) {
228
-                    continue;
229
-                }
230
-                $TKT_ID = $ticket->name();
231
-                // to start, we'll just consider the raw qty to be the maximum availability for this ticket
232
-                $max_tickets = $ticket->qty();
233
-                // but we'll adjust that after looping over each datetime for the ticket and checking reg limits
234
-                $ticket_datetimes = $ticket->datetimes($this->datetime_query_params);
235
-                foreach ($ticket_datetimes as $datetime) {
236
-                    if (! $datetime instanceof EE_Datetime) {
237
-                        continue;
238
-                    }
239
-                    // save all datetimes
240
-                    $this->setDatetime($datetime);
241
-                    $DTT_ID = $datetime->name();
242
-                    $reg_limit = $datetime->reg_limit();
243
-                    // ticket quantity can not exceed datetime reg limit
244
-                    $max_tickets = min($max_tickets, $reg_limit);
245
-                    // as described earlier, because we need to be able to constantly adjust numbers for things,
246
-                    // we are going to move all of our data into the following arrays:
247
-                    // datetime spaces initially represents the reg limit for each datetime,
248
-                    // but this will get adjusted as tickets are accounted for
249
-                    $this->datetime_spaces[$DTT_ID] = $reg_limit;
250
-                    // just an array of ticket IDs grouped by datetime
251
-                    $this->datetime_tickets[$DTT_ID][] = $TKT_ID;
252
-                    // and an array of datetime IDs grouped by ticket
253
-                    $this->ticket_datetimes[$TKT_ID][] = $DTT_ID;
254
-                }
255
-                // total quantity of sold and reserved for each ticket
256
-                $this->tickets_sold[$TKT_ID] = $ticket->sold() + $ticket->reserved();
257
-                // and the maximum ticket quantities for each ticket (adjusted for reg limit)
258
-                $this->ticket_quantities[$TKT_ID] = $max_tickets;
259
-            }
260
-        }
261
-        // sort datetime spaces by reg limit, but maintain our string indexes
262
-        asort($this->datetime_spaces, SORT_NUMERIC);
263
-        // datetime tickets need to be sorted in the SAME order as the above array...
264
-        // so we'll just use array_merge() to take the structure of datetime_spaces
265
-        // but overwrite all of the data with that from datetime_tickets
266
-        $this->datetime_tickets = array_merge(
267
-            $this->datetime_spaces,
268
-            $this->datetime_tickets
269
-        );
270
-        if ($this->debug) {
271
-            \EEH_Debug_Tools::printr($this->datetime_spaces, 'datetime_spaces', __FILE__, __LINE__);
272
-            \EEH_Debug_Tools::printr($this->datetime_tickets, 'datetime_tickets', __FILE__, __LINE__);
273
-            \EEH_Debug_Tools::printr($this->ticket_quantities, 'ticket_quantities', __FILE__, __LINE__);
274
-        }
275
-    }
276
-
277
-
278
-
279
-    /**
280
-     * performs calculations on initialized data
281
-     *
282
-     * @param bool $consider_sold
283
-     * @return int|float
284
-     */
285
-    private function calculate($consider_sold = true)
286
-    {
287
-        if ($this->debug) {
288
-            echo "\n\n" . __LINE__ . ') ' . strtoupper(__METHOD__) . '()';
289
-        }
290
-        foreach ($this->datetime_tickets as $DTT_ID => $tickets) {
291
-            $this->trackAvailableSpacesForDatetimes($DTT_ID, $tickets);
292
-        }
293
-        // total spaces available is just the sum of the spaces available for each datetime
294
-        $spaces_remaining = array_sum($this->total_spaces);
295
-        if($consider_sold) {
296
-            // less the sum of all tickets sold for these datetimes
297
-            $spaces_remaining -= array_sum($this->tickets_sold);
298
-        }
299
-        if ($this->debug) {
300
-            \EEH_Debug_Tools::printr($this->total_spaces, '$this->total_spaces', __FILE__, __LINE__);
301
-            \EEH_Debug_Tools::printr($this->tickets_sold, '$this->tickets_sold', __FILE__, __LINE__);
302
-            \EEH_Debug_Tools::printr($spaces_remaining, '$spaces_remaining', __FILE__, __LINE__);
303
-        }
304
-        return $spaces_remaining;
305
-    }
306
-
307
-
308
-
309
-    /**
310
-     * @param string $DTT_ID
311
-     * @param array  $tickets
312
-     */
313
-    private function trackAvailableSpacesForDatetimes($DTT_ID, array $tickets)
314
-    {
315
-        // make sure a reg limit is set for the datetime
316
-        $reg_limit = isset($this->datetime_spaces[$DTT_ID])
317
-            ? $this->datetime_spaces[$DTT_ID]
318
-            : 0;
319
-        // and bail if it is not
320
-        if (! $reg_limit) {
321
-            if ($this->debug) {
322
-                echo "\n . {$DTT_ID} AT CAPACITY";
323
-            }
324
-            return;
325
-        }
326
-        if ($this->debug) {
327
-            echo "\n\n{$DTT_ID}";
328
-            echo "\n . " . 'REG LIMIT: ' . $reg_limit;
329
-        }
330
-        // set default number of available spaces
331
-        $spaces = 0;
332
-        $this->total_spaces[$DTT_ID] = 0;
333
-        foreach ($tickets as $TKT_ID) {
334
-            $spaces = $this->calculateAvailableSpacesForTicket(
335
-                $DTT_ID,
336
-                $reg_limit,
337
-                $TKT_ID,
338
-                $spaces
339
-            );
340
-        }
341
-        // spaces can't be negative
342
-        $spaces = max($spaces, 0);
343
-        if ($spaces) {
344
-            // track any non-zero values
345
-            $this->total_spaces[$DTT_ID] += $spaces;
346
-            if ($this->debug) {
347
-                echo "\n . spaces: {$spaces}";
348
-            }
349
-        } else {
350
-            if ($this->debug) {
351
-                echo "\n . NO TICKETS AVAILABLE FOR DATETIME";
352
-            }
353
-        }
354
-        if ($this->debug) {
355
-            \EEH_Debug_Tools::printr($this->total_spaces[$DTT_ID], '$spaces_remaining', __FILE__, __LINE__);
356
-            \EEH_Debug_Tools::printr($this->ticket_quantities, '$ticket_quantities', __FILE__, __LINE__);
357
-            \EEH_Debug_Tools::printr($this->datetime_spaces, 'datetime_spaces', __FILE__, __LINE__);
358
-        }
359
-    }
360
-
361
-
362
-
363
-    /**
364
-     * @param string $DTT_ID
365
-     * @param int    $reg_limit
366
-     * @param string $TKT_ID
367
-     * @param int    $spaces
368
-     * @return int
369
-     */
370
-    private function calculateAvailableSpacesForTicket($DTT_ID, $reg_limit,$TKT_ID, $spaces)
371
-    {
372
-        if ($this->debug) {
373
-            echo "\n . {$TKT_ID}";
374
-        }
375
-        // make sure ticket quantity is set
376
-        $ticket_quantity = isset($this->ticket_quantities[$TKT_ID])
377
-            ? $this->ticket_quantities[$TKT_ID]
378
-            : 0;
379
-        if ($ticket_quantity) {
380
-            if ($this->debug) {
381
-                echo "\n . . spaces ({$spaces}) <= reg_limit ({$reg_limit}) = ";
382
-                echo ($spaces <= $reg_limit)
383
-                    ? 'true'
384
-                    : 'false';
385
-            }
386
-            // if the datetime is NOT at full capacity yet
387
-            if ($spaces <= $reg_limit) {
388
-                // then the maximum ticket quantity we can allocate is the lowest value of either:
389
-                //  the number of remaining spaces for the datetime, which is the limit - spaces already taken
390
-                //  or the maximum ticket quantity
391
-                $ticket_quantity = min(($reg_limit - $spaces), $ticket_quantity);
392
-                // adjust the available quantity in our tracking array
393
-                $this->ticket_quantities[$TKT_ID] -= $ticket_quantity;
394
-                // and increment spaces allocated for this datetime
395
-                $spaces += $ticket_quantity;
396
-                if ($this->debug) {
397
-                    echo "\n . . ticket quantity: {$ticket_quantity} ({$TKT_ID})";
398
-                    echo "\n . . . allocate {$ticket_quantity} tickets ({$TKT_ID})";
399
-                    if ($spaces >= $reg_limit) {
400
-                        echo "\n . {$DTT_ID} AT CAPACITY";
401
-                    }
402
-                }
403
-                // now adjust all other datetimes that allow access to this ticket
404
-                $this->adjustDatetimes(
405
-                    $DTT_ID,
406
-                    $spaces,
407
-                    $reg_limit,
408
-                    $TKT_ID,
409
-                    $ticket_quantity
410
-                );
411
-            }
412
-        }
413
-        return $spaces;
414
-    }
415
-
416
-
417
-
418
-    /**
419
-     * subtracts ticket amounts from all datetime reg limits
420
-     * that allow access to the ticket specified,
421
-     * because that ticket could be used
422
-     * to attend any of the datetimes it has access to
423
-     *
424
-     * @param string $DTT_ID
425
-     * @param int    $spaces
426
-     * @param int    $reg_limit
427
-     * @param string $TKT_ID
428
-     * @param int    $ticket_quantity
429
-     */
430
-    private function adjustDatetimes($DTT_ID, $spaces, $reg_limit, $TKT_ID, $ticket_quantity)
431
-    {
432
-        foreach ($this->datetime_tickets as $datetime_ID => $datetime_tickets) {
433
-            // if the supplied ticket has access to this datetime
434
-            if (in_array($TKT_ID, $datetime_tickets, true)) {
435
-                // and datetime has spaces available
436
-                if (isset($this->datetime_spaces[$datetime_ID])) {
437
-                    // then decrement the available spaces for the datetime
438
-                    $this->datetime_spaces[$datetime_ID] -= $ticket_quantity;
439
-                    // but don't let quantities go below zero
440
-                    $this->datetime_spaces[$datetime_ID] = max(
441
-                        $this->datetime_spaces[$datetime_ID],
442
-                        0
443
-                    );
444
-                    if ($this->debug) {
445
-                        echo "\n . . . " . $datetime_ID . " capacity reduced by {$ticket_quantity}";
446
-                        echo " because it allows access to {$TKT_ID}";
447
-                    }
448
-                }
449
-                // if this datetime is at full capacity
450
-                if ($datetime_ID === $DTT_ID && $spaces >= $reg_limit) {
451
-                    // then all of it's tickets are now unavailable
452
-                    foreach ($datetime_tickets as $datetime_ticket) {
453
-                        // so  set any tracked available quantities to zero
454
-                        if (isset($this->ticket_quantities[$datetime_ticket])) {
455
-                            $this->ticket_quantities[$datetime_ticket] = 0;
456
-                        }
457
-                        if ($this->debug) {
458
-                            echo "\n . . . " . $datetime_ticket . ' unavailable: ';
459
-                        }
460
-                    }
461
-                }
462
-            }
463
-        }
464
-    }
26
+	/**
27
+	 * @var EE_Event $event
28
+	 */
29
+	private $event;
30
+
31
+	/**
32
+	 * @var array $datetime_query_params
33
+	 */
34
+	private $datetime_query_params;
35
+
36
+	/**
37
+	 * @var EE_Ticket[] $active_tickets
38
+	 */
39
+	private $active_tickets = array();
40
+
41
+	/**
42
+	 * @var EE_Datetime[] $datetimes
43
+	 */
44
+	private $datetimes = array();
45
+
46
+	/**
47
+	 * Array of Ticket IDs grouped by Datetime
48
+	 *
49
+	 * @var array $datetimes
50
+	 */
51
+	private $datetime_tickets = array();
52
+
53
+	/**
54
+	 * Max spaces for each Datetime (reg limit - previous sold)
55
+	 *
56
+	 * @var array $datetime_spaces
57
+	 */
58
+	private $datetime_spaces = array();
59
+
60
+	/**
61
+	 * Array of Datetime IDs grouped by Ticket
62
+	 *
63
+	 * @var array $ticket_datetimes
64
+	 */
65
+	private $ticket_datetimes = array();
66
+
67
+	/**
68
+	 * maximum ticket quantities for each ticket (adjusted for reg limit)
69
+	 *
70
+	 * @var array $ticket_quantities
71
+	 */
72
+	private $ticket_quantities = array();
73
+
74
+	/**
75
+	 * total quantity of sold and reserved for each ticket
76
+	 *
77
+	 * @var array $tickets_sold
78
+	 */
79
+	private $tickets_sold = array();
80
+
81
+	/**
82
+	 * total spaces available across all datetimes
83
+	 *
84
+	 * @var array $total_spaces
85
+	 */
86
+	private $total_spaces = array();
87
+
88
+	/**
89
+	 * @var boolean $debug
90
+	 */
91
+	private $debug = false;
92
+
93
+
94
+
95
+	/**
96
+	 * EventSpacesCalculator constructor.
97
+	 *
98
+	 * @param EE_Event $event
99
+	 * @param array    $datetime_query_params
100
+	 * @throws EE_Error
101
+	 */
102
+	public function __construct(EE_Event $event, array $datetime_query_params = array())
103
+	{
104
+		$this->event = $event;
105
+		$this->datetime_query_params = $datetime_query_params + array('order_by' => array('DTT_reg_limit' => 'ASC'));
106
+	}
107
+
108
+
109
+
110
+	/**
111
+	 * @return EE_Ticket[]
112
+	 * @throws EE_Error
113
+	 */
114
+	public function getActiveTickets()
115
+	{
116
+		if(empty($this->active_tickets)) {
117
+			$this->active_tickets = $this->event->tickets(
118
+				array(
119
+					array(
120
+						'TKT_end_date' => array('>=', EEM_Ticket::instance()->current_time_for_query('TKT_end_date')),
121
+						'TKT_deleted'  => false,
122
+					),
123
+					'order_by' => array('TKT_qty' => 'ASC')
124
+				)
125
+			);
126
+		}
127
+		return $this->active_tickets;
128
+	}
129
+
130
+
131
+
132
+	/**
133
+	 * @param EE_Ticket[] $active_tickets
134
+	 * @throws EE_Error
135
+	 */
136
+	public function setActiveTickets(array $active_tickets)
137
+	{
138
+		// sort incoming array by ticket quantity (asc)
139
+		usort(
140
+			$active_tickets,
141
+			function (EE_Ticket $a, EE_Ticket $b) {
142
+				if ($a->qty() === $b->qty()) {
143
+					return 0;
144
+				}
145
+				return ($a->qty() < $b->qty()) ? -1 : 1;
146
+			}
147
+		);
148
+		$this->active_tickets = $active_tickets;
149
+	}
150
+
151
+
152
+
153
+	/**
154
+	 * @return EE_Datetime[]
155
+	 */
156
+	public function getDatetimes()
157
+	{
158
+		return $this->datetimes;
159
+	}
160
+
161
+
162
+
163
+	/**
164
+	 * @param EE_Datetime $datetime
165
+	 * @throws EE_Error
166
+	 *
167
+	 */
168
+	public function setDatetime(EE_Datetime $datetime)
169
+	{
170
+		$this->datetimes[$datetime->ID()] = $datetime;
171
+	}
172
+
173
+
174
+
175
+	/**
176
+	 * calculate spaces remaining based on "saleable" tickets
177
+	 *
178
+	 * @return int|float
179
+	 * @throws EE_Error
180
+	 */
181
+	public function spacesRemaining()
182
+	{
183
+		$this->initialize();
184
+		return $this->calculate();
185
+	}
186
+
187
+
188
+
189
+	/**
190
+	 * calculates total available spaces for an event with no regard for sold tickets
191
+	 *
192
+	 * @return int|float
193
+	 * @throws EE_Error
194
+	 */
195
+	public function totalSpacesAvailable()
196
+	{
197
+		$this->initialize();
198
+		return $this->calculate(false);
199
+	}
200
+
201
+
202
+
203
+	/**
204
+	 * Loops through the active tickets for the event
205
+	 * and builds a series of data arrays that will be used for calculating
206
+	 * the total maximum available spaces, as well as the spaces remaining.
207
+	 * Because ticket quantities affect datetime spaces and vice versa,
208
+	 * we need to be constantly updating these data arrays as things change,
209
+	 * which is the entire reason for their existence.
210
+	 *
211
+	 * @throws EE_Error
212
+	 */
213
+	private function initialize()
214
+	{
215
+		if ($this->debug) {
216
+			echo "\n\n" . __LINE__ . ') ' . strtoupper(__METHOD__) . '()';
217
+		}
218
+		$this->datetime_tickets = array();
219
+		$this->datetime_spaces = array();
220
+		$this->ticket_datetimes = array();
221
+		$this->ticket_quantities = array();
222
+		$this->tickets_sold = array();
223
+		$this->total_spaces = array();
224
+		$active_tickets = $this->getActiveTickets();
225
+		if (! empty($active_tickets)) {
226
+			foreach ($active_tickets as $ticket) {
227
+				if (! $ticket instanceof EE_Ticket) {
228
+					continue;
229
+				}
230
+				$TKT_ID = $ticket->name();
231
+				// to start, we'll just consider the raw qty to be the maximum availability for this ticket
232
+				$max_tickets = $ticket->qty();
233
+				// but we'll adjust that after looping over each datetime for the ticket and checking reg limits
234
+				$ticket_datetimes = $ticket->datetimes($this->datetime_query_params);
235
+				foreach ($ticket_datetimes as $datetime) {
236
+					if (! $datetime instanceof EE_Datetime) {
237
+						continue;
238
+					}
239
+					// save all datetimes
240
+					$this->setDatetime($datetime);
241
+					$DTT_ID = $datetime->name();
242
+					$reg_limit = $datetime->reg_limit();
243
+					// ticket quantity can not exceed datetime reg limit
244
+					$max_tickets = min($max_tickets, $reg_limit);
245
+					// as described earlier, because we need to be able to constantly adjust numbers for things,
246
+					// we are going to move all of our data into the following arrays:
247
+					// datetime spaces initially represents the reg limit for each datetime,
248
+					// but this will get adjusted as tickets are accounted for
249
+					$this->datetime_spaces[$DTT_ID] = $reg_limit;
250
+					// just an array of ticket IDs grouped by datetime
251
+					$this->datetime_tickets[$DTT_ID][] = $TKT_ID;
252
+					// and an array of datetime IDs grouped by ticket
253
+					$this->ticket_datetimes[$TKT_ID][] = $DTT_ID;
254
+				}
255
+				// total quantity of sold and reserved for each ticket
256
+				$this->tickets_sold[$TKT_ID] = $ticket->sold() + $ticket->reserved();
257
+				// and the maximum ticket quantities for each ticket (adjusted for reg limit)
258
+				$this->ticket_quantities[$TKT_ID] = $max_tickets;
259
+			}
260
+		}
261
+		// sort datetime spaces by reg limit, but maintain our string indexes
262
+		asort($this->datetime_spaces, SORT_NUMERIC);
263
+		// datetime tickets need to be sorted in the SAME order as the above array...
264
+		// so we'll just use array_merge() to take the structure of datetime_spaces
265
+		// but overwrite all of the data with that from datetime_tickets
266
+		$this->datetime_tickets = array_merge(
267
+			$this->datetime_spaces,
268
+			$this->datetime_tickets
269
+		);
270
+		if ($this->debug) {
271
+			\EEH_Debug_Tools::printr($this->datetime_spaces, 'datetime_spaces', __FILE__, __LINE__);
272
+			\EEH_Debug_Tools::printr($this->datetime_tickets, 'datetime_tickets', __FILE__, __LINE__);
273
+			\EEH_Debug_Tools::printr($this->ticket_quantities, 'ticket_quantities', __FILE__, __LINE__);
274
+		}
275
+	}
276
+
277
+
278
+
279
+	/**
280
+	 * performs calculations on initialized data
281
+	 *
282
+	 * @param bool $consider_sold
283
+	 * @return int|float
284
+	 */
285
+	private function calculate($consider_sold = true)
286
+	{
287
+		if ($this->debug) {
288
+			echo "\n\n" . __LINE__ . ') ' . strtoupper(__METHOD__) . '()';
289
+		}
290
+		foreach ($this->datetime_tickets as $DTT_ID => $tickets) {
291
+			$this->trackAvailableSpacesForDatetimes($DTT_ID, $tickets);
292
+		}
293
+		// total spaces available is just the sum of the spaces available for each datetime
294
+		$spaces_remaining = array_sum($this->total_spaces);
295
+		if($consider_sold) {
296
+			// less the sum of all tickets sold for these datetimes
297
+			$spaces_remaining -= array_sum($this->tickets_sold);
298
+		}
299
+		if ($this->debug) {
300
+			\EEH_Debug_Tools::printr($this->total_spaces, '$this->total_spaces', __FILE__, __LINE__);
301
+			\EEH_Debug_Tools::printr($this->tickets_sold, '$this->tickets_sold', __FILE__, __LINE__);
302
+			\EEH_Debug_Tools::printr($spaces_remaining, '$spaces_remaining', __FILE__, __LINE__);
303
+		}
304
+		return $spaces_remaining;
305
+	}
306
+
307
+
308
+
309
+	/**
310
+	 * @param string $DTT_ID
311
+	 * @param array  $tickets
312
+	 */
313
+	private function trackAvailableSpacesForDatetimes($DTT_ID, array $tickets)
314
+	{
315
+		// make sure a reg limit is set for the datetime
316
+		$reg_limit = isset($this->datetime_spaces[$DTT_ID])
317
+			? $this->datetime_spaces[$DTT_ID]
318
+			: 0;
319
+		// and bail if it is not
320
+		if (! $reg_limit) {
321
+			if ($this->debug) {
322
+				echo "\n . {$DTT_ID} AT CAPACITY";
323
+			}
324
+			return;
325
+		}
326
+		if ($this->debug) {
327
+			echo "\n\n{$DTT_ID}";
328
+			echo "\n . " . 'REG LIMIT: ' . $reg_limit;
329
+		}
330
+		// set default number of available spaces
331
+		$spaces = 0;
332
+		$this->total_spaces[$DTT_ID] = 0;
333
+		foreach ($tickets as $TKT_ID) {
334
+			$spaces = $this->calculateAvailableSpacesForTicket(
335
+				$DTT_ID,
336
+				$reg_limit,
337
+				$TKT_ID,
338
+				$spaces
339
+			);
340
+		}
341
+		// spaces can't be negative
342
+		$spaces = max($spaces, 0);
343
+		if ($spaces) {
344
+			// track any non-zero values
345
+			$this->total_spaces[$DTT_ID] += $spaces;
346
+			if ($this->debug) {
347
+				echo "\n . spaces: {$spaces}";
348
+			}
349
+		} else {
350
+			if ($this->debug) {
351
+				echo "\n . NO TICKETS AVAILABLE FOR DATETIME";
352
+			}
353
+		}
354
+		if ($this->debug) {
355
+			\EEH_Debug_Tools::printr($this->total_spaces[$DTT_ID], '$spaces_remaining', __FILE__, __LINE__);
356
+			\EEH_Debug_Tools::printr($this->ticket_quantities, '$ticket_quantities', __FILE__, __LINE__);
357
+			\EEH_Debug_Tools::printr($this->datetime_spaces, 'datetime_spaces', __FILE__, __LINE__);
358
+		}
359
+	}
360
+
361
+
362
+
363
+	/**
364
+	 * @param string $DTT_ID
365
+	 * @param int    $reg_limit
366
+	 * @param string $TKT_ID
367
+	 * @param int    $spaces
368
+	 * @return int
369
+	 */
370
+	private function calculateAvailableSpacesForTicket($DTT_ID, $reg_limit,$TKT_ID, $spaces)
371
+	{
372
+		if ($this->debug) {
373
+			echo "\n . {$TKT_ID}";
374
+		}
375
+		// make sure ticket quantity is set
376
+		$ticket_quantity = isset($this->ticket_quantities[$TKT_ID])
377
+			? $this->ticket_quantities[$TKT_ID]
378
+			: 0;
379
+		if ($ticket_quantity) {
380
+			if ($this->debug) {
381
+				echo "\n . . spaces ({$spaces}) <= reg_limit ({$reg_limit}) = ";
382
+				echo ($spaces <= $reg_limit)
383
+					? 'true'
384
+					: 'false';
385
+			}
386
+			// if the datetime is NOT at full capacity yet
387
+			if ($spaces <= $reg_limit) {
388
+				// then the maximum ticket quantity we can allocate is the lowest value of either:
389
+				//  the number of remaining spaces for the datetime, which is the limit - spaces already taken
390
+				//  or the maximum ticket quantity
391
+				$ticket_quantity = min(($reg_limit - $spaces), $ticket_quantity);
392
+				// adjust the available quantity in our tracking array
393
+				$this->ticket_quantities[$TKT_ID] -= $ticket_quantity;
394
+				// and increment spaces allocated for this datetime
395
+				$spaces += $ticket_quantity;
396
+				if ($this->debug) {
397
+					echo "\n . . ticket quantity: {$ticket_quantity} ({$TKT_ID})";
398
+					echo "\n . . . allocate {$ticket_quantity} tickets ({$TKT_ID})";
399
+					if ($spaces >= $reg_limit) {
400
+						echo "\n . {$DTT_ID} AT CAPACITY";
401
+					}
402
+				}
403
+				// now adjust all other datetimes that allow access to this ticket
404
+				$this->adjustDatetimes(
405
+					$DTT_ID,
406
+					$spaces,
407
+					$reg_limit,
408
+					$TKT_ID,
409
+					$ticket_quantity
410
+				);
411
+			}
412
+		}
413
+		return $spaces;
414
+	}
415
+
416
+
417
+
418
+	/**
419
+	 * subtracts ticket amounts from all datetime reg limits
420
+	 * that allow access to the ticket specified,
421
+	 * because that ticket could be used
422
+	 * to attend any of the datetimes it has access to
423
+	 *
424
+	 * @param string $DTT_ID
425
+	 * @param int    $spaces
426
+	 * @param int    $reg_limit
427
+	 * @param string $TKT_ID
428
+	 * @param int    $ticket_quantity
429
+	 */
430
+	private function adjustDatetimes($DTT_ID, $spaces, $reg_limit, $TKT_ID, $ticket_quantity)
431
+	{
432
+		foreach ($this->datetime_tickets as $datetime_ID => $datetime_tickets) {
433
+			// if the supplied ticket has access to this datetime
434
+			if (in_array($TKT_ID, $datetime_tickets, true)) {
435
+				// and datetime has spaces available
436
+				if (isset($this->datetime_spaces[$datetime_ID])) {
437
+					// then decrement the available spaces for the datetime
438
+					$this->datetime_spaces[$datetime_ID] -= $ticket_quantity;
439
+					// but don't let quantities go below zero
440
+					$this->datetime_spaces[$datetime_ID] = max(
441
+						$this->datetime_spaces[$datetime_ID],
442
+						0
443
+					);
444
+					if ($this->debug) {
445
+						echo "\n . . . " . $datetime_ID . " capacity reduced by {$ticket_quantity}";
446
+						echo " because it allows access to {$TKT_ID}";
447
+					}
448
+				}
449
+				// if this datetime is at full capacity
450
+				if ($datetime_ID === $DTT_ID && $spaces >= $reg_limit) {
451
+					// then all of it's tickets are now unavailable
452
+					foreach ($datetime_tickets as $datetime_ticket) {
453
+						// so  set any tracked available quantities to zero
454
+						if (isset($this->ticket_quantities[$datetime_ticket])) {
455
+							$this->ticket_quantities[$datetime_ticket] = 0;
456
+						}
457
+						if ($this->debug) {
458
+							echo "\n . . . " . $datetime_ticket . ' unavailable: ';
459
+						}
460
+					}
461
+				}
462
+			}
463
+		}
464
+	}
465 465
 
466 466
 }
467 467
 // Location: EventSpacesCalculator.php
Please login to merge, or discard this patch.
Spacing   +13 added lines, -13 removed lines patch added patch discarded remove patch
@@ -113,7 +113,7 @@  discard block
 block discarded – undo
113 113
      */
114 114
     public function getActiveTickets()
115 115
     {
116
-        if(empty($this->active_tickets)) {
116
+        if (empty($this->active_tickets)) {
117 117
             $this->active_tickets = $this->event->tickets(
118 118
                 array(
119 119
                     array(
@@ -138,7 +138,7 @@  discard block
 block discarded – undo
138 138
         // sort incoming array by ticket quantity (asc)
139 139
         usort(
140 140
             $active_tickets,
141
-            function (EE_Ticket $a, EE_Ticket $b) {
141
+            function(EE_Ticket $a, EE_Ticket $b) {
142 142
                 if ($a->qty() === $b->qty()) {
143 143
                     return 0;
144 144
                 }
@@ -213,7 +213,7 @@  discard block
 block discarded – undo
213 213
     private function initialize()
214 214
     {
215 215
         if ($this->debug) {
216
-            echo "\n\n" . __LINE__ . ') ' . strtoupper(__METHOD__) . '()';
216
+            echo "\n\n".__LINE__.') '.strtoupper(__METHOD__).'()';
217 217
         }
218 218
         $this->datetime_tickets = array();
219 219
         $this->datetime_spaces = array();
@@ -222,9 +222,9 @@  discard block
 block discarded – undo
222 222
         $this->tickets_sold = array();
223 223
         $this->total_spaces = array();
224 224
         $active_tickets = $this->getActiveTickets();
225
-        if (! empty($active_tickets)) {
225
+        if ( ! empty($active_tickets)) {
226 226
             foreach ($active_tickets as $ticket) {
227
-                if (! $ticket instanceof EE_Ticket) {
227
+                if ( ! $ticket instanceof EE_Ticket) {
228 228
                     continue;
229 229
                 }
230 230
                 $TKT_ID = $ticket->name();
@@ -233,7 +233,7 @@  discard block
 block discarded – undo
233 233
                 // but we'll adjust that after looping over each datetime for the ticket and checking reg limits
234 234
                 $ticket_datetimes = $ticket->datetimes($this->datetime_query_params);
235 235
                 foreach ($ticket_datetimes as $datetime) {
236
-                    if (! $datetime instanceof EE_Datetime) {
236
+                    if ( ! $datetime instanceof EE_Datetime) {
237 237
                         continue;
238 238
                     }
239 239
                     // save all datetimes
@@ -285,14 +285,14 @@  discard block
 block discarded – undo
285 285
     private function calculate($consider_sold = true)
286 286
     {
287 287
         if ($this->debug) {
288
-            echo "\n\n" . __LINE__ . ') ' . strtoupper(__METHOD__) . '()';
288
+            echo "\n\n".__LINE__.') '.strtoupper(__METHOD__).'()';
289 289
         }
290 290
         foreach ($this->datetime_tickets as $DTT_ID => $tickets) {
291 291
             $this->trackAvailableSpacesForDatetimes($DTT_ID, $tickets);
292 292
         }
293 293
         // total spaces available is just the sum of the spaces available for each datetime
294 294
         $spaces_remaining = array_sum($this->total_spaces);
295
-        if($consider_sold) {
295
+        if ($consider_sold) {
296 296
             // less the sum of all tickets sold for these datetimes
297 297
             $spaces_remaining -= array_sum($this->tickets_sold);
298 298
         }
@@ -317,7 +317,7 @@  discard block
 block discarded – undo
317 317
             ? $this->datetime_spaces[$DTT_ID]
318 318
             : 0;
319 319
         // and bail if it is not
320
-        if (! $reg_limit) {
320
+        if ( ! $reg_limit) {
321 321
             if ($this->debug) {
322 322
                 echo "\n . {$DTT_ID} AT CAPACITY";
323 323
             }
@@ -325,7 +325,7 @@  discard block
 block discarded – undo
325 325
         }
326 326
         if ($this->debug) {
327 327
             echo "\n\n{$DTT_ID}";
328
-            echo "\n . " . 'REG LIMIT: ' . $reg_limit;
328
+            echo "\n . ".'REG LIMIT: '.$reg_limit;
329 329
         }
330 330
         // set default number of available spaces
331 331
         $spaces = 0;
@@ -367,7 +367,7 @@  discard block
 block discarded – undo
367 367
      * @param int    $spaces
368 368
      * @return int
369 369
      */
370
-    private function calculateAvailableSpacesForTicket($DTT_ID, $reg_limit,$TKT_ID, $spaces)
370
+    private function calculateAvailableSpacesForTicket($DTT_ID, $reg_limit, $TKT_ID, $spaces)
371 371
     {
372 372
         if ($this->debug) {
373 373
             echo "\n . {$TKT_ID}";
@@ -442,7 +442,7 @@  discard block
 block discarded – undo
442 442
                         0
443 443
                     );
444 444
                     if ($this->debug) {
445
-                        echo "\n . . . " . $datetime_ID . " capacity reduced by {$ticket_quantity}";
445
+                        echo "\n . . . ".$datetime_ID." capacity reduced by {$ticket_quantity}";
446 446
                         echo " because it allows access to {$TKT_ID}";
447 447
                     }
448 448
                 }
@@ -455,7 +455,7 @@  discard block
 block discarded – undo
455 455
                             $this->ticket_quantities[$datetime_ticket] = 0;
456 456
                         }
457 457
                         if ($this->debug) {
458
-                            echo "\n . . . " . $datetime_ticket . ' unavailable: ';
458
+                            echo "\n . . . ".$datetime_ticket.' unavailable: ';
459 459
                         }
460 460
                     }
461 461
                 }
Please login to merge, or discard this patch.