Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class GF_CampusVue {
- static $text_domain;
- static $admin_screen_id;
- static $defaults;
- static $add_scripts;
- /**
- * Initialise
- */
- public static function init() {
- global $wp_version;
- self::$defaults = array(
- 'x' => '',
- 'y' => array()
- );
- self::$text_domain = apply_filters( 'gf_cvue_text_domain', 'gf_cvue' );
- self::$admin_screen_id = apply_filters( 'gf_cvue_admin_screen_id', 'gf_cvue' );
- self::$defaults = apply_filters( 'gf_cvue_defaults', self::$defaults );
- add_action( 'init', array( __CLASS__, 'register' ) );
- add_filter( 'gform_add_field_buttons', __CLASS__ . '::gf_cvue_add_group' );
- add_filter( 'gform_field_type_title' , __CLASS__ . '::gf_cvue_custom_titles' );
- add_action( 'gform_field_input' , __CLASS__ . '::gf_cvue_state_field_input', 10, 5 );
- add_action( 'gform_field_input' , __CLASS__ . '::gf_cvue_country_field_input', 10, 5 );
- add_action( 'gform_field_input' , __CLASS__ . '::gf_cvue_campus_field_input', 10, 5 );
- add_action( 'gform_field_input' , __CLASS__ . '::gf_cvue_program_field_input', 10, 5 );
- add_action( 'gform_field_input' , __CLASS__ . '::gf_cvue_background_field_input', 10, 5 );
- add_action( 'gform_field_input' , __CLASS__ . '::gf_cvue_referral_field_input', 10, 5 );
- add_action( 'gform_editor_js' , __CLASS__ . '::gf_cvue_editor_js' );
- add_action( 'gform_advanced_settings' , __CLASS__ . '::gf_cvue_submit_checkbox', 10, 2 );
- add_action( 'gform_editor_js' , __CLASS__ . '::gf_cvue_form_script' );
- add_filter( 'gform_tooltips', __CLASS__ . '::gf_cvue_add_form_settings_tooltips' );
- add_action( 'gform_after_submission', __CLASS__ . '::gf_cvue_submit_to_cvue', 10, 2 );
- }
- /**
- * Register something
- */
- public static function register() {
- }
- /**
- * Add Campus View Grouping
- */
- public static function gf_cvue_add_group( $field_groups ){
- //var_dump($field_groups);
- $campus_vue_fields = array(
- array(
- "class"=>"button",
- "value" => __( "State" , "gravityforms" ),
- "onclick" => "StartAddField('state');"
- ),
- array(
- "class"=>"button",
- "value" => __( "Country" , "gravityforms" ),
- "onclick" => "StartAddField('country');"
- ),
- array(
- "class"=>"button",
- "value" => __( "Campus" , "gravityforms" ),
- "onclick" => "StartAddField('campus');"
- ),
- array(
- "class"=>"button",
- "value" => __( "Program" , "gravityforms" ),
- "onclick" => "StartAddField('program');"
- ),
- array(
- "class"=>"button",
- "value" => __( "Background" , "gravityforms" ),
- "onclick" => "StartAddField('background');"
- ),
- array(
- "class"=>"button",
- "value" => __( "Referral" , "gravityforms" ),
- "onclick" => "StartAddField('referral');"
- )
- );
- $field_groups[] = array(
- "name" => "campus_vue",
- "label"=> __( "Campus Vue" , self::$text_domain ),
- "fields" => $campus_vue_fields
- );
- return $field_groups;
- }
- /**
- * Modify Titles
- */
- public static function gf_cvue_custom_titles( $type ) {
- if ( $type == 'state' )
- return __( 'Select State' , self::$text_domain );
- if ( $type == 'country' )
- return __( 'Select Country' , self::$text_domain );
- if ( $type == 'campus' )
- return __( 'Select Campus' , self::$text_domain );
- if ( $type == 'background' )
- return __( 'Select Background' , self::$text_domain );
- if ( $type == 'program' )
- return __( 'Select Program' , self::$text_domain );
- if ( $type == 'referral' )
- return __( 'Select Referral Method' , self::$text_domain );
- }
- /**
- * State Select Box Field Input
- */
- public static function gf_cvue_state_field_input( $input, $field, $value, $lead_id, $form_id ){
- if ( $field["type"] == "state" ) {
- $disabled = '';
- if ( is_admin() )
- $disabled = "disabled='disabled'";
- $input_name = $form_id .'_' . $field["id"];
- $tabindex = GFCommon::get_tabindex();
- $css = isset( $field['cssClass'] ) ? $field['cssClass'] : '';
- $field_id = $field["id"];
- $css_id = 'state-'. $field['id'];
- $css_classes = $field["type"] . ' ' . esc_attr( $css ) . ' ' . $field['size'];
- $value_esc = esc_html($value);
- //Build field
- $input = "<div class='ginput_container'>";
- $input .= "<select name='input_" . $input_name . "' id='input_" . $css_id . "' class='medium gfield_select " . $css_classes . "' " . $tabindex . " " . $disabled . " >";
- $input .= "<option value='' >Please Select</option>";
- if ( !is_admin() ) {
- $cache = get_transient( 'gf_cvue_states' );
- $options = '';
- if ( false === $cache ) {
- $states = getList( 'State' );
- foreach( $states['State'] as $state ) {
- $options .= "<option value='" . $state->Id . "' >" . $state->Description . "</option>";
- }
- $input .= $options;
- set_transient( 'gf_cvue_states', $options , 60*60*12 );
- } else {
- $input .= $cache;
- }
- }
- $input .= "</select>";
- $input .= "</div>";
- }
- return $input;
- }
- /**
- * Country Select Box Field Input
- */
- public static function gf_cvue_country_field_input( $input, $field, $value, $lead_id, $form_id ){
- if ( $field["type"] == "country" ) {
- $disabled = '';
- if ( is_admin() )
- $disabled = "disabled='disabled'";
- $input_name = $form_id .'_' . $field["id"];
- $tabindex = GFCommon::get_tabindex();
- $css = isset( $field['cssClass'] ) ? $field['cssClass'] : '';
- $field_id = $field["id"];
- $css_id = 'country-'. $field['id'];
- $css_classes = $field["type"] . ' ' . esc_attr( $css ) . ' ' . $field['size'];
- $value_esc = esc_html($value);
- //Build field
- $input = "<div class='ginput_container'>";
- $input .= "<select name='input_" . $input_name . "' id='input_" . $css_id . "' class='medium gfield_select " . $css_classes . "' " . $tabindex . " " . $disabled . " >";
- $input .= "<option value='' >Please Select</option>";
- if ( !is_admin() ) {
- $cache = get_transient( 'gf_cvue_countries' );
- $options = '';
- if ( false === $cache ) {
- $countries = getList( 'Country' );
- foreach( $countries['Country'] as $country ) {
- $options .= "<option value='" . $country->Id . "' >" . $country->Description . "</option>";
- }
- $input .= $options;
- set_transient( 'gf_cvue_countries', $options, 60*60*12 );
- } else {
- $input .= $cache;
- }
- }
- $input .= "</select>";
- $input .= "</div>";
- }
- return $input;
- }
- /**
- * Campus Select Box Field Input
- */
- public static function gf_cvue_campus_field_input( $input, $field, $value, $lead_id, $form_id ){
- if ( $field["type"] == "campus" ) {
- $disabled = '';
- if ( is_admin() )
- $disabled = "disabled='disabled'";
- $input_name = $form_id .'_' . $field["id"];
- $tabindex = GFCommon::get_tabindex();
- $css = isset( $field['cssClass'] ) ? $field['cssClass'] : '';
- $field_id = $field["id"];
- $css_id = 'campus-'. $field['id'];
- $css_classes = $field["type"] . ' ' . esc_attr( $css ) . ' ' . $field['size'];
- $value_esc = esc_html($value);
- //Build field
- $input = "<div class='ginput_container'>";
- $input .= "<select name='input_" . $input_name . "' id='input_" . $css_id . "' class='medium gfield_select " . $css_classes . "' " . $tabindex . " " . $disabled . " >";
- $input .= "<option value='' >Please Select</option>";
- if ( !is_admin() ) {
- $cache = get_transient( 'gf_cvue_campuses' );
- if ( false === $cache ) {
- $campuses = getList( 'Campus' );
- $options = '';
- foreach( $campuses['Campus'] as $campus ) {
- $options .= "<option value='" . $campus->Id . "' >" . $campus->Description . "</option>";
- }
- $input .= $options;
- set_transient( 'gf_cvue_campuses', $options, 60*60*12 );
- } else {
- $input .= $cache;
- }
- }
- $input .= "</select>";
- $input .= "</div>";
- }
- return $input;
- }
- /**
- * Program Select Box Field Input
- */
- public static function gf_cvue_program_field_input( $input, $field, $value, $lead_id, $form_id ){
- if ( $field["type"] == "program" ) {
- $disabled = '';
- if ( is_admin() )
- $disabled = "disabled='disabled'";
- $input_name = $form_id .'_' . $field["id"];
- $tabindex = GFCommon::get_tabindex();
- $css = isset( $field['cssClass'] ) ? $field['cssClass'] : '';
- $field_id = $field["id"];
- $css_id = 'program-'. $field['id'];
- $css_classes = $field["type"] . ' ' . esc_attr( $css ) . ' ' . $field['size'];
- $value_esc = esc_html($value);
- //Build field
- $input = "<div class='ginput_container'>";
- $input .= "<select name='input_" . $input_name . "' id='input_" . $css_id . "' class='medium gfield_select " . $css_classes . "' " . $tabindex . " " . $disabled . " >";
- $input .= "<option value='' >Please Select</option>";
- if ( !is_admin() ) {
- $cache = get_transient( 'gf_cvue_programs' );
- if ( false === $cache ) {
- $programs = getList( 'AreaOfInterest' );
- $options = '';
- foreach( $programs['AreaOfInterest'] as $program ) {
- $options .= "<option value='" . $program->Id . "' >" . $program->Description . "</option>";
- }
- $input .= $options;
- set_transient( 'gf_cvue_programs', $options, 60*60*12 );
- } else {
- $input .= $cache;
- }
- }
- $input .= "</select>";
- $input .= "</div>";
- }
- return $input;
- }
- /**
- * Background Select Box Field Input
- */
- public static function gf_cvue_background_field_input( $input, $field, $value, $lead_id, $form_id ){
- if ( $field["type"] == "background" ) {
- $disabled = '';
- if ( is_admin() )
- $disabled = "disabled='disabled'";
- $input_name = $form_id .'_' . $field["id"];
- $tabindex = GFCommon::get_tabindex();
- $css = isset( $field['cssClass'] ) ? $field['cssClass'] : '';
- $field_id = $field["id"];
- $css_id = 'background-'. $field['id'];
- $css_classes = $field["type"] . ' ' . esc_attr( $css ) . ' ' . $field['size'];
- $value_esc = esc_html($value);
- //Build field
- $input = "<div class='ginput_container'>";
- $input .= "<select name='input_" . $input_name . "' id='input_" . $css_id . "' class='medium gfield_select " . $css_classes . "' " . $tabindex . " " . $disabled . " >";
- $input .= "<option value='' >Choose Educational Background</option>";
- if ( !is_admin() ) {
- $cache = get_transient( 'gf_cvue_backgrounds' );
- if ( false === $cache ) {
- $backgrounds = getList( 'PreviousEducation' );
- $options = '';
- foreach( $backgrounds['PreviousEducation'] as $background ) {
- $options .= "<option value='" . $background->Id . "' >" . $background->Description . "</option>";
- }
- $input .= $options;
- set_transient( 'gf_cvue_backgrounds', $options, 60*60*12 );
- } else {
- $input .= $cache;
- }
- }
- $input .= "</select>";
- $input .= "</div>";
- }
- return $input;
- }
- /**
- * Referral Select Box Field Input
- */
- public static function gf_cvue_referral_field_input( $input, $field, $value, $lead_id, $form_id ){
- if ( $field["type"] == "referral" ) {
- $disabled = '';
- if ( is_admin() )
- $disabled = "disabled='disabled'";
- $input_name = $form_id .'_' . $field["id"];
- $tabindex = GFCommon::get_tabindex();
- $css = isset( $field['cssClass'] ) ? $field['cssClass'] : '';
- $field_id = $field["id"];
- $css_id = 'referral-'. $field['id'];
- $css_classes = $field["type"] . ' ' . esc_attr( $css ) . ' ' . $field['size'];
- $value_esc = esc_html($value);
- //Build field
- $input = "<div class='ginput_container'>";
- $input .= "<select name='input_" . $input_name . "' id='input_" . $css_id . "' class='medium gfield_select " . $css_classes . "' " . $tabindex . " " . $disabled . " >";
- $input .= "<option value='' >Please Select</option>";
- if ( !is_admin() ) {
- $cache = get_transient( 'gf_cvue_referrals' );
- if ( false === $cache ) {
- $sources = getList( 'LeadSource' );
- $options = '';
- foreach( $sources['LeadSource'] as $source ) {
- $options .= "<option value='" . $source->Id . "' >" . $source->Description . "</option>";
- }
- $input .= $options;
- set_transient( 'gf_cvue_referrals', $options, 60*60*12 );
- } else {
- $input .= $cache;
- }
- }
- $input .= "</select>";
- $input .= "</div>";
- }
- return $input;
- }
- /**
- * Custom editor js for Campus View Input Fields
- */
- public static function gf_cvue_editor_js(){
- ?>
- <script type='text/javascript'>
- jQuery(document).ready(function($) {
- var input_fields = new Array( 'state', 'country', 'campus', 'background', 'program', 'referral' );
- var length = input_fields.length;
- for ( var i = 0; i < length; i++) {
- var field_name = input_fields[i];
- //set fields to appear
- fieldSettings[ field_name ] = ".label_setting, .description_setting, .admin_label_setting, .size_setting, .error_message_setting, .css_class_setting, .visibility_setting ";
- //binding to the load field settings event to initialize the checkbox
- $(document).bind("gform_load_field_settings", function(event, field, form){
- jQuery("#field_"+field_name).attr("checked", field["field_"+field_name] == true);
- $("#field_"+field_name+"_value").val(field[ field_name ]);
- });
- }
- });
- </script>
- <?php
- }
- /**
- * Add checkbox for selecting whether or not the form should submit to campusvue
- */
- function gf_cvue_submit_checkbox( $position, $form_id ){
- //create settings on position 50 (right after Admin Label)
- if( $position == 800 ){
- ?>
- <li class="campusvue_setting field_setting">
- <input type="checkbox" id="field_campusvue_value" /> <label for="field_campusvue_value">Submit to CampusVue <?php gform_tooltip('form_field_campusvue_value') ?></label>
- </li>
- <?php
- }
- }
- /**
- * Inject supporting script to the form editor page
- */
- function gf_cvue_form_script(){
- ?>
- <script type="text/javascript">
- jQuery("#field_campusvue_value").attr("checked", form.enableCvueSubmit ? true : false ).change( function() {
- form.enableCvueSubmit = jQuery("#field_campusvue_value").is(":checked");
- });
- form.enableCvueSubmit = jQuery("#field_campusvue_value").is(":checked");
- </script>
- <?php
- }
- /**
- * Add a new tooltip
- */
- function gf_cvue_add_form_settings_tooltips( $tooltips ){
- $tooltips['form_field_campusvue_value'] = "<h6>Submit to CampusVue</h6>Check this box to submit this form's data";
- return $tooltips;
- }
- /**
- * Submit form data to Campus Vue
- */
- function gf_cvue_submit_to_cvue( $entry, $form ) {
- if ( ! isset( $form['enableCvueSubmit'] ) || false == $form['enableCvueSubmit'] )
- return;
- $lead = new Lead();
- foreach ( $form['fields'] as $field ) {
- if ( is_array( $field['inputs'] ) ) {
- foreach ( $field['inputs'] as $input ) {
- switch ( strtolower( $input['label'] ) ) {
- case 'first' :
- $lead->FirstName = $entry[$input['id']];
- break 1;
- case 'last' :
- $lead->LastName = $entry[$input['id']];
- break 1;
- }
- }
- } else {
- switch ( $field['type'] ) {
- case 'state' :
- $lead->StateId = $_POST['input_' . $field['formId'] . '_' . $field['id']];
- break 1;
- case 'country' :
- $lead->CountryId = $_POST['input_' . $field['formId'] . '_' . $field['id']];
- break 1;
- case 'phone' :
- $lead->Phone = $entry[$field['id']];
- break 1;
- case 'email' :
- $lead->EMail = $entry[$field['id']];
- break 1;
- case 'campus' :
- $lead->CampusId = $_POST['input_3_' . $field['id']];
- break 1;
- case 'program' :
- $lead->AreaOfInterestId = $_POST['input_' . $field['formId'] . '_' . $field['id']];
- break 1;
- case 'background' :
- $lead->PreviousEducationId = $_POST['input_' . $field['formId'] . '_' . $field['id']];
- break 1;
- case 'referral' :
- $lead->LeadSourceId = $_POST['input_' . $field['formId'] . '_' . $field['id']];
- break 1;
- case 'text' :
- switch ( strtolower( $field['label'] ) ) {
- case 'address' :
- $lead->Address = $entry[$field['id']];
- break 1;
- case 'city' :
- $lead->City = $entry[$field['id']];
- break 1;
- case 'postcode' :
- $lead->PostalCode = $entry[$field['id']];
- break 1;
- }
- break 1;
- }
- }
- }
- //var_dump( $lead );
- error_log( '$lead = ' . print_r( $lead, true ) );
- addNewLead( $lead );
- var_dump( $res );
- }
- /**
- * Helper function to get the URL of a given file.
- *
- * As this plugin may be used as both a stand-alone plugin and as a submodule of
- * a theme, the standard WP API functions, like plugins_url() can not be used.
- *
- * @since 1.0
- * @return array $post_name => $post_content
- */
- public static function get_url( $file ) {
- // Get the path of this file after the WP content directory
- $post_content_path = substr( dirname( str_replace('\\','/',__FILE__) ), strpos( __FILE__, basename( WP_CONTENT_DIR ) ) + strlen( basename( WP_CONTENT_DIR ) ) );
- // Return a content URL for this path & the specified file
- return content_url( $post_content_path . $file );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement