gallery_field.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. * @param string $value Value of model where stored image ids
  4. * @return array Array of file ids
  5. */
  6. function gallery_field_image_ids_from_value($value)
  7. {
  8. if(strlen($value) == 0 || intval($value) == 0)
  9. {
  10. return array();
  11. }
  12. $return_value = array();
  13. $value_arr = explode(",", $value);
  14. foreach($value_arr as $value_item)
  15. {
  16. $value_item_int = intval($value_item);
  17. if($value_item_int > 0)
  18. {
  19. $return_value[] = $value_item_int;
  20. }
  21. }
  22. return $return_value;
  23. }
  24. function gallery_field_delete_images()
  25. {
  26. $entity_id = $_POST['entity_id'];
  27. $field = $_POST['field'];
  28. $delete_images_list = $_POST['images'];
  29. $delete_ids = explode(",", $delete_images_list);
  30. $entity = get_entity($entity_id);
  31. if(false == $entity->canEdit())
  32. {
  33. throw new Exception("can`t edit");
  34. }
  35. $current_image_ids = gallery_field_image_ids_from_value($entity->$field);
  36. $new_image_ids = array();
  37. foreach($current_image_ids as $image_id)
  38. {
  39. if(in_array($image_id, $delete_ids))
  40. {
  41. $image_entity = get_entity($image_id);
  42. if($image_entity)
  43. {
  44. $image_entity->delete();
  45. }
  46. }
  47. else
  48. {
  49. $new_image_ids[] = $image_id;
  50. }
  51. }
  52. $entity->$field = implode(",", $new_image_ids);
  53. $entity->save();
  54. echo "ok";
  55. }
  56. function gallery_field_show_image($image_id, $size = "default")
  57. {
  58. /* @var $image ElggFile */
  59. $image = get_entity($image_id);
  60. if($size == 'thumb')
  61. {
  62. $image = get_entity($image->thumb_file_guid);
  63. }
  64. $success = false;
  65. $contents = null;
  66. if ($image->open("read")) {
  67. $contents = $image->read($image->getSize());
  68. if ($contents) {
  69. $success = true;
  70. }
  71. $image->close();
  72. }
  73. if($success == false)
  74. {
  75. throw new Exception("can`t read image");
  76. }
  77. header("Content-type: ".$image->getMimeType(), true);
  78. header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', strtotime("+6 months")), true);
  79. header("Pragma: public", true);
  80. header("Cache-Control: public", true);
  81. header("Content-Length: " . strlen($contents));
  82. echo $contents;
  83. }
  84. function gallery_field_save_sort()
  85. {
  86. $entity_id = $_POST['entity_id'];
  87. $field = $_POST['field'];
  88. $sort_images_list = $_POST['images'];
  89. $sort_ids = explode(",", $sort_images_list);
  90. $entity = get_entity($entity_id);
  91. if(false == $entity->canEdit())
  92. {
  93. throw new Exception("can`t edit");
  94. }
  95. $current_image_ids = gallery_field_image_ids_from_value($entity->$field);
  96. if(count($current_image_ids) == 0)
  97. {
  98. return;
  99. }
  100. /**
  101. * check if all values persist in both arrays
  102. */
  103. if(count(array_intersect($sort_ids, $current_image_ids)) != count($current_image_ids))
  104. {
  105. throw new Exception("bad count");
  106. }
  107. $entity->$field = implode(",", $sort_ids);
  108. $entity->save();
  109. }
  110. /*
  111. function gallery_field($entity_id, $entity_field)
  112. {
  113. elgg_load_js('gallery_field_editor');
  114. $entity = get_entity($entity_id);
  115. $metadata = elgg_get_metadata(array(
  116. 'guids' => array($entity_id),
  117. 'metadata_names' => array($entity_field)
  118. ));
  119. $title = elgg_echo("gallery_field:edit_images_title");
  120. $content = elgg_view('gallery_field/editor', array(
  121. 'value' => $metadata->value,
  122. 'entity' => $entity
  123. ));
  124. $body = elgg_view_layout('content', array(
  125. 'content' => $content,
  126. 'title' => $title,
  127. 'filter' => '',
  128. ));
  129. echo elgg_view_page($title, $body);
  130. }*/