tripal_bulk_loader.chado_node.inc

  1. 2.x tripal_bulk_loader/includes/tripal_bulk_loader.chado_node.inc
  2. 3.x tripal_bulk_loader/includes/tripal_bulk_loader.chado_node.inc

Tripal Bulk Loader Node functionality (jobs).

File

tripal_bulk_loader/includes/tripal_bulk_loader.chado_node.inc
View source
  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. '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 = array();
  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', array('template_id','name'))
  38. ->execute();
  39. $templates = array();
  40. foreach ($results as $template) {
  41. $templates [$template->template_id] = $template->name;
  42. }
  43. if (!$templates) {
  44. $form['label'] = array(
  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'] = array(
  52. '#type' => 'fieldset',
  53. '#title' => t('Basic Details'),
  54. );
  55. $form['loader']['loader_name'] = array(
  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'] = array(
  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']= array(
  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'] = array(
  80. '#type' => 'radios',
  81. '#title' => t('File has a Header'),
  82. '#options' => array( 1 => 'Yes', 2 => 'No'),
  83. '#weight' => -7,
  84. '#default_value' => (isset($node->file_has_header)) ? $node->file_has_header : 1,
  85. );
  86. $form['loader']['keep_track_inserted'] = array(
  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' => array( 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 = array();
  121. $nodes[$record->nid]->constants = array();
  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. array(':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. array(':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 = array();
  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[] = array(
  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. array(':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. array(':nids' => array_keys($nodes))
  181. );
  182. foreach ($result as $record) {
  183. $nodes[$record->nid]->constants[$record->group_id][$record->record_id][$record->field_id] = array(
  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(array(
  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 = array();
  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(array(
  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 exist, the
  290. * type of node to be created
  291. *
  292. * @param $account
  293. * A user object representing the user for whom the operation is to be performed
  294. *
  295. * @return
  296. * If the permission for the specified operation is not set then return FALSE. If the
  297. * permission is set then return NULL as this allows other modules to disable
  298. * access. The only exception is when the $op == 'create'. We will always
  299. * return TRUE if the permission is set.
  300. * @ingroup tripal_bulk_loader
  301. */
  302. function tripal_bulk_loader_node_access($node, $op, $account) {
  303. $node_type = $node;
  304. if (is_object($node)) {
  305. $node_type = $node->type;
  306. }
  307. if($node_type == 'tripal_bulk_loader') {
  308. if ($op == 'create') {
  309. if (!user_access('create tripal_bulk_loader', $account)) {
  310. return NODE_ACCESS_DENY;
  311. }
  312. return NODE_ACCESS_ALLOW;
  313. }
  314. if ($op == 'update') {
  315. if (!user_access('edit tripal_bulk_loader', $account)) {
  316. return NODE_ACCESS_DENY;
  317. }
  318. }
  319. if ($op == 'delete') {
  320. if (!user_access('delete tripal_bulk_loader', $account)) {
  321. return NODE_ACCESS_DENY;
  322. }
  323. }
  324. if ($op == 'view') {
  325. if (!user_access('access tripal_bulk_loader', $account)) {
  326. return NODE_ACCESS_DENY;
  327. }
  328. }
  329. return NODE_ACCESS_IGNORE;
  330. }
  331. }
  332. /**
  333. * Implements hook_node_view().
  334. * Acts on all content types.
  335. *
  336. * @ingroup tripal_feature
  337. */
  338. function tripal_bulk_loader_node_view($node, $view_mode, $langcode) {
  339. switch ($node->type) {
  340. case 'tripal_bulk_loader':
  341. // Show feature browser and counts
  342. if ($view_mode == 'full') {
  343. // we want to use the Tripal generic node template
  344. $node->content['#tripal_generic_node_template'] = TRUE;
  345. $node->content['tripal_bulk_loader_base'] = array(
  346. '#theme' => 'tripal_bulk_loader_base',
  347. '#node' => $node,
  348. '#tripal_toc_id' => 'base',
  349. '#tripal_toc_title' => 'Overview',
  350. '#tripal_template_show' => FALSE,
  351. '#weight' => -100,
  352. );
  353. $node->content['tripal_bulk_loader_fields'] = array(
  354. '#theme' => 'tripal_bulk_loader_fields',
  355. '#node' => $node,
  356. '#tripal_toc_id' => 'fields',
  357. '#tripal_toc_title' => 'Data Fields',
  358. '#tripal_template_show' => FALSE,
  359. );
  360. }
  361. if ($view_mode == 'teaser') {
  362. $node->content['tripal_bulk_loader_teaser'] = array(
  363. '#theme' => 'tripal_bulk_loader_teaser',
  364. '#node' => $node,
  365. );
  366. }
  367. break;
  368. }
  369. }

Related topics