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