tripal_project.module 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  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 module for interfacing the GMOD chado database with Drupal, providing viewing of projects'),
  31. 'has_title' => TRUE,
  32. 'title_label' => t('Project Name'),
  33. 'had_body' => TRUE,
  34. 'body_label' => t('Full Description'),
  35. )
  36. );
  37. }
  38. /**
  39. * Implements hook_views_api()
  40. *
  41. * Purpose: Essentially this hook tells drupal that there is views support for
  42. * for this module which then includes tripal_project.views.inc where all the
  43. * views integration code is
  44. *
  45. * @ingroup tripal_project
  46. *
  47. */
  48. function tripal_project_views_api() {
  49. return array(
  50. 'api' => 2.0,
  51. );
  52. }
  53. /**
  54. * Implements hook_menu
  55. *
  56. * @ingroup tripal_project
  57. */
  58. function tripal_project_menu() {
  59. $items[ 'admin/tripal/chado/tripal_project' ]= array(
  60. 'title' => 'Projects',
  61. 'description' => ('A grouping of a variety of data (ie: group natural diversity experiment).'),
  62. 'page callback' => 'tripal_project_admin_project_view',
  63. 'access arguments' => array('adminster tripal projects'),
  64. 'type' => MENU_NORMAL_ITEM
  65. );
  66. $items[ 'admin/tripal/chado/tripal_project/help' ]= array(
  67. 'title' => 'Help',
  68. 'description' => ("A description of the Tripal Project module including a short description of it's usage."),
  69. 'page callback' => 'theme',
  70. 'page arguments' => array('tripal_project_help'),
  71. 'access arguments' => array('adminster tripal projects'),
  72. 'type' => MENU_LOCAL_TASK,
  73. 'weight' => 6
  74. );
  75. $items[ 'admin/tripal/chado/tripal_project/configuration' ]= array(
  76. 'title' => 'Settings',
  77. 'page callback' => 'drupal_get_form',
  78. 'page arguments' => array('tripal_project_admin'),
  79. 'access arguments' => array('adminster tripal projects'),
  80. 'type' => MENU_LOCAL_TASK,
  81. 'weight' => 4
  82. );
  83. $items['admin/tripal/chado/tripal_project/views/projects/enable'] = array(
  84. 'title' => 'Enable Project Administrative View',
  85. 'page callback' => 'tripal_views_admin_enable_view',
  86. 'page arguments' => array('tripal_project_admin_projects', 'admin/tripal/chado/tripal_project'),
  87. 'access arguments' => array('administer tripal_bulk_loader'),
  88. 'type' => MENU_CALLBACK,
  89. );
  90. return $items;
  91. }
  92. /**
  93. * Implements hook_help()
  94. * Purpose: Adds a help page to the module list
  95. */
  96. function tripal_project_help ($path, $arg) {
  97. if ($path == 'admin/help#tripal_project') {
  98. return theme('tripal_project_help', array());
  99. }
  100. }
  101. /**
  102. * Implements hook_perm()
  103. *
  104. * This function sets the permission for the user to access the information in the database.
  105. * This includes creating, inserting, deleting and updating of information in the database
  106. *
  107. *
  108. * @ingroup tripal_project
  109. */
  110. function tripal_project_permission() {
  111. return array(
  112. 'access chado_project content' => array(
  113. 'title' => t('View Projects'),
  114. 'description' => t('Allow users to view project pages.'),
  115. ),
  116. 'create chado_project content' => array(
  117. 'title' => t('Create Projects'),
  118. 'description' => t('Allow users to create new project pages.'),
  119. ),
  120. 'delete chado_project content' => array(
  121. 'title' => t('Delete Projects'),
  122. 'description' => t('Allow users to delete project pages.'),
  123. ),
  124. 'edit chado_project content' => array(
  125. 'title' => t('Edit Projects'),
  126. 'description' => t('Allow users to edit project pages.'),
  127. ),
  128. 'adminster tripal project' => array(
  129. 'title' => t('Administer Projects'),
  130. 'description' => t('Allow users to administer all projects.'),
  131. ),
  132. );
  133. }
  134. /**
  135. * Implement hook_access().
  136. *
  137. * This hook allows node modules to limit access to the node types they define.
  138. *
  139. * @param $node
  140. * The node on which the operation is to be performed, or, if it does not yet exist, the
  141. * type of node to be created
  142. *
  143. * @param $op
  144. * The operation to be performed
  145. *
  146. *
  147. * @param $account
  148. * A user object representing the user for whom the operation is to be performed
  149. *
  150. * @return
  151. * If the permission for the specified operation is not set then return FALSE. If the
  152. * permission is set then return NULL as this allows other modules to disable
  153. * access. The only exception is when the $op == 'create'. We will always
  154. * return TRUE if the permission is set.
  155. *
  156. * @ingroup tripal_project
  157. */
  158. function chado_project_node_access($node, $op, $account) {
  159. if ($op == 'create') {
  160. if (!user_access('create chado_projects content', $account)) {
  161. return FALSE;
  162. }
  163. return TRUE;
  164. }
  165. if ($op == 'update') {
  166. if (!user_access('edit chado_projects content', $account)) {
  167. return FALSE;
  168. }
  169. }
  170. if ($op == 'delete') {
  171. if (!user_access('delete chado_projects content', $account)) {
  172. return FALSE;
  173. }
  174. }
  175. if ($op == 'view') {
  176. if (!user_access('access chado_projects content', $account)) {
  177. return FALSE;
  178. }
  179. }
  180. return NULL;
  181. }
  182. /**
  183. * We need to let drupal know about our theme functions and their arguments.
  184. * We create theme functions to allow users of the module to customize the
  185. * look and feel of the output generated in this module
  186. *
  187. * @ingroup tripal_project
  188. */
  189. function tripal_project_theme() {
  190. $theme_path = drupal_get_path('module', 'tripal_project') . '/theme';
  191. $items = array(
  192. 'tripal_project_base' => array(
  193. 'arguments' => array('node' => NULL),
  194. 'template' => 'tripal_project_base',
  195. 'path' => "$theme_path/tripal_project",
  196. ),
  197. 'tripal_project_contact' => array(
  198. 'arguments' => array('node' => NULL),
  199. 'template' => 'tripal_project_contact',
  200. 'path' => "$theme_path/tripal_project",
  201. ),
  202. 'tripal_project_publications' => array(
  203. 'arguments' => array('node' => NULL),
  204. 'template' => 'tripal_project_publications',
  205. 'path' => "$theme_path/tripal_project",
  206. ),
  207. 'tripal_project_relationships' => array(
  208. 'arguments' => array('node' => NULL),
  209. 'template' => 'tripal_project_relationships',
  210. 'path' => "$theme_path/tripal_project",
  211. ),
  212. 'tripal_project_properties' => array(
  213. 'arguments' => array('node' => NULL),
  214. 'template' => 'tripal_project_properties',
  215. 'path' => "$theme_path/tripal_project",
  216. ),
  217. 'tripal_project_help' => array(
  218. 'template' => 'tripal_project_help',
  219. 'arguments' => array(NULL),
  220. 'path' => $theme_path,
  221. ),
  222. );
  223. return $items;
  224. }
  225. /**
  226. * Implementation of hook_form().
  227. *
  228. * This form takes the Project Title information and description from the user.
  229. *
  230. * @parm $node
  231. * The initialized node
  232. *
  233. * @parm $form_state
  234. * The state of the form, that has the user entered information that is neccessary for adding
  235. * information to the project
  236. *
  237. * @return $form
  238. * An array as described by the Drupal Form API
  239. *
  240. *
  241. * @ingroup tripal_project
  242. */
  243. function chado_project_form(&$node, $form_state) {
  244. $form = array();
  245. $project = $node->project;
  246. // get the project default values. When this module was first created
  247. // the project description was incorrectly stored in the $node->body field.
  248. // It is better to store it in the Chado tables. However, the 'description'
  249. // field of the project table is only 255 characters. So, we are going
  250. // to follow the same as the project module and store the description in
  251. // the projectprop table and leave the project.description field blank.
  252. // however, for backwards compatibitily, we check to see if the description
  253. // is in the $node->body field. If it is we'll use that. When the node is
  254. // edited the text will be moved out of the body and into the projectprop
  255. // table where it should belong.
  256. if ($node->body) {
  257. $project_description = $node->body;
  258. }
  259. else {
  260. $project_description = $node->project_description;
  261. }
  262. if (!$project_description) {
  263. $projectprop = tripal_project_get_property($project->project_id, 'project_description');
  264. $project_description = $projectprop->value;
  265. }
  266. // keep track of the project id if we have. If we do have one then
  267. // this is an update as opposed to an insert.
  268. $form['project_id'] = array(
  269. '#type' => 'value',
  270. '#value' => $project->project_id,
  271. );
  272. $form['title']= array(
  273. '#type' => 'textfield',
  274. '#title' => t('Project Title'),
  275. '#description' => t('Please enter the title for this project. This appears at the top of the project page.'),
  276. '#required' => TRUE,
  277. '#default_value' => $node->title,
  278. '#weight' => 1
  279. );
  280. $form['project_description']= array(
  281. '#type' => 'textarea',
  282. '#title' => t('Project Description'),
  283. '#description' => t('A brief description of the project'),
  284. '#required' => TRUE,
  285. '#default_value' => $project_description,
  286. '#weight' => 5
  287. );
  288. return $form;
  289. }
  290. /**
  291. * validates submission of form when adding or updating a project node
  292. *
  293. * @ingroup tripal_project
  294. */
  295. function chado_project_validate($node, $form, &$form_state) {
  296. $project = 0;
  297. // check to make sure the name on the project is unique
  298. // before we try to insert into chado.
  299. if ($node->project_id) {
  300. $sql = "SELECT * FROM {project} WHERE name = :name AND NOT project_id = :project_id";
  301. $project = chado_query($sql, array(':name' => $node->title, ':project_id' => $node->project_id))->fetchObject();
  302. }
  303. else {
  304. $sql = "SELECT * FROM {project} WHERE name = :name";
  305. $project = chado_query($sql, array(':name' => $node->title))->fetchObject();
  306. }
  307. if ($project) {
  308. form_set_error('title', t('The unique project name already exists. Please choose another'));
  309. }
  310. }
  311. /**
  312. * Implementation of hook_insert().
  313. *
  314. * @parm $node
  315. * Then node that has the information stored within, accessed given the nid
  316. *
  317. *
  318. * @ingroup tripal_project
  319. */
  320. function chado_project_insert($node) {
  321. if ($node->project_id) {
  322. $project['project_id'] = $node->project_id;
  323. }
  324. else {
  325. $values = array(
  326. 'name' => $node->title,
  327. 'description' => '',
  328. );
  329. $project = tripal_core_chado_insert('project', $values);
  330. }
  331. if ($project) {
  332. // add the description property
  333. tripal_project_insert_property($project['project_id'], 'project_description',
  334. $node->project_description);
  335. // make sure the entry for this feature doesn't already exist in the chado_project table
  336. // if it doesn't exist then we want to add it.
  337. $project_id = chado_get_id_for_node('project', $node->nid) ;
  338. if (!$project_id) {
  339. // next add the item to the drupal table
  340. $sql = "
  341. INSERT INTO {chado_project} (nid, vid, project_id)
  342. VALUES (:nid, :vid, :project_id)
  343. ";
  344. db_query($sql, array(':nid' => $node->nid, ':vid' => $node->vid, ':project_id' => $project['project_id']));
  345. }
  346. }
  347. else {
  348. drupal_set_message(t('Unable to add project.', 'warning'));
  349. watchdog('tripal_project', 'Insert feature: Unable to create project where values: %values',
  350. array('%values' => print_r($values, TRUE)), WATCHDOG_WARNING);
  351. }
  352. }
  353. /**
  354. *
  355. * Implementation of hook_delete().
  356. *
  357. * @param $node
  358. * The node which is to be deleted, only chado project and chado_project need to be dealt with
  359. * since the drupal node is deleted automagically
  360. *
  361. *
  362. * @ingroup tripal_project
  363. */
  364. function chado_project_delete($node) {
  365. $project_id = chado_get_id_for_node('project', $node->nid);
  366. // if we don't have a project id for this node then this isn't a node of
  367. // type chado_project or the entry in the chado_project table was lost.
  368. if (!$project_id) {
  369. return;
  370. }
  371. // Remove data from {chado_project}, {node} and {node_revisions} tables of
  372. // drupal database
  373. $sql_del = "DELETE FROM {chado_project} WHERE nid = :nid AND vid = :vid";
  374. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  375. $sql_del = "DELETE FROM {node_revision} WHERE nid = :nid AND vid = :vod";
  376. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  377. $sql_del = "DELETE FROM {node} WHERE nid = :nid AND vid = :vid";
  378. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  379. // Remove data from project and projectprop tables of chado database as well
  380. chado_query("DELETE FROM {projectprop} WHERE project_id = :project_id", array(':project_id' => $project_id));
  381. chado_query("DELETE FROM {project} WHERE project_id = :project_id", array(':project_id' => $project_id));
  382. }
  383. /**
  384. * Implements hook_update().
  385. *
  386. * @param $node
  387. * The node which is to have its containing information updated when the user modifies information
  388. * pertaining to the specific project
  389. *
  390. *
  391. * @ingroup tripal_project
  392. */
  393. function chado_project_update($node) {
  394. if ($node->revision) {
  395. // there is no way to handle revisions in Chado but leave
  396. // this here just to make note we've addressed it.
  397. }
  398. // update the project and the description
  399. $project_id = chado_get_id_for_node('project', $node->nid) ;
  400. $match = array('project_id' => $project_id);
  401. $values = array(
  402. 'name' => $node->title,
  403. 'description' => '',
  404. );
  405. $status = tripal_core_chado_update('project', $match, $values);
  406. tripal_project_update_property($project_id, 'project_description', $node->project_description, 1);
  407. }
  408. /**
  409. * Implementation of node_load().
  410. *
  411. * @param $node
  412. * The node that is to have its containing information loaded
  413. *
  414. * @return $node
  415. * The node, containing the loaded project with the current nid
  416. *
  417. *
  418. * @ingroup tripal_project
  419. */
  420. function chado_project_load($node) {
  421. // get the feature details from chado
  422. $project_id = chado_get_id_for_node('project', $node->nid);
  423. $values = array('project_id' => $project_id);
  424. $project = tripal_core_generate_chado_var('project', $values);
  425. $additions = new stdClass();
  426. $additions->project = $project;
  427. return $additions;
  428. }
  429. /**
  430. *
  431. * @ingroup tripal_project
  432. */
  433. function tripal_project_block_info() {
  434. $blocks['projectbase']['info'] = t('Tripal Project Details');
  435. $blocks['projectbase']['cache'] = DRUPAL_NO_CACHE;
  436. $blocks['projectprops']['info'] = t('Tripal Project Properties');
  437. $blocks['projectprops']['cache'] = DRUPAL_NO_CACHE;
  438. $blocks['projectpubs']['info'] = t('Tripal Project Publications');
  439. $blocks['projectpubs']['cache'] = DRUPAL_NO_CACHE;
  440. $blocks['projectcont']['info'] = t('Tripal Project Contact');
  441. $blocks['projectcont']['cache'] = DRUPAL_NO_CACHE;
  442. $blocks['projectrels']['info'] = t('Tripal Project Relationships');
  443. $blocks['projectrels']['cache'] = DRUPAL_NO_CACHE;
  444. return $blocks;
  445. }
  446. /**
  447. *
  448. * @ingroup tripal_project
  449. */
  450. function tripal_project_block_view($delta = '') {
  451. if (user_access('access chado_project content') and arg(0) == 'node' and is_numeric(arg(1))) {
  452. $nid = arg(1);
  453. $node = node_load($nid);
  454. $block = array();
  455. switch ($delta) {
  456. case 'projectbase':
  457. $block['subject'] = t('Project Details');
  458. $block['content'] = theme('tripal_project_base', $node);
  459. break;
  460. case 'projectprops':
  461. $block['subject'] = t('Properties');
  462. $block['content'] = theme('tripal_project_properties', $node);
  463. break;
  464. case 'projectpubs':
  465. $block['subject'] = t('Publications');
  466. $block['content'] = theme('tripal_project_publications', $node);
  467. break;
  468. case 'projectcont':
  469. $block['subject'] = t('Contact');
  470. $block['content'] = theme('tripal_project_contact', $node);
  471. break;
  472. case 'projectrels':
  473. $block['subject'] = t('Relationships');
  474. $block['content'] = theme('tripal_project_relationships', $node);
  475. break;
  476. default :
  477. }
  478. return $block;
  479. }
  480. }
  481. /**
  482. *
  483. *
  484. * @ingroup tripal_project
  485. */
  486. function tripal_project_preprocess_tripal_project_relationships(&$variables) {
  487. // we want to provide a new variable that contains the matched projects.
  488. $project = $variables['node']->project;
  489. // normally we would use tripal_core_expand_chado_vars to expand our
  490. // organism object and add in the relationships, however whan a large
  491. // number of relationships are present this significantly slows the
  492. // query, therefore we will manually perform the query
  493. $sql = "
  494. SELECT P.name, P.project_id, CP.nid, CVT.name as rel_type
  495. FROM project_relationship PR
  496. INNER JOIN {project} P ON PR.object_project_id = P.project_id
  497. INNER JOIN {cvterm} CVT ON PR.type_id = CVT.cvterm_id
  498. LEFT JOIN public.chado_project CP ON P.project_id = CP.project_id
  499. WHERE PR.subject_project_id = :project_id
  500. ";
  501. $as_subject = chado_query($sql, array(':project_id' => $project->project_id));
  502. $sql = "
  503. SELECT P.name, P.project_id, CP.nid, CVT.name as rel_type
  504. FROM project_relationship PR
  505. INNER JOIN {project} P ON PR.subject_project_id = P.project_id
  506. INNER JOIN {cvterm} CVT ON PR.type_id = CVT.cvterm_id
  507. LEFT JOIN public.chado_project CP ON P.project_id = CP.project_id
  508. WHERE PR.object_project_id = :project_id
  509. ";
  510. $as_object = chado_query($sql, array(':project_id' => $project->project_id));
  511. // combine both object and subject relationshisp into a single array
  512. $relationships = array();
  513. $relationships['object'] = array();
  514. $relationships['subject'] = array();
  515. // iterate through the object relationships
  516. while ($relationship = $as_object->fetchObject()) {
  517. // get the relationship and child types
  518. $rel_type = t(preg_replace('/_/', " ", $relationship->rel_type));
  519. $sub_type = t(preg_replace('/_/', " ", $relationship->sub_type));
  520. if (!array_key_exists($rel_type, $relationships['object'])) {
  521. $relationships['object'][$rel_type] = array();
  522. }
  523. if (!array_key_exists($sub_type, $relationships['object'][$rel_type])) {
  524. $relationships['object'][$rel_type][$sub_type] = array();
  525. }
  526. $relationships['object'][$rel_type][$sub_type][] = $relationship;
  527. }
  528. // now add in the subject relationships
  529. while ($relationship = $as_subject->fetchObject()) {
  530. // get the relationship and child types
  531. $rel_type = t(preg_replace('/_/', " ", $relationship->rel_type));
  532. $obj_type = t(preg_replace('/_/', " ", $relationship->obj_type));
  533. if (!array_key_exists($rel_type, $relationships['subject'])) {
  534. $relationships['subject'][$rel_type] = array();
  535. }
  536. if (!array_key_exists($obj_type, $relationships['subject'][$rel_type])) {
  537. $relationships['subject'][$rel_type][$obj_type] = array();
  538. }
  539. $relationships['subject'][$rel_type][$obj_type][] = $relationship;
  540. }
  541. $project->all_relationships = $relationships;
  542. }
  543. /**
  544. * Implementation of hook_form_alter()
  545. *
  546. * @param $form
  547. * @param $form_state
  548. * @param $form_id
  549. */
  550. function tripal_project_form_alter(&$form, &$form_state, $form_id) {
  551. // turn of preview button for insert/updates
  552. if ($form_id == "chado_project_node_form") {
  553. $form['actions']['preview']['#access'] = FALSE;
  554. }
  555. }