tripal_project.module 17 KB

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