tripal.bulk_update.inc 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. /**
  3. * Updates all existing url aliases for an entity.
  4. *
  5. * @param $bundle_id
  6. * @param $update
  7. * @param $type
  8. */
  9. function tripal_update_all($bundle_id, $update, $type) {
  10. // Load all the entity_ids.
  11. $entity_table = 'chado_'.$bundle_id;
  12. $entities = db_select($entity_table, 'e')
  13. ->fields('e', array('entity_id'))
  14. ->execute()->fetchAll();
  15. $num_entities = count($entities);
  16. // Parse the $update variable for tokens and load those tokens.
  17. preg_match_all("/\[[^\]]*\]/", $update, $bundle_tokens);
  18. // Get the queue so we can add to it. Use a
  19. // descriptive name. It's ok if it doesn't exist yet.
  20. $queue = DrupalQueue::get('entityQueue');
  21. // Push all the items into the queue, one at a time.
  22. // You can push any data in with (arrays, objects, etc).
  23. foreach ($entities as $entity) {
  24. $queue->createItem($entity);
  25. }
  26. $fields = array();
  27. foreach ($bundle_tokens as $bundle_token) {
  28. foreach ($bundle_token as $token) {
  29. $string = str_replace(array('[', ']'), '', $token);
  30. $field_array = field_info_field($string);
  31. $fields[] = $field_array['id'];
  32. }
  33. }
  34. $i = 0;
  35. // Pull items out one at a time.
  36. while ($entity = $queue->claimItem()) {
  37. $arg = tripal_load_entity('TripalEntity', [$entity->data->entity_id], FALSE, $fields);
  38. if ($type == 'alias') {
  39. if (!empty($arg)) {
  40. if (is_array($arg)) {
  41. $ent = reset($arg);
  42. }
  43. // Get the entity controller and clear the cache if requested (default).
  44. $ec = entity_get_controller('TripalEntity');
  45. $ec->setAlias($ent, $update);
  46. }
  47. }
  48. elseif ($type == 'title') {
  49. if (!empty($arg)) {
  50. if (is_array($arg)) {
  51. $ent = reset($arg);
  52. }
  53. $ec = entity_get_controller('TripalEntity');
  54. $ec->setTitle($ent, $update);
  55. }
  56. }
  57. $i++;
  58. // Check if 50 items have been updated, if so print message.
  59. if ($i < $num_entities) {
  60. print $i . "/" . $num_entities . " entities have been updated.\r";
  61. }
  62. // Good, we succeeded. Delete the item as it is no longer needed.
  63. $queue->deleteItem($entity);
  64. }
  65. }