Merge remote-tracking branch 'upstream/4.5' into 4.5-master-2015-01-26-14-28-00
[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 }
42 else {
43 return NULL;
44 }
45 }
46
47 /**
48 * @param CRM_Core_DAO $targetDao
49 *
50 * @return array|null
51 * @throws CRM_Core_Exception
52 */
53 public function getReferenceCount($targetDao) {
54 if (!($targetDao instanceof CRM_Core_DAO_OptionValue)) {
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);
59 }
60 else {
61 return NULL;
62 }
63 }
64
65 /**
66 * @return int|NULL
67 */
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 }
74
75 }