tripal_bulk_loader.module 12 KB

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