Guest User

Untitled

a guest
Apr 23rd, 2014
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 27.27 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Google Docs Guestlist
  4. Version: 2.0
  5. */
  6.  
  7. /* Zend Gdata classes */
  8. $path = dirname(__FILE__);
  9. set_include_path(get_include_path() . PATH_SEPARATOR . $path);
  10.  
  11. require_once 'Zend/Loader.php';
  12. Zend_Loader::loadClass('Zend_Http_Client');
  13. Zend_Loader::loadClass('Zend_Gdata');
  14. Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
  15. Zend_Loader::loadClass('Zend_Gdata_Spreadsheets');
  16.  
  17.  
  18. function admin_wpgc_guestlist_options(){
  19.     ?><div class="wrap"><h2>GoogleDocs Guestlist</h2><?php
  20.         if ($_REQUEST['submit']){
  21.             update_wpgc_guestlist_options();
  22.         }
  23.         print_wpgc_guestlist_form();
  24.  
  25.         ?></div><?php
  26. }
  27.  
  28. function update_wpgc_guestlist_options() {
  29.     $ok = false;
  30.  
  31.         if($_REQUEST['wpgc_guestlist_google_username']) {
  32.             update_option('wpgc_guestlist_google_username',$_REQUEST['wpgc_guestlist_google_username']);
  33.                 $ok = true;
  34.         }
  35.         if($_REQUEST['wpgc_guestlist_google_password']) {
  36.             update_option('wpgc_guestlist_google_password',$_REQUEST['wpgc_guestlist_google_password']);
  37.                 $ok = true;
  38.         }
  39.         if($_REQUEST['wpgc_guestlist_google_spreadsheet_name']) {
  40.             update_option('wpgc_guestlist_google_spreadsheet_name',$_REQUEST['wpgc_guestlist_google_spreadsheet_name']);
  41.                 $ok = true;
  42.         }
  43.         if($_REQUEST['wpgc_guestlist_google_worksheet_name']) {
  44.             update_option('wpgc_guestlist_google_worksheet_name',$_REQUEST['wpgc_guestlist_google_worksheet_name']);
  45.                 $ok = true;
  46.         }
  47.         if($_REQUEST['wpgc_guestlist_wedding_planner_email_address']) {
  48.             update_option('wpgc_guestlist_wedding_planner_email_address',$_REQUEST['wpgc_guestlist_wedding_planner_email_address']);
  49.                 $ok = true;
  50.         }
  51.         if($_REQUEST['wpgc_guestlist_wedding_planner']) {
  52.             update_option('wpgc_guestlist_wedding_planner',$_REQUEST['wpgc_guestlist_wedding_planner']);
  53.                 $ok = true;
  54.         }
  55.         if($ok) {
  56.             ?><div id="message" class="updated fade">
  57.             <p>Options saved.</p>
  58.                 </div><?php
  59.         }
  60.         else {
  61.             ?><div id="message" class="error fade">
  62.             <p>Failed to save options.</p>
  63.                 </div><?php
  64.         }
  65. }
  66.  
  67. function print_wpgc_guestlist_form() {
  68.         $default_username = get_option('wpgc_guestlist_google_username');
  69.         $default_password = get_option('wpgc_guestlist_google_password');
  70.         $default_spreadsheet = get_option('wpgc_guestlist_google_spreadsheet_name');
  71.         $default_worksheet = get_option('wpgc_guestlist_google_worksheet_name');
  72.         $default_email_address = get_option('wpgc_guestlist_wedding_planner_email_address');
  73.         $default_wedding_planner = get_option('wpgc_guestlist_wedding_planner');
  74.         ?>
  75.  
  76.        
  77.         <form method="post">
  78.             <label for="wpgc_guestlist_google_username">Google Username:
  79.                 <input type="text" name="wpgc_guestlist_google_username" value="<?=$default_username?>" />
  80.                 </label><i>Your Google account username with access to wedding spreadsheet</i>
  81.                 <br />
  82.                 <br />
  83.                 <label for="wpgc_guestlist_google_password">Google Password:
  84.                 <input type="password" name="wpgc_guestlist_google_password" value="<?=$default_password?>" />
  85.                 </label><i>Your Google password</i>
  86.                 <br />
  87.                 <br />
  88.                 <label for="wpgc_guestlist_google_spreadsheet_name">Google Spreadsheet Name:
  89.                 <input type="text" name="wpgc_guestlist_google_spreadsheet_name" value="<?=$default_spreadsheet?>" />
  90.                 </label><i>The name of your spreadsheet</i>
  91.                 <br />
  92.                 <br />
  93.                 <label for="wpgc_guestlist_google_worksheet_name">Google Worksheet Name:
  94.                 <input type="text" name="wpgc_guestlist_google_worksheet_name" value="<?=$default_worksheet?>" />
  95.                 </label><i>The name of your worksheet(e.g. Sheet1)</i>
  96.                 <br />
  97.                 <br />
  98.                 <label for="wpgc_guestlist_wedding_planner_email_address">Wedding Planner Email Address:
  99.                 <input type="text" name="wpgc_guestlist_wedding_planner_email_address" value="<?=$default_email_address?>" />
  100.                 </label><i>This account will be emailed every time there is a new RSVP</i>
  101.                 <br />
  102.                 <br />
  103.                  <label for="wpgc_guestlist_wedding_planner">Wedding Planner Name:
  104.                 <input type="text" name="wpgc_guestlist_wedding_planner" value="<?=$default_wedding_planner?>" />
  105.                 </label><i>Name of person to contact directly for questions, confirmations, or problems... (e.g. Bernard and Alice)</i>
  106.                 <br />
  107.                 <br />
  108.                 <br />
  109.                 <input type="submit" name="submit" value="Submit" />
  110.     </form>
  111.         <?php
  112. }
  113.  
  114. function modify_menu() {
  115.     add_options_page(
  116.                 'GoogleDocs Guestlist', //page title
  117.                         'GoogleDocs Guestlist', //subpage title
  118.                         'manage_options',   //access
  119.                         '__FILE__',     //current file
  120.                         'admin_wpgc_guestlist_options'  //options function above
  121.                         );
  122. }
  123.  
  124. /*
  125. Function: wpgc_clean
  126. Meant to clean up user input.... watching out for google docs injection attacks???? shrug
  127. */
  128. function wpgc_clean( $value , $strip = true) {
  129.     return $value;
  130.         //if ($strip) return strip_tags($value);
  131.         //else return $value;
  132. }
  133.  
  134. /*
  135. Function: wpgc_connect
  136.  
  137. Helper function to connect to the Google docs spreadsheet.
  138. returns the google docs client object.
  139.  
  140. */
  141. function wpgc_connect_get_client($user, $pass) {
  142.     // Connect to your account
  143.     $authService = Zend_Gdata_Spreadsheets::AUTH_SERVICE_NAME;
  144.     $httpClient = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $authService);
  145.     $gdClient = new Zend_Gdata_Spreadsheets($httpClient);
  146.    
  147.     return $gdClient;
  148. }
  149.  
  150.  
  151.  
  152. /*
  153. Function: wpgc_get_listFeed_for_guestcode
  154.  
  155. This is a helper function. It connects to the Google docs spreadsheet, finds the right worksheet, and finds only the rows that have $guest_code as its guest code.
  156. TODO: Order the entries by some order
  157.  
  158. */
  159. function wpgc_get_listFeed_for_guestcode($gdClient, $spreadsheet_name,$worksheet_name, $guest_code) {
  160.     // Some variables
  161.    
  162.     $spreadsheet_key = '';
  163.     $worksheet_key = '';
  164.    
  165.    
  166.     // Find your spreadsheet id
  167.     $feed = $gdClient->getSpreadsheetFeed('http://spreadsheets.google.com/feeds/spreadsheets/private/full');
  168.    
  169.     foreach($feed->entries as $entry) {
  170.         if ($entry->title->text == $spreadsheet_name) {
  171.             $id = split('/', $entry->id->text);
  172.             $spreadsheet_key = $id[7];
  173.            
  174.         }
  175.     }
  176.  
  177.     // Connect to your spreadsheet
  178.         $query = new Zend_Gdata_Spreadsheets_DocumentQuery();
  179.         $query->setSpreadsheetKey($spreadsheet_key);
  180.        
  181.         $feed = $gdClient->getWorksheetFeed($query);//LINE AT FAULT
  182.        
  183.     // Find your worksheet id that matches your title (hopefully a unique one)
  184.         $i = 0;
  185.         foreach($feed->entries as $entry) {
  186.             if (
  187.                 (empty($worksheet_name) && $i == 00) ||
  188.                 (!empty($worksheet_name) && strcmp($entry->title->text,$worksheet_name)==0 )
  189.             ) {
  190.                 $id = split('/', $entry->id->text);
  191.                 $worksheet_key = $id[8];
  192.             }
  193.             $i++;
  194.         }
  195.  
  196.     // Run Query
  197.         $query = new Zend_Gdata_Spreadsheets_ListQuery();
  198.         $query->setSpreadsheetKey($spreadsheet_key);
  199.         $query->setWorksheetId($worksheet_key);
  200.         $query->setSpreadsheetQuery('code="'.$guest_code.'"'); // TODO: INJECTION threats?
  201.         return $gdClient->getListFeed($query);
  202. }
  203.  
  204. /*
  205. Name: wpgc_parse_entry
  206.  
  207. Extract the values we care about from a listfeedentry
  208.  
  209. */
  210. function wpgc_parse_entry($listFeedEntry) {
  211.     $onerow_data = $listFeedEntry->getCustom();
  212.     foreach($onerow_data as $column) {
  213.         switch ($column->getColumnName()) {
  214.             case ("title"):
  215.                 $entry["title"] = $column->getText();
  216.             break;
  217.             case ("firstname"):
  218.                 $entry["firstname"] = $column->getText();
  219.             break;
  220.             case ("surname"):
  221.                 $entry["surname"] = $column->getText();
  222.             break;
  223.             case ("address1"):
  224.                 $entry["address1"] = $column->getText();
  225.             break;
  226.             case ("address2"):
  227.                 $entry["address2"] = $column->getText();
  228.             break;
  229.             case ("address3"):
  230.                 $entry["address3"] = $column->getText();
  231.             break;
  232.             case ("city"):
  233.                 $entry["city"] = $column->getText();
  234.             break;
  235.             case ("county"):
  236.                 $entry["county"] = $column->getText();
  237.             break;
  238.             case ("postcode"):
  239.                 $entry["postcode"] = $column->getText();
  240.             break;
  241.             case ("country"):
  242.                 $entry["country"] = $column->getText();
  243.             break;
  244.             case ("email"):
  245.                 $entry["email"] = $column->getText();
  246.             break;
  247.             case ("telephone1"):
  248.                 $entry["telephone1"] = $column->getText();
  249.             break;
  250.             case ("telephone2"):
  251.                 $entry["telephone2"] = $column->getText();
  252.             break;
  253.             case ("language"):
  254.                 $entry["language"] = $column->getText();
  255.             break;
  256.             case ("invitedbr"):
  257.                 $entry["invitedbr"] = $column->getText();
  258.             break;
  259.             case ("attendingbr"):
  260.                 $entry["attendingbr"] = $column->getText();
  261.             break;
  262.             case ("inviteduk"):
  263.                 $entry["inviteduk"] = $column->getText();
  264.             break;
  265.             case ("attendinguk"):
  266.                 $entry["attendinguk"] = $column->getText();
  267.             break;
  268.             case ("messageforguest"):
  269.                 $entry["messageforguest"] = $column->getText();
  270.             break;
  271.             case ("messagefromguest"):
  272.                 $entry["messagefromguest"] = $column->getText();
  273.             break;
  274.             case ("dietaryrequirements"):
  275.                 $entry["dietaryrequirements"] = $column->getText();
  276.             break;
  277.             case ("kidsmeal"):
  278.                 $entry["kidsmeal"] = $column->getText();
  279.             break;
  280.         }
  281.     }
  282. return $entry;
  283. }
  284.  
  285. function wpgc_my_googledocsguestlist ($text) {
  286.     //QUIT if the replacement string doesn't exist
  287.     if (!strstr($text,'wpgc-googledocsguestlist')) return $text;
  288.  
  289.  
  290.         // Key variables
  291.         $user = get_option('wpgc_guestlist_google_username');
  292.         $pass = get_option('wpgc_guestlist_google_password');
  293.         $spreadsheet_name = get_option('wpgc_guestlist_google_spreadsheet_name');
  294.         $worksheet_name = get_option('wpgc_guestlist_google_worksheet_name');
  295.         $wedding_planner_email_address = get_option('wpgc_guestlist_wedding_planner_email_address');
  296.         $wedding_planner = get_option('wpgc_guestlist_wedding_planner');
  297.  
  298.  
  299.  
  300.         //Configuration check
  301.         if (!$user || !$pass || !$spreadsheet_name || !$worksheet_name || !$wedding_planner_email_address) {
  302.             return "This plugin has not been fully configured.  Please fill out all the entries under Settings->GoogleDocs Guestlist.";
  303.         }
  304.  
  305.     $spreadsheet_key = '';
  306.     $worksheet_key = '';
  307.     $guest_code = $_POST['code'];
  308.     $outputtext = '';
  309.     $abort_and_reprint_form = false;
  310.  
  311.     // Login
  312.     // if you have a  code and NO answers, pass
  313.     // if you have a  code, but already answered, no pass
  314.     // if you have bad code, no pass
  315.    
  316.     if (isset($_POST['motion'])) {
  317.     switch ($_POST['motion']) {
  318.         case ('update'):
  319.             // (A) Save to the database
  320.             try {
  321.                 // Connect and retrieve the listFeed for your guestcode
  322.                 $gdClient = wpgc_connect_get_client($user, $pass);
  323.                 $listFeed = wpgc_get_listFeed_for_guestcode($gdClient, $spreadsheet_name, $worksheet_name, $guest_code);
  324.                
  325.                 // CHECK, did they already fill out the form?
  326.                 foreach ($listFeed->entries as $e) {
  327.                     $entry = wpgc_parse_entry($e);
  328.                     if ($entry['attendingbr'] || $entry['attendinguk']) {
  329.                         $abort_and_reprint_form = true;
  330.                         break;
  331.                     }
  332.                 }
  333.                 if ($abort_and_reprint_form) break;
  334.                 $messagefromguest = '';
  335.                 if (isset($_POST['messagefromguest'])) {
  336.                     $messagefromguest = wpgc_clean($_POST['messagefromguest']);
  337.                 }
  338.                 $attendingbr_attendees = array();          
  339.                 $attendinguk_attendees = array();
  340.                 $your_message = '';
  341.                
  342.                 if ($listFeed->count() == 0) {
  343.                     $abort_and_reprint_form = true;
  344.                     break;
  345.                 }
  346.                 // Construct
  347.                 for ($i=0; $i < $listFeed->count(); $i+=1) {
  348.                     // Grab the data from the http post and construct the array
  349.                     $newentry = array();                   
  350.                     if (isset($_POST['title'.$i])) {
  351.                         $newentry["title"] = stripslashes($_POST['title'.$i]);
  352.                     }                  
  353.                     if (isset($_POST['firstname'.$i])) {
  354.                         $newentry["firstname"] = stripslashes($_POST['firstname'.$i]);
  355.                     }                  
  356.                     if (isset($_POST['surname'.$i])) {
  357.                         $newentry["surname"] = stripslashes($_POST['surname'.$i]);
  358.                     }                  
  359.                     if (isset($_POST['address1'.$i])) {
  360.                         $newentry["address1"] = stripslashes($_POST['address1'.$i]);
  361.                     }                  
  362.                     if (isset($_POST['address2'.$i])) {
  363.                         $newentry["address2"] = stripslashes($_POST['address2'.$i]);
  364.                     }                  
  365.                     if (isset($_POST['address3'.$i])) {
  366.                         $newentry["address3"] = stripslashes($_POST['address3'.$i]);
  367.                     }                  
  368.                     if (isset($_POST['city'.$i])) {
  369.                         $newentry["cit"] = stripslashes($_POST['city'.$i]);
  370.                     }                  
  371.                     if (isset($_POST['county'.$i])) {
  372.                         $newentry["county"] = stripslashes($_POST['county'.$i]);
  373.                     }                  
  374.                     if (isset($_POST['country'.$i])) {
  375.                         $newentry["country"] = stripslashes($_POST['country'.$i]);
  376.                     }                  
  377.                     if (isset($_POST['postcode'.$i])) {
  378.                         $newentry["postcode"] = stripslashes($_POST['postcode'.$i]);
  379.                     }                  
  380.                     if (isset($_POST['email'.$i])) {
  381.                         $newentry["email"] = stripslashes($_POST['email'.$i]);
  382.                     }                  
  383.                     if (isset($_POST['telephone1'.$i])) {
  384.                         $newentry["telephone1"] = stripslashes($_POST['telephone1'.$i]);
  385.                     }                  
  386.                     if (isset($_POST['telephone2'.$i])) {
  387.                         $newentry["telephone2"] = stripslashes($_POST['telephone2'.$i]);
  388.                     }                  
  389.                     if (isset($_POST['language'.$i])) {
  390.                         $newentry["language"] = stripslashes($_POST['language'.$i]);
  391.                     }                                      
  392.                     if (isset($_POST['invitedbr'.$i])) {
  393.                         $newentry["invitedbr"] = stripslashes($_POST['invitedbr'.$i]);
  394.                     }
  395.                     if (isset($_POST['attendingbr'.$i])) {
  396.                         $newentry["attendingbr"] = stripslashes($_POST['attendingbr'.$i]);
  397.                     }                  
  398.                     if (isset($_POST['inviteduk'.$i])) {
  399.                         $newentry["inviteduk"] = stripslashes($_POST['inviteduk'.$i]);
  400.                     }
  401.                     if (isset($_POST['attendinguk'.$i])) {
  402.                         $newentry["attendinguk"] = stripslashes($_POST['attendinguk'.$i]);
  403.                     }
  404.                     if (isset($_POST['messageforguest'.$i])) {
  405.                         $newentry["messageforguest"] = stripslashes($_POST['messageforguest'.$i]);
  406.                     }
  407.                     if (isset($_POST['messagefromguest'.$i])) {
  408.                         $newentry["messagefromguest"] = stripslashes($_POST['messagefromguest'.$i]);
  409.                     }                  
  410.                     if (isset($_POST['dietryrequirements'.$i])) {
  411.                         $newentry["dietryrequirements"] = stripslashes($_POST['dietryrequirements'.$i]);
  412.                     }                  
  413.                     if (isset($_POST['kidsmeal'.$i])) {
  414.                         $newentry["kidsmeal"] = stripslashes($_POST['kidsmeal'.$i]);
  415.                     }
  416.                     // for the comments from the quest, we'll only plug it into the first entry
  417.                     if ($i == 0) {
  418.                         $newentry["messagefromguest"] = stripslashes($messagefromguest);
  419.                     }
  420.                     // guest_code
  421.                     $newentry["code"] = $guest_code;
  422.                    
  423.                     // GO! Update the spreadsheet!
  424.                     // What if it fails? I suppose an exception gets thrown...
  425.                     $entry = $gdClient->updateRow($listFeed->entries[$i], $newentry);  
  426.                     /*if !($entry instanceof Zend_Gdata_Spreadsheets_ListEntry) {
  427.                         //echo "Fail"
  428.                         throw new Something Exception
  429.                     }
  430.                     */
  431.                    
  432.                     $checked_entry = wpgc_parse_entry($entry);
  433.                     if ($i == 0) {
  434.                         $your_message = $checked_entry["messagefromguest"];
  435.                     }
  436.                     if (strcmp($checked_entry["attendingbr"],"Attending")==0) {
  437.                         $attendingbr_attendees[] = $checked_entry["firstname"] . " " . $checked_entry["surname"];
  438.                     }
  439.                     if (strcmp($checked_entry["attendinguk"],"Attending")==0) {
  440.                         $attendinguk_attendees[] = $checked_entry["firstname"] . " " . $checked_entry["surname"];
  441.                     }
  442.                 }
  443.  
  444.                 // (B) Give user confirmation
  445.                 $emailreport = "NEW RSVP! \n";
  446.                 $outputtext .= '<b>Thank you for your response!</b><br/>';
  447.                 $outputtext .= "<br/>\n";
  448.                 $plural = '';
  449.                 if (sizeof($attendingbr_attendees) != 1) $plural = "s";
  450.                 $outputtext .= '<b>'.sizeof($attendingbr_attendees).'</b> guest'.$plural.' will be attending the BR Wedding';
  451.                 $emailreport .= sizeof($attendingbr_attendees).' guest'.$plural.' will be attending the BR Wedding';
  452.                
  453.                 if (sizeof($attendingbr_attendees)) {
  454.                     $outputtext .= ":<br/>";
  455.                     $emailreport .= ":\n";
  456.                 } else {
  457.                     $outputtext .=".<br/>";
  458.                     $emailreport .=".\n";
  459.                 }
  460.                
  461.                 for ($i=0; $i<sizeof($attendingbr_attendees); $i+=1) {
  462.                     if (empty($attendingbr_attendees[$i])) $attendingbr_attendees[$i] = "Guest";
  463.                     $outputtext .= htmlspecialchars($attendingbr_attendees[$i],ENT_QUOTES);
  464.                     $emailreport .= htmlspecialchars($attendingbr_attendees[$i],ENT_QUOTES);
  465.                    
  466.                     if ($i != sizeof($attendingbr_attendees)-1) {
  467.                         $outputtext .= ", ";
  468.                         $emailreport .= ", ";
  469.                     }
  470.                 }
  471.                 if (sizeof($attendingbr_attendees)) {
  472.                     $outputtext .= "<br/>\n";
  473.                     $emailreport .= "\n";
  474.                 }
  475.                 $outputtext .= "<br/>\n";
  476.                 $emailreport .= "\n";
  477.  
  478.                 $plural = '';
  479.                 if (sizeof($attendinguk_attendees) != 1) $plural = "s";
  480.                 $outputtext .= '<b>'.sizeof($attendinguk_attendees).'</b> guest'.$plural.' will be attending the UK Wedding';
  481.                 $emailreport .= sizeof($attendinguk_attendees).' guest'.$plural.' will be attending the UK Wedding';
  482.  
  483.                 if (sizeof($attendinguk_attendees)) {
  484.                     $outputtext .= ":<br/>";
  485.                     $emailreport .= ":\n";
  486.                 } else {
  487.                     $outputtext .=".<br/>";
  488.                     $emailreport .=".\n";
  489.                 }
  490.                
  491.                 for ($i=0; $i<sizeof($attendinguk_attendees); $i+=1) {
  492.                     if (empty($attendinguk_attendees[$i])) $attendinguk_attendees[$i] = "Guest";
  493.                     $outputtext .= htmlspecialchars($attendinguk_attendees[$i],ENT_QUOTES);
  494.                     $emailreport .= htmlspecialchars($attendinguk_attendees[$i],ENT_QUOTES);
  495.                     if ($i != sizeof($attendinguk_attendees)-1) {
  496.                         $outputtext .= ", ";
  497.                         $emailreport .= ", ";
  498.                     }
  499.                 }
  500.                
  501.                 if (sizeof($attendinguk_attendees)) {
  502.                     $outputtext .= "<br/>\n";
  503.                     $emailreport .="\n";
  504.                 }
  505.                 $outputtext .= "<br/>\n";
  506.                 $emailreport .= "\n";
  507.  
  508.                 if (!empty($your_message)) {
  509.                     $outputtext .= "Your message will be delivered:<br/><blockquote>";
  510.                     $outputtext .=  htmlspecialchars(stripslashes($your_message),ENT_QUOTES);
  511.                     $outputtext .= "</blockquote><br/>";
  512.                     $emailreport .= "They also wrote a message for you:\n";
  513.                     $emailreport .=  stripslashes($your_message);
  514.                     $emailreport .= "\n";
  515.                 }
  516.  
  517.                 $outputtext .= "<i>Thank you! Your response has been saved and your invitation code has been used. If you need to change your reply or have any questions, please contact " . $wedding_planner . " at <a href='". $wedding_planner_email_address ."'>". $wedding_planner_email_address ."</a>.</i>";
  518.  
  519.                 $subject = "Wedding RSVP";
  520.                 $headers = 'From: '.$wedding_planner_email_address;
  521.                 mail($wedding_planner_email_address, $subject, $emailreport, $headers);
  522.  
  523.             } catch (Zend_Gdata_App_Exception $e) {
  524.                 $outputtext .= "<b>Oops, there was a small glitch.</b> Please try again, or contact ". $wedding_planner ." directly.";
  525.                 //$outputtext .= $e->getMessage() . " " . $e->getTraceAsString();
  526.             }
  527.         break; 
  528.        
  529.         case('edit'):
  530.         try {
  531.         // Connect and retrieve the listFeed for your guestcode
  532.             $gdClient = wpgc_connect_get_client($user, $pass);
  533.             $listFeed = wpgc_get_listFeed_for_guestcode($gdClient, $spreadsheet_name, $worksheet_name, $guest_code);
  534.  
  535.  
  536.             $listFeed = wpgc_get_listFeed_for_guestcode($gdClient, $spreadsheet_name, $worksheet_name, $guest_code);
  537.            
  538.             $already_replied = false;
  539.             // CHECK, did they already fill out the form?
  540.             foreach ($listFeed->entries as $e) {
  541.                 $entry = wpgc_parse_entry($e);
  542.                 if ($entry['attendingbr'] || $entry['attendinguk']) {
  543.                     $already_replied = true;
  544.                     break;
  545.                 }
  546.             }
  547.  
  548.             // Careful... entries might be an empty set
  549.             if ($listFeed->count() > 0 && !$already_replied) {
  550.                 // SUCCESS: We found a party
  551.                 $plural = '';
  552.                 if ($listFeed->count() != 1) $plural = "s";
  553.                 $outputtext .= "Thank you for replying online. We look forward to celebrating with you all! Please indicate whether each member of your party will be attending our wedding day events.<br/><br/><b>We have reserved ".$listFeed->count()." seat".$plural." in your honor.</b><br/>";
  554.                 // Initializing the form. And yes, it is one massive form for the whole page. We differentiate among the attendees by adding an index number after each input 'name', e.g. guestname3
  555.                 $outputtext .= '<form style="text-align: left" action="'.get_permalink().'" method="post">';
  556.  
  557.                 // We use a for loop here because the index i is important to keep as a row identifier when we update the row information later.
  558.                 for ($i = 0; $i < $listFeed->count(); $i +=1) {
  559.                     $entry = wpgc_parse_entry($listFeed->entries[$i]);
  560.  
  561.                     // Populating the sub-parts of the form for each guest
  562.                     $outputtext .= '<b>'.($i+1).'. Title:</b> <input name="Title'.$i.'" type="text" value="'. htmlspecialchars($entry["title"],ENT_QUOTES).'"/>';
  563.                     $outputtext .= "<br/>";
  564.                     $outputtext .= '<b>'.($i+1).'. First Name:</b> <input name="FirstName'.$i.'" type="text" value="'. htmlspecialchars($entry["firstname"],ENT_QUOTES).'"/>';
  565.                     $outputtext .= "<br/>";
  566.                     $outputtext .= '<b>'.($i+1).'. Surname:</b> <input name="Surname'.$i.'" type="text" value="'. htmlspecialchars($entry["surname"],ENT_QUOTES).'"/>';
  567.                     $outputtext .= "<br/>";
  568.                     $outputtext .= '<i>Attending BR:</i> <input name="attendingbr'.$i.'" type="radio" value="Attending" ';
  569.                     if (strcmp($entry["attendingbr"],"Attending")==0)
  570.                         $outputtext .= "checked='checked' ";
  571.                     $outputtext .= "/>Attending   ";
  572.                     $outputtext .= '<input name="attendingbr'.$i.'" type="radio" value="Not Attending" ';
  573.                     if (strcmp($entry["attendingbr"],"Not Attending")==0)
  574.                         $outputtext .= 'checked="checked" ';
  575.                     $outputtext .= "/>Not Attending";
  576.                     $outputtext .= "<br/>";
  577.                     $outputtext .= '<i>Attending UK:</i> <input name="attendinguk'.$i.'" type="radio" value="Attending" ';
  578.                     if (strcmp($entry["attendinguk"],"Attending")==0)
  579.                         $outputtext .= "checked='checked' ";
  580.                     $outputtext .= "/>Attending   ";
  581.                     $outputtext .= '<input name="attendinguk'.$i.'" type="radio" value="Not Attending" ';
  582.                     if (strcmp($entry["attendinguk"],"Not Attending")==0)
  583.                         $outputtext .= "checked='checked' ";
  584.                     $outputtext .= "/>Not Attending";
  585.                     $outputtext .= "<br/>\n";
  586.                     // Message for guest
  587.                     if (!empty($entry["messageforguest"])) {
  588.                         $outputtext .= '<blockquote>'.htmlspecialchars($entry["messageforguest"],ENT_QUOTES).'</blockquote>';
  589.                     }
  590.                     $outputtext .= '<input type="hidden" name="messageforguest'.$i.'" value="'.htmlspecialchars($entry["messageforguest"]).'"/>';
  591.  
  592.                     $outputtext .= "<br/>\n";              
  593.                     $outputtext .= "<br/>\n";
  594.                 }
  595.  
  596.                 // Message from the Guests
  597.                 $outputtext .= "<b>Let us know how you're doing. Leave us a note!</b><br/>\n";
  598.                 $outputtext .= "<textarea name='messagefromguest'></textarea>"; // Note that old comments are not retrieved. We assume that you will only submit an RSVP once.
  599.                 $outputtext .= "<br/>\n";
  600.  
  601.                 // Closing the form
  602.                 $outputtext .= "\n";
  603.                 $outputtext .= "<input type='hidden' name='motion' value='update'/>";
  604.                 $outputtext .= "<input type='hidden' name='code' value='". htmlspecialchars($guest_code,ENT_QUOTES)."'/>";
  605.                 $outputtext .= "<br/>\n";
  606.                 $outputtext .= "<button type='Submit'>Send my RSVP</button>";
  607.                 $outputtext .= " <a href='".get_permalink()."'>Cancel, I will reply later.</a>";
  608.                 $outputtext .= "</form>";
  609.             } else {
  610.                 $outputtext .= "<b>No match.</b> Please try again. <i>If you have already RSVP'd, your code will no longer work. If you would like to change your response, or if you were not able to respond in the first place, please contact " . $wedding_planner . " at <a href='". $wedding_planner_email_address ."'>". $wedding_planner_email_address ."</a>.</i><br />";
  611.                 $outputtext .= "<br/>\n";
  612.                 $outputtext .= 'Please type in the code from your wedding invitation below:<br/>';
  613.                 $outputtext .= '<form style="text-align: left" action="'.get_permalink().'" method="post">';
  614.                 $outputtext .= '<input type="hidden" name="motion" value="edit"/>';
  615.                 $outputtext .= '<input type="text" name="code" /> ';
  616.                 $outputtext .= '<button type="Submit">Submit</button>';
  617.                 $outputtext .= '</form>';
  618.             }
  619.  
  620.         } catch (Zend_Gdata_App_Exception $e) {
  621.             $outputtext .= "Oops. You found an error. Please try again or contact " . $wedding_planner . " at <a href='". $wedding_planner_email_address ."'>". $wedding_planner_email_address ."</a> to confirm your response.";
  622.             //$outputtext .= "[There was a error. Please consult the source code or an experienced programmer. :( ]";
  623.             //$outputtext .= $e->getMessage() . " " . $e->getTraceAsString(); //PL DEBUG
  624.            
  625.             //detailed debug output below
  626. /*              $rtn = "";
  627.                 $count = 0;
  628.                 foreach ($e->getTrace() as $frame) {
  629.                     $args = "";
  630.                     if (isset($frame['args'])) {
  631.                         $args = array();
  632.                         foreach ($frame['args'] as $arg) {
  633.                             if (is_string($arg)) {
  634.                                 $args[] = "'" . $arg . "'";
  635.                             } elseif (is_array($arg)) {
  636.                                 $args[] = "Array";
  637.                             } elseif (is_null($arg)) {
  638.                                 $args[] = 'NULL';
  639.                             } elseif (is_bool($arg)) {
  640.                                 $args[] = ($arg) ? "true" : "false";
  641.                             } elseif (is_object($arg)) {
  642.                                 $args[] = get_class($arg);
  643.                             } elseif (is_resource($arg)) {
  644.                                 $args[] = get_resource_type($arg);
  645.                             } else {
  646.                                 $args[] = $arg;
  647.                             }  
  648.                         }  
  649.                         $args = join(", ", $args);
  650.                     }
  651.                     $rtn .= sprintf( "#%s %s(%s): %s(%s)\n",
  652.                                              $count,
  653.                                              $frame['file'],
  654.                                              $frame['line'],
  655.                                              $frame['function'],
  656.                                              $args );
  657.                     $count++;
  658.                 }
  659.                 return $rtn;
  660.             echo $rtn;*/
  661.             }
  662.        
  663.         break;
  664.     } //endswitch on post motion  
  665.     } //endif (isset)
  666.     else {
  667.        
  668.         $outputtext .= "Please type in the code from your wedding invitation below:<br/>";
  669.         $outputtext .= "<form style='text-align: left' action='".get_permalink()."' method='post'>";
  670.         $outputtext .= "<input type='hidden' name='motion' value='edit'/>";
  671.         $outputtext .= "<input type='text' name='code' value=\"1\" /> ";
  672.         $outputtext .= "<button type='Submit'>Submit</button>";
  673.         $outputtext .= "</form>";
  674.        
  675.     }
  676.    
  677.     // this is covers cases where someone re-posted an update ... it defends against attacks where someone is spamming the update and trying to overwrite entries
  678.     // TODO This currently covers the case where someone has naively click back and resubmitted a form
  679.     // TODO I haven't checked whether an attacker can use the feedback to determine which codes are valid.
  680.     if ($abort_and_reprint_form) {
  681.         $outputtext .= "<b>Sorry, our records were not changed.</b>";
  682.         $outputtext .= "Please type in the code from your wedding invitation below:<br/>";
  683.         $outputtext .= "<form style='text-align: left' action='".get_permalink()."' method='post'>";
  684.         $outputtext .= "<input type='hidden' name='motion' value='edit'/>";
  685.         $outputtext .= "<input type='text' name='code'/> ";
  686.         $outputtext .= "<button type='Submit'>Submit</button>";
  687.         $outputtext .= "</form>";
  688.     }
  689.    
  690.     $text = str_replace('wpgc-googledocsguestlist', $outputtext, $text);
  691.     return $text;
  692. }
  693. add_action('admin_menu', 'modify_menu');
  694. add_filter('the_content', 'wpgc_my_googledocsguestlist');
  695. ?>
Advertisement
Add Comment
Please, Sign In to add comment