Lamplight API examples - creating profiles

Introduction

Version 1.2 of the Lamplight API allows you to create and edit profiles for people or organisations. This example shows you how to create a new profile.

Example

If you would like to access our services, please enter your details here and we'll be in touch as soon as possible.





The code

To create a new profile, we will use our Lamplight_Client to generate requests and handle responses, and the Lamplight_Record_People or Lamplight_Record_Orgs classes to hold our data. You will need v1.2 of the Lamplight php client code, available to download.

The basic steps to create a new profile are:

  1. Instantiate a new Lamplight_Record_People or Lamplight_Record_Orgs object
  2. Validate the data sent from the html form, and set() it
  3. Use the Lamplight_Client to save() it

The data is collected using a standard html form. When it is POSTed back, we set up the appropriate class:


    switch ($_POST['type']) {
        case 'person':
            require_once 'Lamplight/Record/People.php';
            $profile = new Lamplight_Record_People();

            $profile->set('first_name', strip_tags($_POST['first_name']))
                ->set('surname', strip_tags($_POST['surname']));
            break;

        case 'org':
            require_once 'Lamplight/Record/Orgs.php';
            $profile = new Lamplight_Record_Orgs();

            $profile->set('name', strip_tags($_POST['name']));
            break;

        default:
            // invalid: type is required and should be as above
            exit;
    }

    

We will then validate and set the other data from the form:


    // All profiles need a role:
    $profile->setRole('user');

 
    if ($_POST['email']) {
        $profile->set('email', strip_tags($_POST['email']));
    }


    $validInterests = array('Sports', 'Arts', 'Science and Technology');
    $cleanInterests = array();
    // check that POSTed interests are valid:
    foreach ($_POST['interests'] as $interest) {
        if (in_array($interest, $validInterests)) {
            $cleanInterests[] = $interest;
        }
    }
    // If there are any, set them on the record
    if (count($cleanInterests) > 0) {
        $profile->set('interests', $cleanInterests);
    }

    

Within Lamplight we have set up multi-select custom fields for Interests, with some of the options shown. This example shows how you can set multiple values.

We can now pass this to our client to save it, and handle the response that comes back.

    

    $client->save($profile);

    $response = $client->getDatainResponse();

    if ($response->success()) {

        echo 'Thanks, the profile has been added OK, '
            . ' ID is ' . $response->getJsonResponse()->data;

    } else {

        echo 'Unfortunately there was a problem.';
        echo $response->getErrorMessage();

    }
  
  

You can also use the Lamplight_Client methods to see more detail of the errors returned - see the documentation for more details.