events.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. * All event handlers are bundled here
  4. */
  5. /**
  6. * Listen to different events on objects (create/update)
  7. *
  8. * @param string $event the name of the event
  9. * @param string $type the type of the event
  10. * @param ElggObject $object supplied object
  11. *
  12. * @return void
  13. */
  14. function file_tools_object_handler($event, $type, $object) {
  15. if (!empty($object) && elgg_instanceof($object, "object", "file")) {
  16. $folder_guid = (int) get_input("folder_guid", 0);
  17. if (!empty($folder_guid)) {
  18. if ($folder = get_entity($folder_guid)) {
  19. if (!elgg_instanceof($folder, "object", FILE_TOOLS_SUBTYPE)) {
  20. unset($folder_guid);
  21. }
  22. } else {
  23. unset($folder_guid);
  24. }
  25. }
  26. // remove old relationships
  27. remove_entity_relationships($object->getGUID(), FILE_TOOLS_RELATIONSHIP, true);
  28. if (!empty($folder_guid)) {
  29. add_entity_relationship($folder_guid, FILE_TOOLS_RELATIONSHIP, $object->getGUID());
  30. }
  31. }
  32. }
  33. /**
  34. * Listen to delete event on objects
  35. *
  36. * @param string $event the name of the event
  37. * @param string $type the type of the event
  38. * @param ElggObject $object supplied object
  39. *
  40. * @return void
  41. */
  42. function file_tools_object_handler_delete($event, $type, $object) {
  43. static $delete_files;
  44. if (!empty($object) && elgg_instanceof($object, "object", FILE_TOOLS_SUBTYPE)) {
  45. // find subfolders
  46. $options = array(
  47. "type" => "object",
  48. "subtype" => FILE_TOOLS_SUBTYPE,
  49. "container_guid" => $object->getContainerGUID(),
  50. "limit" => false,
  51. "metadata_name_value_pairs" => array(
  52. "name" => "parent_guid",
  53. "value" => $object->getGUID()
  54. ),
  55. "wheres" => array("(e.guid <> " . $object->getGUID() . ")") // prevent deadloops
  56. );
  57. if ($subfolders = elgg_get_entities_from_metadata($options)) {
  58. // delete subfolders
  59. foreach ($subfolders as $subfolder) {
  60. $subfolder->delete();
  61. }
  62. }
  63. // fill the static, to delete files in a folder
  64. if (!isset($delete_files)) {
  65. $delete_files = false;
  66. if (get_input("files") == "yes") {
  67. $delete_files = true;
  68. }
  69. }
  70. // should we remove files?
  71. if ($delete_files) {
  72. // find file in this folder
  73. $options = array(
  74. "type" => "object",
  75. "subtype" => "file",
  76. "container_guid" => $object->getContainerGUID(),
  77. "limit" => false,
  78. "relationship" => FILE_TOOLS_RELATIONSHIP,
  79. "relationship_guid" => $object->getGUID()
  80. );
  81. if ($files = elgg_get_entities_from_relationship($options)) {
  82. // delete files in folder
  83. foreach ($files as $file) {
  84. $file->delete();
  85. }
  86. }
  87. }
  88. }
  89. }