tripal_bulk_loader.module 13 KB

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