tripal_bulk_loader.module 10 KB

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