@@ -40,6 +40,14 @@ class Wp_Odoo_Form_Integrator_Admin {
40
40
*/
41
41
private $ version ;
42
42
43
+ /**
44
+ * The version of this plugin.
45
+ *
46
+ * @since 1.0.0
47
+ * @access private
48
+ * @var string $version The current version of this plugin.
49
+ */
50
+ private $ js_object = array ();
43
51
/**
44
52
* Initialize the class and set its properties.
45
53
*
@@ -52,6 +60,11 @@ public function __construct( $plugin_name, $version ) {
52
60
$ this ->plugin_name = $ plugin_name ;
53
61
$ this ->version = $ version ;
54
62
63
+ /**
64
+ * Defining ajax error messages that common for all admin screens
65
+ */
66
+ $ this ->js_object ['str_notice_ajax_failed ' ] = __ ( 'Ajax service is failed due to internal server error ' , 'wp-odoo-form-integrator ' );
67
+
55
68
}
56
69
57
70
/**
@@ -96,7 +109,283 @@ public function enqueue_scripts() {
96
109
* class.
97
110
*/
98
111
99
- wp_enqueue_script ( $ this ->plugin_name , plugin_dir_url ( __FILE__ ) . 'js/wp-odoo-form-integrator-admin.js ' , array ( 'jquery ' ), $ this ->version , false );
112
+ wp_enqueue_script ( $ this ->plugin_name , plugin_dir_url ( __FILE__ ) . 'js/wp-odoo-form-integrator-admin.js ' , array ( 'jquery ' ), $ this ->version , true );
113
+
114
+ $ js_object = array (
115
+ 'str_error_odoo_url ' => __ ( 'Please provide valid URL ' , 'wp-odoo-form-integrator ' ),
116
+ 'str_error_odoo_database ' => __ ( 'Please provide valid database ' , 'wp-odoo-form-integrator ' ),
117
+ 'str_error_odoo_username ' => __ ( 'Please provide valid username ' , 'wp-odoo-form-integrator ' ),
118
+ 'str_error_odoo_password ' => __ ( 'Please provide valid password ' , 'wp-odoo-form-integrator ' ),
119
+ 'ajx_url ' => admin_url (),
120
+ 'str_notice_ajax_failed ' => __ ( 'Unable to test due to internal server error ' , 'wp-odoo-form-integrator ' ),
121
+ 'str_notice_odoo_connected ' => __ ( 'Successfully connects to Odoo Server ' , 'wp-odoo-form-integrator ' ),
122
+ );
123
+ wp_localize_script ( $ this ->plugin_name , 'wp_odoo_form_integrator_admin_settings ' , $ js_object );
124
+
125
+ }
126
+
127
+ /**
128
+ * Register the administration menu for this plugin into the WordPress Dashboard menu.
129
+ *
130
+ * @since 1.0.0
131
+ */
132
+ public function add_plugin_admin_menu () {
133
+
134
+ add_menu_page (
135
+ __ ( 'WP Odoo Form Integrator ' , 'wp-odoo-form-integrator ' ),
136
+ __ ( 'WP Odoo Form Integrator ' , 'wp-odoo-form-integrator ' ),
137
+ 'manage_options ' ,
138
+ $ this ->plugin_name ,
139
+ array ($ this , 'display_plugin_settings_page ' ),
140
+ 'dashicons-welcome-widgets-menus '
141
+ );
142
+
143
+ add_submenu_page (
144
+ $ this ->plugin_name ,
145
+ __ ( 'Integrated Forms ' , 'wp-odoo-form-integrator ' ),
146
+ __ ( 'Integrated Forms ' , 'wp-odoo-form-integrator ' ),
147
+ 'manage_options ' ,
148
+ 'wp-odoo-form-integrator-integrated-forms ' ,
149
+ array ($ this , 'display_plugin_integrated_forms_page ' )
150
+ );
151
+
152
+ add_submenu_page (
153
+ 'wp-odoo-form-integrator ' ,
154
+ __ ( 'Add New ' , 'wp-odoo-form-integrator ' ),
155
+ __ ( 'Add New ' , 'wp-odoo-form-integrator ' ),
156
+ 'manage_options ' ,
157
+ 'wp-odoo-form-integrator-add-new ' ,
158
+ array ($ this , 'display_plugin_add_new_page ' )
159
+ );
160
+
161
+ add_submenu_page (
162
+ 'wp-odoo-form-integrator ' ,
163
+ __ ( 'Settings ' , 'wp-odoo-form-integrator ' ),
164
+ __ ( 'Settings ' , 'wp-odoo-form-integrator ' ),
165
+ 'manage_options ' ,
166
+ 'wp-odoo-form-integrator-settings ' ,
167
+ array ($ this , 'display_plugin_settings_page ' )
168
+ );
169
+
170
+ remove_submenu_page ('wp-odoo-form-integrator ' ,'wp-odoo-form-integrator ' );
171
+
172
+ }
173
+
174
+ /**
175
+ * This tests Odoo connection. It's called through AJAX service.
176
+ *
177
+ * @since 1.0.0
178
+ */
179
+ public function test_odoo_connection () {
180
+
181
+ /**
182
+ * This is RPC client library used to call Odoo APIs.
183
+ */
184
+ require_once plugin_dir_path ( dirname ( __FILE__ ) ) . 'includes/class-wp-odoo-client.php ' ;
185
+
186
+ $ client = new Wp_Odoo_Client ($ _POST ['wp_odoo_form_odoo_url ' ],
187
+ $ _POST ['wp_odoo_form_odoo_database ' ],
188
+ $ _POST ['wp_odoo_form_odoo_username ' ],
189
+ $ _POST ['wp_odoo_form_odoo_password ' ]);
190
+ try {
191
+ echo json_encode ($ client ->test_authentication ());
192
+ } catch (Exception $ e ) {
193
+ $ ret = array ();
194
+ $ ret ['status ' ] = false ;
195
+ $ ret ['message ' ] = $ e ->getMessage ();
196
+ echo json_encode ($ ret );
197
+ }
198
+ wp_die ();
199
+ }
200
+
201
+ /**
202
+ * This gets all types of supported form types.
203
+ * It's called through AJAX service.
204
+ *
205
+ * @since 1.0.0
206
+ */
207
+ public function get_form_types () {
208
+
209
+ global $ wp_odoo_form_modules ;
210
+
211
+ $ result = array ();
212
+ foreach ($ wp_odoo_form_modules as $ module ) {
213
+ $ object = new $ module ();
214
+ $ result [$ module ] = $ object ->get_plugin_name ();
215
+ }
216
+ echo json_encode ($ result );
217
+ wp_die ();
218
+
219
+ }
220
+
221
+ /**
222
+ * This gets all types of supported form types.
223
+ * It's called through AJAX service.
224
+ *
225
+ * @since 1.0.0
226
+ */
227
+ public function get_all_forms () {
228
+
229
+ $ result = array ();
230
+ $ object = new $ _POST ['module ' ]();
231
+ $ all_forms = $ object ->get_all_forms ();
232
+ foreach ($ all_forms as $ form ) {
233
+ $ result [$ form ['id ' ]] = $ form ['label ' ];
234
+ }
235
+ echo json_encode ($ result );
236
+ wp_die ();
237
+
238
+ }
239
+
240
+ /**
241
+ * This gets all types of supported form types.
242
+ * It's called through AJAX service.
243
+ *
244
+ * @since 1.0.0
245
+ */
246
+ public function get_form_fields () {
247
+
248
+ $ result = array ();
249
+ $ object = new $ _POST ['module ' ]();
250
+ $ all_forms = $ object ->get_form_fields ($ _POST ['form_id ' ]);
251
+ foreach ($ all_forms as $ form ) {
252
+ $ result [$ form ['id ' ]] = $ form ['label ' ];
253
+ }
254
+ echo json_encode ($ result );
255
+ wp_die ();
256
+
257
+ }
258
+
259
+ /**
260
+ * This gets all types of supported form types.
261
+ * It's called through AJAX service.
262
+ *
263
+ * @since 1.0.0
264
+ */
265
+ public function get_odoo_models () {
266
+
267
+ $ result = array ();
268
+ /**
269
+ * TODO: Fetch name and key of all models dynamically from Odoo
270
+ */
271
+ // $result['crm.lead'] = 'crm.lead';
272
+ // $result['res.partner'] = 'res.partner';
273
+ // echo json_encode($result);
274
+ require_once plugin_dir_path ( dirname ( __FILE__ ) ) . 'includes/class-wp-odoo-client.php ' ;
275
+ $ client = new Wp_Odoo_Client (get_option ('wp_odoo_form_odoo_url ' ),
276
+ get_option ('wp_odoo_form_odoo_database ' ),
277
+ get_option ('wp_odoo_form_odoo_username ' ),
278
+ get_option ('wp_odoo_form_odoo_password ' ));
279
+ try {
280
+ echo json_encode ($ client ->get_models ());
281
+ } catch (Exception $ e ) {
282
+ $ ret = array ();
283
+ $ ret ['status ' ] = false ;
284
+ $ ret ['message ' ] = $ e ->getMessage ();
285
+ echo json_encode ($ ret );
286
+ }
287
+ wp_die ();
288
+
289
+ }
290
+
291
+ /**
292
+ * This gets all types of supported form types.
293
+ * It's called through AJAX service.
294
+ *
295
+ * @since 1.0.0
296
+ */
297
+ public function get_odoo_fields () {
298
+
299
+ /**
300
+ * This is RPC client library used to call Odoo APIs.
301
+ */
302
+ require_once plugin_dir_path ( dirname ( __FILE__ ) ) . 'includes/class-wp-odoo-client.php ' ;
303
+ $ client = new Wp_Odoo_Client (get_option ('wp_odoo_form_odoo_url ' ),
304
+ get_option ('wp_odoo_form_odoo_database ' ),
305
+ get_option ('wp_odoo_form_odoo_username ' ),
306
+ get_option ('wp_odoo_form_odoo_password ' ));
307
+ try {
308
+ echo json_encode ($ client ->get_fields ($ _POST ['module ' ]));
309
+ } catch (Exception $ e ) {
310
+ $ ret = array ();
311
+ $ ret ['status ' ] = false ;
312
+ $ ret ['message ' ] = $ e ->getMessage ();
313
+ echo json_encode ($ ret );
314
+ }
315
+ wp_die ();
316
+
317
+ }
318
+
319
+ /**
320
+ * Add settings action link to the plugins page.
321
+ *
322
+ * @since 1.0.0
323
+ */
324
+ public function add_action_links ( $ links ) {
325
+ $ settings_link = array (
326
+ '<a href=" ' . admin_url ( 'options-general.php?page= ' . $ this ->plugin_name ) . '"> ' . __ ('Settings ' , $ this ->plugin_name ) . '</a> ' ,
327
+ );
328
+ return array_merge ( $ settings_link , $ links );
329
+
330
+ }
331
+
332
+ /**
333
+ * Render the settings page for this plugin.
334
+ *
335
+ * @since 1.0.0
336
+ */
337
+ public function display_plugin_settings_page () {
338
+
339
+ wp_enqueue_style ( $ this ->plugin_name , plugin_dir_url ( __FILE__ ) . 'css/wp-odoo-form-integrator-admin-settings.css ' , array (), $ this ->version , 'all ' );
340
+ wp_enqueue_script ( $ this ->plugin_name , plugin_dir_url ( __FILE__ ) . 'js/wp-odoo-form-integrator-admin-settings.js ' , array ( 'jquery ' ), $ this ->version , true );
341
+
342
+ $ this ->js_object ['str_error_odoo_url ' ] = __ ( 'Please provide valid URL ' , 'wp-odoo-form-integrator ' );
343
+ $ this ->js_object ['str_error_odoo_database ' ] = __ ( 'Please provide valid database ' , 'wp-odoo-form-integrator ' );
344
+ $ this ->js_object ['str_error_odoo_username ' ] = __ ( 'Please provide valid username ' , 'wp-odoo-form-integrator ' );
345
+ $ this ->js_object ['str_error_odoo_password ' ] = __ ( 'Please provide valid password ' , 'wp-odoo-form-integrator ' );
346
+ $ this ->js_object ['str_notice_odoo_connected ' ] = __ ( 'WP successfully connected to Odoo Server ' , 'wp-odoo-form-integrator ' );
347
+ wp_localize_script ( $ this ->plugin_name , 'wp_odoo_form_integrator_admin_settings ' , $ this ->js_object );
348
+
349
+ include_once ( 'partials/wp-odoo-form-integrator-admin-display-settings.php ' );
350
+
351
+ }
352
+
353
+ /**
354
+ * Render integrated forms page for this plugin.
355
+ *
356
+ * @since 1.0.0
357
+ */
358
+ public function display_plugin_integrated_forms_page () {
359
+ include_once ( 'partials/wp-odoo-form-integrator-admin-display-integrated-forms.php ' );
360
+ }
361
+
362
+ /**
363
+ * Render integrated forms page for this plugin.
364
+ *
365
+ * @since 1.0.0
366
+ */
367
+ public function display_plugin_add_new_page () {
368
+
369
+ wp_enqueue_style ( $ this ->plugin_name , plugin_dir_url ( __FILE__ ) . 'css/wp-odoo-form-integrator-admin-settings.css ' , array (), $ this ->version , 'all ' );
370
+ wp_enqueue_script ( $ this ->plugin_name , plugin_dir_url ( __FILE__ ) . 'js/wp-odoo-form-integrator-admin-add-new.js ' , array ( 'jquery ' ), $ this ->version , true );
371
+
372
+ $ this ->js_object ['str_select_form_type ' ] = __ ( 'Please select form type ' , 'wp-odoo-form-integrator ' );
373
+ $ this ->js_object ['str_select_form ' ] = __ ( 'Please select form ' , 'wp-odoo-form-integrator ' );
374
+ $ this ->js_object ['str_select_odoo_model ' ] = __ ( 'Please select odoo model ' , 'wp-odoo-form-integrator ' );
375
+ $ this ->js_object ['str_form_type_change_warning ' ] = __ ( 'Change of form type will reset form and field mapping. Do you want to continue? ' , 'wp-odoo-form-integrator ' );
376
+ $ this ->js_object ['str_form_change_warning ' ] = __ ( 'Change of form will reset field mapping. Do you want to continue? ' , 'wp-odoo-form-integrator ' );
377
+ $ this ->js_object ['str_odoo_model_change_warning ' ] = __ ( 'Change of Odoo model will reset field mapping. Do you want to continue? ' , 'wp-odoo-form-integrator ' );
378
+ $ this ->js_object ['str_unable_fetch_odoo_model ' ] = __ ( 'Unable to fetch Odoo models. Please check Settings ' , 'wp-odoo-form-integrator ' );
379
+
380
+ $ this ->js_object ['str_error_mapping_title ' ] = __ ( 'Please provide title ' , 'wp-odoo-form-integrator ' );
381
+ $ this ->js_object ['str_error_mapping_form_type ' ] = __ ( 'Please select form type ' , 'wp-odoo-form-integrator ' );
382
+ $ this ->js_object ['str_error_mapping_form ' ] = __ ( 'Please select form ' , 'wp-odoo-form-integrator ' );
383
+ $ this ->js_object ['str_error_mapping_model ' ] = __ ( 'Please select model ' , 'wp-odoo-form-integrator ' );
384
+ $ this ->js_object ['str_error_mapping_no_fields ' ] = __ ( 'No fields are mapped ' , 'wp-odoo-form-integrator ' );
385
+
386
+ wp_localize_script ( $ this ->plugin_name , 'wp_odoo_form_integrator_admin_add_new ' , $ this ->js_object );
387
+
388
+ include_once ( 'partials/wp-odoo-form-integrator-admin-display-add-new.php ' );
100
389
101
390
}
102
391
0 commit comments