@@ -25,215 +25,215 @@ |
||
| 25 | 25 | */ |
| 26 | 26 | class BatchRequestProcessor |
| 27 | 27 | { |
| 28 | - // phpcs:disable PSR2.Classes.PropertyDeclaration.Underscore |
|
| 29 | - // phpcs:disable PSR2.Methods.MethodDeclaration.Underscore |
|
| 30 | - /** |
|
| 31 | - * Current job's ID (if assigned) |
|
| 32 | - * |
|
| 33 | - * @var string|null |
|
| 34 | - */ |
|
| 35 | - protected $_job_id; |
|
| 28 | + // phpcs:disable PSR2.Classes.PropertyDeclaration.Underscore |
|
| 29 | + // phpcs:disable PSR2.Methods.MethodDeclaration.Underscore |
|
| 30 | + /** |
|
| 31 | + * Current job's ID (if assigned) |
|
| 32 | + * |
|
| 33 | + * @var string|null |
|
| 34 | + */ |
|
| 35 | + protected $_job_id; |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * Current job's parameters |
|
| 39 | - * |
|
| 40 | - * @var JobParameters|null |
|
| 41 | - */ |
|
| 42 | - protected $_job_parameters; |
|
| 43 | - /** |
|
| 44 | - * @var LoaderInterface |
|
| 45 | - */ |
|
| 46 | - private $loader; |
|
| 37 | + /** |
|
| 38 | + * Current job's parameters |
|
| 39 | + * |
|
| 40 | + * @var JobParameters|null |
|
| 41 | + */ |
|
| 42 | + protected $_job_parameters; |
|
| 43 | + /** |
|
| 44 | + * @var LoaderInterface |
|
| 45 | + */ |
|
| 46 | + private $loader; |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * BatchRequestProcessor constructor. |
|
| 50 | - * @param LoaderInterface $loader |
|
| 51 | - */ |
|
| 52 | - public function __construct(LoaderInterface $loader) |
|
| 53 | - { |
|
| 54 | - $this->loader = $loader; |
|
| 55 | - } |
|
| 56 | - // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
|
| 57 | - /** |
|
| 58 | - * Creates a job for the specified batch handler class (which should be autoloaded) |
|
| 59 | - * and the specified request data |
|
| 60 | - * |
|
| 61 | - * @param string $batch_job_handler_class of an auto-loaded class implementing JobHandlerInterface |
|
| 62 | - * @param array $request_data to be used by the batch job handler |
|
| 63 | - * @return JobStepResponse |
|
| 64 | - */ |
|
| 65 | - public function create_job($batch_job_handler_class, $request_data) |
|
| 66 | - { |
|
| 67 | - try { |
|
| 68 | - $this->_job_id = wp_generate_password(15, false); |
|
| 69 | - $obj = $this->instantiate_batch_job_handler_from_classname($batch_job_handler_class); |
|
| 70 | - $this->_job_parameters = new JobParameters($this->_job_id, $batch_job_handler_class, $request_data); |
|
| 71 | - $response = $obj->create_job($this->_job_parameters); |
|
| 72 | - if (! $response instanceof JobStepResponse) { |
|
| 73 | - throw new BatchRequestException( |
|
| 74 | - sprintf( |
|
| 75 | - __( |
|
| 76 | - 'The class implementing JobHandlerInterface did not return a JobStepResponse when create_job was called with %1$s. It needs to return one or throw an Exception', |
|
| 77 | - 'event_espresso' |
|
| 78 | - ), |
|
| 79 | - wp_json_encode($request_data) |
|
| 80 | - ) |
|
| 81 | - ); |
|
| 82 | - } |
|
| 83 | - $success = $this->_job_parameters->save(true); |
|
| 84 | - if (! $success) { |
|
| 85 | - throw new BatchRequestException( |
|
| 86 | - sprintf( |
|
| 87 | - __( |
|
| 88 | - 'Could not save job %1$s to the Wordpress Options table. These were the arguments used: %2$s', |
|
| 89 | - 'event_espresso' |
|
| 90 | - ), |
|
| 91 | - $this->_job_id, |
|
| 92 | - wp_json_encode($request_data) |
|
| 93 | - ) |
|
| 94 | - ); |
|
| 95 | - } |
|
| 96 | - } catch (\Exception $e) { |
|
| 97 | - $response = $this->_get_error_response($e, 'create_job'); |
|
| 98 | - } |
|
| 99 | - return $response; |
|
| 100 | - } |
|
| 48 | + /** |
|
| 49 | + * BatchRequestProcessor constructor. |
|
| 50 | + * @param LoaderInterface $loader |
|
| 51 | + */ |
|
| 52 | + public function __construct(LoaderInterface $loader) |
|
| 53 | + { |
|
| 54 | + $this->loader = $loader; |
|
| 55 | + } |
|
| 56 | + // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
|
| 57 | + /** |
|
| 58 | + * Creates a job for the specified batch handler class (which should be autoloaded) |
|
| 59 | + * and the specified request data |
|
| 60 | + * |
|
| 61 | + * @param string $batch_job_handler_class of an auto-loaded class implementing JobHandlerInterface |
|
| 62 | + * @param array $request_data to be used by the batch job handler |
|
| 63 | + * @return JobStepResponse |
|
| 64 | + */ |
|
| 65 | + public function create_job($batch_job_handler_class, $request_data) |
|
| 66 | + { |
|
| 67 | + try { |
|
| 68 | + $this->_job_id = wp_generate_password(15, false); |
|
| 69 | + $obj = $this->instantiate_batch_job_handler_from_classname($batch_job_handler_class); |
|
| 70 | + $this->_job_parameters = new JobParameters($this->_job_id, $batch_job_handler_class, $request_data); |
|
| 71 | + $response = $obj->create_job($this->_job_parameters); |
|
| 72 | + if (! $response instanceof JobStepResponse) { |
|
| 73 | + throw new BatchRequestException( |
|
| 74 | + sprintf( |
|
| 75 | + __( |
|
| 76 | + 'The class implementing JobHandlerInterface did not return a JobStepResponse when create_job was called with %1$s. It needs to return one or throw an Exception', |
|
| 77 | + 'event_espresso' |
|
| 78 | + ), |
|
| 79 | + wp_json_encode($request_data) |
|
| 80 | + ) |
|
| 81 | + ); |
|
| 82 | + } |
|
| 83 | + $success = $this->_job_parameters->save(true); |
|
| 84 | + if (! $success) { |
|
| 85 | + throw new BatchRequestException( |
|
| 86 | + sprintf( |
|
| 87 | + __( |
|
| 88 | + 'Could not save job %1$s to the Wordpress Options table. These were the arguments used: %2$s', |
|
| 89 | + 'event_espresso' |
|
| 90 | + ), |
|
| 91 | + $this->_job_id, |
|
| 92 | + wp_json_encode($request_data) |
|
| 93 | + ) |
|
| 94 | + ); |
|
| 95 | + } |
|
| 96 | + } catch (\Exception $e) { |
|
| 97 | + $response = $this->_get_error_response($e, 'create_job'); |
|
| 98 | + } |
|
| 99 | + return $response; |
|
| 100 | + } |
|
| 101 | 101 | |
| 102 | 102 | |
| 103 | - /** |
|
| 104 | - * Retrieves the job's arguments |
|
| 105 | - * |
|
| 106 | - * @param string $job_id |
|
| 107 | - * @param int $batch_size |
|
| 108 | - * @return JobStepResponse |
|
| 109 | - */ |
|
| 110 | - public function continue_job($job_id, $batch_size = 50) |
|
| 111 | - { |
|
| 112 | - try { |
|
| 113 | - $this->_job_id = $job_id; |
|
| 114 | - $batch_size = defined('EE_BATCHRUNNER_BATCH_SIZE') ? EE_BATCHRUNNER_BATCH_SIZE : $batch_size; |
|
| 115 | - // get the corresponding WordPress option for the job |
|
| 116 | - $this->_job_parameters = JobParameters::load($this->_job_id); |
|
| 117 | - $handler_obj = $this->instantiate_batch_job_handler_from_classname($this->_job_parameters->classname()); |
|
| 118 | - // continue it |
|
| 119 | - $response = $handler_obj->continue_job($this->_job_parameters, $batch_size); |
|
| 120 | - if (! $response instanceof JobStepResponse) { |
|
| 121 | - throw new BatchRequestException( |
|
| 122 | - sprintf( |
|
| 123 | - __( |
|
| 124 | - 'The class implementing JobHandlerInterface did not return a JobStepResponse when continue_job was called with job %1$s. It needs to return one or throw an Exception', |
|
| 125 | - 'event_espresso' |
|
| 126 | - ), |
|
| 127 | - $this->_job_id |
|
| 128 | - ) |
|
| 129 | - ); |
|
| 130 | - } |
|
| 131 | - $this->_job_parameters->save(); |
|
| 132 | - } catch (\Exception $e) { |
|
| 133 | - $response = $this->_get_error_response($e, 'continue_job'); |
|
| 134 | - } |
|
| 135 | - return $response; |
|
| 136 | - } |
|
| 103 | + /** |
|
| 104 | + * Retrieves the job's arguments |
|
| 105 | + * |
|
| 106 | + * @param string $job_id |
|
| 107 | + * @param int $batch_size |
|
| 108 | + * @return JobStepResponse |
|
| 109 | + */ |
|
| 110 | + public function continue_job($job_id, $batch_size = 50) |
|
| 111 | + { |
|
| 112 | + try { |
|
| 113 | + $this->_job_id = $job_id; |
|
| 114 | + $batch_size = defined('EE_BATCHRUNNER_BATCH_SIZE') ? EE_BATCHRUNNER_BATCH_SIZE : $batch_size; |
|
| 115 | + // get the corresponding WordPress option for the job |
|
| 116 | + $this->_job_parameters = JobParameters::load($this->_job_id); |
|
| 117 | + $handler_obj = $this->instantiate_batch_job_handler_from_classname($this->_job_parameters->classname()); |
|
| 118 | + // continue it |
|
| 119 | + $response = $handler_obj->continue_job($this->_job_parameters, $batch_size); |
|
| 120 | + if (! $response instanceof JobStepResponse) { |
|
| 121 | + throw new BatchRequestException( |
|
| 122 | + sprintf( |
|
| 123 | + __( |
|
| 124 | + 'The class implementing JobHandlerInterface did not return a JobStepResponse when continue_job was called with job %1$s. It needs to return one or throw an Exception', |
|
| 125 | + 'event_espresso' |
|
| 126 | + ), |
|
| 127 | + $this->_job_id |
|
| 128 | + ) |
|
| 129 | + ); |
|
| 130 | + } |
|
| 131 | + $this->_job_parameters->save(); |
|
| 132 | + } catch (\Exception $e) { |
|
| 133 | + $response = $this->_get_error_response($e, 'continue_job'); |
|
| 134 | + } |
|
| 135 | + return $response; |
|
| 136 | + } |
|
| 137 | 137 | |
| 138 | 138 | |
| 139 | - /** |
|
| 140 | - * Instantiates an object of type $classname, which implements |
|
| 141 | - * JobHandlerInterface |
|
| 142 | - * |
|
| 143 | - * @param string $classname |
|
| 144 | - * @return JobHandlerInterface |
|
| 145 | - * @throws BatchRequestException |
|
| 146 | - */ |
|
| 147 | - public function instantiate_batch_job_handler_from_classname($classname) |
|
| 148 | - { |
|
| 149 | - if (! class_exists($classname)) { |
|
| 150 | - throw new BatchRequestException( |
|
| 151 | - sprintf( |
|
| 152 | - __( |
|
| 153 | - 'The class %1$s does not exist, and so could not be used for running a job. It should implement JobHandlerInterface.', |
|
| 154 | - 'event_espresso' |
|
| 155 | - ), |
|
| 156 | - $classname |
|
| 157 | - ) |
|
| 158 | - ); |
|
| 159 | - } |
|
| 160 | - $obj = $this->loader->getNew($classname); |
|
| 161 | - if (! $obj instanceof JobHandlerInterface) { |
|
| 162 | - throw new BatchRequestException( |
|
| 163 | - sprintf( |
|
| 164 | - __( |
|
| 165 | - 'The class %1$s does not implement JobHandlerInterface and so could not be used for running a job', |
|
| 166 | - 'event_espresso' |
|
| 167 | - ), |
|
| 168 | - $classname |
|
| 169 | - ) |
|
| 170 | - ); |
|
| 171 | - } |
|
| 172 | - return $obj; |
|
| 173 | - } |
|
| 139 | + /** |
|
| 140 | + * Instantiates an object of type $classname, which implements |
|
| 141 | + * JobHandlerInterface |
|
| 142 | + * |
|
| 143 | + * @param string $classname |
|
| 144 | + * @return JobHandlerInterface |
|
| 145 | + * @throws BatchRequestException |
|
| 146 | + */ |
|
| 147 | + public function instantiate_batch_job_handler_from_classname($classname) |
|
| 148 | + { |
|
| 149 | + if (! class_exists($classname)) { |
|
| 150 | + throw new BatchRequestException( |
|
| 151 | + sprintf( |
|
| 152 | + __( |
|
| 153 | + 'The class %1$s does not exist, and so could not be used for running a job. It should implement JobHandlerInterface.', |
|
| 154 | + 'event_espresso' |
|
| 155 | + ), |
|
| 156 | + $classname |
|
| 157 | + ) |
|
| 158 | + ); |
|
| 159 | + } |
|
| 160 | + $obj = $this->loader->getNew($classname); |
|
| 161 | + if (! $obj instanceof JobHandlerInterface) { |
|
| 162 | + throw new BatchRequestException( |
|
| 163 | + sprintf( |
|
| 164 | + __( |
|
| 165 | + 'The class %1$s does not implement JobHandlerInterface and so could not be used for running a job', |
|
| 166 | + 'event_espresso' |
|
| 167 | + ), |
|
| 168 | + $classname |
|
| 169 | + ) |
|
| 170 | + ); |
|
| 171 | + } |
|
| 172 | + return $obj; |
|
| 173 | + } |
|
| 174 | 174 | |
| 175 | 175 | |
| 176 | - /** |
|
| 177 | - * Forces a job to be cleaned up |
|
| 178 | - * |
|
| 179 | - * @param string $job_id |
|
| 180 | - * @return JobStepResponse |
|
| 181 | - * @throws BatchRequestException |
|
| 182 | - */ |
|
| 183 | - public function cleanup_job($job_id) |
|
| 184 | - { |
|
| 185 | - try { |
|
| 186 | - $this->_job_id = $job_id; |
|
| 187 | - $job_parameters = JobParameters::load($this->_job_id); |
|
| 188 | - $handler_obj = $this->instantiate_batch_job_handler_from_classname($job_parameters->classname()); |
|
| 189 | - // continue it |
|
| 190 | - $response = $handler_obj->cleanup_job($job_parameters); |
|
| 191 | - if (! $response instanceof JobStepResponse) { |
|
| 192 | - throw new BatchRequestException( |
|
| 193 | - sprintf( |
|
| 194 | - __( |
|
| 195 | - 'The class implementing JobHandlerInterface did not return a JobStepResponse when cleanup_job was called with job %1$s. It needs to return one or throw an Exception', |
|
| 196 | - 'event_espresso' |
|
| 197 | - ), |
|
| 198 | - $this->_job_id |
|
| 199 | - ) |
|
| 200 | - ); |
|
| 201 | - } |
|
| 202 | - $job_parameters->set_status(JobParameters::status_cleaned_up); |
|
| 203 | - $job_parameters->delete(); |
|
| 204 | - return $response; |
|
| 205 | - } catch (\Exception $e) { |
|
| 206 | - $response = $this->_get_error_response($e, 'cleanup_job'); |
|
| 207 | - } |
|
| 208 | - return $response; |
|
| 209 | - } |
|
| 176 | + /** |
|
| 177 | + * Forces a job to be cleaned up |
|
| 178 | + * |
|
| 179 | + * @param string $job_id |
|
| 180 | + * @return JobStepResponse |
|
| 181 | + * @throws BatchRequestException |
|
| 182 | + */ |
|
| 183 | + public function cleanup_job($job_id) |
|
| 184 | + { |
|
| 185 | + try { |
|
| 186 | + $this->_job_id = $job_id; |
|
| 187 | + $job_parameters = JobParameters::load($this->_job_id); |
|
| 188 | + $handler_obj = $this->instantiate_batch_job_handler_from_classname($job_parameters->classname()); |
|
| 189 | + // continue it |
|
| 190 | + $response = $handler_obj->cleanup_job($job_parameters); |
|
| 191 | + if (! $response instanceof JobStepResponse) { |
|
| 192 | + throw new BatchRequestException( |
|
| 193 | + sprintf( |
|
| 194 | + __( |
|
| 195 | + 'The class implementing JobHandlerInterface did not return a JobStepResponse when cleanup_job was called with job %1$s. It needs to return one or throw an Exception', |
|
| 196 | + 'event_espresso' |
|
| 197 | + ), |
|
| 198 | + $this->_job_id |
|
| 199 | + ) |
|
| 200 | + ); |
|
| 201 | + } |
|
| 202 | + $job_parameters->set_status(JobParameters::status_cleaned_up); |
|
| 203 | + $job_parameters->delete(); |
|
| 204 | + return $response; |
|
| 205 | + } catch (\Exception $e) { |
|
| 206 | + $response = $this->_get_error_response($e, 'cleanup_job'); |
|
| 207 | + } |
|
| 208 | + return $response; |
|
| 209 | + } |
|
| 210 | 210 | |
| 211 | 211 | |
| 212 | - /** |
|
| 213 | - * Creates a valid JobStepResponse object from an exception and method name. |
|
| 214 | - * |
|
| 215 | - * @param \Exception $exception |
|
| 216 | - * @param string $method_name |
|
| 217 | - * @return JobStepResponse |
|
| 218 | - */ |
|
| 219 | - protected function _get_error_response(\Exception $exception, $method_name) |
|
| 220 | - { |
|
| 221 | - if (! $this->_job_parameters instanceof JobParameters) { |
|
| 222 | - $this->_job_parameters = new JobParameters($this->_job_id, __('__Unknown__', 'event_espresso'), array()); |
|
| 223 | - } |
|
| 224 | - $this->_job_parameters->set_status(JobParameters::status_error); |
|
| 225 | - return new JobStepResponse( |
|
| 226 | - $this->_job_parameters, |
|
| 227 | - sprintf( |
|
| 228 | - __( |
|
| 229 | - 'An exception of type %1$s occurred while running %2$s. Its message was %3$s and had trace %4$s', |
|
| 230 | - 'event_espresso' |
|
| 231 | - ), |
|
| 232 | - get_class($exception), |
|
| 233 | - 'BatchRunner::' . $method_name . '()', |
|
| 234 | - $exception->getMessage(), |
|
| 235 | - $exception->getTraceAsString() |
|
| 236 | - ) |
|
| 237 | - ); |
|
| 238 | - } |
|
| 212 | + /** |
|
| 213 | + * Creates a valid JobStepResponse object from an exception and method name. |
|
| 214 | + * |
|
| 215 | + * @param \Exception $exception |
|
| 216 | + * @param string $method_name |
|
| 217 | + * @return JobStepResponse |
|
| 218 | + */ |
|
| 219 | + protected function _get_error_response(\Exception $exception, $method_name) |
|
| 220 | + { |
|
| 221 | + if (! $this->_job_parameters instanceof JobParameters) { |
|
| 222 | + $this->_job_parameters = new JobParameters($this->_job_id, __('__Unknown__', 'event_espresso'), array()); |
|
| 223 | + } |
|
| 224 | + $this->_job_parameters->set_status(JobParameters::status_error); |
|
| 225 | + return new JobStepResponse( |
|
| 226 | + $this->_job_parameters, |
|
| 227 | + sprintf( |
|
| 228 | + __( |
|
| 229 | + 'An exception of type %1$s occurred while running %2$s. Its message was %3$s and had trace %4$s', |
|
| 230 | + 'event_espresso' |
|
| 231 | + ), |
|
| 232 | + get_class($exception), |
|
| 233 | + 'BatchRunner::' . $method_name . '()', |
|
| 234 | + $exception->getMessage(), |
|
| 235 | + $exception->getTraceAsString() |
|
| 236 | + ) |
|
| 237 | + ); |
|
| 238 | + } |
|
| 239 | 239 | } |
@@ -69,7 +69,7 @@ discard block |
||
| 69 | 69 | $obj = $this->instantiate_batch_job_handler_from_classname($batch_job_handler_class); |
| 70 | 70 | $this->_job_parameters = new JobParameters($this->_job_id, $batch_job_handler_class, $request_data); |
| 71 | 71 | $response = $obj->create_job($this->_job_parameters); |
| 72 | - if (! $response instanceof JobStepResponse) { |
|
| 72 | + if ( ! $response instanceof JobStepResponse) { |
|
| 73 | 73 | throw new BatchRequestException( |
| 74 | 74 | sprintf( |
| 75 | 75 | __( |
@@ -81,7 +81,7 @@ discard block |
||
| 81 | 81 | ); |
| 82 | 82 | } |
| 83 | 83 | $success = $this->_job_parameters->save(true); |
| 84 | - if (! $success) { |
|
| 84 | + if ( ! $success) { |
|
| 85 | 85 | throw new BatchRequestException( |
| 86 | 86 | sprintf( |
| 87 | 87 | __( |
@@ -117,7 +117,7 @@ discard block |
||
| 117 | 117 | $handler_obj = $this->instantiate_batch_job_handler_from_classname($this->_job_parameters->classname()); |
| 118 | 118 | // continue it |
| 119 | 119 | $response = $handler_obj->continue_job($this->_job_parameters, $batch_size); |
| 120 | - if (! $response instanceof JobStepResponse) { |
|
| 120 | + if ( ! $response instanceof JobStepResponse) { |
|
| 121 | 121 | throw new BatchRequestException( |
| 122 | 122 | sprintf( |
| 123 | 123 | __( |
@@ -146,7 +146,7 @@ discard block |
||
| 146 | 146 | */ |
| 147 | 147 | public function instantiate_batch_job_handler_from_classname($classname) |
| 148 | 148 | { |
| 149 | - if (! class_exists($classname)) { |
|
| 149 | + if ( ! class_exists($classname)) { |
|
| 150 | 150 | throw new BatchRequestException( |
| 151 | 151 | sprintf( |
| 152 | 152 | __( |
@@ -158,7 +158,7 @@ discard block |
||
| 158 | 158 | ); |
| 159 | 159 | } |
| 160 | 160 | $obj = $this->loader->getNew($classname); |
| 161 | - if (! $obj instanceof JobHandlerInterface) { |
|
| 161 | + if ( ! $obj instanceof JobHandlerInterface) { |
|
| 162 | 162 | throw new BatchRequestException( |
| 163 | 163 | sprintf( |
| 164 | 164 | __( |
@@ -188,7 +188,7 @@ discard block |
||
| 188 | 188 | $handler_obj = $this->instantiate_batch_job_handler_from_classname($job_parameters->classname()); |
| 189 | 189 | // continue it |
| 190 | 190 | $response = $handler_obj->cleanup_job($job_parameters); |
| 191 | - if (! $response instanceof JobStepResponse) { |
|
| 191 | + if ( ! $response instanceof JobStepResponse) { |
|
| 192 | 192 | throw new BatchRequestException( |
| 193 | 193 | sprintf( |
| 194 | 194 | __( |
@@ -218,7 +218,7 @@ discard block |
||
| 218 | 218 | */ |
| 219 | 219 | protected function _get_error_response(\Exception $exception, $method_name) |
| 220 | 220 | { |
| 221 | - if (! $this->_job_parameters instanceof JobParameters) { |
|
| 221 | + if ( ! $this->_job_parameters instanceof JobParameters) { |
|
| 222 | 222 | $this->_job_parameters = new JobParameters($this->_job_id, __('__Unknown__', 'event_espresso'), array()); |
| 223 | 223 | } |
| 224 | 224 | $this->_job_parameters->set_status(JobParameters::status_error); |
@@ -230,7 +230,7 @@ discard block |
||
| 230 | 230 | 'event_espresso' |
| 231 | 231 | ), |
| 232 | 232 | get_class($exception), |
| 233 | - 'BatchRunner::' . $method_name . '()', |
|
| 233 | + 'BatchRunner::'.$method_name.'()', |
|
| 234 | 234 | $exception->getMessage(), |
| 235 | 235 | $exception->getTraceAsString() |
| 236 | 236 | ) |
@@ -10,11 +10,11 @@ |
||
| 10 | 10 | */ |
| 11 | 11 | class EE_File_Input_Display_Strategy extends EE_Text_Input_Display_Strategy |
| 12 | 12 | { |
| 13 | - /** |
|
| 14 | - * Override's parent to just set the type. May someday support other arguments. |
|
| 15 | - */ |
|
| 16 | - public function __construct() |
|
| 17 | - { |
|
| 18 | - parent::__construct('file'); |
|
| 19 | - } |
|
| 13 | + /** |
|
| 14 | + * Override's parent to just set the type. May someday support other arguments. |
|
| 15 | + */ |
|
| 16 | + public function __construct() |
|
| 17 | + { |
|
| 18 | + parent::__construct('file'); |
|
| 19 | + } |
|
| 20 | 20 | } |
@@ -15,37 +15,37 @@ |
||
| 15 | 15 | interface FileSubmissionInterface |
| 16 | 16 | { |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * @return string |
|
| 20 | - */ |
|
| 21 | - public function getName(); |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * @return string |
|
| 25 | - */ |
|
| 26 | - public function getType(); |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * @return int |
|
| 30 | - */ |
|
| 31 | - public function getSize(); |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * @return string |
|
| 35 | - */ |
|
| 36 | - public function getTmpFile(); |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * Should just return the filename. |
|
| 40 | - * @since $VID:$ |
|
| 41 | - * @return string |
|
| 42 | - */ |
|
| 43 | - public function __toString(); |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * @return string |
|
| 47 | - */ |
|
| 48 | - public function getErrorCode(); |
|
| 18 | + /** |
|
| 19 | + * @return string |
|
| 20 | + */ |
|
| 21 | + public function getName(); |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * @return string |
|
| 25 | + */ |
|
| 26 | + public function getType(); |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * @return int |
|
| 30 | + */ |
|
| 31 | + public function getSize(); |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * @return string |
|
| 35 | + */ |
|
| 36 | + public function getTmpFile(); |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * Should just return the filename. |
|
| 40 | + * @since $VID:$ |
|
| 41 | + * @return string |
|
| 42 | + */ |
|
| 43 | + public function __toString(); |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * @return string |
|
| 47 | + */ |
|
| 48 | + public function getErrorCode(); |
|
| 49 | 49 | } |
| 50 | 50 | // End of file FileSubmissionInterface.php |
| 51 | 51 | // Location: EventEspresso\core\services\request\files/FileSubmissionInterface.php |
@@ -19,86 +19,86 @@ |
||
| 19 | 19 | */ |
| 20 | 20 | class EE_File_Input extends EE_Form_Input_Base |
| 21 | 21 | { |
| 22 | - /** |
|
| 23 | - * @var array |
|
| 24 | - */ |
|
| 25 | - protected $allowed_file_extensions; |
|
| 22 | + /** |
|
| 23 | + * @var array |
|
| 24 | + */ |
|
| 25 | + protected $allowed_file_extensions; |
|
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * @var array |
|
| 29 | - */ |
|
| 30 | - protected $allowed_mime_types; |
|
| 27 | + /** |
|
| 28 | + * @var array |
|
| 29 | + */ |
|
| 30 | + protected $allowed_mime_types; |
|
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * @param array $options |
|
| 34 | - * @throws InvalidArgumentException |
|
| 35 | - */ |
|
| 36 | - public function __construct($options = array()) |
|
| 37 | - { |
|
| 38 | - if (isset($options['allowed_file_extensions'])) { |
|
| 39 | - $this->allowed_file_extensions = (array) $options['allowed_file_extensions']; |
|
| 40 | - } else { |
|
| 41 | - $this->allowed_file_extensions = ['csv']; |
|
| 42 | - } |
|
| 43 | - if (isset($options['allowed_mime_types'])) { |
|
| 44 | - $this->allowed_mime_types = (array) $options['allowed_file_extensions']; |
|
| 45 | - } else { |
|
| 46 | - $this->allowed_mime_types = ['text/csv']; |
|
| 47 | - } |
|
| 32 | + /** |
|
| 33 | + * @param array $options |
|
| 34 | + * @throws InvalidArgumentException |
|
| 35 | + */ |
|
| 36 | + public function __construct($options = array()) |
|
| 37 | + { |
|
| 38 | + if (isset($options['allowed_file_extensions'])) { |
|
| 39 | + $this->allowed_file_extensions = (array) $options['allowed_file_extensions']; |
|
| 40 | + } else { |
|
| 41 | + $this->allowed_file_extensions = ['csv']; |
|
| 42 | + } |
|
| 43 | + if (isset($options['allowed_mime_types'])) { |
|
| 44 | + $this->allowed_mime_types = (array) $options['allowed_file_extensions']; |
|
| 45 | + } else { |
|
| 46 | + $this->allowed_mime_types = ['text/csv']; |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | - $this->_set_display_strategy(new EE_File_Input_Display_Strategy()); |
|
| 50 | - $this->_set_normalization_strategy(new EE_File_Normalization()); |
|
| 51 | - $this->add_validation_strategy( |
|
| 52 | - new EE_Text_Validation_Strategy( |
|
| 53 | - sprintf( |
|
| 54 | - // translators: %1$s is a list of allowed file extensions. |
|
| 55 | - esc_html__('Please provide a file of the requested filetype: %1$s', 'event_espresso'), |
|
| 56 | - implode(', ', $this->allowed_file_extensions) |
|
| 57 | - ), |
|
| 58 | - '~.*\.(' . implode('|', $this->allowed_file_extensions) . ')$~' |
|
| 59 | - ) |
|
| 60 | - ); |
|
| 61 | - parent::__construct($options); |
|
| 49 | + $this->_set_display_strategy(new EE_File_Input_Display_Strategy()); |
|
| 50 | + $this->_set_normalization_strategy(new EE_File_Normalization()); |
|
| 51 | + $this->add_validation_strategy( |
|
| 52 | + new EE_Text_Validation_Strategy( |
|
| 53 | + sprintf( |
|
| 54 | + // translators: %1$s is a list of allowed file extensions. |
|
| 55 | + esc_html__('Please provide a file of the requested filetype: %1$s', 'event_espresso'), |
|
| 56 | + implode(', ', $this->allowed_file_extensions) |
|
| 57 | + ), |
|
| 58 | + '~.*\.(' . implode('|', $this->allowed_file_extensions) . ')$~' |
|
| 59 | + ) |
|
| 60 | + ); |
|
| 61 | + parent::__construct($options); |
|
| 62 | 62 | |
| 63 | 63 | // It would be great to add this HTML attribute, but jQuery validate chokes on it. |
| 64 | - $this->set_other_html_attributes( |
|
| 65 | - $this->other_html_attributes() |
|
| 66 | - . ' extension="' |
|
| 67 | - . implode( |
|
| 68 | - ',', |
|
| 69 | - $this->allowed_file_extensions |
|
| 70 | - ) |
|
| 71 | - . '"' |
|
| 72 | - ); |
|
| 73 | - } |
|
| 64 | + $this->set_other_html_attributes( |
|
| 65 | + $this->other_html_attributes() |
|
| 66 | + . ' extension="' |
|
| 67 | + . implode( |
|
| 68 | + ',', |
|
| 69 | + $this->allowed_file_extensions |
|
| 70 | + ) |
|
| 71 | + . '"' |
|
| 72 | + ); |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - /** |
|
| 76 | - * $_FILES has a really weird structure. So we let `FilesDataHandler` take care of finding the file info for |
|
| 77 | - * this input. |
|
| 78 | - * @since $VID:$ |
|
| 79 | - * @param array $req_data |
|
| 80 | - * @return FileSubmissionInterface |
|
| 81 | - * @throws InvalidArgumentException |
|
| 82 | - * @throws InvalidDataTypeException |
|
| 83 | - * @throws InvalidInterfaceException |
|
| 84 | - */ |
|
| 85 | - public function find_form_data_for_this_section($req_data) |
|
| 86 | - { |
|
| 87 | - // ignore $req_data. Files are in the files data handler. |
|
| 88 | - $fileDataHandler = LoaderFactory::getLoader()->getShared( |
|
| 89 | - 'EventEspresso\core\services\request\files\FilesDataHandler' |
|
| 90 | - ); |
|
| 91 | - return $fileDataHandler->getFileObject($this->html_name()); |
|
| 92 | - } |
|
| 75 | + /** |
|
| 76 | + * $_FILES has a really weird structure. So we let `FilesDataHandler` take care of finding the file info for |
|
| 77 | + * this input. |
|
| 78 | + * @since $VID:$ |
|
| 79 | + * @param array $req_data |
|
| 80 | + * @return FileSubmissionInterface |
|
| 81 | + * @throws InvalidArgumentException |
|
| 82 | + * @throws InvalidDataTypeException |
|
| 83 | + * @throws InvalidInterfaceException |
|
| 84 | + */ |
|
| 85 | + public function find_form_data_for_this_section($req_data) |
|
| 86 | + { |
|
| 87 | + // ignore $req_data. Files are in the files data handler. |
|
| 88 | + $fileDataHandler = LoaderFactory::getLoader()->getShared( |
|
| 89 | + 'EventEspresso\core\services\request\files\FilesDataHandler' |
|
| 90 | + ); |
|
| 91 | + return $fileDataHandler->getFileObject($this->html_name()); |
|
| 92 | + } |
|
| 93 | 93 | |
| 94 | - /** |
|
| 95 | - * Don't transform the file submission object into a string, thanks. |
|
| 96 | - * |
|
| 97 | - * @param string $value |
|
| 98 | - * @return null|string |
|
| 99 | - */ |
|
| 100 | - protected function _sanitize($value) |
|
| 101 | - { |
|
| 102 | - return $value; |
|
| 103 | - } |
|
| 94 | + /** |
|
| 95 | + * Don't transform the file submission object into a string, thanks. |
|
| 96 | + * |
|
| 97 | + * @param string $value |
|
| 98 | + * @return null|string |
|
| 99 | + */ |
|
| 100 | + protected function _sanitize($value) |
|
| 101 | + { |
|
| 102 | + return $value; |
|
| 103 | + } |
|
| 104 | 104 | } |
@@ -55,7 +55,7 @@ |
||
| 55 | 55 | esc_html__('Please provide a file of the requested filetype: %1$s', 'event_espresso'), |
| 56 | 56 | implode(', ', $this->allowed_file_extensions) |
| 57 | 57 | ), |
| 58 | - '~.*\.(' . implode('|', $this->allowed_file_extensions) . ')$~' |
|
| 58 | + '~.*\.('.implode('|', $this->allowed_file_extensions).')$~' |
|
| 59 | 59 | ) |
| 60 | 60 | ); |
| 61 | 61 | parent::__construct($options); |
@@ -29,639 +29,639 @@ |
||
| 29 | 29 | abstract class FormHandler implements FormHandlerInterface |
| 30 | 30 | { |
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * will add opening and closing HTML form tags as well as a submit button |
|
| 34 | - */ |
|
| 35 | - const ADD_FORM_TAGS_AND_SUBMIT = 'add_form_tags_and_submit'; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * will add opening and closing HTML form tags but NOT a submit button |
|
| 39 | - */ |
|
| 40 | - const ADD_FORM_TAGS_ONLY = 'add_form_tags_only'; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * will NOT add opening and closing HTML form tags but will add a submit button |
|
| 44 | - */ |
|
| 45 | - const ADD_FORM_SUBMIT_ONLY = 'add_form_submit_only'; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * will NOT add opening and closing HTML form tags NOR a submit button |
|
| 49 | - */ |
|
| 50 | - const DO_NOT_SETUP_FORM = 'do_not_setup_form'; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * if set to false, then this form has no displayable content, |
|
| 54 | - * and will only be used for processing data sent passed via GET or POST |
|
| 55 | - * defaults to true ( ie: form has displayable content ) |
|
| 56 | - * |
|
| 57 | - * @var boolean $displayable |
|
| 58 | - */ |
|
| 59 | - private $displayable = true; |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * @var string $form_name |
|
| 63 | - */ |
|
| 64 | - private $form_name; |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * @var string $admin_name |
|
| 68 | - */ |
|
| 69 | - private $admin_name; |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * @var string $slug |
|
| 73 | - */ |
|
| 74 | - private $slug; |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * @var string $submit_btn_text |
|
| 78 | - */ |
|
| 79 | - private $submit_btn_text; |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * @var string $form_action |
|
| 83 | - */ |
|
| 84 | - private $form_action; |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * form params in key value pairs |
|
| 88 | - * can be added to form action URL or as hidden inputs |
|
| 89 | - * |
|
| 90 | - * @var array $form_args |
|
| 91 | - */ |
|
| 92 | - private $form_args = array(); |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * value of one of the string constant above |
|
| 96 | - * |
|
| 97 | - * @var string $form_config |
|
| 98 | - */ |
|
| 99 | - private $form_config; |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * whether or not the form was determined to be invalid |
|
| 103 | - * |
|
| 104 | - * @var boolean $form_has_errors |
|
| 105 | - */ |
|
| 106 | - private $form_has_errors; |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * the absolute top level form section being used on the page |
|
| 110 | - * |
|
| 111 | - * @var EE_Form_Section_Proper $form |
|
| 112 | - */ |
|
| 113 | - private $form; |
|
| 114 | - |
|
| 115 | - /** |
|
| 116 | - * @var EE_Registry $registry |
|
| 117 | - */ |
|
| 118 | - protected $registry; |
|
| 119 | - |
|
| 120 | - // phpcs:disable PEAR.Functions.ValidDefaultValue.NotAtEnd |
|
| 121 | - /** |
|
| 122 | - * Form constructor. |
|
| 123 | - * |
|
| 124 | - * @param string $form_name |
|
| 125 | - * @param string $admin_name |
|
| 126 | - * @param string $slug |
|
| 127 | - * @param string $form_action |
|
| 128 | - * @param string $form_config |
|
| 129 | - * @param EE_Registry $registry |
|
| 130 | - * @throws InvalidDataTypeException |
|
| 131 | - * @throws DomainException |
|
| 132 | - * @throws InvalidArgumentException |
|
| 133 | - */ |
|
| 134 | - public function __construct( |
|
| 135 | - $form_name, |
|
| 136 | - $admin_name, |
|
| 137 | - $slug, |
|
| 138 | - $form_action = '', |
|
| 139 | - $form_config = FormHandler::ADD_FORM_TAGS_AND_SUBMIT, |
|
| 140 | - EE_Registry $registry |
|
| 141 | - ) { |
|
| 142 | - $this->setFormName($form_name); |
|
| 143 | - $this->setAdminName($admin_name); |
|
| 144 | - $this->setSlug($slug); |
|
| 145 | - $this->setFormAction($form_action); |
|
| 146 | - $this->setFormConfig($form_config); |
|
| 147 | - $this->setSubmitBtnText(esc_html__('Submit', 'event_espresso')); |
|
| 148 | - $this->registry = $registry; |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - |
|
| 152 | - /** |
|
| 153 | - * @return array |
|
| 154 | - */ |
|
| 155 | - public static function getFormConfigConstants() |
|
| 156 | - { |
|
| 157 | - return array( |
|
| 158 | - FormHandler::ADD_FORM_TAGS_AND_SUBMIT, |
|
| 159 | - FormHandler::ADD_FORM_TAGS_ONLY, |
|
| 160 | - FormHandler::ADD_FORM_SUBMIT_ONLY, |
|
| 161 | - FormHandler::DO_NOT_SETUP_FORM, |
|
| 162 | - ); |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * @param bool $for_display |
|
| 168 | - * @return EE_Form_Section_Proper |
|
| 169 | - * @throws EE_Error |
|
| 170 | - * @throws LogicException |
|
| 171 | - */ |
|
| 172 | - public function form($for_display = false) |
|
| 173 | - { |
|
| 174 | - if (! $this->formIsValid()) { |
|
| 175 | - return null; |
|
| 176 | - } |
|
| 177 | - if ($for_display) { |
|
| 178 | - $form_config = $this->formConfig(); |
|
| 179 | - if ($form_config === FormHandler::ADD_FORM_TAGS_AND_SUBMIT |
|
| 180 | - || $form_config === FormHandler::ADD_FORM_SUBMIT_ONLY |
|
| 181 | - ) { |
|
| 182 | - $this->appendSubmitButton(); |
|
| 183 | - $this->clearFormButtonFloats(); |
|
| 184 | - } |
|
| 185 | - } |
|
| 186 | - return $this->form; |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - |
|
| 190 | - /** |
|
| 191 | - * @return boolean |
|
| 192 | - * @throws LogicException |
|
| 193 | - */ |
|
| 194 | - public function formIsValid() |
|
| 195 | - { |
|
| 196 | - if ($this->form instanceof EE_Form_Section_Proper) { |
|
| 197 | - return true; |
|
| 198 | - } |
|
| 199 | - $form = apply_filters( |
|
| 200 | - 'FHEE__EventEspresso_core_libraries_form_sections_form_handlers_FormHandler__formIsValid__generated_form_object', |
|
| 201 | - $this->generate(), |
|
| 202 | - $this |
|
| 203 | - ); |
|
| 204 | - if ($this->verifyForm($form)) { |
|
| 205 | - $this->setForm($form); |
|
| 206 | - } |
|
| 207 | - return true; |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - |
|
| 211 | - /** |
|
| 212 | - * @param EE_Form_Section_Proper|null $form |
|
| 213 | - * @return bool |
|
| 214 | - * @throws LogicException |
|
| 215 | - */ |
|
| 216 | - public function verifyForm(EE_Form_Section_Proper $form = null) |
|
| 217 | - { |
|
| 218 | - $form = $form !== null ? $form : $this->form; |
|
| 219 | - if ($form instanceof EE_Form_Section_Proper) { |
|
| 220 | - return true; |
|
| 221 | - } |
|
| 222 | - throw new LogicException( |
|
| 223 | - sprintf( |
|
| 224 | - esc_html__('The "%1$s" form is invalid or missing. %2$s', 'event_espresso'), |
|
| 225 | - $this->form_name, |
|
| 226 | - var_export($form, true) |
|
| 227 | - ) |
|
| 228 | - ); |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - |
|
| 232 | - /** |
|
| 233 | - * @param EE_Form_Section_Proper $form |
|
| 234 | - */ |
|
| 235 | - public function setForm(EE_Form_Section_Proper $form) |
|
| 236 | - { |
|
| 237 | - $this->form = $form; |
|
| 238 | - } |
|
| 239 | - |
|
| 240 | - |
|
| 241 | - /** |
|
| 242 | - * @return boolean |
|
| 243 | - */ |
|
| 244 | - public function displayable() |
|
| 245 | - { |
|
| 246 | - return $this->displayable; |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - |
|
| 250 | - /** |
|
| 251 | - * @param boolean $displayable |
|
| 252 | - */ |
|
| 253 | - public function setDisplayable($displayable = false) |
|
| 254 | - { |
|
| 255 | - $this->displayable = filter_var($displayable, FILTER_VALIDATE_BOOLEAN); |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - |
|
| 259 | - /** |
|
| 260 | - * a public name for the form that can be displayed on the frontend of a site |
|
| 261 | - * |
|
| 262 | - * @return string |
|
| 263 | - */ |
|
| 264 | - public function formName() |
|
| 265 | - { |
|
| 266 | - return $this->form_name; |
|
| 267 | - } |
|
| 268 | - |
|
| 269 | - |
|
| 270 | - /** |
|
| 271 | - * @param string $form_name |
|
| 272 | - * @throws InvalidDataTypeException |
|
| 273 | - */ |
|
| 274 | - public function setFormName($form_name) |
|
| 275 | - { |
|
| 276 | - if (! is_string($form_name)) { |
|
| 277 | - throw new InvalidDataTypeException('$form_name', $form_name, 'string'); |
|
| 278 | - } |
|
| 279 | - $this->form_name = $form_name; |
|
| 280 | - } |
|
| 281 | - |
|
| 282 | - |
|
| 283 | - /** |
|
| 284 | - * a public name for the form that can be displayed, but only in the admin |
|
| 285 | - * |
|
| 286 | - * @return string |
|
| 287 | - */ |
|
| 288 | - public function adminName() |
|
| 289 | - { |
|
| 290 | - return $this->admin_name; |
|
| 291 | - } |
|
| 292 | - |
|
| 293 | - |
|
| 294 | - /** |
|
| 295 | - * @param string $admin_name |
|
| 296 | - * @throws InvalidDataTypeException |
|
| 297 | - */ |
|
| 298 | - public function setAdminName($admin_name) |
|
| 299 | - { |
|
| 300 | - if (! is_string($admin_name)) { |
|
| 301 | - throw new InvalidDataTypeException('$admin_name', $admin_name, 'string'); |
|
| 302 | - } |
|
| 303 | - $this->admin_name = $admin_name; |
|
| 304 | - } |
|
| 305 | - |
|
| 306 | - |
|
| 307 | - /** |
|
| 308 | - * a URL friendly string that can be used for identifying the form |
|
| 309 | - * |
|
| 310 | - * @return string |
|
| 311 | - */ |
|
| 312 | - public function slug() |
|
| 313 | - { |
|
| 314 | - return $this->slug; |
|
| 315 | - } |
|
| 316 | - |
|
| 317 | - |
|
| 318 | - /** |
|
| 319 | - * @param string $slug |
|
| 320 | - * @throws InvalidDataTypeException |
|
| 321 | - */ |
|
| 322 | - public function setSlug($slug) |
|
| 323 | - { |
|
| 324 | - if (! is_string($slug)) { |
|
| 325 | - throw new InvalidDataTypeException('$slug', $slug, 'string'); |
|
| 326 | - } |
|
| 327 | - $this->slug = $slug; |
|
| 328 | - } |
|
| 329 | - |
|
| 330 | - |
|
| 331 | - /** |
|
| 332 | - * @return string |
|
| 333 | - */ |
|
| 334 | - public function submitBtnText() |
|
| 335 | - { |
|
| 336 | - return $this->submit_btn_text; |
|
| 337 | - } |
|
| 338 | - |
|
| 339 | - |
|
| 340 | - /** |
|
| 341 | - * @param string $submit_btn_text |
|
| 342 | - * @throws InvalidDataTypeException |
|
| 343 | - * @throws InvalidArgumentException |
|
| 344 | - */ |
|
| 345 | - public function setSubmitBtnText($submit_btn_text) |
|
| 346 | - { |
|
| 347 | - if (! is_string($submit_btn_text)) { |
|
| 348 | - throw new InvalidDataTypeException('$submit_btn_text', $submit_btn_text, 'string'); |
|
| 349 | - } |
|
| 350 | - if (empty($submit_btn_text)) { |
|
| 351 | - throw new InvalidArgumentException( |
|
| 352 | - esc_html__('Can not set Submit button text because an empty string was provided.', 'event_espresso') |
|
| 353 | - ); |
|
| 354 | - } |
|
| 355 | - $this->submit_btn_text = $submit_btn_text; |
|
| 356 | - } |
|
| 357 | - |
|
| 358 | - |
|
| 359 | - /** |
|
| 360 | - * @return string |
|
| 361 | - */ |
|
| 362 | - public function formAction() |
|
| 363 | - { |
|
| 364 | - return ! empty($this->form_args) |
|
| 365 | - ? add_query_arg($this->form_args, $this->form_action) |
|
| 366 | - : $this->form_action; |
|
| 367 | - } |
|
| 368 | - |
|
| 369 | - |
|
| 370 | - /** |
|
| 371 | - * @param string $form_action |
|
| 372 | - * @throws InvalidDataTypeException |
|
| 373 | - */ |
|
| 374 | - public function setFormAction($form_action) |
|
| 375 | - { |
|
| 376 | - if (! is_string($form_action)) { |
|
| 377 | - throw new InvalidDataTypeException('$form_action', $form_action, 'string'); |
|
| 378 | - } |
|
| 379 | - $this->form_action = $form_action; |
|
| 380 | - } |
|
| 381 | - |
|
| 382 | - |
|
| 383 | - /** |
|
| 384 | - * @param array $form_args |
|
| 385 | - * @throws InvalidDataTypeException |
|
| 386 | - * @throws InvalidArgumentException |
|
| 387 | - */ |
|
| 388 | - public function addFormActionArgs($form_args = array()) |
|
| 389 | - { |
|
| 390 | - if (is_object($form_args)) { |
|
| 391 | - throw new InvalidDataTypeException( |
|
| 392 | - '$form_args', |
|
| 393 | - $form_args, |
|
| 394 | - 'anything other than an object was expected.' |
|
| 395 | - ); |
|
| 396 | - } |
|
| 397 | - if (empty($form_args)) { |
|
| 398 | - throw new InvalidArgumentException( |
|
| 399 | - esc_html__('The redirect arguments can not be an empty array.', 'event_espresso') |
|
| 400 | - ); |
|
| 401 | - } |
|
| 402 | - $this->form_args = array_merge($this->form_args, $form_args); |
|
| 403 | - } |
|
| 404 | - |
|
| 405 | - |
|
| 406 | - /** |
|
| 407 | - * @return string |
|
| 408 | - */ |
|
| 409 | - public function formConfig() |
|
| 410 | - { |
|
| 411 | - return $this->form_config; |
|
| 412 | - } |
|
| 413 | - |
|
| 414 | - |
|
| 415 | - /** |
|
| 416 | - * @param string $form_config |
|
| 417 | - * @throws DomainException |
|
| 418 | - */ |
|
| 419 | - public function setFormConfig($form_config) |
|
| 420 | - { |
|
| 421 | - if (! in_array( |
|
| 422 | - $form_config, |
|
| 423 | - array( |
|
| 424 | - FormHandler::ADD_FORM_TAGS_AND_SUBMIT, |
|
| 425 | - FormHandler::ADD_FORM_TAGS_ONLY, |
|
| 426 | - FormHandler::ADD_FORM_SUBMIT_ONLY, |
|
| 427 | - FormHandler::DO_NOT_SETUP_FORM, |
|
| 428 | - ), |
|
| 429 | - true |
|
| 430 | - ) |
|
| 431 | - ) { |
|
| 432 | - throw new DomainException( |
|
| 433 | - sprintf( |
|
| 434 | - esc_html__( |
|
| 435 | - '"%1$s" is not a valid value for the form config. Please use one of the class constants on \EventEspresso\core\libraries\form_sections\form_handlers\Form', |
|
| 436 | - 'event_espresso' |
|
| 437 | - ), |
|
| 438 | - $form_config |
|
| 439 | - ) |
|
| 440 | - ); |
|
| 441 | - } |
|
| 442 | - $this->form_config = $form_config; |
|
| 443 | - } |
|
| 444 | - |
|
| 445 | - |
|
| 446 | - /** |
|
| 447 | - * called after the form is instantiated |
|
| 448 | - * and used for performing any logic that needs to occur early |
|
| 449 | - * before any of the other methods are called. |
|
| 450 | - * returns true if everything is ok to proceed, |
|
| 451 | - * and false if no further form logic should be implemented |
|
| 452 | - * |
|
| 453 | - * @return boolean |
|
| 454 | - */ |
|
| 455 | - public function initialize() |
|
| 456 | - { |
|
| 457 | - $this->form_has_errors = EE_Error::has_error(true); |
|
| 458 | - return true; |
|
| 459 | - } |
|
| 460 | - |
|
| 461 | - |
|
| 462 | - /** |
|
| 463 | - * used for setting up css and js |
|
| 464 | - * |
|
| 465 | - * @return void |
|
| 466 | - * @throws LogicException |
|
| 467 | - * @throws EE_Error |
|
| 468 | - */ |
|
| 469 | - public function enqueueStylesAndScripts() |
|
| 470 | - { |
|
| 471 | - $this->form()->enqueue_js(); |
|
| 472 | - } |
|
| 473 | - |
|
| 474 | - |
|
| 475 | - /** |
|
| 476 | - * creates and returns the actual form |
|
| 477 | - * |
|
| 478 | - * @return EE_Form_Section_Proper |
|
| 479 | - */ |
|
| 480 | - abstract public function generate(); |
|
| 481 | - |
|
| 482 | - |
|
| 483 | - /** |
|
| 484 | - * creates and returns an EE_Submit_Input labeled "Submit" |
|
| 485 | - * |
|
| 486 | - * @param string $text |
|
| 487 | - * @return EE_Submit_Input |
|
| 488 | - */ |
|
| 489 | - public function generateSubmitButton($text = '') |
|
| 490 | - { |
|
| 491 | - $text = ! empty($text) ? $text : $this->submitBtnText(); |
|
| 492 | - return new EE_Submit_Input( |
|
| 493 | - array( |
|
| 494 | - 'html_name' => 'ee-form-submit-' . $this->slug(), |
|
| 495 | - 'html_id' => 'ee-form-submit-' . $this->slug(), |
|
| 496 | - 'html_class' => 'ee-form-submit', |
|
| 497 | - 'html_label' => ' ', |
|
| 498 | - 'other_html_attributes' => ' rel="' . $this->slug() . '"', |
|
| 499 | - 'default' => $text, |
|
| 500 | - ) |
|
| 501 | - ); |
|
| 502 | - } |
|
| 503 | - |
|
| 504 | - |
|
| 505 | - /** |
|
| 506 | - * calls generateSubmitButton() and appends it onto the form along with a float clearing div |
|
| 507 | - * |
|
| 508 | - * @param string $text |
|
| 509 | - * @return void |
|
| 510 | - * @throws EE_Error |
|
| 511 | - */ |
|
| 512 | - public function appendSubmitButton($text = '') |
|
| 513 | - { |
|
| 514 | - if ($this->form->subsection_exists($this->slug() . '-submit-btn')) { |
|
| 515 | - return; |
|
| 516 | - } |
|
| 517 | - $this->form->add_subsections( |
|
| 518 | - array($this->slug() . '-submit-btn' => $this->generateSubmitButton($text)), |
|
| 519 | - null, |
|
| 520 | - false |
|
| 521 | - ); |
|
| 522 | - } |
|
| 523 | - |
|
| 524 | - |
|
| 525 | - /** |
|
| 526 | - * creates and returns an EE_Submit_Input labeled "Cancel" |
|
| 527 | - * |
|
| 528 | - * @param string $text |
|
| 529 | - * @return EE_Submit_Input |
|
| 530 | - */ |
|
| 531 | - public function generateCancelButton($text = '') |
|
| 532 | - { |
|
| 533 | - $cancel_button = new EE_Submit_Input( |
|
| 534 | - array( |
|
| 535 | - 'html_name' => 'ee-form-submit-' . $this->slug(), // YES! Same name as submit !!! |
|
| 536 | - 'html_id' => 'ee-cancel-form-' . $this->slug(), |
|
| 537 | - 'html_class' => 'ee-cancel-form', |
|
| 538 | - 'html_label' => ' ', |
|
| 539 | - 'other_html_attributes' => ' rel="' . $this->slug() . '"', |
|
| 540 | - 'default' => ! empty($text) ? $text : esc_html__('Cancel', 'event_espresso'), |
|
| 541 | - ) |
|
| 542 | - ); |
|
| 543 | - $cancel_button->set_button_css_attributes(false); |
|
| 544 | - return $cancel_button; |
|
| 545 | - } |
|
| 546 | - |
|
| 547 | - |
|
| 548 | - /** |
|
| 549 | - * appends a float clearing div onto end of form |
|
| 550 | - * |
|
| 551 | - * @return void |
|
| 552 | - * @throws EE_Error |
|
| 553 | - */ |
|
| 554 | - public function clearFormButtonFloats() |
|
| 555 | - { |
|
| 556 | - $this->form->add_subsections( |
|
| 557 | - array( |
|
| 558 | - 'clear-submit-btn-float' => new EE_Form_Section_HTML( |
|
| 559 | - EEH_HTML::div('', '', 'clear-float') . EEH_HTML::divx() |
|
| 560 | - ), |
|
| 561 | - ), |
|
| 562 | - null, |
|
| 563 | - false |
|
| 564 | - ); |
|
| 565 | - } |
|
| 566 | - |
|
| 567 | - |
|
| 568 | - /** |
|
| 569 | - * takes the generated form and displays it along with ony other non-form HTML that may be required |
|
| 570 | - * returns a string of HTML that can be directly echoed in a template |
|
| 571 | - * |
|
| 572 | - * @return string |
|
| 573 | - * @throws \InvalidArgumentException |
|
| 574 | - * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 575 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 576 | - * @throws LogicException |
|
| 577 | - * @throws EE_Error |
|
| 578 | - */ |
|
| 579 | - public function display() |
|
| 580 | - { |
|
| 581 | - $form_html = apply_filters( |
|
| 582 | - 'FHEE__EventEspresso_core_libraries_form_sections_form_handlers_FormHandler__display__before_form', |
|
| 583 | - '' |
|
| 584 | - ); |
|
| 585 | - $form_config = $this->formConfig(); |
|
| 586 | - if ($form_config === FormHandler::ADD_FORM_TAGS_AND_SUBMIT |
|
| 587 | - || $form_config === FormHandler::ADD_FORM_TAGS_ONLY |
|
| 588 | - ) { |
|
| 589 | - $additional_props = $this->requiresMultipartEnctype() |
|
| 590 | - ? 'enctype="multipart/form-data"' |
|
| 591 | - : ''; |
|
| 592 | - $form_html .= $this->form()->form_open( |
|
| 593 | - $this->formAction(), |
|
| 594 | - 'POST', |
|
| 595 | - $additional_props |
|
| 596 | - ); |
|
| 597 | - } |
|
| 598 | - $form_html .= $this->form(true)->get_html(); |
|
| 599 | - if ($form_config === FormHandler::ADD_FORM_TAGS_AND_SUBMIT |
|
| 600 | - || $form_config === FormHandler::ADD_FORM_TAGS_ONLY |
|
| 601 | - ) { |
|
| 602 | - $form_html .= $this->form()->form_close(); |
|
| 603 | - } |
|
| 604 | - $form_html .= apply_filters( |
|
| 605 | - 'FHEE__EventEspresso_core_libraries_form_sections_form_handlers_FormHandler__display__after_form', |
|
| 606 | - '' |
|
| 607 | - ); |
|
| 608 | - return $form_html; |
|
| 609 | - } |
|
| 610 | - |
|
| 611 | - /** |
|
| 612 | - * Determines if this form needs "enctype='multipart/form-data'" or not. |
|
| 613 | - * @since $VID:$ |
|
| 614 | - * @return bool |
|
| 615 | - * @throws EE_Error |
|
| 616 | - */ |
|
| 617 | - public function requiresMultipartEnctype() |
|
| 618 | - { |
|
| 619 | - foreach ($this->form()->inputs_in_subsections() as $input) { |
|
| 620 | - if ($input instanceof EE_File_Input) { |
|
| 621 | - return true; |
|
| 622 | - } |
|
| 623 | - } |
|
| 624 | - return false; |
|
| 625 | - } |
|
| 626 | - |
|
| 627 | - |
|
| 628 | - /** |
|
| 629 | - * handles processing the form submission |
|
| 630 | - * returns true or false depending on whether the form was processed successfully or not |
|
| 631 | - * |
|
| 632 | - * @param array $submitted_form_data |
|
| 633 | - * @return array |
|
| 634 | - * @throws \InvalidArgumentException |
|
| 635 | - * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 636 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 637 | - * @throws EE_Error |
|
| 638 | - * @throws LogicException |
|
| 639 | - * @throws InvalidFormSubmissionException |
|
| 640 | - */ |
|
| 641 | - public function process($submitted_form_data = array()) |
|
| 642 | - { |
|
| 643 | - if (! $this->form()->was_submitted($submitted_form_data)) { |
|
| 644 | - throw new InvalidFormSubmissionException($this->form_name); |
|
| 645 | - } |
|
| 646 | - $this->form(true)->receive_form_submission($submitted_form_data); |
|
| 647 | - if (! $this->form()->is_valid()) { |
|
| 648 | - throw new InvalidFormSubmissionException( |
|
| 649 | - $this->form_name, |
|
| 650 | - sprintf( |
|
| 651 | - esc_html__( |
|
| 652 | - 'The "%1$s" form is invalid. Please correct the following errors and resubmit: %2$s %3$s', |
|
| 653 | - 'event_espresso' |
|
| 654 | - ), |
|
| 655 | - $this->form_name, |
|
| 656 | - '<br />', |
|
| 657 | - implode('<br />', $this->form()->get_validation_errors_accumulated()) |
|
| 658 | - ) |
|
| 659 | - ); |
|
| 660 | - } |
|
| 661 | - return apply_filters( |
|
| 662 | - 'FHEE__EventEspresso_core_libraries_form_sections_form_handlers_FormHandler__process__valid_data', |
|
| 663 | - $this->form()->valid_data(), |
|
| 664 | - $this |
|
| 665 | - ); |
|
| 666 | - } |
|
| 32 | + /** |
|
| 33 | + * will add opening and closing HTML form tags as well as a submit button |
|
| 34 | + */ |
|
| 35 | + const ADD_FORM_TAGS_AND_SUBMIT = 'add_form_tags_and_submit'; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * will add opening and closing HTML form tags but NOT a submit button |
|
| 39 | + */ |
|
| 40 | + const ADD_FORM_TAGS_ONLY = 'add_form_tags_only'; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * will NOT add opening and closing HTML form tags but will add a submit button |
|
| 44 | + */ |
|
| 45 | + const ADD_FORM_SUBMIT_ONLY = 'add_form_submit_only'; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * will NOT add opening and closing HTML form tags NOR a submit button |
|
| 49 | + */ |
|
| 50 | + const DO_NOT_SETUP_FORM = 'do_not_setup_form'; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * if set to false, then this form has no displayable content, |
|
| 54 | + * and will only be used for processing data sent passed via GET or POST |
|
| 55 | + * defaults to true ( ie: form has displayable content ) |
|
| 56 | + * |
|
| 57 | + * @var boolean $displayable |
|
| 58 | + */ |
|
| 59 | + private $displayable = true; |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * @var string $form_name |
|
| 63 | + */ |
|
| 64 | + private $form_name; |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * @var string $admin_name |
|
| 68 | + */ |
|
| 69 | + private $admin_name; |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * @var string $slug |
|
| 73 | + */ |
|
| 74 | + private $slug; |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * @var string $submit_btn_text |
|
| 78 | + */ |
|
| 79 | + private $submit_btn_text; |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * @var string $form_action |
|
| 83 | + */ |
|
| 84 | + private $form_action; |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * form params in key value pairs |
|
| 88 | + * can be added to form action URL or as hidden inputs |
|
| 89 | + * |
|
| 90 | + * @var array $form_args |
|
| 91 | + */ |
|
| 92 | + private $form_args = array(); |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * value of one of the string constant above |
|
| 96 | + * |
|
| 97 | + * @var string $form_config |
|
| 98 | + */ |
|
| 99 | + private $form_config; |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * whether or not the form was determined to be invalid |
|
| 103 | + * |
|
| 104 | + * @var boolean $form_has_errors |
|
| 105 | + */ |
|
| 106 | + private $form_has_errors; |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * the absolute top level form section being used on the page |
|
| 110 | + * |
|
| 111 | + * @var EE_Form_Section_Proper $form |
|
| 112 | + */ |
|
| 113 | + private $form; |
|
| 114 | + |
|
| 115 | + /** |
|
| 116 | + * @var EE_Registry $registry |
|
| 117 | + */ |
|
| 118 | + protected $registry; |
|
| 119 | + |
|
| 120 | + // phpcs:disable PEAR.Functions.ValidDefaultValue.NotAtEnd |
|
| 121 | + /** |
|
| 122 | + * Form constructor. |
|
| 123 | + * |
|
| 124 | + * @param string $form_name |
|
| 125 | + * @param string $admin_name |
|
| 126 | + * @param string $slug |
|
| 127 | + * @param string $form_action |
|
| 128 | + * @param string $form_config |
|
| 129 | + * @param EE_Registry $registry |
|
| 130 | + * @throws InvalidDataTypeException |
|
| 131 | + * @throws DomainException |
|
| 132 | + * @throws InvalidArgumentException |
|
| 133 | + */ |
|
| 134 | + public function __construct( |
|
| 135 | + $form_name, |
|
| 136 | + $admin_name, |
|
| 137 | + $slug, |
|
| 138 | + $form_action = '', |
|
| 139 | + $form_config = FormHandler::ADD_FORM_TAGS_AND_SUBMIT, |
|
| 140 | + EE_Registry $registry |
|
| 141 | + ) { |
|
| 142 | + $this->setFormName($form_name); |
|
| 143 | + $this->setAdminName($admin_name); |
|
| 144 | + $this->setSlug($slug); |
|
| 145 | + $this->setFormAction($form_action); |
|
| 146 | + $this->setFormConfig($form_config); |
|
| 147 | + $this->setSubmitBtnText(esc_html__('Submit', 'event_espresso')); |
|
| 148 | + $this->registry = $registry; |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + |
|
| 152 | + /** |
|
| 153 | + * @return array |
|
| 154 | + */ |
|
| 155 | + public static function getFormConfigConstants() |
|
| 156 | + { |
|
| 157 | + return array( |
|
| 158 | + FormHandler::ADD_FORM_TAGS_AND_SUBMIT, |
|
| 159 | + FormHandler::ADD_FORM_TAGS_ONLY, |
|
| 160 | + FormHandler::ADD_FORM_SUBMIT_ONLY, |
|
| 161 | + FormHandler::DO_NOT_SETUP_FORM, |
|
| 162 | + ); |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * @param bool $for_display |
|
| 168 | + * @return EE_Form_Section_Proper |
|
| 169 | + * @throws EE_Error |
|
| 170 | + * @throws LogicException |
|
| 171 | + */ |
|
| 172 | + public function form($for_display = false) |
|
| 173 | + { |
|
| 174 | + if (! $this->formIsValid()) { |
|
| 175 | + return null; |
|
| 176 | + } |
|
| 177 | + if ($for_display) { |
|
| 178 | + $form_config = $this->formConfig(); |
|
| 179 | + if ($form_config === FormHandler::ADD_FORM_TAGS_AND_SUBMIT |
|
| 180 | + || $form_config === FormHandler::ADD_FORM_SUBMIT_ONLY |
|
| 181 | + ) { |
|
| 182 | + $this->appendSubmitButton(); |
|
| 183 | + $this->clearFormButtonFloats(); |
|
| 184 | + } |
|
| 185 | + } |
|
| 186 | + return $this->form; |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + |
|
| 190 | + /** |
|
| 191 | + * @return boolean |
|
| 192 | + * @throws LogicException |
|
| 193 | + */ |
|
| 194 | + public function formIsValid() |
|
| 195 | + { |
|
| 196 | + if ($this->form instanceof EE_Form_Section_Proper) { |
|
| 197 | + return true; |
|
| 198 | + } |
|
| 199 | + $form = apply_filters( |
|
| 200 | + 'FHEE__EventEspresso_core_libraries_form_sections_form_handlers_FormHandler__formIsValid__generated_form_object', |
|
| 201 | + $this->generate(), |
|
| 202 | + $this |
|
| 203 | + ); |
|
| 204 | + if ($this->verifyForm($form)) { |
|
| 205 | + $this->setForm($form); |
|
| 206 | + } |
|
| 207 | + return true; |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + |
|
| 211 | + /** |
|
| 212 | + * @param EE_Form_Section_Proper|null $form |
|
| 213 | + * @return bool |
|
| 214 | + * @throws LogicException |
|
| 215 | + */ |
|
| 216 | + public function verifyForm(EE_Form_Section_Proper $form = null) |
|
| 217 | + { |
|
| 218 | + $form = $form !== null ? $form : $this->form; |
|
| 219 | + if ($form instanceof EE_Form_Section_Proper) { |
|
| 220 | + return true; |
|
| 221 | + } |
|
| 222 | + throw new LogicException( |
|
| 223 | + sprintf( |
|
| 224 | + esc_html__('The "%1$s" form is invalid or missing. %2$s', 'event_espresso'), |
|
| 225 | + $this->form_name, |
|
| 226 | + var_export($form, true) |
|
| 227 | + ) |
|
| 228 | + ); |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + |
|
| 232 | + /** |
|
| 233 | + * @param EE_Form_Section_Proper $form |
|
| 234 | + */ |
|
| 235 | + public function setForm(EE_Form_Section_Proper $form) |
|
| 236 | + { |
|
| 237 | + $this->form = $form; |
|
| 238 | + } |
|
| 239 | + |
|
| 240 | + |
|
| 241 | + /** |
|
| 242 | + * @return boolean |
|
| 243 | + */ |
|
| 244 | + public function displayable() |
|
| 245 | + { |
|
| 246 | + return $this->displayable; |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + |
|
| 250 | + /** |
|
| 251 | + * @param boolean $displayable |
|
| 252 | + */ |
|
| 253 | + public function setDisplayable($displayable = false) |
|
| 254 | + { |
|
| 255 | + $this->displayable = filter_var($displayable, FILTER_VALIDATE_BOOLEAN); |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + |
|
| 259 | + /** |
|
| 260 | + * a public name for the form that can be displayed on the frontend of a site |
|
| 261 | + * |
|
| 262 | + * @return string |
|
| 263 | + */ |
|
| 264 | + public function formName() |
|
| 265 | + { |
|
| 266 | + return $this->form_name; |
|
| 267 | + } |
|
| 268 | + |
|
| 269 | + |
|
| 270 | + /** |
|
| 271 | + * @param string $form_name |
|
| 272 | + * @throws InvalidDataTypeException |
|
| 273 | + */ |
|
| 274 | + public function setFormName($form_name) |
|
| 275 | + { |
|
| 276 | + if (! is_string($form_name)) { |
|
| 277 | + throw new InvalidDataTypeException('$form_name', $form_name, 'string'); |
|
| 278 | + } |
|
| 279 | + $this->form_name = $form_name; |
|
| 280 | + } |
|
| 281 | + |
|
| 282 | + |
|
| 283 | + /** |
|
| 284 | + * a public name for the form that can be displayed, but only in the admin |
|
| 285 | + * |
|
| 286 | + * @return string |
|
| 287 | + */ |
|
| 288 | + public function adminName() |
|
| 289 | + { |
|
| 290 | + return $this->admin_name; |
|
| 291 | + } |
|
| 292 | + |
|
| 293 | + |
|
| 294 | + /** |
|
| 295 | + * @param string $admin_name |
|
| 296 | + * @throws InvalidDataTypeException |
|
| 297 | + */ |
|
| 298 | + public function setAdminName($admin_name) |
|
| 299 | + { |
|
| 300 | + if (! is_string($admin_name)) { |
|
| 301 | + throw new InvalidDataTypeException('$admin_name', $admin_name, 'string'); |
|
| 302 | + } |
|
| 303 | + $this->admin_name = $admin_name; |
|
| 304 | + } |
|
| 305 | + |
|
| 306 | + |
|
| 307 | + /** |
|
| 308 | + * a URL friendly string that can be used for identifying the form |
|
| 309 | + * |
|
| 310 | + * @return string |
|
| 311 | + */ |
|
| 312 | + public function slug() |
|
| 313 | + { |
|
| 314 | + return $this->slug; |
|
| 315 | + } |
|
| 316 | + |
|
| 317 | + |
|
| 318 | + /** |
|
| 319 | + * @param string $slug |
|
| 320 | + * @throws InvalidDataTypeException |
|
| 321 | + */ |
|
| 322 | + public function setSlug($slug) |
|
| 323 | + { |
|
| 324 | + if (! is_string($slug)) { |
|
| 325 | + throw new InvalidDataTypeException('$slug', $slug, 'string'); |
|
| 326 | + } |
|
| 327 | + $this->slug = $slug; |
|
| 328 | + } |
|
| 329 | + |
|
| 330 | + |
|
| 331 | + /** |
|
| 332 | + * @return string |
|
| 333 | + */ |
|
| 334 | + public function submitBtnText() |
|
| 335 | + { |
|
| 336 | + return $this->submit_btn_text; |
|
| 337 | + } |
|
| 338 | + |
|
| 339 | + |
|
| 340 | + /** |
|
| 341 | + * @param string $submit_btn_text |
|
| 342 | + * @throws InvalidDataTypeException |
|
| 343 | + * @throws InvalidArgumentException |
|
| 344 | + */ |
|
| 345 | + public function setSubmitBtnText($submit_btn_text) |
|
| 346 | + { |
|
| 347 | + if (! is_string($submit_btn_text)) { |
|
| 348 | + throw new InvalidDataTypeException('$submit_btn_text', $submit_btn_text, 'string'); |
|
| 349 | + } |
|
| 350 | + if (empty($submit_btn_text)) { |
|
| 351 | + throw new InvalidArgumentException( |
|
| 352 | + esc_html__('Can not set Submit button text because an empty string was provided.', 'event_espresso') |
|
| 353 | + ); |
|
| 354 | + } |
|
| 355 | + $this->submit_btn_text = $submit_btn_text; |
|
| 356 | + } |
|
| 357 | + |
|
| 358 | + |
|
| 359 | + /** |
|
| 360 | + * @return string |
|
| 361 | + */ |
|
| 362 | + public function formAction() |
|
| 363 | + { |
|
| 364 | + return ! empty($this->form_args) |
|
| 365 | + ? add_query_arg($this->form_args, $this->form_action) |
|
| 366 | + : $this->form_action; |
|
| 367 | + } |
|
| 368 | + |
|
| 369 | + |
|
| 370 | + /** |
|
| 371 | + * @param string $form_action |
|
| 372 | + * @throws InvalidDataTypeException |
|
| 373 | + */ |
|
| 374 | + public function setFormAction($form_action) |
|
| 375 | + { |
|
| 376 | + if (! is_string($form_action)) { |
|
| 377 | + throw new InvalidDataTypeException('$form_action', $form_action, 'string'); |
|
| 378 | + } |
|
| 379 | + $this->form_action = $form_action; |
|
| 380 | + } |
|
| 381 | + |
|
| 382 | + |
|
| 383 | + /** |
|
| 384 | + * @param array $form_args |
|
| 385 | + * @throws InvalidDataTypeException |
|
| 386 | + * @throws InvalidArgumentException |
|
| 387 | + */ |
|
| 388 | + public function addFormActionArgs($form_args = array()) |
|
| 389 | + { |
|
| 390 | + if (is_object($form_args)) { |
|
| 391 | + throw new InvalidDataTypeException( |
|
| 392 | + '$form_args', |
|
| 393 | + $form_args, |
|
| 394 | + 'anything other than an object was expected.' |
|
| 395 | + ); |
|
| 396 | + } |
|
| 397 | + if (empty($form_args)) { |
|
| 398 | + throw new InvalidArgumentException( |
|
| 399 | + esc_html__('The redirect arguments can not be an empty array.', 'event_espresso') |
|
| 400 | + ); |
|
| 401 | + } |
|
| 402 | + $this->form_args = array_merge($this->form_args, $form_args); |
|
| 403 | + } |
|
| 404 | + |
|
| 405 | + |
|
| 406 | + /** |
|
| 407 | + * @return string |
|
| 408 | + */ |
|
| 409 | + public function formConfig() |
|
| 410 | + { |
|
| 411 | + return $this->form_config; |
|
| 412 | + } |
|
| 413 | + |
|
| 414 | + |
|
| 415 | + /** |
|
| 416 | + * @param string $form_config |
|
| 417 | + * @throws DomainException |
|
| 418 | + */ |
|
| 419 | + public function setFormConfig($form_config) |
|
| 420 | + { |
|
| 421 | + if (! in_array( |
|
| 422 | + $form_config, |
|
| 423 | + array( |
|
| 424 | + FormHandler::ADD_FORM_TAGS_AND_SUBMIT, |
|
| 425 | + FormHandler::ADD_FORM_TAGS_ONLY, |
|
| 426 | + FormHandler::ADD_FORM_SUBMIT_ONLY, |
|
| 427 | + FormHandler::DO_NOT_SETUP_FORM, |
|
| 428 | + ), |
|
| 429 | + true |
|
| 430 | + ) |
|
| 431 | + ) { |
|
| 432 | + throw new DomainException( |
|
| 433 | + sprintf( |
|
| 434 | + esc_html__( |
|
| 435 | + '"%1$s" is not a valid value for the form config. Please use one of the class constants on \EventEspresso\core\libraries\form_sections\form_handlers\Form', |
|
| 436 | + 'event_espresso' |
|
| 437 | + ), |
|
| 438 | + $form_config |
|
| 439 | + ) |
|
| 440 | + ); |
|
| 441 | + } |
|
| 442 | + $this->form_config = $form_config; |
|
| 443 | + } |
|
| 444 | + |
|
| 445 | + |
|
| 446 | + /** |
|
| 447 | + * called after the form is instantiated |
|
| 448 | + * and used for performing any logic that needs to occur early |
|
| 449 | + * before any of the other methods are called. |
|
| 450 | + * returns true if everything is ok to proceed, |
|
| 451 | + * and false if no further form logic should be implemented |
|
| 452 | + * |
|
| 453 | + * @return boolean |
|
| 454 | + */ |
|
| 455 | + public function initialize() |
|
| 456 | + { |
|
| 457 | + $this->form_has_errors = EE_Error::has_error(true); |
|
| 458 | + return true; |
|
| 459 | + } |
|
| 460 | + |
|
| 461 | + |
|
| 462 | + /** |
|
| 463 | + * used for setting up css and js |
|
| 464 | + * |
|
| 465 | + * @return void |
|
| 466 | + * @throws LogicException |
|
| 467 | + * @throws EE_Error |
|
| 468 | + */ |
|
| 469 | + public function enqueueStylesAndScripts() |
|
| 470 | + { |
|
| 471 | + $this->form()->enqueue_js(); |
|
| 472 | + } |
|
| 473 | + |
|
| 474 | + |
|
| 475 | + /** |
|
| 476 | + * creates and returns the actual form |
|
| 477 | + * |
|
| 478 | + * @return EE_Form_Section_Proper |
|
| 479 | + */ |
|
| 480 | + abstract public function generate(); |
|
| 481 | + |
|
| 482 | + |
|
| 483 | + /** |
|
| 484 | + * creates and returns an EE_Submit_Input labeled "Submit" |
|
| 485 | + * |
|
| 486 | + * @param string $text |
|
| 487 | + * @return EE_Submit_Input |
|
| 488 | + */ |
|
| 489 | + public function generateSubmitButton($text = '') |
|
| 490 | + { |
|
| 491 | + $text = ! empty($text) ? $text : $this->submitBtnText(); |
|
| 492 | + return new EE_Submit_Input( |
|
| 493 | + array( |
|
| 494 | + 'html_name' => 'ee-form-submit-' . $this->slug(), |
|
| 495 | + 'html_id' => 'ee-form-submit-' . $this->slug(), |
|
| 496 | + 'html_class' => 'ee-form-submit', |
|
| 497 | + 'html_label' => ' ', |
|
| 498 | + 'other_html_attributes' => ' rel="' . $this->slug() . '"', |
|
| 499 | + 'default' => $text, |
|
| 500 | + ) |
|
| 501 | + ); |
|
| 502 | + } |
|
| 503 | + |
|
| 504 | + |
|
| 505 | + /** |
|
| 506 | + * calls generateSubmitButton() and appends it onto the form along with a float clearing div |
|
| 507 | + * |
|
| 508 | + * @param string $text |
|
| 509 | + * @return void |
|
| 510 | + * @throws EE_Error |
|
| 511 | + */ |
|
| 512 | + public function appendSubmitButton($text = '') |
|
| 513 | + { |
|
| 514 | + if ($this->form->subsection_exists($this->slug() . '-submit-btn')) { |
|
| 515 | + return; |
|
| 516 | + } |
|
| 517 | + $this->form->add_subsections( |
|
| 518 | + array($this->slug() . '-submit-btn' => $this->generateSubmitButton($text)), |
|
| 519 | + null, |
|
| 520 | + false |
|
| 521 | + ); |
|
| 522 | + } |
|
| 523 | + |
|
| 524 | + |
|
| 525 | + /** |
|
| 526 | + * creates and returns an EE_Submit_Input labeled "Cancel" |
|
| 527 | + * |
|
| 528 | + * @param string $text |
|
| 529 | + * @return EE_Submit_Input |
|
| 530 | + */ |
|
| 531 | + public function generateCancelButton($text = '') |
|
| 532 | + { |
|
| 533 | + $cancel_button = new EE_Submit_Input( |
|
| 534 | + array( |
|
| 535 | + 'html_name' => 'ee-form-submit-' . $this->slug(), // YES! Same name as submit !!! |
|
| 536 | + 'html_id' => 'ee-cancel-form-' . $this->slug(), |
|
| 537 | + 'html_class' => 'ee-cancel-form', |
|
| 538 | + 'html_label' => ' ', |
|
| 539 | + 'other_html_attributes' => ' rel="' . $this->slug() . '"', |
|
| 540 | + 'default' => ! empty($text) ? $text : esc_html__('Cancel', 'event_espresso'), |
|
| 541 | + ) |
|
| 542 | + ); |
|
| 543 | + $cancel_button->set_button_css_attributes(false); |
|
| 544 | + return $cancel_button; |
|
| 545 | + } |
|
| 546 | + |
|
| 547 | + |
|
| 548 | + /** |
|
| 549 | + * appends a float clearing div onto end of form |
|
| 550 | + * |
|
| 551 | + * @return void |
|
| 552 | + * @throws EE_Error |
|
| 553 | + */ |
|
| 554 | + public function clearFormButtonFloats() |
|
| 555 | + { |
|
| 556 | + $this->form->add_subsections( |
|
| 557 | + array( |
|
| 558 | + 'clear-submit-btn-float' => new EE_Form_Section_HTML( |
|
| 559 | + EEH_HTML::div('', '', 'clear-float') . EEH_HTML::divx() |
|
| 560 | + ), |
|
| 561 | + ), |
|
| 562 | + null, |
|
| 563 | + false |
|
| 564 | + ); |
|
| 565 | + } |
|
| 566 | + |
|
| 567 | + |
|
| 568 | + /** |
|
| 569 | + * takes the generated form and displays it along with ony other non-form HTML that may be required |
|
| 570 | + * returns a string of HTML that can be directly echoed in a template |
|
| 571 | + * |
|
| 572 | + * @return string |
|
| 573 | + * @throws \InvalidArgumentException |
|
| 574 | + * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 575 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 576 | + * @throws LogicException |
|
| 577 | + * @throws EE_Error |
|
| 578 | + */ |
|
| 579 | + public function display() |
|
| 580 | + { |
|
| 581 | + $form_html = apply_filters( |
|
| 582 | + 'FHEE__EventEspresso_core_libraries_form_sections_form_handlers_FormHandler__display__before_form', |
|
| 583 | + '' |
|
| 584 | + ); |
|
| 585 | + $form_config = $this->formConfig(); |
|
| 586 | + if ($form_config === FormHandler::ADD_FORM_TAGS_AND_SUBMIT |
|
| 587 | + || $form_config === FormHandler::ADD_FORM_TAGS_ONLY |
|
| 588 | + ) { |
|
| 589 | + $additional_props = $this->requiresMultipartEnctype() |
|
| 590 | + ? 'enctype="multipart/form-data"' |
|
| 591 | + : ''; |
|
| 592 | + $form_html .= $this->form()->form_open( |
|
| 593 | + $this->formAction(), |
|
| 594 | + 'POST', |
|
| 595 | + $additional_props |
|
| 596 | + ); |
|
| 597 | + } |
|
| 598 | + $form_html .= $this->form(true)->get_html(); |
|
| 599 | + if ($form_config === FormHandler::ADD_FORM_TAGS_AND_SUBMIT |
|
| 600 | + || $form_config === FormHandler::ADD_FORM_TAGS_ONLY |
|
| 601 | + ) { |
|
| 602 | + $form_html .= $this->form()->form_close(); |
|
| 603 | + } |
|
| 604 | + $form_html .= apply_filters( |
|
| 605 | + 'FHEE__EventEspresso_core_libraries_form_sections_form_handlers_FormHandler__display__after_form', |
|
| 606 | + '' |
|
| 607 | + ); |
|
| 608 | + return $form_html; |
|
| 609 | + } |
|
| 610 | + |
|
| 611 | + /** |
|
| 612 | + * Determines if this form needs "enctype='multipart/form-data'" or not. |
|
| 613 | + * @since $VID:$ |
|
| 614 | + * @return bool |
|
| 615 | + * @throws EE_Error |
|
| 616 | + */ |
|
| 617 | + public function requiresMultipartEnctype() |
|
| 618 | + { |
|
| 619 | + foreach ($this->form()->inputs_in_subsections() as $input) { |
|
| 620 | + if ($input instanceof EE_File_Input) { |
|
| 621 | + return true; |
|
| 622 | + } |
|
| 623 | + } |
|
| 624 | + return false; |
|
| 625 | + } |
|
| 626 | + |
|
| 627 | + |
|
| 628 | + /** |
|
| 629 | + * handles processing the form submission |
|
| 630 | + * returns true or false depending on whether the form was processed successfully or not |
|
| 631 | + * |
|
| 632 | + * @param array $submitted_form_data |
|
| 633 | + * @return array |
|
| 634 | + * @throws \InvalidArgumentException |
|
| 635 | + * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 636 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 637 | + * @throws EE_Error |
|
| 638 | + * @throws LogicException |
|
| 639 | + * @throws InvalidFormSubmissionException |
|
| 640 | + */ |
|
| 641 | + public function process($submitted_form_data = array()) |
|
| 642 | + { |
|
| 643 | + if (! $this->form()->was_submitted($submitted_form_data)) { |
|
| 644 | + throw new InvalidFormSubmissionException($this->form_name); |
|
| 645 | + } |
|
| 646 | + $this->form(true)->receive_form_submission($submitted_form_data); |
|
| 647 | + if (! $this->form()->is_valid()) { |
|
| 648 | + throw new InvalidFormSubmissionException( |
|
| 649 | + $this->form_name, |
|
| 650 | + sprintf( |
|
| 651 | + esc_html__( |
|
| 652 | + 'The "%1$s" form is invalid. Please correct the following errors and resubmit: %2$s %3$s', |
|
| 653 | + 'event_espresso' |
|
| 654 | + ), |
|
| 655 | + $this->form_name, |
|
| 656 | + '<br />', |
|
| 657 | + implode('<br />', $this->form()->get_validation_errors_accumulated()) |
|
| 658 | + ) |
|
| 659 | + ); |
|
| 660 | + } |
|
| 661 | + return apply_filters( |
|
| 662 | + 'FHEE__EventEspresso_core_libraries_form_sections_form_handlers_FormHandler__process__valid_data', |
|
| 663 | + $this->form()->valid_data(), |
|
| 664 | + $this |
|
| 665 | + ); |
|
| 666 | + } |
|
| 667 | 667 | } |
@@ -13,40 +13,40 @@ |
||
| 13 | 13 | class EE_File_Normalization extends EE_Normalization_Strategy_Base |
| 14 | 14 | { |
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * Keep in mind $value_to_normalize should be a FileSubmissionInterface or null, so this shouldn't really do |
|
| 18 | - * much (other than NOT convert it to a string or something). |
|
| 19 | - * @param string $value_to_normalize |
|
| 20 | - * @return FileSubmissionInterface |
|
| 21 | - */ |
|
| 22 | - public function normalize($value_to_normalize) |
|
| 23 | - { |
|
| 24 | - if ($value_to_normalize instanceof FileSubmissionInterface || is_null($value_to_normalize)) { |
|
| 25 | - return $value_to_normalize; |
|
| 26 | - } else { |
|
| 27 | - throw new EE_Validation_Error( |
|
| 28 | - esc_html__('The file input has an invalid format.', 'event_espresso') |
|
| 29 | - ); |
|
| 30 | - } |
|
| 31 | - } |
|
| 16 | + /** |
|
| 17 | + * Keep in mind $value_to_normalize should be a FileSubmissionInterface or null, so this shouldn't really do |
|
| 18 | + * much (other than NOT convert it to a string or something). |
|
| 19 | + * @param string $value_to_normalize |
|
| 20 | + * @return FileSubmissionInterface |
|
| 21 | + */ |
|
| 22 | + public function normalize($value_to_normalize) |
|
| 23 | + { |
|
| 24 | + if ($value_to_normalize instanceof FileSubmissionInterface || is_null($value_to_normalize)) { |
|
| 25 | + return $value_to_normalize; |
|
| 26 | + } else { |
|
| 27 | + throw new EE_Validation_Error( |
|
| 28 | + esc_html__('The file input has an invalid format.', 'event_espresso') |
|
| 29 | + ); |
|
| 30 | + } |
|
| 31 | + } |
|
| 32 | 32 | |
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * This may be called prematurely on submitted data, so we actually don't want to convert it into a string because |
|
| 36 | - * we'll lose all the FileSubmissionInterface data. So prefer to leave it alone. FileSubmissionInterface |
|
| 37 | - * can be cast to a string just fine so it's good as-is. |
|
| 38 | - * |
|
| 39 | - * @param string $normalized_value |
|
| 40 | - * @return string |
|
| 41 | - */ |
|
| 42 | - public function unnormalize($normalized_value) |
|
| 43 | - { |
|
| 44 | - if ($normalized_value instanceof FileSubmissionInterface || is_null($normalized_value)) { |
|
| 45 | - // Leave it as the object, it can be treated like a string because it |
|
| 46 | - // overrides __toString() |
|
| 47 | - return $normalized_value; |
|
| 48 | - } else { |
|
| 49 | - return (string) $normalized_value; |
|
| 50 | - } |
|
| 51 | - } |
|
| 34 | + /** |
|
| 35 | + * This may be called prematurely on submitted data, so we actually don't want to convert it into a string because |
|
| 36 | + * we'll lose all the FileSubmissionInterface data. So prefer to leave it alone. FileSubmissionInterface |
|
| 37 | + * can be cast to a string just fine so it's good as-is. |
|
| 38 | + * |
|
| 39 | + * @param string $normalized_value |
|
| 40 | + * @return string |
|
| 41 | + */ |
|
| 42 | + public function unnormalize($normalized_value) |
|
| 43 | + { |
|
| 44 | + if ($normalized_value instanceof FileSubmissionInterface || is_null($normalized_value)) { |
|
| 45 | + // Leave it as the object, it can be treated like a string because it |
|
| 46 | + // overrides __toString() |
|
| 47 | + return $normalized_value; |
|
| 48 | + } else { |
|
| 49 | + return (string) $normalized_value; |
|
| 50 | + } |
|
| 51 | + } |
|
| 52 | 52 | } |
@@ -18,156 +18,156 @@ |
||
| 18 | 18 | class FqcnLocator extends Locator |
| 19 | 19 | { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * @var array $FQCNs |
|
| 23 | - */ |
|
| 24 | - protected $FQCNs = array(); |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * @var array $namespaces |
|
| 28 | - */ |
|
| 29 | - protected $namespaces = array(); |
|
| 30 | - |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * @access protected |
|
| 34 | - * @param string $namespace |
|
| 35 | - * @param string $namespace_base_dir |
|
| 36 | - * @throws InvalidDataTypeException |
|
| 37 | - */ |
|
| 38 | - protected function setNamespace($namespace, $namespace_base_dir) |
|
| 39 | - { |
|
| 40 | - if (! is_string($namespace)) { |
|
| 41 | - throw new InvalidDataTypeException('$namespace', $namespace, 'string'); |
|
| 42 | - } |
|
| 43 | - if (! is_string($namespace_base_dir)) { |
|
| 44 | - throw new InvalidDataTypeException('$namespace_base_dir', $namespace_base_dir, 'string'); |
|
| 45 | - } |
|
| 46 | - $this->namespaces[ $namespace ] = $namespace_base_dir; |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * @access public |
|
| 52 | - * @return array |
|
| 53 | - */ |
|
| 54 | - public function getFQCNs() |
|
| 55 | - { |
|
| 56 | - return $this->FQCNs; |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * @access public |
|
| 62 | - * @return int |
|
| 63 | - */ |
|
| 64 | - public function count() |
|
| 65 | - { |
|
| 66 | - return count($this->FQCNs); |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * given a valid namespace, will find all files that match the provided mask |
|
| 72 | - * |
|
| 73 | - * @access public |
|
| 74 | - * @param string|array $namespaces |
|
| 75 | - * @return array |
|
| 76 | - * @throws InvalidClassException |
|
| 77 | - * @throws InvalidDataTypeException |
|
| 78 | - */ |
|
| 79 | - public function locate($namespaces) |
|
| 80 | - { |
|
| 81 | - if (! (is_string($namespaces) || is_array($namespaces))) { |
|
| 82 | - throw new InvalidDataTypeException('$namespaces', $namespaces, 'string or array'); |
|
| 83 | - } |
|
| 84 | - foreach ((array) $namespaces as $namespace) { |
|
| 85 | - foreach ($this->findFQCNsByNamespace($namespace) as $key => $file) { |
|
| 86 | - $this->FQCNs[ $key ] = $file; |
|
| 87 | - } |
|
| 88 | - } |
|
| 89 | - return $this->FQCNs; |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * given a partial namespace, will find all files in that folder |
|
| 95 | - * ** PLZ NOTE ** |
|
| 96 | - * This assumes that all files within the specified folder should be loaded |
|
| 97 | - * |
|
| 98 | - * @access protected |
|
| 99 | - * @param string $partial_namespace |
|
| 100 | - * @return array |
|
| 101 | - * @throws InvalidClassException |
|
| 102 | - * @throws InvalidDataTypeException |
|
| 103 | - */ |
|
| 104 | - protected function findFQCNsByNamespace($partial_namespace) |
|
| 105 | - { |
|
| 106 | - $iterator = new FilesystemIterator( |
|
| 107 | - $this->getDirectoryFromPartialNamespace($partial_namespace) |
|
| 108 | - ); |
|
| 109 | - $iterator->setFlags(FilesystemIterator::CURRENT_AS_FILEINFO); |
|
| 110 | - $iterator->setFlags(FilesystemIterator::UNIX_PATHS); |
|
| 111 | - if (iterator_count($iterator) === 0) { |
|
| 112 | - return array(); |
|
| 113 | - } |
|
| 114 | - foreach ($iterator as $file) { |
|
| 115 | - if ($file->isFile() && $file->getExtension() === 'php') { |
|
| 116 | - $file = $file->getPath() . DS . $file->getBasename('.php'); |
|
| 117 | - foreach ($this->namespaces as $namespace => $base_dir) { |
|
| 118 | - $namespace .= Psr4Autoloader::NS; |
|
| 119 | - if (strpos($file, $base_dir) === 0) { |
|
| 120 | - $this->FQCNs[] = Psr4Autoloader::NS . str_replace( |
|
| 121 | - array($base_dir, DS), |
|
| 122 | - array($namespace, Psr4Autoloader::NS), |
|
| 123 | - $file |
|
| 124 | - ); |
|
| 125 | - } |
|
| 126 | - } |
|
| 127 | - } |
|
| 128 | - } |
|
| 129 | - return $this->FQCNs; |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - |
|
| 133 | - /** |
|
| 134 | - * getDirectoryFromPartialNamespace |
|
| 135 | - * |
|
| 136 | - * @access protected |
|
| 137 | - * @param string $partial_namespace almost fully qualified class name ? |
|
| 138 | - * @return string |
|
| 139 | - * @throws InvalidDataTypeException |
|
| 140 | - * @throws InvalidClassException |
|
| 141 | - */ |
|
| 142 | - protected function getDirectoryFromPartialNamespace($partial_namespace) |
|
| 143 | - { |
|
| 144 | - if (empty($partial_namespace)) { |
|
| 145 | - throw new InvalidClassException($partial_namespace); |
|
| 146 | - } |
|
| 147 | - // load our PSR-4 Autoloader so we can get the list of registered namespaces from it |
|
| 148 | - $psr4_loader = \EE_Psr4AutoloaderInit::psr4_loader(); |
|
| 149 | - // breakup the incoming namespace into segments so we can loop thru them |
|
| 150 | - $namespace_segments = explode(Psr4Autoloader::NS, trim($partial_namespace, Psr4Autoloader::NS)); |
|
| 151 | - // we're only interested in the Vendor and secondary base, so pull those from the array |
|
| 152 | - $vendor_base = array_slice($namespace_segments, 0, 2); |
|
| 153 | - $namespace = $prefix = null; |
|
| 154 | - while (! empty($vendor_base)) { |
|
| 155 | - $namespace = implode(Psr4Autoloader::NS, $vendor_base); |
|
| 156 | - // check if there's a base directory registered for that namespace |
|
| 157 | - $prefix = $psr4_loader->prefixes($namespace . Psr4Autoloader::NS); |
|
| 158 | - if (! empty($prefix) && ! empty($prefix[0])) { |
|
| 159 | - // found one! |
|
| 160 | - break; |
|
| 161 | - } |
|
| 162 | - // remove base and try vendor only portion of namespace |
|
| 163 | - array_pop($vendor_base); |
|
| 164 | - } |
|
| 165 | - // nope? then the incoming namespace is invalid |
|
| 166 | - if (empty($prefix) || empty($prefix[0])) { |
|
| 167 | - throw new InvalidClassException($partial_namespace); |
|
| 168 | - } |
|
| 169 | - $this->setNamespace($namespace, $prefix[0]); |
|
| 170 | - // but if it's good, add that base directory to the rest of the path, and return it |
|
| 171 | - return $prefix[0] . implode(DS, array_diff($namespace_segments, $vendor_base)) . DS; |
|
| 172 | - } |
|
| 21 | + /** |
|
| 22 | + * @var array $FQCNs |
|
| 23 | + */ |
|
| 24 | + protected $FQCNs = array(); |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * @var array $namespaces |
|
| 28 | + */ |
|
| 29 | + protected $namespaces = array(); |
|
| 30 | + |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * @access protected |
|
| 34 | + * @param string $namespace |
|
| 35 | + * @param string $namespace_base_dir |
|
| 36 | + * @throws InvalidDataTypeException |
|
| 37 | + */ |
|
| 38 | + protected function setNamespace($namespace, $namespace_base_dir) |
|
| 39 | + { |
|
| 40 | + if (! is_string($namespace)) { |
|
| 41 | + throw new InvalidDataTypeException('$namespace', $namespace, 'string'); |
|
| 42 | + } |
|
| 43 | + if (! is_string($namespace_base_dir)) { |
|
| 44 | + throw new InvalidDataTypeException('$namespace_base_dir', $namespace_base_dir, 'string'); |
|
| 45 | + } |
|
| 46 | + $this->namespaces[ $namespace ] = $namespace_base_dir; |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * @access public |
|
| 52 | + * @return array |
|
| 53 | + */ |
|
| 54 | + public function getFQCNs() |
|
| 55 | + { |
|
| 56 | + return $this->FQCNs; |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * @access public |
|
| 62 | + * @return int |
|
| 63 | + */ |
|
| 64 | + public function count() |
|
| 65 | + { |
|
| 66 | + return count($this->FQCNs); |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * given a valid namespace, will find all files that match the provided mask |
|
| 72 | + * |
|
| 73 | + * @access public |
|
| 74 | + * @param string|array $namespaces |
|
| 75 | + * @return array |
|
| 76 | + * @throws InvalidClassException |
|
| 77 | + * @throws InvalidDataTypeException |
|
| 78 | + */ |
|
| 79 | + public function locate($namespaces) |
|
| 80 | + { |
|
| 81 | + if (! (is_string($namespaces) || is_array($namespaces))) { |
|
| 82 | + throw new InvalidDataTypeException('$namespaces', $namespaces, 'string or array'); |
|
| 83 | + } |
|
| 84 | + foreach ((array) $namespaces as $namespace) { |
|
| 85 | + foreach ($this->findFQCNsByNamespace($namespace) as $key => $file) { |
|
| 86 | + $this->FQCNs[ $key ] = $file; |
|
| 87 | + } |
|
| 88 | + } |
|
| 89 | + return $this->FQCNs; |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * given a partial namespace, will find all files in that folder |
|
| 95 | + * ** PLZ NOTE ** |
|
| 96 | + * This assumes that all files within the specified folder should be loaded |
|
| 97 | + * |
|
| 98 | + * @access protected |
|
| 99 | + * @param string $partial_namespace |
|
| 100 | + * @return array |
|
| 101 | + * @throws InvalidClassException |
|
| 102 | + * @throws InvalidDataTypeException |
|
| 103 | + */ |
|
| 104 | + protected function findFQCNsByNamespace($partial_namespace) |
|
| 105 | + { |
|
| 106 | + $iterator = new FilesystemIterator( |
|
| 107 | + $this->getDirectoryFromPartialNamespace($partial_namespace) |
|
| 108 | + ); |
|
| 109 | + $iterator->setFlags(FilesystemIterator::CURRENT_AS_FILEINFO); |
|
| 110 | + $iterator->setFlags(FilesystemIterator::UNIX_PATHS); |
|
| 111 | + if (iterator_count($iterator) === 0) { |
|
| 112 | + return array(); |
|
| 113 | + } |
|
| 114 | + foreach ($iterator as $file) { |
|
| 115 | + if ($file->isFile() && $file->getExtension() === 'php') { |
|
| 116 | + $file = $file->getPath() . DS . $file->getBasename('.php'); |
|
| 117 | + foreach ($this->namespaces as $namespace => $base_dir) { |
|
| 118 | + $namespace .= Psr4Autoloader::NS; |
|
| 119 | + if (strpos($file, $base_dir) === 0) { |
|
| 120 | + $this->FQCNs[] = Psr4Autoloader::NS . str_replace( |
|
| 121 | + array($base_dir, DS), |
|
| 122 | + array($namespace, Psr4Autoloader::NS), |
|
| 123 | + $file |
|
| 124 | + ); |
|
| 125 | + } |
|
| 126 | + } |
|
| 127 | + } |
|
| 128 | + } |
|
| 129 | + return $this->FQCNs; |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + |
|
| 133 | + /** |
|
| 134 | + * getDirectoryFromPartialNamespace |
|
| 135 | + * |
|
| 136 | + * @access protected |
|
| 137 | + * @param string $partial_namespace almost fully qualified class name ? |
|
| 138 | + * @return string |
|
| 139 | + * @throws InvalidDataTypeException |
|
| 140 | + * @throws InvalidClassException |
|
| 141 | + */ |
|
| 142 | + protected function getDirectoryFromPartialNamespace($partial_namespace) |
|
| 143 | + { |
|
| 144 | + if (empty($partial_namespace)) { |
|
| 145 | + throw new InvalidClassException($partial_namespace); |
|
| 146 | + } |
|
| 147 | + // load our PSR-4 Autoloader so we can get the list of registered namespaces from it |
|
| 148 | + $psr4_loader = \EE_Psr4AutoloaderInit::psr4_loader(); |
|
| 149 | + // breakup the incoming namespace into segments so we can loop thru them |
|
| 150 | + $namespace_segments = explode(Psr4Autoloader::NS, trim($partial_namespace, Psr4Autoloader::NS)); |
|
| 151 | + // we're only interested in the Vendor and secondary base, so pull those from the array |
|
| 152 | + $vendor_base = array_slice($namespace_segments, 0, 2); |
|
| 153 | + $namespace = $prefix = null; |
|
| 154 | + while (! empty($vendor_base)) { |
|
| 155 | + $namespace = implode(Psr4Autoloader::NS, $vendor_base); |
|
| 156 | + // check if there's a base directory registered for that namespace |
|
| 157 | + $prefix = $psr4_loader->prefixes($namespace . Psr4Autoloader::NS); |
|
| 158 | + if (! empty($prefix) && ! empty($prefix[0])) { |
|
| 159 | + // found one! |
|
| 160 | + break; |
|
| 161 | + } |
|
| 162 | + // remove base and try vendor only portion of namespace |
|
| 163 | + array_pop($vendor_base); |
|
| 164 | + } |
|
| 165 | + // nope? then the incoming namespace is invalid |
|
| 166 | + if (empty($prefix) || empty($prefix[0])) { |
|
| 167 | + throw new InvalidClassException($partial_namespace); |
|
| 168 | + } |
|
| 169 | + $this->setNamespace($namespace, $prefix[0]); |
|
| 170 | + // but if it's good, add that base directory to the rest of the path, and return it |
|
| 171 | + return $prefix[0] . implode(DS, array_diff($namespace_segments, $vendor_base)) . DS; |
|
| 172 | + } |
|
| 173 | 173 | } |
@@ -37,13 +37,13 @@ discard block |
||
| 37 | 37 | */ |
| 38 | 38 | protected function setNamespace($namespace, $namespace_base_dir) |
| 39 | 39 | { |
| 40 | - if (! is_string($namespace)) { |
|
| 40 | + if ( ! is_string($namespace)) { |
|
| 41 | 41 | throw new InvalidDataTypeException('$namespace', $namespace, 'string'); |
| 42 | 42 | } |
| 43 | - if (! is_string($namespace_base_dir)) { |
|
| 43 | + if ( ! is_string($namespace_base_dir)) { |
|
| 44 | 44 | throw new InvalidDataTypeException('$namespace_base_dir', $namespace_base_dir, 'string'); |
| 45 | 45 | } |
| 46 | - $this->namespaces[ $namespace ] = $namespace_base_dir; |
|
| 46 | + $this->namespaces[$namespace] = $namespace_base_dir; |
|
| 47 | 47 | } |
| 48 | 48 | |
| 49 | 49 | |
@@ -78,12 +78,12 @@ discard block |
||
| 78 | 78 | */ |
| 79 | 79 | public function locate($namespaces) |
| 80 | 80 | { |
| 81 | - if (! (is_string($namespaces) || is_array($namespaces))) { |
|
| 81 | + if ( ! (is_string($namespaces) || is_array($namespaces))) { |
|
| 82 | 82 | throw new InvalidDataTypeException('$namespaces', $namespaces, 'string or array'); |
| 83 | 83 | } |
| 84 | 84 | foreach ((array) $namespaces as $namespace) { |
| 85 | 85 | foreach ($this->findFQCNsByNamespace($namespace) as $key => $file) { |
| 86 | - $this->FQCNs[ $key ] = $file; |
|
| 86 | + $this->FQCNs[$key] = $file; |
|
| 87 | 87 | } |
| 88 | 88 | } |
| 89 | 89 | return $this->FQCNs; |
@@ -113,11 +113,11 @@ discard block |
||
| 113 | 113 | } |
| 114 | 114 | foreach ($iterator as $file) { |
| 115 | 115 | if ($file->isFile() && $file->getExtension() === 'php') { |
| 116 | - $file = $file->getPath() . DS . $file->getBasename('.php'); |
|
| 116 | + $file = $file->getPath().DS.$file->getBasename('.php'); |
|
| 117 | 117 | foreach ($this->namespaces as $namespace => $base_dir) { |
| 118 | 118 | $namespace .= Psr4Autoloader::NS; |
| 119 | 119 | if (strpos($file, $base_dir) === 0) { |
| 120 | - $this->FQCNs[] = Psr4Autoloader::NS . str_replace( |
|
| 120 | + $this->FQCNs[] = Psr4Autoloader::NS.str_replace( |
|
| 121 | 121 | array($base_dir, DS), |
| 122 | 122 | array($namespace, Psr4Autoloader::NS), |
| 123 | 123 | $file |
@@ -151,11 +151,11 @@ discard block |
||
| 151 | 151 | // we're only interested in the Vendor and secondary base, so pull those from the array |
| 152 | 152 | $vendor_base = array_slice($namespace_segments, 0, 2); |
| 153 | 153 | $namespace = $prefix = null; |
| 154 | - while (! empty($vendor_base)) { |
|
| 154 | + while ( ! empty($vendor_base)) { |
|
| 155 | 155 | $namespace = implode(Psr4Autoloader::NS, $vendor_base); |
| 156 | 156 | // check if there's a base directory registered for that namespace |
| 157 | - $prefix = $psr4_loader->prefixes($namespace . Psr4Autoloader::NS); |
|
| 158 | - if (! empty($prefix) && ! empty($prefix[0])) { |
|
| 157 | + $prefix = $psr4_loader->prefixes($namespace.Psr4Autoloader::NS); |
|
| 158 | + if ( ! empty($prefix) && ! empty($prefix[0])) { |
|
| 159 | 159 | // found one! |
| 160 | 160 | break; |
| 161 | 161 | } |
@@ -168,6 +168,6 @@ discard block |
||
| 168 | 168 | } |
| 169 | 169 | $this->setNamespace($namespace, $prefix[0]); |
| 170 | 170 | // but if it's good, add that base directory to the rest of the path, and return it |
| 171 | - return $prefix[0] . implode(DS, array_diff($namespace_segments, $vendor_base)) . DS; |
|
| 171 | + return $prefix[0].implode(DS, array_diff($namespace_segments, $vendor_base)).DS; |
|
| 172 | 172 | } |
| 173 | 173 | } |
@@ -13,747 +13,747 @@ |
||
| 13 | 13 | class EEM_Datetime extends EEM_Soft_Delete_Base |
| 14 | 14 | { |
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * @var EEM_Datetime $_instance |
|
| 18 | - */ |
|
| 19 | - protected static $_instance; |
|
| 20 | - |
|
| 21 | - |
|
| 22 | - /** |
|
| 23 | - * private constructor to prevent direct creation |
|
| 24 | - * |
|
| 25 | - * @param string $timezone A string representing the timezone we want to set for returned Date Time Strings |
|
| 26 | - * (and any incoming timezone data that gets saved). |
|
| 27 | - * Note this just sends the timezone info to the date time model field objects. |
|
| 28 | - * Default is NULL |
|
| 29 | - * (and will be assumed using the set timezone in the 'timezone_string' wp option) |
|
| 30 | - * @throws EE_Error |
|
| 31 | - * @throws InvalidArgumentException |
|
| 32 | - * @throws InvalidArgumentException |
|
| 33 | - */ |
|
| 34 | - protected function __construct($timezone) |
|
| 35 | - { |
|
| 36 | - $this->singular_item = esc_html__('Datetime', 'event_espresso'); |
|
| 37 | - $this->plural_item = esc_html__('Datetimes', 'event_espresso'); |
|
| 38 | - $this->_tables = array( |
|
| 39 | - 'Datetime' => new EE_Primary_Table('esp_datetime', 'DTT_ID'), |
|
| 40 | - ); |
|
| 41 | - $this->_fields = array( |
|
| 42 | - 'Datetime' => array( |
|
| 43 | - 'DTT_ID' => new EE_Primary_Key_Int_Field( |
|
| 44 | - 'DTT_ID', |
|
| 45 | - esc_html__('Datetime ID', 'event_espresso') |
|
| 46 | - ), |
|
| 47 | - 'EVT_ID' => new EE_Foreign_Key_Int_Field( |
|
| 48 | - 'EVT_ID', |
|
| 49 | - esc_html__('Event ID', 'event_espresso'), |
|
| 50 | - false, |
|
| 51 | - 0, |
|
| 52 | - 'Event' |
|
| 53 | - ), |
|
| 54 | - 'DTT_name' => new EE_Plain_Text_Field( |
|
| 55 | - 'DTT_name', |
|
| 56 | - esc_html__('Datetime Name', 'event_espresso'), |
|
| 57 | - false, |
|
| 58 | - '' |
|
| 59 | - ), |
|
| 60 | - 'DTT_description' => new EE_Post_Content_Field( |
|
| 61 | - 'DTT_description', |
|
| 62 | - esc_html__('Description for Datetime', 'event_espresso'), |
|
| 63 | - false, |
|
| 64 | - '' |
|
| 65 | - ), |
|
| 66 | - 'DTT_EVT_start' => new EE_Datetime_Field( |
|
| 67 | - 'DTT_EVT_start', |
|
| 68 | - esc_html__('Start time/date of Event', 'event_espresso'), |
|
| 69 | - false, |
|
| 70 | - EE_Datetime_Field::now, |
|
| 71 | - $timezone |
|
| 72 | - ), |
|
| 73 | - 'DTT_EVT_end' => new EE_Datetime_Field( |
|
| 74 | - 'DTT_EVT_end', |
|
| 75 | - esc_html__('End time/date of Event', 'event_espresso'), |
|
| 76 | - false, |
|
| 77 | - EE_Datetime_Field::now, |
|
| 78 | - $timezone |
|
| 79 | - ), |
|
| 80 | - 'DTT_reg_limit' => new EE_Infinite_Integer_Field( |
|
| 81 | - 'DTT_reg_limit', |
|
| 82 | - esc_html__('Registration Limit for this time', 'event_espresso'), |
|
| 83 | - true, |
|
| 84 | - EE_INF |
|
| 85 | - ), |
|
| 86 | - 'DTT_sold' => new EE_Integer_Field( |
|
| 87 | - 'DTT_sold', |
|
| 88 | - esc_html__('How many sales for this Datetime that have occurred', 'event_espresso'), |
|
| 89 | - true, |
|
| 90 | - 0 |
|
| 91 | - ), |
|
| 92 | - 'DTT_reserved' => new EE_Integer_Field( |
|
| 93 | - 'DTT_reserved', |
|
| 94 | - esc_html__('Quantity of tickets reserved, but not yet fully purchased', 'event_espresso'), |
|
| 95 | - false, |
|
| 96 | - 0 |
|
| 97 | - ), |
|
| 98 | - 'DTT_is_primary' => new EE_Boolean_Field( |
|
| 99 | - 'DTT_is_primary', |
|
| 100 | - esc_html__('Flag indicating datetime is primary one for event', 'event_espresso'), |
|
| 101 | - false, |
|
| 102 | - false |
|
| 103 | - ), |
|
| 104 | - 'DTT_order' => new EE_Integer_Field( |
|
| 105 | - 'DTT_order', |
|
| 106 | - esc_html__('The order in which the Datetime is displayed', 'event_espresso'), |
|
| 107 | - false, |
|
| 108 | - 0 |
|
| 109 | - ), |
|
| 110 | - 'DTT_parent' => new EE_Integer_Field( |
|
| 111 | - 'DTT_parent', |
|
| 112 | - esc_html__('Indicates what DTT_ID is the parent of this DTT_ID', 'event_espresso'), |
|
| 113 | - true, |
|
| 114 | - 0 |
|
| 115 | - ), |
|
| 116 | - 'DTT_deleted' => new EE_Trashed_Flag_Field( |
|
| 117 | - 'DTT_deleted', |
|
| 118 | - esc_html__('Flag indicating datetime is archived', 'event_espresso'), |
|
| 119 | - false, |
|
| 120 | - false |
|
| 121 | - ), |
|
| 122 | - ), |
|
| 123 | - ); |
|
| 124 | - $this->_model_relations = array( |
|
| 125 | - 'Ticket' => new EE_HABTM_Relation('Datetime_Ticket'), |
|
| 126 | - 'Event' => new EE_Belongs_To_Relation(), |
|
| 127 | - 'Checkin' => new EE_Has_Many_Relation(), |
|
| 128 | - 'Datetime_Ticket' => new EE_Has_Many_Relation(), |
|
| 129 | - ); |
|
| 130 | - $path_to_event_model = 'Event'; |
|
| 131 | - $this->model_chain_to_password = $path_to_event_model; |
|
| 132 | - $this->_model_chain_to_wp_user = $path_to_event_model; |
|
| 133 | - // this model is generally available for reading |
|
| 134 | - $this->_cap_restriction_generators[ EEM_Base::caps_read ] = new EE_Restriction_Generator_Event_Related_Public( |
|
| 135 | - $path_to_event_model |
|
| 136 | - ); |
|
| 137 | - $this->_cap_restriction_generators[ EEM_Base::caps_read_admin ] = new EE_Restriction_Generator_Event_Related_Protected( |
|
| 138 | - $path_to_event_model |
|
| 139 | - ); |
|
| 140 | - $this->_cap_restriction_generators[ EEM_Base::caps_edit ] = new EE_Restriction_Generator_Event_Related_Protected( |
|
| 141 | - $path_to_event_model |
|
| 142 | - ); |
|
| 143 | - $this->_cap_restriction_generators[ EEM_Base::caps_delete ] = new EE_Restriction_Generator_Event_Related_Protected( |
|
| 144 | - $path_to_event_model, |
|
| 145 | - EEM_Base::caps_edit |
|
| 146 | - ); |
|
| 147 | - parent::__construct($timezone); |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - |
|
| 151 | - /** |
|
| 152 | - * create new blank datetime |
|
| 153 | - * |
|
| 154 | - * @access public |
|
| 155 | - * @return EE_Datetime[] array on success, FALSE on fail |
|
| 156 | - * @throws EE_Error |
|
| 157 | - * @throws InvalidArgumentException |
|
| 158 | - * @throws InvalidDataTypeException |
|
| 159 | - * @throws ReflectionException |
|
| 160 | - * @throws InvalidInterfaceException |
|
| 161 | - */ |
|
| 162 | - public function create_new_blank_datetime() |
|
| 163 | - { |
|
| 164 | - // makes sure timezone is always set. |
|
| 165 | - $timezone_string = $this->get_timezone(); |
|
| 166 | - /** |
|
| 167 | - * Filters the initial start date for the new datetime. |
|
| 168 | - * Any time included in this value will be overridden later so use additional filters to modify the time. |
|
| 169 | - * |
|
| 170 | - * @param int $start_date Unixtimestamp representing now + 30 days in seconds. |
|
| 171 | - * @return int unixtimestamp |
|
| 172 | - */ |
|
| 173 | - $start_date = apply_filters( |
|
| 174 | - 'FHEE__EEM_Datetime__create_new_blank_datetime__start_date', |
|
| 175 | - $this->current_time_for_query('DTT_EVT_start', true) + MONTH_IN_SECONDS |
|
| 176 | - ); |
|
| 177 | - /** |
|
| 178 | - * Filters the initial end date for the new datetime. |
|
| 179 | - * Any time included in this value will be overridden later so use additional filters to modify the time. |
|
| 180 | - * |
|
| 181 | - * @param int $end_data Unixtimestamp representing now + 30 days in seconds. |
|
| 182 | - * @return int unixtimestamp |
|
| 183 | - */ |
|
| 184 | - $end_date = apply_filters( |
|
| 185 | - 'FHEE__EEM_Datetime__create_new_blank_datetime__end_date', |
|
| 186 | - $this->current_time_for_query('DTT_EVT_end', true) + MONTH_IN_SECONDS |
|
| 187 | - ); |
|
| 188 | - $blank_datetime = EE_Datetime::new_instance( |
|
| 189 | - array( |
|
| 190 | - 'DTT_EVT_start' => $start_date, |
|
| 191 | - 'DTT_EVT_end' => $end_date, |
|
| 192 | - 'DTT_order' => 1, |
|
| 193 | - 'DTT_reg_limit' => EE_INF, |
|
| 194 | - ), |
|
| 195 | - $timezone_string |
|
| 196 | - ); |
|
| 197 | - /** |
|
| 198 | - * Filters the initial start time and format for the new EE_Datetime instance. |
|
| 199 | - * |
|
| 200 | - * @param array $start_time An array having size 2. First element is the time, second element is the time |
|
| 201 | - * format. |
|
| 202 | - * @return array |
|
| 203 | - */ |
|
| 204 | - $start_time = apply_filters( |
|
| 205 | - 'FHEE__EEM_Datetime__create_new_blank_datetime__start_time', |
|
| 206 | - ['8am', 'ga'] |
|
| 207 | - ); |
|
| 208 | - /** |
|
| 209 | - * Filters the initial end time and format for the new EE_Datetime instance. |
|
| 210 | - * |
|
| 211 | - * @param array $end_time An array having size 2. First element is the time, second element is the time |
|
| 212 | - * format |
|
| 213 | - * @return array |
|
| 214 | - */ |
|
| 215 | - $end_time = apply_filters( |
|
| 216 | - 'FHEE__EEM_Datetime__create_new_blank_datetime__end_time', |
|
| 217 | - ['5pm', 'ga'] |
|
| 218 | - ); |
|
| 219 | - $this->validateStartAndEndTimeForBlankDate($start_time, $end_time); |
|
| 220 | - $blank_datetime->set_start_time( |
|
| 221 | - $this->convert_datetime_for_query( |
|
| 222 | - 'DTT_EVT_start', |
|
| 223 | - $start_time[0], |
|
| 224 | - $start_time[1], |
|
| 225 | - $timezone_string |
|
| 226 | - ) |
|
| 227 | - ); |
|
| 228 | - $blank_datetime->set_end_time( |
|
| 229 | - $this->convert_datetime_for_query( |
|
| 230 | - 'DTT_EVT_end', |
|
| 231 | - $end_time[0], |
|
| 232 | - $end_time[1], |
|
| 233 | - $timezone_string |
|
| 234 | - ) |
|
| 235 | - ); |
|
| 236 | - return array($blank_datetime); |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - |
|
| 240 | - /** |
|
| 241 | - * Validates whether the start_time and end_time are in the expected format. |
|
| 242 | - * @param array $start_time |
|
| 243 | - * @param array $end_time |
|
| 244 | - * @throws InvalidArgumentException |
|
| 245 | - * @throws InvalidDataTypeException |
|
| 246 | - */ |
|
| 247 | - private function validateStartAndEndTimeForBlankDate($start_time, $end_time) |
|
| 248 | - { |
|
| 249 | - if (! is_array($start_time)) { |
|
| 250 | - throw new InvalidDataTypeException('start_time', $start_time, 'array'); |
|
| 251 | - } |
|
| 252 | - if (! is_array($end_time)) { |
|
| 253 | - throw new InvalidDataTypeException('end_time', $end_time, 'array'); |
|
| 254 | - } |
|
| 255 | - if (count($start_time) !== 2) { |
|
| 256 | - throw new InvalidArgumentException( |
|
| 257 | - sprintf( |
|
| 258 | - 'The variable %1$s is expected to be an array with two elements. The first item in the ' |
|
| 259 | - . 'array should be a valid time string, the second item in the array should be a valid time format', |
|
| 260 | - '$start_time' |
|
| 261 | - ) |
|
| 262 | - ); |
|
| 263 | - } |
|
| 264 | - if (count($end_time) !== 2) { |
|
| 265 | - throw new InvalidArgumentException( |
|
| 266 | - sprintf( |
|
| 267 | - 'The variable %1$s is expected to be an array with two elements. The first item in the ' |
|
| 268 | - . 'array should be a valid time string, the second item in the array should be a valid time format', |
|
| 269 | - '$end_time' |
|
| 270 | - ) |
|
| 271 | - ); |
|
| 272 | - } |
|
| 273 | - } |
|
| 274 | - |
|
| 275 | - |
|
| 276 | - /** |
|
| 277 | - * get event start date from db |
|
| 278 | - * |
|
| 279 | - * @access public |
|
| 280 | - * @param int $EVT_ID |
|
| 281 | - * @return EE_Datetime[] array on success, FALSE on fail |
|
| 282 | - * @throws EE_Error |
|
| 283 | - */ |
|
| 284 | - public function get_all_event_dates($EVT_ID = 0) |
|
| 285 | - { |
|
| 286 | - if (! $EVT_ID) { // on add_new_event event_id gets set to 0 |
|
| 287 | - return $this->create_new_blank_datetime(); |
|
| 288 | - } |
|
| 289 | - $results = $this->get_datetimes_for_event_ordered_by_DTT_order($EVT_ID); |
|
| 290 | - if (empty($results)) { |
|
| 291 | - return $this->create_new_blank_datetime(); |
|
| 292 | - } |
|
| 293 | - return $results; |
|
| 294 | - } |
|
| 295 | - |
|
| 296 | - |
|
| 297 | - /** |
|
| 298 | - * get all datetimes attached to an event ordered by the DTT_order field |
|
| 299 | - * |
|
| 300 | - * @public |
|
| 301 | - * @param int $EVT_ID event id |
|
| 302 | - * @param boolean $include_expired |
|
| 303 | - * @param boolean $include_deleted |
|
| 304 | - * @param int $limit If included then limit the count of results by |
|
| 305 | - * the given number |
|
| 306 | - * @return EE_Datetime[] |
|
| 307 | - * @throws EE_Error |
|
| 308 | - */ |
|
| 309 | - public function get_datetimes_for_event_ordered_by_DTT_order( |
|
| 310 | - $EVT_ID, |
|
| 311 | - $include_expired = true, |
|
| 312 | - $include_deleted = true, |
|
| 313 | - $limit = null |
|
| 314 | - ) { |
|
| 315 | - // sanitize EVT_ID |
|
| 316 | - $EVT_ID = absint($EVT_ID); |
|
| 317 | - $old_assumption = $this->get_assumption_concerning_values_already_prepared_by_model_object(); |
|
| 318 | - $this->assume_values_already_prepared_by_model_object(EEM_Base::prepared_for_use_in_db); |
|
| 319 | - $where_params = array('Event.EVT_ID' => $EVT_ID); |
|
| 320 | - $query_params = ! empty($limit) |
|
| 321 | - ? array( |
|
| 322 | - $where_params, |
|
| 323 | - 'limit' => $limit, |
|
| 324 | - 'order_by' => array('DTT_order' => 'ASC'), |
|
| 325 | - 'default_where_conditions' => 'none', |
|
| 326 | - ) |
|
| 327 | - : array( |
|
| 328 | - $where_params, |
|
| 329 | - 'order_by' => array('DTT_order' => 'ASC'), |
|
| 330 | - 'default_where_conditions' => 'none', |
|
| 331 | - ); |
|
| 332 | - if (! $include_expired) { |
|
| 333 | - $query_params[0]['DTT_EVT_end'] = array('>=', current_time('mysql', true)); |
|
| 334 | - } |
|
| 335 | - if ($include_deleted) { |
|
| 336 | - $query_params[0]['DTT_deleted'] = array('IN', array(true, false)); |
|
| 337 | - } |
|
| 338 | - /** @var EE_Datetime[] $result */ |
|
| 339 | - $result = $this->get_all($query_params); |
|
| 340 | - $this->assume_values_already_prepared_by_model_object($old_assumption); |
|
| 341 | - return $result; |
|
| 342 | - } |
|
| 343 | - |
|
| 344 | - |
|
| 345 | - /** |
|
| 346 | - * Gets the datetimes for the event (with the given limit), and orders them by "importance". |
|
| 347 | - * By importance, we mean that the primary datetimes are most important (DEPRECATED FOR NOW), |
|
| 348 | - * and then the earlier datetimes are the most important. |
|
| 349 | - * Maybe we'll want this to take into account datetimes that haven't already passed, but we don't yet. |
|
| 350 | - * |
|
| 351 | - * @param int $EVT_ID |
|
| 352 | - * @param int $limit |
|
| 353 | - * @return EE_Datetime[]|EE_Base_Class[] |
|
| 354 | - * @throws EE_Error |
|
| 355 | - */ |
|
| 356 | - public function get_datetimes_for_event_ordered_by_importance($EVT_ID = 0, $limit = null) |
|
| 357 | - { |
|
| 358 | - return $this->get_all( |
|
| 359 | - array( |
|
| 360 | - array('Event.EVT_ID' => $EVT_ID), |
|
| 361 | - 'limit' => $limit, |
|
| 362 | - 'order_by' => array('DTT_EVT_start' => 'ASC'), |
|
| 363 | - 'default_where_conditions' => 'none', |
|
| 364 | - ) |
|
| 365 | - ); |
|
| 366 | - } |
|
| 367 | - |
|
| 368 | - |
|
| 369 | - /** |
|
| 370 | - * @param int $EVT_ID |
|
| 371 | - * @param boolean $include_expired |
|
| 372 | - * @param boolean $include_deleted |
|
| 373 | - * @return EE_Datetime |
|
| 374 | - * @throws EE_Error |
|
| 375 | - */ |
|
| 376 | - public function get_oldest_datetime_for_event($EVT_ID, $include_expired = false, $include_deleted = false) |
|
| 377 | - { |
|
| 378 | - $results = $this->get_datetimes_for_event_ordered_by_start_time( |
|
| 379 | - $EVT_ID, |
|
| 380 | - $include_expired, |
|
| 381 | - $include_deleted, |
|
| 382 | - 1 |
|
| 383 | - ); |
|
| 384 | - if ($results) { |
|
| 385 | - return array_shift($results); |
|
| 386 | - } |
|
| 387 | - return null; |
|
| 388 | - } |
|
| 389 | - |
|
| 390 | - |
|
| 391 | - /** |
|
| 392 | - * Gets the 'primary' datetime for an event. |
|
| 393 | - * |
|
| 394 | - * @param int $EVT_ID |
|
| 395 | - * @param bool $try_to_exclude_expired |
|
| 396 | - * @param bool $try_to_exclude_deleted |
|
| 397 | - * @return \EE_Datetime |
|
| 398 | - * @throws EE_Error |
|
| 399 | - */ |
|
| 400 | - public function get_primary_datetime_for_event( |
|
| 401 | - $EVT_ID, |
|
| 402 | - $try_to_exclude_expired = true, |
|
| 403 | - $try_to_exclude_deleted = true |
|
| 404 | - ) { |
|
| 405 | - if ($try_to_exclude_expired) { |
|
| 406 | - $non_expired = $this->get_oldest_datetime_for_event($EVT_ID, false, false); |
|
| 407 | - if ($non_expired) { |
|
| 408 | - return $non_expired; |
|
| 409 | - } |
|
| 410 | - } |
|
| 411 | - if ($try_to_exclude_deleted) { |
|
| 412 | - $expired_even = $this->get_oldest_datetime_for_event($EVT_ID, true); |
|
| 413 | - if ($expired_even) { |
|
| 414 | - return $expired_even; |
|
| 415 | - } |
|
| 416 | - } |
|
| 417 | - return $this->get_oldest_datetime_for_event($EVT_ID, true, true); |
|
| 418 | - } |
|
| 419 | - |
|
| 420 | - |
|
| 421 | - /** |
|
| 422 | - * Gets ALL the datetimes for an event (including trashed ones, for now), ordered |
|
| 423 | - * only by start date |
|
| 424 | - * |
|
| 425 | - * @param int $EVT_ID |
|
| 426 | - * @param boolean $include_expired |
|
| 427 | - * @param boolean $include_deleted |
|
| 428 | - * @param int $limit |
|
| 429 | - * @return EE_Datetime[] |
|
| 430 | - * @throws EE_Error |
|
| 431 | - */ |
|
| 432 | - public function get_datetimes_for_event_ordered_by_start_time( |
|
| 433 | - $EVT_ID, |
|
| 434 | - $include_expired = true, |
|
| 435 | - $include_deleted = true, |
|
| 436 | - $limit = null |
|
| 437 | - ) { |
|
| 438 | - // sanitize EVT_ID |
|
| 439 | - $EVT_ID = absint($EVT_ID); |
|
| 440 | - $old_assumption = $this->get_assumption_concerning_values_already_prepared_by_model_object(); |
|
| 441 | - $this->assume_values_already_prepared_by_model_object(EEM_Base::prepared_for_use_in_db); |
|
| 442 | - $query_params = array(array('Event.EVT_ID' => $EVT_ID), 'order_by' => array('DTT_EVT_start' => 'asc')); |
|
| 443 | - if (! $include_expired) { |
|
| 444 | - $query_params[0]['DTT_EVT_end'] = array('>=', current_time('mysql', true)); |
|
| 445 | - } |
|
| 446 | - if ($include_deleted) { |
|
| 447 | - $query_params[0]['DTT_deleted'] = array('IN', array(true, false)); |
|
| 448 | - } |
|
| 449 | - if ($limit) { |
|
| 450 | - $query_params['limit'] = $limit; |
|
| 451 | - } |
|
| 452 | - /** @var EE_Datetime[] $result */ |
|
| 453 | - $result = $this->get_all($query_params); |
|
| 454 | - $this->assume_values_already_prepared_by_model_object($old_assumption); |
|
| 455 | - return $result; |
|
| 456 | - } |
|
| 457 | - |
|
| 458 | - |
|
| 459 | - /** |
|
| 460 | - * Gets ALL the datetimes for an ticket (including trashed ones, for now), ordered |
|
| 461 | - * only by start date |
|
| 462 | - * |
|
| 463 | - * @param int $TKT_ID |
|
| 464 | - * @param boolean $include_expired |
|
| 465 | - * @param boolean $include_deleted |
|
| 466 | - * @param int $limit |
|
| 467 | - * @return EE_Datetime[] |
|
| 468 | - * @throws EE_Error |
|
| 469 | - */ |
|
| 470 | - public function get_datetimes_for_ticket_ordered_by_start_time( |
|
| 471 | - $TKT_ID, |
|
| 472 | - $include_expired = true, |
|
| 473 | - $include_deleted = true, |
|
| 474 | - $limit = null |
|
| 475 | - ) { |
|
| 476 | - // sanitize TKT_ID |
|
| 477 | - $TKT_ID = absint($TKT_ID); |
|
| 478 | - $old_assumption = $this->get_assumption_concerning_values_already_prepared_by_model_object(); |
|
| 479 | - $this->assume_values_already_prepared_by_model_object(EEM_Base::prepared_for_use_in_db); |
|
| 480 | - $query_params = array(array('Ticket.TKT_ID' => $TKT_ID), 'order_by' => array('DTT_EVT_start' => 'asc')); |
|
| 481 | - if (! $include_expired) { |
|
| 482 | - $query_params[0]['DTT_EVT_end'] = array('>=', current_time('mysql', true)); |
|
| 483 | - } |
|
| 484 | - if ($include_deleted) { |
|
| 485 | - $query_params[0]['DTT_deleted'] = array('IN', array(true, false)); |
|
| 486 | - } |
|
| 487 | - if ($limit) { |
|
| 488 | - $query_params['limit'] = $limit; |
|
| 489 | - } |
|
| 490 | - /** @var EE_Datetime[] $result */ |
|
| 491 | - $result = $this->get_all($query_params); |
|
| 492 | - $this->assume_values_already_prepared_by_model_object($old_assumption); |
|
| 493 | - return $result; |
|
| 494 | - } |
|
| 495 | - |
|
| 496 | - |
|
| 497 | - /** |
|
| 498 | - * Gets all the datetimes for a ticket (including trashed ones, for now), ordered by the DTT_order for the |
|
| 499 | - * datetimes. |
|
| 500 | - * |
|
| 501 | - * @param int $TKT_ID ID of ticket to retrieve the datetimes for |
|
| 502 | - * @param boolean $include_expired whether to include expired datetimes or not |
|
| 503 | - * @param boolean $include_deleted whether to include trashed datetimes or not. |
|
| 504 | - * @param int|null $limit if null, no limit, if int then limit results by |
|
| 505 | - * that number |
|
| 506 | - * @return EE_Datetime[] |
|
| 507 | - * @throws EE_Error |
|
| 508 | - */ |
|
| 509 | - public function get_datetimes_for_ticket_ordered_by_DTT_order( |
|
| 510 | - $TKT_ID, |
|
| 511 | - $include_expired = true, |
|
| 512 | - $include_deleted = true, |
|
| 513 | - $limit = null |
|
| 514 | - ) { |
|
| 515 | - // sanitize id. |
|
| 516 | - $TKT_ID = absint($TKT_ID); |
|
| 517 | - $old_assumption = $this->get_assumption_concerning_values_already_prepared_by_model_object(); |
|
| 518 | - $this->assume_values_already_prepared_by_model_object(EEM_Base::prepared_for_use_in_db); |
|
| 519 | - $where_params = array('Ticket.TKT_ID' => $TKT_ID); |
|
| 520 | - $query_params = array($where_params, 'order_by' => array('DTT_order' => 'ASC')); |
|
| 521 | - if (! $include_expired) { |
|
| 522 | - $query_params[0]['DTT_EVT_end'] = array('>=', current_time('mysql', true)); |
|
| 523 | - } |
|
| 524 | - if ($include_deleted) { |
|
| 525 | - $query_params[0]['DTT_deleted'] = array('IN', array(true, false)); |
|
| 526 | - } |
|
| 527 | - if ($limit) { |
|
| 528 | - $query_params['limit'] = $limit; |
|
| 529 | - } |
|
| 530 | - /** @var EE_Datetime[] $result */ |
|
| 531 | - $result = $this->get_all($query_params); |
|
| 532 | - $this->assume_values_already_prepared_by_model_object($old_assumption); |
|
| 533 | - return $result; |
|
| 534 | - } |
|
| 535 | - |
|
| 536 | - |
|
| 537 | - /** |
|
| 538 | - * Gets the most important datetime for a particular event (ie, the primary event usually. But if for some WACK |
|
| 539 | - * reason it doesn't exist, we consider the earliest event the most important) |
|
| 540 | - * |
|
| 541 | - * @param int $EVT_ID |
|
| 542 | - * @return EE_Datetime |
|
| 543 | - * @throws EE_Error |
|
| 544 | - */ |
|
| 545 | - public function get_most_important_datetime_for_event($EVT_ID) |
|
| 546 | - { |
|
| 547 | - $results = $this->get_datetimes_for_event_ordered_by_importance($EVT_ID, 1); |
|
| 548 | - if ($results) { |
|
| 549 | - return array_shift($results); |
|
| 550 | - } |
|
| 551 | - return null; |
|
| 552 | - } |
|
| 553 | - |
|
| 554 | - |
|
| 555 | - /** |
|
| 556 | - * This returns a wpdb->results Array of all DTT month and years matching the incoming query params and |
|
| 557 | - * grouped by month and year. |
|
| 558 | - * |
|
| 559 | - * @param array $where_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
| 560 | - * @param string $evt_active_status A string representing the evt active status to filter the months by. |
|
| 561 | - * Can be: |
|
| 562 | - * - '' = no filter |
|
| 563 | - * - upcoming = Published events with at least one upcoming datetime. |
|
| 564 | - * - expired = Events with all datetimes expired. |
|
| 565 | - * - active = Events that are published and have at least one datetime that |
|
| 566 | - * starts before now and ends after now. |
|
| 567 | - * - inactive = Events that are either not published. |
|
| 568 | - * @return EE_Base_Class[] |
|
| 569 | - * @throws EE_Error |
|
| 570 | - * @throws InvalidArgumentException |
|
| 571 | - * @throws InvalidArgumentException |
|
| 572 | - */ |
|
| 573 | - public function get_dtt_months_and_years($where_params, $evt_active_status = '') |
|
| 574 | - { |
|
| 575 | - $current_time_for_DTT_EVT_start = $this->current_time_for_query('DTT_EVT_start'); |
|
| 576 | - $current_time_for_DTT_EVT_end = $this->current_time_for_query('DTT_EVT_end'); |
|
| 577 | - switch ($evt_active_status) { |
|
| 578 | - case 'upcoming': |
|
| 579 | - $where_params['Event.status'] = 'publish'; |
|
| 580 | - // if there are already query_params matching DTT_EVT_start then we need to modify that to add them. |
|
| 581 | - if (isset($where_params['DTT_EVT_start'])) { |
|
| 582 | - $where_params['DTT_EVT_start*****'] = $where_params['DTT_EVT_start']; |
|
| 583 | - } |
|
| 584 | - $where_params['DTT_EVT_start'] = array('>', $current_time_for_DTT_EVT_start); |
|
| 585 | - break; |
|
| 586 | - case 'expired': |
|
| 587 | - if (isset($where_params['Event.status'])) { |
|
| 588 | - unset($where_params['Event.status']); |
|
| 589 | - } |
|
| 590 | - // get events to exclude |
|
| 591 | - $exclude_query[0] = array_merge( |
|
| 592 | - $where_params, |
|
| 593 | - array('DTT_EVT_end' => array('>', $current_time_for_DTT_EVT_end)) |
|
| 594 | - ); |
|
| 595 | - // first get all events that have datetimes where its not expired. |
|
| 596 | - $event_ids = $this->_get_all_wpdb_results( |
|
| 597 | - $exclude_query, |
|
| 598 | - OBJECT_K, |
|
| 599 | - 'Datetime.EVT_ID' |
|
| 600 | - ); |
|
| 601 | - $event_ids = array_keys($event_ids); |
|
| 602 | - if (isset($where_params['DTT_EVT_end'])) { |
|
| 603 | - $where_params['DTT_EVT_end****'] = $where_params['DTT_EVT_end']; |
|
| 604 | - } |
|
| 605 | - $where_params['DTT_EVT_end'] = array('<', $current_time_for_DTT_EVT_end); |
|
| 606 | - $where_params['Event.EVT_ID'] = array('NOT IN', $event_ids); |
|
| 607 | - break; |
|
| 608 | - case 'active': |
|
| 609 | - $where_params['Event.status'] = 'publish'; |
|
| 610 | - if (isset($where_params['DTT_EVT_start'])) { |
|
| 611 | - $where_params['Datetime.DTT_EVT_start******'] = $where_params['DTT_EVT_start']; |
|
| 612 | - } |
|
| 613 | - if (isset($where_params['Datetime.DTT_EVT_end'])) { |
|
| 614 | - $where_params['Datetime.DTT_EVT_end*****'] = $where_params['DTT_EVT_end']; |
|
| 615 | - } |
|
| 616 | - $where_params['DTT_EVT_start'] = array('<', $current_time_for_DTT_EVT_start); |
|
| 617 | - $where_params['DTT_EVT_end'] = array('>', $current_time_for_DTT_EVT_end); |
|
| 618 | - break; |
|
| 619 | - case 'inactive': |
|
| 620 | - if (isset($where_params['Event.status'])) { |
|
| 621 | - unset($where_params['Event.status']); |
|
| 622 | - } |
|
| 623 | - if (isset($where_params['OR'])) { |
|
| 624 | - $where_params['AND']['OR'] = $where_params['OR']; |
|
| 625 | - } |
|
| 626 | - if (isset($where_params['DTT_EVT_end'])) { |
|
| 627 | - $where_params['AND']['DTT_EVT_end****'] = $where_params['DTT_EVT_end']; |
|
| 628 | - unset($where_params['DTT_EVT_end']); |
|
| 629 | - } |
|
| 630 | - if (isset($where_params['DTT_EVT_start'])) { |
|
| 631 | - $where_params['AND']['DTT_EVT_start'] = $where_params['DTT_EVT_start']; |
|
| 632 | - unset($where_params['DTT_EVT_start']); |
|
| 633 | - } |
|
| 634 | - $where_params['AND']['Event.status'] = array('!=', 'publish'); |
|
| 635 | - break; |
|
| 636 | - } |
|
| 637 | - $query_params[0] = $where_params; |
|
| 638 | - $query_params['group_by'] = array('dtt_year', 'dtt_month'); |
|
| 639 | - $query_params['order_by'] = array('DTT_EVT_start' => 'DESC'); |
|
| 640 | - $query_interval = EEH_DTT_Helper::get_sql_query_interval_for_offset( |
|
| 641 | - $this->get_timezone(), |
|
| 642 | - 'DTT_EVT_start' |
|
| 643 | - ); |
|
| 644 | - $columns_to_select = array( |
|
| 645 | - 'dtt_year' => array('YEAR(' . $query_interval . ')', '%s'), |
|
| 646 | - 'dtt_month' => array('MONTHNAME(' . $query_interval . ')', '%s'), |
|
| 647 | - 'dtt_month_num' => array('MONTH(' . $query_interval . ')', '%s'), |
|
| 648 | - ); |
|
| 649 | - return $this->_get_all_wpdb_results($query_params, OBJECT, $columns_to_select); |
|
| 650 | - } |
|
| 651 | - |
|
| 652 | - |
|
| 653 | - /** |
|
| 654 | - * Updates the DTT_sold attribute on each datetime (based on the registrations |
|
| 655 | - * for the tickets for each datetime) |
|
| 656 | - * |
|
| 657 | - * @param EE_Base_Class[]|EE_Datetime[] $datetimes |
|
| 658 | - * @throws EE_Error |
|
| 659 | - */ |
|
| 660 | - public function update_sold($datetimes) |
|
| 661 | - { |
|
| 662 | - EE_Error::doing_it_wrong( |
|
| 663 | - __FUNCTION__, |
|
| 664 | - esc_html__( |
|
| 665 | - 'Please use \EEM_Ticket::update_tickets_sold() instead which will in turn correctly update both the Ticket AND Datetime counts.', |
|
| 666 | - 'event_espresso' |
|
| 667 | - ), |
|
| 668 | - '4.9.32.rc.005' |
|
| 669 | - ); |
|
| 670 | - foreach ($datetimes as $datetime) { |
|
| 671 | - $datetime->update_sold(); |
|
| 672 | - } |
|
| 673 | - } |
|
| 674 | - |
|
| 675 | - |
|
| 676 | - /** |
|
| 677 | - * Gets the total number of tickets available at a particular datetime |
|
| 678 | - * (does NOT take into account the datetime's spaces available) |
|
| 679 | - * |
|
| 680 | - * @param int $DTT_ID |
|
| 681 | - * @param array $query_params |
|
| 682 | - * @return int of tickets available. If sold out, return less than 1. If infinite, returns EE_INF, IF there are NO |
|
| 683 | - * tickets attached to datetime then FALSE is returned. |
|
| 684 | - */ |
|
| 685 | - public function sum_tickets_currently_available_at_datetime($DTT_ID, array $query_params = array()) |
|
| 686 | - { |
|
| 687 | - $datetime = $this->get_one_by_ID($DTT_ID); |
|
| 688 | - if ($datetime instanceof EE_Datetime) { |
|
| 689 | - return $datetime->tickets_remaining($query_params); |
|
| 690 | - } |
|
| 691 | - return 0; |
|
| 692 | - } |
|
| 693 | - |
|
| 694 | - |
|
| 695 | - /** |
|
| 696 | - * This returns an array of counts of datetimes in the database for each Datetime status that can be queried. |
|
| 697 | - * |
|
| 698 | - * @param array $stati_to_include If included you can restrict the statuses we return counts for by including the |
|
| 699 | - * stati you want counts for as values in the array. An empty array returns counts |
|
| 700 | - * for all valid stati. |
|
| 701 | - * @param array $query_params If included can be used to refine the conditions for returning the count (i.e. |
|
| 702 | - * only for Datetimes connected to a specific event, or specific ticket. |
|
| 703 | - * @return array The value returned is an array indexed by Datetime Status and the values are the counts. The |
|
| 704 | - * @throws EE_Error |
|
| 705 | - * stati used as index keys are: EE_Datetime::active EE_Datetime::upcoming |
|
| 706 | - * EE_Datetime::expired |
|
| 707 | - */ |
|
| 708 | - public function get_datetime_counts_by_status(array $stati_to_include = array(), array $query_params = array()) |
|
| 709 | - { |
|
| 710 | - // only accept where conditions for this query. |
|
| 711 | - $_where = isset($query_params[0]) ? $query_params[0] : array(); |
|
| 712 | - $status_query_args = array( |
|
| 713 | - EE_Datetime::active => array_merge( |
|
| 714 | - $_where, |
|
| 715 | - array('DTT_EVT_start' => array('<', time()), 'DTT_EVT_end' => array('>', time())) |
|
| 716 | - ), |
|
| 717 | - EE_Datetime::upcoming => array_merge( |
|
| 718 | - $_where, |
|
| 719 | - array('DTT_EVT_start' => array('>', time())) |
|
| 720 | - ), |
|
| 721 | - EE_Datetime::expired => array_merge( |
|
| 722 | - $_where, |
|
| 723 | - array('DTT_EVT_end' => array('<', time())) |
|
| 724 | - ), |
|
| 725 | - ); |
|
| 726 | - if (! empty($stati_to_include)) { |
|
| 727 | - foreach (array_keys($status_query_args) as $status) { |
|
| 728 | - if (! in_array($status, $stati_to_include, true)) { |
|
| 729 | - unset($status_query_args[ $status ]); |
|
| 730 | - } |
|
| 731 | - } |
|
| 732 | - } |
|
| 733 | - // loop through and query counts for each stati. |
|
| 734 | - $status_query_results = array(); |
|
| 735 | - foreach ($status_query_args as $status => $status_where_conditions) { |
|
| 736 | - $status_query_results[ $status ] = EEM_Datetime::count( |
|
| 737 | - array($status_where_conditions), |
|
| 738 | - 'DTT_ID', |
|
| 739 | - true |
|
| 740 | - ); |
|
| 741 | - } |
|
| 742 | - return $status_query_results; |
|
| 743 | - } |
|
| 744 | - |
|
| 745 | - |
|
| 746 | - /** |
|
| 747 | - * Returns the specific count for a given Datetime status matching any given query_params. |
|
| 748 | - * |
|
| 749 | - * @param string $status Valid string representation for Datetime status requested. (Defaults to Active). |
|
| 750 | - * @param array $query_params |
|
| 751 | - * @return int |
|
| 752 | - * @throws EE_Error |
|
| 753 | - */ |
|
| 754 | - public function get_datetime_count_for_status($status = EE_Datetime::active, array $query_params = array()) |
|
| 755 | - { |
|
| 756 | - $count = $this->get_datetime_counts_by_status(array($status), $query_params); |
|
| 757 | - return ! empty($count[ $status ]) ? $count[ $status ] : 0; |
|
| 758 | - } |
|
| 16 | + /** |
|
| 17 | + * @var EEM_Datetime $_instance |
|
| 18 | + */ |
|
| 19 | + protected static $_instance; |
|
| 20 | + |
|
| 21 | + |
|
| 22 | + /** |
|
| 23 | + * private constructor to prevent direct creation |
|
| 24 | + * |
|
| 25 | + * @param string $timezone A string representing the timezone we want to set for returned Date Time Strings |
|
| 26 | + * (and any incoming timezone data that gets saved). |
|
| 27 | + * Note this just sends the timezone info to the date time model field objects. |
|
| 28 | + * Default is NULL |
|
| 29 | + * (and will be assumed using the set timezone in the 'timezone_string' wp option) |
|
| 30 | + * @throws EE_Error |
|
| 31 | + * @throws InvalidArgumentException |
|
| 32 | + * @throws InvalidArgumentException |
|
| 33 | + */ |
|
| 34 | + protected function __construct($timezone) |
|
| 35 | + { |
|
| 36 | + $this->singular_item = esc_html__('Datetime', 'event_espresso'); |
|
| 37 | + $this->plural_item = esc_html__('Datetimes', 'event_espresso'); |
|
| 38 | + $this->_tables = array( |
|
| 39 | + 'Datetime' => new EE_Primary_Table('esp_datetime', 'DTT_ID'), |
|
| 40 | + ); |
|
| 41 | + $this->_fields = array( |
|
| 42 | + 'Datetime' => array( |
|
| 43 | + 'DTT_ID' => new EE_Primary_Key_Int_Field( |
|
| 44 | + 'DTT_ID', |
|
| 45 | + esc_html__('Datetime ID', 'event_espresso') |
|
| 46 | + ), |
|
| 47 | + 'EVT_ID' => new EE_Foreign_Key_Int_Field( |
|
| 48 | + 'EVT_ID', |
|
| 49 | + esc_html__('Event ID', 'event_espresso'), |
|
| 50 | + false, |
|
| 51 | + 0, |
|
| 52 | + 'Event' |
|
| 53 | + ), |
|
| 54 | + 'DTT_name' => new EE_Plain_Text_Field( |
|
| 55 | + 'DTT_name', |
|
| 56 | + esc_html__('Datetime Name', 'event_espresso'), |
|
| 57 | + false, |
|
| 58 | + '' |
|
| 59 | + ), |
|
| 60 | + 'DTT_description' => new EE_Post_Content_Field( |
|
| 61 | + 'DTT_description', |
|
| 62 | + esc_html__('Description for Datetime', 'event_espresso'), |
|
| 63 | + false, |
|
| 64 | + '' |
|
| 65 | + ), |
|
| 66 | + 'DTT_EVT_start' => new EE_Datetime_Field( |
|
| 67 | + 'DTT_EVT_start', |
|
| 68 | + esc_html__('Start time/date of Event', 'event_espresso'), |
|
| 69 | + false, |
|
| 70 | + EE_Datetime_Field::now, |
|
| 71 | + $timezone |
|
| 72 | + ), |
|
| 73 | + 'DTT_EVT_end' => new EE_Datetime_Field( |
|
| 74 | + 'DTT_EVT_end', |
|
| 75 | + esc_html__('End time/date of Event', 'event_espresso'), |
|
| 76 | + false, |
|
| 77 | + EE_Datetime_Field::now, |
|
| 78 | + $timezone |
|
| 79 | + ), |
|
| 80 | + 'DTT_reg_limit' => new EE_Infinite_Integer_Field( |
|
| 81 | + 'DTT_reg_limit', |
|
| 82 | + esc_html__('Registration Limit for this time', 'event_espresso'), |
|
| 83 | + true, |
|
| 84 | + EE_INF |
|
| 85 | + ), |
|
| 86 | + 'DTT_sold' => new EE_Integer_Field( |
|
| 87 | + 'DTT_sold', |
|
| 88 | + esc_html__('How many sales for this Datetime that have occurred', 'event_espresso'), |
|
| 89 | + true, |
|
| 90 | + 0 |
|
| 91 | + ), |
|
| 92 | + 'DTT_reserved' => new EE_Integer_Field( |
|
| 93 | + 'DTT_reserved', |
|
| 94 | + esc_html__('Quantity of tickets reserved, but not yet fully purchased', 'event_espresso'), |
|
| 95 | + false, |
|
| 96 | + 0 |
|
| 97 | + ), |
|
| 98 | + 'DTT_is_primary' => new EE_Boolean_Field( |
|
| 99 | + 'DTT_is_primary', |
|
| 100 | + esc_html__('Flag indicating datetime is primary one for event', 'event_espresso'), |
|
| 101 | + false, |
|
| 102 | + false |
|
| 103 | + ), |
|
| 104 | + 'DTT_order' => new EE_Integer_Field( |
|
| 105 | + 'DTT_order', |
|
| 106 | + esc_html__('The order in which the Datetime is displayed', 'event_espresso'), |
|
| 107 | + false, |
|
| 108 | + 0 |
|
| 109 | + ), |
|
| 110 | + 'DTT_parent' => new EE_Integer_Field( |
|
| 111 | + 'DTT_parent', |
|
| 112 | + esc_html__('Indicates what DTT_ID is the parent of this DTT_ID', 'event_espresso'), |
|
| 113 | + true, |
|
| 114 | + 0 |
|
| 115 | + ), |
|
| 116 | + 'DTT_deleted' => new EE_Trashed_Flag_Field( |
|
| 117 | + 'DTT_deleted', |
|
| 118 | + esc_html__('Flag indicating datetime is archived', 'event_espresso'), |
|
| 119 | + false, |
|
| 120 | + false |
|
| 121 | + ), |
|
| 122 | + ), |
|
| 123 | + ); |
|
| 124 | + $this->_model_relations = array( |
|
| 125 | + 'Ticket' => new EE_HABTM_Relation('Datetime_Ticket'), |
|
| 126 | + 'Event' => new EE_Belongs_To_Relation(), |
|
| 127 | + 'Checkin' => new EE_Has_Many_Relation(), |
|
| 128 | + 'Datetime_Ticket' => new EE_Has_Many_Relation(), |
|
| 129 | + ); |
|
| 130 | + $path_to_event_model = 'Event'; |
|
| 131 | + $this->model_chain_to_password = $path_to_event_model; |
|
| 132 | + $this->_model_chain_to_wp_user = $path_to_event_model; |
|
| 133 | + // this model is generally available for reading |
|
| 134 | + $this->_cap_restriction_generators[ EEM_Base::caps_read ] = new EE_Restriction_Generator_Event_Related_Public( |
|
| 135 | + $path_to_event_model |
|
| 136 | + ); |
|
| 137 | + $this->_cap_restriction_generators[ EEM_Base::caps_read_admin ] = new EE_Restriction_Generator_Event_Related_Protected( |
|
| 138 | + $path_to_event_model |
|
| 139 | + ); |
|
| 140 | + $this->_cap_restriction_generators[ EEM_Base::caps_edit ] = new EE_Restriction_Generator_Event_Related_Protected( |
|
| 141 | + $path_to_event_model |
|
| 142 | + ); |
|
| 143 | + $this->_cap_restriction_generators[ EEM_Base::caps_delete ] = new EE_Restriction_Generator_Event_Related_Protected( |
|
| 144 | + $path_to_event_model, |
|
| 145 | + EEM_Base::caps_edit |
|
| 146 | + ); |
|
| 147 | + parent::__construct($timezone); |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + |
|
| 151 | + /** |
|
| 152 | + * create new blank datetime |
|
| 153 | + * |
|
| 154 | + * @access public |
|
| 155 | + * @return EE_Datetime[] array on success, FALSE on fail |
|
| 156 | + * @throws EE_Error |
|
| 157 | + * @throws InvalidArgumentException |
|
| 158 | + * @throws InvalidDataTypeException |
|
| 159 | + * @throws ReflectionException |
|
| 160 | + * @throws InvalidInterfaceException |
|
| 161 | + */ |
|
| 162 | + public function create_new_blank_datetime() |
|
| 163 | + { |
|
| 164 | + // makes sure timezone is always set. |
|
| 165 | + $timezone_string = $this->get_timezone(); |
|
| 166 | + /** |
|
| 167 | + * Filters the initial start date for the new datetime. |
|
| 168 | + * Any time included in this value will be overridden later so use additional filters to modify the time. |
|
| 169 | + * |
|
| 170 | + * @param int $start_date Unixtimestamp representing now + 30 days in seconds. |
|
| 171 | + * @return int unixtimestamp |
|
| 172 | + */ |
|
| 173 | + $start_date = apply_filters( |
|
| 174 | + 'FHEE__EEM_Datetime__create_new_blank_datetime__start_date', |
|
| 175 | + $this->current_time_for_query('DTT_EVT_start', true) + MONTH_IN_SECONDS |
|
| 176 | + ); |
|
| 177 | + /** |
|
| 178 | + * Filters the initial end date for the new datetime. |
|
| 179 | + * Any time included in this value will be overridden later so use additional filters to modify the time. |
|
| 180 | + * |
|
| 181 | + * @param int $end_data Unixtimestamp representing now + 30 days in seconds. |
|
| 182 | + * @return int unixtimestamp |
|
| 183 | + */ |
|
| 184 | + $end_date = apply_filters( |
|
| 185 | + 'FHEE__EEM_Datetime__create_new_blank_datetime__end_date', |
|
| 186 | + $this->current_time_for_query('DTT_EVT_end', true) + MONTH_IN_SECONDS |
|
| 187 | + ); |
|
| 188 | + $blank_datetime = EE_Datetime::new_instance( |
|
| 189 | + array( |
|
| 190 | + 'DTT_EVT_start' => $start_date, |
|
| 191 | + 'DTT_EVT_end' => $end_date, |
|
| 192 | + 'DTT_order' => 1, |
|
| 193 | + 'DTT_reg_limit' => EE_INF, |
|
| 194 | + ), |
|
| 195 | + $timezone_string |
|
| 196 | + ); |
|
| 197 | + /** |
|
| 198 | + * Filters the initial start time and format for the new EE_Datetime instance. |
|
| 199 | + * |
|
| 200 | + * @param array $start_time An array having size 2. First element is the time, second element is the time |
|
| 201 | + * format. |
|
| 202 | + * @return array |
|
| 203 | + */ |
|
| 204 | + $start_time = apply_filters( |
|
| 205 | + 'FHEE__EEM_Datetime__create_new_blank_datetime__start_time', |
|
| 206 | + ['8am', 'ga'] |
|
| 207 | + ); |
|
| 208 | + /** |
|
| 209 | + * Filters the initial end time and format for the new EE_Datetime instance. |
|
| 210 | + * |
|
| 211 | + * @param array $end_time An array having size 2. First element is the time, second element is the time |
|
| 212 | + * format |
|
| 213 | + * @return array |
|
| 214 | + */ |
|
| 215 | + $end_time = apply_filters( |
|
| 216 | + 'FHEE__EEM_Datetime__create_new_blank_datetime__end_time', |
|
| 217 | + ['5pm', 'ga'] |
|
| 218 | + ); |
|
| 219 | + $this->validateStartAndEndTimeForBlankDate($start_time, $end_time); |
|
| 220 | + $blank_datetime->set_start_time( |
|
| 221 | + $this->convert_datetime_for_query( |
|
| 222 | + 'DTT_EVT_start', |
|
| 223 | + $start_time[0], |
|
| 224 | + $start_time[1], |
|
| 225 | + $timezone_string |
|
| 226 | + ) |
|
| 227 | + ); |
|
| 228 | + $blank_datetime->set_end_time( |
|
| 229 | + $this->convert_datetime_for_query( |
|
| 230 | + 'DTT_EVT_end', |
|
| 231 | + $end_time[0], |
|
| 232 | + $end_time[1], |
|
| 233 | + $timezone_string |
|
| 234 | + ) |
|
| 235 | + ); |
|
| 236 | + return array($blank_datetime); |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + |
|
| 240 | + /** |
|
| 241 | + * Validates whether the start_time and end_time are in the expected format. |
|
| 242 | + * @param array $start_time |
|
| 243 | + * @param array $end_time |
|
| 244 | + * @throws InvalidArgumentException |
|
| 245 | + * @throws InvalidDataTypeException |
|
| 246 | + */ |
|
| 247 | + private function validateStartAndEndTimeForBlankDate($start_time, $end_time) |
|
| 248 | + { |
|
| 249 | + if (! is_array($start_time)) { |
|
| 250 | + throw new InvalidDataTypeException('start_time', $start_time, 'array'); |
|
| 251 | + } |
|
| 252 | + if (! is_array($end_time)) { |
|
| 253 | + throw new InvalidDataTypeException('end_time', $end_time, 'array'); |
|
| 254 | + } |
|
| 255 | + if (count($start_time) !== 2) { |
|
| 256 | + throw new InvalidArgumentException( |
|
| 257 | + sprintf( |
|
| 258 | + 'The variable %1$s is expected to be an array with two elements. The first item in the ' |
|
| 259 | + . 'array should be a valid time string, the second item in the array should be a valid time format', |
|
| 260 | + '$start_time' |
|
| 261 | + ) |
|
| 262 | + ); |
|
| 263 | + } |
|
| 264 | + if (count($end_time) !== 2) { |
|
| 265 | + throw new InvalidArgumentException( |
|
| 266 | + sprintf( |
|
| 267 | + 'The variable %1$s is expected to be an array with two elements. The first item in the ' |
|
| 268 | + . 'array should be a valid time string, the second item in the array should be a valid time format', |
|
| 269 | + '$end_time' |
|
| 270 | + ) |
|
| 271 | + ); |
|
| 272 | + } |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + |
|
| 276 | + /** |
|
| 277 | + * get event start date from db |
|
| 278 | + * |
|
| 279 | + * @access public |
|
| 280 | + * @param int $EVT_ID |
|
| 281 | + * @return EE_Datetime[] array on success, FALSE on fail |
|
| 282 | + * @throws EE_Error |
|
| 283 | + */ |
|
| 284 | + public function get_all_event_dates($EVT_ID = 0) |
|
| 285 | + { |
|
| 286 | + if (! $EVT_ID) { // on add_new_event event_id gets set to 0 |
|
| 287 | + return $this->create_new_blank_datetime(); |
|
| 288 | + } |
|
| 289 | + $results = $this->get_datetimes_for_event_ordered_by_DTT_order($EVT_ID); |
|
| 290 | + if (empty($results)) { |
|
| 291 | + return $this->create_new_blank_datetime(); |
|
| 292 | + } |
|
| 293 | + return $results; |
|
| 294 | + } |
|
| 295 | + |
|
| 296 | + |
|
| 297 | + /** |
|
| 298 | + * get all datetimes attached to an event ordered by the DTT_order field |
|
| 299 | + * |
|
| 300 | + * @public |
|
| 301 | + * @param int $EVT_ID event id |
|
| 302 | + * @param boolean $include_expired |
|
| 303 | + * @param boolean $include_deleted |
|
| 304 | + * @param int $limit If included then limit the count of results by |
|
| 305 | + * the given number |
|
| 306 | + * @return EE_Datetime[] |
|
| 307 | + * @throws EE_Error |
|
| 308 | + */ |
|
| 309 | + public function get_datetimes_for_event_ordered_by_DTT_order( |
|
| 310 | + $EVT_ID, |
|
| 311 | + $include_expired = true, |
|
| 312 | + $include_deleted = true, |
|
| 313 | + $limit = null |
|
| 314 | + ) { |
|
| 315 | + // sanitize EVT_ID |
|
| 316 | + $EVT_ID = absint($EVT_ID); |
|
| 317 | + $old_assumption = $this->get_assumption_concerning_values_already_prepared_by_model_object(); |
|
| 318 | + $this->assume_values_already_prepared_by_model_object(EEM_Base::prepared_for_use_in_db); |
|
| 319 | + $where_params = array('Event.EVT_ID' => $EVT_ID); |
|
| 320 | + $query_params = ! empty($limit) |
|
| 321 | + ? array( |
|
| 322 | + $where_params, |
|
| 323 | + 'limit' => $limit, |
|
| 324 | + 'order_by' => array('DTT_order' => 'ASC'), |
|
| 325 | + 'default_where_conditions' => 'none', |
|
| 326 | + ) |
|
| 327 | + : array( |
|
| 328 | + $where_params, |
|
| 329 | + 'order_by' => array('DTT_order' => 'ASC'), |
|
| 330 | + 'default_where_conditions' => 'none', |
|
| 331 | + ); |
|
| 332 | + if (! $include_expired) { |
|
| 333 | + $query_params[0]['DTT_EVT_end'] = array('>=', current_time('mysql', true)); |
|
| 334 | + } |
|
| 335 | + if ($include_deleted) { |
|
| 336 | + $query_params[0]['DTT_deleted'] = array('IN', array(true, false)); |
|
| 337 | + } |
|
| 338 | + /** @var EE_Datetime[] $result */ |
|
| 339 | + $result = $this->get_all($query_params); |
|
| 340 | + $this->assume_values_already_prepared_by_model_object($old_assumption); |
|
| 341 | + return $result; |
|
| 342 | + } |
|
| 343 | + |
|
| 344 | + |
|
| 345 | + /** |
|
| 346 | + * Gets the datetimes for the event (with the given limit), and orders them by "importance". |
|
| 347 | + * By importance, we mean that the primary datetimes are most important (DEPRECATED FOR NOW), |
|
| 348 | + * and then the earlier datetimes are the most important. |
|
| 349 | + * Maybe we'll want this to take into account datetimes that haven't already passed, but we don't yet. |
|
| 350 | + * |
|
| 351 | + * @param int $EVT_ID |
|
| 352 | + * @param int $limit |
|
| 353 | + * @return EE_Datetime[]|EE_Base_Class[] |
|
| 354 | + * @throws EE_Error |
|
| 355 | + */ |
|
| 356 | + public function get_datetimes_for_event_ordered_by_importance($EVT_ID = 0, $limit = null) |
|
| 357 | + { |
|
| 358 | + return $this->get_all( |
|
| 359 | + array( |
|
| 360 | + array('Event.EVT_ID' => $EVT_ID), |
|
| 361 | + 'limit' => $limit, |
|
| 362 | + 'order_by' => array('DTT_EVT_start' => 'ASC'), |
|
| 363 | + 'default_where_conditions' => 'none', |
|
| 364 | + ) |
|
| 365 | + ); |
|
| 366 | + } |
|
| 367 | + |
|
| 368 | + |
|
| 369 | + /** |
|
| 370 | + * @param int $EVT_ID |
|
| 371 | + * @param boolean $include_expired |
|
| 372 | + * @param boolean $include_deleted |
|
| 373 | + * @return EE_Datetime |
|
| 374 | + * @throws EE_Error |
|
| 375 | + */ |
|
| 376 | + public function get_oldest_datetime_for_event($EVT_ID, $include_expired = false, $include_deleted = false) |
|
| 377 | + { |
|
| 378 | + $results = $this->get_datetimes_for_event_ordered_by_start_time( |
|
| 379 | + $EVT_ID, |
|
| 380 | + $include_expired, |
|
| 381 | + $include_deleted, |
|
| 382 | + 1 |
|
| 383 | + ); |
|
| 384 | + if ($results) { |
|
| 385 | + return array_shift($results); |
|
| 386 | + } |
|
| 387 | + return null; |
|
| 388 | + } |
|
| 389 | + |
|
| 390 | + |
|
| 391 | + /** |
|
| 392 | + * Gets the 'primary' datetime for an event. |
|
| 393 | + * |
|
| 394 | + * @param int $EVT_ID |
|
| 395 | + * @param bool $try_to_exclude_expired |
|
| 396 | + * @param bool $try_to_exclude_deleted |
|
| 397 | + * @return \EE_Datetime |
|
| 398 | + * @throws EE_Error |
|
| 399 | + */ |
|
| 400 | + public function get_primary_datetime_for_event( |
|
| 401 | + $EVT_ID, |
|
| 402 | + $try_to_exclude_expired = true, |
|
| 403 | + $try_to_exclude_deleted = true |
|
| 404 | + ) { |
|
| 405 | + if ($try_to_exclude_expired) { |
|
| 406 | + $non_expired = $this->get_oldest_datetime_for_event($EVT_ID, false, false); |
|
| 407 | + if ($non_expired) { |
|
| 408 | + return $non_expired; |
|
| 409 | + } |
|
| 410 | + } |
|
| 411 | + if ($try_to_exclude_deleted) { |
|
| 412 | + $expired_even = $this->get_oldest_datetime_for_event($EVT_ID, true); |
|
| 413 | + if ($expired_even) { |
|
| 414 | + return $expired_even; |
|
| 415 | + } |
|
| 416 | + } |
|
| 417 | + return $this->get_oldest_datetime_for_event($EVT_ID, true, true); |
|
| 418 | + } |
|
| 419 | + |
|
| 420 | + |
|
| 421 | + /** |
|
| 422 | + * Gets ALL the datetimes for an event (including trashed ones, for now), ordered |
|
| 423 | + * only by start date |
|
| 424 | + * |
|
| 425 | + * @param int $EVT_ID |
|
| 426 | + * @param boolean $include_expired |
|
| 427 | + * @param boolean $include_deleted |
|
| 428 | + * @param int $limit |
|
| 429 | + * @return EE_Datetime[] |
|
| 430 | + * @throws EE_Error |
|
| 431 | + */ |
|
| 432 | + public function get_datetimes_for_event_ordered_by_start_time( |
|
| 433 | + $EVT_ID, |
|
| 434 | + $include_expired = true, |
|
| 435 | + $include_deleted = true, |
|
| 436 | + $limit = null |
|
| 437 | + ) { |
|
| 438 | + // sanitize EVT_ID |
|
| 439 | + $EVT_ID = absint($EVT_ID); |
|
| 440 | + $old_assumption = $this->get_assumption_concerning_values_already_prepared_by_model_object(); |
|
| 441 | + $this->assume_values_already_prepared_by_model_object(EEM_Base::prepared_for_use_in_db); |
|
| 442 | + $query_params = array(array('Event.EVT_ID' => $EVT_ID), 'order_by' => array('DTT_EVT_start' => 'asc')); |
|
| 443 | + if (! $include_expired) { |
|
| 444 | + $query_params[0]['DTT_EVT_end'] = array('>=', current_time('mysql', true)); |
|
| 445 | + } |
|
| 446 | + if ($include_deleted) { |
|
| 447 | + $query_params[0]['DTT_deleted'] = array('IN', array(true, false)); |
|
| 448 | + } |
|
| 449 | + if ($limit) { |
|
| 450 | + $query_params['limit'] = $limit; |
|
| 451 | + } |
|
| 452 | + /** @var EE_Datetime[] $result */ |
|
| 453 | + $result = $this->get_all($query_params); |
|
| 454 | + $this->assume_values_already_prepared_by_model_object($old_assumption); |
|
| 455 | + return $result; |
|
| 456 | + } |
|
| 457 | + |
|
| 458 | + |
|
| 459 | + /** |
|
| 460 | + * Gets ALL the datetimes for an ticket (including trashed ones, for now), ordered |
|
| 461 | + * only by start date |
|
| 462 | + * |
|
| 463 | + * @param int $TKT_ID |
|
| 464 | + * @param boolean $include_expired |
|
| 465 | + * @param boolean $include_deleted |
|
| 466 | + * @param int $limit |
|
| 467 | + * @return EE_Datetime[] |
|
| 468 | + * @throws EE_Error |
|
| 469 | + */ |
|
| 470 | + public function get_datetimes_for_ticket_ordered_by_start_time( |
|
| 471 | + $TKT_ID, |
|
| 472 | + $include_expired = true, |
|
| 473 | + $include_deleted = true, |
|
| 474 | + $limit = null |
|
| 475 | + ) { |
|
| 476 | + // sanitize TKT_ID |
|
| 477 | + $TKT_ID = absint($TKT_ID); |
|
| 478 | + $old_assumption = $this->get_assumption_concerning_values_already_prepared_by_model_object(); |
|
| 479 | + $this->assume_values_already_prepared_by_model_object(EEM_Base::prepared_for_use_in_db); |
|
| 480 | + $query_params = array(array('Ticket.TKT_ID' => $TKT_ID), 'order_by' => array('DTT_EVT_start' => 'asc')); |
|
| 481 | + if (! $include_expired) { |
|
| 482 | + $query_params[0]['DTT_EVT_end'] = array('>=', current_time('mysql', true)); |
|
| 483 | + } |
|
| 484 | + if ($include_deleted) { |
|
| 485 | + $query_params[0]['DTT_deleted'] = array('IN', array(true, false)); |
|
| 486 | + } |
|
| 487 | + if ($limit) { |
|
| 488 | + $query_params['limit'] = $limit; |
|
| 489 | + } |
|
| 490 | + /** @var EE_Datetime[] $result */ |
|
| 491 | + $result = $this->get_all($query_params); |
|
| 492 | + $this->assume_values_already_prepared_by_model_object($old_assumption); |
|
| 493 | + return $result; |
|
| 494 | + } |
|
| 495 | + |
|
| 496 | + |
|
| 497 | + /** |
|
| 498 | + * Gets all the datetimes for a ticket (including trashed ones, for now), ordered by the DTT_order for the |
|
| 499 | + * datetimes. |
|
| 500 | + * |
|
| 501 | + * @param int $TKT_ID ID of ticket to retrieve the datetimes for |
|
| 502 | + * @param boolean $include_expired whether to include expired datetimes or not |
|
| 503 | + * @param boolean $include_deleted whether to include trashed datetimes or not. |
|
| 504 | + * @param int|null $limit if null, no limit, if int then limit results by |
|
| 505 | + * that number |
|
| 506 | + * @return EE_Datetime[] |
|
| 507 | + * @throws EE_Error |
|
| 508 | + */ |
|
| 509 | + public function get_datetimes_for_ticket_ordered_by_DTT_order( |
|
| 510 | + $TKT_ID, |
|
| 511 | + $include_expired = true, |
|
| 512 | + $include_deleted = true, |
|
| 513 | + $limit = null |
|
| 514 | + ) { |
|
| 515 | + // sanitize id. |
|
| 516 | + $TKT_ID = absint($TKT_ID); |
|
| 517 | + $old_assumption = $this->get_assumption_concerning_values_already_prepared_by_model_object(); |
|
| 518 | + $this->assume_values_already_prepared_by_model_object(EEM_Base::prepared_for_use_in_db); |
|
| 519 | + $where_params = array('Ticket.TKT_ID' => $TKT_ID); |
|
| 520 | + $query_params = array($where_params, 'order_by' => array('DTT_order' => 'ASC')); |
|
| 521 | + if (! $include_expired) { |
|
| 522 | + $query_params[0]['DTT_EVT_end'] = array('>=', current_time('mysql', true)); |
|
| 523 | + } |
|
| 524 | + if ($include_deleted) { |
|
| 525 | + $query_params[0]['DTT_deleted'] = array('IN', array(true, false)); |
|
| 526 | + } |
|
| 527 | + if ($limit) { |
|
| 528 | + $query_params['limit'] = $limit; |
|
| 529 | + } |
|
| 530 | + /** @var EE_Datetime[] $result */ |
|
| 531 | + $result = $this->get_all($query_params); |
|
| 532 | + $this->assume_values_already_prepared_by_model_object($old_assumption); |
|
| 533 | + return $result; |
|
| 534 | + } |
|
| 535 | + |
|
| 536 | + |
|
| 537 | + /** |
|
| 538 | + * Gets the most important datetime for a particular event (ie, the primary event usually. But if for some WACK |
|
| 539 | + * reason it doesn't exist, we consider the earliest event the most important) |
|
| 540 | + * |
|
| 541 | + * @param int $EVT_ID |
|
| 542 | + * @return EE_Datetime |
|
| 543 | + * @throws EE_Error |
|
| 544 | + */ |
|
| 545 | + public function get_most_important_datetime_for_event($EVT_ID) |
|
| 546 | + { |
|
| 547 | + $results = $this->get_datetimes_for_event_ordered_by_importance($EVT_ID, 1); |
|
| 548 | + if ($results) { |
|
| 549 | + return array_shift($results); |
|
| 550 | + } |
|
| 551 | + return null; |
|
| 552 | + } |
|
| 553 | + |
|
| 554 | + |
|
| 555 | + /** |
|
| 556 | + * This returns a wpdb->results Array of all DTT month and years matching the incoming query params and |
|
| 557 | + * grouped by month and year. |
|
| 558 | + * |
|
| 559 | + * @param array $where_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
| 560 | + * @param string $evt_active_status A string representing the evt active status to filter the months by. |
|
| 561 | + * Can be: |
|
| 562 | + * - '' = no filter |
|
| 563 | + * - upcoming = Published events with at least one upcoming datetime. |
|
| 564 | + * - expired = Events with all datetimes expired. |
|
| 565 | + * - active = Events that are published and have at least one datetime that |
|
| 566 | + * starts before now and ends after now. |
|
| 567 | + * - inactive = Events that are either not published. |
|
| 568 | + * @return EE_Base_Class[] |
|
| 569 | + * @throws EE_Error |
|
| 570 | + * @throws InvalidArgumentException |
|
| 571 | + * @throws InvalidArgumentException |
|
| 572 | + */ |
|
| 573 | + public function get_dtt_months_and_years($where_params, $evt_active_status = '') |
|
| 574 | + { |
|
| 575 | + $current_time_for_DTT_EVT_start = $this->current_time_for_query('DTT_EVT_start'); |
|
| 576 | + $current_time_for_DTT_EVT_end = $this->current_time_for_query('DTT_EVT_end'); |
|
| 577 | + switch ($evt_active_status) { |
|
| 578 | + case 'upcoming': |
|
| 579 | + $where_params['Event.status'] = 'publish'; |
|
| 580 | + // if there are already query_params matching DTT_EVT_start then we need to modify that to add them. |
|
| 581 | + if (isset($where_params['DTT_EVT_start'])) { |
|
| 582 | + $where_params['DTT_EVT_start*****'] = $where_params['DTT_EVT_start']; |
|
| 583 | + } |
|
| 584 | + $where_params['DTT_EVT_start'] = array('>', $current_time_for_DTT_EVT_start); |
|
| 585 | + break; |
|
| 586 | + case 'expired': |
|
| 587 | + if (isset($where_params['Event.status'])) { |
|
| 588 | + unset($where_params['Event.status']); |
|
| 589 | + } |
|
| 590 | + // get events to exclude |
|
| 591 | + $exclude_query[0] = array_merge( |
|
| 592 | + $where_params, |
|
| 593 | + array('DTT_EVT_end' => array('>', $current_time_for_DTT_EVT_end)) |
|
| 594 | + ); |
|
| 595 | + // first get all events that have datetimes where its not expired. |
|
| 596 | + $event_ids = $this->_get_all_wpdb_results( |
|
| 597 | + $exclude_query, |
|
| 598 | + OBJECT_K, |
|
| 599 | + 'Datetime.EVT_ID' |
|
| 600 | + ); |
|
| 601 | + $event_ids = array_keys($event_ids); |
|
| 602 | + if (isset($where_params['DTT_EVT_end'])) { |
|
| 603 | + $where_params['DTT_EVT_end****'] = $where_params['DTT_EVT_end']; |
|
| 604 | + } |
|
| 605 | + $where_params['DTT_EVT_end'] = array('<', $current_time_for_DTT_EVT_end); |
|
| 606 | + $where_params['Event.EVT_ID'] = array('NOT IN', $event_ids); |
|
| 607 | + break; |
|
| 608 | + case 'active': |
|
| 609 | + $where_params['Event.status'] = 'publish'; |
|
| 610 | + if (isset($where_params['DTT_EVT_start'])) { |
|
| 611 | + $where_params['Datetime.DTT_EVT_start******'] = $where_params['DTT_EVT_start']; |
|
| 612 | + } |
|
| 613 | + if (isset($where_params['Datetime.DTT_EVT_end'])) { |
|
| 614 | + $where_params['Datetime.DTT_EVT_end*****'] = $where_params['DTT_EVT_end']; |
|
| 615 | + } |
|
| 616 | + $where_params['DTT_EVT_start'] = array('<', $current_time_for_DTT_EVT_start); |
|
| 617 | + $where_params['DTT_EVT_end'] = array('>', $current_time_for_DTT_EVT_end); |
|
| 618 | + break; |
|
| 619 | + case 'inactive': |
|
| 620 | + if (isset($where_params['Event.status'])) { |
|
| 621 | + unset($where_params['Event.status']); |
|
| 622 | + } |
|
| 623 | + if (isset($where_params['OR'])) { |
|
| 624 | + $where_params['AND']['OR'] = $where_params['OR']; |
|
| 625 | + } |
|
| 626 | + if (isset($where_params['DTT_EVT_end'])) { |
|
| 627 | + $where_params['AND']['DTT_EVT_end****'] = $where_params['DTT_EVT_end']; |
|
| 628 | + unset($where_params['DTT_EVT_end']); |
|
| 629 | + } |
|
| 630 | + if (isset($where_params['DTT_EVT_start'])) { |
|
| 631 | + $where_params['AND']['DTT_EVT_start'] = $where_params['DTT_EVT_start']; |
|
| 632 | + unset($where_params['DTT_EVT_start']); |
|
| 633 | + } |
|
| 634 | + $where_params['AND']['Event.status'] = array('!=', 'publish'); |
|
| 635 | + break; |
|
| 636 | + } |
|
| 637 | + $query_params[0] = $where_params; |
|
| 638 | + $query_params['group_by'] = array('dtt_year', 'dtt_month'); |
|
| 639 | + $query_params['order_by'] = array('DTT_EVT_start' => 'DESC'); |
|
| 640 | + $query_interval = EEH_DTT_Helper::get_sql_query_interval_for_offset( |
|
| 641 | + $this->get_timezone(), |
|
| 642 | + 'DTT_EVT_start' |
|
| 643 | + ); |
|
| 644 | + $columns_to_select = array( |
|
| 645 | + 'dtt_year' => array('YEAR(' . $query_interval . ')', '%s'), |
|
| 646 | + 'dtt_month' => array('MONTHNAME(' . $query_interval . ')', '%s'), |
|
| 647 | + 'dtt_month_num' => array('MONTH(' . $query_interval . ')', '%s'), |
|
| 648 | + ); |
|
| 649 | + return $this->_get_all_wpdb_results($query_params, OBJECT, $columns_to_select); |
|
| 650 | + } |
|
| 651 | + |
|
| 652 | + |
|
| 653 | + /** |
|
| 654 | + * Updates the DTT_sold attribute on each datetime (based on the registrations |
|
| 655 | + * for the tickets for each datetime) |
|
| 656 | + * |
|
| 657 | + * @param EE_Base_Class[]|EE_Datetime[] $datetimes |
|
| 658 | + * @throws EE_Error |
|
| 659 | + */ |
|
| 660 | + public function update_sold($datetimes) |
|
| 661 | + { |
|
| 662 | + EE_Error::doing_it_wrong( |
|
| 663 | + __FUNCTION__, |
|
| 664 | + esc_html__( |
|
| 665 | + 'Please use \EEM_Ticket::update_tickets_sold() instead which will in turn correctly update both the Ticket AND Datetime counts.', |
|
| 666 | + 'event_espresso' |
|
| 667 | + ), |
|
| 668 | + '4.9.32.rc.005' |
|
| 669 | + ); |
|
| 670 | + foreach ($datetimes as $datetime) { |
|
| 671 | + $datetime->update_sold(); |
|
| 672 | + } |
|
| 673 | + } |
|
| 674 | + |
|
| 675 | + |
|
| 676 | + /** |
|
| 677 | + * Gets the total number of tickets available at a particular datetime |
|
| 678 | + * (does NOT take into account the datetime's spaces available) |
|
| 679 | + * |
|
| 680 | + * @param int $DTT_ID |
|
| 681 | + * @param array $query_params |
|
| 682 | + * @return int of tickets available. If sold out, return less than 1. If infinite, returns EE_INF, IF there are NO |
|
| 683 | + * tickets attached to datetime then FALSE is returned. |
|
| 684 | + */ |
|
| 685 | + public function sum_tickets_currently_available_at_datetime($DTT_ID, array $query_params = array()) |
|
| 686 | + { |
|
| 687 | + $datetime = $this->get_one_by_ID($DTT_ID); |
|
| 688 | + if ($datetime instanceof EE_Datetime) { |
|
| 689 | + return $datetime->tickets_remaining($query_params); |
|
| 690 | + } |
|
| 691 | + return 0; |
|
| 692 | + } |
|
| 693 | + |
|
| 694 | + |
|
| 695 | + /** |
|
| 696 | + * This returns an array of counts of datetimes in the database for each Datetime status that can be queried. |
|
| 697 | + * |
|
| 698 | + * @param array $stati_to_include If included you can restrict the statuses we return counts for by including the |
|
| 699 | + * stati you want counts for as values in the array. An empty array returns counts |
|
| 700 | + * for all valid stati. |
|
| 701 | + * @param array $query_params If included can be used to refine the conditions for returning the count (i.e. |
|
| 702 | + * only for Datetimes connected to a specific event, or specific ticket. |
|
| 703 | + * @return array The value returned is an array indexed by Datetime Status and the values are the counts. The |
|
| 704 | + * @throws EE_Error |
|
| 705 | + * stati used as index keys are: EE_Datetime::active EE_Datetime::upcoming |
|
| 706 | + * EE_Datetime::expired |
|
| 707 | + */ |
|
| 708 | + public function get_datetime_counts_by_status(array $stati_to_include = array(), array $query_params = array()) |
|
| 709 | + { |
|
| 710 | + // only accept where conditions for this query. |
|
| 711 | + $_where = isset($query_params[0]) ? $query_params[0] : array(); |
|
| 712 | + $status_query_args = array( |
|
| 713 | + EE_Datetime::active => array_merge( |
|
| 714 | + $_where, |
|
| 715 | + array('DTT_EVT_start' => array('<', time()), 'DTT_EVT_end' => array('>', time())) |
|
| 716 | + ), |
|
| 717 | + EE_Datetime::upcoming => array_merge( |
|
| 718 | + $_where, |
|
| 719 | + array('DTT_EVT_start' => array('>', time())) |
|
| 720 | + ), |
|
| 721 | + EE_Datetime::expired => array_merge( |
|
| 722 | + $_where, |
|
| 723 | + array('DTT_EVT_end' => array('<', time())) |
|
| 724 | + ), |
|
| 725 | + ); |
|
| 726 | + if (! empty($stati_to_include)) { |
|
| 727 | + foreach (array_keys($status_query_args) as $status) { |
|
| 728 | + if (! in_array($status, $stati_to_include, true)) { |
|
| 729 | + unset($status_query_args[ $status ]); |
|
| 730 | + } |
|
| 731 | + } |
|
| 732 | + } |
|
| 733 | + // loop through and query counts for each stati. |
|
| 734 | + $status_query_results = array(); |
|
| 735 | + foreach ($status_query_args as $status => $status_where_conditions) { |
|
| 736 | + $status_query_results[ $status ] = EEM_Datetime::count( |
|
| 737 | + array($status_where_conditions), |
|
| 738 | + 'DTT_ID', |
|
| 739 | + true |
|
| 740 | + ); |
|
| 741 | + } |
|
| 742 | + return $status_query_results; |
|
| 743 | + } |
|
| 744 | + |
|
| 745 | + |
|
| 746 | + /** |
|
| 747 | + * Returns the specific count for a given Datetime status matching any given query_params. |
|
| 748 | + * |
|
| 749 | + * @param string $status Valid string representation for Datetime status requested. (Defaults to Active). |
|
| 750 | + * @param array $query_params |
|
| 751 | + * @return int |
|
| 752 | + * @throws EE_Error |
|
| 753 | + */ |
|
| 754 | + public function get_datetime_count_for_status($status = EE_Datetime::active, array $query_params = array()) |
|
| 755 | + { |
|
| 756 | + $count = $this->get_datetime_counts_by_status(array($status), $query_params); |
|
| 757 | + return ! empty($count[ $status ]) ? $count[ $status ] : 0; |
|
| 758 | + } |
|
| 759 | 759 | } |
@@ -121,7 +121,7 @@ discard block |
||
| 121 | 121 | ), |
| 122 | 122 | ), |
| 123 | 123 | ); |
| 124 | - $this->_model_relations = array( |
|
| 124 | + $this->_model_relations = array( |
|
| 125 | 125 | 'Ticket' => new EE_HABTM_Relation('Datetime_Ticket'), |
| 126 | 126 | 'Event' => new EE_Belongs_To_Relation(), |
| 127 | 127 | 'Checkin' => new EE_Has_Many_Relation(), |
@@ -131,16 +131,16 @@ discard block |
||
| 131 | 131 | $this->model_chain_to_password = $path_to_event_model; |
| 132 | 132 | $this->_model_chain_to_wp_user = $path_to_event_model; |
| 133 | 133 | // this model is generally available for reading |
| 134 | - $this->_cap_restriction_generators[ EEM_Base::caps_read ] = new EE_Restriction_Generator_Event_Related_Public( |
|
| 134 | + $this->_cap_restriction_generators[EEM_Base::caps_read] = new EE_Restriction_Generator_Event_Related_Public( |
|
| 135 | 135 | $path_to_event_model |
| 136 | 136 | ); |
| 137 | - $this->_cap_restriction_generators[ EEM_Base::caps_read_admin ] = new EE_Restriction_Generator_Event_Related_Protected( |
|
| 137 | + $this->_cap_restriction_generators[EEM_Base::caps_read_admin] = new EE_Restriction_Generator_Event_Related_Protected( |
|
| 138 | 138 | $path_to_event_model |
| 139 | 139 | ); |
| 140 | - $this->_cap_restriction_generators[ EEM_Base::caps_edit ] = new EE_Restriction_Generator_Event_Related_Protected( |
|
| 140 | + $this->_cap_restriction_generators[EEM_Base::caps_edit] = new EE_Restriction_Generator_Event_Related_Protected( |
|
| 141 | 141 | $path_to_event_model |
| 142 | 142 | ); |
| 143 | - $this->_cap_restriction_generators[ EEM_Base::caps_delete ] = new EE_Restriction_Generator_Event_Related_Protected( |
|
| 143 | + $this->_cap_restriction_generators[EEM_Base::caps_delete] = new EE_Restriction_Generator_Event_Related_Protected( |
|
| 144 | 144 | $path_to_event_model, |
| 145 | 145 | EEM_Base::caps_edit |
| 146 | 146 | ); |
@@ -246,10 +246,10 @@ discard block |
||
| 246 | 246 | */ |
| 247 | 247 | private function validateStartAndEndTimeForBlankDate($start_time, $end_time) |
| 248 | 248 | { |
| 249 | - if (! is_array($start_time)) { |
|
| 249 | + if ( ! is_array($start_time)) { |
|
| 250 | 250 | throw new InvalidDataTypeException('start_time', $start_time, 'array'); |
| 251 | 251 | } |
| 252 | - if (! is_array($end_time)) { |
|
| 252 | + if ( ! is_array($end_time)) { |
|
| 253 | 253 | throw new InvalidDataTypeException('end_time', $end_time, 'array'); |
| 254 | 254 | } |
| 255 | 255 | if (count($start_time) !== 2) { |
@@ -283,7 +283,7 @@ discard block |
||
| 283 | 283 | */ |
| 284 | 284 | public function get_all_event_dates($EVT_ID = 0) |
| 285 | 285 | { |
| 286 | - if (! $EVT_ID) { // on add_new_event event_id gets set to 0 |
|
| 286 | + if ( ! $EVT_ID) { // on add_new_event event_id gets set to 0 |
|
| 287 | 287 | return $this->create_new_blank_datetime(); |
| 288 | 288 | } |
| 289 | 289 | $results = $this->get_datetimes_for_event_ordered_by_DTT_order($EVT_ID); |
@@ -329,7 +329,7 @@ discard block |
||
| 329 | 329 | 'order_by' => array('DTT_order' => 'ASC'), |
| 330 | 330 | 'default_where_conditions' => 'none', |
| 331 | 331 | ); |
| 332 | - if (! $include_expired) { |
|
| 332 | + if ( ! $include_expired) { |
|
| 333 | 333 | $query_params[0]['DTT_EVT_end'] = array('>=', current_time('mysql', true)); |
| 334 | 334 | } |
| 335 | 335 | if ($include_deleted) { |
@@ -440,7 +440,7 @@ discard block |
||
| 440 | 440 | $old_assumption = $this->get_assumption_concerning_values_already_prepared_by_model_object(); |
| 441 | 441 | $this->assume_values_already_prepared_by_model_object(EEM_Base::prepared_for_use_in_db); |
| 442 | 442 | $query_params = array(array('Event.EVT_ID' => $EVT_ID), 'order_by' => array('DTT_EVT_start' => 'asc')); |
| 443 | - if (! $include_expired) { |
|
| 443 | + if ( ! $include_expired) { |
|
| 444 | 444 | $query_params[0]['DTT_EVT_end'] = array('>=', current_time('mysql', true)); |
| 445 | 445 | } |
| 446 | 446 | if ($include_deleted) { |
@@ -478,7 +478,7 @@ discard block |
||
| 478 | 478 | $old_assumption = $this->get_assumption_concerning_values_already_prepared_by_model_object(); |
| 479 | 479 | $this->assume_values_already_prepared_by_model_object(EEM_Base::prepared_for_use_in_db); |
| 480 | 480 | $query_params = array(array('Ticket.TKT_ID' => $TKT_ID), 'order_by' => array('DTT_EVT_start' => 'asc')); |
| 481 | - if (! $include_expired) { |
|
| 481 | + if ( ! $include_expired) { |
|
| 482 | 482 | $query_params[0]['DTT_EVT_end'] = array('>=', current_time('mysql', true)); |
| 483 | 483 | } |
| 484 | 484 | if ($include_deleted) { |
@@ -518,7 +518,7 @@ discard block |
||
| 518 | 518 | $this->assume_values_already_prepared_by_model_object(EEM_Base::prepared_for_use_in_db); |
| 519 | 519 | $where_params = array('Ticket.TKT_ID' => $TKT_ID); |
| 520 | 520 | $query_params = array($where_params, 'order_by' => array('DTT_order' => 'ASC')); |
| 521 | - if (! $include_expired) { |
|
| 521 | + if ( ! $include_expired) { |
|
| 522 | 522 | $query_params[0]['DTT_EVT_end'] = array('>=', current_time('mysql', true)); |
| 523 | 523 | } |
| 524 | 524 | if ($include_deleted) { |
@@ -641,10 +641,10 @@ discard block |
||
| 641 | 641 | $this->get_timezone(), |
| 642 | 642 | 'DTT_EVT_start' |
| 643 | 643 | ); |
| 644 | - $columns_to_select = array( |
|
| 645 | - 'dtt_year' => array('YEAR(' . $query_interval . ')', '%s'), |
|
| 646 | - 'dtt_month' => array('MONTHNAME(' . $query_interval . ')', '%s'), |
|
| 647 | - 'dtt_month_num' => array('MONTH(' . $query_interval . ')', '%s'), |
|
| 644 | + $columns_to_select = array( |
|
| 645 | + 'dtt_year' => array('YEAR('.$query_interval.')', '%s'), |
|
| 646 | + 'dtt_month' => array('MONTHNAME('.$query_interval.')', '%s'), |
|
| 647 | + 'dtt_month_num' => array('MONTH('.$query_interval.')', '%s'), |
|
| 648 | 648 | ); |
| 649 | 649 | return $this->_get_all_wpdb_results($query_params, OBJECT, $columns_to_select); |
| 650 | 650 | } |
@@ -723,17 +723,17 @@ discard block |
||
| 723 | 723 | array('DTT_EVT_end' => array('<', time())) |
| 724 | 724 | ), |
| 725 | 725 | ); |
| 726 | - if (! empty($stati_to_include)) { |
|
| 726 | + if ( ! empty($stati_to_include)) { |
|
| 727 | 727 | foreach (array_keys($status_query_args) as $status) { |
| 728 | - if (! in_array($status, $stati_to_include, true)) { |
|
| 729 | - unset($status_query_args[ $status ]); |
|
| 728 | + if ( ! in_array($status, $stati_to_include, true)) { |
|
| 729 | + unset($status_query_args[$status]); |
|
| 730 | 730 | } |
| 731 | 731 | } |
| 732 | 732 | } |
| 733 | 733 | // loop through and query counts for each stati. |
| 734 | 734 | $status_query_results = array(); |
| 735 | 735 | foreach ($status_query_args as $status => $status_where_conditions) { |
| 736 | - $status_query_results[ $status ] = EEM_Datetime::count( |
|
| 736 | + $status_query_results[$status] = EEM_Datetime::count( |
|
| 737 | 737 | array($status_where_conditions), |
| 738 | 738 | 'DTT_ID', |
| 739 | 739 | true |
@@ -754,6 +754,6 @@ discard block |
||
| 754 | 754 | public function get_datetime_count_for_status($status = EE_Datetime::active, array $query_params = array()) |
| 755 | 755 | { |
| 756 | 756 | $count = $this->get_datetime_counts_by_status(array($status), $query_params); |
| 757 | - return ! empty($count[ $status ]) ? $count[ $status ] : 0; |
|
| 757 | + return ! empty($count[$status]) ? $count[$status] : 0; |
|
| 758 | 758 | } |
| 759 | 759 | } |
@@ -16,89 +16,89 @@ |
||
| 16 | 16 | abstract class EE_Data_Migration_Script_Stage_Table extends EE_Data_Migration_Script_Stage |
| 17 | 17 | { |
| 18 | 18 | |
| 19 | - protected $_old_table; |
|
| 19 | + protected $_old_table; |
|
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * @var string The columns to select. May be overridden for children. |
|
| 23 | - */ |
|
| 24 | - protected $select_expression = '*'; |
|
| 21 | + /** |
|
| 22 | + * @var string The columns to select. May be overridden for children. |
|
| 23 | + */ |
|
| 24 | + protected $select_expression = '*'; |
|
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * Set in the constructor to add this sql to both the counting query in |
|
| 28 | - * EE_Data_Migration_Script_Stage_Table::_count_records_to_migrate() and |
|
| 29 | - * EE_Data_Migration_Script_Stage_Table::_get_rows(). |
|
| 30 | - * Eg "where column_name like '%some_value%'" |
|
| 31 | - * |
|
| 32 | - * @var string |
|
| 33 | - */ |
|
| 34 | - protected $_extra_where_sql; |
|
| 26 | + /** |
|
| 27 | + * Set in the constructor to add this sql to both the counting query in |
|
| 28 | + * EE_Data_Migration_Script_Stage_Table::_count_records_to_migrate() and |
|
| 29 | + * EE_Data_Migration_Script_Stage_Table::_get_rows(). |
|
| 30 | + * Eg "where column_name like '%some_value%'" |
|
| 31 | + * |
|
| 32 | + * @var string |
|
| 33 | + */ |
|
| 34 | + protected $_extra_where_sql; |
|
| 35 | 35 | |
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * IMPORTANT: if an error is encountered, or everything is finished, this stage should update its status property |
|
| 39 | - * accordingly. Note: it should not alter the count of items migrated. That is done in the public function that |
|
| 40 | - * calls this. IMPORTANT: The count of items migrated should ONLY be less than $num_items_to_migrate when it's the |
|
| 41 | - * last migration step, otherwise it should always return $num_items_to_migrate. (Eg, if we're migrating attendees |
|
| 42 | - * rows from the database, and $num_items_to_migrate is set to 50, then we SHOULD actually migrate 50 rows,but at |
|
| 43 | - * very least we MUST report/return 50 items migrated) |
|
| 44 | - * |
|
| 45 | - * @param int $num_items |
|
| 46 | - * @return int number of items ACTUALLY migrated |
|
| 47 | - */ |
|
| 48 | - public function _migration_step($num_items = 50) |
|
| 49 | - { |
|
| 50 | - $rows = $this->_get_rows($num_items); |
|
| 51 | - $items_actually_migrated = 0; |
|
| 52 | - foreach ($rows as $old_row) { |
|
| 53 | - $this->_migrate_old_row($old_row); |
|
| 54 | - $items_actually_migrated++; |
|
| 55 | - } |
|
| 56 | - if ($this->count_records_migrated() + $items_actually_migrated >= $this->count_records_to_migrate()) { |
|
| 57 | - $this->set_completed(); |
|
| 58 | - } |
|
| 59 | - return $items_actually_migrated; |
|
| 60 | - } |
|
| 37 | + /** |
|
| 38 | + * IMPORTANT: if an error is encountered, or everything is finished, this stage should update its status property |
|
| 39 | + * accordingly. Note: it should not alter the count of items migrated. That is done in the public function that |
|
| 40 | + * calls this. IMPORTANT: The count of items migrated should ONLY be less than $num_items_to_migrate when it's the |
|
| 41 | + * last migration step, otherwise it should always return $num_items_to_migrate. (Eg, if we're migrating attendees |
|
| 42 | + * rows from the database, and $num_items_to_migrate is set to 50, then we SHOULD actually migrate 50 rows,but at |
|
| 43 | + * very least we MUST report/return 50 items migrated) |
|
| 44 | + * |
|
| 45 | + * @param int $num_items |
|
| 46 | + * @return int number of items ACTUALLY migrated |
|
| 47 | + */ |
|
| 48 | + public function _migration_step($num_items = 50) |
|
| 49 | + { |
|
| 50 | + $rows = $this->_get_rows($num_items); |
|
| 51 | + $items_actually_migrated = 0; |
|
| 52 | + foreach ($rows as $old_row) { |
|
| 53 | + $this->_migrate_old_row($old_row); |
|
| 54 | + $items_actually_migrated++; |
|
| 55 | + } |
|
| 56 | + if ($this->count_records_migrated() + $items_actually_migrated >= $this->count_records_to_migrate()) { |
|
| 57 | + $this->set_completed(); |
|
| 58 | + } |
|
| 59 | + return $items_actually_migrated; |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - /** |
|
| 63 | - * Gets the rows for each migration stage from the old table |
|
| 64 | - * |
|
| 65 | - * @global wpdb $wpdb |
|
| 66 | - * @param int $limit |
|
| 67 | - * @return array of arrays like $wpdb->get_results($sql, ARRAY_A) |
|
| 68 | - */ |
|
| 69 | - protected function _get_rows($limit) |
|
| 70 | - { |
|
| 71 | - global $wpdb; |
|
| 72 | - $start_at_record = $this->count_records_migrated(); |
|
| 73 | - $query = "SELECT {$this->select_expression} FROM {$this->_old_table} {$this->_extra_where_sql} " . $wpdb->prepare( |
|
| 74 | - "LIMIT %d, %d", |
|
| 75 | - $start_at_record, |
|
| 76 | - $limit |
|
| 77 | - ); |
|
| 78 | - return $wpdb->get_results($query, ARRAY_A); |
|
| 79 | - } |
|
| 62 | + /** |
|
| 63 | + * Gets the rows for each migration stage from the old table |
|
| 64 | + * |
|
| 65 | + * @global wpdb $wpdb |
|
| 66 | + * @param int $limit |
|
| 67 | + * @return array of arrays like $wpdb->get_results($sql, ARRAY_A) |
|
| 68 | + */ |
|
| 69 | + protected function _get_rows($limit) |
|
| 70 | + { |
|
| 71 | + global $wpdb; |
|
| 72 | + $start_at_record = $this->count_records_migrated(); |
|
| 73 | + $query = "SELECT {$this->select_expression} FROM {$this->_old_table} {$this->_extra_where_sql} " . $wpdb->prepare( |
|
| 74 | + "LIMIT %d, %d", |
|
| 75 | + $start_at_record, |
|
| 76 | + $limit |
|
| 77 | + ); |
|
| 78 | + return $wpdb->get_results($query, ARRAY_A); |
|
| 79 | + } |
|
| 80 | 80 | |
| 81 | 81 | |
| 82 | - /** |
|
| 83 | - * Counts the records to migrate; the public version may cache it |
|
| 84 | - * |
|
| 85 | - * @return int |
|
| 86 | - */ |
|
| 87 | - public function _count_records_to_migrate() |
|
| 88 | - { |
|
| 89 | - global $wpdb; |
|
| 90 | - $query = "SELECT COUNT(*) FROM {$this->_old_table} {$this->_extra_where_sql}"; |
|
| 91 | - $count = $wpdb->get_var($query); |
|
| 92 | - return $count; |
|
| 93 | - } |
|
| 82 | + /** |
|
| 83 | + * Counts the records to migrate; the public version may cache it |
|
| 84 | + * |
|
| 85 | + * @return int |
|
| 86 | + */ |
|
| 87 | + public function _count_records_to_migrate() |
|
| 88 | + { |
|
| 89 | + global $wpdb; |
|
| 90 | + $query = "SELECT COUNT(*) FROM {$this->_old_table} {$this->_extra_where_sql}"; |
|
| 91 | + $count = $wpdb->get_var($query); |
|
| 92 | + return $count; |
|
| 93 | + } |
|
| 94 | 94 | |
| 95 | - /** |
|
| 96 | - * takes care of migrating this particular row from the OLD table to whatever its |
|
| 97 | - * representation is in the new database. If there are errors, use $this->add_error to log them. If there is a |
|
| 98 | - * fatal error which prevents all future migrations, throw an exception describing it |
|
| 99 | - * |
|
| 100 | - * @param array $old_row an associative array where keys are column names and values are their values. |
|
| 101 | - * @return null |
|
| 102 | - */ |
|
| 103 | - abstract protected function _migrate_old_row($old_row); |
|
| 95 | + /** |
|
| 96 | + * takes care of migrating this particular row from the OLD table to whatever its |
|
| 97 | + * representation is in the new database. If there are errors, use $this->add_error to log them. If there is a |
|
| 98 | + * fatal error which prevents all future migrations, throw an exception describing it |
|
| 99 | + * |
|
| 100 | + * @param array $old_row an associative array where keys are column names and values are their values. |
|
| 101 | + * @return null |
|
| 102 | + */ |
|
| 103 | + abstract protected function _migrate_old_row($old_row); |
|
| 104 | 104 | } |
@@ -70,7 +70,7 @@ |
||
| 70 | 70 | { |
| 71 | 71 | global $wpdb; |
| 72 | 72 | $start_at_record = $this->count_records_migrated(); |
| 73 | - $query = "SELECT {$this->select_expression} FROM {$this->_old_table} {$this->_extra_where_sql} " . $wpdb->prepare( |
|
| 73 | + $query = "SELECT {$this->select_expression} FROM {$this->_old_table} {$this->_extra_where_sql} ".$wpdb->prepare( |
|
| 74 | 74 | "LIMIT %d, %d", |
| 75 | 75 | $start_at_record, |
| 76 | 76 | $limit |