| 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 | * Option group name. |
| 9 | * |
| 10 | * @var string |
| 11 | */ |
| 12 | protected $targetOptionGroupName; |
| 13 | |
| 14 | /** |
| 15 | * Target Option Group ID. |
| 16 | * |
| 17 | * @var int|null |
| 18 | */ |
| 19 | protected $targetOptionGroupId; |
| 20 | |
| 21 | /** |
| 22 | * @param $refTable |
| 23 | * @param $refKey |
| 24 | * @param null $targetTable |
| 25 | * @param string $targetKey |
| 26 | * @param null $optionGroupName |
| 27 | */ |
| 28 | public function __construct($refTable, $refKey, $targetTable = NULL, $targetKey = 'id', $optionGroupName) { |
| 29 | parent::__construct($refTable, $refKey, $targetTable, $targetKey, NULL); |
| 30 | $this->targetOptionGroupName = $optionGroupName; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * @param CRM_Core_DAO $targetDao |
| 35 | * |
| 36 | * @return null|Object |
| 37 | * @throws CRM_Core_Exception |
| 38 | */ |
| 39 | public function findReferences($targetDao) { |
| 40 | if (!($targetDao instanceof CRM_Core_DAO_OptionValue)) { |
| 41 | throw new CRM_Core_Exception("Mismatched reference: expected OptionValue but received " . get_class($targetDao)); |
| 42 | } |
| 43 | if ($targetDao->option_group_id == $this->getTargetOptionGroupId()) { |
| 44 | return parent::findReferences($targetDao); |
| 45 | } |
| 46 | else { |
| 47 | return NULL; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Get Reference Count. |
| 53 | * |
| 54 | * @param CRM_Core_DAO $targetDao |
| 55 | * |
| 56 | * @return array|null |
| 57 | * @throws CRM_Core_Exception |
| 58 | */ |
| 59 | public function getReferenceCount($targetDao) { |
| 60 | if (!($targetDao instanceof CRM_Core_DAO_OptionValue)) { |
| 61 | throw new CRM_Core_Exception("Mismatched reference: expected OptionValue but received " . get_class($targetDao)); |
| 62 | } |
| 63 | if ($targetDao->option_group_id == $this->getTargetOptionGroupId()) { |
| 64 | return parent::getReferenceCount($targetDao); |
| 65 | } |
| 66 | else { |
| 67 | return NULL; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Get target option group ID. |
| 73 | * |
| 74 | * @return int |
| 75 | */ |
| 76 | public function getTargetOptionGroupId() { |
| 77 | if ($this->targetOptionGroupId === NULL) { |
| 78 | $this->targetOptionGroupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', $this->targetOptionGroupName, 'id', 'name'); |
| 79 | } |
| 80 | return $this->targetOptionGroupId; |
| 81 | } |
| 82 | |
| 83 | } |