-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathEntityStore.php
61 lines (50 loc) · 1.54 KB
/
EntityStore.php
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
<?php
namespace Civi\Pop;
/**
* A place to cache CiviCRM entities to reduce API calls
*/
class EntityStore {
private $store = array();
private $entityParams = array('Event' => array('is_template' => false));
function getRandom($entity, $filter = []){
$key = $this->getKey($entity, $filter);
return $this->store[$key][$this->getRandomId($entity, $filter)];
}
function getSpecific($entity, $filter = [], $id){
$key = $this->getKey($entity, $filter);
if(!isset($this->store[$key])){
$this->init($entity, $filter);
}
return $this->store[$key][$id];
}
function getRandomId($entity, $filter = []){
$key = $this->getKey($entity, $filter);
if(!isset($this->store[$key])){
$this->init($entity, $filter);
}
return array_rand($this->store[$key]);
}
function getKey(&$entity, &$filter){
if(in_array($entity, array('Individual', 'Household', 'Organization'))){
$filter['contact_type'] = $entity;
$entity = 'Contact';
}
$key = $entity;
if(isset($filter)){
ksort($filter);
$key .= json_encode($filter);
}
return $key;
}
function init($entity, $filter){
if(isset($this->entityParams[$entity])){
$filter = array_merge($filter, $this->entityParams[$entity]);
}
$params = array_merge($filter, array('options' => array('limit' => 10000)));
$result = Connection::api3($entity, 'get', $params);
$this->store[$this->getKey($entity, $filter)] = $result['values'];
}
function add($entity, $id){
$this->store[$entity] = $id;
}
}