Current Path :
/
home
/
zrcdevco
/
www
/
zrc-email-program
/
Or
Select Your Path :
Upload File :
New :
File
Dir
/home/zrcdevco/www/zrc-email-program/ZRCNewReedMailChimpNewABC.php
<?php // // A program to unzip a set of text files. Load the files into a database and then select the related fields into a single output file. // copywrite - Trionia, Inc. // author - James H. Murphy // date - 10:50 AM 4/20/2011 // update - 12/15/2014 // This update is to utilize the new Reeds Insight data structure and includes some feature updates. // These include selecting a date range and automatic download of the files from the Reed Server // Add a date range picker // - Final update step move FTP processing into a single loop // update - 4/23/2015 // Updated the query to include contacts associated with the project. // update - 1/19/2016 // Include timezone declaration to meet new requirements for date/time functions set_time_limit(100); error_reporting(E_ALL); date_default_timezone_set('America/New_York'); include('zrc_pdf_email.php'); include('zrc_ftp_files.php'); if (!(isset($_GET['startDate']) && isset($_GET['endDate']))) { ?> <form> Select a Start Date prior to the End Date. </br> Enter a start date: <input type="date" name="startDate"></br> Enter a end date: <input type="date" name="endDate"></br> SendFaxes:</br> <!--<input type="radio" name="sendFaxes" value="true" checked>Send Faxes --> </br> <input type="radio" name="sendFaxes" value="false">Test Mode - Do not Send Faxes </br> <input type='submit' name='Submit' value='submit'> </form> <?php } else { $startDate = $_GET['startDate']; $endDate = $_GET['endDate']; echo 'Start Date: '.$_GET['startDate'].'</br>'; echo 'End Date'.$_GET['endDate'].'</br>'; $sendFaxes = $_GET['sendFaxes']=='true'; $newStart = date("l jS \of F Y h:i:s A"); $newEnd = date_add(14 + days); echo $newStart; echo $newEnd; // Download Files from Reed FTP server //download_reed_files($startDate, $endDate); // Function to download the FTP files from the Reed Server function download_reed_files_xml($startDate, $endDate) { // Connect to FTP // $conn_id = ftp_connect('ftp.reeddatalink.com'); $conn_id = ftp_connect('ftp.cmdintegration.com'); // login with username and password $login_result = ftp_login($conn_id, 'zrcworldwide', 'ZrCW0rlD!'); echo "Login Results: $login_result\n"; $processDate = $startDate; while ($processDate <= $endDate) { echo 'Process Date: '.$processDate.'</br>'; echo "<br />Start\n"; echo date("l jS \of F Y h:i:s A") . "<br>"; // Reed Insight files contain a date stamp so new code will be required to // add the date stamp. // Use ftp to retrieve the files from the Reed Server // define some variables $file_date = str_replace('-','',$processDate); $zipfile_name = "1.3_DL_ZRCWorldwide_XML_" . $file_date . ".zip"; $local_file = $zipfile_name; $server_file = '/zrcworldwide_insight/'.$zipfile_name; // try to download $server_file and save to $local_file if (ftp_get($conn_id, $local_file, $server_file, FTP_BINARY)) { echo "Successfully written to $local_file\n"; } else { echo "There was a problem writing to $local_file\n"; } echo date("l jS \of F Y h:i:s A") . "<br>"; // increment process date $date = new DateTime($processDate); $date->add(new DateInterval('P1D')); $processDate = $date->format('Y-m-d'); echo date("l jS \of F Y h:i:s A") . "<br>"; } } download_reed_files_xml($startDate, $endDate); $firstPass = TRUE; $processDate = $startDate; // Connect to DB // Connect to database $conn = mysql_connect('localhost','zrcworld_m41lch1','z3L8;cZ^Oy4o'); if (!$conn) { die('Could not connect: ' . mysql_error()); } else { echo "<br />connect successful"; } if (mysql_select_db('zrcworld_trioniac',$conn)) { echo "Database Selected"; } else { echo "Error selecting database: " . mysql_error(); } // Set big selects for the following queries. $session_query = "SET SESSION SQL_BIG_SELECTS=1;"; mysql_query($session_query,$conn); if (mysql_errno()) { echo "Error session results: " . mysql_error(); } while ($processDate <= $endDate) { echo 'Process Date: '.$processDate.'</br>'; echo "<br />Start\n"; echo date("l jS \of F Y h:i:s A") . "<br>"; // Unzip the results from Reed Connect // Code taken from the website http://www.timlinden.com/blog/website-development/unzip-files-with-php/ // Reed Insight files contain a date stamp so new code will be required to // add the date stamp. // Use ftp to retrieve the files from the Reed Server // define some variables $file_date = str_replace('-','',$processDate); $zipfile_name = "1.3_DL_ZRCWorldwide_XML_" . $file_date . ".zip"; $file = "1.3_DL_ZRCWorldwide_XML_" . $file_date . ".xml"; echo date("l jS \of F Y h:i:s A") . "<br>"; $zip = zip_open( $zipfile_name ); $filesToProcess = array($file); if (is_resource($zip)) { while ($zip_entry = zip_read($zip)) { $fp = fopen(zip_entry_name($zip_entry), "w"); if (zip_entry_open($zip, $zip_entry, "r")) { if (in_array(zip_entry_name($zip_entry), $filesToProcess) ) { $buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)); fwrite($fp,"$buf"); // echo "<br />".zip_entry_name($zip_entry); zip_entry_close($zip_entry); fclose($fp); } } } zip_close($zip); } echo "<br />files unzipped"; echo date("l jS \of F Y h:i:s A") . "<br>"; //Pull the Do Not Fax list $exclude_numbers[] = ''; // Process each file foreach ($filesToProcess as $file_name) { if (file_exists($file_name)) { $xml = simplexml_load_file($file_name); $a=array("0"=>"Roles:"); $prim=array("0"=>"Primary contacts:"); $cemail=array("0"=>"Company emails:"); $fcn=array("0"=>"First contact name:"); foreach($xml->Project as $project1){ $projectname = (string) $project1->attributes()->Title; echo "<br />Projectname: <br />"; echo $projectname; foreach($project1->Companies->children() as $company){ // print_r($company); // The Attributes of the $company have to be cast as strings $participantRoles = (string) $company->ParticipantRoles->ParticipantRole['RoleClassification']; echo "<br />Participant Roles: <br />"; echo $participantRoles; array_push($a,"$participantRoles"); $primary = (string) $company->ParticipantRoles->ParticipantRole['ContactName']; echo "<br />Primary: <br />"; echo $primary; $companyEmail = (string) $company->Email; $companyName = (string) $company['Name']; echo "<br />Company Name: <br />"; echo $companyName; $first_contact = $company->Contacts->Contact[0]; $first_contactName = $first_contact['Name']; $first_contactEmail = $first_contact['Email']; if (preg_match("/(Architect|Structural|Contractor|Steel)/i",$participantRoles)) { echo "<br />In Architect Engineer Loop<br />"; if ($primary == $contact['Name']) { $name = $primary; } if (empty ($primary)) { $name = $companyName; $primary = "No primary"; } if (!empty($companyEmail)){ $email = $companyEmail; } if (empty($companyEmail)){ $email = $first_contactEmail; $name = $first_contactName; $companyEmail = "No cEmail"; } array_push($fcn,"$first_contactName"); array_push($prim,"$primary"); array_push($cemail,"$companyEmail"); foreach( $company->Contacts as $contacts ){ foreach( $contacts as $contact ){ echo "<br />Primary Contact<br />"; echo $primary; echo "<br />Contact to Update<br />"; echo $contact['Name']."<br />"; print_r($contact['Name']); echo "<br />Contact to Update<br />"; echo $contact['Name']; $email = (Array)$contact->Email; $email = $email['0']; $name = explode( ' ', $contact['Name']); $first = array_shift($name); $last = array_pop($name); $name_middle = trim(implode(' ', $name)); echo '<br />First: ' . $first . ', '; echo 'Last: ' . $last . ', '; echo 'Middle: ' . $name_middle . '.'; echo "<br />Projectname: <br />"; echo $projectname; //$first = $name['0']; //$last = $name['1']; $architects[] = array( 'email_address' => $email, 'status' => 'subscribed', 'merge_fields' => array( 'FNAME' => $first, 'LNAME' => $last, 'PNAME' => $projectname, 'COMPANY' => $companyName, 'ROLE' => $participantRoles), ); } } } } } $a = array_unique($a); print_r($a); print_r($prim); echo "<br /><br />"; print_r($cemail); echo "<br /><br />"; print_r($fcn); } else { exit('Failed to open test.xml.'. $file_name); } $file = fopen($file_name,"r"); $table_name = substr($file_name,0,strpos($file_name,'.csv')); // Read in the header line $line = fgets($file); // Remove all double quotes (") from the header line $line = str_replace('"','',$line); //print_r($line); $column_names = explode(',',$line); while (!feof($file)) { $data_line = fgets($file); //print_r( $data_line); // Enter in blank strings to insert in the DB $data_line = str_replace(', ,',', "",',$data_line); $data_line = str_replace(', ,',',"",',$data_line); // Check for comma as the last character in the string. If so add an empty string. // last comma is 4 characters from the end due to cr, lf and space characters. if (strrpos($data_line,',',0) == (strlen($data_line)-4)) $data_line .= '""'; } fclose($file); echo "<br />$file_name loaded"; echo date("l jS \of F Y h:i:s A") . "<br>"; } echo "<br />all data loaded"; echo date("l jS \of F Y h:i:s A") . "<br>"; echo date("l jS \of F Y h:i:s A") . "<br>"; $firstPass = False; // increment process date $date = new DateTime($processDate); $date->add(new DateInterval('P1D')); $processDate = $date->format('Y-m-d'); echo date("l jS \of F Y h:i:s A") . "<br>"; } fclose($outFileArchitect); echo "<br />Faxes Sent - Total $totalFaxesArchitect<br />"; // export results to a file for download echo "<br />data ready for download<br />"; echo "<a href='reed_merge_data_architects.csv'>Right Click and Save As for Reed Merge File</a>"; fclose($outFileBidder); echo "<br />Faxes Sent - Total $totalFaxesBidder<br />"; // export results to a file for download echo "<br />data ready for download<br />"; echo "<a href='reed_merge_data.csv'>Right Click and Save As for Reed Merge File</a>"; //output faxes sent to database for reporting /*$sql = "INSERT INTO FaxHistory (processdate, architectfaxes, bidderfaxes) VALUES ('".$processDate."','".$totalFaxesArchitect."','".$totalFaxesBidder."')"; if (mysql_query($sql, $conn)) { echo "History updated"; } else { echo "Error: " . $sql . "<br>" . mysql_error($conn); }*/ // close the FTP connection echo date("l jS \of F Y h:i:s A") . "<br>"; } // End While ?> <?php /* * Add a 'member' to a 'list' via mailchimp API v3.x * @ http://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/#create-post_lists_list_id_members * * ================ * BACKGROUND * Typical use case is that this code would get run by an .ajax() jQuery call or possibly a form action * The live data you need will get transferred via the global $_POST variable * That data must be put into an array with keys that match the mailchimp endpoints, check the above link for those * You also need to include your API key and list ID for this to work. * You'll just have to go get those and type them in here, see README.md * ================ */ // Set API Key and list ID to add a subscriber $api_key = '932cf48f8d91b5e3a9a30fab82867732-us16'; $list_id = '9e6a91943a'; /* ================ * DESTINATION URL * Note: your API URL has a location subdomain at the front of the URL string * It can vary depending on where you are in the world * To determine yours, check the last 3 digits of your API key * ================ */ $url = 'https://us16.api.mailchimp.com/3.0/lists/9e6a91943a/members'; /* ================ * DATA SETUP * Encode data into a format that the add subscriber mailchimp end point is looking for * Must include 'email_address' and 'status' * Statuses: pending = they get an email; subscribed = they don't get an email * Custom fields go into the 'merge_fields' as another array * More here: http://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/#create-post_lists_list_id_members * ================ */ /*$pfb_data = array( 'email_address' => $_POST['emailname'], 'status' => 'pending', 'merge_fields' => array( 'FNAME' => $_POST['firstname'], 'LNAME' => $_POST['lastname'], 'ZIPCODE' => $_POST['zipcode'] ),*/ if( !empty( $architects)){ foreach( $architects as $architect ){ $pfb_data = array( 'email_address' => $_POST['emailname'], 'status' => 'pending', 'merge_fields' => array( 'FNAME' => $_POST['firstname'], 'LNAME' => $_POST['lastname'], 'PNAME' => $_POST['projectname'], 'COMPANY' => $_POST['commpanyName'], 'ROLE' => $_POST['participantRoles'] ), ); print_r ($pfb_data); // Encode the data $encoded_pfb_data = json_encode($architect); // Setup cURL sequence $ch = curl_init(); /* ================ * cURL OPTIONS * The tricky one here is the _USERPWD - this is how you transfer the API key over * _RETURNTRANSFER allows us to get the response into a variable which is nice * This example just POSTs, we don't edit/modify - just a simple add to a list * _POSTFIELDS does the heavy lifting * _SSL_VERIFYPEER should probably be set but I didn't do it here * ================ */ curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $api_key); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_TIMEOUT, 10); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $encoded_pfb_data); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $results = curl_exec($ch); // store response $response = curl_getinfo($ch, CURLINFO_HTTP_CODE); // get HTTP CODE $errors = curl_error($ch); // store errors print_r( $results ); curl_close($ch); // Returns info back to jQuery .ajax or just outputs onto the page $results = array( 'results' => $result_info, 'response' => $response, 'errors' => $errors ); // Sends data back to the page OR the ajax() in your JS echo json_encode($results); } } ?>