tripal.module 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  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/dashboard'] = array(
  100. 'title' => 'Dashboard',
  101. 'description' => t("A dashboard view of Tripal including new fields for entities."),
  102. 'weight' => 0,
  103. 'page callback' => 'tripal_admin_usage_page',
  104. 'access arguments' => array('administer tripal'),
  105. 'type' => MENU_NORMAL_ITEM,
  106. 'file' => 'includes/tripal_admin_usage_page.inc',
  107. 'file path' => drupal_get_path('module', 'tripal'),
  108. );
  109. // $items['admin/tripal/loaders/obo_loader'] = array(
  110. // 'title' => 'OBO Controlled Vocabulary Loader',
  111. // 'description' => t("Import vocabularies and terms in OBO format."),
  112. // 'access arguments' => array('administer tripal'),
  113. // 'page callback' => 'drupal_get_form',
  114. // 'page arguments' => array('tripal_vocabulary_import_form'),
  115. // 'file' => 'includes/tripal.admin.inc',
  116. // 'file path' => drupal_get_path('module', 'tripal'),
  117. // 'type' => MENU_NORMAL_ITEM,
  118. // );
  119. $items['admin/tripal/extension'] = array(
  120. 'title' => 'Extensions',
  121. 'description' => t("Configuration and management pages for Tripal extension modules."),
  122. 'weight' => 8,
  123. 'access arguments' => array('administer tripal'),
  124. );
  125. // Menu items for facilitating import of extension modules.
  126. $items['admin/tripal/extension/available'] = array(
  127. 'title' => 'Available Extensions',
  128. 'description' => t('Look for extensions to add new functionality to this
  129. site. Tripal can be extended with new functionality developed
  130. by other Tripal site developers. These include modules with new or
  131. different functionality, bulk loading templates, or materialized
  132. views. Anyone can create new extensions and share those for
  133. others to use. Once shared they will appear in this list.'),
  134. 'access arguments' => array('administer tripal'),
  135. 'page callback' => 'drupal_get_form',
  136. 'page arguments' => array('tripal_extensions_form'),
  137. 'type' => MENU_NORMAL_ITEM,
  138. 'file' => 'includes/tripal.extensions.inc',
  139. 'file path' => drupal_get_path('module', 'tripal'),
  140. 'weight' => -100
  141. );
  142. // $items['admin/tripal/extension/import'] = array(
  143. // 'title' => 'Import Extensions',
  144. // '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.',
  145. // 'page callback' => 'drupal_get_form',
  146. // 'page arguments' => array('tripal_extensions_form'),
  147. // 'access arguments' => array('administer tripal'),
  148. // 'type' => MENU_NORMAL_ITEM,
  149. // 'file' => 'includes/tripal.extensions.inc',
  150. // 'file path' => drupal_get_path('module', 'tripal'),
  151. // 'weight' => -100,
  152. // );
  153. // Jobs Management
  154. $items['admin/tripal/tripal_jobs'] = array(
  155. 'title' => 'Jobs',
  156. 'description' => t('Provides tools for managing jobs submitted to Tripal. In some
  157. cases, long-running tasks are too slow to complete within a single
  158. browser session. The Tripal jobs system allows long-running tasks
  159. to be submitted to a queue that can be executed manually by the
  160. site admin or automatically using a module such as the ') .
  161. l('Tripal Daemon', 'https://www.drupal.org/project/tripal_daemon', array('attributes' => array('target' => '_blank'))) .
  162. ' extension module.',
  163. 'page callback' => 'tripal_jobs_admin_view',
  164. 'access arguments' => array('administer tripal'),
  165. 'type' => MENU_NORMAL_ITEM,
  166. 'weight' => 0,
  167. 'file' => 'includes/tripal.jobs.inc',
  168. );
  169. $items['admin/tripal/tripal_jobs/help'] = array(
  170. 'title' => 'Help',
  171. 'description' => t('Help for the tripal job management system'),
  172. 'page callback' => 'theme',
  173. 'page arguments' => array('tripal_job_help'),
  174. 'access arguments' => array('administer tripal'),
  175. 'type' => MENU_LOCAL_TASK,
  176. 'weight' => 10
  177. );
  178. $items['admin/tripal/tripal_jobs/cancel/%'] = array(
  179. 'title' => 'Jobs',
  180. 'description' => t('Cancel a pending job'),
  181. 'page callback' => 'tripal_cancel_job',
  182. 'page arguments' => array(4),
  183. 'access arguments' => array('administer tripal'),
  184. 'type' => MENU_CALLBACK,
  185. 'file' => 'api/tripal.jobs.api.inc',
  186. );
  187. $items['admin/tripal/tripal_jobs/status/%'] = array(
  188. 'page callback' => 'tripal_jobs_status_view',
  189. 'page arguments' => array(4),
  190. 'access arguments' => array('administer tripal'),
  191. 'type' => MENU_CALLBACK,
  192. 'file' => 'includes/tripal.jobs.inc',
  193. );
  194. $items['admin/tripal/tripal_jobs/rerun/%'] = array(
  195. 'title' => 'Jobs',
  196. 'description' => t('Re-run an existing job.'),
  197. 'page callback' => 'tripal_rerun_job',
  198. 'page arguments' => array(4),
  199. 'access arguments' => array('administer tripal'),
  200. 'type' => MENU_CALLBACK,
  201. 'file' => 'includes/tripal.jobs.inc',
  202. );
  203. $items['admin/tripal/tripal_jobs/view/%'] = array(
  204. 'title' => 'Jobs Details',
  205. 'description' => t('View job details.'),
  206. 'page callback' => 'tripal_jobs_view',
  207. 'page arguments' => array(4),
  208. 'access arguments' => array('administer tripal'),
  209. 'type' => MENU_CALLBACK,
  210. 'file' => 'includes/tripal.jobs.inc',
  211. );
  212. $items['admin/tripal/tripal_jobs/views/jobs/enable'] = array(
  213. 'title' => 'Enable Jobs Administrative View',
  214. 'page callback' => 'tripal_enable_view',
  215. 'page arguments' => array('tripal_admin_jobs', 'admin/tripal/tripal_jobs'),
  216. 'access arguments' => array('administer tripal'),
  217. 'type' => MENU_CALLBACK,
  218. 'file' => 'includes/tripal.jobs.inc',
  219. );
  220. /*
  221. * AJAX Callbacks.
  222. */
  223. $items['bio_data/ajax/field_attach/%'] = array(
  224. 'page callback' => 'tripal_ajax_attach_field',
  225. 'page arguments' => array(3),
  226. 'access arguments' => array('access content'),
  227. 'type' => MENU_CALLBACK,
  228. 'file' => 'includes/tripal.entity.inc',
  229. 'file path' => drupal_get_path('module', 'tripal'),
  230. );
  231. /*
  232. * Dashboard Action Item callbacks.
  233. */
  234. $items['admin/disable/notification/%'] = array(
  235. 'page callback' => 'tripal_disable_admin_notification',
  236. 'page arguments' => array(3),
  237. 'access arguments' => array('access content'),
  238. 'type' => MENU_CALLBACK,
  239. 'file' => 'includes/tripal_admin_usage_page.inc',
  240. 'file path' => drupal_get_path('module', 'tripal'),
  241. );
  242. $items['admin/import/field/%/%/%/%'] = array(
  243. 'page callback' => 'tripal_admin_notification_import_field',
  244. 'page arguments' => array(3, 4, 5, 6),
  245. 'access arguments' => array('access content'),
  246. 'type' => MENU_CALLBACK,
  247. 'file' => 'includes/tripal_admin_usage_page.inc',
  248. 'file path' => drupal_get_path('module', 'tripal'),
  249. );
  250. /*
  251. * Term Lookup
  252. */
  253. // TODO: finish this menu callback.
  254. // $items['cv/lookup'] = array(
  255. // 'title' => 'Vocabulary Lookup',
  256. // 'description' => t("Provides a tool to discover controlled vocabularies and their terms used by this site."),
  257. // 'access arguments' => array('access content'),
  258. // 'page callback' => 'drupal_get_form',
  259. // 'page arguments' => array('tripal_vocabulary_lookup_form'),
  260. // 'file' => 'includes/tripal.term_lookup.inc',
  261. // 'file path' => drupal_get_path('module', 'tripal'),
  262. // 'type' => MENU_NORMAL_ITEM,
  263. // );
  264. $items['cv/lookup/%/%'] = array(
  265. 'title' => 'Vocabulary Lookup',
  266. 'description' => t("Provides a tool to discover controlled vocabularies and their terms used by this site."),
  267. 'access arguments' => array('access content'),
  268. 'page callback' => 'tripal_vocabulary_lookup_term_page',
  269. 'page arguments' => array(2, 3),
  270. 'file' => 'includes/tripal.term_lookup.inc',
  271. 'file path' => drupal_get_path('module', 'tripal'),
  272. 'type' => MENU_CALLBACK,
  273. );
  274. // Adds a +Check for new fields link on the 'Tripal Content Types' page.
  275. $items['admin/structure/bio_data/manage/%/fields/check'] = array(
  276. 'title' => 'Check for new fields',
  277. 'description' => t('Check if new fields should be added to this content type.'),
  278. 'page callback' => 'tripal_check_new_fields',
  279. 'page arguments' => array(4),
  280. 'access arguments' => array('administer tripal'),
  281. 'file' => 'api/tripal.entities.api.inc',
  282. 'file path' => drupal_get_path('module', 'tripal'),
  283. 'type' => MENU_LOCAL_ACTION,
  284. );
  285. $items['tripal/upload'] = array(
  286. 'page callback' => 'tripal_file_upload',
  287. 'access arguments' => array('upload files'),
  288. 'file' => '/includes/tripal.upload.inc',
  289. 'type' => MENU_CALLBACK,
  290. );
  291. // Add in the loaders
  292. $importers = tripal_get_importers();
  293. foreach ($importers as $class_name) {
  294. tripal_load_include_importer_class($class_name);
  295. if (class_exists($class_name)) {
  296. $items['admin/tripal/loaders/' . $class_name::$machine_name] = array(
  297. 'title' => $class_name::$name,
  298. 'description' => $class_name::$description,
  299. 'page callback' => 'drupal_get_form',
  300. 'page arguments' => array('tripal_get_importer_form', $class_name),
  301. 'access arguments' => array('administer tripal'),
  302. 'type' => MENU_NORMAL_ITEM,
  303. 'file' => 'includes/tripal.importer.inc',
  304. 'file path' => drupal_get_path('module', 'tripal'),
  305. );
  306. }
  307. }
  308. return $items;
  309. }
  310. /**
  311. * Implements hook_permission().
  312. */
  313. function tripal_permission() {
  314. $permissions = array(
  315. 'administer tripal' => array(
  316. 'title' => t('Administer Tripal'),
  317. 'description' => t('Allow the user to access administrative pages of Tripal. This includes management of jobs, the storage systems, extensions and the controlled vocabularies.'),
  318. 'restrict access' => TRUE,
  319. ),
  320. 'access tripal content overview' => array(
  321. 'title' => t('Access the Tripal content overview page'),
  322. 'description' => t('Get an overview of all Tripal content'),
  323. 'restrict access' => TRUE,
  324. ),
  325. 'manage tripal content types' => array(
  326. 'title' => t('Manage Tripal content types'),
  327. 'description' => t('Allows the user to create, update and delete Tripal content types.'),
  328. 'restrict access' => TRUE,
  329. ),
  330. 'view dev helps' => array(
  331. 'title' => t('View Developer Hints'),
  332. 'description' => t('Tripal will provide blue shaded boxes that provide
  333. instructions for how to customize or setup specific pages on a
  334. site. This permission should be enabled for developers. But can
  335. be disabled once developers are accustomed to these hints.'),
  336. 'restrict access' => TRUE,
  337. ),
  338. );
  339. // Add permissions for each content type.
  340. $bundles = tripal_get_content_types();
  341. foreach ($bundles as $bundle) {
  342. $permissions['view ' . $bundle->name] = array(
  343. 'title' => t('%label: View Content', array('%label' => $bundle->label)),
  344. 'description' => t('Allow the user to view %label content', array('%label' => $bundle->label)),
  345. );
  346. $permissions['create ' . $bundle->name] = array(
  347. 'title' => t('%label: Create Content', array('%label' => $bundle->label)),
  348. 'description' => t('Allow the user to create %label content', array('%label' => $bundle->label)),
  349. 'restrict access' => TRUE,
  350. );
  351. $permissions['edit ' . $bundle->name] = array(
  352. 'title' => t('%label: Edit Content', array('%label' => $bundle->label)),
  353. 'description' => t('Allow the user to edit %label content', array('%label' => $bundle->label)),
  354. 'restrict access' => TRUE,
  355. );
  356. $permissions['delete ' . $bundle->name] = array(
  357. 'title' => t('%label: Delete Content', array('%label' => $bundle->label)),
  358. 'description' => t('Allow the user to delete %label content', array('%label' => $bundle->label)),
  359. 'restrict access' => TRUE,
  360. );
  361. }
  362. return $permissions;
  363. }
  364. /**
  365. * Implements hook_theme().
  366. * Registers template files/functions used by this module.
  367. *
  368. * @ingroup tripal
  369. */
  370. function tripal_theme($existing, $type, $theme, $path) {
  371. $themes = array(
  372. // Admin messages theme
  373. 'tripal_admin_message' => array(
  374. 'function' => 'theme_tripal_admin_message',
  375. 'variables' => array('message' => NULL),
  376. ),
  377. 'tripal_entity' => array(
  378. 'render element' => 'elements',
  379. 'template' => 'tripal_entity',
  380. 'path' => "$path/theme/templates"
  381. ),
  382. 'tripal_add_list' => array(
  383. 'variables' => array('content' => NULL),
  384. ),
  385. // Themeing for all fields.
  386. 'tripal_field_default' => array(
  387. 'render element' => 'element',
  388. 'file' => 'includes/tripal.fields.inc',
  389. ),
  390. );
  391. return $themes;
  392. }
  393. /**
  394. * Implements hook_coder_ignore().
  395. * Defines the path to the file (tripal.coder_ignores.txt) where ignore rules for coder are stored
  396. */
  397. function tripal_coder_ignore() {
  398. return array(
  399. 'path' => drupal_get_path('module', 'tripal'),
  400. 'line prefix' => drupal_get_path('module', 'tripal'),
  401. );
  402. }
  403. /**
  404. * Implements hook_libraries_info().
  405. */
  406. function tripal_libraries_info() {
  407. $libraries = array();
  408. $libraries['d3'] = array(
  409. 'name' => 'D3.js',
  410. 'vendor url' => 'http://d3js.org/',
  411. 'download url' => 'https://github.com/mbostock/d3',
  412. 'version arguments' => array(
  413. 'file' => 'd3.js',
  414. 'pattern' => '/\s*version: "(\d+\.\d+\.\d+)"/',
  415. ),
  416. 'files' => array(
  417. 'js' => array(
  418. 'd3.min.js',
  419. ),
  420. ),
  421. );
  422. return $libraries;
  423. }
  424. /**
  425. * Implements hook_admin_paths().
  426. * Define administrative paths.
  427. */
  428. function tripal_admin_paths() {
  429. if (variable_get('node_admin_theme')) {
  430. $paths = array(
  431. 'bio_data/*/edit' => TRUE,
  432. 'bio_data/*/delete' => TRUE,
  433. 'bio_data/add' => TRUE,
  434. 'bio_data/add/*' => TRUE,
  435. );
  436. return $paths;
  437. }
  438. }
  439. /**
  440. * Implements hook_menu_local_tasks_alter().
  441. *
  442. * Used to add action links to pages.
  443. */
  444. function tripal_menu_local_tasks_alter(&$data, $router_item, $root_path) {
  445. // Add an "Add Tripal Content" action link to the Admin >> Content >>
  446. // Biological Content page.
  447. if ($root_path == 'admin/content/bio_data') {
  448. $item = menu_get_item('bio_data/add');
  449. if ($item['access']) {
  450. $data['actions']['output'][] = array(
  451. '#theme' => 'menu_local_action',
  452. '#link' => $item,
  453. );
  454. }
  455. }
  456. }
  457. /**
  458. * Implements hook_shortcut_default_set().
  459. * Modify the shortcut menu to include Biological content links.
  460. *
  461. * @param object $account
  462. * The user account whose default shortcut set will be returned. If not provided, the
  463. * function will return the currently logged-in user's default shortcut set.
  464. *
  465. * @return
  466. * An object representing the default shortcut set.
  467. */
  468. function tripal_shortcut_default_set($account) {
  469. $sets = shortcut_sets();
  470. $found = FALSE;
  471. foreach ($sets as $set) {
  472. if ($set->title == 'TripalDefault') {
  473. $found = TRUE;
  474. }
  475. }
  476. if (!$found) {
  477. $t = get_t();
  478. // Create an initial default shortcut set.
  479. $shortcut_set = new stdClass();
  480. $shortcut_set->title = $t('TripalDefault');
  481. $shortcut_set->links = array(
  482. array(
  483. 'link_path' => 'node/add',
  484. 'link_title' => $t('Add content'),
  485. 'weight' => -35,
  486. ),
  487. array(
  488. 'link_path' => 'bio_data/add',
  489. 'link_title' => 'Add Tripal Content',
  490. 'weight' => -30,
  491. ),
  492. array(
  493. 'link_path' => 'admin/content',
  494. 'link_title' => $t('Find content'),
  495. 'weight' => -25,
  496. ),
  497. array(
  498. 'link_path' => 'admin/content/bio_data',
  499. 'link_title' => 'Find Tripal Content',
  500. 'weight' => -20,
  501. ),
  502. );
  503. shortcut_set_save($shortcut_set);
  504. }
  505. $sets = shortcut_sets();
  506. foreach ($sets as $set) {
  507. if ($set->title == 'TripalDefault') {
  508. return $set->set_name;
  509. }
  510. }
  511. }
  512. /**
  513. * Menu argument loader; Load a tripal data type by string.
  514. *
  515. * This function is not meant to be used as an API function. It is only meant
  516. * for use in the menu to resolve the %tripal_bundle wildcard.
  517. *
  518. * @param $type
  519. * The machine-readable name of a tripal data type to load.
  520. * @return
  521. * A tripal data type array or FALSE if $type does not exist.
  522. */
  523. function TripalBundle_load($bundle_type, $reset = FALSE) {
  524. // Get the type of entity by the ID.
  525. $bundle = db_select('tripal_bundle', 'tdt')
  526. ->fields('tdt')
  527. ->condition('name', $bundle_type)
  528. ->execute()
  529. ->fetchObject();
  530. if ($bundle) {
  531. $entity = entity_load('TripalBundle', array($bundle->id), array(), $reset);
  532. return reset($entity);
  533. }
  534. return FALSE;
  535. }
  536. /**
  537. * Allows the menu system to use a wildcard to fetch the entity.
  538. *
  539. * Make sure that the wildcard you choose in the tripal_entity entity
  540. * definition fits the function name here.
  541. *
  542. * This function is not meant to be used as an API function. It is only meant
  543. * for use in the menu to resolve the %tripal_entity wildcard.
  544. *
  545. * @param $id
  546. * Integer specifying the tripal_entity id.
  547. * @param $reset
  548. * A boolean indicating that the internal cache should be reset.
  549. * @return
  550. * A fully-loaded $tripal_entity object or FALSE if it cannot be loaded.
  551. *
  552. * @see tripal_entity_load_multiple()
  553. */
  554. function TripalEntity_load($id, $reset = FALSE) {
  555. // $entity = entity_load('TripalEntity', array($id), array(), $reset);
  556. $entity = tripal_load_entity('TripalEntity', array($id), $reset);
  557. return reset($entity);
  558. }
  559. /**
  560. * Imports all of the Tripal API into scope.
  561. *
  562. * Typically this function call is not necessary as all of the API is
  563. * automaticaly included by the tripal module. However this function can
  564. * be useful in the .install files during a site upgrade when the tripal
  565. * module is not enabld.
  566. *
  567. * Example usage:
  568. * @code
  569. * module_load_include('module', 'tripal', 'tripal');
  570. * tripal_import_api();
  571. * @endcode
  572. *
  573. */
  574. function tripal_import_api() {
  575. module_load_include('inc', 'tripal', 'api/tripal.d3js.api');
  576. module_load_include('inc', 'tripal', 'api/tripal.fields.api');
  577. module_load_include('inc', 'tripal', 'api/tripal.importer.api');
  578. module_load_include('inc', 'tripal', 'api/tripal.terms.api');
  579. module_load_include('inc', 'tripal', 'api/tripal.entities.api');
  580. module_load_include('inc', 'tripal', 'api/tripal.files.api');
  581. module_load_include('inc', 'tripal', 'api/tripal.jobs.api');
  582. module_load_include('inc', 'tripal', 'api/tripal.notice.api');
  583. module_load_include('inc', 'tripal', 'api/tripal.variables.api');
  584. module_load_include('inc', 'tripal', 'api/tripal.DEPRECATED.api');
  585. }
  586. /**
  587. * Implements hook_form_alter().
  588. */
  589. function tripal_form_alter(&$form, $form_state, $form_id) {
  590. // If this is the field_ui_field_edit_form (i.e. the form that appears
  591. // when editing a field that is attached to an entity). Then we want
  592. // to add term settings for any field attached to a TripalEntity
  593. // content type.
  594. if ($form_id == 'field_ui_field_edit_form' and $form['#instance']['entity_type'] == 'TripalEntity') {
  595. tripal_field_instance_settings_form_alter($form, $form_state);
  596. }
  597. }
  598. function tripal_check_new_fields($bundle_name) {
  599. tripal_refresh_bundle_fields($bundle_name);
  600. drupal_goto("admin/structure/bio_data/manage/$bundle_name/fields");
  601. }
  602. /**
  603. * Implements hook_block_info().
  604. */
  605. function tripal_block_info() {
  606. $blocks = array();
  607. $blocks['notifications_block'] = array(
  608. 'info' => t('Dashboard Notifications'),
  609. 'visibility' => BLOCK_VISIBILITY_LISTED,
  610. 'pages' => 'admin/tripal/dashboard',
  611. 'status' => TRUE,
  612. 'region' => 'content',
  613. 'properties' => array(
  614. 'administrative' => TRUE,
  615. ),
  616. );
  617. $blocks['powered_by_tripal'] = array(
  618. 'info' => t('Powered by Tripal'),
  619. 'cache' => DRUPAL_NO_CACHE,
  620. );
  621. return $blocks;
  622. }
  623. /**
  624. * Implements hook_block_view().
  625. */
  626. function tripal_block_view($delta = ''){
  627. // The $delta parameter tells us which block is being requested.
  628. switch ($delta) {
  629. case 'powered_by_tripal':
  630. $size = variable_get('powered_by_tripal_size', 'small');
  631. $type = variable_get('powered_by_tripal_type', 'bw');
  632. $image = 'powered_by_tripal_bw_small.png';
  633. if ($size == 'small' and $type == 'col') {
  634. $image = 'powered_by_tripal_small.png';
  635. }
  636. if ($size == 'large' and $type == 'bw') {
  637. $image = 'powered_by_tripal_bw.png';
  638. }
  639. if ($size == 'large' and $type == 'col') {
  640. $image = 'powered_by_tripal.png';
  641. }
  642. $block['title'] = '';
  643. $block['content'] = array(
  644. '#markup' => '<a href="http://tripal.info"><img border="0" src="' . drupal_get_path('module', 'tripal') . '/theme/images/' . $image . '"></a>',
  645. );
  646. break;
  647. case 'notifications_block':
  648. // Create your block content here
  649. $block['content'] = '';
  650. // Prepare table header
  651. $header = array(
  652. 'title' => array('data' => t('Title')),
  653. 'details' => array('data' => t('Details')),
  654. 'type' => array('data' => t('Type'), 'field' => 'tan.type'),
  655. 'actions' => array('data' => t('Actions'))
  656. );
  657. $query = db_select('tripal_admin_notfications', 'tan')
  658. ->extend('TableSort');
  659. $results = $query->fields('tan')
  660. ->condition('enabled', 1, '=')
  661. ->orderByHeader($header)
  662. ->execute()->fetchAll();
  663. $rows = array();
  664. foreach($results as $result){
  665. $data['operation'] = ' | ';
  666. $data['operation'] .= l(t('Dismiss Notification'), 'admin/disable/notification/' . $result->note_id);
  667. $actions = unserialize($result->actions);
  668. foreach($actions as $action){
  669. $label = key($actions);
  670. $link = $action;
  671. }
  672. $rows[] = array(
  673. 'Title' => $result->title,
  674. 'Details' => $result->details,
  675. 'Type' => $result->type,
  676. 'Actions' => l(t($label), $link) . $data['operation'],
  677. );
  678. }
  679. if(!empty($rows)) {
  680. //Number of records shown in per page
  681. $per_page = 10;
  682. $current_page = pager_default_initialize(count($rows), $per_page);
  683. $chunks = array_chunk($rows, $per_page, TRUE);
  684. // Output of table with the paging
  685. $table = theme('table',
  686. array(
  687. "header" => $header,
  688. "rows" => $chunks[ $current_page ],
  689. "attributes" => array(),
  690. "sticky" => TRUE,
  691. "caption" => "",
  692. "colgroups" => array(),
  693. "empty" => t("No notifications.")
  694. )
  695. );
  696. $table .= theme('pager', array('quantity', count($rows)));
  697. $fieldset_table = array(
  698. '#title' => t('Notifications'),
  699. '#collapsed' => FALSE,
  700. '#collapsible' => TRUE,
  701. '#attributes' => array('class' => array('collapsible')),
  702. '#children' => $table,
  703. );
  704. //return pager with limited number of records.
  705. $block['content'] = theme('fieldset', array('element' => $fieldset_table));
  706. }
  707. else {
  708. $block['content'] = 'There are no notifications at this time.';
  709. }
  710. break;
  711. }
  712. return $block;
  713. }
  714. /**
  715. * Implements hook_block_save().
  716. */
  717. function tripal_block_save($delta = '', $edit = array()) {
  718. switch ($delta) {
  719. case 'powered_by_tripal':
  720. if (!empty($edit['logo_size'])) {
  721. variable_set('powered_by_tripal_size', $edit['logo_size']);
  722. }
  723. if (!empty($edit['logo_type'])) {
  724. variable_set('powered_by_tripal_type', $edit['logo_type']);
  725. }
  726. }
  727. }
  728. /**
  729. * Implements hook_block_configure().
  730. */
  731. function tripal_block_configure ($delta = '') {
  732. $form = array();
  733. switch ($delta) {
  734. case 'powered_by_tripal':
  735. $form['logo_size'] = array(
  736. '#type' => 'radios',
  737. '#title' => t('Logo Size'),
  738. '#default_value' => variable_get('powered_by_tripal_size', 'small'),
  739. '#options' => array(
  740. 'large' => t('Large'),
  741. 'small' => t('Small')
  742. ),
  743. '#description' => t('Select if you would like a small or large "Powered by Tripal" logo.'),
  744. );
  745. $form['logo_type'] = array(
  746. '#type' => 'radios',
  747. '#title' => t('Logo Type'),
  748. '#default_value' => variable_get('powered_by_tripal_type', 'bw'),
  749. '#options' => array(
  750. 'bw' => t('Gray scale'),
  751. 'col' => t('Colored')
  752. ),
  753. '#description' => t('Select if you would like a black and white or colored "Powered by Tripal" logo.'),
  754. );
  755. }
  756. return $form;
  757. }
  758. /**
  759. * Implements hook_cron().
  760. */
  761. function tripal_cron() {
  762. if (variable_get('tripal_admin_notification_creation_during_cron', TRUE)) {
  763. $modules = module_implements('tripal_cron_notification');
  764. foreach ($modules as $module) {
  765. $function = $module . '_tripal_cron_notification';
  766. $function();
  767. }
  768. watchdog('tripal_cron', 'tripal_cron ran');
  769. }
  770. }
  771. /**
  772. * Implements hook_element_info().
  773. *
  774. * Used for creating new form API elements.
  775. */
  776. function tripal_element_info() {
  777. // Element for uploading large files. This form element
  778. // accepts the following keys when using in a form:
  779. // - #title: The title that will appear above the element.
  780. // - #description: The description that will appear below the element.
  781. // - #usage_type: Required. The type of file. Thie will be stored in
  782. // the 'type' column of the file_usage table.
  783. // - #usage_id: Required. A unique numeric ID representing an entity, node
  784. // or some other record identifier. This can be any identifier that
  785. // makes sense to the module that implements a form that uses this
  786. // element.
  787. $elements['html5_file'] = array(
  788. '#input' => 'TRUE',
  789. '#process' => array('tripal_html5_file_process'),
  790. '#element_validate' => array('tripal_html5_file_validate'),
  791. '#value_callback' => 'tripal_html5_file_value',
  792. );
  793. return $elements;
  794. }
  795. /**
  796. * The process function for the html5_file form element.
  797. */
  798. function tripal_html5_file_process($element, $form_state, $complete_form) {
  799. // $nid = $element['#webform_component']['nid'];
  800. // $form_key = $element['#webform_component']['form_key'];
  801. $type = $element['#usage_id'] . '-' . $element['#usage_type'];
  802. $name = $element['#name'];
  803. $headers = array(
  804. array('data' => 'File'),
  805. array('data' => 'Size', 'width' => '10%'),
  806. array('data' => 'Upload Progress', 'width' => '20%'),
  807. array('data' => 'Action', 'width' => '10%')
  808. );
  809. $rows = array();
  810. $table_vars = array(
  811. 'header' => $headers,
  812. 'rows' => $rows,
  813. 'attributes' => array(
  814. 'class' => array('tripal-html5-file-upload-table'),
  815. 'id' => 'tripal-html5-file-upload-table-' . $type
  816. ),
  817. 'sticky' => TRUE,
  818. 'colgroups' => array(),
  819. 'empty' => t('There are currently no files.'),
  820. );
  821. $element['value'] = array(
  822. '#type' => 'value',
  823. '#value' => '',
  824. );
  825. $element['html5_file_table_key'] = array(
  826. '#type' => 'hidden',
  827. '#value' => $type,
  828. '#attributes' => array(
  829. 'class' => array('tripal-html5-file-upload-table-key')
  830. )
  831. );
  832. $element['html5_file_table'] = array(
  833. '#type' => 'item',
  834. '#title' => $element['#title'],
  835. '#description' => $element['#description'],
  836. '#markup' => theme('table', $table_vars)
  837. );
  838. $element[$name] = array(
  839. '#type' => 'hidden',
  840. '#attributes' => array('id' => 'tripal-html5-upload-fid-' . $type),
  841. '#default_value' => $element['#value'],
  842. );
  843. $element['html5_file_submit'] = array(
  844. '#type' => 'submit',
  845. '#value' => 'Upload File',
  846. '#name' => 'tripal_html5_file_upload_submit-' . $type,
  847. // We don't want this button to submit as the file upload
  848. // is handled by the JavaScript code.
  849. '#attributes' => array(
  850. 'id' => 'tripal-html5-file-upload-submit-' . $type,
  851. 'onclick' => 'return (false);'
  852. )
  853. );
  854. drupal_add_js(drupal_get_path ('module', 'tripal') . '/theme/js/TripalUploader.js');
  855. drupal_add_js(drupal_get_path ('module', 'tripal') . '/theme/js/TripalUploadFile.js');
  856. drupal_add_js(drupal_get_path ('module', 'tripal') . '/theme/js/tripal.file.js');
  857. return $element;
  858. }
  859. /**
  860. *
  861. */
  862. function tripal_html5_file_validate($element, &$form_state) {
  863. $is_required = $element['#required'];
  864. $fid = $element['#value'];
  865. if ($is_required and !$fid) {
  866. form_error($element, t('A file must be uploaded.'));
  867. }
  868. }
  869. /**
  870. * Implements hook_handle_uplaoded_file().
  871. */
  872. function tripal_handle_uploaded_file($filename, $filepath, $type) {
  873. global $user;
  874. // Split the type into a node ID and form_key
  875. list($nid, $form_key) = explode('-', $type);
  876. // See if this file is already managed then add another entry fin the
  877. // usage table.
  878. $fid = db_select('file_managed', 'fm')
  879. ->fields('fm', array('fid'))
  880. ->condition('uri', $filepath)
  881. ->execute()
  882. ->fetchField();
  883. if ($fid) {
  884. $file = file_load($fid);
  885. file_usage_add($file, 'tripal', $form_key, $nid);
  886. return $fid;
  887. }
  888. // Create a file object.
  889. $file = new stdClass();
  890. $file->uri = $filepath;
  891. $file->filename = $filename;
  892. $file->filemime = file_get_mimetype($filepath);
  893. $file->uid = $user->uid;
  894. $file->status = FILE_STATUS_PERMANENT;
  895. $file = file_save($file);
  896. return $file->fid;
  897. }
  898. /**
  899. *
  900. */
  901. function tripal_html5_file_value($element, $input = FALSE, &$form_state) {
  902. if ($input) {
  903. return $input;
  904. }
  905. }