save.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * Elgg Market Plugin
  4. * @package market
  5. */
  6. elgg_load_library('market');
  7. // start a new sticky form session in case of failure
  8. elgg_make_sticky_form('market');
  9. // store errors to pass along
  10. $error = FALSE;
  11. $error_forward_url = REFERER;
  12. $user = elgg_get_logged_in_user_entity();
  13. // edit or create a new entity
  14. $guid = get_input('guid');
  15. if ($guid) {
  16. $entity = get_entity($guid);
  17. if (elgg_instanceof($entity, 'object', 'market') && $entity->canEdit()) {
  18. $post = $entity;
  19. } else {
  20. register_error(elgg_echo('market:error:post_not_found'));
  21. forward(get_input('forward', REFERER));
  22. }
  23. } else {
  24. $post = new ElggObject();
  25. $post->subtype = 'market';
  26. $new_post = true;
  27. }
  28. $values = array(
  29. 'title' => '',
  30. 'marketcategory' => '',
  31. 'market_type' => '',
  32. 'location' => '',
  33. 'custom' => '',
  34. 'description' => '',
  35. 'price' => '',
  36. 'access_id' => ACCESS_DEFAULT,
  37. 'tags' => '',
  38. 'container_guid' => (int)get_input('container_guid'),
  39. );
  40. // fail if a required entity isn't set
  41. $required = array('title', 'marketcategory', 'market_type', 'description');
  42. // load from POST and do sanity and access checking
  43. foreach ($values as $name => $default) {
  44. if ($name === 'title') {
  45. $value = htmlspecialchars(get_input('title', $default, false), ENT_QUOTES, 'UTF-8');
  46. } else {
  47. $value = get_input($name, $default);
  48. }
  49. if (in_array($name, $required) && empty($value)) {
  50. $error = elgg_echo("market:error:missing:$name");
  51. }
  52. if ($error) {
  53. break;
  54. }
  55. switch ($name) {
  56. case 'tags':
  57. $values[$name] = string_to_tag_array($value);
  58. break;
  59. case 'container_guid':
  60. // this can't be empty or saving the base entity fails
  61. if (!empty($value)) {
  62. if (can_write_to_container($user->getGUID(), $value)) {
  63. $values[$name] = $value;
  64. } else {
  65. $error = elgg_echo("market:error:cannot_write_to_container");
  66. }
  67. } else {
  68. unset($values[$name]);
  69. }
  70. break;
  71. default:
  72. $values[$name] = $value;
  73. break;
  74. }
  75. }
  76. // assign values to the entity, stopping on error.
  77. if (!$error) {
  78. foreach ($values as $name => $value) {
  79. $post->$name = $value;
  80. }
  81. }
  82. // only try to save base entity if no errors
  83. if (!$error) {
  84. if ($post->save()) {
  85. // remove sticky form entries
  86. elgg_clear_sticky_form('market');
  87. system_message(elgg_echo('market:posted'));
  88. // add to river if changing status or published, regardless of new post
  89. // because we remove it for drafts.
  90. if ($new_post) {
  91. elgg_create_river_item(array(
  92. 'view' => 'river/object/market/create',
  93. 'action_type' => 'create',
  94. 'subject_guid' => $user->guid,
  95. 'object_guid' => $post->guid,
  96. ));
  97. }
  98. // Image 1 upload
  99. if ((isset($_FILES['upload1']['name'])) && (substr_count($_FILES['upload1']['type'],'image/'))) {
  100. $imgdata1 = get_uploaded_file('upload1');
  101. market_add_image($post, $imgdata1, 1);
  102. }
  103. // Image 2 upload
  104. if ((isset($_FILES['upload2']['name'])) && (substr_count($_FILES['upload2']['type'],'image/'))) {
  105. $imgdata2 = get_uploaded_file('upload2');
  106. market_add_image($post, $imgdata2, 2);
  107. }
  108. // Image 3 upload
  109. if ((isset($_FILES['upload3']['name'])) && (substr_count($_FILES['upload3']['type'],'image/'))) {
  110. $imgdata3 = get_uploaded_file('upload3');
  111. market_add_image($post, $imgdata3, 3);
  112. }
  113. // Image 4 upload
  114. if ((isset($_FILES['upload4']['name'])) && (substr_count($_FILES['upload4']['type'],'image/'))) {
  115. $imgdata4 = get_uploaded_file('upload4');
  116. market_add_image($post, $imgdata4, 4);
  117. }
  118. forward($post->getURL());
  119. } else {
  120. register_error(elgg_echo('market:error:cannot_save'));
  121. forward($error_forward_url);
  122. }
  123. } else {
  124. register_error($error);
  125. forward($error_forward_url);
  126. }