-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUNLPersonEntityController.inc
65 lines (53 loc) · 1.72 KB
/
UNLPersonEntityController.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
class UNLPersonEntityController extends EntityAPIController {
/**
* The base url for the api. Trailing slash is important.
*
* @var string
*/
public static $api_url = 'https://directory.unl.edu/';
public function load($ids = array(), $conditions = array()) {
if (!is_array($ids)) {
$ids = (array)$ids;
}
$entities = array();
// This method takes an array of IDs, but our webservice only supports loading one entity at a time.
foreach ($ids as $id) {
// This function should contain all the code to make a request to the web service and handle any errors.
if ($entity = unl_person_load_unl_person($id)) {
// Entities must be keyed by entity ID in order for field values to be correctly attached.
$entities[$entity->uid] = $entity;
}
}
return $entities;
}
/**
* Search for entities and load them.
* This simply asks our api for people that match the search term.
*
* @param $term
* @return array
*/
public static function search($term) {
$url = self::$api_url . '?q=' . urlencode($term) . '&format=json';
$results = array();
if (empty($term)) {
return $results;
}
if (!$json = file_get_contents($url)) {
watchdog('unl_person', 'Unable to get contents at %url', array('%url'=>$url));
return $results;
}
if (!$data = json_decode($json, true)) {
watchdog('unl_person', 'Unable to json_decode for %url', array('%url'=>$url));
return $results;
}
foreach ($data as $record) {
if (!$entity = unl_person_json_to_unl_person($record)) {
continue;
}
$results[$entity->uid] = $entity;
}
return $results;
}
}