Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_filter("gform_pre_render_12", "populate_checkbox");
- add_filter("gform_admin_pre_render_12", "populate_checkbox");
- add_action("gform_after_submission_12", "saveToTrackerDB", 10, 2);
- // Function to save the Challenge Tracker data to the database.
- function saveToTrackerDB($entry, $form)
- {
- // Declare Variables
- $numFieldID = 2;
- $numNameEntry = 1;
- $numDaysInMonth = date('t');
- $numMonth = date('n');
- global $wpdb;
- $FirstSQL = "";
- $SecondSQL = "";
- $sqlStr = "SELECT * FROM ChallengeTracker WHERE ((strName='" . $entry[$numNameEntry] . "') AND (numMonth=" . $numMonth . "));";
- // First, check if the entry (userID) already exists in the database
- if ( ($dbResult = $wpdb->get_row($wpdb->prepare($sqlStr))) != null)
- {
- // It does so we'll UPDATE the existing record for this userID during this month.
- $update = true;
- $FirstSQL = "UPDATE ChallengeTracker SET ";
- $SecondSQL = "";
- $endSQL = " WHERE ((strName='". $entry[$numNameEntry] . "') AND (numMonth=" . $numMonth ."));";
- } else {
- // It doesn't so we'll need to INSERT (add) a new record.
- $FirstSQL = "INSERT INTO ChallengeTracker (strName, numMonth, ";
- $SecondSQL = " VALUES ('" . $entry[$numNameEntry] . "'," . $numMonth . ",";
- $endSQL = ");";
- }
- // Loop through all of the possible entries for the month and save the ones that have been
- // "set" (checked).
- // Loops through all of the entry values
- for ($i=1; $i<=$numDaysInMonth; $i++) {
- // We only care about the Checkbox field(s)
- if ($entry[$numFieldID . "." . $i] != null)
- {
- // Not an empty checkbox field
- if ($update) {
- $FirstSQL .= "boolDay" . $i . "=" . $i . ",";
- } else {
- $FirstSQL .= "boolDay" . $i . ",";
- $SecondSQL .= $i . ",";
- }
- } else {
- if ($update) {
- $FirstSQL .= "boolDay" . $i . "=NULL,";
- } else {
- $FirstSQL .= "boolDay" . $i . ",";
- $SecondSQL .= "NULL,";
- }
- }
- };
- // Close the parenthesis and trim off the last - incorrect - comma.
- // Remove the comma from the last entry on the string(s)
- $FirstSQL = rtrim($FirstSQL, ",");
- $SecondSQL = rtrim($SecondSQL, ",");
- if ($update) {
- $FirstSQL .= " ";
- } else {
- $FirstSQL .= ")";
- $SecondSQL .= "";
- } // End of if
- // Create the SQL statement
- $sqlStr = $FirstSQL . $SecondSQL . $endSQL;
- // Add/Update the ChallengeTracker Table for this month/user
- $wpdb->query($sqlStr);
- } // End of function
- // Function to actually populate the check-boxes
- function populate_checkbox($form)
- {
- global $wpdb;
- $strName = $_GET["jan_challenge_userid"];
- $arrMonth = array("Jan", "Feb", "March", "April", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec");
- $numMonth = date('n');
- $numDay = date('j');
- $numDaysInMonth = date('t');
- $numMonthIndex = ($numMonth - 1);
- $choices = array($numDaysInMonth);
- $inputs = array($numDaysInMonth);
- $numFieldID = 2;
- $dbField = null;
- for ($i = 0; $i < $numDaysInMonth ; $i++)
- {
- $cnt = $i + 1;
- $sqlStr = "SELECT boolDay" . $cnt . " FROM ChallengeTracker WHERE ((strName='" . $strName . "') AND (numMonth=" . $numMonth . "));";
- $dbResult = $wpdb->get_var( $wpdb->prepare( $sqlStr ) );
- if ($cnt == $numDay)
- {
- $strDayInfo = "<em>" . $arrMonth[$numMonthIndex] . " " . $cnt . "</em>";
- } else {
- $strDayInfo = $arrMonth[$numMonthIndex] . " " . $cnt;
- }
- if ($dbResult != NULL)
- {
- $choices[$i] = array("text" => $strDayInfo, "value" => $cnt, "isSelected" => "1");
- } else {
- $choices[$i] = array("text" => $strDayInfo, "value" => $cnt);
- }
- // Add inputs to array (create Inputs)
- $inputs[$i] = array("label" => $arrMonth[$numMonthIndex] . " " . $cnt, "id" => $numFieldID . "." . $i);
- } // End of For Loop
- //Adding items to field id $numFieldID
- foreach($form["fields"] as &$field) {
- if($field["id"] == $numFieldID) {
- $field["choices"] = $choices;
- $field["inputs"] = $inputs;
- }
- }
- return $form;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement