Lamplight API examples - work listing

Any data shown on this page is fictious - this is an example!

The example

This example requests published work records but uses a custom class to render dates in a format more to our liking.

The code

Again we're using the php Lamplight_Client class, which makes constructing requests a bit easier. First, set up the client

     $client = new Lamplight_Client('', array(
       'key'     => LAMPLIGHT_APIKEY,
       'lampid'  => LAMPLIGHT_ID,
       'project' => LAMPLIGHT_PROJECT
    ));   
    
    

This gives us an Http Client with the authentication parameters set (we use an include to define the LAMPLIGHT_APIKEY, LAMPLIGHT_ID and LAMPLIGHT_PROJECT constants). We don't need to pass a uri because the Lamplight_Client does it for us.

Next, tell it what we want to fetch. We want some (fetchSome()) work (fetchWork()) records, for the next week. Additional parameters use setParameterGet($key, $value) (as we're making a GET request, the default - setParameterPost for POST requests); we want records after today and before next week. Dates need to be in ISO 8601 YYYY-MM-DD format. And finally make the request(). Note that Lamplight_Client provides a fluent interface so that you can chain your method calls to build the request.

$orgResponse = $client->fetchWork()
                      ->fetchSome()
                      ->setParameterGet('after', date('Y-m-d'))
                      ->setParameterGet('before', date('Y-m-d', strtotime('+1 week')))
                      ->request();
                      
    

We're going to use a custom Record class to allow for nicer rendering of the start and end dates in templates. My_Record_WorkSummary, the class we're using is described in examples with the api docs. This class implements the renderField($fieldName) method, and renders dates more nicely. It looks like this:


class My_Record_WorkSummary extends Lamplight_Record_Abstract {

    public function renderField($field) {

        $val = $this->get($field);

        // date fields get rendered differently:
        if ($field == 'start_date' || $field == 'end_date') {

            return date('H:i, d/m/Y', strtotime($val));
      
        }
    
        // otherwise use the default renderer:
        return parent::renderField($field);
  
    }

}

    

So to use it, we pass the name of our custom class to the call to getRecordSet() and use a render template:

$recset = $client->getRecordSet('My_Record_WorkSummary');

if (!$recset->getErrors()) {

    $recordTemplate = '<li><h4>{title}</h4>'
         . '<i>Start date:</i> {start_date} <br/>'
         . '<i>End date:</i> {end_date} <br/>'
         . '<a href="workdetail.php?id={id}" title="see more info on {title}">'
         . 'more...</a>'
         . '</li>';
          
    echo '<ul>' . $recset->render($recordTemplate)  . '</ul>';

}
 
    

The call to render() on the RecordSet will iterate through the Records, which in this case will be instances of My_Record_WorkSummary. The template will be used to swap out placeholders with the return value of renderField, which in this case will reformat dates.