class-gen-eu-registration-dashboard.php 7.17 KB
Newer Older
1
2
3
4
5
6
7
8
9
<?php
/**
 * The file that defines the core plugin class
 *
 * A class definition that includes attributes and functions used across both the
 * public-facing side of the site and the admin area.
 *
 * @since      1.0.0
 *
Rodrigo's avatar
Rodrigo committed
10
11
 * @package    GEN_EU_Registration_Dashboard
 * @subpackage GEN_EU_Registration_Dashboard/includes
12
13
 */

Rodrigo's avatar
Rodrigo committed
14
15
use GEN_EU\Registration\Dashboard\GEN_EU_Form_Parameters;

16
17
18
19
20
21
22
23
24
25
/**
 * The core plugin class.
 *
 * This is used to define internationalization, admin-specific hooks, and
 * public-facing site hooks.
 *
 * Also maintains the unique identifier of this plugin as well as the current
 * version of the plugin.
 *
 * @since      1.0.0
Rodrigo's avatar
Rodrigo committed
26
27
 * @package    GEN_EU_Registration_Dashboard
 * @subpackage GEN_EU_Registration_Dashboard/includes
28
29
 * @author     Rodrigo Espirito Santo Braga <rodrigo@gen-europe.prg>
 */
Rodrigo's avatar
Rodrigo committed
30
class GEN_EU_Registration_Dashboard {
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
	/**
	 * The unique identifier of this plugin.
	 *
	 * @since    1.0.0
	 * @access   protected
	 * @var      string    $plugin_name    The string used to uniquely identify this plugin.
	 */
	protected $plugin_name;
	/**
	 * The current version of the plugin.
	 *
	 * @since    1.0.0
	 * @access   protected
	 * @var      string    $version    The current version of the plugin.
	 */
	protected $version;
Angel Matilla's avatar
Angel Matilla committed
47
48
49
50
51
52
53
54
	/**
	 * The ID of the form to display.
	 *
	 * @since    1.0.2
	 * @access   protected
	 * @var      string    $form_id    The ID of the form to display.
	 */
	protected $form_id;
Rodrigo's avatar
Rodrigo committed
55
56
57
58
59
60
61
62
	/**
	 * List of errors occurred.
	 *
	 * @since    1.0.0
	 * @access   protected
	 * @var      WP_Error    $error_list    List of errors occurred.
	 */
	protected $error_list;
63
64
65
66
67
68
69
70
71
	/**
	 * Define the core functionality of the plugin.
	 *
	 * Set the plugin name and the plugin version that can be used throughout the plugin.
	 * Load the dependencies, and set the hooks for the admin area.
	 *
	 * @since    1.0.0
	 */
	public function __construct() {
Rodrigo's avatar
Rodrigo committed
72
73
		if ( defined( 'GEN_EU_REGISTRATION_DASHBOARD_VERSION' ) ) {
			$this->version = GEN_EU_REGISTRATION_DASHBOARD_VERSION;
74
75
76
		} else {
			$this->version = '1.0.0';
		}
Rodrigo's avatar
Rodrigo committed
77
		$this->plugin_name = 'gen-eu-registration-dashboard';
78
79
80
81
82
83
84
		$this->load_dependencies();
	}
	/**
	 * Load the required dependencies for this plugin.
	 *
	 * Include the following files that make up the plugin:
	 *
Rodrigo's avatar
Rodrigo committed
85
	 * - GEN_EU_Registrant. Model to process data from each registrant.
86
87
88
89
90
91
92
93
94
95
	 *
	 * Rregister the hooks with WordPress.
	 *
	 * @since    1.0.0
	 * @access   private
	 */
	private function load_dependencies() {
		/**
		 * The Registrant Class used to handle information about each registrant.
		 */
Rodrigo's avatar
Rodrigo committed
96
97
98
99
100
101
102
103
104
105
106
		require_once plugin_dir_path( __FILE__ ) . 'registrant/class-gen-eu-registrant.php';
		require_once plugin_dir_path( __FILE__ ) . 'registrant/class-gen-eu-attendee.php';
		require_once plugin_dir_path( __FILE__ ) . 'registrant/class-gen-eu-kid.php';
		require_once plugin_dir_path( __FILE__ ) . 'registrant/class-gen-eu-delegate.php';
		require_once plugin_dir_path( __FILE__ ) . 'class-gen-eu-utility.php';
		require_once plugin_dir_path( __FILE__ ) . 'class-gen-eu-caldera-forms-data-collector.php';
		require_once plugin_dir_path( __FILE__ ) . 'dashboard/class-gen-eu-dashboard-loader.php';
		require_once plugin_dir_path( __FILE__ ) . 'dashboard/class-gen-eu-dashboard-viewer.php';
		require_once plugin_dir_path( __FILE__ ) . 'class-gen-eu-enum.php';
		require_once plugin_dir_path( __FILE__ ) . 'class-gen-eu-registration.php';
		require_once plugin_dir_path( __FILE__ ) . 'class-gen-eu-form-parameters.php';
107
108
109
110

		// Register style sheet.
		add_action( 'admin_enqueue_scripts', array( $this, 'register_styles' ) );
		// Adding the function that creates a submenu to the admin menu.
111
112
		add_action( 'admin_menu', array( $this, 'create_dashboard_menu_link' ) );

113
114
115
116
117
	}
	/**
	 * Include CSS file for the plugin.
	 */
	public function register_styles() {
Rodrigo's avatar
Rodrigo committed
118
		wp_register_style( $this->plugin_name, plugins_url( 'gen-eu-registration-dashboard/assets/gen-eu-registration-dashboard-styles.css' ), null, $this->version );
119
120
121
		wp_enqueue_style( $this->plugin_name );
	}

122
123
124
125
126
	/** Function that creates a submenu to the admin menuif the user is admin, otherwise creates a new menu item*/
	public function create_dashboard_menu_link() {
		if ( current_user_can( 'manage_options' ) ) {
			add_submenu_page(
				'caldera-forms',
Rodrigo's avatar
Rodrigo committed
127
128
				'GEN EU Registration Dashboard',
				'<span class="caldera-forms-menu-dashicon"><span class="dashicons dashicons-analytics"></span>GEN EU Registration Dashboard',
129
130
131
132
133
134
135
136
137
138
139
				'edit_pages',
				$this->plugin_name,
				array( $this, 'create_admin_page' )
			);
		} elseif ( current_user_can( 'read_private_posts' ) ) {
			add_menu_page(
				'GEN Registration Tracker',
				'GEN Registration Tracker',
				'read_private_posts',
				$this->plugin_name,
				array( $this, 'create_admin_page' ),
Angel Matilla's avatar
Angel Matilla committed
140
				'dashicons-analytics'
141
142
143
			);
		}

144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
	}

	/**
	 * The name of the plugin used to uniquely identify it within the context of
	 * WordPress and to define internationalization functionality.
	 *
	 * @since     1.0.0
	 * @return    string    The name of the plugin.
	 */
	public function get_plugin_name() {
		return $this->plugin_name;
	}
	/**
	 * Retrieve the version number of the plugin.
	 *
	 * @since     1.0.0
	 * @return    string    The version number of the plugin.
	 */
	public function get_version() {
		return $this->version;
	}

	/** Functtion that creates a administration page
	 *
	 * @since     1.0.0
	 */
	public function create_admin_page() {
171
		if ( ! current_user_can( 'read_private_posts' ) ) {
172
173
174
			wp_die( esc_html__( 'You do not have sufficient permissions to access this page.' ) );
		}

Angel Matilla's avatar
Angel Matilla committed
175
176
		echo '<form action="#" method="post">';
		echo '<hr><select name="form-id">';
177
		if(isset($_POST['form-id'])){
178
			$this->form_id = $_POST['form-id'];
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
			foreach ( Gen_EU_Form_Parameters::FORM_PICKER as $slug => $form ) {
				if($slug == Gen_EU_Form_Parameters::FORM_PICKER[$this->form_id]){
					echo '<option value="' . esc_attr( $slug ) . '" selected>' . esc_html( $form['label'] ) . '</option>';
				}
				else{
					echo '<option value="' . esc_attr( $slug ) . '">' . esc_html( $form['label'] ) . '</option>';
				}
			}
		}
		else{
			foreach ( Gen_EU_Form_Parameters::FORM_PICKER as $slug => $form ) {
				if($slug == Gen_EU_Form_Parameters::ID){
					echo '<option value="' . esc_attr( $slug ) . '" selected>' . esc_html( $form['label'] ) . '</option>';
				}
				else{
					echo '<option value="' . esc_attr( $slug ) . '">' . esc_html( $form['label'] ) . '</option>';
				}
			}
Angel Matilla's avatar
Angel Matilla committed
197
		}
Angel Matilla's avatar
Angel Matilla committed
198
		echo '</select>';
Angel Matilla's avatar
Angel Matilla committed
199

Angel Matilla's avatar
Angel Matilla committed
200
201
		echo '<input type="submit" name="submit" value="Load form" />';
		echo '</form>';
Angel Matilla's avatar
Angel Matilla committed
202

Angel Matilla's avatar
Angel Matilla committed
203
		$this->error_list = new WP_Error();
Angel Matilla's avatar
Angel Matilla committed
204

Angel Matilla's avatar
Angel Matilla committed
205
206
		if(isset($_POST['form-id'])){
			$this->form_id = $_POST['form-id'];
Angel Matilla's avatar
Angel Matilla committed
207
			$data_source      = GEN_EU_Caldera_Forms_Data_Collector::get_instance($this->form_id);
Angel Matilla's avatar
Angel Matilla committed
208
			echo '<h1>GEN EU Registration Dashboard ' . Gen_EU_Form_Parameters::FORM_PICKER[$this->form_id]['label'] . '</h1>';
Angel Matilla's avatar
Angel Matilla committed
209
			echo '<hr>';
Angel Matilla's avatar
Angel Matilla committed
210
211
		}
		else{
Angel Matilla's avatar
Angel Matilla committed
212
			echo '<h1>GEN EU Registration Dashboard ' . Gen_EU_Form_Parameters::FORM_PICKER[GEN_EU_Form_Parameters::ID]['label'] . '</h1>';
Angel Matilla's avatar
Angel Matilla committed
213
			echo '<hr>';
Angel Matilla's avatar
Angel Matilla committed
214
215
			$data_source      = GEN_EU_Caldera_Forms_Data_Collector::get_instance( GEN_EU_Form_Parameters::ID );
		}		
Rodrigo's avatar
Rodrigo committed
216
217
		$dashboard        = GEN_EU_Dashboard_Loader::get_instance( $data_source ); // get instance of a singleton.
		GEN_EU_Dashboard_Viewer::display_dashboard( $dashboard );
218
219
	}
}