eed0b5d93c2a229f5ab2f7e205ce7a1d42a67581
[civicrm-core.git] / CRM / Core / Reference / OptionValue.php
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
17 /**
18 * @param $refTable
19 * @param $refKey
20 * @param null $targetTable
21 * @param string $targetKey
22 * @param null $optionGroupName
23 */
24 public function __construct($refTable, $refKey, $targetTable = NULL, $targetKey = 'id', $optionGroupName) {
25 parent::__construct($refTable, $refKey, $targetTable, $targetKey, NULL);
26 $this->targetOptionGroupName = $optionGroupName;
27 }
28
29 /**
30 * @param CRM_Core_DAO $targetDao
31 *
32 * @return null|Object
33 * @throws CRM_Core_Exception
34 */
35 public function findReferences($targetDao) {
36 if (! ($targetDao instanceof CRM_Core_DAO_OptionValue)) {
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);
41 } else {
42 return NULL;
43 }
44 }
45
46 /**
47 * @param CRM_Core_DAO $targetDao
48 *
49 * @return array|null
50 * @throws CRM_Core_Exception
51 */
52 public function getReferenceCount($targetDao) {
53 if (! ($targetDao instanceof CRM_Core_DAO_OptionValue)) {
54 throw new CRM_Core_Exception("Mismatched reference: expected OptionValue but received " . get_class($targetDao));
55 }
56 if ($targetDao->option_group_id == $this->getTargetOptionGroupId()) {
57 return parent::getReferenceCount($targetDao);
58 } else {
59 return NULL;
60 }
61 }
62
63 /**
64 * @return int|NULL
65 */
66 public function getTargetOptionGroupId() {
67 if ($this->targetOptionGroupId === NULL) {
68 $this->targetOptionGroupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', $this->targetOptionGroupName, 'id', 'name');
69 }
70 return $this->targetOptionGroupId;
71 }
72 }