PageRenderTime 25ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/drupal/sites/all/modules/civicrm/CRM/Core/Component.php

https://github.com/michaelmcandrew/vaw
PHP | 381 lines | 272 code | 59 blank | 50 comment | 37 complexity | 4e2ff0feb7f71da14ab6664cd668c031 MD5 | raw file
  1. <?php
  2. /*
  3. +--------------------------------------------------------------------+
  4. | CiviCRM version 3.4 |
  5. +--------------------------------------------------------------------+
  6. | Copyright CiviCRM LLC (c) 2004-2011 |
  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. +--------------------------------------------------------------------+
  26. */
  27. /**
  28. * Component stores all the static and dynamic information of the various
  29. * CiviCRM components
  30. *
  31. * @package CRM
  32. * @copyright CiviCRM LLC (c) 2004-2011
  33. * $Id$
  34. *
  35. */
  36. class CRM_Core_Component
  37. {
  38. /*
  39. * End part (filename) of the component information class'es name
  40. * that needs to be present in components main directory.
  41. */
  42. const COMPONENT_INFO_CLASS = 'Info';
  43. private static $_info = null;
  44. static $_contactSubTypes = null;
  45. private static function &_info( $force = false ) {
  46. if( self::$_info == null || $force ) {
  47. self::$_info = array( );
  48. $c = array();
  49. $config = CRM_Core_Config::singleton( );
  50. $c = self::getComponents();
  51. foreach( $c as $name => $comp ) {
  52. if ( in_array( $name, $config->enableComponents ) ) {
  53. self::$_info[$name] = $comp;
  54. }
  55. }
  56. }
  57. return self::$_info;
  58. }
  59. static function get( $name, $attribute = null)
  60. {
  61. $comp = CRM_Utils_Array::value( $name, self::_info() );
  62. if ( $attribute ) {
  63. return CRM_Utils_Array::value( $attribute, $comp->info );
  64. }
  65. return $comp;
  66. }
  67. public static function &getComponents( $force = false )
  68. {
  69. static $_cache = null;
  70. if ( ! $_cache || $force ) {
  71. $_cache = array( );
  72. require_once 'CRM/Core/DAO/Component.php';
  73. $cr = new CRM_Core_DAO_Component();
  74. $cr->find( false );
  75. while ( $cr->fetch( ) ) {
  76. $infoClass = $cr->namespace . '_' . self::COMPONENT_INFO_CLASS;
  77. require_once( str_replace( '_', DIRECTORY_SEPARATOR, $infoClass ) . '.php' );
  78. $infoObject = new $infoClass( $cr->name, $cr->namespace, $cr->id );
  79. if ( $infoObject->info['name'] !== $cr->name ) {
  80. CRM_Core_Error::fatal( "There is a discrepancy between name in component registry and in info file ({$cr->name})." );
  81. }
  82. $_cache[$cr->name] = $infoObject;
  83. unset( $infoObject );
  84. }
  85. }
  86. return $_cache;
  87. }
  88. public function &getEnabledComponents( $force = false )
  89. {
  90. return self::_info( $force );
  91. }
  92. public function &getNames( $translated = false )
  93. {
  94. $allComponents = self::getComponents();
  95. $names = array();
  96. foreach ( $allComponents as $name => $comp ) {
  97. if( $translated ) {
  98. $names[$comp->componentID] = $comp->info['translatedName'];
  99. } else {
  100. $names[$comp->componentID] = $name;
  101. }
  102. }
  103. return $names;
  104. }
  105. static function invoke( &$args, $type )
  106. {
  107. $info = self::_info( );
  108. $config = CRM_Core_Config::singleton( );
  109. $firstArg = CRM_Utils_Array::value( 1, $args, '' );
  110. $secondArg = CRM_Utils_Array::value( 2, $args, '' );
  111. foreach ( $info as $name => $comp ) {
  112. if ( in_array( $name, $config->enableComponents ) &&
  113. ( ( $comp->info['url'] === $firstArg && $type == 'main' ) ||
  114. ( $comp->info['url'] === $secondArg && $type == 'admin' ) ) ) {
  115. if ( $type == 'main' ) {
  116. // also set the smarty variables to the current component
  117. $template = CRM_Core_Smarty::singleton( );
  118. $template->assign( 'activeComponent', $name );
  119. if( CRM_Utils_Array::value( 'formTpl', $comp->info[$name] ) ) {
  120. $template->assign( 'formTpl', $comp->info[$name]['formTpl'] );
  121. }
  122. if( CRM_Utils_Array::value( 'css', $comp->info[$name] ) ) {
  123. $styleSheets = '<style type="text/css">@import url(' .
  124. "{$config->resourceBase}css/{$comp->info[$name]['css']});</style>";
  125. CRM_Utils_System::addHTMLHead( $styleSheet );
  126. }
  127. }
  128. $inv = $comp->getInvokeObject();
  129. $inv->$type( $args );
  130. return true;
  131. }
  132. }
  133. return false;
  134. }
  135. static function xmlMenu( ) {
  136. // lets build the menu for all components
  137. $info = self::getComponents( true );
  138. $files = array( );
  139. foreach( $info as $name => $comp ) {
  140. $files = array_merge( $files,
  141. $comp->menuFiles( ) );
  142. }
  143. return $files;
  144. }
  145. static function &menu( )
  146. {
  147. $info = self::_info( );
  148. $items = array( );
  149. foreach( $info as $name => $comp ) {
  150. $mnu = $comp->getMenuObject( );
  151. $ret = $mnu->permissioned( );
  152. $items = array_merge( $items, $ret );
  153. $ret = $mnu->main( $task );
  154. $items = array_merge( $items, $ret );
  155. }
  156. return $items;
  157. }
  158. static function addConfig( &$config, $oldMode = false )
  159. {
  160. $info = self::_info( );
  161. foreach( $info as $name => $comp ) {
  162. $cfg = $comp->getConfigObject( );
  163. $cfg->add( $config, $oldMode );
  164. }
  165. return;
  166. }
  167. static function getComponentID( $componentName ) {
  168. $info = self::_info( );
  169. return $info[$componentName]->componentID;
  170. }
  171. static function getComponentName( $componentID ) {
  172. $info = self::_info( );
  173. $componentName = null;
  174. foreach ( $info as $compName => $component ) {
  175. if ( $component->componentID == $componentID ) {
  176. $componentName = $compName;
  177. break;
  178. }
  179. }
  180. return $componentName;
  181. }
  182. static function &getQueryFields( )
  183. {
  184. $info = self::_info( );
  185. $fields = array( );
  186. foreach( $info as $name => $comp ) {
  187. if( $comp->usesSearch( ) ) {
  188. $bqr = $comp->getBAOQueryObject( );
  189. $flds = $bqr->getFields( );
  190. $fields = array_merge( $fields, $flds );
  191. }
  192. }
  193. return $fields;
  194. }
  195. static function alterQuery( &$query, $fnName )
  196. {
  197. $info = self::_info( );
  198. foreach( $info as $name => $comp ) {
  199. if( $comp->usesSearch( ) ) {
  200. $bqr = $comp->getBAOQueryObject( );
  201. $bqr->$fnName( $query );
  202. }
  203. }
  204. }
  205. static function from( $fieldName, $mode, $side )
  206. {
  207. $info = self::_info( );
  208. $from = null;
  209. foreach( $info as $name => $comp ) {
  210. if( $comp->usesSearch( ) ) {
  211. $bqr = $comp->getBAOQueryObject( );
  212. $from = $bqr->from( $fieldName, $mode, $side );
  213. if( $from ) {
  214. return $from;
  215. }
  216. }
  217. }
  218. return $from;
  219. }
  220. static function &defaultReturnProperties( $mode )
  221. {
  222. $info = self::_info( );
  223. $properties = null;
  224. foreach( $info as $name => $comp ) {
  225. if( $comp->usesSearch( ) ) {
  226. $bqr = $comp->getBAOQueryObject( );
  227. $properties = $bqr->defaultReturnProperties( $mode );
  228. if( $properties ) {
  229. return $properties;
  230. }
  231. }
  232. }
  233. return $properties;
  234. }
  235. static function &buildSearchForm( &$form )
  236. {
  237. $info = self::_info( );
  238. foreach( $info as $name => $comp ) {
  239. if( $comp->usesSearch( ) ) {
  240. $bqr = $comp->getBAOQueryObject( );
  241. $bqr->buildSearchForm( $form );
  242. }
  243. }
  244. }
  245. static function &addShowHide( &$showHide )
  246. {
  247. $info = self::_info( );
  248. foreach( $info as $name => $comp ) {
  249. if( $comp->usesSearch( ) ) {
  250. $bqr = $comp->getBAOQueryObject( );
  251. $bqr->addShowHide( $showHide );
  252. }
  253. }
  254. }
  255. static function searchAction( &$row, $id )
  256. {
  257. $info = self::_info( );
  258. foreach( $info as $name => $comp ) {
  259. if( $comp->usesSearch( ) ) {
  260. $bqr = $comp->getBAOQueryObject( );
  261. $bqr->searchAction( $row, $id );
  262. }
  263. }
  264. }
  265. static function &contactSubTypes( )
  266. {
  267. if( self::$_contactSubTypes == null ) {
  268. self::$_contactSubTypes = array( );
  269. if( CRM_Core_Permission::access( 'Quest' ) ) {
  270. // Generalize this at some point
  271. self::$_contactSubTypes =
  272. array(
  273. 'Student' =>
  274. array( 'View' =>
  275. array( 'file' => 'CRM/Quest/Page/View/Student.php',
  276. 'class' => 'CRM_Quest_Page_View_Student' ),
  277. )
  278. );
  279. }
  280. }
  281. return self::$_contactSubTypes;
  282. }
  283. static function &contactSubTypeProperties( $subType, $op )
  284. {
  285. $properties = self::contactSubTypes( );
  286. if( array_key_exists( $subType, $properties ) &&
  287. array_key_exists( $op, $properties[$subType] ) ) {
  288. return $properties[$subType][$op];
  289. }
  290. return CRM_Core_DAO::$_nullObject;
  291. }
  292. static function &taskList( )
  293. {
  294. $info = self::_info( );
  295. $tasks = array( );
  296. foreach( $info as $name => $value ) {
  297. if( CRM_Utils_Array::value( 'task', $info[$name] ) ) {
  298. $tasks += $info[$name]['task'];
  299. }
  300. }
  301. return $tasks;
  302. }
  303. /**
  304. * Function to handle table dependencies of components
  305. *
  306. * @param array $tables array of tables
  307. *
  308. * @return null
  309. * @access public
  310. * @static
  311. */
  312. static function tableNames( &$tables )
  313. {
  314. $info = self::_info( );
  315. foreach( $info as $name => $comp ) {
  316. if( $comp->usesSearch( ) ) {
  317. $bqr = $comp->getBAOQueryObject( );
  318. $bqr->tableNames( $tables );
  319. }
  320. }
  321. }
  322. }