CRM-14478 - Add CRM_Core_DAO::getReferenceCounts()
[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 function __construct($refTable, $refKey, $targetTable = NULL, $targetKey = 'id', $optionGroupName) {
18 parent::__construct($refTable, $refKey, $targetTable, $targetKey, NULL);
19 $this->targetOptionGroupName = $optionGroupName;
20 }
21
22 public function findReferences($targetDao) {
23 if (! ($targetDao instanceof CRM_Core_DAO_OptionValue)) {
24 throw new CRM_Core_Exception("Mismatched reference: expected OptionValue but received " . get_class($targetDao));
25 }
26 if ($targetDao->option_group_id == $this->getTargetOptionGroupId()) {
27 return parent::findReferences($targetDao);
28 } else {
29 return NULL;
30 }
31 }
32
33 public function getReferenceCount($targetDao) {
34 if (! ($targetDao instanceof CRM_Core_DAO_OptionValue)) {
35 throw new CRM_Core_Exception("Mismatched reference: expected OptionValue but received " . get_class($targetDao));
36 }
37 if ($targetDao->option_group_id == $this->getTargetOptionGroupId()) {
38 return parent::getReferenceCount($targetDao);
39 } else {
40 return NULL;
41 }
42 }
43
44 public function getTargetOptionGroupId() {
45 if ($this->targetOptionGroupId === NULL) {
46 $this->targetOptionGroupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', $this->targetOptionGroupName, 'id', 'name');
47 }
48 return $this->targetOptionGroupId;
49 }
50 }