2012111901.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. // Only admin users can run this upgrade
  3. if ( !elgg_is_admin_logged_in() ) {
  4. return;
  5. }
  6. $file_version = elgg_get_upgrade_file_version(basename(__FILE__));
  7. $upgrade_version = elgg_get_plugin_setting('upgrade_version', 'tidypics');
  8. if ( $upgrade_version and $file_version <= $upgrade_version) {
  9. return;
  10. }
  11. $db_prefix = elgg_get_config('dbprefix');
  12. $ignore_access = elgg_set_ignore_access(TRUE);
  13. // Count tidypics_batch entities that need to correct the access_id
  14. $options = array(
  15. 'type' => 'object',
  16. 'subtype' => 'tidypics_batch',
  17. 'limit' => 0,
  18. 'count' => TRUE,
  19. 'joins' => array("JOIN {$db_prefix}entities e2 ON e2.guid = e.container_guid"),
  20. 'wheres' => array("e.access_id <> e2.access_id"),
  21. );
  22. $tidypics_batch = elgg_get_entities($options);
  23. // If no entities found set upgrade as runned
  24. if ( !$tidypics_batch ) {
  25. return elgg_set_plugin_setting('upgrade_version', $file_version, 'tidypics');
  26. }
  27. // Correct the access_id of the tidypics_batch entities
  28. if ( $tidypics_batch ) {
  29. $options['count'] = FALSE;
  30. $batch = new ElggBatch('elgg_get_entities', $options, 'tidypics_2012111901');
  31. // Correct the acces_id from river itens
  32. $tidypics_batch_river = elgg_get_river(array('object_guids' => $batch->callbackResult, 'action_types' => 'create', 'types' => 'object', 'subtypes' => 'tidypics_batch', 'limit' => 0));
  33. tidypics_adjust_river_access_id($tidypics_batch_river);
  34. error_log("Tidypics batches upgrade (2012111901) succeeded");
  35. }
  36. elgg_set_ignore_access($ignore_access);
  37. // Set upgrade as runned
  38. return elgg_set_plugin_setting('upgrade_version', $file_version, 'tidypics');
  39. /**
  40. * Change the tidypics_batch's access_id to container's access_id (album object)
  41. *
  42. * @param ElggObject $entity
  43. * @param string $getter
  44. * @param array $options
  45. */
  46. function tidypics_2012111901($entity, $getter, $options) {
  47. $album = $entity->getContainerEntity();
  48. if ( $guid = tidypics_adjust_batch_access_id($entity, $album) ) {
  49. return $guid;
  50. }
  51. }
  52. /**
  53. * Change the tidypics_batch's access_id
  54. *
  55. * @param ElggObject $entity
  56. * @param ElggObject $album
  57. * @return void
  58. */
  59. function tidypics_adjust_batch_access_id(ElggObject $entity = NULL, ElggObject $album = NULL) {
  60. if ( !elgg_instanceof($entity, 'object', 'tidypics_batch') or !elgg_instanceof($album, 'object', 'album') ) {
  61. return FALSE;
  62. }
  63. if ( $entity->access_id != $album->access_id ) {
  64. $entity->access_id = $album->access_id;
  65. return $entity->save();
  66. }
  67. }
  68. /**
  69. * Change the river iten's acces_id to album acces_id
  70. *
  71. * @param Array $river_itens
  72. * @return void
  73. */
  74. function tidypics_adjust_river_access_id(array $river_itens = NULL) {
  75. if ( !$river_itens or !count($river_itens) ) {
  76. return;
  77. }
  78. foreach ($river_itens as $item) {
  79. $object = $item->getObjectEntity();
  80. $access_id = $object->access_id;
  81. if ( $item->access_id != $access_id ) {
  82. update_river_access_by_object($object->guid, $access_id);
  83. }
  84. }
  85. }