Queue - When UserJob.queue_id works down to zero tasks, update status and fire hook
[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 string $refTable
17 * @param string $refKey
18 * @param string $targetTable
19 * @param string $targetKey
20 * @param string|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 string
32 */
33 public function getReferenceTable() {
34 return $this->refTable;
35 }
36
37 /**
38 * @return string
39 */
40 public function getReferenceKey() {
41 return $this->refKey;
42 }
43
44 /**
45 * @return string|null
46 */
47 public function getTypeColumn() {
48 return $this->refTypeColumn;
49 }
50
51 /**
52 * @return string
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 * @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
82 /**
83 * @param CRM_Core_DAO $targetDao
84 *
85 * @return Object
86 */
87 public function findReferences($targetDao) {
88 $targetColumn = $this->getTargetKey();
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 }
94 $params = [
95 1 => [$targetDao->$targetColumn, 'String'],
96 ];
97 $sql = <<<EOS
98 SELECT {$select}
99 FROM {$this->getReferenceTable()}
100 WHERE {$this->getReferenceKey()} = %1
101 EOS;
102
103 $daoName = CRM_Core_DAO_AllCoreTables::getClassForTable($this->getReferenceTable());
104 $result = CRM_Core_DAO::executeQuery($sql, $params, TRUE, $daoName);
105 return $result;
106 }
107
108 /**
109 * @param CRM_Core_DAO $targetDao
110 *
111 * @return array
112 */
113 public function getReferenceCount($targetDao) {
114 $targetColumn = $this->getTargetKey();
115 $count = 0;
116 if ($targetDao->{$targetColumn} !== '' && $targetDao->{$targetColumn} !== NULL) {
117
118 $params = [
119 1 => [$targetDao->{$targetColumn} ?? '', 'String'],
120 ];
121 $sql = <<<EOS
122 SELECT count(*)
123 FROM {$this->getReferenceTable()}
124 WHERE {$this->getReferenceKey()} = %1
125 EOS;
126 $count = CRM_Core_DAO::singleValueQuery($sql, $params);
127 }
128
129 return [
130 'name' => implode(':', ['sql', $this->getReferenceTable(), $this->getReferenceKey()]),
131 'type' => get_class($this),
132 'table' => $this->getReferenceTable(),
133 'key' => $this->getReferenceKey(),
134 'count' => $count,
135 ];
136 }
137
138 }