market.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. /**
  3. * Market helper functions
  4. *
  5. * @package Market
  6. */
  7. /**
  8. * Pull together market variables for the save/edit form
  9. *
  10. * @param Market $post
  11. * @return array
  12. */
  13. function market_prepare_form_vars($post = NULL) {
  14. $values = array(
  15. 'title' => NULL,
  16. 'description' => NULL,
  17. 'price' => NULL,
  18. 'access_id' => ACCESS_DEFAULT,
  19. 'marketcategory' => NULL,
  20. 'market_type' => NULL,
  21. 'location' => NULL,
  22. 'custom' => NULL,
  23. 'tags' => NULL,
  24. 'container_guid' => elgg_get_page_owner_guid(),
  25. 'guid' => NULL,
  26. );
  27. if ($post) {
  28. foreach (array_keys($values) as $field) {
  29. if (isset($post->$field)) {
  30. $values[$field] = $post->$field;
  31. }
  32. }
  33. }
  34. if (elgg_is_sticky_form('market')) {
  35. $sticky_values = elgg_get_sticky_values('market');
  36. foreach ($sticky_values as $key => $value) {
  37. $values[$key] = $value;
  38. }
  39. }
  40. elgg_clear_sticky_form('market');
  41. if (!$post) {
  42. return $values;
  43. }
  44. return $values;
  45. }
  46. /**
  47. * Delete market post and pictures
  48. *
  49. * @param Market $post
  50. * @return array
  51. */
  52. function market_delete_post($post = NULL) {
  53. if (!$post) {
  54. return false;
  55. }
  56. // Get owning user
  57. $owner = get_entity($post->getOwner());
  58. $owner_guid = $owner->guid;
  59. // Delete the images
  60. $prefix = "market/".$guid;
  61. $small = $prefix."small.jpg";
  62. $medium = $prefix."medium.jpg";
  63. $large = $prefix."large.jpg";
  64. $master = $prefix."master.jpg";
  65. $original = $prefix.".jpg";
  66. if ($small) {
  67. $delfile = new ElggFile();
  68. $delfile->owner_guid = $owner_guid;
  69. $delfile->setFilename($small);
  70. $delfile->delete();
  71. }
  72. if ($medium) {
  73. $delfile = new ElggFile();
  74. $delfile->owner_guid = $owner_guid;
  75. $delfile->setFilename($medium);
  76. $delfile->delete();
  77. }
  78. if ($large) {
  79. $delfile = new ElggFile();
  80. $delfile->owner_guid = $owner_guid;
  81. $delfile->setFilename($large);
  82. $delfile->delete();
  83. }
  84. if ($master) {
  85. $delfile = new ElggFile();
  86. $delfile->owner_guid = $owner_guid;
  87. $delfile->setFilename($master);
  88. $delfile->delete();
  89. }
  90. if ($original) {
  91. $delfile = new ElggFile();
  92. $delfile->owner_guid = $owner_guid;
  93. $delfile->setFilename($original);
  94. $delfile->delete();
  95. }
  96. // Delete the market post
  97. $rowsaffected = $post->delete();
  98. if ($rowsaffected > 0) {
  99. // Success
  100. return true;
  101. } else {
  102. // Error
  103. return false;
  104. }
  105. }
  106. /**
  107. * Add images to market post
  108. *
  109. * @param Market $post
  110. * @return array
  111. */
  112. function market_add_image($post = NULL, $data = NULL, $imagenum = 0) {
  113. if (!$post || !$data) {
  114. return false;
  115. }
  116. $filenum = $imagenum;
  117. if ($imagenum == 1) {
  118. $filenum = '';
  119. }
  120. $prefix = "market/".$post->guid;
  121. $filehandler = new ElggFile();
  122. $filehandler->owner_guid = $post->owner_guid;
  123. $filehandler->setFilename($prefix . $filenum . ".jpg");
  124. $filehandler->open("write");
  125. $filehandler->write($data);
  126. $filehandler->close();
  127. $small = get_resized_image_from_existing_file($filehandler->getFilenameOnFilestore(),40,40, true);
  128. $medium = get_resized_image_from_existing_file($filehandler->getFilenameOnFilestore(),153,153, true);
  129. $large = get_resized_image_from_existing_file($filehandler->getFilenameOnFilestore(),200,200, false);
  130. $master = get_resized_image_from_existing_file($filehandler->getFilenameOnFilestore(),600,800, false);
  131. if ($small) {
  132. $sizes = array('small' => $small, 'medium' => $medium, 'large' => $large, 'master' => $master);
  133. foreach($sizes as $size => $imgdata) {
  134. $thumb = new ElggFile();
  135. $thumb->owner_guid = $post->owner_guid;
  136. $thumb->setMimeType('image/jpeg');
  137. $thumb->setFilename($prefix.$size.$filenum.'.jpg');
  138. $thumb->open('write');
  139. $thumb->write($imgdata);
  140. $thumb->close();
  141. }
  142. // Set image in metadata array
  143. market_set_images($post, $imagenum);
  144. }
  145. }
  146. /**
  147. * Set what images has been uploaded (1,2,3, or 4)
  148. *
  149. * @param Market $post
  150. * @return array
  151. */
  152. function market_set_images($post, $imagenum) {
  153. // Check images metadata, if empty create initial array
  154. if ($post->images == '') {
  155. $post->images = serialize(array(1 => 0, 2 => 0, 3 => 0, 4 => 0));
  156. }
  157. // Create image array and seialize it into metadata
  158. $images = unserialize($post->images);
  159. $new_array = array();
  160. foreach ($images as $key => $value) {
  161. if ($key == $imagenum) {
  162. $value = 1;
  163. }
  164. $new_array[$key] = $value;
  165. }
  166. $post->images = serialize($new_array);
  167. return true;
  168. }
  169. /**
  170. * Delete market post and pictures
  171. *
  172. * @param Market $post
  173. * @return array
  174. */
  175. function market_delete_image($post = NULL, $imagenum) {
  176. if (!$post || !$imagenum) {
  177. return false;
  178. }
  179. $filenum = $imagenum;
  180. if ($imagenum == 1) {
  181. $filenum = '';
  182. }
  183. $owner = get_entity($post->getOwner());
  184. $owner_guid = $owner->guid;
  185. $prefix = "market/{$post->guid}";
  186. $names = array("{$prefix}small{$filenum}.jpg", "{$prefix}medium{$filenum}.jpg", "{$prefix}large{$filenum}.jpg", "{$prefix}master{$filenum}.jpg");
  187. foreach($names as $name) {
  188. $delfile = new ElggFile();
  189. $delfile->owner_guid = $owner_guid;
  190. $delfile->setFilename($name);
  191. $delfile->delete();
  192. }
  193. $images = unserialize($post->images);
  194. $new_array = array();
  195. foreach ($images as $key => $value) {
  196. if ($key == $imagenum) {
  197. $value = 0;
  198. }
  199. $new_array[$key] = $value;
  200. }
  201. $post->images = serialize($new_array);
  202. $post->save();
  203. return true;
  204. }