Getting the listing of published records is covered in this example.
The difference this time is that we check whether it's possible to add attendees
for each record, by looking at the boolean property may_add_attend
of each record returned. If we can, we add a checkbox to each record listing:
if ($rec->may_add_attend) { echo ''; }
And we add a simple HTML email input and submit button at the bottom.
The Lamplight datain API accepts a comma-separated list of work record ids in the request to add attendees. So we process the POSTed data and use the Lamplight_Client to send the request.
// Get the work ids as a comma separated list: $wids = implode(",", array_keys($_POST['attend'])); // Set and send the request to add the person with email address // to the work records $client->attendWork($wids, $_POST['attendemail']) ->request();
When sending a 'batch add' request like this, Lamplight returns a status
for each record: it may be possible to add the person to some records
but not others. In this case we call getDatainResponse
, which
gives us a Lamplight_Datain_Response instance. This 'parent' response instance
in turn contains 'child' Lamplight_Datain_Response instances for each record
that you've tried to add them to. The parent implements Iterator
and has an isMultiple
method so that we can check if we've got
lots to look at.
$resp = $client->getDatainResponse(); if ($resp->success()) { echo 'Thanks, you are all booked in'; } else { echo 'Ooh, there was a problem: '; if ($resp->isMultiple()) { // Check the status of each addition: foreach ($resp as $r) { if ($r->success()) { echo '
Thanks, we booked you in to record ID ' . $r->getId(); } else { echo '
There was a problem with ' . $r->getId() . ': ' . $r->getErrorMessage(); } } } else { echo $resp->getErrorMessage(); } }
In the 'multiple' case, success()
returns true only if
everything was successful. If some added OK but others didn't, you can
iterate through the child instances and check for success on each of them.