Advertisement
sjolshagen

gform_hooks-01252012

Jan 25th, 2012
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.38 KB | None | 0 0
  1. add_filter("gform_pre_render_12", "populate_checkbox");
  2. add_filter("gform_admin_pre_render_12", "populate_checkbox");
  3. add_action("gform_after_submission_12", "saveToTrackerDB", 10, 2);
  4.  
  5. // Function to save the Challenge Tracker data to the database.
  6. function saveToTrackerDB($entry, $form)
  7. {
  8.     // Declare Variables
  9.     $numFieldID = 2;
  10.     $numNameEntry = 1;
  11.     $numDaysInMonth = date('t');
  12.     $numMonth = date('n');
  13.     global $wpdb;
  14.  
  15.     $FirstSQL = "";
  16.     $SecondSQL = "";
  17.  
  18.     $sqlStr = "SELECT * FROM ChallengeTracker WHERE ((strName='" . $entry[$numNameEntry] . "') AND (numMonth=" . $numMonth . "));";
  19.     // First, check if the entry (userID) already exists in the database
  20.     if ( ($dbResult = $wpdb->get_row($wpdb->prepare($sqlStr))) != null)
  21.     {
  22.         // It does so we'll UPDATE the existing record for this userID during this month.
  23.         $update = true;
  24.         $FirstSQL = "UPDATE ChallengeTracker SET ";
  25.         $SecondSQL = "";
  26.         $endSQL = " WHERE ((strName='". $entry[$numNameEntry] . "') AND (numMonth=" . $numMonth ."));";
  27.     } else {
  28.         // It doesn't so we'll need to INSERT (add) a new record.
  29.         $FirstSQL = "INSERT INTO ChallengeTracker (strName, numMonth, ";
  30.         $SecondSQL = " VALUES ('" . $entry[$numNameEntry] . "'," . $numMonth . ",";
  31.         $endSQL = ");";
  32.     }
  33.  
  34.     // Loop through all of the possible entries for the month and save the ones that have been
  35.     // "set" (checked).
  36.  
  37.     // Loops through all of the entry values
  38.     for ($i=1; $i<=$numDaysInMonth; $i++) {
  39.         // We only care about the Checkbox field(s)
  40.         if ($entry[$numFieldID . "." . $i] != null)
  41.         {
  42.             // Not an empty checkbox field
  43.             if ($update) {
  44.                 $FirstSQL .= "boolDay" . $i . "=" . $i . ",";
  45.             } else {
  46.                 $FirstSQL .= "boolDay" . $i . ",";
  47.                 $SecondSQL .= $i . ",";
  48.             }
  49.         } else {
  50.             if ($update) {
  51.                 $FirstSQL .= "boolDay" . $i . "=NULL,";
  52.             } else {
  53.                 $FirstSQL .= "boolDay" . $i . ",";
  54.                 $SecondSQL .= "NULL,";
  55.             }
  56.         }
  57.      };
  58.     // Close the parenthesis and trim off the last - incorrect - comma.
  59.     // Remove the comma from the last entry on the string(s)
  60.     $FirstSQL = rtrim($FirstSQL, ",");
  61.     $SecondSQL = rtrim($SecondSQL, ",");
  62.  
  63.     if ($update) {
  64.         $FirstSQL .= " ";
  65.     } else {
  66.         $FirstSQL .= ")";
  67.         $SecondSQL .= "";
  68.     } // End of if
  69.  
  70.     // Create the SQL statement
  71.     $sqlStr = $FirstSQL . $SecondSQL . $endSQL;
  72.  
  73.     // Add/Update the ChallengeTracker Table for this month/user
  74.     $wpdb->query($sqlStr);
  75.  
  76. } // End of function
  77.  
  78. // Function to actually populate the check-boxes
  79. function populate_checkbox($form)
  80. {
  81.     global $wpdb;
  82.     $strName = $_GET["jan_challenge_userid"];
  83.     $arrMonth = array("Jan", "Feb", "March", "April", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec");
  84.     $numMonth = date('n');
  85.     $numDay = date('j');
  86.     $numDaysInMonth = date('t');
  87.     $numMonthIndex = ($numMonth - 1);
  88.     $choices = array($numDaysInMonth);
  89.     $inputs = array($numDaysInMonth);
  90.     $numFieldID = 2;
  91.     $dbField = null;
  92.  
  93.     for ($i = 0; $i < $numDaysInMonth ; $i++)
  94.     {
  95.         $cnt = $i + 1;
  96.  
  97.         $sqlStr = "SELECT boolDay" . $cnt . " FROM ChallengeTracker WHERE ((strName='" . $strName . "') AND (numMonth=" . $numMonth . "));";
  98.         $dbResult = $wpdb->get_var( $wpdb->prepare( $sqlStr ) );
  99.  
  100.         if ($cnt == $numDay)
  101.         {
  102.             $strDayInfo = "<em>" . $arrMonth[$numMonthIndex] . " " . $cnt . "</em>";
  103.         } else {
  104.             $strDayInfo = $arrMonth[$numMonthIndex] . " " . $cnt;
  105.         }
  106.  
  107.         if ($dbResult != NULL)
  108.         {
  109.         $choices[$i] = array("text" => $strDayInfo, "value" => $cnt, "isSelected" => "1");
  110.         } else {
  111.             $choices[$i] = array("text" => $strDayInfo, "value" => $cnt);
  112.         }
  113.  
  114.         // Add inputs to array (create Inputs)
  115.         $inputs[$i] = array("label" => $arrMonth[$numMonthIndex] . " " . $cnt, "id" => $numFieldID . "." . $i);
  116.  
  117.     } // End of For Loop
  118.  
  119.     //Adding items to field id $numFieldID
  120.     foreach($form["fields"] as &$field) {
  121.         if($field["id"] == $numFieldID) {
  122.             $field["choices"] = $choices;
  123.             $field["inputs"] = $inputs;
  124.         }
  125.     }
  126.  
  127.     return $form;
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement