| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Description of a one-way link between two entities |
| 5 | * |
| 6 | * This is a basic SQL foreign key. |
| 7 | */ |
| 8 | class CRM_Core_Reference_Basic implements CRM_Core_Reference_Interface { |
| 9 | protected $refTable; |
| 10 | protected $refKey; |
| 11 | protected $refTypeColumn; |
| 12 | protected $targetTable; |
| 13 | protected $targetKey; |
| 14 | |
| 15 | /** |
| 16 | * @param $refTable |
| 17 | * @param $refKey |
| 18 | * @param null $targetTable |
| 19 | * @param string $targetKey |
| 20 | * @param null $refTypeColumn |
| 21 | */ |
| 22 | public function __construct($refTable, $refKey, $targetTable = NULL, $targetKey = 'id', $refTypeColumn = NULL) { |
| 23 | $this->refTable = $refTable; |
| 24 | $this->refKey = $refKey; |
| 25 | $this->targetTable = $targetTable; |
| 26 | $this->targetKey = $targetKey; |
| 27 | $this->refTypeColumn = $refTypeColumn; |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * @return mixed |
| 32 | */ |
| 33 | public function getReferenceTable() { |
| 34 | return $this->refTable; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * @return mixed |
| 39 | */ |
| 40 | public function getReferenceKey() { |
| 41 | return $this->refKey; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * @return null |
| 46 | */ |
| 47 | public function getTypeColumn() { |
| 48 | return $this->refTypeColumn; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * @return null |
| 53 | */ |
| 54 | public function getTargetTable() { |
| 55 | return $this->targetTable; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * @return string |
| 60 | */ |
| 61 | public function getTargetKey() { |
| 62 | return $this->targetKey; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * @param string $tableName |
| 67 | * |
| 68 | * @return bool |
| 69 | */ |
| 70 | public function matchesTargetTable($tableName) { |
| 71 | return ($this->getTargetTable() === $tableName); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * @param CRM_Core_DAO $targetDao |
| 76 | * |
| 77 | * @return Object |
| 78 | */ |
| 79 | public function findReferences($targetDao) { |
| 80 | $targetColumn = $this->getTargetKey(); |
| 81 | $params = array( |
| 82 | 1 => array($targetDao->$targetColumn, 'String'), |
| 83 | ); |
| 84 | $sql = <<<EOS |
| 85 | SELECT id |
| 86 | FROM {$this->getReferenceTable()} |
| 87 | WHERE {$this->getReferenceKey()} = %1 |
| 88 | EOS; |
| 89 | |
| 90 | $daoName = CRM_Core_DAO_AllCoreTables::getClassForTable($this->getReferenceTable()); |
| 91 | $result = CRM_Core_DAO::executeQuery($sql, $params, TRUE, $daoName); |
| 92 | return $result; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * @param CRM_Core_DAO $targetDao |
| 97 | * |
| 98 | * @return array |
| 99 | */ |
| 100 | public function getReferenceCount($targetDao) { |
| 101 | $targetColumn = $this->getTargetKey(); |
| 102 | $params = array( |
| 103 | 1 => array($targetDao->$targetColumn, 'String'), |
| 104 | ); |
| 105 | $sql = <<<EOS |
| 106 | SELECT count(id) |
| 107 | FROM {$this->getReferenceTable()} |
| 108 | WHERE {$this->getReferenceKey()} = %1 |
| 109 | EOS; |
| 110 | |
| 111 | return array( |
| 112 | 'name' => implode(':', array('sql', $this->getReferenceTable(), $this->getReferenceKey())), |
| 113 | 'type' => get_class($this), |
| 114 | 'table' => $this->getReferenceTable(), |
| 115 | 'key' => $this->getReferenceKey(), |
| 116 | 'count' => CRM_Core_DAO::singleValueQuery($sql, $params), |
| 117 | ); |
| 118 | } |
| 119 | |
| 120 | } |