tripal_pub.module 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. <?php
  2. /**
  3. * @file
  4. * This file contains the basic functions needed for this drupal module.
  5. * The drupal tripal_pub module maps directly to the chado X module.
  6. *
  7. * For documentation regarding the Chado X module:
  8. * @see http://gmod.org/wiki/Chado_General_Module
  9. */
  10. //-----------------------------------------------------------------------------
  11. // SECTION: Main Section for Tripal Publication Module
  12. //-----------------------------------------------------------------------------
  13. /**
  14. * Implements hook_pub_views_api()
  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 located.
  18. */
  19. function tripal_pub_views_api() {
  20. return array(
  21. 'api' => 2.0,
  22. );
  23. }
  24. /**
  25. * Implementation of hook_node_info()
  26. *
  27. * This tripal_pub_node_info, is a simple node that describes the functionallity of the module. It specifies
  28. * that the title (Project Name) and body (Description) are set to true so the information can be
  29. * entered
  30. *
  31. */
  32. function tripal_pub_node_info() {
  33. return array(
  34. 'tripal_pub' => array(
  35. 'name' => t('Publication'),
  36. 'module' => 'tripal_pub',
  37. 'description' => t('A module for interfacing the GMOD chado database with Drupal, providing viewing of publications'),
  38. 'has_title' => TRUE,
  39. 'title_label' =>t('Title'),
  40. 'had_body' => FALSE,
  41. )
  42. );
  43. }
  44. /**
  45. * Implementation of tripal_pub_form().
  46. *
  47. * This form takes the following information:A Publication Title,Volume title,Volume,Series Name,
  48. * Issue,Publication Year,Pages where the Article is located, Miniref,Type-Id, if the article is Obsolete,
  49. * Publishing company,Pubplication Place and a Uniquename for the the instance. It then puts the
  50. * infromation into the Chado_project database table based on its 'pub_id'.
  51. *
  52. * @parm &$node
  53. * The node that is created when the database is initialized
  54. *
  55. * @parm $form_state
  56. * The state of the form, that has the user entered information that is neccessary for, setting
  57. * up the database of the project
  58. *
  59. * @return $form
  60. * The information that was enterd allong with
  61. *
  62. */
  63. function tripal_pub_form(&$node, $form_state) {
  64. $type = node_get_types('type', $node);
  65. //define form elements for the node's title and body.
  66. $form['title'] = array(
  67. '#type' => 'textfield',
  68. '#title' => t('Article Title'),
  69. '#description' => t('Title Heading'),
  70. '#required' => TRUE,
  71. '#default_value' => $node->title
  72. );
  73. $form['volumetitle'] = array(
  74. '#type' => 'textfield',
  75. '#title' => t('Volume Title'),
  76. '#description' => t('Title of part if one of a series.'),
  77. '#required' => TRUE,
  78. '#default_value' => isset($node->pub_id->volumetitle) ? $node->pub_id->volumetitle : ''
  79. );
  80. $form['volume'] = array(
  81. '#type' => 'textfield',
  82. '#title' => t('Volume'),
  83. '#required' => FALSE,
  84. '#default_value' => isset($node->pub_id->volume) ? $node->pub_id->volume : ''
  85. );
  86. $form['series_name'] = array(
  87. '#type' => 'textfield',
  88. '#title' => t('Series Name'),
  89. '#required' => TRUE,
  90. '#default_value' => isset($node->pub_id->series_name) ? $node->pub_id->series_name : ''
  91. );
  92. $form['issue'] = array(
  93. '#type' => 'textfield',
  94. '#title' => t('Issue'),
  95. '#required' => FALSE,
  96. '#default_value' => isset($node->pub_id->issue) ? $node->pub_id->issue : ''
  97. );
  98. $form['pyear'] = array(
  99. '#type' => 'textfield',
  100. '#title' => t('Publication Year'),
  101. '#required' => FALSE,
  102. '#default_value' => isset($node->pub_id->pyear) ? $node->pub_id->pyear : ''
  103. );
  104. $form['pages'] = array(
  105. '#type' => 'textfield',
  106. '#title' => t('Pages'),
  107. '#description' => t('Page number range[s], e.g. 457--459, viii + 664pp, lv--lvii.'),
  108. '#required' => TRUE,
  109. '#default_value' => isset($node->pub_id->pages) ? $node->pub_id->pages : ''
  110. );
  111. $form['miniref'] = array(
  112. '#type' => 'textfield',
  113. '#title' => t('Mini-Ref'),
  114. '#required' => FALSE,
  115. '#default_value' => isset($node->pub_id->miniref) ? $node->pub_id->miniref : ''
  116. );
  117. $form['uniquename'] = array(
  118. '#type' => 'textfield',
  119. '#title' => t('Unique Name'),
  120. '#required' => TRUE,
  121. '#default_value' => isset($node->pub_id->uniquename) ? $node->pub_id->uniquename : ''
  122. );
  123. $values= array(
  124. 'cv_id'=>array(
  125. 'name'=>'publication'),
  126. );
  127. //population select list with 'cvterm' names
  128. $result = tripal_core_chado_select ('cvterm', array('cvterm_id', 'name'), $values);
  129. foreach ($result as $value) {
  130. $newArray[$value->cvterm_id]=$value->name; //options for the select list
  131. }
  132. $form['type_id'] = array(
  133. '#type' => 'select',
  134. '#title' => t('Publication Type'),
  135. '#options' => $newArray,
  136. '#description' => t('Type of publication (book, journal, poem, graffiti, etc). Uses pub cv.'),
  137. '#required' => TRUE,
  138. '#default_value'=>isset($node->pub_id->type_id) ? $node->pub_id->type_id : ''
  139. );
  140. $form['is_obsolete'] = array(
  141. '#type' => 'checkbox',
  142. '#title' => t('Is Obsolete?(Check for Yes)'),
  143. '#required' => TRUE,
  144. '#default_value'=>isset($node->pub_id->is_obsolete) ? $node->pub_id->is_obsolete : FALSE
  145. );
  146. $form['publisher'] = array(
  147. '#type' => 'textfield',
  148. '#title' => t('Publisher Name'),
  149. '#required' => FALSE,
  150. '#default_value' => isset($node->pub_id->publisher) ? $node->pub_id->publisher : ''
  151. );
  152. $form['pubplace'] = array(
  153. '#type' => 'textfield',
  154. '#title' => t('Place of Publication'),
  155. '#required' => FALSE,
  156. '#default_value' => isset($node->pub_id->pubplace) ? $node->pub_id->pubplace : ''
  157. );
  158. return $form;
  159. }
  160. /**
  161. * Implements tripal_pub_help()
  162. *
  163. * This function simply states, in HTML tags, the creator of the the module and the contact
  164. * for the programmer
  165. *
  166. * @parm $path
  167. * The absolute path of the module and help information
  168. *
  169. * @parm $arg
  170. * The argument
  171. */
  172. function tripal_pub_help($path, $arg) {
  173. switch ($path) {
  174. case 'admin/help#tripal_pub':
  175. return '<p>'. t('Module Created By:Chad Krilow (e-mail:cnk046@mail.usask.ca)') .'</p>';
  176. break;
  177. }
  178. }
  179. /**
  180. * Implements hook_pub_perm()
  181. *
  182. * This function sets the permission for the user to access the information in the database.
  183. * This includes creating, inserting, deleting and updating of information in the database
  184. *
  185. */
  186. function tripal_pub_perm() {
  187. return array('create tripal_pub', 'edit own tripal_pub');
  188. }
  189. /**
  190. * Implements tripal_project_access()
  191. *
  192. * This function sets the access permission for database operations.
  193. *
  194. * @parm $op
  195. * The operation that is to be performed
  196. *
  197. * @parm $node
  198. * The specific node which is to have the operation performed
  199. *
  200. * @parm $account
  201. * The account of the user that is performing the operations
  202. *
  203. * @return
  204. * True if a operation was performed, else False
  205. *
  206. */
  207. function tripal_pub_access($op, $node, $account) {
  208. if ($op == 'create') {
  209. // Only users with permission to do so may create this node type.
  210. if(!user_access('create tripal_pub', $account)){
  211. return FALSE;
  212. }
  213. }
  214. // Users who create a node may edit or delete it later, assuming they have the
  215. // necessary permissions.
  216. if ($op == 'update' || $op == 'delete') {
  217. if (!user_access('edit own tripal_pub',$account)) {
  218. return FALSE;
  219. }
  220. if (user_access('edit own tripal_pub',$account) && ($account->uid != $node->uid)) {
  221. return FALSE;
  222. }
  223. }
  224. return NULL;
  225. }
  226. /**
  227. * Implementation of tripal_pub_insert()
  228. *
  229. * This function takes the user entered information and inserts it into the database
  230. *
  231. * @parm $node
  232. * Then node which has the information stored within the node-ID
  233. *
  234. */
  235. function tripal_pub_insert($node) {
  236. $values = array(
  237. 'title' => $node->title,
  238. 'volumetitle' => $node->volumetitle,
  239. 'volume'=>$node->volume,
  240. 'series_name'=>$node->series_name,
  241. 'issue'=>$node->issue,
  242. 'pyear'=>$node->pyear,
  243. 'pages'=>$node->pages,
  244. 'miniref'=>$node->miniref,
  245. 'type_id'=>$node->type_id,
  246. 'is_obsolete'=>$node->is_obsolete,
  247. 'publisher'=>$node->publisher,
  248. 'pubplace'=>$node->pubplace,
  249. 'uniquename' => $node->uniquename
  250. );
  251. //inserts info into chado table.
  252. $result = tripal_core_chado_insert('pub',$values);
  253. //inserts the row of vid,nid,project_id into the chado_project table
  254. db_query("INSERT INTO {chado_pub} (nid, vid, pub_id) VALUES (%d, %d, %d)",
  255. $node->nid,
  256. $node->vid,
  257. $result['pub_id']
  258. );
  259. }
  260. /**
  261. * Implementation of tripal_pub_delete().
  262. *
  263. * This function takes a node that is linked to a publication-Id, and if 'Delete' is selected
  264. * in the 'Edit' section of the page, the Selected publication is deleted from the database.
  265. *
  266. * @param $node
  267. * The node that is to be deleted
  268. */
  269. function tripal_pub_delete($node) {
  270. // Matching all revision, by using the node's pub_id.
  271. $values = array(
  272. 'pub_id' => $node->pub_id->pub_id,
  273. );
  274. //deleting row in chado table
  275. tripal_core_chado_delete('pub',$values);
  276. //deleteing in drupal chado_project table
  277. db_query('DELETE FROM {chado_pub} WHERE nid = %d', $node->nid);
  278. }
  279. /**
  280. *Implementation of tripal_pub_update().
  281. *
  282. * This function updates the node that is to be modified. All the modified aspects of the
  283. * database are updated as they are edited in the 'Edit' form.
  284. *
  285. * @param $node
  286. * the node that is to be modified, all of the modified sections of the chado table are updated
  287. *
  288. */
  289. function tripal_pub_update($node){
  290. $values = array(
  291. 'title' => $node->title,
  292. 'volumetitle' => $node->volumetitle,
  293. 'volume'=>$node->volume,
  294. 'series_name'=>$node->series_name,
  295. 'issue'=>$node->issue,
  296. 'pyear'=>$node->pyear,
  297. 'pages'=>$node->pages,
  298. 'miniref'=>$node->miniref,
  299. 'uniquename'=>$node->uniquename,
  300. 'type_id'=>$node->type_id,
  301. 'is_obsolete'=>$node->is_obsolete,
  302. 'publisher'=>$node->publisher,
  303. 'pubplace'=>$node->pubplace,
  304. );
  305. $result = db_fetch_object(db_query('SELECT pub_id FROM {chado_pub} WHERE nid=%d AND vid=%d ',$node->nid, $node->vid));
  306. //extract pub_id from the chad table for update function
  307. $match = array( 'pub_id' => $result->pub_id ) ;
  308. //$table to be updated, $match is the 'pub_id', $value are the values that are to be updated
  309. $update_result = tripal_core_chado_update('pub',$match,$values);
  310. }
  311. /**
  312. * Implementation of tripal_pub_load().
  313. *
  314. * This function is used to load the information, based on the 'pub_id', into the database
  315. *
  316. * @param $node
  317. * The node that contains the information which is to be loaded into the database, contains the
  318. * user entered information
  319. *
  320. * @return $node
  321. * The node containing the information which is to loaded in the database
  322. */
  323. function tripal_pub_load($node) {
  324. $result = db_fetch_object(db_query('SELECT * FROM {chado_pub} WHERE nid=%d AND vid=%d ',$node->nid, $node->vid));
  325. $values = array(
  326. 'pub_id' => $result->pub_id,
  327. );
  328. if(empty($result->pub_id)){
  329. drupal_set_message("Unable to find publication", 'error');
  330. }else{
  331. $node->pub_id = tripal_core_generate_chado_var('pub',$values);
  332. }
  333. return $node;
  334. }
  335. //-----------------------------------------------------------------------------
  336. // END OF SOFTWARE
  337. //-----------------------------------------------------------------------------