tripal_bulk_loader.module 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. <?php
  2. include('tripal_bulk_loader.loader.inc');
  3. /**
  4. * tripal_bulk_loader_init
  5. */
  6. function tripal_bulk_loader_init(){
  7. // Add javascript and style sheet
  8. drupal_add_css(drupal_get_path('theme', 'tripal').'/css/tripal_bulk_loader.css');
  9. drupal_add_js(drupal_get_path('theme', 'tripal').'/js/tripal_bulk_loader.js');
  10. }
  11. /**
  12. * tripal_bulk_loader_menu
  13. */
  14. function tripal_bulk_loader_menu() {
  15. $items = array();
  16. // Show all loaders
  17. $items['/tripal_bulk_loaders'] = array(
  18. 'title' => 'Tripal Bulk Loaders',
  19. 'description' => 'Tripal bulk loaders for loading tab-delimited file into chado database',
  20. 'page callback' => 'tripal_bulk_loader_list',
  21. 'access arguments' => array('access tripal_bulk_loader'),
  22. 'type' => MENU_NORMAL_ITEM,
  23. );
  24. // Admin page to create the template
  25. $items['admin/tripal/tripal_bulk_loader_template'] = array(
  26. 'title' => 'Bulk Loader Template',
  27. 'description' => 'Create loader template for loading tab-delimited data',
  28. 'page callback' => 'tripal_bulk_loader_admin_template',
  29. 'access arguments' => array('administer site configuration'),
  30. 'type' => MENU_NORMAL_ITEM,
  31. 'file' => 'tripal_bulk_loader.admin.inc',
  32. );
  33. $items['admin/tripal/tripal_bulk_loader_template/add'] = array(
  34. 'title' => 'Create Bulk Loader Template',
  35. 'description' => 'Create loader template for loading tab-delimited data',
  36. 'page callback' => 'drupal_get_form',
  37. 'page arguments' => array('tripal_bulk_loader_create_template_base_form'),
  38. 'access arguments' => array('administer site configuration'),
  39. 'type' => MENU_NORMAL_ITEM,
  40. 'file' => 'tripal_bulk_loader.admin.inc',
  41. );
  42. $items['admin/tripal/tripal_bulk_loader_template/add/chado_column_ahah'] = array(
  43. 'page callback' => 'tripal_bulk_loader_add_chado_column_ahah',
  44. 'access arguments' => array('administer site configuration'),
  45. 'type' => MENU_CALLBACK,
  46. 'file' => 'tripal_bulk_loader.admin.inc',
  47. );
  48. $items['admin/tripal/tripal_bulk_loader_template/add/field_type_ahah'] = array(
  49. 'page callback' => 'tripal_bulk_loader_add_field_type_ahah',
  50. 'access arguments' => array('administer site configuration'),
  51. 'type' => MENU_CALLBACK,
  52. 'file' => 'tripal_bulk_loader.admin.inc',
  53. );
  54. //tripal_bulk_loader_edit_template_base_form
  55. $items['admin/tripal/tripal_bulk_loader_template/edit'] = array(
  56. 'title' => 'Edit Bulk Loader Template',
  57. 'description' => 'Edit bulk loader template',
  58. 'page callback' => 'drupal_get_form',
  59. 'page arguments' => array('tripal_bulk_loader_edit_template_base_form'),
  60. 'access arguments' => array('administer site configuration'),
  61. 'type' => MENU_NORMAL_ITEM,
  62. 'file' => 'tripal_bulk_loader.admin.inc',
  63. );
  64. $items['admin/tripal/tripal_bulk_loader_template/edit/edit_fields_ahah'] = array(
  65. 'page callback' => 'tripal_bulk_loader_edit_fields_ahah',
  66. 'access arguments' => array('administer site configuration'),
  67. 'type' => MENU_CALLBACK,
  68. 'file' => 'tripal_bulk_loader.admin.inc',
  69. );
  70. $items['admin/tripal/tripal_bulk_loader_template/edit/chado_column_ahah'] = array(
  71. 'page callback' => 'tripal_bulk_loader_edit_chado_column_ahah',
  72. 'access arguments' => array('administer site configuration'),
  73. 'type' => MENU_CALLBACK,
  74. 'file' => 'tripal_bulk_loader.admin.inc',
  75. );
  76. $items['admin/tripal/tripal_bulk_loader_template/edit/field_type_ahah'] = array(
  77. 'page callback' => 'tripal_bulk_loader_edit_field_type_ahah',
  78. 'access arguments' => array('administer site configuration'),
  79. 'type' => MENU_CALLBACK,
  80. 'file' => 'tripal_bulk_loader.admin.inc',
  81. );
  82. $items['admin/tripal/tripal_bulk_loader_template/delete'] = array(
  83. 'title' => 'Delete Bulk Loader Template',
  84. 'description' => 'Delete bulk loader template',
  85. 'page callback' => 'drupal_get_form',
  86. 'page arguments' => array('tripal_bulk_loader_delete_template_base_form'),
  87. 'access arguments' => array('administer site configuration'),
  88. 'type' => MENU_NORMAL_ITEM,
  89. 'file' => 'tripal_bulk_loader.admin.inc',
  90. );
  91. return $items;
  92. }
  93. /**
  94. * tripal_bulk_loader_list
  95. */
  96. function tripal_bulk_loader_list () {
  97. return "Loaders";
  98. }
  99. /**
  100. * tripal_bulk_loader_access
  101. */
  102. function tripal_bulk_loader_access($op, $node, $account){
  103. if ($op == 'create') {
  104. return user_access('create tripal_bulk_loader', $account);
  105. }
  106. if ($op == 'update') {
  107. if (user_access('edit tripal_bulk_loader', $account)) {
  108. return TRUE;
  109. }
  110. }
  111. if ($op == 'delete') {
  112. if (user_access('delete tripal_bulk_loader', $account)) {
  113. return TRUE;
  114. }
  115. }
  116. if ($op == 'view') {
  117. if (user_access('access tripal_bulk_loader', $account)) {
  118. return TRUE;
  119. }
  120. }
  121. return FALSE;
  122. }
  123. /**
  124. * tripal_bulk_loader_perm
  125. */
  126. function tripal_bulk_loader_perm(){
  127. return array(
  128. 'access tripal_bulk_loader',
  129. 'create tripal_bulk_loader',
  130. 'delete tripal_bulk_loader',
  131. 'edit tripal_bulk_loader',
  132. );
  133. }
  134. /**
  135. * tripal_bulk_loader_node_info
  136. */
  137. function tripal_bulk_loader_node_info() {
  138. $nodes = array();
  139. $nodes['tripal_bulk_loader'] = array(
  140. 'name' => t('Bulk Loading Job'),
  141. 'module' => 'tripal_bulk_loader',
  142. 'description' => t('A bulk loader for inserting tab-delimited data into chado database'),
  143. 'has_title' => TRUE,
  144. 'has_body' => FALSE,
  145. 'locked' => TRUE
  146. );
  147. return $nodes;
  148. }
  149. /**
  150. * tripal_bulk_loader_form
  151. */
  152. function tripal_bulk_loader_form ($node){
  153. $form = array();
  154. $sql = "SELECT * FROM {tripal_bulk_loader_template}";
  155. $results = db_query($sql);
  156. $templates = array ();
  157. while ($template = db_fetch_object ($results)) {
  158. $templates [$template->template_id] = $template->name;
  159. }
  160. if (!$templates) {
  161. $form['label'] = array(
  162. '#type' => 'item',
  163. '#description' => t("Loader template needs to be created before any bulk loader can be added. Go to 'Tripal Management > Bulk Loader Template' to create the template."),
  164. '#weight' => 0,
  165. );
  166. return $form;
  167. }
  168. $form['loader_name'] = array(
  169. '#type' => 'textfield',
  170. '#title' => t('Loading Job Name'),
  171. '#weight' => -3,
  172. '#required' => TRUE,
  173. '#default_value' => $node->loader_name
  174. );
  175. $form['template_id'] = array(
  176. '#type' => 'select',
  177. '#title' => t('Template'),
  178. '#description' => t('Please specify a template for this loader'),
  179. '#options' => $templates,
  180. '#weight' => -2,
  181. '#required' => TRUE,
  182. '#default_value' => $node->template_id
  183. );
  184. $form['file']= array(
  185. '#type' => 'textfield',
  186. '#title' => t('Data File'),
  187. '#description' => t('Please specify the data file to be loaded.'),
  188. '#weight' => -1,
  189. '#default_value' => $node->file
  190. );
  191. return $form;
  192. }
  193. /**
  194. * tripal_bulk_loader_theme
  195. */
  196. function tripal_bulk_loader_theme() {
  197. return array(
  198. 'tripal_bulk_loader_node_form' => array(
  199. 'arguments' => array('form' => NULL),
  200. ),
  201. 'tripal_bulk_loader_template' => array(
  202. 'arguments'=> array('template_id' => NULL),
  203. 'template' => 'tripal_bulk_loader_template'
  204. ),
  205. 'tripal_bulk_loader_edit_template_base_form' => array(
  206. 'arguments' => array('form' => NULL),
  207. 'template' => 'tripal_bulk_loader_edit_template_base_form',
  208. ),
  209. );
  210. }
  211. /**
  212. * theme_tripal_bulk_loader_node_form
  213. */
  214. function theme_tripal_bulk_loader_node_form($form) {
  215. // Do not show [Save] and [Preview] buttons if loader template is not available
  216. if($form['label']['#type'] == "item") {
  217. unset($form['buttons']);
  218. }
  219. return drupal_render($form);
  220. }
  221. /**
  222. * tripal_bulk_loader_load
  223. */
  224. function tripal_bulk_loader_load($node){
  225. $sql = "SELECT * FROM {tripal_bulk_loader} WHERE nid = %d";
  226. $node = db_fetch_object(db_query($sql, $node->nid));
  227. $node->title = 'Bulk Loading Job: '.$node->loader_name;
  228. // Add the loader template
  229. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
  230. $results = db_fetch_object(db_query($sql, $node->template_id));
  231. $template = unserialize($results->template_array);
  232. $node->template = $results;
  233. $node->template->template_array = $template;
  234. return $node;
  235. }
  236. /**
  237. * tripal_bulk_loader_insert
  238. */
  239. function tripal_bulk_loader_insert ($node) {
  240. $sql = "INSERT INTO {tripal_bulk_loader} (nid, loader_name, template_id, file) VALUES (%d, '%s', %d, '%s')";
  241. db_query($sql, $node->nid, $node->loader_name, $node->template_id, $node->file);
  242. $node->title =$node->loader_name;
  243. drupal_write_record('node',$node,'nid');
  244. drupal_write_record('node_revision',$node,'nid');
  245. // Add a job if the user want to load the data
  246. global $user;
  247. if($node->job) {
  248. $job_args[0] =$node->loader_name;
  249. $job_args[1] = $node->template_id;
  250. $job_args[2] = $node->file;
  251. if (is_readable($node->file)) {
  252. $fname = preg_replace("/.*\/(.*)/", "$1", $node->file);
  253. tripal_add_job("Bulk Load: $fname",'tripal_bulk_loader', 'tripal_bulk_loader_load_data', $job_args, $user->uid);
  254. } else {
  255. drupal_set_message("Can not open $node->file. Job not scheduled.");
  256. }
  257. }
  258. }
  259. /**
  260. * tripal_bulk_loader_delete
  261. */
  262. function tripal_bulk_loader_delete ($node) {
  263. $sql = "DELETE FROM {tripal_bulk_loader} WHERE nid = %d";
  264. db_query($sql, $node->nid);
  265. }
  266. /**
  267. * tripal_bulk_loader_update
  268. */
  269. function tripal_bulk_loader_update ($node) {
  270. $sql = "UPDATE {tripal_bulk_loader} SET nid = %d, loader_name = '%s', template_id = %d, file = '%s' WHERE nid = %d";
  271. db_query($sql, $node->nid, $node->loader_name, $node->template_id, $node->file, $node->nid);
  272. // Add a job if the user want to load the data
  273. global $user;
  274. if($node->job) {
  275. $job_args[0] =$node->loader_name;
  276. $job_args[1] = $node->template_id;
  277. $job_args[2] = $node->file;
  278. if (is_readable($node->file)) {
  279. $fname = preg_replace("/.*\/(.*)/", "$1", $node->file);
  280. tripal_add_job("Bulk Load: $fname",'tripal_bulk_loader', 'tripal_bulk_loader_load_data', $job_args, $user->uid);
  281. } else {
  282. drupal_set_message("Can not open $node->file. Job not scheduled.");
  283. }
  284. }
  285. }
  286. function tripal_bulk_loader_preprocess_tripal_bulk_loader_template (&$variables) {
  287. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
  288. $template = db_fetch_object(db_query($sql, $variables['template_id']));
  289. $template->template_array = unserialize($template->template_array);
  290. $variables['template'] = $template;
  291. }