tripal_project.module 18 KB

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