tripal_project.module

  1. 2.x tripal_project/tripal_project.module
  2. 3.x legacy/tripal_project/tripal_project.module
  3. 1.x tripal_project/tripal_project.module

This file contains the basic functions needed for this drupal module. The drupal tripal_project module maps directly to the chado general module.

For documentation regarding the Chado General module:

tripal_project Project Module

See also

http://gmod.org/wiki/Chado_General_Module

File

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

Related topics