Commit | Line | Data |
---|---|---|
ffcef054 TO |
1 | <?php |
2 | ||
3 | /** | |
4 | * Description of a one-way link between an option-value and an entity | |
5 | */ | |
6 | class CRM_Core_Reference_OptionValue extends CRM_Core_Reference_Basic { | |
7 | /** | |
8 | * @var string option-group-name | |
9 | */ | |
10 | protected $targetOptionGroupName; | |
11 | ||
12 | /** | |
13 | * @var int|NULL null if not yet loaded | |
14 | */ | |
15 | protected $targetOptionGroupId; | |
16 | ||
a0ee3941 EM |
17 | /** |
18 | * @param $refTable | |
19 | * @param $refKey | |
20 | * @param null $targetTable | |
21 | * @param string $targetKey | |
22 | * @param null $optionGroupName | |
23 | */ | |
00be9182 | 24 | public function __construct($refTable, $refKey, $targetTable = NULL, $targetKey = 'id', $optionGroupName) { |
ffcef054 TO |
25 | parent::__construct($refTable, $refKey, $targetTable, $targetKey, NULL); |
26 | $this->targetOptionGroupName = $optionGroupName; | |
27 | } | |
28 | ||
a0ee3941 EM |
29 | /** |
30 | * @param CRM_Core_DAO $targetDao | |
31 | * | |
32 | * @return null|Object | |
33 | * @throws CRM_Core_Exception | |
34 | */ | |
ffcef054 | 35 | public function findReferences($targetDao) { |
353ffa53 | 36 | if (!($targetDao instanceof CRM_Core_DAO_OptionValue)) { |
ffcef054 TO |
37 | throw new CRM_Core_Exception("Mismatched reference: expected OptionValue but received " . get_class($targetDao)); |
38 | } | |
39 | if ($targetDao->option_group_id == $this->getTargetOptionGroupId()) { | |
40 | return parent::findReferences($targetDao); | |
0db6c3e1 TO |
41 | } |
42 | else { | |
ffcef054 TO |
43 | return NULL; |
44 | } | |
45 | } | |
46 | ||
a0ee3941 EM |
47 | /** |
48 | * @param CRM_Core_DAO $targetDao | |
49 | * | |
50 | * @return array|null | |
51 | * @throws CRM_Core_Exception | |
52 | */ | |
1256c139 | 53 | public function getReferenceCount($targetDao) { |
353ffa53 | 54 | if (!($targetDao instanceof CRM_Core_DAO_OptionValue)) { |
1256c139 TO |
55 | throw new CRM_Core_Exception("Mismatched reference: expected OptionValue but received " . get_class($targetDao)); |
56 | } | |
57 | if ($targetDao->option_group_id == $this->getTargetOptionGroupId()) { | |
58 | return parent::getReferenceCount($targetDao); | |
0db6c3e1 TO |
59 | } |
60 | else { | |
1256c139 TO |
61 | return NULL; |
62 | } | |
63 | } | |
64 | ||
a0ee3941 EM |
65 | /** |
66 | * @return int|NULL | |
67 | */ | |
ffcef054 TO |
68 | public function getTargetOptionGroupId() { |
69 | if ($this->targetOptionGroupId === NULL) { | |
70 | $this->targetOptionGroupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', $this->targetOptionGroupName, 'id', 'name'); | |
71 | } | |
72 | return $this->targetOptionGroupId; | |
73 | } | |
96025800 | 74 | |
a0ee3941 | 75 | } |