Current Path :
/
home
/
zrcdevco
/
www
/
zrc-email-program
/
Or
Select Your Path :
Upload File :
New :
File
Dir
/home/zrcdevco/www/zrc-email-program/ZRCNewReedMailChimp4.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'; // 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); foreach($xml->Project as $project1){ $projectname = $project1->attributes()->Title; echo "<br />Projectname: <br />"; echo $projectname; $participantRoles = $project1->Companies->Company->ParticipantRoles->ParticipantRole->attributes()->RoleClassification; echo "<br />Participant Roles: <br />"; echo $participantRoles; $primary = $project1->Companies->Company->ParticipantRoles->ParticipantRole->attributes()->ContactName; echo "<br />Primary: <br />"; echo $primary; $contactName = $project1->Companies->Company->Contacts->Contact->attributes()->Name; echo "<br />Name: <br />"; echo ($contactName); foreach($project1->Companies as $company){ foreach( $company as $comp){ $i = 0; $architects [$i++] = array(); if( $comp['Role'] == "Architect" ){ foreach( $comp->Contacts as $contacts ){ foreach( $contacts as $contact ){ if( !empty( $contact->Email )){ $email = (Array)$contact->Email; $email = $email['0']; $name = explode( ' ', $contact['Name']); $first = $name['0']; $last = $name['1']; $architects[] = array( 'email_address' => $email, 'status' => 'subscribed', 'merge_fields' => array( 'FNAME' => $first, 'LNAME' => $last ), ); } } } } } } } print_r( $architects ); } 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 ?>