class-gforms.php 12.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
<?php

if ( ! defined( 'ABSPATH' ) ) { 
    exit; // Exit if accessed directly
}

if (class_exists("GFForms")) {

    GFForms::include_feed_addon_framework();

    class WPF_GForms_Integration extends GFFeedAddOn {
        protected $_version = WP_FUSION_VERSION; 
        protected $_min_gravityforms_version = "1.7.9999";
        protected $_slug = "wpfgforms";
        protected $_full_path = __FILE__;
        protected $_title = "CRM Integration";
        protected $_short_title = "WP Fusion";
        protected $postvars = array();


		/**
		 * Get parent running
		 *
		 * @access  public
		 * @return  void
		 */

        public function init(){

            parent::init();

        }


		/**
		 * Triggered when form is submitted
		 *
		 * @access  public
		 * @return  void
		 */

        public function process_feed( $feed, $entry, $form ){

			$update_data = array();

			// Prepare update array
			foreach($feed['meta']['wpf_fields'] as $id => $data) {

				// Convert dashes back into points for isset
				$id = str_replace('-', '.', $id);

				if(isset($entry[$id]) && !empty($entry[$id]) && !empty($data['crm_field'])) {

					$value = apply_filters( 'wpf_format_field_value', $entry[$id], $data['type'], $data['crm_field'] );

					if(!empty($value)) {
						$update_data[$data['crm_field']] = $value;

						if($data['type'] == 'email') {
							$email_address = $entry[$id];
						}

					}
				}
			}

			// If no email and user not logged in don't bother
			if(!isset($email_address) && !is_user_logged_in()) {

				return;

			} elseif (!isset($email_address) && is_user_logged_in()) {

				global $current_user;
				$contact_id = wp_fusion()->user->get_contact_id( $current_user->ID );

			} else {

				if(is_object(get_user_by( 'email', $email_address ))) {

					// Check and see if a local user exists
					$user = get_user_by( 'email', $email_address );
					$user_id = $user->ID;
					$contact_id = wp_fusion()->user->get_contact_id($user_id);

				} else {

					// Check and see if user exists in CRM
					$user_id = false;
					$contact_id = wp_fusion()->crm->get_contact_id( $email_address );

				}
			}

			$update_data = apply_filters( 'wpf_gform_pre_submission', $update_data, $user_id, $contact_id );

			if($contact_id != false) {

				// Update Infusionsoft if contact ID exists
				wp_fusion()->crm->update_contact( $contact_id, $update_data, false );

			} else {

				// Add contact if doesn't exist yet
				$contact_id = wp_fusion()->crm->add_contact( $update_data, false );

			}

			// Apply tags if set
			if(isset($feed['meta']['wpf_tags'])) {

				// Even if the user is logged in, they may have submitted the form with a different email. This makes sure the tags are applied to the right record
				if(isset($user_id))
					$user_info = get_userdata( $user_id );

				if(isset($user_info) && $user_info->user_email == $email_address) {

					// If user exists locally, apply the tags locally as well
					wp_fusion()->user->apply_tags($feed['meta']['wpf_tags'], $user_id);

				} else {

					wp_fusion()->crm->apply_tags( $feed['meta']['wpf_tags'], $contact_id );

				}

			}

			do_action( 'wpf_gform_post_submission', $update_data, $user_id, $contact_id );

        }

		/**
		 * Displays table for mapping fields
		 *
		 * @access  public
		 * @return  void
		 */

        public function settings_wpf_fields( $field ) {

        	$form = $this->get_current_form();

			// Quiz Handling
			$quiz_fields = GFAPI::get_fields_by_type( $form, array( 'quiz' ) );

			if ( !empty( $quiz_fields ) ) {

				$quiz_fields = array(
							'gquiz_score' 		=> 'Quiz Score Total',
							'gquiz_percent'	 	=> 'Quiz Score Percentage',
							'gquiz_grade' 		=> 'Quiz Grade',
							'gquiz_is_pass' 	=> 'Quiz Pass/Fail'
					);

			}

        	echo '<table class="settings-field-map-table wpf-field-map" cellspacing="0" cellpadding="0">';

        		echo '<tbody>';

        			$email_found = false;

        			foreach($form['fields'] as $field) {

        				if($field->inputs == null) {

        					// Handing for simple fields (no subfields)

        					if($field->type == 'email')
        						$email_found = true;

	        				echo '<tr>';
	        					echo '<td><label>' . $field->label . '<label></td>';
	        					echo '<td><i class="fa fa-angle-double-right"></i></td>';
	        					echo '<td>';
								wpf_render_crm_field_select( $this->get_setting('wpf_fields[' . $field->id . '][crm_field]'), '_gaddon_setting_wpf_fields', $field->id );

								$this->settings_hidden(
									array(
										'label'         => '',
										'name'          => 'wpf_fields[' . $field->id . '][type]',
										'default_value' => $field->type
									)
								);

	        					echo '</td>';
	        				echo '</tr>';

	        			} else {

	        				// Fields with subfields (Name, Address, etc.)

        					foreach($field->inputs as $input) {

        						if(!isset($input['isHidden']) || $input['isHidden'] == false) {

		        					if($field->type == 'email')
		        						$email_found = true;

	        						if($input['label'] == 'First') {
	        							$std = 'First Name';
	        							$name = 'FirstName';
	        						} elseif($input['label'] == 'Last') {
	        							$std = 'Last Name';
	        							$name = 'LastName';
	        						} else {
	        							$std = '';
	        							$name = '';
	        						}

		        					echo '<tr>';
		        					echo '<td><label>' . $field->label . ' - ' . $input['label'] .'<label></td>';
		        					echo '<td><i class="fa fa-angle-double-right"></i></td>';

		        					echo '<td>';
		        					wpf_render_crm_field_select( $this->get_setting('wpf_fields[' . str_replace('.', '-', $input['id']) . '][crm_field]'), '_gaddon_setting_wpf_fields', str_replace('.', '-', $input['id']) );
									
									$this->settings_hidden(
										array(
											'label'         => '',
											'name'          => 'wpf_fields[' . str_replace('.', '-', $input['id']) . '][type]',
											'default_value' => $field->type
										)
									);

		        					echo '</td>';
		        					echo '</tr>';

        						}

        					}

	        			}

        			}

					if ( !empty( $quiz_fields ) ) {

						echo '<tr><td colspan="2"><strong><br />Quiz Fields</strong></td></tr>';

						foreach( $quiz_fields as $id => $label ) {

	        				echo '<tr>';
	        					echo '<td><label>' . $label . '<label></td>';
	        					echo '<td><i class="fa fa-angle-double-right"></i></td>';
	        					echo '<td>';
								wpf_render_crm_field_select( $this->get_setting('wpf_fields[' . $id . '][crm_field]'), '_gaddon_setting_wpf_fields', $id );

								$this->settings_hidden(
									array(
										'label'         => '',
										'name'          => 'wpf_fields[' . $id . '][type]',
										'default_value' => 'text'
									)
								);

	        					echo '</td>';
	        				echo '</tr>';

						}

					}

        		echo '</tbody>';
        	echo '</table>';

        	if($email_found == false)
        		echo '<div class="alert danger"><strong>Warning:</strong> No <i>email</i> type field found on this form. Entries from guest users will not be sent to ' . wp_fusion()->crm->name . '.</div>';


        }

		/**
		 * Saves settings
		 *
		 * @access  public
		 * @return  array Settings
		 */

        public function save_wpf_fields($field, $setting) {

        	foreach($setting as $index => $fields) {

        		if(!empty($fields['crm_field'])) {
					$setting[$index]['crm_field'] = $setting[$index]['crm_field'];
        		} else {
        			unset($setting[$index]);
        		}

        	}

        	return $setting;

        }


		/**
		 * Renders tag multi select field
		 *
		 * @access  public
		 * @return  void
		 */

        public function settings_wpf_tags( $field ) {

        	wpf_render_tag_multiselect($this->get_setting('wpf_tags'), '_gaddon_setting_wpf_tags');

        }

		/**
		 * Defines settings for the feed
		 *
		 * @access  public
		 * @return  array Feed settings
		 */

    	public function feed_settings_fields() {
            return array(
                array(
                    "title"  => WPF_CRM_NAME . " Integration",
                    "fields" => array(

                        array(
                            "label"   => "Feed name",
                            "type"    => "text",
                            "name"    => "feedName",
                            "tooltip" => "Enter a name to remember this feed by.",
                            "class"   => "small"
                        ),

                        array(
                            "name" => "wpf_fields",
                            "label" => "Map Fields",
                            "type" => "wpf_fields",
                            "tooltip" => "Select a CRM field from the dropdown, or leave blank to disable sync",
                            'save_callback' => array( $this, 'save_wpf_fields' )
                        ),

                        array(
                            "name" => "wpf_tags",
                            "label" => "Apply Tags",
                            "type" => "wpf_tags",
                            "tooltip" => "Select tags to be applied when a user submits this form.",
                        ),

						array(
							'type'           => 'feed_condition',
							'name'           => 'condition',
							'label'          => 'Opt-In Condition',
							'checkbox_label' => 'Enable Condition',
							'instructions'   => 'Process this feed if'
						)
                    )
                )
            );
        }

		/**
		 * Creates columns for feed
		 *
		 * @access  public
		 * @return  array Feed settings
		 */

		public function feed_list_columns() {
			return array(
				'feedName' 	=> __('Name', 'wp-fusion'),
				'gftags' 	=> __('Applies Tags', 'wp-fusion'),
			);
		}

		/**
		 * Displays tags in custom column
		 *
		 * @access  public
		 * @return  string Configured tags
		 */


		public function get_column_value_gftags( $feed ){

			$tags = rgars( $feed, 'meta/wpf_tags' );
			$tag_labels = array();
			foreach($tags as $tag) {
				$tag_labels[] = wp_fusion()->user->get_tag_label($tag);
			}

		    return '<b>' . implode(', ', $tag_labels) . '</b>';
		}

		/**
		 * Loads stylesheets
		 *
		 * @access  public
		 * @return  array Styles
		 */

		public function styles() {
            $styles = array(
                array("handle"  => "wpf_gforms_css",
                      "src"     => WPF_DIR_URL . 'assets/css/wpf-gforms.css',
                      "version" => $this->_version,
                      "enqueue" => array(
                          array( 'tab' => 'wpfgforms' )
                      )
                ),
                array("handle"  => "select4",
                      "src"     => WPF_DIR_URL . 'includes/admin/options/lib/select2/select4.min.css',
                      "version" => '4.0.1',
                      "enqueue" => array(
                          array( 'tab' => 'wpfgforms' )
                      )
                ),
                array("handle"  => "wpf-admin",
                      "src"     => WPF_DIR_URL . 'assets/css/wpf-admin.css',
                      "version" => $this->_version,
                      "enqueue" => array(
                          array( 'tab' => 'wpfgforms' )
                      )
                ),
            );

            return array_merge(parent::styles(), $styles);
        }

		/**
		 * Loads scripts
		 *
		 * @access  public
		 * @return  array Scripts
		 */

		public function scripts() {
			$scripts = array(
				array(
					'handle'  => 'select4',
					'src'     => WPF_DIR_URL . 'includes/admin/options/lib/select2/select4.min.js',
					'version' => '4.0.1',
					'deps'    => array( 'jquery' ),
					'enqueue' => array(
						array(
							'admin_page' => array( 'form_settings' ),
							'tab'        => 'wpfgforms',
						),
					),
				),
				array(
					'handle'  => 'wpf-admin',
					'src'     => WPF_DIR_URL . 'assets/js/wpf-admin.js',
					'version' => $this->_version,
					'deps'    => array( 'jquery', 'select4' ),
					'enqueue' => array(
						array(
							'admin_page' => array( 'form_settings' ),
							'tab'        => 'wpfgforms',
						),
					),
				),
			);
			return array_merge( parent::scripts(), $scripts );
		}

    }

    new WPF_GForms_Integration;

}