tripal_bulk_loader.chado_node.inc 12 KB

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