tripal_project.module 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <?php
  2. require('tripal_project.admin.inc');
  3. /**
  4. * @file
  5. * This file contains the basic functions needed for this drupal module.
  6. * The drupal tripal_project module maps directly to the chado general module.
  7. *
  8. * For documentation regarding the Chado General module:
  9. * @see http://gmod.org/wiki/Chado_General_Module
  10. */
  11. //-----------------------------------------------------------------------------
  12. // SECTION: Main Outline for Tripal Project Module
  13. //-----------------------------------------------------------------------------
  14. /**
  15. * Implements hook_views_api()
  16. *
  17. * Purpose: Essentially this hook tells drupal that there is views support for
  18. * for this module which then includes tripal_project.views.inc where all the
  19. * views integration code is
  20. *
  21. */
  22. function tripal_project_views_api() {
  23. return array(
  24. 'api' => 2.0,
  25. );
  26. }
  27. /**
  28. * Implements hook_menu
  29. */
  30. function tripal_project_menu() {
  31. $items[ 'admin/tripal/tripal_project' ]= array(
  32. 'title' => 'Projects',
  33. 'page callback' => 'tripal_project_administration_description_page',
  34. 'access arguments' => array('administer site configuration'),
  35. 'type' => MENU_NORMAL_ITEM
  36. );
  37. $items[ 'admin/tripal/tripal_project/configuration' ]= array(
  38. 'title' => 'Configuration',
  39. 'page callback' => 'tripal_project_configuration_page',
  40. 'access arguments' => array('administer site configuration'),
  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',
  55. 'create chado_projects',
  56. 'edit own chado_projects'
  57. );
  58. }
  59. /**
  60. * Implements hook_access()
  61. *
  62. * This function sets the access permission for operations on the database.
  63. *
  64. * @parm $op
  65. * The operation that is to be performed
  66. *
  67. * @parm $node
  68. * The specific node that is to have the operation performed
  69. *
  70. * @parm $account
  71. * The account of the user that is performing the operations
  72. *
  73. * @return
  74. * True if a operation was performed
  75. *
  76. */
  77. function chado_project_access($op, $node, $account) {
  78. if ($op == 'create') {
  79. // Only users with permission to do so may create this node type.
  80. if (!user_access('create chado_projects', $account)) {
  81. return FALSE;
  82. }
  83. }
  84. // Users who create a node may edit or delete it later, assuming they have the necessary permissions.
  85. if ($op == 'update' || $op == 'delete') {
  86. if (!user_access('edit own chado_projects', $account)) {
  87. return FALSE;
  88. }
  89. if (user_access('edit own chado_projects', $account) &&
  90. $account->uid != $node->uid) {
  91. return FALSE;
  92. }
  93. }
  94. if ($op == 'view') {
  95. if (!user_access('access chado_projects', $account)) {
  96. return FALSE;
  97. }
  98. }
  99. return NULL;
  100. }
  101. //-----------------------------------------------------------------------------
  102. // SECTION: Node Functionality
  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. * Implementation of hook_form().
  127. *
  128. * This form takes the Project Title information and description from the user.
  129. *
  130. * @parm &$node
  131. * The initialized node
  132. *
  133. * @parm $form_state
  134. * The state of the form, that has the user entered information that is neccessary for adding
  135. * information to the project
  136. *
  137. * @return $form
  138. * An array as described by the Drupal Form API
  139. *
  140. */
  141. function chado_project_form(&$node, $form_state) {
  142. $type = node_get_types('type', $node);
  143. $form['title'] = array(
  144. '#type' => 'textfield',
  145. '#title' => check_plain($type->title_label),
  146. '#required' => TRUE,
  147. '#default_value' => $node->title,
  148. '#weight' => -5
  149. );
  150. $form['description'] = array(
  151. '#type' => 'textfield',
  152. '#maxlength' => 255,
  153. '#title' => 'Short Description',
  154. '#default_value' => $node->project->description,
  155. );
  156. $form['body_filter']['body'] = array(
  157. '#type' => 'textarea',
  158. '#title' => check_plain($type->body_label),
  159. '#default_value' => $node->body,
  160. );
  161. $form['body_filter']['filter'] = filter_form($node->format);
  162. // whether or not the project exists in chado
  163. $form['project_id'] = array(
  164. '#type' => 'value',
  165. '#value' => ($node->project->project_id) ? $node->project->project_id : FALSE,
  166. );
  167. return $form;
  168. }
  169. /**
  170. * Implementation of hook_insert().
  171. *
  172. * @parm $node
  173. * Then node that has the information stored within, accessed given the nid
  174. *
  175. */
  176. function chado_project_insert($node) {
  177. $values = array(
  178. 'name' => $node->title,
  179. 'description' => $node->description,
  180. );
  181. if (!$node->project_id) {
  182. //inserts info into chado table.
  183. $result = tripal_core_chado_insert('project', $values);
  184. $node->project_id = $result['project_id'];
  185. }
  186. //inserts the row of vid,nid,project_id into the chado_project table
  187. db_query("INSERT INTO {chado_project} (vid, nid, project_id) VALUES (%d, %d, %d)", $node->vid, $node->nid, $node->project_id);
  188. }
  189. /**
  190. *
  191. * Implementation of hook_delete().
  192. *
  193. * @param $node
  194. * The node which is to be deleted, only chado project and chado_project need to be dealt with
  195. * since the drupal node is deleted automagically
  196. *
  197. */
  198. function chado_project_delete($node) {
  199. // Notice that we're matching all revision, by using the node's nid.
  200. // Find the project to delete
  201. $values = array(
  202. 'project_id' => $node->project->project_id,
  203. );
  204. tripal_core_chado_delete('project', $values);
  205. //deleteing in drupal chado_project table
  206. db_query('DELETE FROM {chado_project} WHERE nid = %d', $node->nid);
  207. }
  208. /**
  209. * Implements hook_update().
  210. *
  211. * @param $node
  212. * The node which is to have its containing information updated when the user modifies information
  213. * pertaining to the specific project
  214. *
  215. */
  216. function chado_project_update($node) {
  217. // Find the project to update
  218. $match= array(
  219. 'project_id' => $node->project_id,
  220. );
  221. // New values
  222. $values = array(
  223. 'name' => $node->title,
  224. 'description' => $node->description,
  225. );
  226. $result = tripal_core_chado_update('project', $match, $values);
  227. }
  228. /**
  229. * Implementation of node_load().
  230. *
  231. * @param $node
  232. * The node that is to have its containing information loaded
  233. *
  234. * @return $node
  235. * The node, containing the loaded project with the current nid
  236. *
  237. */
  238. function chado_project_load($node) {
  239. //selecting the coresponding table information
  240. $result = db_fetch_object(db_query('SELECT * FROM {chado_project} WHERE nid=%d AND vid=%d', $node->nid, $node->vid));
  241. //assigning the project-Id to a variable
  242. $values = array(
  243. 'project_id' => $result->project_id,
  244. );
  245. //the current project set to the 'project' with the $values(project-Id)
  246. $node->project = tripal_core_generate_chado_var('project', $values);
  247. return $node;
  248. }
  249. //-----------------------------------------------------------------------------
  250. // END OF SOFTWARE
  251. //-----------------------------------------------------------------------------