tripal_eimage.module 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. <?php
  2. require('includes/tripal_eimage.admin.inc');
  3. require('api/tripal_eimage.api.inc');
  4. /**
  5. * @file
  6. * This file contains the basic functions needed for this drupal module.
  7. * The drupal tripal_eimage 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_eimage Eimage Module
  13. * @ingroup tripal_modules
  14. */
  15. /**
  16. * Implements hook_menu
  17. *
  18. * @ingroup tripal_eimage
  19. */
  20. function tripal_eimage_menu() {
  21. $items[ 'admin/tripal/tripal_eimage' ]= array(
  22. 'title' => 'Eimages',
  23. 'description' => ('A module for interfacing the GMOD chado database with Drupal, providing viewing of eimages'),
  24. 'page callback' => 'theme',
  25. 'page arguments' => array('tripal_eimage_admin'),
  26. 'access arguments' => array('adminster tripal eimages'),
  27. 'type' => MENU_NORMAL_ITEM
  28. );
  29. $items[ 'admin/tripal/tripal_eimage/configuration' ]= array(
  30. 'title' => 'Configuration',
  31. 'page callback' => 'drupal_get_form',
  32. 'page arguments' => array('tripal_eimage_admin'),
  33. 'access arguments' => array('adminster tripal eimages'),
  34. 'type' => MENU_NORMAL_ITEM
  35. );
  36. return $items;
  37. }
  38. /**
  39. * Implements hook_perm()
  40. *
  41. * This function sets the permission for the user to access the information in the database.
  42. * This includes creating, inserting, deleting and updating of information in the database
  43. *
  44. *
  45. * @ingroup tripal_eimage
  46. */
  47. function tripal_eimage_perm() {
  48. return array(
  49. 'access chado_eimages content',
  50. 'create chado_eimages content',
  51. 'delete chado_eimages content',
  52. 'edit chado_eimages content',
  53. 'adminster tripal eimages',
  54. );
  55. }
  56. /**
  57. * Implement hook_access().
  58. *
  59. * This hook allows node modules to limit access to the node types they define.
  60. *
  61. * @param $op
  62. * The operation to be performed
  63. *
  64. * @param $node
  65. * The node on which the operation is to be performed, or, if it does not yet exist, the
  66. * type of node to be created
  67. *
  68. * @param $account
  69. * A user object representing the user for whom the operation is to be performed
  70. *
  71. * @return
  72. * If the permission for the specified operation is not set then return FALSE. If the
  73. * permission is set then return NULL as this allows other modules to disable
  74. * access. The only exception is when the $op == 'create'. We will always
  75. * return TRUE if the permission is set.
  76. *
  77. * @ingroup tripal_eimage
  78. */
  79. function chado_eimage_access($op, $node, $account) {
  80. if ($op == 'create') {
  81. if (!user_access('create chado_eimages content', $account)) {
  82. return FALSE;
  83. }
  84. return TRUE;
  85. }
  86. if ($op == 'update') {
  87. if (!user_access('edit chado_eimages content', $account)) {
  88. return FALSE;
  89. }
  90. }
  91. if ($op == 'delete') {
  92. if (!user_access('delete chado_eimages content', $account)) {
  93. return FALSE;
  94. }
  95. }
  96. if ($op == 'view') {
  97. if (!user_access('access chado_eimages content', $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(Eimage Name) and body(Description) set to true so that they information can be
  108. * entered
  109. *
  110. *
  111. * @ingroup tripal_eimage
  112. */
  113. function tripal_eimage_node_info() {
  114. return array(
  115. 'chado_eimage' => array(
  116. 'name' => t('Eimage'),
  117. 'module' => 'chado_eimage',
  118. 'description' => t('A module for interfacing the GMOD chado database with Drupal, providing viewing of eimages'),
  119. 'has_title' => TRUE,
  120. 'title_label' => t('Eimage'),
  121. 'had_body' => FALSE
  122. )
  123. );
  124. }
  125. /**
  126. * We need to let drupal know about our theme functions and their arguments.
  127. * We create theme functions to allow users of the module to customize the
  128. * look and feel of the output generated in this module
  129. *
  130. * @ingroup tripal_eimage
  131. */
  132. function tripal_eimage_theme() {
  133. return array(
  134. 'tripal_eimage_base' => array(
  135. 'arguments' => array('node' => NULL),
  136. 'template' => 'tripal_eimage_base',
  137. ),
  138. 'tripal_eimage_admin' => array(
  139. 'template' => 'tripal_eimage_admin',
  140. 'arguments' => array(NULL),
  141. 'path' => drupal_get_path('module', 'tripal_eimage') . '/theme'
  142. ),
  143. );
  144. }
  145. /**
  146. * Implementation of hook_form().
  147. *
  148. * This form takes the Eimage Title information and description from the user.
  149. *
  150. * @parm $node
  151. * The initialized node
  152. *
  153. * @parm $form_state
  154. * The state of the form, that has the user entered information that is neccessary for adding
  155. * information to the eimage
  156. *
  157. * @return $form
  158. * An array as described by the Drupal Form API
  159. *
  160. *
  161. * @ingroup tripal_eimage
  162. */
  163. function chado_eimage_form(&$node, $form_state) {
  164. $form = array();
  165. $eimage = $node->eimage;
  166. // keep track of the eimage id if we have. If we do have one then
  167. // this is an update as opposed to an insert.
  168. $form['eimage_id'] = array(
  169. '#type' => 'value',
  170. '#value' => $eimage->eimage_id,
  171. );
  172. $form['title']= array(
  173. '#type' => 'textfield',
  174. '#title' => t('Eimage URI'),
  175. '#description' => t('Please enter the URI for this eimage. This appears at the top of the eimage page.'),
  176. '#required' => TRUE,
  177. '#default_value' => $node->title,
  178. '#weight' => 1
  179. );
  180. $form['eimage_type']= array(
  181. '#type' => 'textfield',
  182. '#title' => t('Eimage Type'),
  183. '#description' => t('Type of the eimage'),
  184. '#required' => FALSE,
  185. '#default_value' => $eimage_type,
  186. '#weight' => 2
  187. );
  188. $form['eimage_data']= array(
  189. '#type' => 'textarea',
  190. '#title' => t('Eimage Data'),
  191. '#description' => t('Data of the eimage'),
  192. '#required' => FALSE,
  193. '#default_value' => $eimage_data,
  194. '#weight' => 3
  195. );
  196. return $form;
  197. }
  198. /**
  199. * validates submission of form when adding or updating a eimage node
  200. *
  201. * @ingroup tripal_eimage
  202. */
  203. function chado_eimage_validate($node) {
  204. }
  205. /**
  206. * Implementation of hook_insert().
  207. *
  208. * @parm $node
  209. * Then node that has the information stored within, accessed given the nid
  210. *
  211. *
  212. * @ingroup tripal_eimage
  213. */
  214. function chado_eimage_insert($node) {
  215. if ($node->eimage_id) {
  216. $eimage['eimage_id'] = $node->eimage_id;
  217. }
  218. else {
  219. $image_uri = trim($node->title);
  220. $eimage_type = trim($node->eimage_type);
  221. $eimage_data = trim($node->eimage_data);
  222. $values = array(
  223. 'image_uri' => $image_uri,
  224. 'eimage_type' => $eimage_type ? $eimage_type : '',
  225. 'eimage_data' => $eimage_data ? $eimage_data : NULL,
  226. );
  227. $eimage = tripal_core_chado_insert('eimage', $values);
  228. }
  229. if ($eimage) {
  230. // make sure the entry for this feature doesn't already exist in the chado_eimage table
  231. // if it doesn't exist then we want to add it.
  232. $eimage_id = chado_get_id_for_node('eimage', $node) ;
  233. if (!$eimage_id) {
  234. // next add the item to the drupal table
  235. $sql = "INSERT INTO {chado_eimage} (nid, vid, eimage_id) ".
  236. "VALUES (%d, %d, %d)";
  237. db_query($sql, $node->nid, $node->vid, $eimage['eimage_id']);
  238. }
  239. }
  240. else {
  241. drupal_set_message(t('Unable to add eimage.', 'warning'));
  242. watchdog('tripal_eimage', 'Insert eimage: Unable to create eimage where values: %values',
  243. array('%values' => print_r($values, TRUE)), WATCHDOG_WARNING);
  244. }
  245. }
  246. /**
  247. *
  248. * Implementation of hook_delete().
  249. *
  250. * @param $node
  251. * The node which is to be deleted, only chado eimage and chado_eimage need to be dealt with
  252. * since the drupal node is deleted automagically
  253. *
  254. *
  255. * @ingroup tripal_eimage
  256. */
  257. function chado_eimage_delete($node) {
  258. $eimage_id = chado_get_id_for_node('eimage', $node);
  259. // if we don't have a eimage id for this node then this isn't a node of
  260. // type chado_eimage or the entry in the chado_eimage table was lost.
  261. if (!$eimage_id) {
  262. return;
  263. }
  264. // Remove data from {chado_eimage}, {node} and {node_revisions} tables of
  265. // drupal database
  266. $sql_del = "DELETE FROM {chado_eimage} ".
  267. "WHERE nid = %d ".
  268. "AND vid = %d";
  269. db_query($sql_del, $node->nid, $node->vid);
  270. $sql_del = "DELETE FROM {node_revisions} ".
  271. "WHERE nid = %d ".
  272. "AND vid = %d";
  273. db_query($sql_del, $node->nid, $node->vid);
  274. $sql_del = "DELETE FROM {node} ".
  275. "WHERE nid = %d ".
  276. "AND vid = %d";
  277. db_query($sql_del, $node->nid, $node->vid);
  278. // Remove data from eimage table of chado database as well
  279. chado_query("DELETE FROM {eimage} WHERE eimage_id = %d", $eimage_id);
  280. }
  281. /**
  282. * Implements hook_update().
  283. *
  284. * @param $node
  285. * The node which is to have its containing information updated when the user modifies information
  286. * pertaining to the specific eimage
  287. *
  288. *
  289. * @ingroup tripal_eimage
  290. */
  291. function chado_eimage_update($node) {
  292. if ($node->revision) {
  293. // there is no way to handle revisions in Chado but leave
  294. // this here just to make not we've addressed it.
  295. }
  296. // update the eimage and the description
  297. $eimage_id = chado_get_id_for_node('eimage', $node) ;
  298. $match = array(
  299. 'eimage_id' => $eimage_id,
  300. );
  301. $image_uri = trim($node->title);
  302. $eimage_type = trim($node->eimage_type);
  303. $eimage_data = trim($node->eimage_data);
  304. $values = array(
  305. 'image_uri' => $image_uri,
  306. 'eimage_type' => $eimage_type ? $eimage_type : '',
  307. 'eimage_data' => $eimage_data ? $eimage_data : NULL,
  308. );
  309. $status = tripal_core_chado_update('eimage', $match, $values);
  310. }
  311. /**
  312. * Implementation of node_load().
  313. *
  314. * @param $node
  315. * The node that is to have its containing information loaded
  316. *
  317. * @return $node
  318. * The node, containing the loaded eimage with the current nid
  319. *
  320. *
  321. * @ingroup tripal_eimage
  322. */
  323. function chado_eimage_load($node) {
  324. // get the feature details from chado
  325. $eimage_id = chado_get_id_for_node('eimage', $node);
  326. $values = array('eimage_id' => $eimage_id);
  327. $eimage = tripal_core_generate_chado_var('eimage', $values);
  328. $additions = new stdClass();
  329. $additions->eimage = $eimage;
  330. return $additions;
  331. }