tripal_bulk_loader.module 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. <?php
  2. include('tripal_bulk_loader.loader.inc');
  3. include('tripal_bulk_loader.constants.inc');
  4. include('tripal_bulk_loader.admin.inc');
  5. include('tripal_bulk_loader.admin.templates.inc');
  6. /**
  7. * Implements hook_init
  8. * Used to add stylesheets and javascript files to the header
  9. */
  10. function tripal_bulk_loader_init() {
  11. // Add javascript and style sheet
  12. drupal_add_css(drupal_get_path('theme', 'tripal') . '/css/tripal_bulk_loader.css');
  13. drupal_add_js(drupal_get_path('theme', 'tripal') . '/js/tripal_bulk_loader.js');
  14. }
  15. /**
  16. * Implements hook_menu
  17. */
  18. function tripal_bulk_loader_menu() {
  19. $items = array();
  20. // Show all loaders
  21. $items['tripal_bulk_loaders'] = array(
  22. 'title' => 'Tripal Bulk Loaders',
  23. 'description' => 'Tripal bulk loaders for loading tab-delimited file into chado database',
  24. 'page callback' => 'tripal_bulk_loader_list',
  25. 'access arguments' => array('access tripal_bulk_loader'),
  26. 'type' => MENU_NORMAL_ITEM,
  27. );
  28. // Bulk Loading Job Node
  29. $items['node/%node/constants/%/edit'] = array(
  30. 'title' => 'Edit Constant Set',
  31. 'description' => 'Edit a group of constants associated with the current bulk loader',
  32. 'page callback' => 'drupal_get_form',
  33. 'page arguments' => array('tripal_bulk_loader_edit_constant_set_form', 1, 3),
  34. 'access arguments' => array('administer site configuration'),
  35. 'type' => MENU_CALLBACK,
  36. );
  37. $items['node/%node/constants/%/delete'] = array(
  38. 'title' => 'Delete Constant Set',
  39. 'description' => 'Delete a group of constants associated with the current bulk loader',
  40. 'page callback' => 'drupal_get_form',
  41. 'page arguments' => array('tripal_bulk_loader_delete_constant_set_form', 1, 3),
  42. 'access arguments' => array('administer site configuration'),
  43. 'type' => MENU_CALLBACK,
  44. );
  45. // Admin pages -----------------
  46. $items['admin/tripal/tripal_bulk_loader_template'] = array(
  47. 'title' => 'Bulk Loader',
  48. 'description' => 'Templates for loading tab-delimited data',
  49. 'page callback' => 'tripal_bulk_loader_admin_template',
  50. 'access arguments' => array('administer site configuration'),
  51. 'type' => MENU_NORMAL_ITEM,
  52. );
  53. $items['admin/tripal/tripal_bulk_loader_template/configure'] = array(
  54. 'title' => 'Configure',
  55. 'description' => 'Configuration of global options related to bulk loading jobs',
  56. 'page callback' => 'drupal_get_form',
  57. 'page arguments' => array('tripal_bulk_loader_configuration_form'),
  58. 'access arguments' => array('administer site configuration'),
  59. 'type' => MENU_NORMAL_ITEM,
  60. );
  61. $items['admin/tripal/tripal_bulk_loader_template/manage_templates'] = array(
  62. 'title' => 'Manage Templates',
  63. 'description' => 'Create/Update/Delete/Import/Export Templates',
  64. 'page callback' => 'tripal_bulk_loader_admin_manage_templates',
  65. 'access arguments' => array('administer site configuration'),
  66. 'type' => MENU_NORMAL_ITEM,
  67. );
  68. $items['admin/tripal/tripal_bulk_loader_template/jobs'] = array(
  69. 'title' => 'Jobs',
  70. 'description' => 'Listing of Bulk Loading Jobs',
  71. 'page callback' => 'tripal_bulk_loader_admin_jobs',
  72. 'access arguments' => array('administer site configuration'),
  73. 'type' => MENU_NORMAL_ITEM,
  74. );
  75. // Create/Edit Template --------
  76. $items['admin/tripal/tripal_bulk_loader_template/manage_templates/create'] = array(
  77. 'title' => 'Create Template',
  78. 'description' => 'Create loader template for loading tab-delimited data',
  79. 'page callback' => 'drupal_get_form',
  80. 'page arguments' => array('tripal_bulk_loader_modify_template_base_form', 'create'),
  81. 'access arguments' => array('administer site configuration'),
  82. 'type' => MENU_NORMAL_ITEM,
  83. );
  84. $items['admin/tripal/tripal_bulk_loader_template/manage_templates/edit'] = array(
  85. 'title' => 'Edit Template',
  86. 'description' => 'Edit loader template for loading tab-delimited data',
  87. 'page callback' => 'drupal_get_form',
  88. 'page arguments' => array('tripal_bulk_loader_modify_template_base_form', 'edit'),
  89. 'access arguments' => array('administer site configuration'),
  90. 'type' => MENU_NORMAL_ITEM,
  91. );
  92. $items['admin/tripal/tripal_bulk_loader_template/edit_record'] = array(
  93. 'title' => 'Edit Template Record',
  94. 'description' => 'Edit a record in an existing tripal bulk loader template.',
  95. 'page callback' => 'drupal_get_form',
  96. 'page arguments' => array('tripal_bulk_loader_edit_template_record_form'),
  97. 'access arguments' => array('administer site configuration'),
  98. 'type' => MENU_CALLBACK,
  99. );
  100. $items['admin/tripal/tripal_bulk_loader_template/add_field'] = array(
  101. 'title' => 'Add Template Field',
  102. 'description' => 'Add a template field to an existing tripal bulk loader template.',
  103. 'page callback' => 'drupal_get_form',
  104. 'page arguments' => array('tripal_bulk_loader_add_template_field_form'),
  105. 'access arguments' => array('administer site configuration'),
  106. 'type' => MENU_CALLBACK,
  107. );
  108. $items['admin/tripal/tripal_bulk_loader_template/edit_field'] = array(
  109. 'title' => 'Edit Template Field',
  110. 'description' => 'Edit an existing field from a tripal bulk loader template.',
  111. 'page callback' => 'drupal_get_form',
  112. 'page arguments' => array('tripal_bulk_loader_edit_template_field_form'),
  113. 'access arguments' => array('administer site configuration'),
  114. 'type' => MENU_CALLBACK,
  115. );
  116. // Delete Template -----
  117. $items['admin/tripal/tripal_bulk_loader_template/manage_templates/delete'] = array(
  118. 'title' => 'Delete Template',
  119. 'description' => 'Delete bulk loader template',
  120. 'page callback' => 'drupal_get_form',
  121. 'page arguments' => array('tripal_bulk_loader_delete_template_base_form'),
  122. 'access arguments' => array('administer site configuration'),
  123. 'type' => MENU_NORMAL_ITEM,
  124. );
  125. // Import/Export ---------
  126. $items['admin/tripal/tripal_bulk_loader_template/manage_templates/import'] = array(
  127. 'title' => 'Import Template',
  128. 'description' => 'Import Loaders',
  129. 'page callback' => 'drupal_get_form',
  130. 'page arguments' => array('tripal_bulk_loader_import_export_template_form', 'import'),
  131. 'access arguments' => array('administer site configuration'),
  132. 'type' => MENU_NORMAL_ITEM,
  133. );
  134. $items['admin/tripal/tripal_bulk_loader_template/manage_templates/export'] = array(
  135. 'title' => 'Export Template',
  136. 'description' => 'Export Loaders',
  137. 'page callback' => 'drupal_get_form',
  138. 'page arguments' => array('tripal_bulk_loader_import_export_template_form', 'export'),
  139. 'access arguments' => array('administer site configuration'),
  140. 'type' => MENU_NORMAL_ITEM,
  141. );
  142. // AHAH ---------
  143. $items['admin/tripal/tripal_bulk_loader_template/add_field_ahah'] = array(
  144. 'page callback' => 'tripal_bulk_loader_add_field_ahah',
  145. 'access arguments' => array('administer site configuration'),
  146. 'type' => MENU_CALLBACK,
  147. );
  148. $items['admin/tripal/tripal_bulk_loader_template/edit_field_ahah'] = array(
  149. 'page callback' => 'tripal_bulk_loader_edit_field_ahah',
  150. 'access arguments' => array('administer site configuration'),
  151. 'type' => MENU_CALLBACK,
  152. );
  153. return $items;
  154. }
  155. /**
  156. * Implements hook_theme
  157. */
  158. function tripal_bulk_loader_theme() {
  159. return array(
  160. 'tripal_bulk_loader_set_constants_form' => array(
  161. 'arguments' => array('form' => NULL),
  162. ),
  163. 'tripal_bulk_loader_template' => array(
  164. 'arguments' => array('template_id' => NULL),
  165. 'template' => 'tripal_bulk_loader_template'
  166. ),
  167. 'tripal_bulk_loader_modify_template_base_form' => array(
  168. 'arguments' => array('form' => NULL),
  169. 'template' => 'tripal_bulk_loader_modify_template_base_form',
  170. ),
  171. 'tripal_bulk_loader_edit_template_field_form' => array(
  172. 'arguments' => array('form' => NULL),
  173. 'template' => 'tripal_bulk_loader_edit_template_field_form',
  174. ),
  175. 'tripal_bulk_loader_add_template_field_form' => array(
  176. 'arguments' => array('form' => NULL),
  177. 'template' => 'tripal_bulk_loader_add_template_field_form',
  178. ),
  179. );
  180. }
  181. /**
  182. * Implements hook_access
  183. */
  184. function tripal_bulk_loader_access($op, $node, $account) {
  185. if ($op == 'create') {
  186. if (!user_access('create tripal_bulk_loader', $account)) {
  187. return FALSE;
  188. }
  189. }
  190. if ($op == 'update') {
  191. if (!user_access('edit tripal_bulk_loader', $account)) {
  192. return FALSE;
  193. }
  194. }
  195. if ($op == 'delete') {
  196. if (!user_access('delete tripal_bulk_loader', $account)) {
  197. return FALSE;
  198. }
  199. }
  200. if ($op == 'view') {
  201. if (!user_access('access tripal_bulk_loader', $account)) {
  202. return FALSE;
  203. }
  204. }
  205. return NULL;
  206. }
  207. /**
  208. * Implements hook_perm
  209. */
  210. function tripal_bulk_loader_perm() {
  211. return array(
  212. 'access tripal_bulk_loader',
  213. 'create tripal_bulk_loader',
  214. 'delete tripal_bulk_loader',
  215. 'edit tripal_bulk_loader',
  216. );
  217. }
  218. /**
  219. * Creates a listing page for all bulk loading jobs
  220. */
  221. function tripal_bulk_loader_list() {
  222. $num_results_per_page = 50;
  223. $output = '';
  224. $header = array('', 'Status', 'Loader', 'File');
  225. $rows = array();
  226. $query = 'SELECT * FROM {tripal_bulk_loader} l '
  227. .'LEFT JOIN {node} n ON n.nid = l.nid '
  228. .'LEFT JOIN {tripal_bulk_loader_template} t ON t.template_id = cast(l.template_id as integer)';
  229. $resource = pager_query($query, $num_results_per_page, 0, NULL);
  230. while ($r = db_fetch_object($resource)) {
  231. $row = array(
  232. l($r->title, 'node/' . $r->nid),
  233. $r->job_status,
  234. $r->name,
  235. $r->file
  236. );
  237. $rows[] = $row;
  238. }
  239. $output .= theme('table', $header, $rows);
  240. return $output;
  241. }
  242. //////////////////////////////////////////////////////////////////////////////////////////////
  243. // Node Functions
  244. //////////////////////////////////////////////////////////////////////////////////////////////
  245. /**
  246. * Implements hook_node_info
  247. */
  248. function tripal_bulk_loader_node_info() {
  249. $nodes = array();
  250. $nodes['tripal_bulk_loader'] = array(
  251. 'name' => t('Bulk Loading Job'),
  252. 'module' => 'tripal_bulk_loader',
  253. 'description' => t('A bulk loader for inserting tab-delimited data into chado database'),
  254. 'has_title' => TRUE,
  255. 'has_body' => FALSE,
  256. 'locked' => TRUE
  257. );
  258. return $nodes;
  259. }
  260. /**
  261. * Implements node_form
  262. * Used to gather the extra details stored with a Bulk Loading Job Node
  263. */
  264. function tripal_bulk_loader_form($node, $form_state) {
  265. $form = array();
  266. if (isset($form_state['values'])) {
  267. $node = $form_state['values'] + (array)$node;
  268. $node = (object) $node;
  269. }
  270. $sql = "SELECT * FROM {tripal_bulk_loader_template}";
  271. $results = db_query($sql);
  272. $templates = array();
  273. while ($template = db_fetch_object ($results)) {
  274. $templates [$template->template_id] = $template->name;
  275. }
  276. if (!$templates) {
  277. $form['label'] = array(
  278. '#type' => 'item',
  279. '#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."),
  280. '#weight' => -10,
  281. );
  282. return $form;
  283. }
  284. $form['loader'] = array(
  285. '#type' => 'fieldset',
  286. '#title' => t('Basic Details'),
  287. );
  288. $form['loader']['loader_name'] = array(
  289. '#type' => 'textfield',
  290. '#title' => t('Loading Job Name'),
  291. '#weight' => -10,
  292. '#required' => TRUE,
  293. '#default_value' => $node->loader_name
  294. );
  295. $form['loader']['template_id'] = array(
  296. '#type' => 'select',
  297. '#title' => t('Template'),
  298. '#description' => t('Please specify a template for this loader'),
  299. '#options' => $templates,
  300. '#weight' => -9,
  301. '#required' => TRUE,
  302. '#default_value' => $node->template_id,
  303. );
  304. $form['loader']['file']= array(
  305. '#type' => 'textfield',
  306. '#title' => t('Data File'),
  307. '#description' => t('Please specify the data file to be loaded.'),
  308. '#weight' => -8,
  309. '#default_value' => $node->file
  310. );
  311. $form['loader']['has_header'] = array(
  312. '#type' => 'radios',
  313. '#title' => t('File has a Header'),
  314. '#options' => array( 1 => 'Yes', 2 => 'No'),
  315. '#weight' => -7,
  316. '#default_value' => $node->file_has_header,
  317. );
  318. return $form;
  319. }
  320. /**
  321. * Implements node_load
  322. */
  323. function tripal_bulk_loader_load($node) {
  324. $sql = "SELECT * FROM {tripal_bulk_loader} WHERE nid = %d";
  325. $node = db_fetch_object(db_query($sql, $node->nid));
  326. $node->title = 'Bulk Loading Job: ' . $node->loader_name;
  327. // Add the loader template
  328. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
  329. $results = db_fetch_object(db_query($sql, $node->template_id));
  330. $template = unserialize($results->template_array);
  331. $node->template = $results;
  332. $node->template->template_array = $template;
  333. // Add inserted records
  334. $sql = 'SELECT * FROM {tripal_bulk_loader_inserted} WHERE nid=%d';
  335. $resource = db_query($sql, $node->nid);
  336. while ($r = db_fetch_object($resource)) {
  337. $r->num_inserted = sizeof(preg_split('/,/', $r->ids_inserted));
  338. $node->inserted_records->{$r->table_inserted_into} = $r;
  339. }
  340. // Add exposed field list
  341. $node->exposed_fields = array();
  342. if ($template) {
  343. foreach ($template as $record_id => $record) {
  344. foreach ($record['fields'] as $field_id => $field) {
  345. if ($field['exposed']) {
  346. $node->exposed_fields[] = array(
  347. 'record_id' => $record_id,
  348. 'field_id' => $field_id,
  349. 'title' => $field['title'],
  350. );
  351. }
  352. }
  353. }
  354. if (empty($node->exposed_fields)) {
  355. $node->exposed_fields[] = array();
  356. }
  357. }
  358. // Add constants
  359. $sql = 'SELECT * FROM {tripal_bulk_loader_constants} WHERE nid=%d ORDER BY group_id, record_id, field_id';
  360. $resource = db_query($sql, $node->nid);
  361. while ($r = db_fetch_object($resource)) {
  362. $node->constants[$r->group_id][$r->record_id][$r->field_id] = array(
  363. 'constant_id' => $r->constant_id,
  364. 'group_id' => $r->group_id,
  365. 'chado_table' => $r->chado_table,
  366. 'chado_field' => $r->chado_field,
  367. 'record_id' => $r->record_id,
  368. 'field_id' => $r->field_id,
  369. 'value' => $r->value
  370. );
  371. }
  372. if (!$node->constants) {
  373. $node->constants[] = array();
  374. }
  375. return $node;
  376. }
  377. /**
  378. * Implements node_insert
  379. * Insert the data from the node form on Create content
  380. */
  381. function tripal_bulk_loader_insert($node) {
  382. // Insert into tripal_bulk_loader
  383. $sql = "INSERT INTO {tripal_bulk_loader} (nid, loader_name, template_id, file, file_has_header, job_status) VALUES (%d, '%s', %d, '%s', %d, '%s')";
  384. db_query($sql, $node->nid, $node->loader_name, $node->template_id, $node->file, $node->has_header, 'Initialized');
  385. // Update title
  386. $node->title =$node->loader_name;
  387. drupal_write_record('node', $node, 'nid');
  388. drupal_write_record('node_revision', $node, 'nid');
  389. drupal_set_message(t('After reviewing the details, please Submit this Job (by clicking the "Submit Job" button below). No data will be loaded until the submitted job is reached in the queue.'));
  390. }
  391. /**
  392. * Implements node_delete
  393. * Deletes the data when the delete button on the node form is clicked
  394. */
  395. function tripal_bulk_loader_delete($node) {
  396. $sql = "DELETE FROM {tripal_bulk_loader} WHERE nid = %d";
  397. db_query($sql, $node->nid);
  398. }
  399. /**
  400. * Implements node_update
  401. * Updates the data submitted by the node form on edit
  402. */
  403. function tripal_bulk_loader_update($node) {
  404. // Update tripal_bulk_loader
  405. $sql = "UPDATE {tripal_bulk_loader} SET nid = %d, loader_name = '%s', template_id = %d, file = '%s', file_has_header = '%s' WHERE nid = %d";
  406. db_query($sql, $node->nid, $node->loader_name, $node->template_id, $node->file, $node->has_header, $node->nid);
  407. // Add a job if the user want to load the data
  408. global $user;
  409. if ($node->job) {
  410. $job_args[0] =$node->loader_name;
  411. $job_args[1] = $node->template_id;
  412. $job_args[2] = $node->file;
  413. if (is_readable($node->file)) {
  414. $fname = preg_replace("/.*\/(.*)/", "$1", $node->file);
  415. tripal_add_job("Bulk Load: $fname", 'tripal_bulk_loader', 'tripal_bulk_loader_load_data', $job_args, $user->uid);
  416. }
  417. else {
  418. drupal_set_message(t("Can not open %file. Job not scheduled.", array('%file' => $node->file)));
  419. }
  420. }
  421. }
  422. ///////////////////////////////////////////////////////////
  423. /**
  424. * Preprocessor function for the tripal_bulk_loader template
  425. */
  426. function tripal_bulk_loader_preprocess_tripal_bulk_loader_template(&$variables) {
  427. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
  428. $template = db_fetch_object(db_query($sql, $variables['template_id']));
  429. $template->template_array = unserialize($template->template_array);
  430. $variables['template'] = $template;
  431. }
  432. /**
  433. * Implements hook_job_describe_args()
  434. * Specifically to make viewing past tripal jobs more readable for jobs registered by this module
  435. *
  436. * @params $callback
  437. * The callback passed into tripal_add_job()
  438. * @param $args
  439. * The arguements passed into tripal_add_job()
  440. * @return
  441. * An array where keys are the human readable headers describing each arguement
  442. * and the value is the aguement passed in after formatting
  443. */
  444. function tripal_bulk_loader_job_describe_args($callback, $args) {
  445. $new_args = array();
  446. if ($callback == 'tripal_bulk_loader_load_data') {
  447. //1st arg is the nid for a bulk loader node
  448. $node = node_load($args[0]);
  449. $new_args['Bulk Loading Job'] = l($node->title, 'node/' . $args[0]);
  450. return $new_args;
  451. }
  452. }
  453. /**
  454. * Implements hook_coder_ignore().
  455. * Defines the path to the file (tripal_bulk_loader.coder_ignores.txt) where ignore rules for coder are stored
  456. */
  457. function tripal_bulk_loader_coder_ignore() {
  458. return array(
  459. 'path' => drupal_get_path('module', 'tripal_bulk_loader'),
  460. 'line prefix' => drupal_get_path('module', 'tripal_bulk_loader'),
  461. );
  462. }