tripal_project.module 17 KB

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