Commit | Line | Data |
---|---|---|
11626cf1 TO |
1 | <?php |
2 | ||
3 | /** | |
4 | * Description of a one-way link between two entities | |
5 | * | |
6 | * This is a generic, soft-foreign key based on a pair of columns (entity_id, entity_table). | |
7 | */ | |
8 | class CRM_Core_Reference_Dynamic extends CRM_Core_Reference_Basic { | |
9 | ||
a0ee3941 EM |
10 | /** |
11 | * @param string $tableName | |
12 | * | |
13 | * @return bool | |
14 | */ | |
11626cf1 TO |
15 | public function matchesTargetTable($tableName) { |
16 | return TRUE; | |
17 | } | |
18 | ||
19 | /** | |
20 | * Create a query to find references to a particular record | |
21 | * | |
22 | * @param CRM_Core_DAO $targetDao the instance for which we want references | |
23 | * @return CRM_Core_DAO a query-handle (like the result of CRM_Core_DAO::executeQuery) | |
24 | */ | |
25 | public function findReferences($targetDao) { | |
26 | $refColumn = $this->getReferenceKey(); | |
27 | $targetColumn = $this->getTargetKey(); | |
28 | ||
29 | $params = array( | |
30 | 1 => array($targetDao->$targetColumn, 'String'), | |
31 | ||
32 | // If anyone complains about $targetDao::getTableName(), then could use | |
33 | // "{get_class($targetDao)}::getTableName();" | |
34 | 2 => array($targetDao::getTableName(), 'String'), | |
35 | ); | |
36 | ||
37 | $sql = <<<EOS | |
38 | SELECT id | |
39 | FROM {$this->getReferenceTable()} | |
40 | WHERE {$refColumn} = %1 | |
41 | AND {$this->getTypeColumn()} = %2 | |
42 | EOS; | |
43 | ||
44 | $daoName = CRM_Core_DAO_AllCoreTables::getClassForTable($this->getReferenceTable()); | |
45 | $result = CRM_Core_DAO::executeQuery($sql, $params, TRUE, $daoName); | |
46 | return $result; | |
47 | } | |
1256c139 | 48 | |
a0ee3941 EM |
49 | /** |
50 | * @param CRM_Core_DAO $targetDao | |
51 | * | |
52 | * @return array | |
53 | */ | |
1256c139 TO |
54 | public function getReferenceCount($targetDao) { |
55 | $targetColumn = $this->getTargetKey(); | |
56 | $params = array( | |
57 | 1 => array($targetDao->$targetColumn, 'String'), | |
58 | ||
59 | // If anyone complains about $targetDao::getTableName(), then could use | |
60 | // "{get_class($targetDao)}::getTableName();" | |
61 | 2 => array($targetDao::getTableName(), 'String'), | |
62 | ); | |
63 | ||
64 | $sql = <<<EOS | |
65 | SELECT count(id) | |
66 | FROM {$this->getReferenceTable()} | |
67 | WHERE {$this->getReferenceKey()} = %1 | |
68 | AND {$this->getTypeColumn()} = %2 | |
69 | EOS; | |
70 | ||
71 | return array( | |
72 | 'name' => implode(':', array('sql', $this->getReferenceTable(), $this->getReferenceKey())), | |
73 | 'type' => get_class($this), | |
74 | 'table' => $this->getReferenceTable(), | |
75 | 'key' => $this->getReferenceKey(), | |
76 | 'count' => CRM_Core_DAO::singleValueQuery($sql, $params) | |
77 | ); | |
78 | } | |
79 | ||
11626cf1 | 80 | } |