Merge pull request #17871 from seamuslee001/deprecated_jquery
[civicrm-core.git] / CRM / Core / Reference / Basic.php
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 $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 }
86 $params = [
87 1 => [$targetDao->$targetColumn, 'String'],
88 ];
89 $sql = <<<EOS
90 SELECT {$select}
91 FROM {$this->getReferenceTable()}
92 WHERE {$this->getReferenceKey()} = %1
93 EOS;
94
95 $daoName = CRM_Core_DAO_AllCoreTables::getClassForTable($this->getReferenceTable());
96 $result = CRM_Core_DAO::executeQuery($sql, $params, TRUE, $daoName);
97 return $result;
98 }
99
100 /**
101 * @param CRM_Core_DAO $targetDao
102 *
103 * @return array
104 */
105 public function getReferenceCount($targetDao) {
106 $targetColumn = $this->getTargetKey();
107 $params = [
108 1 => [$targetDao->$targetColumn, 'String'],
109 ];
110 $sql = <<<EOS
111 SELECT count(*)
112 FROM {$this->getReferenceTable()}
113 WHERE {$this->getReferenceKey()} = %1
114 EOS;
115
116 return [
117 'name' => implode(':', ['sql', $this->getReferenceTable(), $this->getReferenceKey()]),
118 'type' => get_class($this),
119 'table' => $this->getReferenceTable(),
120 'key' => $this->getReferenceKey(),
121 'count' => CRM_Core_DAO::singleValueQuery($sql, $params),
122 ];
123 }
124
125 }