tripal_project.module 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. <?php
  2. require('includes/tripal_project.admin.inc');
  3. require('api/tripal_project.api.inc');
  4. /**
  5. * @file
  6. * This file contains the basic functions needed for this drupal module.
  7. * The drupal tripal_project module maps directly to the chado general module.
  8. *
  9. * For documentation regarding the Chado General module:
  10. * @see http://gmod.org/wiki/Chado_General_Module
  11. *
  12. * @defgroup tripal_project Project Module
  13. * @ingroup tripal_modules
  14. */
  15. /**
  16. * Implementation of hook_node_info().
  17. *
  18. * This node_info, is a simple node that describes the functionallity of the module. It specifies
  19. * that the title(Project Name) and body(Description) set to true so that they information can be
  20. * entered
  21. *
  22. *
  23. * @ingroup tripal_project
  24. */
  25. function tripal_project_node_info() {
  26. return array(
  27. 'chado_project' => array(
  28. 'name' => t('Project'),
  29. 'base' => 'chado_project',
  30. 'description' => t('A project from the Chado database'),
  31. 'has_title' => TRUE,
  32. 'title_label' => t('Project Name'),
  33. 'had_body' => TRUE,
  34. 'body_label' => t('Full Description'),
  35. 'chado_node_api' => array(
  36. 'base_table' => 'project',
  37. 'hook_prefix' => 'chado_project',
  38. 'record_type_title' => array(
  39. 'singular' => t('Project'),
  40. 'plural' => t('Projects')
  41. ),
  42. 'sync_filters' => array(
  43. 'type_id' => FALSE,
  44. 'organism_id' => FALSE
  45. ),
  46. ),
  47. ),
  48. );
  49. }
  50. /**
  51. * Implements hook_views_api()
  52. *
  53. * Purpose: Essentially this hook tells drupal that there is views support for
  54. * for this module which then includes tripal_project.views.inc where all the
  55. * views integration code is
  56. *
  57. * @ingroup tripal_project
  58. *
  59. */
  60. function tripal_project_views_api() {
  61. return array(
  62. 'api' => 2.0,
  63. );
  64. }
  65. /**
  66. * Implements hook_menu
  67. *
  68. * @ingroup tripal_project
  69. */
  70. function tripal_project_menu() {
  71. $items[ 'admin/tripal/chado/tripal_project' ]= array(
  72. 'title' => 'Projects',
  73. 'description' => ('A project. Can be used for grouping data such as with the natural diversity module data.'),
  74. 'page callback' => 'tripal_project_admin_project_view',
  75. 'access arguments' => array('adminster tripal projects'),
  76. 'type' => MENU_NORMAL_ITEM
  77. );
  78. $items['admin/tripal/chado/tripal_project/help']= array(
  79. 'title' => 'Help',
  80. 'description' => ("Basic Description of Tripal Project Module Functionality."),
  81. 'page callback' => 'theme',
  82. 'page arguments' => array('tripal_project.help'),
  83. 'access arguments' => array('adminster tripal projects'),
  84. 'type' => MENU_LOCAL_TASK,
  85. 'weight' => 6
  86. );
  87. $items['admin/tripal/chado/tripal_project/configuration']= array(
  88. 'title' => 'Settings',
  89. 'page callback' => 'drupal_get_form',
  90. 'page arguments' => array('tripal_project_admin'),
  91. 'access arguments' => array('adminster tripal projects'),
  92. 'type' => MENU_LOCAL_TASK,
  93. 'weight' => 4
  94. );
  95. $items['admin/tripal/chado/tripal_project/sync'] = array(
  96. 'title' => ' Sync',
  97. 'description' => 'Create pages on this site for projects stored in Chado',
  98. 'page callback' => 'drupal_get_form',
  99. 'page arguments' => array('tripal_core_chado_node_sync_form', 'tripal_project', 'chado_project'),
  100. 'access arguments' => array('administer tripal projects'),
  101. 'type' => MENU_LOCAL_TASK,
  102. 'weight' => 0
  103. );
  104. $items['admin/tripal/chado/tripal_project/views/projects/enable'] = array(
  105. 'title' => 'Enable Project Administrative View',
  106. 'page callback' => 'tripal_views_admin_enable_view',
  107. 'page arguments' => array('tripal_project_admin_projects', 'admin/tripal/chado/tripal_project'),
  108. 'access arguments' => array('administer tripal projects'),
  109. 'type' => MENU_CALLBACK,
  110. );
  111. return $items;
  112. }
  113. /**
  114. * Implements hook_help()
  115. * Purpose: Adds a help page to the module list
  116. */
  117. function tripal_project_help ($path, $arg) {
  118. if ($path == 'admin/help#tripal_project') {
  119. return theme('tripal_project_help', array());
  120. }
  121. }
  122. /**
  123. * Implements hook_perm()
  124. *
  125. * This function sets the permission for the user to access the information in the database.
  126. * This includes creating, inserting, deleting and updating of information in the database
  127. *
  128. *
  129. * @ingroup tripal_project
  130. */
  131. function tripal_project_permission() {
  132. return array(
  133. 'access chado_project content' => array(
  134. 'title' => t('View Projects'),
  135. 'description' => t('Allow users to view project pages.'),
  136. ),
  137. 'create chado_project content' => array(
  138. 'title' => t('Create Projects'),
  139. 'description' => t('Allow users to create new project pages.'),
  140. ),
  141. 'delete chado_project content' => array(
  142. 'title' => t('Delete Projects'),
  143. 'description' => t('Allow users to delete project pages.'),
  144. ),
  145. 'edit chado_project content' => array(
  146. 'title' => t('Edit Projects'),
  147. 'description' => t('Allow users to edit project pages.'),
  148. ),
  149. 'adminster tripal project' => array(
  150. 'title' => t('Administer Projects'),
  151. 'description' => t('Allow users to administer all projects.'),
  152. ),
  153. );
  154. }
  155. /**
  156. * We need to let drupal know about our theme functions and their arguments.
  157. * We create theme functions to allow users of the module to customize the
  158. * look and feel of the output generated in this module
  159. *
  160. * @ingroup tripal_project
  161. */
  162. function tripal_project_theme($existing, $type, $theme, $path) {
  163. $core_path = drupal_get_path('module', 'tripal_core');
  164. $items = array(
  165. 'node__chado_project' => array(
  166. 'template' => 'node--chado-generic',
  167. 'render element' => 'node',
  168. 'base hook' => 'node',
  169. 'path' => "$core_path/theme",
  170. ),
  171. 'tripal_project.base' => array(
  172. 'variables' => array('node' => NULL),
  173. 'template' => 'tripal_project.base',
  174. 'path' => "$path/theme/tripal_project",
  175. ),
  176. 'tripal_project.contact' => array(
  177. 'variables' => array('node' => NULL),
  178. 'template' => 'tripal_project.contact',
  179. 'path' => "$path/theme/tripal_project",
  180. ),
  181. 'tripal_project.properties' => array(
  182. 'variables' => array('node' => NULL),
  183. 'template' => 'tripal_project.properties',
  184. 'path' => "$path/theme/tripal_project",
  185. ),
  186. 'tripal_project.publications' => array(
  187. 'variables' => array('node' => NULL),
  188. 'template' => 'tripal_project.publications',
  189. 'path' => "$path/theme/tripal_project",
  190. ),
  191. 'tripal_project.relationships' => array(
  192. 'variables' => array('node' => NULL),
  193. 'template' => 'tripal_project.relationships',
  194. 'path' => "$path/theme/tripal_project",
  195. ),
  196. 'tripal_project.teaser' => array(
  197. 'variables' => array('node' => NULL),
  198. 'template' => 'tripal_project.teaser',
  199. 'path' => "$path/theme/tripal_project",
  200. ),
  201. 'tripal_project.help' => array(
  202. 'variables' => 'tripal_project.help',
  203. 'variables' => array(NULL),
  204. 'path' => "$path/theme",
  205. ),
  206. );
  207. return $items;
  208. }
  209. /**
  210. *
  211. * @ingroup tripal_feature
  212. */
  213. function tripal_project_node_view($node, $view_mode, $langcode) {
  214. switch ($node->type) {
  215. case 'chado_project':
  216. // Show feature browser and counts
  217. if ($view_mode == 'full') {
  218. $node->content['tripal_project.base'] = array(
  219. '#value' => theme('tripal_project.base', array('node' => $node)),
  220. );
  221. $node->content['tripal_project.contact'] = array(
  222. '#value' => theme('tripal_project.contact', array('node' => $node)),
  223. );
  224. $node->content['tripal_project.properties'] = array(
  225. '#value' => theme('tripal_project.properties', array('node' => $node)),
  226. );
  227. $node->content['tripal_project.publications'] = array(
  228. '#value' => theme('tripal_project.publications', array('node' => $node)),
  229. );
  230. $node->content['tripal_project.relationships'] = array(
  231. '#value' => theme('tripal_project.relationships', array('node' => $node)),
  232. );
  233. }
  234. if ($view_mode == 'teaser') {
  235. $node->content['tripal_project_teaser'] = array(
  236. '#value' => theme('tripal_project_teaser', array('node' => $node)),
  237. );
  238. }
  239. break;
  240. }
  241. }
  242. /**
  243. *
  244. * @ingroup tripal_project
  245. */
  246. function tripal_project_block_info() {
  247. $blocks['projectbase']['info'] = t('Tripal Project Details');
  248. $blocks['projectbase']['cache'] = DRUPAL_NO_CACHE;
  249. $blocks['projectprops']['info'] = t('Tripal Project Properties');
  250. $blocks['projectprops']['cache'] = DRUPAL_NO_CACHE;
  251. $blocks['projectpubs']['info'] = t('Tripal Project Publications');
  252. $blocks['projectpubs']['cache'] = DRUPAL_NO_CACHE;
  253. $blocks['projectcont']['info'] = t('Tripal Project Contact');
  254. $blocks['projectcont']['cache'] = DRUPAL_NO_CACHE;
  255. $blocks['projectrels']['info'] = t('Tripal Project Relationships');
  256. $blocks['projectrels']['cache'] = DRUPAL_NO_CACHE;
  257. return $blocks;
  258. }
  259. /**
  260. *
  261. * @ingroup tripal_project
  262. */
  263. function tripal_project_block_view($delta = '') {
  264. if (user_access('access chado_project content') and arg(0) == 'node' and is_numeric(arg(1))) {
  265. $nid = arg(1);
  266. $node = node_load($nid);
  267. $block = array();
  268. switch ($delta) {
  269. case 'projectbase':
  270. $block['subject'] = t('Project Details');
  271. $block['content'] = theme('tripal_project_base', $node);
  272. break;
  273. case 'projectprops':
  274. $block['subject'] = t('Properties');
  275. $block['content'] = theme('tripal_project_properties', $node);
  276. break;
  277. case 'projectpubs':
  278. $block['subject'] = t('Publications');
  279. $block['content'] = theme('tripal_project_publications', $node);
  280. break;
  281. case 'projectcont':
  282. $block['subject'] = t('Contact');
  283. $block['content'] = theme('tripal_project_contact', $node);
  284. break;
  285. case 'projectrels':
  286. $block['subject'] = t('Relationships');
  287. $block['content'] = theme('tripal_project_relationships', $node);
  288. break;
  289. default :
  290. }
  291. return $block;
  292. }
  293. }
  294. /**
  295. *
  296. *
  297. * @ingroup tripal_project
  298. */
  299. function tripal_project_preprocess_tripal_project_relationships(&$variables) {
  300. // we want to provide a new variable that contains the matched projects.
  301. $project = $variables['node']->project;
  302. // normally we would use tripal_core_expand_chado_vars to expand our
  303. // organism object and add in the relationships, however whan a large
  304. // number of relationships are present this significantly slows the
  305. // query, therefore we will manually perform the query
  306. $sql = "
  307. SELECT P.name, P.project_id, CP.nid, CVT.name as rel_type
  308. FROM project_relationship PR
  309. INNER JOIN {project} P ON PR.object_project_id = P.project_id
  310. INNER JOIN {cvterm} CVT ON PR.type_id = CVT.cvterm_id
  311. LEFT JOIN public.chado_project CP ON P.project_id = CP.project_id
  312. WHERE PR.subject_project_id = :project_id
  313. ";
  314. $as_subject = chado_query($sql, array(':project_id' => $project->project_id));
  315. $sql = "
  316. SELECT P.name, P.project_id, CP.nid, CVT.name as rel_type
  317. FROM project_relationship PR
  318. INNER JOIN {project} P ON PR.subject_project_id = P.project_id
  319. INNER JOIN {cvterm} CVT ON PR.type_id = CVT.cvterm_id
  320. LEFT JOIN public.chado_project CP ON P.project_id = CP.project_id
  321. WHERE PR.object_project_id = :project_id
  322. ";
  323. $as_object = chado_query($sql, array(':project_id' => $project->project_id));
  324. // combine both object and subject relationshisp into a single array
  325. $relationships = array();
  326. $relationships['object'] = array();
  327. $relationships['subject'] = array();
  328. // iterate through the object relationships
  329. while ($relationship = $as_object->fetchObject()) {
  330. // get the relationship and child types
  331. $rel_type = t(preg_replace('/_/', " ", $relationship->rel_type));
  332. $sub_type = t(preg_replace('/_/', " ", $relationship->sub_type));
  333. if (!array_key_exists($rel_type, $relationships['object'])) {
  334. $relationships['object'][$rel_type] = array();
  335. }
  336. if (!array_key_exists($sub_type, $relationships['object'][$rel_type])) {
  337. $relationships['object'][$rel_type][$sub_type] = array();
  338. }
  339. $relationships['object'][$rel_type][$sub_type][] = $relationship;
  340. }
  341. // now add in the subject relationships
  342. while ($relationship = $as_subject->fetchObject()) {
  343. // get the relationship and child types
  344. $rel_type = t(preg_replace('/_/', " ", $relationship->rel_type));
  345. $obj_type = t(preg_replace('/_/', " ", $relationship->obj_type));
  346. if (!array_key_exists($rel_type, $relationships['subject'])) {
  347. $relationships['subject'][$rel_type] = array();
  348. }
  349. if (!array_key_exists($obj_type, $relationships['subject'][$rel_type])) {
  350. $relationships['subject'][$rel_type][$obj_type] = array();
  351. }
  352. $relationships['subject'][$rel_type][$obj_type][] = $relationship;
  353. }
  354. $project->all_relationships = $relationships;
  355. }
  356. /**
  357. * Implementation of hook_form_alter()
  358. *
  359. * @param $form
  360. * @param $form_state
  361. * @param $form_id
  362. */
  363. function tripal_project_form_alter(&$form, &$form_state, $form_id) {
  364. // turn of preview button for insert/updates
  365. if ($form_id == "chado_project_node_form") {
  366. $form['actions']['preview']['#access'] = FALSE;
  367. }
  368. }