tripal_project.module 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. <?php
  2. require('includes/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. * Implements hook_views_api()
  13. *
  14. * Purpose: Essentially this hook tells drupal that there is views support for
  15. * for this module which then includes tripal_project.views.inc where all the
  16. * views integration code is
  17. *
  18. */
  19. function tripal_project_views_api() {
  20. return array(
  21. 'api' => 2.0,
  22. );
  23. }
  24. /**
  25. * Implements hook_menu
  26. */
  27. function tripal_project_menu() {
  28. $items[ 'admin/tripal/tripal_project' ]= array(
  29. 'title' => 'Projects',
  30. 'page callback' => 'theme',
  31. 'page arguments' => array('tripal_project_admin'),
  32. 'access arguments' => array('adminster tripal projects'),
  33. 'type' => MENU_NORMAL_ITEM
  34. );
  35. $items[ 'admin/tripal/tripal_project/configuration' ]= array(
  36. 'title' => 'Configuration',
  37. 'page callback' => 'drupal_get_form',
  38. 'page arguments' => array('tripal_project_admin'),
  39. 'access arguments' => array('adminster tripal projects'),
  40. 'type' => MENU_NORMAL_ITEM
  41. );
  42. return $items;
  43. }
  44. /**
  45. * Implements hook_perm()
  46. *
  47. * This function sets the permission for the user to access the information in the database.
  48. * This includes creating, inserting, deleting and updating of information in the database
  49. *
  50. */
  51. function tripal_project_perm() {
  52. return array(
  53. 'access chado_projects content',
  54. 'create chado_projects content',
  55. 'delete chado_projects content',
  56. 'edit chado_projects content',
  57. 'adminster tripal projects',
  58. );
  59. }
  60. /**
  61. * Implements hook_access()
  62. *
  63. * This function sets the access permission for operations on the database.
  64. *
  65. * @parm $op
  66. * The operation that is to be performed
  67. *
  68. * @parm $node
  69. * The specific node that is to have the operation performed
  70. *
  71. * @parm $account
  72. * The account of the user that is performing the operations
  73. *
  74. * @return
  75. * True if a operation was performed
  76. *
  77. */
  78. function chado_project_access($op, $node, $account) {
  79. if ($op == 'create') {
  80. // Only users with permission to do so may create this node type.
  81. if (!user_access('create chado_projects', $account)) {
  82. return FALSE;
  83. }
  84. }
  85. // Users who create a node may edit or delete it later, assuming they have the necessary permissions.
  86. if ($op == 'update' || $op == 'delete') {
  87. if (!user_access('edit own chado_projects', $account)) {
  88. return FALSE;
  89. }
  90. if (user_access('edit own chado_projects', $account) &&
  91. $account->uid != $node->uid) {
  92. return FALSE;
  93. }
  94. }
  95. if ($op == 'view') {
  96. if (!user_access('access chado_projects', $account)) {
  97. return FALSE;
  98. }
  99. }
  100. return NULL;
  101. }
  102. /**
  103. * Implementation of hook_node_info().
  104. *
  105. * This node_info, is a simple node that describes the functionallity of the module. It specifies
  106. * that the title(Project Name) and body(Description) set to true so that they information can be
  107. * entered
  108. *
  109. */
  110. function tripal_project_node_info() {
  111. return array(
  112. 'chado_project' => array(
  113. 'name' => t('Project'),
  114. 'module' => 'chado_project',
  115. 'description' => t('A module for interfacing the GMOD chado database with Drupal, providing viewing of projects'),
  116. 'has_title' => TRUE,
  117. 'title_label' => t('Project Name'),
  118. 'had_body' => TRUE,
  119. 'body_label' => t('Full Description'),
  120. )
  121. );
  122. }
  123. /**
  124. * We need to let drupal know about our theme functions and their arguments.
  125. * We create theme functions to allow users of the module to customize the
  126. * look and feel of the output generated in this module
  127. *
  128. * @ingroup tripal_project
  129. */
  130. function tripal_project_theme() {
  131. return array(
  132. 'tripal_project_base' => array(
  133. 'arguments' => array('node' => NULL),
  134. 'template' => 'tripal_project_base',
  135. ),
  136. 'tripal_project_admin' => array(
  137. 'template' => 'tripal_project_admin',
  138. 'arguments' => array(NULL),
  139. 'path' => drupal_get_path('module', 'tripal_project') . '/theme'
  140. ),
  141. );
  142. }
  143. /**
  144. * Implementation of hook_form().
  145. *
  146. * This form takes the Project Title information and description from the user.
  147. *
  148. * @parm &$node
  149. * The initialized node
  150. *
  151. * @parm $form_state
  152. * The state of the form, that has the user entered information that is neccessary for adding
  153. * information to the project
  154. *
  155. * @return $form
  156. * An array as described by the Drupal Form API
  157. *
  158. */
  159. function chado_project_form(&$node, $form_state) {
  160. $type = node_get_types('type', $node);
  161. $form['title'] = array(
  162. '#type' => 'textfield',
  163. '#title' => check_plain($type->title_label),
  164. '#required' => TRUE,
  165. '#default_value' => $node->title,
  166. '#weight' => -5
  167. );
  168. $form['description'] = array(
  169. '#type' => 'textfield',
  170. '#maxlength' => 255,
  171. '#title' => 'Short Description',
  172. '#description' => t('Please provide a very short description (less than 255 characters) describing this project.'),
  173. '#default_value' => $node->project->description,
  174. );
  175. $form['body_filter']['body'] = array(
  176. '#type' => 'textarea',
  177. '#title' => check_plain($type->body_label),
  178. '#default_value' => $node->body,
  179. );
  180. $form['body_filter']['filter'] = filter_form($node->format);
  181. // whether or not the project exists in chado
  182. $form['project_id'] = array(
  183. '#type' => 'value',
  184. '#value' => ($node->project->project_id) ? $node->project->project_id : FALSE,
  185. );
  186. return $form;
  187. }
  188. /**
  189. * Implementation of hook_insert().
  190. *
  191. * @parm $node
  192. * Then node that has the information stored within, accessed given the nid
  193. *
  194. */
  195. function chado_project_insert($node) {
  196. $values = array(
  197. 'name' => $node->title,
  198. 'description' => $node->description,
  199. );
  200. if (!$node->project_id) {
  201. //inserts info into chado table.
  202. $result = tripal_core_chado_insert('project', $values);
  203. $node->project_id = $result['project_id'];
  204. }
  205. //inserts the row of vid,nid,project_id into the chado_project table
  206. db_query("INSERT INTO {chado_project} (vid, nid, project_id) VALUES (%d, %d, %d)", $node->vid, $node->nid, $node->project_id);
  207. }
  208. /**
  209. *
  210. * Implementation of hook_delete().
  211. *
  212. * @param $node
  213. * The node which is to be deleted, only chado project and chado_project need to be dealt with
  214. * since the drupal node is deleted automagically
  215. *
  216. */
  217. function chado_project_delete($node) {
  218. // Notice that we're matching all revision, by using the node's nid.
  219. // Find the project to delete
  220. $values = array(
  221. 'project_id' => $node->project->project_id,
  222. );
  223. tripal_core_chado_delete('project', $values);
  224. //deleteing in drupal chado_project table
  225. db_query('DELETE FROM {chado_project} WHERE nid = %d', $node->nid);
  226. }
  227. /**
  228. * Implements hook_update().
  229. *
  230. * @param $node
  231. * The node which is to have its containing information updated when the user modifies information
  232. * pertaining to the specific project
  233. *
  234. */
  235. function chado_project_update($node) {
  236. if ($node->revision) {
  237. // there is no way to handle revisions in Chado but leave
  238. // this here just to make not we've addressed it.
  239. }
  240. // Find the project to update
  241. $match= array(
  242. 'project_id' => $node->project_id,
  243. );
  244. // New values
  245. $values = array(
  246. 'name' => $node->title,
  247. 'description' => $node->description,
  248. );
  249. $result = tripal_core_chado_update('project', $match, $values);
  250. }
  251. /**
  252. * Implementation of node_load().
  253. *
  254. * @param $node
  255. * The node that is to have its containing information loaded
  256. *
  257. * @return $node
  258. * The node, containing the loaded project with the current nid
  259. *
  260. */
  261. function chado_project_load($node) {
  262. //selecting the coresponding table information
  263. $result = db_fetch_object(db_query('SELECT * FROM {chado_project} WHERE nid=%d AND vid=%d', $node->nid, $node->vid));
  264. //assigning the project-Id to a variable
  265. $values = array(
  266. 'project_id' => $result->project_id,
  267. );
  268. //the current project set to the 'project' with the $values(project-Id)
  269. $node->project = tripal_core_generate_chado_var('project', $values);
  270. return $node;
  271. }