Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: Gravity Forms Injury Form
- Plugin URI: http://www.requincreative.com/
- Description: Adds Necessary functionality to Gravity Forms for Injury Calculations
- Author: Requin Creative Group
- Version: 1.0
- Author URI: http://www.requincreative.com
- Text Domain: gravity-forms-injury
- License: GPLv2 or later
- License URI: http://www.gnu.org/licenses/gpl-2.0.html
- Domain Path: /languages
- Copyright 2015 Requin Creative Group
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 3 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- register_activation_hook( __FILE__, 'gf_injury_install' );
- function gf_injury_install () {
- global $wpdb;
- $table_name = $wpdb->prefix . "injuries";
- global $wpdb;
- $charset_collate = $wpdb->get_charset_collate();
- $sql = "CREATE TABLE $table_name (
- id mediumint(9) NOT NULL AUTO_INCREMENT,
- name text NOT NULL,
- min varchar(55) NOT NULL,
- max varchar(55) NOT NULL,
- UNIQUE KEY id (id)
- ) $charset_collate;";
- require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
- dbDelta( $sql );
- }
- add_action('gform_after_submission_1', 'injury_add_entry_to_db', 10, 2);
- function injury_add_entry_to_db($entry, $form) {
- $lists = $entry[1];
- $lists = unserialize($lists);
- global $wpdb;
- $table = $wpdb->prefix.'injuries';
- foreach ($lists as $list) {
- $wpdb->insert(
- $table,
- array(
- 'name' => $list["Injury Name"],
- 'min' => $list["Minimum P&S"],
- 'max' => $list["Maximum P&S"],
- )
- );
- }
- }
- wp_enqueue_script('injury', plugins_url( "/js/custom.js", __FILE__), array('jquery'), '1.0', true);
- add_action("wp_ajax_get_injury_data", "get_injury_data_callback");
- add_action("wp_ajax_nopriv_get_injury_data", "get_injury_data_callback");
- // Dropdown A - Dealer Country
- add_filter("gform_pre_render_2", "dropdown_injury_list");
- //add_filter("gform_admin_pre_render", "dropdown_dealer_country");
- function dropdown_injury_list($form){
- if($form["id"] != 2)
- return $form;
- //$terms = get_terms("dealer-country");
- global $wpdb;
- $injuries = $wpdb->get_results( 'SELECT * FROM '.$wpdb->prefix.'injuries ORDER BY name ASC', ARRAY_A );
- $items = array();
- $items[] = array( "text" => 'Select an Injury', "value" => ' ' );
- foreach($injuries as $injury)
- $items[] = array( "text" => $injury['name'], "value" => $injury['id'] );
- foreach($form["fields"] as &$field){
- if($field["id"] == 2 ){
- $field["cssClass"] = 'injury-list';
- $field["choices"] = $items;
- }
- }
- return $form;
- }
- ?>
- <script>
- injuryFilter = function () {
- var injuryClass = ".injury-list select";
- var minClass = ".min-value input";
- var maxClass = ".max-value input";
- var results;
- jQuery(injuryClass).on("change", function () {
- var injurySelect = jQuery(this);
- injury = injurySelect.val();
- if (injury != '') {
- var data = {
- 'action': 'get_injury_data',
- 'id': injury
- };
- jQuery.post('<?php echo admin_url( 'admin-ajax.php' ); ?>', data, function(response) {
- console.log(response);
- });
- }
- });
- }
- </script>
- <?php
- function get_injury_data_callback() {
- global $wpdb;
- $id = $_POST['id'];
- $injuries = $wpdb->get_results( 'SELECT min, max FROM '.$wpdb->prefix.'injuries WHERE id='.$id, ARRAY_A );
- echo json_encode($injuries, true);
- wp_die();
- }
- ?>
- <?php
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement