editphoto.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. /**
  3. * GNU Social
  4. * Copyright (C) 2010, Free Software Foundation, Inc.
  5. *
  6. * PHP version 5
  7. *
  8. * LICENCE:
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * @category Widget
  23. * @package GNU Social
  24. * @author Ian Denhardt <ian@zenhack.net>
  25. * @author Sean Corbett <sean@gnu.org>
  26. * @author Max Shinn <trombonechamp@gmail.com>
  27. * @copyright 2010 Free Software Foundation, Inc.
  28. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  29. */
  30. if (!defined('STATUSNET')) {
  31. exit(1);
  32. }
  33. class EditphotoAction extends Action
  34. {
  35. var $user = null;
  36. function prepare(array $args = array())
  37. {
  38. parent::prepare($args);
  39. $args = $this->returnToArgs();
  40. $this->user = common_current_user();
  41. $this->photoid = $args[1]['photoid'];
  42. $this->photo = GNUsocialPhoto::getKV('id', $this->photoid);
  43. return true;
  44. }
  45. function handle()
  46. {
  47. parent::handle();
  48. if($_SERVER['REQUEST_METHOD'] == 'POST') {
  49. $this->handlePost();
  50. }
  51. $this->showPage();
  52. }
  53. function title()
  54. {
  55. if ($this->photo->title)
  56. return _m('Edit photo - ' . $this->photo->title);
  57. else
  58. return _m('Edit photo');
  59. }
  60. function showContent()
  61. {
  62. if ($this->photo->album_id == 0) {
  63. $this->element('p', array(), _('This photo does not exist or was deleted.'));
  64. return;
  65. }
  66. if ($this->user->profile_id != $this->photo->profile_id) {
  67. $this->element('p', array(), _('You are not authorized to edit this photo.'));
  68. return;
  69. }
  70. //showForm() data
  71. if(!empty($this->msg)) {
  72. $class = ($this->success) ? 'success' : 'error';
  73. $this->element('p', array('class' => $class), $this->msg);
  74. }
  75. $this->element('img', array('src' => $this->photo->uri));
  76. $this->elementStart('form', array('method' => 'post',
  77. 'action' => '/editphoto/' . $this->photo->id));
  78. $this->elementStart('ul', 'form_data');
  79. $this->elementStart('li');
  80. $this->input('phototitle', _("Title"), $this->photo->title, _("The title of the photo. (Optional)"));
  81. $this->elementEnd('li');
  82. $this->elementStart('li');
  83. $this->textarea('photo_description', _("Description"), $this->photo->photo_description, _("A description of the photo. (Optional)"));
  84. $this->elementEnd('li');
  85. $this->elementStart('li');
  86. $this->dropdown('album', _("Album"), $this->albumList(), _("The album in which to place this photo"), false, $this->photo->album_id);
  87. $this->elementEnd('li');
  88. $this->elementEnd('ul');
  89. $this->submit('update', _('Update'));
  90. $this->elementEnd('form');
  91. $this->element('br');
  92. $this->elementStart('form', array('method' => 'post',
  93. 'action' => '/editphoto/' . $this->photo->id));
  94. $this->element('input', array('type' => 'submit',
  95. 'name' => 'delete',
  96. 'value' => _('Delete Photo'),
  97. 'id' => 'delete',
  98. 'class' => 'submit',
  99. 'onclick' => 'return confirm(\'Are you sure you would like to delete this photo?\')'));
  100. $this->elementEnd('form');
  101. }
  102. function handlePost()
  103. {
  104. common_log(LOG_INFO, 'handlPost()!');
  105. if ($this->photo->album_id == 0) {
  106. $this->element('p', array(), _('This photo does not exist or was deleted.'));
  107. return;
  108. }
  109. if ($this->user->profile_id != $this->photo->profile_id) {
  110. $this->element('p', array(), _('You are not authorized to edit this photo.'));
  111. return;
  112. }
  113. if ($this->arg('update')) {
  114. $this->updatePhoto();
  115. }
  116. if ($this->arg('delete')) {
  117. $this->deletePhoto();
  118. }
  119. }
  120. function showForm($msg, $success=false)
  121. {
  122. $this->msg = $msg;
  123. $this->success = $success;
  124. // $this->showPage();
  125. }
  126. function albumList()
  127. {
  128. $cur = common_current_user();
  129. $album = new GNUsocialPhotoAlbum();
  130. $album->user_id = $cur->id;
  131. $albumlist = array();
  132. if (!$album->find()) {
  133. GNUsocialPhotoAlbum::newAlbum($cur->id, 'Default');
  134. }
  135. while ($album->fetch()) {
  136. $albumlist[$album->album_id] = $album->album_name;
  137. }
  138. return $albumlist;
  139. }
  140. function updatePhoto()
  141. {
  142. $cur = common_current_user();
  143. $this->photo->title = $this->trimmed('phototitle');
  144. $this->photo->photo_description = $this->trimmed('photo_description');
  145. $profile_id = $cur->id;
  146. $album = GNUsocialPhotoAlbum::getKV('album_id', $this->trimmed('album'));
  147. if ($album->profile_id != $profile_id) {
  148. $this->showForm(_('Error: This is not your album!'));
  149. return;
  150. }
  151. $this->photo->album_id = $album->album_id;
  152. if ($this->photo->validate())
  153. $this->photo->update();
  154. else {
  155. $this->showForm(_('Error: The photo data is not valid.'));
  156. return;
  157. }
  158. common_redirect('/photo/' . $this->photo->id, '303');
  159. // common_redirect exits
  160. }
  161. function deletePhoto()
  162. {
  163. //For redirection
  164. $oldalbum = $this->album_id;
  165. $notice = Notice::getKV('id', $this->photo->notice_id);
  166. $this->photo->delete();
  167. if (Event::handle('StartDeleteOwnNotice', array($this->user, $notice))) {
  168. $notice->deleteAs($this->scoped);
  169. Event::handle('EndDeleteOwnNotice', array($this->user, $notice));
  170. }
  171. $this->showForm(_('Success!'));
  172. common_redirect('/' . $this->user->nickname . '/photos/' . $oldalbum, '303');
  173. }
  174. }