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. $nodes[$record->nid]->inserted_records->{$record->table_inserted_into} = $record;
  171. }
  172. // Add constants
  173. // Add fields from tripal_bulk_loader_constants
  174. $result = db_query('SELECT tblc.* FROM {tripal_bulk_loader_constants} tblc '
  175. . 'WHERE tblc.nid IN (:nids) '
  176. . 'ORDER BY group_id, record_id, field_id',
  177. array(':nids' => array_keys($nodes))
  178. );
  179. foreach ($result as $record) {
  180. $nodes[$record->nid]->constants[$record->group_id][$record->record_id][$record->field_id] = array(
  181. 'constant_id' => $record->constant_id,
  182. 'group_id' => $record->group_id,
  183. 'chado_table' => $record->chado_table,
  184. 'chado_field' => $record->chado_field,
  185. 'record_id' => $record->record_id,
  186. 'field_id' => $record->field_id,
  187. 'value' => $record->value
  188. );
  189. }
  190. }
  191. /**
  192. * Implements hook_node_presave().
  193. * Acts on all node types.
  194. *
  195. * @ingroup tripal_bulk_loader
  196. */
  197. function tripal_bulk_loader_node_presave($node) {
  198. // We need to set the title using loader details before the node is saved
  199. // which has already been done by the time hook_insert is called
  200. switch ($node->type) {
  201. case 'tripal_bulk_loader':
  202. $node->title = 'Bulk Loading Job: ' . $node->loader_name;
  203. break;
  204. }
  205. }
  206. /**
  207. * Implements hook_insert().
  208. * Insert the data from the node form on Create content
  209. *
  210. * D7 Changes: seems to need db_insert; not recommended to change $node
  211. *
  212. * @ingroup tripal_bulk_loader
  213. */
  214. function tripal_bulk_loader_insert($node) {
  215. db_insert('tripal_bulk_loader')->fields(array(
  216. 'nid' => $node->nid,
  217. 'loader_name' => trim($node->loader_name),
  218. 'template_id' => $node->template_id,
  219. 'file' => trim($node->file),
  220. 'file_has_header' => $node->has_header,
  221. 'job_status' => 'Initialized',
  222. 'keep_track_inserted' => $node->keep_track_inserted
  223. ))->execute();
  224. 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.'));
  225. }
  226. /**
  227. * Implements hook_delete().
  228. * Deletes the data when the delete button on the node form is clicked
  229. *
  230. * @ingroup tripal_bulk_loader
  231. */
  232. function tripal_bulk_loader_delete($node) {
  233. $tables = array();
  234. $tables[] = 'tripal_bulk_loader';
  235. $tables[] = 'tripal_bulk_loader_constants';
  236. $tables[] = 'tripal_bulk_loader_inserted';
  237. foreach($tables as $table) {
  238. db_delete($table)
  239. ->condition('nid',$node->nid)
  240. ->execute();
  241. }
  242. }
  243. /**
  244. * Implements hook_update().
  245. * Updates the data submitted by the node form on edit
  246. *
  247. * @ingroup tripal_bulk_loader
  248. */
  249. function tripal_bulk_loader_update($node) {
  250. // Update tripal_bulk_loader
  251. db_update('tripal_bulk_loader')->fields(array(
  252. 'nid' => $node->nid,
  253. 'loader_name' => trim($node->loader_name),
  254. 'template_id' => $node->template_id,
  255. 'file' => trim($node->file),
  256. 'file_has_header' => $node->has_header,
  257. 'keep_track_inserted' => $node->keep_track_inserted
  258. ))->condition('nid',$node->nid)->execute();
  259. // Add a job if the user want to load the data
  260. /**
  261. No job checkbox in the form
  262. global $user;
  263. if ($node->job) {
  264. $job_args[0] =$node->loader_name;
  265. $job_args[1] = $node->template_id;
  266. $job_args[2] = $node->file;
  267. if (is_readable($node->file)) {
  268. $fname = preg_replace("/.*\/(.*)/", "$1", $node->file);
  269. tripal_add_job("Bulk Load: $fname", 'tripal_bulk_loader', 'tripal_bulk_loader_load_data', $job_args, $user->uid);
  270. }
  271. else {
  272. drupal_set_message(t("Can not open %file. Job not scheduled.", array('%file' => $node->file)));
  273. }
  274. }
  275. */
  276. }
  277. /**
  278. * Implement hook_access().
  279. *
  280. * This hook allows node modules to limit access to the node types they define.
  281. *
  282. * @param $op
  283. * The operation to be performed
  284. *
  285. * @param $node
  286. * The node on which the operation is to be performed, or, if it does not yet exist, the
  287. * type of node to be created
  288. *
  289. * @param $account
  290. * A user object representing the user for whom the operation is to be performed
  291. *
  292. * @return
  293. * If the permission for the specified operation is not set then return FALSE. If the
  294. * permission is set then return NULL as this allows other modules to disable
  295. * access. The only exception is when the $op == 'create'. We will always
  296. * return TRUE if the permission is set.
  297. * @ingroup tripal_bulk_loader
  298. */
  299. function tripal_bulk_loader_node_access($node, $op, $account) {
  300. $node_type = $node;
  301. if (is_object($node)) {
  302. $node_type = $node->type;
  303. }
  304. if($node_type == 'tripal_bulk_loader') {
  305. if ($op == 'create') {
  306. if (!user_access('create tripal_bulk_loader', $account)) {
  307. return NODE_ACCESS_DENY;
  308. }
  309. return NODE_ACCESS_ALLOW;
  310. }
  311. if ($op == 'update') {
  312. if (!user_access('edit tripal_bulk_loader', $account)) {
  313. return NODE_ACCESS_DENY;
  314. }
  315. }
  316. if ($op == 'delete') {
  317. if (!user_access('delete tripal_bulk_loader', $account)) {
  318. return NODE_ACCESS_DENY;
  319. }
  320. }
  321. if ($op == 'view') {
  322. if (!user_access('access tripal_bulk_loader', $account)) {
  323. return NODE_ACCESS_DENY;
  324. }
  325. }
  326. return NODE_ACCESS_IGNORE;
  327. }
  328. }
  329. /**
  330. * Implements hook_node_view().
  331. * Acts on all content types.
  332. *
  333. * @ingroup tripal_feature
  334. */
  335. function tripal_bulk_loader_node_view($node, $view_mode, $langcode) {
  336. switch ($node->type) {
  337. case 'tripal_bulk_loader':
  338. // Show feature browser and counts
  339. if ($view_mode == 'full') {
  340. // we want to use the Tripal generic node template
  341. $node->content['#tripal_generic_node_template'] = TRUE;
  342. $node->content['tripal_bulk_loader_base'] = array(
  343. '#theme' => 'tripal_bulk_loader_base',
  344. '#node' => $node,
  345. '#tripal_toc_id' => 'base',
  346. '#tripal_toc_title' => 'Overview',
  347. '#tripal_template_show' => FALSE,
  348. '#weight' => -100,
  349. );
  350. $node->content['tripal_bulk_loader_fields'] = array(
  351. '#theme' => 'tripal_bulk_loader_fields',
  352. '#node' => $node,
  353. '#tripal_toc_id' => 'fields',
  354. '#tripal_toc_title' => 'Data Fields',
  355. '#tripal_template_show' => FALSE,
  356. );
  357. }
  358. if ($view_mode == 'teaser') {
  359. $node->content['tripal_bulk_loader_teaser'] = array(
  360. '#theme' => 'tripal_bulk_loader_teaser',
  361. '#node' => $node,
  362. );
  363. }
  364. break;
  365. }
  366. }

Related topics