1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* Class {name} |
5
|
|
|
* |
6
|
|
|
* Description here |
7
|
|
|
* |
8
|
|
|
* @package Event Espresso |
9
|
|
|
* @subpackage |
10
|
|
|
* @author Mike Nelson |
11
|
|
|
* @since 4.8.26 |
12
|
|
|
* |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace EventEspressoBatchRequest\JobHandlers; |
16
|
|
|
use EventEspressoBatchRequest\JobHandlerBaseClasses\JobHandlerFile; |
17
|
|
|
use EventEspressoBatchRequest\Helpers\BatchRequestException; |
18
|
|
|
use EventEspressoBatchRequest\Helpers\JobParameters; |
19
|
|
|
use EventEspressoBatchRequest\Helpers\JobStepResponse; |
20
|
|
|
|
21
|
|
|
if (!defined('EVENT_ESPRESSO_VERSION')) { |
22
|
|
|
exit('No direct script access allowed'); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
class AttendeesReport extends JobHandlerFile { |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
public function create_job(JobParameters $job_parameters) { |
30
|
|
|
if( ! \EE_Capabilities::instance()->current_user_can( 'ee_read_contacts', 'generating_report' ) ) { |
31
|
|
|
throw new BatchRequestException( |
32
|
|
|
__( 'You do not have permission to view contacts', 'event_espresso') |
33
|
|
|
); |
34
|
|
|
} |
35
|
|
|
$filepath = $this->create_file_from_job_with_name( |
36
|
|
|
$job_parameters->job_id(), |
37
|
|
|
__('contact-list-report.csv', 'event_espresso') |
38
|
|
|
); |
39
|
|
|
$job_parameters->add_extra_data( 'filepath', $filepath ); |
40
|
|
|
$job_parameters->set_job_size( $this->count_units_to_process() ); |
41
|
|
|
//we should also set the header columns |
42
|
|
|
$csv_data_for_row = $this->get_csv_data( 0, 1 ); |
43
|
|
|
\EE_Registry::instance()->load_helper( 'Export' ); |
44
|
|
|
\EEH_Export::write_data_array_to_csv( $filepath, $csv_data_for_row, true ); |
45
|
|
|
//if we actually processed a row there, record it |
46
|
|
|
if( $job_parameters->job_size() ) { |
47
|
|
|
$job_parameters->mark_processed( 1 ); |
48
|
|
|
} |
49
|
|
|
return new JobStepResponse( |
50
|
|
|
$job_parameters, |
51
|
|
|
__( 'Contacts report started successfully...', 'event_espresso' ) |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
|
56
|
|
View Code Duplication |
public function continue_job(JobParameters $job_parameters, $batch_size = 50) { |
|
|
|
|
57
|
|
|
$csv_data = $this->get_csv_data( $job_parameters->units_processed(), $batch_size ); |
58
|
|
|
\EE_Registry::instance()->load_helper( 'Export' ); |
59
|
|
|
\EEH_Export::write_data_array_to_csv( $job_parameters->extra_datum( 'filepath' ), $csv_data, false ); |
|
|
|
|
60
|
|
|
$units_processed = count( $csv_data ); |
61
|
|
|
$job_parameters->mark_processed( $units_processed ); |
62
|
|
|
$extra_response_data = array( |
63
|
|
|
'file_url' => '' |
64
|
|
|
); |
65
|
|
|
if( $units_processed < $batch_size ) { |
66
|
|
|
$job_parameters->set_status( JobParameters::status_complete ); |
67
|
|
|
$extra_response_data[ 'file_url' ] = $this->get_url_to_file( $job_parameters->extra_datum( 'filepath' ) ); |
|
|
|
|
68
|
|
|
} |
69
|
|
|
return new JobStepResponse( |
70
|
|
|
$job_parameters, |
71
|
|
|
sprintf( |
72
|
|
|
__( 'Wrote %1$s rows to report CSV file...', 'event_espresso' ), |
73
|
|
|
count( $csv_data ) ), |
74
|
|
|
$extra_response_data ); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
|
78
|
|
View Code Duplication |
public function cleanup_job(JobParameters $job_parameters) { |
|
|
|
|
79
|
|
|
$this->_file_helper->delete( |
80
|
|
|
\EEH_File::remove_filename_from_filepath( $job_parameters->extra_datum( 'filepath' ) ), |
|
|
|
|
81
|
|
|
true, |
82
|
|
|
'd' |
83
|
|
|
); |
84
|
|
|
return new JobStepResponse( $job_parameters, __( 'Cleaned up temporary file', 'event_espresso' ) ); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function count_units_to_process() { |
88
|
|
|
return \EEM_Attendee::instance()->count(); |
89
|
|
|
} |
90
|
|
|
public function get_csv_data( $offset, $limit ) { |
91
|
|
|
$attendee_rows = \EEM_Attendee::instance()->get_all_wpdb_results( |
92
|
|
|
array( |
93
|
|
|
'limit' => array( $offset, $limit ), |
94
|
|
|
'force_join' => array( 'State', 'Country' ) ) ); |
95
|
|
|
$csv_data = array(); |
96
|
|
View Code Duplication |
foreach( $attendee_rows as $attendee_row ){ |
|
|
|
|
97
|
|
|
$csv_row = array(); |
98
|
|
|
foreach( \EEM_Attendee::instance()->field_settings() as $field_name => $field_obj ){ |
99
|
|
|
if( $field_name == 'STA_ID' ){ |
100
|
|
|
$state_name_field = \EEM_State::instance()->field_settings_for( 'STA_name' ); |
101
|
|
|
$csv_row[ __( 'State', 'event_espresso' ) ] = $attendee_row[ $state_name_field->get_qualified_column() ]; |
102
|
|
|
}elseif( $field_name == 'CNT_ISO' ){ |
103
|
|
|
$country_name_field = \EEM_Country::instance()->field_settings_for( 'CNT_name' ); |
104
|
|
|
$csv_row[ __( 'Country', 'event_espresso' ) ] = $attendee_row[ $country_name_field->get_qualified_column() ]; |
105
|
|
|
}else{ |
106
|
|
|
$csv_row[ $field_obj->get_nicename() ] = $attendee_row[ $field_obj->get_qualified_column() ]; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
$csv_data[] = $csv_row; |
110
|
|
|
} |
111
|
|
|
return $csv_data; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
} |
115
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.