tripal_bulk_loader.chado_node.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. <?php
  2. /**
  3. * @file
  4. * Tripal Bulk Loader Node functionality (jobs).
  5. *
  6. * @ingroup tripal_bulk_loader
  7. */
  8. /**
  9. * Implements hook_node_info().
  10. *
  11. * @ingroup tripal_bulk_loader
  12. */
  13. function tripal_bulk_loader_node_info() {
  14. $nodes = array();
  15. $nodes['tripal_bulk_loader'] = array(
  16. 'name' => t('Bulk Loading Job'),
  17. 'base' => 'tripal_bulk_loader',
  18. 'description' => t('A bulk loader for inserting tab-delimited data into chado database'),
  19. 'has_title' => TRUE,
  20. 'has_body' => FALSE,
  21. 'locked' => TRUE
  22. );
  23. return $nodes;
  24. }
  25. /**
  26. * Implements hook_form().
  27. * Used to gather the extra details stored with a Bulk Loading Job Node
  28. *
  29. * @ingroup tripal_bulk_loader
  30. */
  31. function tripal_bulk_loader_form($node, $form_state) {
  32. $form = array();
  33. if (isset($form_state['values'])) {
  34. $node = $form_state['values'] + (array)$node;
  35. $node = (object) $node;
  36. }
  37. $results = db_select('tripal_bulk_loader_template', 't')
  38. ->fields('t', array('template_id','name'))
  39. ->execute();
  40. $templates = array();
  41. foreach ($results as $template) {
  42. $templates [$template->template_id] = $template->name;
  43. }
  44. if (!$templates) {
  45. $form['label'] = array(
  46. '#type' => 'item',
  47. '#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."),
  48. '#weight' => -10,
  49. );
  50. return $form;
  51. }
  52. $form['loader'] = array(
  53. '#type' => 'fieldset',
  54. '#title' => t('Basic Details'),
  55. );
  56. $form['loader']['loader_name'] = array(
  57. '#type' => 'textfield',
  58. '#title' => t('Loading Job Name'),
  59. '#weight' => -10,
  60. '#required' => TRUE,
  61. '#default_value' => (isset($node->loader_name)) ? $node->loader_name : ''
  62. );
  63. $form['loader']['template_id'] = array(
  64. '#type' => 'select',
  65. '#title' => t('Template'),
  66. '#description' => t('Please specify a template for this loader'),
  67. '#options' => $templates,
  68. '#weight' => -9,
  69. '#required' => TRUE,
  70. '#default_value' => (isset($node->template_id)) ? $node->template_id : current($templates),
  71. );
  72. $form['loader']['file']= array(
  73. '#type' => 'textfield',
  74. '#title' => t('Data File'),
  75. '#description' => t('Please specify the data file to be loaded. This must be a tab-delimited text file with UNIX line endings.'),
  76. '#weight' => -8,
  77. '#default_value' => (isset($node->file)) ? $node->file : '',
  78. '#maxlength' => 1024,
  79. );
  80. $form['loader']['has_header'] = array(
  81. '#type' => 'radios',
  82. '#title' => t('File has a Header'),
  83. '#options' => array( 1 => 'Yes', 2 => 'No'),
  84. '#weight' => -7,
  85. '#default_value' => (isset($node->file_has_header)) ? $node->file_has_header : 1,
  86. );
  87. $form['loader']['keep_track_inserted'] = array(
  88. '#type' => 'radios',
  89. '#title' => t('Keep track of inserted record IDs'),
  90. '#description' => t('This enables the ability to revert an entire loading job even if '
  91. .'it completed successfully. Furthermore, it displays the number of records '
  92. .'successfully inserted into each table.'),
  93. '#options' => array( 1 => 'Yes', 0 => 'No'),
  94. '#weight' => -7,
  95. '#default_value' => (isset($node->keep_track_inserted)) ? $node->keep_track_inserted : variable_get('tripal_bulk_loader_keep_track_inserted', 0),
  96. );
  97. return $form;
  98. }
  99. /**
  100. * Implements hook_load().
  101. *
  102. * D7 Changes: now loads all $nodes at once so need to add loops
  103. *
  104. * @ingroup tripal_bulk_loader
  105. */
  106. function tripal_bulk_loader_load($nodes) {
  107. // Loading Job Details
  108. // Add fields from the tripal_bulk_loader
  109. $result = db_select('tripal_bulk_loader', 'tbl')
  110. ->fields('tbl')
  111. ->condition('nid',array_keys($nodes),'IN')
  112. ->execute();
  113. foreach ($result as $record) {
  114. $nodes[$record->nid]->loader_name = $record->loader_name;
  115. $nodes[$record->nid]->template_id = $record->template_id;
  116. $nodes[$record->nid]->file = $record->file;
  117. $nodes[$record->nid]->job_id = $record->job_id;
  118. $nodes[$record->nid]->job_status = $record->job_status;
  119. $nodes[$record->nid]->file_has_header = $record->file_has_header;
  120. $nodes[$record->nid]->keep_track_inserted = $record->keep_track_inserted;
  121. $nodes[$record->nid]->exposed_fields = array();
  122. $nodes[$record->nid]->constants = array();
  123. }
  124. // Job Details
  125. // Add fields from tripal_jobs
  126. $result = db_query('SELECT tbl.nid, tj.* FROM {tripal_jobs} tj '
  127. . 'LEFT JOIN {tripal_bulk_loader} tbl ON tbl.job_id=tj.job_id '
  128. . 'WHERE tbl.nid IN (:nids)',
  129. array(':nids' => array_keys($nodes))
  130. );
  131. foreach ($result as $record) {
  132. $nodes[$record->nid]->job = $record;
  133. }
  134. // Add the Loader Template
  135. // Add fields from tripal_bulk_loader_template
  136. $result = db_query('SELECT tbl.nid, tblt.* FROM {tripal_bulk_loader_template} tblt '
  137. . 'LEFT JOIN {tripal_bulk_loader} tbl ON tbl.template_id=tblt.template_id '
  138. . 'WHERE tbl.nid IN (:nids)',
  139. array(':nids' => array_keys($nodes))
  140. );
  141. foreach ($result as $dbrecord) {
  142. $nodes[$dbrecord->nid]->template = $dbrecord;
  143. $nodes[$dbrecord->nid]->template->template_array = unserialize($dbrecord->template_array);
  144. // Add exposed field list
  145. $template = $nodes[$dbrecord->nid]->template->template_array;
  146. $nodes[$dbrecord->nid]->exposed_fields = array();
  147. if ($template) {
  148. foreach ($template as $record_id => $record) {
  149. foreach ($record['fields'] as $field_id => $field) {
  150. if (isset($field['exposed'])) {
  151. if ($field['exposed']) {
  152. $nodes[$dbrecord->nid]->exposed_fields[] = array(
  153. 'record_id' => $record_id,
  154. 'field_id' => $field_id,
  155. 'title' => $field['title'],
  156. );
  157. }
  158. }
  159. }
  160. }
  161. }
  162. }
  163. // Add inserted records
  164. // Add fields from tripal_bulk_loader_inserted
  165. $result = db_query('SELECT tbli.* FROM {tripal_bulk_loader_inserted} tbli '
  166. . 'WHERE tbli.nid IN (:nids)',
  167. array(':nids' => array_keys($nodes))
  168. );
  169. foreach ($result as $record) {
  170. $record->num_inserted = sizeof(preg_split('/,/', $record->ids_inserted));
  171. $nodes[$record->nid]->inserted_records->{$record->table_inserted_into} = $record;
  172. }
  173. // Add constants
  174. // Add fields from tripal_bulk_loader_constants
  175. $result = db_query('SELECT tblc.* FROM {tripal_bulk_loader_constants} tblc '
  176. . 'WHERE tblc.nid IN (:nids) '
  177. . 'ORDER BY group_id, record_id, field_id',
  178. array(':nids' => array_keys($nodes))
  179. );
  180. foreach ($result as $record) {
  181. $nodes[$record->nid]->constants[$record->group_id][$record->record_id][$record->field_id] = array(
  182. 'constant_id' => $record->constant_id,
  183. 'group_id' => $record->group_id,
  184. 'chado_table' => $record->chado_table,
  185. 'chado_field' => $record->chado_field,
  186. 'record_id' => $record->record_id,
  187. 'field_id' => $record->field_id,
  188. 'value' => $record->value
  189. );
  190. }
  191. }
  192. /**
  193. * Implements hook_node_presave().
  194. * Acts on all node types.
  195. *
  196. * @ingroup tripal_bulk_loader
  197. */
  198. function tripal_bulk_loader_node_presave($node) {
  199. // We need to set the title using loader details before the node is saved
  200. // which has already been done by the time hook_insert is called
  201. switch ($node->type) {
  202. case 'tripal_bulk_loader':
  203. $node->title = 'Bulk Loading Job: ' . $node->loader_name;
  204. break;
  205. }
  206. }
  207. /**
  208. * Implements hook_insert().
  209. * Insert the data from the node form on Create content
  210. *
  211. * D7 Changes: seems to need db_insert; not recommended to change $node
  212. *
  213. * @ingroup tripal_bulk_loader
  214. */
  215. function tripal_bulk_loader_insert($node) {
  216. db_insert('tripal_bulk_loader')->fields(array(
  217. 'nid' => $node->nid,
  218. 'loader_name' => $node->loader_name,
  219. 'template_id' => $node->template_id,
  220. 'file' => $node->file,
  221. 'file_has_header' => $node->has_header,
  222. 'job_status' => 'Initialized',
  223. 'keep_track_inserted' => $node->keep_track_inserted
  224. ))->execute();
  225. 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.'));
  226. }
  227. /**
  228. * Implements hook_delete().
  229. * Deletes the data when the delete button on the node form is clicked
  230. *
  231. * @ingroup tripal_bulk_loader
  232. */
  233. function tripal_bulk_loader_delete($node) {
  234. $tables = array();
  235. $tables[] = 'tripal_bulk_loader';
  236. $tables[] = 'tripal_bulk_loader_constants';
  237. $tables[] = 'tripal_bulk_loader_inserted';
  238. foreach($tables as $table) {
  239. db_delete($table)
  240. ->condition('nid',$node->nid)
  241. ->execute();
  242. }
  243. }
  244. /**
  245. * Implements hook_update().
  246. * Updates the data submitted by the node form on edit
  247. *
  248. * @ingroup tripal_bulk_loader
  249. */
  250. function tripal_bulk_loader_update($node) {
  251. // Update tripal_bulk_loader
  252. db_update('tripal_bulk_loader')->fields(array(
  253. 'nid' => $node->nid,
  254. 'loader_name' => $node->loader_name,
  255. 'template_id' => $node->template_id,
  256. 'file' => $node->file,
  257. 'file_has_header' => $node->has_header,
  258. 'keep_track_inserted' => $node->keep_track_inserted
  259. ))->condition('nid',$node->nid)->execute();
  260. // Add a job if the user want to load the data
  261. /**
  262. No job checkbox in the form
  263. global $user;
  264. if ($node->job) {
  265. $job_args[0] =$node->loader_name;
  266. $job_args[1] = $node->template_id;
  267. $job_args[2] = $node->file;
  268. if (is_readable($node->file)) {
  269. $fname = preg_replace("/.*\/(.*)/", "$1", $node->file);
  270. tripal_add_job("Bulk Load: $fname", 'tripal_bulk_loader', 'tripal_bulk_loader_load_data', $job_args, $user->uid);
  271. }
  272. else {
  273. drupal_set_message(t("Can not open %file. Job not scheduled.", array('%file' => $node->file)));
  274. }
  275. }
  276. */
  277. }
  278. /**
  279. * Implement hook_access().
  280. *
  281. * This hook allows node modules to limit access to the node types they define.
  282. *
  283. * @param $op
  284. * The operation to be performed
  285. *
  286. * @param $node
  287. * The node on which the operation is to be performed, or, if it does not yet exist, the
  288. * type of node to be created
  289. *
  290. * @param $account
  291. * A user object representing the user for whom the operation is to be performed
  292. *
  293. * @return
  294. * If the permission for the specified operation is not set then return FALSE. If the
  295. * permission is set then return NULL as this allows other modules to disable
  296. * access. The only exception is when the $op == 'create'. We will always
  297. * return TRUE if the permission is set.
  298. * @ingroup tripal_bulk_loader
  299. */
  300. function tripal_bulk_loader_node_access($node, $op, $account) {
  301. if ($op == 'create') {
  302. if (!user_access('create tripal_bulk_loader', $account)) {
  303. return NODE_ACCESS_DENY;
  304. }
  305. return NODE_ACCESS_ALLOW;
  306. }
  307. if ($op == 'update') {
  308. if (!user_access('edit tripal_bulk_loader', $account)) {
  309. return NODE_ACCESS_DENY;
  310. }
  311. return NODE_ACCESS_ALLOW;
  312. }
  313. if ($op == 'delete') {
  314. if (!user_access('delete tripal_bulk_loader', $account)) {
  315. return NODE_ACCESS_DENY;
  316. }
  317. return NODE_ACCESS_ALLOW;
  318. }
  319. if ($op == 'view') {
  320. if (!user_access('access tripal_bulk_loader', $account)) {
  321. return NODE_ACCESS_DENY;
  322. }
  323. return NODE_ACCESS_ALLOW;
  324. }
  325. return NODE_ACCESS_IGNORE;
  326. }