PluginProbe
FormsCRM – Connect Forms to CRM directly / 3.2
FormsCRM – Connect Forms to CRM directly v3.2
4.4.3 4.4.2 4.4.1 4.4.0 4.3.3 trunk 1.1.0 1.2.0 1.2.1 3.0 3.1 3.1.1 3.10.0 3.11.0 3.12.0 3.12.2 3.12.3 3.12.4 3.13.0 3.13.1 3.13.2 3.13.3 3.13.4 3.13.5 3.14.0 All 63 releases
formscrm / includes / class-gravityforms.php

class-gravityforms.php in FormsCRM – Connect Forms to CRM directly 3.2, at includes/class-gravityforms.php

452 lines 13.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Functions for CRM in Gravity Forms
4 *
5 * All helpers functions for Gravity Forms
6 *
7 * @package WordPress
8 * @author David Perez <david@closemarketing.net>
9 * @copyright 2019 Closemarketing
10 * @version 1.0
11 */
12
13 GFForms::include_feed_addon_framework();
14 global $formscrm_api;
15
16 /**
17 * Class for Addon GravityForms
18 */
19 class GFCRM extends GFFeedAddOn {
20
21 protected $_version = FORMSCRM_VERSION;
22 protected $_min_gravityforms_version = '1.9.0';
23 protected $_slug = 'formscrm';
24 protected $_path = 'formscrm/crm.php';
25 protected $_full_path = __FILE__;
26 protected $_url = 'https://www.formscrm.com';
27 protected $_title = 'CRM Add-On';
28 protected $_short_title = 'FormsCRM';
29
30 // Members plugin integration.
31 protected $_capabilities = array(
32 'formscrm',
33 'formscrm_uninstall',
34 );
35
36 // Permissions.
37 protected $_capabilities_settings_page = 'formscrm';
38 protected $_capabilities_form_settings = 'formscrm';
39 protected $_capabilities_uninstall = 'formscrm_uninstall';
40 protected $_enable_rg_autoupgrade = true;
41
42 private static $_instance = null;
43
44 private $crmlib;
45
46 public static function get_instance() {
47 if ( self::$_instance == null ) {
48 self::$_instance = new GFCRM();
49 }
50
51 return self::$_instance;
52 }
53 /**
54 * Init function of library
55 *
56 * @return void
57 */
58 public function init() {
59
60 parent::init();
61 load_plugin_textdomain( 'formscrm', FALSE, '/formscrm/languages' );
62
63 }
64
65 public function init_admin() {
66 parent::init_admin();
67
68 $this->ensure_upgrade();
69 }
70
71 /**
72 * Plugin settings
73 *
74 * @return void
75 */
76 public function plugin_settings_fields() {
77
78 global $formscrm_api;
79
80 return array(
81 array(
82 'title' => __( 'CRM Account Information', 'formscrm' ),
83 'description' => __( 'Use this connector with CRM software. Use Gravity Forms to collect customer information and automatically add them to your CRM Leads.', 'formscrm' ),
84 'fields' => array(
85 array(
86 'name' => 'fc_crm_type',
87 'label' => __( 'CRM Type', 'formscrm' ),
88 'type' => 'select',
89 'class' => 'medium',
90 'onchange' => 'jQuery(this).parents("form").submit();',
91 'choices' => formscrm_get_choices(),
92 ),
93 array(
94 'name' => 'fc_crm_url',
95 'label' => __( 'CRM URL', 'formscrm' ),
96 'type' => 'text',
97 'class' => 'medium',
98 'tooltip' => __( 'Use the URL with http and the ending slash /.', 'formscrm' ),
99 'tooltip_class' => 'tooltipclass',
100 'dependency' => array(
101 'field' => 'fc_crm_type',
102 'values' => formscrm_get_dependency_url(),
103 ),
104 ),
105 array(
106 'name' => 'fc_crm_username',
107 'label' => __( 'Username', 'formscrm' ),
108 'type' => 'text',
109 'class' => 'medium',
110 'dependency' => array(
111 'field' => 'fc_crm_type',
112 'values' => formscrm_get_dependency_username(),
113 ),
114 'feedback_callback' => $this->login_api_crm(),
115 ),
116 array(
117 'name' => 'fc_crm_password',
118 'label' => __('Password', 'formscrm' ),
119 'type' => 'api_key',
120 'class' => 'medium',
121 'tooltip' => __( 'Use the password of the actual user.', 'formscrm' ),
122 'tooltip_class' => 'tooltipclass',
123 'dependency' => array(
124 'field' => 'fc_crm_type',
125 'values' => formscrm_get_dependency_password(),
126 ),
127 ),
128 array(
129 'name' => 'fc_crm_apipassword',
130 'label' => __('API Password for User', 'formscrm'),
131 'type' => 'api_key',
132 'class' => 'medium',
133 //'feedback_callback' => $this->login_api_crm(),
134 'tooltip' => __('Find the API Password in the profile of the user in CRM.', 'formscrm'),
135 'tooltip_class' => 'tooltipclass',
136 'dependency' => array(
137 'field' => 'fc_crm_type',
138 'values' => formscrm_get_dependency_apipassword(),
139 ),
140 ),
141 array(
142 'name' => 'fc_crm_apisales',
143 'label' => __('Password and Security Key', 'formscrm'),
144 'type' => 'api_key',
145 'class' => 'medium',
146 'tooltip' => __( '"Password""SecurityKey" Go to My Settings / Reset my Security Key.', 'formscrm'),
147 'tooltip_class' => 'tooltipclass',
148 'dependency' => array(
149 'field' => 'fc_crm_type',
150 'values' => formscrm_get_dependency_apisales(),
151 ),
152 ),
153 array(
154 'name' => 'fc_crm_odoodb',
155 'label' => __( 'Odoo DB Name', 'formscrm' ),
156 'type' => 'text',
157 'class' => 'medium',
158 'dependency' => array(
159 'field' => 'fc_crm_type',
160 'values' => formscrm_get_dependency_odoodb(),
161 ),
162 ),
163 ),
164 ),
165 );
166 }
167
168 public function settings_api_key( $field, $echo = true ) {
169
170 $field['type'] = 'text';
171
172 $api_key_field = $this->settings_text( $field, false );
173
174 //switch type="text" to type="password" so the key is not visible
175 $api_key_field = str_replace('type="text"', 'type="password"', $api_key_field);
176
177 $caption = '<small>' . sprintf( esc_html__( 'Find a Password or API key depending of CRM.', 'formscrm' ) ) . '</small>';
178
179 if ( $echo ) {
180 echo esc_html( $api_key_field ) . '</br>' . esc_html( $caption );
181 }
182
183 return $api_key_field . '</br>' . $caption;
184 }
185
186 /**
187 * Forms Settings
188 *
189 * @param array $form Form.
190 * @param string $feed_id Feed id.
191 * @return void
192 */
193 public function feed_edit_page( $form, $feed_id ) {
194
195 // Ensures valid credentials were entered in the settings page.
196 if ( false == $this->login_api_crm() ) {
197 ?>
198 <div class="notice notice-error">
199 <?php
200 esc_html_e( 'We are unable to login to CRM.', 'formscrm' );
201 echo ' <a href="' . esc_url( $this->get_plugin_settings_url() ) . '">' . esc_html__( 'Use Settings Page', 'formscrm' ) . '</a>';
202 ?>
203 </div>
204 <?php
205 return;
206 }
207
208 echo '<script type="text/javascript">var form = ' . esc_html( GFCommon::json_encode( $form ) ) . ';</script>';
209
210 parent::feed_edit_page( $form, $feed_id );
211 }
212
213 /**
214 * Include library connector
215 *
216 * @param string $crmtype Type of CRM.
217 * @return void
218 */
219 private function include_library( $crmtype ) {
220 if ( isset( $_POST['_gform_setting_fc_crm_type'] ) ) {
221 $crmtype = sanitize_text_field( $_POST['_gform_setting_fc_crm_type'] );
222 }
223
224 if ( isset( $crmtype ) ) {
225 $crmname = strtolower( $crmtype );
226 $crmclassname = str_replace( ' ', '', $crmname );
227 $crmclassname = 'CRMLIB_' . strtoupper( $crmclassname );
228 $crmname = str_replace( ' ', '_', $crmname );
229
230 $array_path = formscrm_get_crmlib_path();
231 if ( isset( $array_path[ $crmname ] ) ) {
232 include_once $array_path[ $crmname ];
233 }
234
235 formscrm_debug_message( $array_path[ $crmname ]);
236
237 if ( class_exists( $crmclassname ) ) {
238 $this->crmlib = new $crmclassname();
239 }
240 }
241 }
242
243 public function feed_settings_fields() {
244
245 $settings = $this->get_plugin_settings();
246 $this->include_library( $settings['fc_crm_type'] );
247
248 return array(
249 array(
250 'title' => __( 'CRM Feed', 'formscrm' ),
251 'description' => '',
252 'fields' => array(
253 array(
254 'name' => 'feedName',
255 'label' => __( 'Name', 'formscrm' ),
256 'type' => 'text',
257 'required' => true,
258 'class' => 'medium',
259 'tooltip' => '<h6>' . __( 'Name', 'formscrm' ) . '</h6>' . __( 'Enter a feed name to uniquely identify this setup.', 'formscrm' ),
260 ),
261 array(
262 'name' => 'gf_crm_module',
263 'label' => __( 'CRM Module', 'formscrm' ),
264 'type' => 'select',
265 'class' => 'medium',
266 'onchange' => 'jQuery(this).parents("form").submit();',
267 'choices' => $this->crmlib->list_modules( $settings ),
268 ),
269 array(
270 'name' => 'listFields',
271 'label' => __( 'Map Fields', 'formscrm' ),
272 'type' => 'field_map',
273 'dependency' => 'gf_crm_module',
274 'field_map' => $this->crmlib->list_fields( $settings, $this->get_setting( 'gf_crm_module' ) ),
275 'tooltip' => '<h6>' . __( 'Map Fields', 'formscrm' ) . '</h6>' . __('Associate your CRM custom fields to the appropriate Gravity Form fields by selecting the appropriate form field from the list.', 'formscrm' ),
276 ),
277 ),
278 ),
279 );
280 }
281
282 public function ensure_upgrade() {
283
284 if (get_option('gf_crm_upgrade')) {
285 return false;
286 }
287
288 $feeds = $this->get_feeds();
289 if ( empty( $feeds ) ) {
290
291 // Force Add-On framework upgrade.
292 $this->upgrade( '2.0' );
293 }
294
295 update_option( 'gf_crm_upgrade', 1 );
296 }
297
298 public function feed_list_columns() {
299 return array(
300 'feedName' => __( 'Name', 'formscrm' ),
301 );
302 }
303
304 public function process_feed( $feed, $entry, $form ) {
305
306 if ( ! $this->is_valid_key() ) {
307 return;
308 }
309
310 $this->export_feed( $entry, $form, $feed );
311 }
312
313 public function export_feed( $entry, $form, $feed ) {
314 $settings = $this->get_plugin_settings();
315 $this->include_library( $settings['fc_crm_type'] );
316
317 if ( ! empty( $feed['meta']['listFields_first_name'] ) ) {
318 $name = $this->get_name( $entry, $feed['meta']['listFields_first_name'] );
319 }
320
321 $merge_vars = array();
322 $field_maps = $this->get_field_map_fields( $feed, 'listFields' );
323
324 foreach ( $field_maps as $var_key => $field_id ) {
325 $field = RGFormsModel::get_field( $form, $field_id );
326
327 if ( isset( $field['type'] ) && GFCommon::is_product_field( $field['type'] ) && rgar( $field, 'enablePrice' ) ) {
328 $ary = explode('|', $entry[$field_id]);
329 $product_name = count($ary) > 0 ? $ary[0] : '';
330 $merge_vars[] = array('name' => $var_key, 'value' => $product_name);
331 } elseif ( $field && RGFormsModel::get_input_type( $field ) == 'checkbox' ) {
332 $value = '';
333 foreach ( $field['inputs'] as $input ) {
334 $index = (string) $input['id'];
335 $value_n = apply_filters( 'formscrm_field_value', rgar( $entry, $index ), $form['id'], $field_id, $entry );
336 $value .= $value_n;
337 if ( $value_n ) {
338 $value .= '|';
339 }
340 }
341 $value = substr( $value, 0, -1 );
342 $merge_vars[] = array(
343 'name' => $var_key,
344 'value' => $value,
345 );
346 } elseif ( $field && RGFormsModel::get_input_type( $field ) == 'multiselect' ) {
347 $value = apply_filters( 'formscrm_field_value', rgar( $entry, $field_id ), $form['id'], $field_id, $entry );
348 $value = str_replace( ',', '|', $value );
349
350 $merge_vars[] = array(
351 'name' => $var_key,
352 'value' => $value,
353 );
354 } elseif ( $field && RGFormsModel::get_input_type( $field ) == 'textarea' ) {
355 $value = apply_filters( 'formscrm_field_value', rgar( $entry, $field_id ), $form['id'], $field_id, $entry );
356 $value = str_replace( array( "\r", "\n" ), ' ', $value );
357 $merge_vars[] = array(
358 'name' => $var_key,
359 'value' => $value,
360 );
361 } else {
362 $merge_vars[] = array(
363 'name' => $var_key,
364 'value' => apply_filters( 'formscrm_field_value', rgar( $entry, $field_id ), $form['id'], $field_id, $entry ),
365 );
366 }
367 }
368
369 $override_custom_fields = apply_filters( 'formscrm_override_blank_custom_fields', false, $entry, $form, $feed );
370 if ( ! $override_custom_fields ) {
371 $merge_vars = $this->remove_blank_custom_fields( $merge_vars );
372 }
373
374 $settings = $this->get_plugin_settings();
375
376 formscrm_debug_message( $settings );
377 formscrm_debug_message( $merge_vars );
378
379 $id = $this->crmlib->create_entry( $settings, $merge_vars );
380
381 formscrm_debug_message( $id );
382 }
383
384 private static function remove_blank_custom_fields( $merge_vars ) {
385 $i = 0;
386
387 $count = count( $merge_vars );
388
389 for ( $i = 0; $i < $count; $i++ ) {
390 if ( rgblank( $merge_vars[ $i ]['value'] ) ) {
391 unset( $merge_vars[ $i ] );
392 }
393 }
394 // resort the array because items could have been removed, this will give an error from CRM if the keys are not in numeric sequence.
395 sort( $merge_vars );
396 return $merge_vars;
397 }
398
399 private function get_name( $entry, $field_id ) {
400
401 // If field is simple (one input), simply return full content.
402 $name = rgar( $entry, $field_id );
403 if ( ! empty( $name ) ) {
404 return $name;
405 }
406
407 // Complex field (multiple inputs). Join all pieces and create name.
408 $prefix = trim( rgar( $entry, $field_id . '.2' ) );
409 $first = trim( rgar( $entry, $field_id . '.3' ) );
410 $last = trim( rgar( $entry, $field_id . '.6' ) );
411 $suffix = trim( rgar( $entry, $field_id . '.8' ) );
412
413 $name = $prefix;
414 $name .= ! empty( $name ) && ! empty( $first ) ? " $first" : $first;
415 $name .= ! empty( $name ) && ! empty( $last ) ? " $last" : $last;
416 $name .= ! empty( $name ) && ! empty( $suffix ) ? " $suffix" : $suffix;
417
418 return $name;
419 }
420
421 private function is_valid_key() {
422 $result_api = $this->login_api_crm();
423
424 return $result_api;
425 }
426
427 private function login_api_crm() {
428 $login_result = false;
429
430 // Logins to CRM.
431 $settings = $this->get_plugin_settings();
432
433 if ( isset( $settings['fc_crm_type'] ) ) {
434 $this->include_library( $settings['fc_crm_type'] );
435 }
436
437 if ( isset( $this->crmlib ) ) {
438 $login_result = $this->crmlib->login( $settings );
439 formscrm_debug_message( $login_result );
440 }
441
442 formscrm_testserver();
443
444 if ( ! isset( $login_result ) ) {
445 $login_result = '';
446 }
447
448 return $login_result;
449 }
450
451 } //from main class
452