tripal_project.module 17 KB

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