tripal.module 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158
  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. $items['admin/tripal/tripal_jobs/execute/%'] = array(
  201. 'title' => 'Jobs',
  202. 'description' => t('Execute an existing job'),
  203. 'page callback' => 'tripal_execute_job',
  204. 'page arguments' => array(4),
  205. 'access arguments' => array('administer tripal'),
  206. 'type' => MENU_CALLBACK,
  207. );
  208. /*
  209. * AJAX Callbacks.
  210. */
  211. $items['bio_data/ajax/field_attach/%'] = array(
  212. 'page callback' => 'tripal_ajax_attach_field',
  213. 'page arguments' => array(3),
  214. 'access arguments' => array('access content'),
  215. 'type' => MENU_CALLBACK,
  216. 'file' => 'includes/tripal.entity.inc',
  217. 'file path' => drupal_get_path('module', 'tripal'),
  218. );
  219. /*
  220. * Dashboard Action Item callbacks.
  221. */
  222. $items['admin/disable/notification/%'] = array(
  223. 'page callback' => 'tripal_disable_admin_notification',
  224. 'page arguments' => array(3),
  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. $items['admin/import/field/%/%/%/%'] = array(
  231. 'page callback' => 'tripal_admin_notification_import_field',
  232. 'page arguments' => array(3, 4, 5, 6),
  233. 'access arguments' => array('access content'),
  234. 'type' => MENU_CALLBACK,
  235. 'file' => 'includes/tripal.admin_blocks.inc',
  236. 'file path' => drupal_get_path('module', 'tripal'),
  237. );
  238. /*
  239. * Term Lookup
  240. */
  241. // TODO: finish this menu callback.
  242. // $items['cv/lookup'] = array(
  243. // 'title' => 'Vocabulary Lookup',
  244. // 'description' => t("Provides a tool to discover controlled vocabularies and their terms used by this site."),
  245. // 'access arguments' => array('access content'),
  246. // 'page callback' => 'drupal_get_form',
  247. // 'page arguments' => array('tripal_vocabulary_lookup_form'),
  248. // 'file' => 'includes/tripal.term_lookup.inc',
  249. // 'file path' => drupal_get_path('module', 'tripal'),
  250. // 'type' => MENU_NORMAL_ITEM,
  251. // );
  252. $items['cv/lookup/%'] = array(
  253. 'title' => 'Vocabulary Lookup',
  254. 'description' => t("Provides a tool to discover controlled vocabularies"),
  255. 'access arguments' => array('access content'),
  256. 'page callback' => 'tripal_vocabulary_lookup_page',
  257. 'page arguments' => array(2),
  258. 'file' => 'includes/tripal.term_lookup.inc',
  259. 'file path' => drupal_get_path('module', 'tripal'),
  260. 'type' => MENU_CALLBACK,
  261. );
  262. $items['cv/lookup/%/%'] = array(
  263. 'title' => 'Vocabulary Term Lookup',
  264. 'description' => t("Provides a tool to discover controlled vocabularies terms used by this site."),
  265. 'access arguments' => array('access content'),
  266. 'page callback' => 'tripal_vocabulary_lookup_term_page',
  267. 'page arguments' => array(2, 3),
  268. 'file' => 'includes/tripal.term_lookup.inc',
  269. 'file path' => drupal_get_path('module', 'tripal'),
  270. 'type' => MENU_CALLBACK,
  271. );
  272. // Adds a +Check for new fields link on the 'Tripal Content Types' page.
  273. $items['admin/structure/bio_data/manage/%/fields/check'] = array(
  274. 'title' => 'Check for new fields',
  275. 'description' => t('Check if new fields should be added to this content type.'),
  276. 'page callback' => 'tripal_check_new_fields',
  277. 'page arguments' => array(4),
  278. 'access arguments' => array('administer tripal'),
  279. 'file' => 'api/tripal.entities.api.inc',
  280. 'file path' => drupal_get_path('module', 'tripal'),
  281. 'type' => MENU_LOCAL_ACTION,
  282. );
  283. $items['tripal/upload'] = array(
  284. 'page callback' => 'tripal_file_upload',
  285. 'access arguments' => array('upload files'),
  286. 'file' => '/includes/tripal.upload.inc',
  287. 'type' => MENU_CALLBACK,
  288. );
  289. // Add in the loaders
  290. $importers = tripal_get_importers();
  291. foreach ($importers as $class_name) {
  292. tripal_load_include_importer_class($class_name);
  293. if (class_exists($class_name)) {
  294. $items['admin/tripal/loaders/' . $class_name::$machine_name] = array(
  295. 'title' => $class_name::$name,
  296. 'description' => $class_name::$description,
  297. 'page callback' => 'drupal_get_form',
  298. 'page arguments' => array('tripal_get_importer_form', $class_name),
  299. 'access arguments' => array('administer tripal'),
  300. 'type' => MENU_NORMAL_ITEM,
  301. 'file' => 'includes/tripal.importer.inc',
  302. 'file path' => drupal_get_path('module', 'tripal'),
  303. );
  304. }
  305. }
  306. return $items;
  307. }
  308. /**
  309. * Implements hook_permission().
  310. */
  311. function tripal_permission() {
  312. $permissions = array(
  313. 'administer tripal' => array(
  314. 'title' => t('Administer Tripal'),
  315. 'description' => t('Allow the user to access administrative pages of Tripal. This includes management of jobs, the storage systems, extensions and the controlled vocabularies.'),
  316. 'restrict access' => TRUE,
  317. ),
  318. 'access tripal content overview' => array(
  319. 'title' => t('Access the Tripal content overview page'),
  320. 'description' => t('Get an overview of all Tripal content'),
  321. 'restrict access' => TRUE,
  322. ),
  323. 'manage tripal content types' => array(
  324. 'title' => t('Manage Tripal content types'),
  325. 'description' => t('Allows the user to create, update and delete Tripal content types.'),
  326. 'restrict access' => TRUE,
  327. ),
  328. 'upload files' => array(
  329. 'title' => t('Upload Files'),
  330. 'description' => t('Allows the user to upload files using Tripal\'s HTML5 loader.'),
  331. ),
  332. 'view dev helps' => array(
  333. 'title' => t('View Developer Hints'),
  334. 'description' => t('Tripal will provide blue shaded boxes that provide
  335. instructions for how to customize or setup specific pages on a
  336. site. This permission should be enabled for developers. But can
  337. be disabled once developers are accustomed to these hints.'),
  338. 'restrict access' => TRUE,
  339. ),
  340. );
  341. // Add permissions for each content type.
  342. $bundles = tripal_get_content_types();
  343. foreach ($bundles as $bundle) {
  344. $permissions['view ' . $bundle->name] = array(
  345. 'title' => t('%label: View Content', array('%label' => $bundle->label)),
  346. 'description' => t('Allow the user to view %label content', array('%label' => $bundle->label)),
  347. );
  348. $permissions['create ' . $bundle->name] = array(
  349. 'title' => t('%label: Create Content', array('%label' => $bundle->label)),
  350. 'description' => t('Allow the user to create %label content', array('%label' => $bundle->label)),
  351. 'restrict access' => TRUE,
  352. );
  353. $permissions['edit ' . $bundle->name] = array(
  354. 'title' => t('%label: Edit Content', array('%label' => $bundle->label)),
  355. 'description' => t('Allow the user to edit %label content', array('%label' => $bundle->label)),
  356. 'restrict access' => TRUE,
  357. );
  358. $permissions['delete ' . $bundle->name] = array(
  359. 'title' => t('%label: Delete Content', array('%label' => $bundle->label)),
  360. 'description' => t('Allow the user to delete %label content', array('%label' => $bundle->label)),
  361. 'restrict access' => TRUE,
  362. );
  363. }
  364. return $permissions;
  365. }
  366. /**
  367. * Implements hook_theme().
  368. * Registers template files/functions used by this module.
  369. *
  370. * @ingroup tripal
  371. */
  372. function tripal_theme($existing, $type, $theme, $path) {
  373. $themes = array(
  374. // Admin messages theme
  375. 'tripal_admin_message' => array(
  376. 'function' => 'theme_tripal_admin_message',
  377. 'variables' => array('message' => NULL),
  378. ),
  379. 'tripal_entity' => array(
  380. 'render element' => 'elements',
  381. 'template' => 'tripal_entity',
  382. 'path' => "$path/theme/templates"
  383. ),
  384. 'tripal_add_list' => array(
  385. 'variables' => array('content' => NULL),
  386. ),
  387. // Themeing for all fields.
  388. 'tripal_field_default' => array(
  389. 'render element' => 'element',
  390. 'file' => 'includes/tripal.fields.inc',
  391. ),
  392. );
  393. return $themes;
  394. }
  395. /**
  396. * Implements hook_coder_ignore().
  397. * Defines the path to the file (tripal.coder_ignores.txt) where ignore rules for coder are stored
  398. */
  399. function tripal_coder_ignore() {
  400. return array(
  401. 'path' => drupal_get_path('module', 'tripal'),
  402. 'line prefix' => drupal_get_path('module', 'tripal'),
  403. );
  404. }
  405. /**
  406. * Implements hook_libraries_info().
  407. */
  408. function tripal_libraries_info() {
  409. $libraries = array();
  410. $libraries['d3'] = array(
  411. 'name' => 'D3.js',
  412. 'vendor url' => 'http://d3js.org/',
  413. 'download url' => 'https://github.com/mbostock/d3',
  414. 'version arguments' => array(
  415. 'file' => 'd3.js',
  416. 'pattern' => '/\s*version: "(\d+\.\d+\.\d+)"/',
  417. ),
  418. 'files' => array(
  419. 'js' => array(
  420. 'd3.min.js',
  421. ),
  422. ),
  423. );
  424. return $libraries;
  425. }
  426. /**
  427. * Implements hook_admin_paths().
  428. * Define administrative paths.
  429. */
  430. function tripal_admin_paths() {
  431. if (variable_get('node_admin_theme')) {
  432. $paths = array(
  433. 'bio_data/*/edit' => TRUE,
  434. 'bio_data/*/delete' => TRUE,
  435. 'bio_data/add' => TRUE,
  436. 'bio_data/add/*' => TRUE,
  437. );
  438. return $paths;
  439. }
  440. }
  441. /**
  442. * Implements hook_menu_local_tasks_alter().
  443. *
  444. * Used to add action links to pages.
  445. */
  446. function tripal_menu_local_tasks_alter(&$data, $router_item, $root_path) {
  447. // Add an "Add Tripal Content" action link to the Admin >> Content >>
  448. // Biological Content page.
  449. if ($root_path == 'admin/content/bio_data') {
  450. $item = menu_get_item('bio_data/add');
  451. if ($item['access']) {
  452. $data['actions']['output'][] = array(
  453. '#theme' => 'menu_local_action',
  454. '#link' => $item,
  455. );
  456. }
  457. }
  458. }
  459. /**
  460. * Implements hook_shortcut_default_set().
  461. * Modify the shortcut menu to include Biological content links.
  462. *
  463. * @param object $account
  464. * The user account whose default shortcut set will be returned. If not provided, the
  465. * function will return the currently logged-in user's default shortcut set.
  466. *
  467. * @return
  468. * An object representing the default shortcut set.
  469. */
  470. function tripal_shortcut_default_set($account) {
  471. $sets = shortcut_sets();
  472. $found = FALSE;
  473. foreach ($sets as $set) {
  474. if ($set->title == 'TripalDefault') {
  475. $found = TRUE;
  476. }
  477. }
  478. if (!$found) {
  479. $t = get_t();
  480. // Create an initial default shortcut set.
  481. $shortcut_set = new stdClass();
  482. $shortcut_set->title = $t('TripalDefault');
  483. $shortcut_set->links = array(
  484. array(
  485. 'link_path' => 'node/add',
  486. 'link_title' => $t('Add content'),
  487. 'weight' => -35,
  488. ),
  489. array(
  490. 'link_path' => 'bio_data/add',
  491. 'link_title' => 'Add Tripal Content',
  492. 'weight' => -30,
  493. ),
  494. array(
  495. 'link_path' => 'admin/content',
  496. 'link_title' => $t('Find content'),
  497. 'weight' => -25,
  498. ),
  499. array(
  500. 'link_path' => 'admin/content/bio_data',
  501. 'link_title' => 'Find Tripal Content',
  502. 'weight' => -20,
  503. ),
  504. );
  505. shortcut_set_save($shortcut_set);
  506. }
  507. $sets = shortcut_sets();
  508. foreach ($sets as $set) {
  509. if ($set->title == 'TripalDefault') {
  510. return $set->set_name;
  511. }
  512. }
  513. }
  514. /**
  515. * Menu argument loader; Load a tripal data type by string.
  516. *
  517. * This function is not meant to be used as an API function. It is only meant
  518. * for use in the menu to resolve the %tripal_bundle wildcard.
  519. *
  520. * @param $type
  521. * The machine-readable name of a tripal data type to load.
  522. * @return
  523. * A tripal data type array or FALSE if $type does not exist.
  524. */
  525. function TripalBundle_load($bundle_type, $reset = FALSE) {
  526. // Get the type of entity by the ID.
  527. $bundle = db_select('tripal_bundle', 'tdt')
  528. ->fields('tdt')
  529. ->condition('name', $bundle_type)
  530. ->execute()
  531. ->fetchObject();
  532. if ($bundle) {
  533. $entity = entity_load('TripalBundle', array($bundle->id), array(), $reset);
  534. return reset($entity);
  535. }
  536. return FALSE;
  537. }
  538. /**
  539. * Allows the menu system to use a wildcard to fetch the entity.
  540. *
  541. * Make sure that the wildcard you choose in the tripal_entity entity
  542. * definition fits the function name here.
  543. *
  544. * This function is not meant to be used as an API function. It is only meant
  545. * for use in the menu to resolve the %tripal_entity wildcard.
  546. *
  547. * @param $id
  548. * Integer specifying the tripal_entity id.
  549. * @param $reset
  550. * A boolean indicating that the internal cache should be reset.
  551. * @return
  552. * A fully-loaded $tripal_entity object or FALSE if it cannot be loaded.
  553. *
  554. * @see tripal_entity_load_multiple()
  555. */
  556. function TripalEntity_load($id, $reset = FALSE) {
  557. // $entity = entity_load('TripalEntity', array($id), array(), $reset);
  558. $entity = tripal_load_entity('TripalEntity', array($id), $reset);
  559. return reset($entity);
  560. }
  561. /**
  562. * Imports all of the Tripal API into scope.
  563. *
  564. * Typically this function call is not necessary as all of the API is
  565. * automaticaly included by the tripal module. However this function can
  566. * be useful in the .install files during a site upgrade when the tripal
  567. * module is not enabld.
  568. *
  569. * Example usage:
  570. * @code
  571. * module_load_include('module', 'tripal', 'tripal');
  572. * tripal_import_api();
  573. * @endcode
  574. *
  575. */
  576. function tripal_import_api() {
  577. module_load_include('inc', 'tripal', 'api/tripal.d3js.api');
  578. module_load_include('inc', 'tripal', 'api/tripal.fields.api');
  579. module_load_include('inc', 'tripal', 'api/tripal.importer.api');
  580. module_load_include('inc', 'tripal', 'api/tripal.terms.api');
  581. module_load_include('inc', 'tripal', 'api/tripal.entities.api');
  582. module_load_include('inc', 'tripal', 'api/tripal.files.api');
  583. module_load_include('inc', 'tripal', 'api/tripal.jobs.api');
  584. module_load_include('inc', 'tripal', 'api/tripal.notice.api');
  585. module_load_include('inc', 'tripal', 'api/tripal.variables.api');
  586. module_load_include('inc', 'tripal', 'api/tripal.DEPRECATED.api');
  587. }
  588. /**
  589. * Implements hook_form_alter().
  590. */
  591. function tripal_form_alter(&$form, $form_state, $form_id) {
  592. // If this is the field_ui_field_edit_form (i.e. the form that appears
  593. // when editing a field that is attached to an entity). Then we want
  594. // to add term settings for any field attached to a TripalEntity
  595. // content type.
  596. if ($form_id == 'field_ui_field_edit_form' and $form['#instance']['entity_type'] == 'TripalEntity') {
  597. tripal_field_instance_settings_form_alter($form, $form_state);
  598. }
  599. // Remove fields that have no form. It's just a bit too confusing to have
  600. // widgets appear in the form but without and form elements inside them.
  601. if ($form_id == 'tripal_entity_form') {
  602. $children = element_children($form);
  603. foreach ($children as $child) {
  604. // count the number of form elements
  605. if (array_key_exists('und', $form[$child])) {
  606. $total_widgets = 0;
  607. foreach ($form[$child]['und'] as $delta => $element) {
  608. if (is_numeric($delta)) {
  609. $total_widgets += count(element_children($element));
  610. // Ignore the weight column
  611. if (array_key_exists('_weight', $element)) {
  612. $total_widgets--;
  613. }
  614. // Ignore a hidden value column
  615. if (array_key_exists('value', $element) and $element['value']['#type'] == 'value') {
  616. $total_widgets--;
  617. }
  618. }
  619. }
  620. // If we have no widgets then here's not a form for this field so just
  621. // remove it.
  622. if ($total_widgets == 0) {
  623. //unset($form[$child]);
  624. }
  625. }
  626. }
  627. }
  628. }
  629. function tripal_check_new_fields($bundle_name) {
  630. $bundle = tripal_load_bundle_entity(array('name' => $bundle_name));
  631. $term = tripal_load_term_entity(array('term_id' => $bundle->term_id));
  632. $added = tripal_create_bundle_fields($bundle, $term);
  633. if (count($added) == 0) {
  634. drupal_set_message('No new fields were added');
  635. }
  636. foreach ($added as $field_name) {
  637. drupal_set_message('Added field: ' . $field_name);
  638. }
  639. drupal_goto("admin/structure/bio_data/manage/$bundle_name/fields");
  640. }
  641. /**
  642. * Implements hook_block_info().
  643. */
  644. function tripal_block_info() {
  645. $blocks = array();
  646. $admin_theme = 'seven';
  647. $blocks['notifications_block'] = array(
  648. 'info' => t('Dashboard Notifications'),
  649. 'visibility' => BLOCK_VISIBILITY_LISTED,
  650. 'pages' => 'admin/dashboard',
  651. 'status' => TRUE,
  652. 'theme' => $admin_theme,
  653. 'region' => 'dashboard_main',
  654. 'properties' => array(
  655. 'administrative' => TRUE,
  656. ),
  657. );
  658. $blocks['content_type_barchart'] = array(
  659. 'info' => t('Published Tripal Content'),
  660. 'visibility' => BLOCK_VISIBILITY_LISTED,
  661. 'pages' => 'admin/dashboard',
  662. 'status' => TRUE,
  663. 'theme' => $admin_theme,
  664. 'region' => 'dashboard_main',
  665. 'properties' => array(
  666. 'administrative' => TRUE,
  667. ),
  668. );
  669. $blocks['powered_by_tripal'] = array(
  670. 'info' => t('Powered by Tripal'),
  671. 'cache' => DRUPAL_NO_CACHE,
  672. );
  673. return $blocks;
  674. }
  675. /**
  676. * Implements hook_block_view().
  677. */
  678. function tripal_block_view($delta = ''){
  679. global $base_path;
  680. $block = array();
  681. // The $delta parameter tells us which block is being requested.
  682. switch ($delta) {
  683. case 'powered_by_tripal':
  684. $size = variable_get('powered_by_tripal_size', 'small');
  685. $type = variable_get('powered_by_tripal_type', 'bw');
  686. $image = 'powered_by_tripal_bw_small.png';
  687. if ($size == 'small' and $type == 'col') {
  688. $image = 'powered_by_tripal_small.png';
  689. }
  690. if ($size == 'large' and $type == 'bw') {
  691. $image = 'powered_by_tripal_bw.png';
  692. }
  693. if ($size == 'large' and $type == 'col') {
  694. $image = 'powered_by_tripal.png';
  695. }
  696. $block['title'] = '';
  697. $block['content'] = array(
  698. '#markup' => '<a href="http://tripal.info"><img border="0" src="' . $base_path . drupal_get_path('module', 'tripal') . '/theme/images/' . $image . '"></a>',
  699. );
  700. break;
  701. case 'content_type_barchart':
  702. // The number of content types
  703. $entity_types = db_select('tripal_bundle', 'tb')
  704. ->fields('tb')
  705. ->execute()
  706. ->fetchAll();
  707. $entity_count_listing = array();
  708. // The number of entities per content type.
  709. foreach($entity_types as $entity_types => $entity_type){
  710. $result = db_select('chado_' . $entity_type->name, 'et')
  711. ->fields('et')
  712. ->execute();
  713. $number_of_entities = $result->rowCount();
  714. $entity_count_listing[$entity_types] = array(
  715. 'name' => $entity_type->label,
  716. 'count' => $number_of_entities,
  717. );
  718. }
  719. tripal_add_d3js();
  720. drupal_add_js(drupal_get_path ('module', 'tripal') . '/theme/js/tripal.dashboard.js');
  721. drupal_add_css(drupal_get_path ('module', 'tripal') . '/theme/css/tripal.dashboard.css');
  722. drupal_add_library('system', 'drupal.collapse');
  723. drupal_add_js("var entityCountListing = " . json_encode($entity_count_listing) . ";", array('type' => 'inline'));
  724. $output = "<div id=\"tripal-entity-types\" class=\"tripal-entity-types-pane\">
  725. <p>A list of the published Tripal content types and the number of each.</p>
  726. <div id=\"tripal-entity-type-chart\"></div>
  727. </div>";
  728. $block['title'] = '';
  729. $block['content'] = array(
  730. '#markup' => $output,
  731. );
  732. break;
  733. case 'notifications_block':
  734. // Create your block content here
  735. $block['content'] = '';
  736. // Prepare table header
  737. $header = array(
  738. 'title' => array('data' => t('Title')),
  739. 'details' => array('data' => t('Details')),
  740. 'type' => array('data' => t('Type'), 'field' => 'tan.type'),
  741. 'actions' => array('data' => t('Actions'))
  742. );
  743. $query = db_select('tripal_admin_notfications', 'tan')
  744. ->extend('TableSort');
  745. $results = $query->fields('tan')
  746. ->condition('enabled', 1, '=')
  747. ->orderByHeader($header)
  748. ->execute()->fetchAll();
  749. $rows = array();
  750. foreach($results as $result){
  751. $data['operation'] = ' | ';
  752. $data['operation'] .= l(t('Dismiss Notification'), 'admin/disable/notification/' . $result->note_id);
  753. $actions = unserialize($result->actions);
  754. foreach($actions as $action){
  755. $label = key($actions);
  756. $link = $action;
  757. }
  758. $rows[] = array(
  759. 'Title' => $result->title,
  760. 'Details' => $result->details,
  761. 'Type' => $result->type,
  762. 'Actions' => l(t($label), $link) . $data['operation'],
  763. );
  764. }
  765. if(!empty($rows)) {
  766. //Number of records shown in per page
  767. $per_page = 10;
  768. $current_page = pager_default_initialize(count($rows), $per_page);
  769. $chunks = array_chunk($rows, $per_page, TRUE);
  770. // Output of table with the paging
  771. $table = theme('table',
  772. array(
  773. "header" => $header,
  774. "rows" => $chunks[ $current_page ],
  775. "attributes" => array(),
  776. "sticky" => TRUE,
  777. "caption" => "",
  778. "colgroups" => array(),
  779. "empty" => t("No notifications.")
  780. )
  781. );
  782. $table .= theme('pager', array('quantity', count($rows)));
  783. $fieldset_table = array(
  784. '#title' => t('Notifications'),
  785. '#collapsed' => FALSE,
  786. '#collapsible' => TRUE,
  787. '#attributes' => array('class' => array('collapsible')),
  788. '#children' => $table,
  789. );
  790. //return pager with limited number of records.
  791. $block['content'] = theme('fieldset', array('element' => $fieldset_table));
  792. }
  793. else {
  794. $block['content'] = 'There are no notifications at this time.';
  795. }
  796. $block['title'] = 'Tripal Administrative Notifications';
  797. break;
  798. }
  799. return $block;
  800. }
  801. /**
  802. * Implements hook_block_save().
  803. */
  804. function tripal_block_save($delta = '', $edit = array()) {
  805. switch ($delta) {
  806. case 'powered_by_tripal':
  807. if (!empty($edit['logo_size'])) {
  808. variable_set('powered_by_tripal_size', $edit['logo_size']);
  809. }
  810. if (!empty($edit['logo_type'])) {
  811. variable_set('powered_by_tripal_type', $edit['logo_type']);
  812. }
  813. }
  814. }
  815. /**
  816. * Implements hook_block_configure().
  817. */
  818. function tripal_block_configure ($delta = '') {
  819. $form = array();
  820. switch ($delta) {
  821. case 'powered_by_tripal':
  822. $form['logo_size'] = array(
  823. '#type' => 'radios',
  824. '#title' => t('Logo Size'),
  825. '#default_value' => variable_get('powered_by_tripal_size', 'small'),
  826. '#options' => array(
  827. 'large' => t('Large'),
  828. 'small' => t('Small')
  829. ),
  830. '#description' => t('Select if you would like a small or large "Powered by Tripal" logo.'),
  831. );
  832. $form['logo_type'] = array(
  833. '#type' => 'radios',
  834. '#title' => t('Logo Type'),
  835. '#default_value' => variable_get('powered_by_tripal_type', 'bw'),
  836. '#options' => array(
  837. 'bw' => t('Gray scale'),
  838. 'col' => t('Colored')
  839. ),
  840. '#description' => t('Select if you would like a black and white or colored "Powered by Tripal" logo.'),
  841. );
  842. }
  843. return $form;
  844. }
  845. /**
  846. * Implements hook_cron().
  847. */
  848. function tripal_cron() {
  849. if (variable_get('tripal_admin_notification_creation_during_cron', TRUE)) {
  850. $modules = module_implements('tripal_cron_notification');
  851. foreach ($modules as $module) {
  852. $function = $module . '_tripal_cron_notification';
  853. $function();
  854. }
  855. watchdog('tripal_cron', 'tripal_cron ran');
  856. }
  857. }
  858. /**
  859. * Implements hook_element_info().
  860. *
  861. * Used for creating new form API elements.
  862. */
  863. function tripal_element_info() {
  864. // Element for uploading large files. This form element
  865. // accepts the following keys when using in a form:
  866. // - #title: The title that will appear above the element.
  867. // - #description: The description that will appear below the element.
  868. // - #usage_type: Required. The type of file. This will be stored in
  869. // the 'type' column of the file_usage table.
  870. // - #usage_id: Required. A unique numeric ID representing an entity, node
  871. // or some other record identifier. This can be any identifier that
  872. // makes sense to the module that implements a form that uses this
  873. // element.
  874. $elements['html5_file'] = array(
  875. '#input' => 'TRUE',
  876. '#process' => array('tripal_html5_file_process'),
  877. '#element_validate' => array('tripal_html5_file_validate'),
  878. '#value_callback' => 'tripal_html5_file_value',
  879. );
  880. return $elements;
  881. }
  882. /**
  883. * The process function for the html5_file form element.
  884. */
  885. function tripal_html5_file_process($element, $form_state, $complete_form) {
  886. $module = array_key_exists('#usage_module', $element) ? $element['#usage_module'] : 'tripal';
  887. $type = $element['#usage_id'] . '-' . $element['#usage_type'] . '-' . $module;
  888. $name = $element['#name'];
  889. $name = preg_replace('/[^\w]/', '_', $name);
  890. $headers = array(
  891. array('data' => 'File'),
  892. array('data' => 'Size', 'width' => '10%'),
  893. array('data' => 'Upload Progress', 'width' => '20%'),
  894. array('data' => 'Action', 'width' => '10%')
  895. );
  896. $rows = array();
  897. $table_vars = array(
  898. 'header' => $headers,
  899. 'rows' => $rows,
  900. 'attributes' => array(
  901. 'class' => array('tripal-html5-file-upload-table'),
  902. 'id' => 'tripal-html5-file-upload-table-' . $type
  903. ),
  904. 'sticky' => TRUE,
  905. 'colgroups' => array(),
  906. 'empty' => t('There are currently no files.'),
  907. );
  908. $element['html5_file_table_key'] = array(
  909. '#type' => 'hidden',
  910. '#value' => $type,
  911. '#attributes' => array(
  912. 'class' => array('tripal-html5-file-upload-table-key')
  913. )
  914. );
  915. $element['html5_file_table'] = array(
  916. '#type' => 'item',
  917. '#title' => $element['#title'],
  918. '#description' => $element['#description'],
  919. '#markup' => theme('table', $table_vars)
  920. );
  921. $element[$name] = array(
  922. '#type' => 'hidden',
  923. '#attributes' => array('id' => 'tripal-html5-upload-fid-' . $type),
  924. '#default_value' => $element['#value'],
  925. );
  926. $element['html5_file_submit'] = array(
  927. '#type' => 'submit',
  928. '#value' => 'Upload File',
  929. '#name' => 'tripal_html5_file_upload_submit-' . $type,
  930. // We don't want this button to submit as the file upload
  931. // is handled by the JavaScript code.
  932. '#attributes' => array(
  933. 'id' => 'tripal-html5-file-upload-submit-' . $type,
  934. 'onclick' => 'return (false);'
  935. )
  936. );
  937. drupal_add_js(drupal_get_path ('module', 'tripal') . '/theme/js/TripalUploader.js');
  938. drupal_add_js(drupal_get_path ('module', 'tripal') . '/theme/js/TripalUploadFile.js');
  939. drupal_add_js(drupal_get_path ('module', 'tripal') . '/theme/js/tripal.file.js');
  940. return $element;
  941. }
  942. /**
  943. *
  944. */
  945. function tripal_html5_file_validate($element, &$form_state) {
  946. $is_required = $element['#required'];
  947. $fid = $element['#value'];
  948. if ($is_required and !$fid) {
  949. form_error($element, t('A file must be uploaded.'));
  950. }
  951. }
  952. /**
  953. * Implements hook_handle_uploaded_file().
  954. */
  955. function tripal_handle_uploaded_file($filename, $filepath, $type) {
  956. global $user;
  957. // Split the type into a node ID and form_key
  958. list($id, $form_key) = explode('-', $type);
  959. // See if this file is already managed then add another entry fin the
  960. // usage table.
  961. $fid = db_select('file_managed', 'fm')
  962. ->fields('fm', array('fid'))
  963. ->condition('uri', $filepath)
  964. ->execute()
  965. ->fetchField();
  966. // Create a file object.
  967. if (!$fid) {
  968. $file = new stdClass();
  969. $file->uri = $filepath;
  970. $file->filename = $filename;
  971. $file->filemime = file_get_mimetype($filepath);
  972. $file->uid = $user->uid;
  973. $file->status = FILE_STATUS_PERMANENT;
  974. $file = file_save($file);
  975. $fid = $file->fid;
  976. }
  977. $file = file_load($fid);
  978. file_usage_add($file, 'tripal', $form_key, $id);
  979. return $fid;
  980. }
  981. /**
  982. * Implements hook_value() for the html5_file form element.
  983. */
  984. function tripal_html5_file_value($element, $input = FALSE, &$form_state) {
  985. if ($input) {
  986. if (is_array($input)) {
  987. $name = $element['#name'];
  988. $name = preg_replace('/[^\w]/', '_', $name);
  989. return $input[$name];
  990. }
  991. else {
  992. return $input;
  993. }
  994. }
  995. }
  996. /**
  997. * Implements hook_field_display_alter().
  998. *
  999. * @param $display
  1000. * @param $context
  1001. */
  1002. function tripal_field_display_TripalEntity_alter(&$display, $context){
  1003. $field_name = $context['field']['field_name'];
  1004. $bundle = $context['entity']->bundle;
  1005. $bundle_info = tripal_load_bundle_entity(array('name' => $bundle));
  1006. // Hide fields that are empty, but only if the hide_empty_field variable
  1007. // is set to 'hide' for this bundel.
  1008. $hide_variable = tripal_get_bundle_variable('hide_empty_field', $bundle_info->id, 'hide');
  1009. if($hide_variable == 'hide'){
  1010. $item = field_get_items('TripalEntity', $context['entity'], $field_name);
  1011. if($item) {
  1012. $field = field_info_field($field_name);
  1013. if(tripal_field_is_empty($item[0], $field)) {
  1014. // Stop the right rail element from rendering.
  1015. drupal_add_css('.' . $field_name.' {display: none;}', 'inline');
  1016. }
  1017. }
  1018. }
  1019. }
  1020. /**
  1021. * Implements hook_field_group_table_rows_alter().
  1022. *
  1023. * This hook is used for hiding empty rows in a Field Group Table field group.
  1024. *
  1025. * @param $element
  1026. * The field group object.
  1027. * @param $children
  1028. * An array of field names that are contained in the field group.
  1029. */
  1030. function tripal_field_group_table_rows_alter(&$element, &$children) {
  1031. // Iterate through the children of this table field group.
  1032. foreach ($children as $index => $child) {
  1033. if (is_array($element[$child])) {
  1034. $bundle = $element[$child]['#bundle'];
  1035. $bundle_info = tripal_load_bundle_entity(array('name' => $bundle));
  1036. // If the hide empty variable is turned on then remove fields from
  1037. // the field group.
  1038. $hide_variable = tripal_get_bundle_variable('hide_empty_field', $bundle_info->id, 'hide');
  1039. if($hide_variable == 'hide'){
  1040. $items = $element[$child]['#items'];
  1041. // Case #1: there are no items.
  1042. if (count($items) == 0) {
  1043. unset($children[$index]);
  1044. unset($element[$child]);
  1045. }
  1046. // Case #2: there is one item but the value is empty.
  1047. if (count($items) == 1 and array_key_exists('value', $items[0]) and empty($items[0]['value'])) {
  1048. unset($children[$index]);
  1049. unset($element[$child]);
  1050. }
  1051. }
  1052. }
  1053. }
  1054. }