tripal_project.module 18 KB

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