tripal.module 45 KB

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