tripal_project.module 17 KB

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