tripal.module 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122
  1. <?php
  2. /**
  3. * @file
  4. * The Tripal Core module
  5. */
  6. // Import the full Tripal API into scope.
  7. tripal_import_api();
  8. require_once "includes/tripal.field_storage.inc";
  9. require_once "includes/tripal.fields.inc";
  10. require_once "includes/tripal.entity.inc";
  11. require_once "includes/TripalVocab.inc";
  12. require_once "includes/TripalVocabController.inc";
  13. require_once "includes/TripalVocabViewsController.inc";
  14. require_once "includes/TripalTerm.inc";
  15. require_once "includes/TripalTermController.inc";
  16. require_once "includes/TripalTermViewsController.inc";
  17. require_once "includes/TripalEntity.inc";
  18. require_once "includes/TripalEntityController.inc";
  19. require_once "includes/TripalEntityUIController.inc";
  20. require_once "includes/TripalEntityViewsController.inc";
  21. require_once "includes/TripalBundle.inc";
  22. require_once "includes/TripalBundleController.inc";
  23. require_once "includes/TripalBundleUIController.inc";
  24. require_once "includes/TripalBundleViewsController.inc";
  25. require_once "includes/TripalFields/TripalField.inc";
  26. require_once "includes/TripalFields/TripalFieldWidget.inc";
  27. require_once "includes/TripalFields/TripalFieldFormatter.inc";
  28. require_once "includes/TripalFieldQuery.inc";
  29. require_once "includes/TripalJob.inc";
  30. require_once "includes/TripalImporter.inc";
  31. /**
  32. * @defgroup tripal Tripal Core Module
  33. * @ingroup tripal_modules
  34. * @{
  35. * Functionality useful for all other Tripal modules including the Tripal jobs, files,
  36. * materialized views and custom table functions.
  37. * @}
  38. */
  39. /**
  40. * Implements hook_views_api().
  41. */
  42. function tripal_views_api() {
  43. return array(
  44. 'api' => 3,
  45. );
  46. }
  47. /**
  48. * Implements hook_init().
  49. *
  50. * @ingroup tripal
  51. */
  52. function tripal_init() {
  53. global $base_url;
  54. // add some variables for all javasript to use for building URLs
  55. $clean_urls = variable_get('clean_url', 0);
  56. $tripal_path = url(drupal_get_path('module', 'tripal'));
  57. drupal_add_js("
  58. var baseurl = '$base_url';
  59. var isClean = $clean_urls;
  60. var tripal_path = '$tripal_path';",
  61. 'inline', 'header');
  62. // make sure the date time settings are the way Tripal will insert them
  63. // otherwise PostgreSQL version that may have a different datestyle setting
  64. // will fail when inserting or updating a date column in a table.
  65. db_query("SET DATESTYLE TO :style", array(':style' => 'MDY'));
  66. }
  67. function tripal_menu_alter(&$items) {
  68. //drupal_debug($items);
  69. }
  70. /**
  71. * Implements hook_menu().
  72. * Defines all menu items needed by Tripal Core
  73. *
  74. * @ingroup tripal
  75. */
  76. function tripal_menu() {
  77. $items = array();
  78. // Tripal setting groups
  79. $items['admin/tripal'] = array(
  80. 'title' => 'Tripal',
  81. 'description' => t("Manage the behavior or Tripal and its various modules."),
  82. 'weight' => -8,
  83. 'page callback' => 'system_admin_menu_block_page',
  84. 'access arguments' => array('administer tripal'),
  85. 'file' => 'system.admin.inc',
  86. 'file path' => drupal_get_path('module', 'system'),
  87. );
  88. $items['admin/tripal/storage'] = array(
  89. 'title' => 'Data Storage',
  90. 'description' => t("Tripal is designed to access biological
  91. data in any data storage back-end. A storage back-end must have a
  92. module that can be installed that interfaces with Tripal. By default
  93. the base Tripal package provides The Tripal Chado module for storing
  94. data in the GMOD Chado database schema. All available storage backends
  95. and their administrative tools are found here."),
  96. 'weight' => 8,
  97. 'access arguments' => array('administer tripal'),
  98. );
  99. $items['admin/tripal/extension'] = array(
  100. 'title' => 'Extensions',
  101. 'description' => t("Configuration and management pages for Tripal extension modules."),
  102. 'weight' => 8,
  103. 'access arguments' => array('administer tripal'),
  104. );
  105. // Menu items for facilitating import of extension modules.
  106. $items['admin/tripal/extension/available'] = array(
  107. 'title' => 'Available Extensions',
  108. 'description' => t('Look for extensions to add new functionality to this
  109. site. Tripal can be extended with new functionality developed
  110. by other Tripal site developers. These include modules with new or
  111. different functionality, bulk loading templates, or materialized
  112. views. Anyone can create new extensions and share those for
  113. others to use. Once shared they will appear in this list.'),
  114. 'access arguments' => array('administer tripal'),
  115. 'page callback' => 'drupal_get_form',
  116. 'page arguments' => array('tripal_extensions_form'),
  117. 'type' => MENU_NORMAL_ITEM,
  118. 'file' => 'includes/tripal.extensions.inc',
  119. 'file path' => drupal_get_path('module', 'tripal'),
  120. 'weight' => -100
  121. );
  122. // $items['admin/tripal/extension/import'] = array(
  123. // 'title' => 'Import Extensions',
  124. // 'description' => 'Provides a list of the available extensions that are registered at the tripal.info site. From this page you can easily import or install extensions to your site.',
  125. // 'page callback' => 'drupal_get_form',
  126. // 'page arguments' => array('tripal_extensions_form'),
  127. // 'access arguments' => array('administer tripal'),
  128. // 'type' => MENU_NORMAL_ITEM,
  129. // 'file' => 'includes/tripal.extensions.inc',
  130. // 'file path' => drupal_get_path('module', 'tripal'),
  131. // 'weight' => -100,
  132. // );
  133. // Jobs Management
  134. $items['admin/tripal/tripal_jobs'] = array(
  135. 'title' => 'Jobs',
  136. 'description' => t('Provides tools for managing jobs submitted to Tripal. In some
  137. cases, long-running tasks are too slow to complete within a single
  138. browser session. The Tripal jobs system allows long-running tasks
  139. to be submitted to a queue that can be executed manually by the
  140. site admin or automatically using a module such as the ') .
  141. l('Tripal Daemon', 'https://www.drupal.org/project/tripal_daemon', array('attributes' => array('target' => '_blank'))) .
  142. ' extension module.',
  143. 'page callback' => 'tripal_jobs_admin_view',
  144. 'access arguments' => array('administer tripal'),
  145. 'type' => MENU_NORMAL_ITEM,
  146. 'weight' => 0,
  147. 'file' => 'includes/tripal.jobs.inc',
  148. );
  149. $items['admin/tripal/tripal_jobs/help'] = array(
  150. 'title' => 'Help',
  151. 'description' => t('Help for the tripal job management system'),
  152. 'page callback' => 'theme',
  153. 'page arguments' => array('tripal_job_help'),
  154. 'access arguments' => array('administer tripal'),
  155. 'type' => MENU_LOCAL_TASK,
  156. 'weight' => 10
  157. );
  158. $items['admin/tripal/tripal_jobs/cancel/%'] = array(
  159. 'title' => 'Jobs',
  160. 'description' => t('Cancel a pending job'),
  161. 'page callback' => 'tripal_cancel_job',
  162. 'page arguments' => array(4),
  163. 'access arguments' => array('administer tripal'),
  164. 'type' => MENU_CALLBACK,
  165. 'file' => 'api/tripal.jobs.api.inc',
  166. );
  167. $items['admin/tripal/tripal_jobs/status/%'] = array(
  168. 'page callback' => 'tripal_jobs_status_view',
  169. 'page arguments' => array(4),
  170. 'access arguments' => array('administer tripal'),
  171. 'type' => MENU_CALLBACK,
  172. 'file' => 'includes/tripal.jobs.inc',
  173. );
  174. $items['admin/tripal/tripal_jobs/rerun/%'] = array(
  175. 'title' => 'Jobs',
  176. 'description' => t('Re-run an existing job.'),
  177. 'page callback' => 'tripal_rerun_job',
  178. 'page arguments' => array(4),
  179. 'access arguments' => array('administer tripal'),
  180. 'type' => MENU_CALLBACK,
  181. 'file' => 'includes/tripal.jobs.inc',
  182. );
  183. $items['admin/tripal/tripal_jobs/view/%'] = array(
  184. 'title' => 'Jobs Details',
  185. 'description' => t('View job details.'),
  186. 'page callback' => 'tripal_jobs_view',
  187. 'page arguments' => array(4),
  188. 'access arguments' => array('administer tripal'),
  189. 'type' => MENU_CALLBACK,
  190. 'file' => 'includes/tripal.jobs.inc',
  191. );
  192. $items['admin/tripal/tripal_jobs/views/jobs/enable'] = array(
  193. 'title' => 'Enable Jobs Administrative View',
  194. 'page callback' => 'tripal_enable_view',
  195. 'page arguments' => array('tripal_admin_jobs', 'admin/tripal/tripal_jobs'),
  196. 'access arguments' => array('administer tripal'),
  197. 'type' => MENU_CALLBACK,
  198. 'file' => 'includes/tripal.jobs.inc',
  199. );
  200. /*
  201. * AJAX Callbacks.
  202. */
  203. $items['bio_data/ajax/field_attach/%'] = array(
  204. 'page callback' => 'tripal_ajax_attach_field',
  205. 'page arguments' => array(3),
  206. 'access arguments' => array('access content'),
  207. 'type' => MENU_CALLBACK,
  208. 'file' => 'includes/tripal.entity.inc',
  209. 'file path' => drupal_get_path('module', 'tripal'),
  210. );
  211. /*
  212. * Dashboard Action Item callbacks.
  213. */
  214. $items['admin/disable/notification/%'] = array(
  215. 'page callback' => 'tripal_disable_admin_notification',
  216. 'page arguments' => array(3),
  217. 'access arguments' => array('access content'),
  218. 'type' => MENU_CALLBACK,
  219. 'file' => 'includes/tripal.admin_blocks.inc',
  220. 'file path' => drupal_get_path('module', 'tripal'),
  221. );
  222. $items['admin/import/field/%/%/%/%'] = array(
  223. 'page callback' => 'tripal_admin_notification_import_field',
  224. 'page arguments' => array(3, 4, 5, 6),
  225. 'access arguments' => array('access content'),
  226. 'type' => MENU_CALLBACK,
  227. 'file' => 'includes/tripal.admin_blocks.inc',
  228. 'file path' => drupal_get_path('module', 'tripal'),
  229. );
  230. /*
  231. * Term Lookup
  232. */
  233. // TODO: finish this menu callback.
  234. // $items['cv/lookup'] = array(
  235. // 'title' => 'Vocabulary Lookup',
  236. // 'description' => t("Provides a tool to discover controlled vocabularies and their terms used by this site."),
  237. // 'access arguments' => array('access content'),
  238. // 'page callback' => 'drupal_get_form',
  239. // 'page arguments' => array('tripal_vocabulary_lookup_form'),
  240. // 'file' => 'includes/tripal.term_lookup.inc',
  241. // 'file path' => drupal_get_path('module', 'tripal'),
  242. // 'type' => MENU_NORMAL_ITEM,
  243. // );
  244. $items['cv/lookup/%'] = array(
  245. 'title' => 'Vocabulary Lookup',
  246. 'description' => t("Provides a tool to discover controlled vocabularies"),
  247. 'access arguments' => array('access content'),
  248. 'page callback' => 'tripal_vocabulary_lookup_page',
  249. 'page arguments' => array(2),
  250. 'file' => 'includes/tripal.term_lookup.inc',
  251. 'file path' => drupal_get_path('module', 'tripal'),
  252. 'type' => MENU_CALLBACK,
  253. );
  254. $items['cv/lookup/%/%'] = array(
  255. 'title' => 'Vocabulary Term Lookup',
  256. 'description' => t("Provides a tool to discover controlled vocabularies terms used by this site."),
  257. 'access arguments' => array('access content'),
  258. 'page callback' => 'tripal_vocabulary_lookup_term_page',
  259. 'page arguments' => array(2, 3),
  260. 'file' => 'includes/tripal.term_lookup.inc',
  261. 'file path' => drupal_get_path('module', 'tripal'),
  262. 'type' => MENU_CALLBACK,
  263. );
  264. // Adds a +Check for new fields link on the 'Tripal Content Types' page.
  265. $items['admin/structure/bio_data/manage/%/fields/check'] = array(
  266. 'title' => 'Check for new fields',
  267. 'description' => t('Check if new fields should be added to this content type.'),
  268. 'page callback' => 'tripal_check_new_fields',
  269. 'page arguments' => array(4),
  270. 'access arguments' => array('administer tripal'),
  271. 'file' => 'api/tripal.entities.api.inc',
  272. 'file path' => drupal_get_path('module', 'tripal'),
  273. 'type' => MENU_LOCAL_ACTION,
  274. );
  275. $items['tripal/upload'] = array(
  276. 'page callback' => 'tripal_file_upload',
  277. 'access arguments' => array('upload files'),
  278. 'file' => '/includes/tripal.upload.inc',
  279. 'type' => MENU_CALLBACK,
  280. );
  281. // Add in the loaders
  282. $importers = tripal_get_importers();
  283. foreach ($importers as $class_name) {
  284. tripal_load_include_importer_class($class_name);
  285. if (class_exists($class_name)) {
  286. $items['admin/tripal/loaders/' . $class_name::$machine_name] = array(
  287. 'title' => $class_name::$name,
  288. 'description' => $class_name::$description,
  289. 'page callback' => 'drupal_get_form',
  290. 'page arguments' => array('tripal_get_importer_form', $class_name),
  291. 'access arguments' => array('administer tripal'),
  292. 'type' => MENU_NORMAL_ITEM,
  293. 'file' => 'includes/tripal.importer.inc',
  294. 'file path' => drupal_get_path('module', 'tripal'),
  295. );
  296. }
  297. }
  298. return $items;
  299. }
  300. /**
  301. * Implements hook_permission().
  302. */
  303. function tripal_permission() {
  304. $permissions = array(
  305. 'administer tripal' => array(
  306. 'title' => t('Administer Tripal'),
  307. 'description' => t('Allow the user to access administrative pages of Tripal. This includes management of jobs, the storage systems, extensions and the controlled vocabularies.'),
  308. 'restrict access' => TRUE,
  309. ),
  310. 'access tripal content overview' => array(
  311. 'title' => t('Access the Tripal content overview page'),
  312. 'description' => t('Get an overview of all Tripal content'),
  313. 'restrict access' => TRUE,
  314. ),
  315. 'manage tripal content types' => array(
  316. 'title' => t('Manage Tripal content types'),
  317. 'description' => t('Allows the user to create, update and delete Tripal content types.'),
  318. 'restrict access' => TRUE,
  319. ),
  320. 'upload files' => array(
  321. 'title' => t('Upload Files'),
  322. 'description' => t('Allows the user to upload files using Tripal\'s HTML5 loader.'),
  323. ),
  324. 'view dev helps' => array(
  325. 'title' => t('View Developer Hints'),
  326. 'description' => t('Tripal will provide blue shaded boxes that provide
  327. instructions for how to customize or setup specific pages on a
  328. site. This permission should be enabled for developers. But can
  329. be disabled once developers are accustomed to these hints.'),
  330. 'restrict access' => TRUE,
  331. ),
  332. );
  333. // Add permissions for each content type.
  334. $bundles = tripal_get_content_types();
  335. foreach ($bundles as $bundle) {
  336. $permissions['view ' . $bundle->name] = array(
  337. 'title' => t('%label: View Content', array('%label' => $bundle->label)),
  338. 'description' => t('Allow the user to view %label content', array('%label' => $bundle->label)),
  339. );
  340. $permissions['create ' . $bundle->name] = array(
  341. 'title' => t('%label: Create Content', array('%label' => $bundle->label)),
  342. 'description' => t('Allow the user to create %label content', array('%label' => $bundle->label)),
  343. 'restrict access' => TRUE,
  344. );
  345. $permissions['edit ' . $bundle->name] = array(
  346. 'title' => t('%label: Edit Content', array('%label' => $bundle->label)),
  347. 'description' => t('Allow the user to edit %label content', array('%label' => $bundle->label)),
  348. 'restrict access' => TRUE,
  349. );
  350. $permissions['delete ' . $bundle->name] = array(
  351. 'title' => t('%label: Delete Content', array('%label' => $bundle->label)),
  352. 'description' => t('Allow the user to delete %label content', array('%label' => $bundle->label)),
  353. 'restrict access' => TRUE,
  354. );
  355. }
  356. return $permissions;
  357. }
  358. /**
  359. * Implements hook_theme().
  360. * Registers template files/functions used by this module.
  361. *
  362. * @ingroup tripal
  363. */
  364. function tripal_theme($existing, $type, $theme, $path) {
  365. $themes = array(
  366. // Admin messages theme
  367. 'tripal_admin_message' => array(
  368. 'function' => 'theme_tripal_admin_message',
  369. 'variables' => array('message' => NULL),
  370. ),
  371. 'tripal_entity' => array(
  372. 'render element' => 'elements',
  373. 'template' => 'tripal_entity',
  374. 'path' => "$path/theme/templates"
  375. ),
  376. 'tripal_add_list' => array(
  377. 'variables' => array('content' => NULL),
  378. ),
  379. // Themeing for all fields.
  380. 'tripal_field_default' => array(
  381. 'render element' => 'element',
  382. 'file' => 'includes/tripal.fields.inc',
  383. ),
  384. );
  385. return $themes;
  386. }
  387. /**
  388. * Implements hook_coder_ignore().
  389. * Defines the path to the file (tripal.coder_ignores.txt) where ignore rules for coder are stored
  390. */
  391. function tripal_coder_ignore() {
  392. return array(
  393. 'path' => drupal_get_path('module', 'tripal'),
  394. 'line prefix' => drupal_get_path('module', 'tripal'),
  395. );
  396. }
  397. /**
  398. * Implements hook_libraries_info().
  399. */
  400. function tripal_libraries_info() {
  401. $libraries = array();
  402. $libraries['d3'] = array(
  403. 'name' => 'D3.js',
  404. 'vendor url' => 'http://d3js.org/',
  405. 'download url' => 'https://github.com/mbostock/d3',
  406. 'version arguments' => array(
  407. 'file' => 'd3.js',
  408. 'pattern' => '/\s*version: "(\d+\.\d+\.\d+)"/',
  409. ),
  410. 'files' => array(
  411. 'js' => array(
  412. 'd3.min.js',
  413. ),
  414. ),
  415. );
  416. return $libraries;
  417. }
  418. /**
  419. * Implements hook_admin_paths().
  420. * Define administrative paths.
  421. */
  422. function tripal_admin_paths() {
  423. if (variable_get('node_admin_theme')) {
  424. $paths = array(
  425. 'bio_data/*/edit' => TRUE,
  426. 'bio_data/*/delete' => TRUE,
  427. 'bio_data/add' => TRUE,
  428. 'bio_data/add/*' => TRUE,
  429. );
  430. return $paths;
  431. }
  432. }
  433. /**
  434. * Implements hook_menu_local_tasks_alter().
  435. *
  436. * Used to add action links to pages.
  437. */
  438. function tripal_menu_local_tasks_alter(&$data, $router_item, $root_path) {
  439. // Add an "Add Tripal Content" action link to the Admin >> Content >>
  440. // Biological Content page.
  441. if ($root_path == 'admin/content/bio_data') {
  442. $item = menu_get_item('bio_data/add');
  443. if ($item['access']) {
  444. $data['actions']['output'][] = array(
  445. '#theme' => 'menu_local_action',
  446. '#link' => $item,
  447. );
  448. }
  449. }
  450. }
  451. /**
  452. * Implements hook_shortcut_default_set().
  453. * Modify the shortcut menu to include Biological content links.
  454. *
  455. * @param object $account
  456. * The user account whose default shortcut set will be returned. If not provided, the
  457. * function will return the currently logged-in user's default shortcut set.
  458. *
  459. * @return
  460. * An object representing the default shortcut set.
  461. */
  462. function tripal_shortcut_default_set($account) {
  463. $sets = shortcut_sets();
  464. $found = FALSE;
  465. foreach ($sets as $set) {
  466. if ($set->title == 'TripalDefault') {
  467. $found = TRUE;
  468. }
  469. }
  470. if (!$found) {
  471. $t = get_t();
  472. // Create an initial default shortcut set.
  473. $shortcut_set = new stdClass();
  474. $shortcut_set->title = $t('TripalDefault');
  475. $shortcut_set->links = array(
  476. array(
  477. 'link_path' => 'node/add',
  478. 'link_title' => $t('Add content'),
  479. 'weight' => -35,
  480. ),
  481. array(
  482. 'link_path' => 'bio_data/add',
  483. 'link_title' => 'Add Tripal Content',
  484. 'weight' => -30,
  485. ),
  486. array(
  487. 'link_path' => 'admin/content',
  488. 'link_title' => $t('Find content'),
  489. 'weight' => -25,
  490. ),
  491. array(
  492. 'link_path' => 'admin/content/bio_data',
  493. 'link_title' => 'Find Tripal Content',
  494. 'weight' => -20,
  495. ),
  496. );
  497. shortcut_set_save($shortcut_set);
  498. }
  499. $sets = shortcut_sets();
  500. foreach ($sets as $set) {
  501. if ($set->title == 'TripalDefault') {
  502. return $set->set_name;
  503. }
  504. }
  505. }
  506. /**
  507. * Menu argument loader; Load a tripal data type by string.
  508. *
  509. * This function is not meant to be used as an API function. It is only meant
  510. * for use in the menu to resolve the %tripal_bundle wildcard.
  511. *
  512. * @param $type
  513. * The machine-readable name of a tripal data type to load.
  514. * @return
  515. * A tripal data type array or FALSE if $type does not exist.
  516. */
  517. function TripalBundle_load($bundle_type, $reset = FALSE) {
  518. // Get the type of entity by the ID.
  519. $bundle = db_select('tripal_bundle', 'tdt')
  520. ->fields('tdt')
  521. ->condition('name', $bundle_type)
  522. ->execute()
  523. ->fetchObject();
  524. if ($bundle) {
  525. $entity = entity_load('TripalBundle', array($bundle->id), array(), $reset);
  526. return reset($entity);
  527. }
  528. return FALSE;
  529. }
  530. /**
  531. * Allows the menu system to use a wildcard to fetch the entity.
  532. *
  533. * Make sure that the wildcard you choose in the tripal_entity entity
  534. * definition fits the function name here.
  535. *
  536. * This function is not meant to be used as an API function. It is only meant
  537. * for use in the menu to resolve the %tripal_entity wildcard.
  538. *
  539. * @param $id
  540. * Integer specifying the tripal_entity id.
  541. * @param $reset
  542. * A boolean indicating that the internal cache should be reset.
  543. * @return
  544. * A fully-loaded $tripal_entity object or FALSE if it cannot be loaded.
  545. *
  546. * @see tripal_entity_load_multiple()
  547. */
  548. function TripalEntity_load($id, $reset = FALSE) {
  549. // $entity = entity_load('TripalEntity', array($id), array(), $reset);
  550. $entity = tripal_load_entity('TripalEntity', array($id), $reset);
  551. return reset($entity);
  552. }
  553. /**
  554. * Imports all of the Tripal API into scope.
  555. *
  556. * Typically this function call is not necessary as all of the API is
  557. * automaticaly included by the tripal module. However this function can
  558. * be useful in the .install files during a site upgrade when the tripal
  559. * module is not enabld.
  560. *
  561. * Example usage:
  562. * @code
  563. * module_load_include('module', 'tripal', 'tripal');
  564. * tripal_import_api();
  565. * @endcode
  566. *
  567. */
  568. function tripal_import_api() {
  569. module_load_include('inc', 'tripal', 'api/tripal.d3js.api');
  570. module_load_include('inc', 'tripal', 'api/tripal.fields.api');
  571. module_load_include('inc', 'tripal', 'api/tripal.importer.api');
  572. module_load_include('inc', 'tripal', 'api/tripal.terms.api');
  573. module_load_include('inc', 'tripal', 'api/tripal.entities.api');
  574. module_load_include('inc', 'tripal', 'api/tripal.files.api');
  575. module_load_include('inc', 'tripal', 'api/tripal.jobs.api');
  576. module_load_include('inc', 'tripal', 'api/tripal.notice.api');
  577. module_load_include('inc', 'tripal', 'api/tripal.variables.api');
  578. module_load_include('inc', 'tripal', 'api/tripal.DEPRECATED.api');
  579. }
  580. /**
  581. * Implements hook_form_alter().
  582. */
  583. function tripal_form_alter(&$form, $form_state, $form_id) {
  584. // If this is the field_ui_field_edit_form (i.e. the form that appears
  585. // when editing a field that is attached to an entity). Then we want
  586. // to add term settings for any field attached to a TripalEntity
  587. // content type.
  588. if ($form_id == 'field_ui_field_edit_form' and $form['#instance']['entity_type'] == 'TripalEntity') {
  589. tripal_field_instance_settings_form_alter($form, $form_state);
  590. }
  591. }
  592. function tripal_check_new_fields($bundle_name) {
  593. $bundle = tripal_load_bundle_entity(array('name' => $bundle_name));
  594. $term = tripal_load_term_entity(array('term_id' => $bundle->term_id));
  595. $added = tripal_create_bundle_fields($bundle, $term);
  596. if (count($added) == 0) {
  597. drupal_set_message('No new fields were added');
  598. }
  599. foreach ($added as $field_name) {
  600. drupal_set_message('Added field: ' . $field_name);
  601. }
  602. drupal_goto("admin/structure/bio_data/manage/$bundle_name/fields");
  603. }
  604. /**
  605. * Implements hook_block_info().
  606. */
  607. function tripal_block_info() {
  608. $blocks = array();
  609. $admin_theme = 'seven';
  610. $blocks['notifications_block'] = array(
  611. 'info' => t('Dashboard Notifications'),
  612. 'visibility' => BLOCK_VISIBILITY_LISTED,
  613. 'pages' => 'admin/dashboard',
  614. 'status' => TRUE,
  615. 'theme' => $admin_theme,
  616. 'region' => 'dashboard_main',
  617. 'properties' => array(
  618. 'administrative' => TRUE,
  619. ),
  620. );
  621. $blocks['content_type_barchart'] = array(
  622. 'info' => t('Tripal Content Type Count'),
  623. 'visibility' => BLOCK_VISIBILITY_LISTED,
  624. 'pages' => 'admin/dashboard',
  625. 'status' => TRUE,
  626. 'theme' => $admin_theme,
  627. 'region' => 'dashboard_main',
  628. 'properties' => array(
  629. 'administrative' => TRUE,
  630. ),
  631. );
  632. $blocks['powered_by_tripal'] = array(
  633. 'info' => t('Powered by Tripal'),
  634. 'cache' => DRUPAL_NO_CACHE,
  635. );
  636. return $blocks;
  637. }
  638. /**
  639. * Implements hook_block_view().
  640. */
  641. function tripal_block_view($delta = ''){
  642. global $base_path;
  643. $block = array();
  644. // The $delta parameter tells us which block is being requested.
  645. switch ($delta) {
  646. case 'powered_by_tripal':
  647. $size = variable_get('powered_by_tripal_size', 'small');
  648. $type = variable_get('powered_by_tripal_type', 'bw');
  649. $image = 'powered_by_tripal_bw_small.png';
  650. if ($size == 'small' and $type == 'col') {
  651. $image = 'powered_by_tripal_small.png';
  652. }
  653. if ($size == 'large' and $type == 'bw') {
  654. $image = 'powered_by_tripal_bw.png';
  655. }
  656. if ($size == 'large' and $type == 'col') {
  657. $image = 'powered_by_tripal.png';
  658. }
  659. $block['title'] = '';
  660. $block['content'] = array(
  661. '#markup' => '<a href="http://tripal.info"><img border="0" src="' . $base_path . drupal_get_path('module', 'tripal') . '/theme/images/' . $image . '"></a>',
  662. );
  663. break;
  664. case 'content_type_barchart':
  665. // The number of content types
  666. $entity_types = db_select('tripal_bundle', 'tb')
  667. ->fields('tb')
  668. ->execute()
  669. ->fetchAll();
  670. $entity_count_listing = array();
  671. // The number of entities per content type.
  672. foreach($entity_types as $entity_types => $entity_type){
  673. $result = db_select('chado_'.$entity_type->name, 'et')
  674. ->fields('et')
  675. ->execute();
  676. $number_of_entities = $result->rowCount();
  677. $entity_count_listing[$entity_types] = array(
  678. 'name' => $entity_type->label,
  679. 'count' => $number_of_entities,
  680. );
  681. }
  682. tripal_add_d3js();
  683. drupal_add_js(drupal_get_path ('module', 'tripal') . '/theme/js/tripal.dashboard.js');
  684. drupal_add_css(drupal_get_path ('module', 'tripal') . '/theme/css/tripal.dashboard.css');
  685. drupal_add_library('system', 'drupal.collapse');
  686. drupal_add_js("var entityCountListing = " . json_encode($entity_count_listing) . ";", array('type' => 'inline'));
  687. $output = "<div id=\"tripal-entity-types\" class=\"tripal-entity-types-pane\">
  688. <p>A list of the Tripal Content Types and the number of each.</p>
  689. <div id=\"tripal-entity-type-chart\"></div>
  690. </div>";
  691. $block['title'] = '';
  692. $block['content'] = array(
  693. '#markup' => $output,
  694. );
  695. break;
  696. case 'notifications_block':
  697. // Create your block content here
  698. $block['content'] = '';
  699. // Prepare table header
  700. $header = array(
  701. 'title' => array('data' => t('Title')),
  702. 'details' => array('data' => t('Details')),
  703. 'type' => array('data' => t('Type'), 'field' => 'tan.type'),
  704. 'actions' => array('data' => t('Actions'))
  705. );
  706. $query = db_select('tripal_admin_notfications', 'tan')
  707. ->extend('TableSort');
  708. $results = $query->fields('tan')
  709. ->condition('enabled', 1, '=')
  710. ->orderByHeader($header)
  711. ->execute()->fetchAll();
  712. $rows = array();
  713. foreach($results as $result){
  714. $data['operation'] = ' | ';
  715. $data['operation'] .= l(t('Dismiss Notification'), 'admin/disable/notification/' . $result->note_id);
  716. $actions = unserialize($result->actions);
  717. foreach($actions as $action){
  718. $label = key($actions);
  719. $link = $action;
  720. }
  721. $rows[] = array(
  722. 'Title' => $result->title,
  723. 'Details' => $result->details,
  724. 'Type' => $result->type,
  725. 'Actions' => l(t($label), $link) . $data['operation'],
  726. );
  727. }
  728. if(!empty($rows)) {
  729. //Number of records shown in per page
  730. $per_page = 10;
  731. $current_page = pager_default_initialize(count($rows), $per_page);
  732. $chunks = array_chunk($rows, $per_page, TRUE);
  733. // Output of table with the paging
  734. $table = theme('table',
  735. array(
  736. "header" => $header,
  737. "rows" => $chunks[ $current_page ],
  738. "attributes" => array(),
  739. "sticky" => TRUE,
  740. "caption" => "",
  741. "colgroups" => array(),
  742. "empty" => t("No notifications.")
  743. )
  744. );
  745. $table .= theme('pager', array('quantity', count($rows)));
  746. $fieldset_table = array(
  747. '#title' => t('Notifications'),
  748. '#collapsed' => FALSE,
  749. '#collapsible' => TRUE,
  750. '#attributes' => array('class' => array('collapsible')),
  751. '#children' => $table,
  752. );
  753. //return pager with limited number of records.
  754. $block['content'] = theme('fieldset', array('element' => $fieldset_table));
  755. }
  756. else {
  757. $block['content'] = 'There are no notifications at this time.';
  758. }
  759. $block['title'] = 'Tripal Administrative Notifications';
  760. break;
  761. }
  762. return $block;
  763. }
  764. /**
  765. * Implements hook_block_save().
  766. */
  767. function tripal_block_save($delta = '', $edit = array()) {
  768. switch ($delta) {
  769. case 'powered_by_tripal':
  770. if (!empty($edit['logo_size'])) {
  771. variable_set('powered_by_tripal_size', $edit['logo_size']);
  772. }
  773. if (!empty($edit['logo_type'])) {
  774. variable_set('powered_by_tripal_type', $edit['logo_type']);
  775. }
  776. }
  777. }
  778. /**
  779. * Implements hook_block_configure().
  780. */
  781. function tripal_block_configure ($delta = '') {
  782. $form = array();
  783. switch ($delta) {
  784. case 'powered_by_tripal':
  785. $form['logo_size'] = array(
  786. '#type' => 'radios',
  787. '#title' => t('Logo Size'),
  788. '#default_value' => variable_get('powered_by_tripal_size', 'small'),
  789. '#options' => array(
  790. 'large' => t('Large'),
  791. 'small' => t('Small')
  792. ),
  793. '#description' => t('Select if you would like a small or large "Powered by Tripal" logo.'),
  794. );
  795. $form['logo_type'] = array(
  796. '#type' => 'radios',
  797. '#title' => t('Logo Type'),
  798. '#default_value' => variable_get('powered_by_tripal_type', 'bw'),
  799. '#options' => array(
  800. 'bw' => t('Gray scale'),
  801. 'col' => t('Colored')
  802. ),
  803. '#description' => t('Select if you would like a black and white or colored "Powered by Tripal" logo.'),
  804. );
  805. }
  806. return $form;
  807. }
  808. /**
  809. * Implements hook_cron().
  810. */
  811. function tripal_cron() {
  812. if (variable_get('tripal_admin_notification_creation_during_cron', TRUE)) {
  813. $modules = module_implements('tripal_cron_notification');
  814. foreach ($modules as $module) {
  815. $function = $module . '_tripal_cron_notification';
  816. $function();
  817. }
  818. watchdog('tripal_cron', 'tripal_cron ran');
  819. }
  820. }
  821. /**
  822. * Implements hook_element_info().
  823. *
  824. * Used for creating new form API elements.
  825. */
  826. function tripal_element_info() {
  827. // Element for uploading large files. This form element
  828. // accepts the following keys when using in a form:
  829. // - #title: The title that will appear above the element.
  830. // - #description: The description that will appear below the element.
  831. // - #usage_type: Required. The type of file. This will be stored in
  832. // the 'type' column of the file_usage table.
  833. // - #usage_id: Required. A unique numeric ID representing an entity, node
  834. // or some other record identifier. This can be any identifier that
  835. // makes sense to the module that implements a form that uses this
  836. // element.
  837. $elements['html5_file'] = array(
  838. '#input' => 'TRUE',
  839. '#process' => array('tripal_html5_file_process'),
  840. '#element_validate' => array('tripal_html5_file_validate'),
  841. '#value_callback' => 'tripal_html5_file_value',
  842. );
  843. return $elements;
  844. }
  845. /**
  846. * The process function for the html5_file form element.
  847. */
  848. function tripal_html5_file_process($element, $form_state, $complete_form) {
  849. $module = array_key_exists('#usage_module', $element) ? $element['#usage_module'] : 'tripal';
  850. $type = $element['#usage_id'] . '-' . $element['#usage_type'] . '-' . $module;
  851. $name = $element['#name'];
  852. $name = preg_replace('/[^\w]/', '_', $name);
  853. $headers = array(
  854. array('data' => 'File'),
  855. array('data' => 'Size', 'width' => '10%'),
  856. array('data' => 'Upload Progress', 'width' => '20%'),
  857. array('data' => 'Action', 'width' => '10%')
  858. );
  859. $rows = array();
  860. $table_vars = array(
  861. 'header' => $headers,
  862. 'rows' => $rows,
  863. 'attributes' => array(
  864. 'class' => array('tripal-html5-file-upload-table'),
  865. 'id' => 'tripal-html5-file-upload-table-' . $type
  866. ),
  867. 'sticky' => TRUE,
  868. 'colgroups' => array(),
  869. 'empty' => t('There are currently no files.'),
  870. );
  871. $element['html5_file_table_key'] = array(
  872. '#type' => 'hidden',
  873. '#value' => $type,
  874. '#attributes' => array(
  875. 'class' => array('tripal-html5-file-upload-table-key')
  876. )
  877. );
  878. $element['html5_file_table'] = array(
  879. '#type' => 'item',
  880. '#title' => $element['#title'],
  881. '#description' => $element['#description'],
  882. '#markup' => theme('table', $table_vars)
  883. );
  884. $element[$name] = array(
  885. '#type' => 'hidden',
  886. '#attributes' => array('id' => 'tripal-html5-upload-fid-' . $type),
  887. '#default_value' => $element['#value'],
  888. );
  889. $element['html5_file_submit'] = array(
  890. '#type' => 'submit',
  891. '#value' => 'Upload File',
  892. '#name' => 'tripal_html5_file_upload_submit-' . $type,
  893. // We don't want this button to submit as the file upload
  894. // is handled by the JavaScript code.
  895. '#attributes' => array(
  896. 'id' => 'tripal-html5-file-upload-submit-' . $type,
  897. 'onclick' => 'return (false);'
  898. )
  899. );
  900. drupal_add_js(drupal_get_path ('module', 'tripal') . '/theme/js/TripalUploader.js');
  901. drupal_add_js(drupal_get_path ('module', 'tripal') . '/theme/js/TripalUploadFile.js');
  902. drupal_add_js(drupal_get_path ('module', 'tripal') . '/theme/js/tripal.file.js');
  903. return $element;
  904. }
  905. /**
  906. *
  907. */
  908. function tripal_html5_file_validate($element, &$form_state) {
  909. $is_required = $element['#required'];
  910. $fid = $element['#value'];
  911. if ($is_required and !$fid) {
  912. form_error($element, t('A file must be uploaded.'));
  913. }
  914. }
  915. /**
  916. * Implements hook_handle_uploaded_file().
  917. */
  918. function tripal_handle_uploaded_file($filename, $filepath, $type) {
  919. global $user;
  920. // Split the type into a node ID and form_key
  921. list($id, $form_key) = explode('-', $type);
  922. // See if this file is already managed then add another entry fin the
  923. // usage table.
  924. $fid = db_select('file_managed', 'fm')
  925. ->fields('fm', array('fid'))
  926. ->condition('uri', $filepath)
  927. ->execute()
  928. ->fetchField();
  929. // Create a file object.
  930. if (!$fid) {
  931. $file = new stdClass();
  932. $file->uri = $filepath;
  933. $file->filename = $filename;
  934. $file->filemime = file_get_mimetype($filepath);
  935. $file->uid = $user->uid;
  936. $file->status = FILE_STATUS_PERMANENT;
  937. $file = file_save($file);
  938. $fid = $file->fid;
  939. }
  940. $file = file_load($fid);
  941. file_usage_add($file, 'tripal', $form_key, $id);
  942. return $fid;
  943. }
  944. /**
  945. * Implements hook_value() for the html5_file form element.
  946. */
  947. function tripal_html5_file_value($element, $input = FALSE, &$form_state) {
  948. if ($input) {
  949. if (is_array($input)) {
  950. $name = $element['#name'];
  951. $name = preg_replace('/[^\w]/', '_', $name);
  952. return $input[$name];
  953. }
  954. else {
  955. return $input;
  956. }
  957. }
  958. }
  959. /**
  960. * Implements hook_field_display_alter().
  961. *
  962. * @param $display
  963. * @param $context
  964. */
  965. function tripal_field_display_TripalEntity_alter(&$display, $context){
  966. $field_name = $context['field']['field_name'];
  967. $bundle = $context['entity']->bundle;
  968. $bundle_info = tripal_load_bundle_entity(array('name' => $bundle));
  969. // Hide fields that are empty, but only if the hide_empty_field variable
  970. // is set to 'hide' for this bundel.
  971. $hide_variable = tripal_get_bundle_variable('hide_empty_field', $bundle_info->id, 'hide');
  972. if($hide_variable == 'hide'){
  973. $item = field_get_items('TripalEntity', $context['entity'], $field_name);
  974. if($item) {
  975. $field = field_info_field($field_name);
  976. if(tripal_field_is_empty($item[0], $field)) {
  977. // Stop the right rail element from rendering.
  978. drupal_add_css('.' . $field_name.' {display: none;}', 'inline');
  979. }
  980. }
  981. }
  982. }
  983. /**
  984. * Implements hook_field_group_table_rows_alter().
  985. *
  986. * This hook is used for hiding empty rows in a Field Group Table field group.
  987. *
  988. * @param $element
  989. * The field group object.
  990. * @param $children
  991. * An array of field names that are cpntained in the field group
  992. */
  993. function tripal_field_group_table_rows_alter(&$element, &$children) {
  994. // Iterate through the children of this table field group.
  995. foreach ($children as $index => $child) {
  996. if (is_array($element[$child])) {
  997. $bundle = $element[$child]['#bundle'];
  998. $bundle_info = tripal_load_bundle_entity(array('name' => $bundle));
  999. // If the hide empty variable is turned on then remove fields from
  1000. // the field group.
  1001. $hide_variable = tripal_get_bundle_variable('hide_empty_field', $bundle_info->id, 'hide');
  1002. if($hide_variable == 'hide'){
  1003. $items = $element[$child]['#items'];
  1004. // Case #1: there are no items.
  1005. if (count($items) == 0) {
  1006. unset($children[$index]);
  1007. unset($element[$child]);
  1008. }
  1009. // Case #2: there is one item but the value is empty.
  1010. if (count($items) == 1 and array_key_exists('value', $items[0]) and empty($items[0]['value'])) {
  1011. unset($children[$index]);
  1012. unset($element[$child]);
  1013. }
  1014. }
  1015. }
  1016. }
  1017. }