Merge pull request #22035 from eileenmcnaughton/campaign
[civicrm-core.git] / CRM / Core / Reference / Basic.php
CommitLineData
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 8class 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
17019d49
CW
74 /**
75 * @return array
76 * [table_name => EntityName]
77 */
78 public function getTargetEntities(): array {
79 return [$this->targetTable => CRM_Core_DAO_AllCoreTables::getEntityNameForTable($this->targetTable)];
80 }
81
a0ee3941
EM
82 /**
83 * @param CRM_Core_DAO $targetDao
84 *
85 * @return Object
86 */
de49f39c 87 public function findReferences($targetDao) {
de49f39c 88 $targetColumn = $this->getTargetKey();
21ca2cb6 89 $select = 'id';
90 // CRM-19385: Since id is removed, return all rows for cache tables.
91 if (!CRM_Core_BAO_SchemaHandler::checkIfFieldExists($this->getReferenceTable(), 'id')) {
92 $select = '*';
93 }
be2fb01f
CW
94 $params = [
95 1 => [$targetDao->$targetColumn, 'String'],
96 ];
de49f39c 97 $sql = <<<EOS
21ca2cb6 98SELECT {$select}
de49f39c 99FROM {$this->getReferenceTable()}
11626cf1 100WHERE {$this->getReferenceKey()} = %1
de49f39c 101EOS;
11626cf1 102
de49f39c
TO
103 $daoName = CRM_Core_DAO_AllCoreTables::getClassForTable($this->getReferenceTable());
104 $result = CRM_Core_DAO::executeQuery($sql, $params, TRUE, $daoName);
105 return $result;
106 }
1256c139 107
a0ee3941
EM
108 /**
109 * @param CRM_Core_DAO $targetDao
110 *
111 * @return array
112 */
1256c139
TO
113 public function getReferenceCount($targetDao) {
114 $targetColumn = $this->getTargetKey();
eb2a4878
EM
115 $count = 0;
116 if ($targetDao->{$targetColumn} !== '' && $targetDao->{$targetColumn} !== NULL) {
117
118 $params = [
119 1 => [$targetDao->{$targetColumn} ?? '', 'String'],
120 ];
121 $sql = <<<EOS
21ca2cb6 122SELECT count(*)
1256c139
TO
123FROM {$this->getReferenceTable()}
124WHERE {$this->getReferenceKey()} = %1
125EOS;
eb2a4878
EM
126 $count = CRM_Core_DAO::singleValueQuery($sql, $params);
127 }
1256c139 128
be2fb01f
CW
129 return [
130 'name' => implode(':', ['sql', $this->getReferenceTable(), $this->getReferenceKey()]),
1256c139
TO
131 'type' => get_class($this),
132 'table' => $this->getReferenceTable(),
133 'key' => $this->getReferenceKey(),
eb2a4878 134 'count' => $count,
be2fb01f 135 ];
1256c139 136 }
96025800 137
71e5aa5c 138}