Merge pull request #5386 from colemanw/CRM-15706
[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
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();
11626cf1 81 $params = array(
21dfd5f5 82 1 => array($targetDao->$targetColumn, 'String'),
11626cf1 83 );
de49f39c
TO
84 $sql = <<<EOS
85SELECT id
86FROM {$this->getReferenceTable()}
11626cf1 87WHERE {$this->getReferenceKey()} = %1
de49f39c 88EOS;
11626cf1 89
de49f39c
TO
90 $daoName = CRM_Core_DAO_AllCoreTables::getClassForTable($this->getReferenceTable());
91 $result = CRM_Core_DAO::executeQuery($sql, $params, TRUE, $daoName);
92 return $result;
93 }
1256c139 94
a0ee3941
EM
95 /**
96 * @param CRM_Core_DAO $targetDao
97 *
98 * @return array
99 */
1256c139
TO
100 public function getReferenceCount($targetDao) {
101 $targetColumn = $this->getTargetKey();
102 $params = array(
21dfd5f5 103 1 => array($targetDao->$targetColumn, 'String'),
1256c139
TO
104 );
105 $sql = <<<EOS
106SELECT count(id)
107FROM {$this->getReferenceTable()}
108WHERE {$this->getReferenceKey()} = %1
109EOS;
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(),
21dfd5f5 116 'count' => CRM_Core_DAO::singleValueQuery($sql, $params),
1256c139
TO
117 );
118 }
96025800 119
71e5aa5c 120}