Advertisement
Guest User

Untitled

a guest
Feb 6th, 2015
353
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.11 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name:    Gravity Forms Injury Form
  4. Plugin URI:     http://www.requincreative.com/
  5. Description:    Adds Necessary functionality to Gravity Forms for Injury Calculations
  6. Author:         Requin Creative Group
  7. Version:        1.0
  8. Author URI:     http://www.requincreative.com
  9. Text Domain:    gravity-forms-injury
  10. License:        GPLv2 or later
  11. License URI:    http://www.gnu.org/licenses/gpl-2.0.html
  12. Domain Path:    /languages
  13.  
  14. Copyright 2015 Requin Creative Group
  15.  
  16. This program is free software; you can redistribute it and/or modify
  17. it under the terms of the GNU General Public License as published by
  18. the Free Software Foundation; either version 3 of the License, or
  19. (at your option) any later version.
  20.  
  21. This program is distributed in the hope that it will be useful,
  22. but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  24. GNU General Public License for more details.
  25.  
  26. You should have received a copy of the GNU General Public License
  27. along with this program.  If not, see <http://www.gnu.org/licenses/>.
  28.  
  29. */
  30.  
  31. register_activation_hook( __FILE__, 'gf_injury_install' );
  32.  
  33.  
  34. function gf_injury_install () {
  35.    global $wpdb;
  36.  
  37.    $table_name = $wpdb->prefix . "injuries";
  38.    
  39.    global $wpdb;
  40.  
  41. $charset_collate = $wpdb->get_charset_collate();
  42.  
  43. $sql = "CREATE TABLE $table_name (
  44.  id mediumint(9) NOT NULL AUTO_INCREMENT,
  45.  name text NOT NULL,
  46.  min varchar(55) NOT NULL,
  47.  max varchar(55) NOT NULL,
  48.  UNIQUE KEY id (id)
  49. ) $charset_collate;";
  50.  
  51. require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
  52. dbDelta( $sql );
  53. }
  54.  
  55. add_action('gform_after_submission_1', 'injury_add_entry_to_db', 10, 2);
  56. function injury_add_entry_to_db($entry, $form) {
  57.  
  58.  
  59.       $lists = $entry[1];
  60.       $lists =  unserialize($lists);
  61.  
  62.  
  63.     global $wpdb;
  64.     $table = $wpdb->prefix.'injuries';
  65.  
  66.  
  67.     foreach ($lists as $list) {
  68.  
  69.             $wpdb->insert(
  70.                $table,
  71.                 array(
  72.                   'name' => $list["Injury Name"],
  73.                   'min' => $list["Minimum P&S"],
  74.                   'max' => $list["Maximum P&S"],
  75.                     )
  76.             );
  77.     }
  78.  
  79. }
  80.  
  81.  
  82.  
  83. wp_enqueue_script('injury', plugins_url( "/js/custom.js", __FILE__), array('jquery'), '1.0', true);
  84.  
  85.  
  86.  add_action("wp_ajax_get_injury_data", "get_injury_data_callback");
  87.  add_action("wp_ajax_nopriv_get_injury_data", "get_injury_data_callback");
  88.  
  89.     // Dropdown A - Dealer Country
  90. add_filter("gform_pre_render_2", "dropdown_injury_list");
  91. //add_filter("gform_admin_pre_render", "dropdown_dealer_country");
  92. function dropdown_injury_list($form){
  93.         if($form["id"] != 2)
  94.            return $form;
  95.        
  96.         //$terms = get_terms("dealer-country");
  97.     global $wpdb;
  98.         $injuries = $wpdb->get_results( 'SELECT * FROM '.$wpdb->prefix.'injuries ORDER BY name ASC', ARRAY_A );
  99.    
  100.         $items = array();
  101.         $items[] = array( "text" => 'Select an Injury', "value" => ' ' );
  102.         foreach($injuries as $injury)
  103.             $items[] = array( "text" => $injury['name'], "value" => $injury['id'] );
  104.         foreach($form["fields"] as &$field){
  105.             if($field["id"] == 2 ){
  106.                 $field["cssClass"] = 'injury-list';
  107.                 $field["choices"] = $items;
  108.             }  
  109.         }
  110.         return $form;  
  111.     }
  112.  
  113.    
  114.    
  115.    
  116.    
  117. ?>
  118.  
  119. <script>
  120.         injuryFilter = function () {
  121.  
  122. var injuryClass = ".injury-list select";
  123. var minClass = ".min-value input";
  124. var maxClass = ".max-value input";
  125. var results;
  126.  
  127.     jQuery(injuryClass).on("change", function () {
  128.     var injurySelect = jQuery(this);
  129.     injury = injurySelect.val();
  130.  
  131.             if (injury != '') {
  132.                         var data = {
  133.                     'action': 'get_injury_data',
  134.                     'id': injury
  135.                 };
  136.                        
  137.                     jQuery.post('<?php echo admin_url( 'admin-ajax.php' ); ?>', data, function(response) {
  138.                        
  139.                                                 console.log(response);
  140.                                                
  141.                    
  142.                     });
  143.                
  144.                 }
  145.     });
  146.        
  147.  
  148.  
  149. }
  150.  
  151. </script>
  152.  
  153. <?php
  154.  
  155. function get_injury_data_callback() {
  156.    global $wpdb;
  157.    $id = $_POST['id'];
  158.  
  159.      $injuries = $wpdb->get_results( 'SELECT min, max FROM '.$wpdb->prefix.'injuries WHERE id='.$id, ARRAY_A );
  160.    
  161.    echo json_encode($injuries, true);
  162.    
  163.    
  164. wp_die();
  165. }
  166.  
  167.  
  168. ?>
  169.  
  170.  
  171.  
  172. <?php
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement