| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | +--------------------------------------------------------------------+ |
| 5 | | CiviCRM version 4.7 | |
| 6 | +--------------------------------------------------------------------+ |
| 7 | | Copyright CiviCRM LLC (c) 2004-2016 | |
| 8 | +--------------------------------------------------------------------+ |
| 9 | | This file is a part of CiviCRM. | |
| 10 | | | |
| 11 | | CiviCRM is free software; you can copy, modify, and distribute it | |
| 12 | | under the terms of the GNU Affero General Public License | |
| 13 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | |
| 14 | | | |
| 15 | | CiviCRM is distributed in the hope that it will be useful, but | |
| 16 | | WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 17 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
| 18 | | See the GNU Affero General Public License for more details. | |
| 19 | | | |
| 20 | | You should have received a copy of the GNU Affero General Public | |
| 21 | | License and the CiviCRM Licensing Exception along | |
| 22 | | with this program; if not, contact CiviCRM LLC | |
| 23 | | at info[AT]civicrm[DOT]org. If you have questions about the | |
| 24 | | GNU Affero General Public License or the licensing of CiviCRM, | |
| 25 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | |
| 26 | +--------------------------------------------------------------------+ |
| 27 | */ |
| 28 | |
| 29 | /** |
| 30 | * |
| 31 | * @package CRM |
| 32 | * @copyright CiviCRM LLC (c) 2004-2016 |
| 33 | * $Id$ |
| 34 | * |
| 35 | */ |
| 36 | class CRM_Core_DAO_AllCoreTables { |
| 37 | |
| 38 | static private $tables = NULL; |
| 39 | static private $daoToClass = NULL; |
| 40 | static private $entityTypes = NULL; |
| 41 | |
| 42 | static public function init($fresh = FALSE) { |
| 43 | static $init = FALSE; |
| 44 | if ($init && !$fresh) { return; |
| 45 | } |
| 46 | |
| 47 | $file = preg_replace('/\.php$/', '.data.php', __FILE__); |
| 48 | $entityTypes = require $file; |
| 49 | CRM_Utils_Hook::entityTypes($entityTypes); |
| 50 | |
| 51 | self::$entityTypes = array(); |
| 52 | self::$tables = array(); |
| 53 | self::$daoToClass = array(); |
| 54 | foreach ($entityTypes as $entityType) { |
| 55 | self::registerEntityType($entityType['name'], $entityType['class'], $entityType['table']); |
| 56 | } |
| 57 | |
| 58 | $init = TRUE; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * (Quasi-Private) Do not call externally (except for unit-testing) |
| 63 | */ |
| 64 | static public function registerEntityType($daoName, $className, $tableName) { |
| 65 | self::$daoToClass[$daoName] = $className; |
| 66 | self::$tables[$tableName] = $className; |
| 67 | self::$entityTypes[$className] = array( |
| 68 | 'name' => $daoName, |
| 69 | 'class' => $className, |
| 70 | 'table' => $tableName, |
| 71 | ); |
| 72 | } |
| 73 | |
| 74 | static public function get() { |
| 75 | self::init(); |
| 76 | return self::$entityTypes; |
| 77 | } |
| 78 | |
| 79 | static public function tables() { |
| 80 | self::init(); |
| 81 | return self::$tables; |
| 82 | } |
| 83 | |
| 84 | static public function daoToClass() { |
| 85 | self::init(); |
| 86 | return self::$daoToClass; |
| 87 | } |
| 88 | |
| 89 | static public function getCoreTables() { |
| 90 | return self::tables(); |
| 91 | } |
| 92 | |
| 93 | static public function isCoreTable($tableName) { |
| 94 | return FALSE !== array_search($tableName, self::tables()); |
| 95 | } |
| 96 | |
| 97 | static public function getCanonicalClassName($className) { |
| 98 | return str_replace('_BAO_', '_DAO_', $className); |
| 99 | } |
| 100 | |
| 101 | static public function getClasses() { |
| 102 | return array_values(self::daoToClass()); |
| 103 | } |
| 104 | |
| 105 | static public function getClassForTable($tableName) { |
| 106 | return CRM_Utils_Array::value($tableName, self::tables()); |
| 107 | } |
| 108 | |
| 109 | static public function getFullName($daoName) { |
| 110 | return CRM_Utils_Array::value($daoName, self::daoToClass()); |
| 111 | } |
| 112 | |
| 113 | static public function getBriefName($className) { |
| 114 | return CRM_Utils_Array::value($className, array_flip(self::daoToClass())); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * @param string $className DAO or BAO name |
| 119 | * @return string|FALSE SQL table name |
| 120 | */ |
| 121 | static public function getTableForClass($className) { |
| 122 | return array_search(self::getCanonicalClassName($className), self::tables()); |
| 123 | } |
| 124 | |
| 125 | static public function reinitializeCache($fresh = FALSE) { |
| 126 | self::init($fresh); |
| 127 | } |
| 128 | |
| 129 | } |