tripal_bulk_loader.module 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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' => 'Templates 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/create'] = 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_modify_template_base_form', 'create'),
  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/edit'] = array(
  44. 'title' => 'Edit Bulk Loader Template',
  45. 'description' => 'Edit loader template for loading tab-delimited data',
  46. 'page callback' => 'drupal_get_form',
  47. 'page arguments' => array('tripal_bulk_loader_modify_template_base_form', 'edit'),
  48. 'access arguments' => array('administer site configuration'),
  49. 'type' => MENU_NORMAL_ITEM,
  50. 'file' => 'tripal_bulk_loader.admin.inc',
  51. );
  52. $items['admin/tripal/tripal_bulk_loader_template/add_field'] = array(
  53. 'title' => 'Add Template Field',
  54. 'description' => 'Add a template field to an existing tripal bulk loader template.',
  55. 'page callback' => 'drupal_get_form',
  56. 'page arguments' => array('tripal_bulk_loader_add_template_field_form'),
  57. 'access arguments' => array('administer site configuration'),
  58. 'type' => MENU_CALLBACK,
  59. 'file' => 'tripal_bulk_loader.admin.inc',
  60. );
  61. $items['admin/tripal/tripal_bulk_loader_template/edit_field'] = array(
  62. 'title' => 'Edit Template Field',
  63. 'description' => 'Edit an existing field from a tripal bulk loader template.',
  64. 'page callback' => 'drupal_get_form',
  65. 'page arguments' => array('tripal_bulk_loader_edit_template_field_form'),
  66. 'access arguments' => array('administer site configuration'),
  67. 'type' => MENU_CALLBACK,
  68. 'file' => 'tripal_bulk_loader.admin.inc',
  69. );
  70. // Delete Template -----
  71. $items['admin/tripal/tripal_bulk_loader_template/delete'] = array(
  72. 'title' => 'Delete Bulk Loader Template',
  73. 'description' => 'Delete bulk loader template',
  74. 'page callback' => 'drupal_get_form',
  75. 'page arguments' => array('tripal_bulk_loader_delete_template_base_form'),
  76. 'access arguments' => array('administer site configuration'),
  77. 'type' => MENU_NORMAL_ITEM,
  78. 'file' => 'tripal_bulk_loader.admin.inc',
  79. );
  80. // Import/Export ---------
  81. $items['admin/tripal/tripal_bulk_loader_template/import'] = array(
  82. 'title' => 'Import Bulk Loader Template',
  83. 'description' => 'Import Loaders',
  84. 'page callback' => 'drupal_get_form',
  85. 'page arguments' => array('tripal_bulk_loader_import_export_template_form', 'import'),
  86. 'access arguments' => array('administer site configuration'),
  87. 'type' => MENU_NORMAL_ITEM,
  88. 'file' => 'tripal_bulk_loader.admin.inc',
  89. );
  90. $items['admin/tripal/tripal_bulk_loader_template/export'] = array(
  91. 'title' => 'Export Bulk Loader Template',
  92. 'description' => 'Export Loaders',
  93. 'page callback' => 'drupal_get_form',
  94. 'page arguments' => array('tripal_bulk_loader_import_export_template_form', 'export'),
  95. 'access arguments' => array('administer site configuration'),
  96. 'type' => MENU_NORMAL_ITEM,
  97. 'file' => 'tripal_bulk_loader.admin.inc',
  98. );
  99. // AHAH ---------
  100. $items['admin/tripal/tripal_bulk_loader_template/add_field_ahah'] = array(
  101. 'page callback' => 'tripal_bulk_loader_add_field_ahah',
  102. 'access arguments' => array('administer site configuration'),
  103. 'type' => MENU_CALLBACK,
  104. 'file' => 'tripal_bulk_loader.admin.inc',
  105. );
  106. $items['admin/tripal/tripal_bulk_loader_template/edit_field_ahah'] = array(
  107. 'page callback' => 'tripal_bulk_loader_edit_field_ahah',
  108. 'access arguments' => array('administer site configuration'),
  109. 'type' => MENU_CALLBACK,
  110. 'file' => 'tripal_bulk_loader.admin.inc',
  111. );
  112. return $items;
  113. }
  114. /**
  115. * tripal_bulk_loader_list
  116. */
  117. function tripal_bulk_loader_list () {
  118. return "Loaders";
  119. }
  120. /**
  121. * tripal_bulk_loader_access
  122. */
  123. function tripal_bulk_loader_access($op, $node, $account){
  124. if ($op == 'create') {
  125. return user_access('create tripal_bulk_loader', $account);
  126. }
  127. if ($op == 'update') {
  128. if (user_access('edit tripal_bulk_loader', $account)) {
  129. return TRUE;
  130. }
  131. }
  132. if ($op == 'delete') {
  133. if (user_access('delete tripal_bulk_loader', $account)) {
  134. return TRUE;
  135. }
  136. }
  137. if ($op == 'view') {
  138. if (user_access('access tripal_bulk_loader', $account)) {
  139. return TRUE;
  140. }
  141. }
  142. return FALSE;
  143. }
  144. /**
  145. * tripal_bulk_loader_perm
  146. */
  147. function tripal_bulk_loader_perm(){
  148. return array(
  149. 'access tripal_bulk_loader',
  150. 'create tripal_bulk_loader',
  151. 'delete tripal_bulk_loader',
  152. 'edit tripal_bulk_loader',
  153. );
  154. }
  155. /**
  156. * tripal_bulk_loader_node_info
  157. */
  158. function tripal_bulk_loader_node_info() {
  159. $nodes = array();
  160. $nodes['tripal_bulk_loader'] = array(
  161. 'name' => t('Bulk Loading Job'),
  162. 'module' => 'tripal_bulk_loader',
  163. 'description' => t('A bulk loader for inserting tab-delimited data into chado database'),
  164. 'has_title' => TRUE,
  165. 'has_body' => FALSE,
  166. 'locked' => TRUE
  167. );
  168. return $nodes;
  169. }
  170. /**
  171. * tripal_bulk_loader_form
  172. */
  173. function tripal_bulk_loader_form ($node){
  174. $form = array();
  175. $sql = "SELECT * FROM {tripal_bulk_loader_template}";
  176. $results = db_query($sql);
  177. $templates = array ();
  178. while ($template = db_fetch_object ($results)) {
  179. $templates [$template->template_id] = $template->name;
  180. }
  181. if (!$templates) {
  182. $form['label'] = array(
  183. '#type' => 'item',
  184. '#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."),
  185. '#weight' => 0,
  186. );
  187. return $form;
  188. }
  189. $form['loader_name'] = array(
  190. '#type' => 'textfield',
  191. '#title' => t('Loading Job Name'),
  192. '#weight' => -3,
  193. '#required' => TRUE,
  194. '#default_value' => $node->loader_name
  195. );
  196. $form['template_id'] = array(
  197. '#type' => 'select',
  198. '#title' => t('Template'),
  199. '#description' => t('Please specify a template for this loader'),
  200. '#options' => $templates,
  201. '#weight' => -2,
  202. '#required' => TRUE,
  203. '#default_value' => $node->template_id
  204. );
  205. $form['file']= array(
  206. '#type' => 'textfield',
  207. '#title' => t('Data File'),
  208. '#description' => t('Please specify the data file to be loaded.'),
  209. '#weight' => -1,
  210. '#default_value' => $node->file
  211. );
  212. return $form;
  213. }
  214. /**
  215. * tripal_bulk_loader_theme
  216. */
  217. function tripal_bulk_loader_theme() {
  218. return array(
  219. 'tripal_bulk_loader_node_form' => array(
  220. 'arguments' => array('form' => NULL),
  221. ),
  222. 'tripal_bulk_loader_template' => array(
  223. 'arguments'=> array('template_id' => NULL),
  224. 'template' => 'tripal_bulk_loader_template'
  225. ),
  226. 'tripal_bulk_loader_modify_template_base_form' => array(
  227. 'arguments' => array('form' => NULL),
  228. 'template' => 'tripal_bulk_loader_modify_template_base_form',
  229. ),
  230. );
  231. }
  232. /**
  233. * theme_tripal_bulk_loader_node_form
  234. */
  235. function theme_tripal_bulk_loader_node_form($form) {
  236. // Do not show [Save] and [Preview] buttons if loader template is not available
  237. if($form['label']['#type'] == "item") {
  238. unset($form['buttons']);
  239. }
  240. return drupal_render($form);
  241. }
  242. /**
  243. * tripal_bulk_loader_load
  244. */
  245. function tripal_bulk_loader_load($node){
  246. $sql = "SELECT * FROM {tripal_bulk_loader} WHERE nid = %d";
  247. $node = db_fetch_object(db_query($sql, $node->nid));
  248. $node->title = 'Bulk Loading Job: '.$node->loader_name;
  249. // Add the loader template
  250. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
  251. $results = db_fetch_object(db_query($sql, $node->template_id));
  252. $template = unserialize($results->template_array);
  253. $node->template = $results;
  254. $node->template->template_array = $template;
  255. return $node;
  256. }
  257. /**
  258. * tripal_bulk_loader_insert
  259. */
  260. function tripal_bulk_loader_insert ($node) {
  261. $sql = "INSERT INTO {tripal_bulk_loader} (nid, loader_name, template_id, file) VALUES (%d, '%s', %d, '%s')";
  262. db_query($sql, $node->nid, $node->loader_name, $node->template_id, $node->file);
  263. $node->title =$node->loader_name;
  264. drupal_write_record('node',$node,'nid');
  265. drupal_write_record('node_revision',$node,'nid');
  266. // Add a job if the user want to load the data
  267. global $user;
  268. if($node->job) {
  269. $job_args[0] =$node->loader_name;
  270. $job_args[1] = $node->template_id;
  271. $job_args[2] = $node->file;
  272. if (is_readable($node->file)) {
  273. $fname = preg_replace("/.*\/(.*)/", "$1", $node->file);
  274. tripal_add_job("Bulk Load: $fname",'tripal_bulk_loader', 'tripal_bulk_loader_load_data', $job_args, $user->uid);
  275. } else {
  276. drupal_set_message("Can not open $node->file. Job not scheduled.");
  277. }
  278. }
  279. }
  280. /**
  281. * tripal_bulk_loader_delete
  282. */
  283. function tripal_bulk_loader_delete ($node) {
  284. $sql = "DELETE FROM {tripal_bulk_loader} WHERE nid = %d";
  285. db_query($sql, $node->nid);
  286. }
  287. /**
  288. * tripal_bulk_loader_update
  289. */
  290. function tripal_bulk_loader_update ($node) {
  291. $sql = "UPDATE {tripal_bulk_loader} SET nid = %d, loader_name = '%s', template_id = %d, file = '%s' WHERE nid = %d";
  292. db_query($sql, $node->nid, $node->loader_name, $node->template_id, $node->file, $node->nid);
  293. // Add a job if the user want to load the data
  294. global $user;
  295. if($node->job) {
  296. $job_args[0] =$node->loader_name;
  297. $job_args[1] = $node->template_id;
  298. $job_args[2] = $node->file;
  299. if (is_readable($node->file)) {
  300. $fname = preg_replace("/.*\/(.*)/", "$1", $node->file);
  301. tripal_add_job("Bulk Load: $fname",'tripal_bulk_loader', 'tripal_bulk_loader_load_data', $job_args, $user->uid);
  302. } else {
  303. drupal_set_message("Can not open $node->file. Job not scheduled.");
  304. }
  305. }
  306. }
  307. /**
  308. * Preprocessor function for the tripal_bulk_loader template
  309. */
  310. function tripal_bulk_loader_preprocess_tripal_bulk_loader_template (&$variables) {
  311. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
  312. $template = db_fetch_object(db_query($sql, $variables['template_id']));
  313. $template->template_array = unserialize($template->template_array);
  314. $variables['template'] = $template;
  315. }
  316. /**
  317. * Implements hook_job_describe_args()
  318. * Specifically to make viewing past tripal jobs more readable for jobs registered by this module
  319. *
  320. * @params $callback
  321. * The callback passed into tripal_add_job()
  322. * @param $args
  323. * The arguements passed into tripal_add_job()
  324. * @return
  325. * An array where keys are the human readable headers describing each arguement
  326. * and the value is the aguement passed in after formatting
  327. */
  328. function tripal_bulk_loader_job_describe_args($callback,$args){
  329. $new_args = array();
  330. if($callback == 'tripal_bulk_loader_load_data'){
  331. //1st arg is the nid for a bulk loader node
  332. $node = node_load($args[0]);
  333. $new_args['Bulk Loading Job'] = l($node->title, 'node/'.$args[0]);
  334. return $new_args;
  335. }
  336. }