Commit | Line | Data |
---|---|---|
e869b07d CW |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
e869b07d | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
e869b07d CW |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
e869b07d | 27 | |
5bc392e6 EM |
28 | /** |
29 | * Class CRM_Utils_Api | |
30 | */ | |
e869b07d CW |
31 | class CRM_Utils_Api { |
32 | /** | |
764dd190 | 33 | * Attempts to retrieve the API entity name from any calling class. |
84403e23 | 34 | * FIXME: This is a bit hackish but the naming convention for forms is not very strict |
764dd190 RS |
35 | * |
36 | * @param string|object $classNameOrObject | |
f4aaa82a EM |
37 | * |
38 | * @return string | |
e869b07d CW |
39 | * @throws CRM_Core_Exception |
40 | */ | |
00be9182 | 41 | public static function getEntityName($classNameOrObject) { |
e869b07d CW |
42 | require_once 'api/api.php'; |
43 | $className = is_string($classNameOrObject) ? $classNameOrObject : get_class($classNameOrObject); | |
44 | ||
45 | // First try the obvious replacements | |
46 | $daoName = str_replace(array('_BAO_', '_Form_', '_Page_'), '_DAO_', $className); | |
4846df91 | 47 | $entityName = CRM_Core_DAO_AllCoreTables::getBriefName($daoName); |
e869b07d CW |
48 | |
49 | // If that didn't work, try a different pattern | |
4846df91 | 50 | if (!$entityName) { |
e1462487 CW |
51 | list(, $parent, , $child) = explode('_', $className); |
52 | $daoName = "CRM_{$parent}_DAO_$child"; | |
4846df91 | 53 | $entityName = CRM_Core_DAO_AllCoreTables::getBriefName($daoName); |
e869b07d CW |
54 | } |
55 | ||
56 | // If that didn't work, try a different pattern | |
4846df91 | 57 | if (!$entityName) { |
e1462487 | 58 | $daoName = "CRM_{$parent}_DAO_$parent"; |
4846df91 | 59 | $entityName = CRM_Core_DAO_AllCoreTables::getBriefName($daoName); |
e1462487 CW |
60 | } |
61 | ||
62 | // If that didn't work, try a different pattern | |
4846df91 | 63 | if (!$entityName) { |
e1462487 | 64 | $daoName = "CRM_Core_DAO_$child"; |
4846df91 | 65 | $entityName = CRM_Core_DAO_AllCoreTables::getBriefName($daoName); |
e869b07d | 66 | } |
84403e23 CW |
67 | |
68 | // If that didn't work, try using just the trailing name | |
4846df91 CW |
69 | if (!$entityName) { |
70 | $entityName = CRM_Core_DAO_AllCoreTables::getFullName($child) ? $child : NULL; | |
84403e23 CW |
71 | } |
72 | ||
73 | // If that didn't work, try using just the leading name | |
4846df91 CW |
74 | if (!$entityName) { |
75 | $entityName = CRM_Core_DAO_AllCoreTables::getFullName($parent) ? $parent : NULL; | |
84403e23 CW |
76 | } |
77 | ||
4846df91 | 78 | if (!$entityName) { |
e869b07d CW |
79 | throw new CRM_Core_Exception('Could not find api name for supplied class'); |
80 | } | |
4846df91 | 81 | return $entityName; |
e869b07d | 82 | } |
96025800 | 83 | |
e869b07d | 84 | } |