Jump to content

  •  

Photo

Recording a timestamp when user edits a record


  • Please log in to reply
6 replies to this topic

#1 HazMatt  journeyman

HazMatt
  • Members
  • 245 posts
  • LocationMinnesota, USA
  • Skill Level:Intermediate
  • Certification:13
  • Membership:TechNet, FileMaker Business Alliance, FIleMaker Platinum Member
  • Time Online: 7h 22m 35s

Posted 05 October 2010 - 10:16 AM

I'm putting together a PHP based solution (largely generated from the FileMaker PHP Site Assistant) to allow customers easy access to their email marketing subscriptions. Here's an example link that the email recipient would click on to unsubscribe:
http://66.173.8.234/...aign=Newsletter

I'd like to record a timestamp when someone updates their subscriptions so I can pull a report in a date period (like between newsletters). The auto-enter modification timestamp works, but it doesn't target a specific field so I can't rely on that. Is there a way to trigger a script when the user hits the "Save" button?

I can post my source code if that would be helpful. Thanks!
  • 0

#2 HazMatt  journeyman

HazMatt
  • Members
  • 245 posts
  • LocationMinnesota, USA
  • Skill Level:Intermediate
  • Certification:13
  • Membership:TechNet, FileMaker Business Alliance, FIleMaker Platinum Member
  • Time Online: 7h 22m 35s

Posted 05 October 2010 - 07:43 PM

Source code for "browserecord.php"
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<?php

    /**
    * FileMaker PHP Site Assistant Generated File
    */

    require_once 'fmview.php';
    require_once 'FileMaker.php';
    require_once 'error.php';
	
	// Set find criteria
	$contact_id = $_GET['contact_id'];
	$email = $_GET['email'];
	$campaign = $_GET['campaign'];
	
    $cgi = new CGI();
    $cgi->storeFile();
    
    $databaseName = 'smpl_interface';
    $layoutName = 'OWN—Contact Unsubscribe (Web)';

    $userName = 'username';
    $passWord = 'password';

    $fm = & new FileMaker();
    $fm->setProperty('database', $databaseName);
    $fm->setProperty('username', $userName);
    $fm->setProperty('password', $passWord);
    
    ExitOnError($fm);
    $layout = $fm->getLayout($layoutName);
    ExitOnError($layout);

    // formats for dates and times
    $displayDateFormat = '%m/%d/%Y';
    $displayTimeFormat = '%I:%M:%S %P';
    $displayDateTimeFormat = '%m/%d/%Y %I:%M:%S %P';
    $submitDateOrder = 'mdy';

    $record = NULL;
    $response = NULL;
    $action = $cgi->get('-action');
    $recid = $cgi->get('-recid');

    switch ($action) {
        case "delete" <img src='http://fmforums.com/forum/public/style_emoticons/<#EMO_DIR#>/wink.gif' class='bbc_emoticon' alt=':' />
            {
                $pageRedirect = true;
                include "deleterecord.php";
                break;
            }
        case "duplicate" <img src='http://fmforums.com/forum/public/style_emoticons/<#EMO_DIR#>/crazy.gif' class='bbc_emoticon' alt=':' />
            {

                //  create a new duplicate command
                $duplicateCommand = $fm->newDuplicateCommand($layoutName, $recid);
                $result = $duplicateCommand->execute();
                ExitOnError($result);

                //  the result contains the record
                $records = $result->getRecords();
                $record = $records[0];

                //  store the recid so we can display the duplicated record in the browse page
                $cgi->store('-recid', $record->getRecordId());
                $recid = $cgi->get('-recid');
                break;
            }
        case "edit" <img src='http://fmforums.com/forum/public/style_emoticons/<#EMO_DIR#>/crazy.gif' class='bbc_emoticon' alt=':' />
            {
                $recid = $cgi->get('-recid');

                // create an edit command for the record specified by the '-recid'
                $editCommand = $fm->newEditCommand($layoutName, $recid);
                ExitOnError($editCommand);

                // get the posted record data from the edit page
                $recorddata = $cgi->get('recorddata');
                if (isset ($recorddata)) {
                    //The following block of code will only submit fields that have changed.
                    // Get the current record being edited. 
                    $currentRecord = $fm->getRecordById($layoutName, $recid);
                    // Get a list of fields on the layout.
                    $listofFields = $layout->listFields();
                    $counter = 0;
                    if($listofFields != null ){
                        // for each field check to see if the new value entered by the user is equivalent
                        // to the old value. If it is remove it from the array so it does not get committed. 
                        foreach ($listofFields as $aField){
                            $fieldObj = $currentRecord->getLayout()->getField($aField);
                            $resultType = $fieldObj->getResult();
                            $originalValue = $currentRecord->getField($aField);
       
                            if ($resultType == "date")
                                $originalValue = displayDate($originalValue, $displayDateFormat);
                            else if ($resultType == "time")
                                $originalValue = displayTime($originalValue, $displayTimeFormat);
                            else if ($resultType == "timestamp")
                                $originalValue = displayTimeStamp($originalValue, $displayDateTimeFormat);
                            
                            // The field's value is in the same position in the $recorddata array as the field's name is in the 
                            // $listofFields array.  
                            if(isset ( $recorddata[$counter])){ 
                                 if ( $originalValue === $recorddata[$counter] ){
                                        unset($recorddata[$counter]);
                                    }
                              $counter++;
                            }
                        }
                    }   
                
                    $fieldEditRecords = $cgi->get("fieldEditRecords");
                    
                    $onlyCheckBoxField = false;
                    if (sizeof($recorddata) == 0){
                        if(isset($fieldEditRecords[0]) && $fieldEditRecords[0]->isCheckBox()){
                            $onlyCheckBoxField = true;
                        }
                    }   
                
                    //After the proper set of fields have been identified submit the record data.
                    //This check is to ensure that if no fields have been modified code does not submit any changes. 
                    if( sizeof($recorddata) > 0 || $onlyCheckBoxField){
                    
                     //Convert any time/date formatted field 
                    if (isset($fieldEditRecords)) {
    
                        foreach ($recorddata as $field => $value) {
    
                            if(array_key_exists($field, $fieldEditRecords)){
                                $fieldEditRecord = $fieldEditRecords[$field];
                            }
    
                            if (is_null($fieldEditRecord) === false && $fieldEditRecord->isEditBox()){
                            
                                $resultType = $fieldEditRecord->getResultType();
                                        if ($resultType == "date")
                                            $value = submitDate($value, $displayDateFormat);
                                        else if ($resultType == "time")
                                            $value = submitTime($value);
                                        else if ($resultType == "timestamp")
                                            $value = submitTimeStamp($value, $displayDateFormat);
                                
                                     $recorddata[$field] =$value;
                             }
                         }
                     }
                    
                        $response = submitRecordData($recorddata, $editCommand, $cgi, $layout->listFields());
                        $cgi->clear('recorddata');
                        ExitOnError($response);
                        
                        $firstRecord = $response->getFirstRecord();
                        ExitOnError($firstRecord);
                        $cgi->store('-recid', $firstRecord->getRecordId());
                        $recid = $cgi->get('-recid');
            
        
                        // now get the record we just edited
                        $record = $fm->getRecordById($layoutName, $recid);
                    }else{
                        $recid = $cgi->get('-recid');
                        if (!isset ($recid))
                            $recid = 1;
                        $record = $fm->getRecordById($layoutName, $recid);
                        $cgi->clear('recorddata');
                        ExitOnError($record);
                        
                    }
                }   
                break;
            }
        case "new" <img src='http://fmforums.com/forum/public/style_emoticons/<#EMO_DIR#>/crazy.gif' class='bbc_emoticon' alt=':' />
            {

                // create the new add command
                $newrecordrequest = $fm->newAddCommand($layoutName);
                ExitOnError($newrecordrequest);

                // get the submitted record data
                $recorddata = $cgi->get('recorddata');
                if (isset ($recorddata)) {
                
                 //Convert any time/date formatted field 
                    $fieldEditRecords = $cgi->get("fieldEditRecords");
                    if (isset($fieldEditRecords)) {
    
                        foreach ($recorddata as $field => $value) {
    
                            if(array_key_exists($field, $fieldEditRecords)){
                                $fieldEditRecord = $fieldEditRecords[$field];
                            }
    
                            if (isset($fieldEditRecord) && is_null($fieldEditRecord) === false && $fieldEditRecord->isEditBox()){
                                $resultType = $fieldEditRecord->getResultType();
               
                                    if ($resultType == "date")
                                        $value = submitDate($value, $displayDateFormat);
                                    else if ($resultType == "time")
                                        $value = submitTime($value);
                                    else if ($resultType == "timestamp")
                                        $value = submitTimeStamp($value, $displayDateFormat);
                            
                                 $recorddata[$field] =$value;
                             }
                         }
                     }

                    //  submit the data to the db
                    $result = submitRecordData($recorddata, $newrecordrequest, $cgi, $layout->listFields());

                    //  clear the stored record data
                    $cgi->clear('recorddata');
                    
                    if( $result === NULL ){
                        DisplayErrorandExit("commit failed!");
                    }else{
                        ExitOnError($result);
                    }
                } else {
                    $findallCommand = $fm->newFindAllCommand($layoutName);
                    $result = $findallCommand->execute();
                }
                if ($result->getFetchCount() > 0) {
                    $records = $result->getRecords();
                    $record = $records[0];
                    $recid = $record->getRecordId();
                } else {
                    DisplayError('No Records are present.');
                }

                break;
            }
        case "browse" <img src='http://fmforums.com/forum/public/style_emoticons/<#EMO_DIR#>/crazy.gif' class='bbc_emoticon' alt=':' />
        default <img src='http://fmforums.com/forum/public/style_emoticons/<#EMO_DIR#>/crazy.gif' class='bbc_emoticon' alt=':' />
            {
				$recid = $cgi->get('-recid');
				if (!isset ($recid))
					$recid = 1;
				$record = $fm->getRecordById($layoutName, $recid);
				ExitOnError($record);
				break;				
            }
    }   
?>

<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Unsubscribe</title>
<style type="text/css">
<!--
body {
	font-family: Arial, Helvetica, sans-serif;
	font-size: 12px;
	color: #000000;
}

table.record {
	border: 1px solid #cccccc;
	background-color: #f9f8f8;
}

td.field_name {
	text-align: right;
	padding: 3px 0 3px 0;
	vertical-align: top;
}

td.field_data {
	text-align: left;
}

.edit_button {
	text-align: center;
}

-->
</style>
</head>

<body style="margin: 15px;">
<?php
	echo '<div style="float: left; background-color: #FFFF00; border: 1px solid #cccccc; padding: 5px; margin-bottom: 15px;">';
		echo '<strong>Debugger Window</strong>
';
		echo 'Record ID:  ' . $recid . '
';
		echo 'Contact ID:  ' . $contact_id . '
';
		echo 'Email Address:  ' . $email . '
';
		echo 'Campaign:  ' . $campaign;
	echo '</div>';
?>
<div style="clear: both;">
	<form method="post" action="editrecord.php">
		<input type="hidden" name="-db" value="<?php echo $databaseName ?>">
		<input type="hidden" name="-lay" value="<?php echo $layoutName ?>">
		<input type="hidden" name="-recid" value="<?php echo $recid ?>">
		<table cellpadding="1" cellspacing="6" class="record">
			<tr>
				<td class="field_name">Customer ID:</td>
				<td class="field_data"><?php echo nl2br(storeFieldNames('customer_id', 0, $record, true, 'EDITTEXT', 'number'))?></td>
			</tr>
			<tr>
				<td class="field_name">Name:</td>
				<td class="field_data"><?php echo nl2br(storeFieldNames('full_name', 0, $record, false, 'EDITTEXT', 'text'))?></td>
			</tr>
			<tr>
				<td class="field_name">Email Address:</td>
				<td class="field_data"><?php echo nl2br(storeFieldNames('email', 0, $record, true, 'EDITTEXT', 'text'))?></td>
			</tr>
			<tr>
				<td class="field_name">Monthly Newsletter:</td>
				<td class="field_data">
					<?php
						echo '<input type="checkbox" name="monthly_newsletter" value="1"';
						if (nl2br( $record->getField('newsletter_subscribed', 0))) { echo ' checked' ; }
						echo ' disabled';
					?>
				</td>
			</tr>
			<tr>
				<td class="field_name">Last Updated:</td>
				<td class="field_data"><?php echo displayTimeStamp(storeFieldNames('newsletter_subscription_last_updated', 0, $record, true, 'EDITTEXT', 'timestamp'), $displayDateTimeFormat)?></td>
			</tr>
			<tr class="edit_button">
				<td colspan="2"><input type="submit" name="-find" value="Edit Subscriptions"></td>
			</tr>
		</table>
	</form>
</div>
</body>
</html>


  • 0

#3 HazMatt  journeyman

HazMatt
  • Members
  • 245 posts
  • LocationMinnesota, USA
  • Skill Level:Intermediate
  • Certification:13
  • Membership:TechNet, FileMaker Business Alliance, FIleMaker Platinum Member
  • Time Online: 7h 22m 35s

Posted 05 October 2010 - 07:47 PM

Source code for "editrecord.php"
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<?php

    /**
    * FileMaker PHP Site Assistant Generated File
    */

    require_once 'fmview.php';
    require_once 'FileMaker.php';
    require_once 'error.php';

    $cgi = new CGI();
    $cgi->storeFile();
    
    $databaseName = 'smpl_interface';
    $layoutName = 'OWN—Contact Unsubscribe (Web)';

    $userName = 'username';
    $passWord = 'password';

    $fm = & new FileMaker();
    $fm->setProperty('database', $databaseName);
    $fm->setProperty('username', $userName);
    $fm->setProperty('password', $passWord);
    
    ExitOnError($fm);
    $layout = $fm->getLayout($layoutName);
    ExitOnError($layout);

    // formats for dates and times
    $displayDateFormat = '%m/%d/%Y';
    $displayTimeFormat = '%I:%M:%S %P';
    $displayDateTimeFormat = '%m/%d/%Y %I:%M:%S %P';
    $submitDateOrder = 'mdy';

    $recid = $cgi->get('-recid');
    if (!isset($recid))
        $recid = 1;
    $record = $fm->getRecordById($layoutName, $recid);
    ExitOnError($record);
    
    class EmptyRecord {
        function getRelatedSet($relationName) {
            return array(new EmptyRecord());
        }

        function getField($field, $repetition = 0) {
        }

        function getRecordId() {
        }
    }
	
	// Retrieve contact record specifics
    $contact_id = $cgi->get('contact_id');
    $email = $cgi->get('email');
    $campaign = $cgi->get('campaign');
    
?>

<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Unsubscribe</title>
<style type="text/css">
<!--
body {
	font-family: Arial, Helvetica, sans-serif;
	font-size: 12px;
	color: #000000;
}

table.record {
	border: 1px solid #cccccc;
	background-color: #f9f8f8;
}

td.field_name {
	text-align: right;
	padding: 3px 0 3px 0;
	vertical-align: top;
}

td.field_data {
	text-align: left;
}

.edit_button {
	text-align: center;
}

-->
</style>
</head>

<body style="margin: 15px;">
<?php
	echo '<div style="float: left; background-color: #FFFF00; border: 1px solid #cccccc; padding: 5px; margin-bottom: 15px;">';
		echo '<strong>Debugger Window</strong>
';
		echo 'Record ID:  ' . $recid . '
';
		echo 'Contact ID:  ' . $contact_id . '
';
		echo 'Email Address:  ' . $email . '
';
		echo 'Campaign:  ' . $campaign;
	echo '</div>';
?>
<div style="clear: both;">
	<form method="post" action="browserecord.php">
		<input type="hidden" name="-db" value="<?php echo $databaseName ?>">
		<input type="hidden" name="-lay" value="<?php echo $layoutName ?>">
		<input type="hidden" name="-recid" value="<?php echo $recid ?>">
		<input type="hidden" name="-action" value="edit">
		<table cellpadding="1" cellspacing="6" class="record">
			<tr>
				<td class="field_name">Customer ID:</td>
				<td class="field_data"><?php echo nl2br( $record->getField('customer_id', 0) ) ?></td>
			</tr>
			<tr>
				<td class="field_name">Name:</td>
				<td class="field_data"><?php echo nl2br( $record->getField('full_name', 0) )?></td>
			</tr>
			<tr>
				<td class="field_name">Email Address:</td>
				<td class="field_data"><?php echo nl2br( $record->getField('email', 0) )?></td>
			</tr>
			<tr>
				<td class="field_name">Monthly Newsletter:</td>
				<td class="field_data">
					<?php $fieldValue = $record->getField('newsletter_subscribed', 0) ; ?>
					<?php getInputChoices("checkbox", $layout->getValueListTwoFields('Checkbox', (isset($master_record)) ? $master_record->getRecordId() <img src='http://fmforums.com/forum/public/style_emoticons/<#EMO_DIR#>/wink.gif' class='bbc_emoticon' alt=':' /> $record->getRecordId()), $fieldValue, getFieldFormName('newsletter_subscribed', 0, $record, true, 'CHECKBOX', 'number'), 'number', $submitDateOrder);?>
				</td>
			</tr>
			<tr>
				<td class="field_name">Last Updated:</td>
				<td class="field_data"><?php echo displayTimeStamp( $record->getField('newsletter_subscription_last_updated', 0) , $displayDateTimeFormat) ?></td>
			</tr>
			<tr class="edit_button">
				<td colspan="2">
					<input type="submit" name="-edit" value="Save">
					<input type="reset" name="reset" value="Reset">
					<input type="button" onclick='<?php echo ("window.location="browserecord.php?-action=browse&-recid=$recid&contact_id=$contact_id&email=$email&campaign=$campaign"") ?>' name="cancel" value="Cancel">
				</td>
			</tr>
		</table>
	</form>
</div>
</body>
</html>


  • 0

#4 HazMatt  journeyman

HazMatt
  • Members
  • 245 posts
  • LocationMinnesota, USA
  • Skill Level:Intermediate
  • Certification:13
  • Membership:TechNet, FileMaker Business Alliance, FIleMaker Platinum Member
  • Time Online: 7h 22m 35s

Posted 06 October 2010 - 07:20 AM

I found my solution. :
http://filemakeraddi...ields-from.html
  • 0

#5 almedajohnson  novice

almedajohnson
  • Members
  • 15 posts
  • FM Application:4
  • :

Posted 12 October 2010 - 08:59 PM

So finally what is the scene?? Have you got the solution?? If yes then just close this thread and if not then tell us your problem.
  • 0

#6 HazMatt  journeyman

HazMatt
  • Members
  • 245 posts
  • LocationMinnesota, USA
  • Skill Level:Intermediate
  • Certification:13
  • Membership:TechNet, FileMaker Business Alliance, FIleMaker Platinum Member
  • Time Online: 7h 22m 35s

Posted 13 October 2010 - 05:15 AM

Well the solution is in the link I posted here, but if you need it spelled out, here's the exact code I used:

//  Record timestamp (http://filemakeraddict.blogspot.com/2009/07/setting-filemaker-timestamp-fields-from.html)
$user_update_request = $fm->newEditCommand($layoutName, $recid);
$user_update_request->setField('subscriptions_last_updated', date("m/d/Y h:i:s A"));
$user_update_result = $user_update_request->execute();

And I'm pretty sure you can't 'close a thread' on FM Forums…
  • 0

#7 bcooney  consultant

bcooney
  • Moderators
  • 5,791 posts
  • LocationLong Island, NY
  • FM Application:13 Advance
  • FMGo:iPad
  • Platform:Cross Platform
  • Skill Level:Expert
  • Certification:9, 10, 11, 12
  • Membership:TechNet
  • Time Online: 24d 3h 24m 50s

Posted 13 October 2010 - 05:27 AM

Nice of you to post the code.

I'm watching Almeda, however, as s/he's posted several times without really offering any input, perhaps in an attempt to "share" the link to her website in her signature.
  • 0




FMForum Advertisers