tripal.bulk_update.inc 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. $i = 0;
  27. //Pull items out one at a time.
  28. while($entity = $queue->claimItem()) {
  29. $arg = tripal_load_entity('TripalEntity', [$entity->data->entity_id], $bundle_tokens);
  30. if ($type == 'alias') {
  31. if (!empty($arg)) {
  32. if(is_array($arg)){
  33. $ent = reset($arg);
  34. }
  35. // Get the entity controller and clear the cache if requested (default).
  36. $ec = entity_get_controller('TripalEntity');
  37. $ec->setAlias($ent, $update);
  38. }
  39. }
  40. elseif ($type == 'title') {
  41. if (!empty($arg)) {
  42. if(is_array($arg)){
  43. $ent = reset($arg);
  44. }
  45. $ec = entity_get_controller('TripalEntity');
  46. $ec->setTitle($ent, $update);
  47. }
  48. }
  49. $i++;
  50. // Check if 50 items have been updated, if so print message.
  51. if ($i < $num_entities){
  52. print $i."/".$num_entities." entities have been updated.\r";
  53. }
  54. //Good, we succeeded. Delete the item as it is no longer needed.
  55. $queue->deleteItem($entity);
  56. }
  57. }