editpeopletag.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Edit an existing group
  6. *
  7. * PHP version 5
  8. *
  9. * LICENCE: 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 Group
  23. * @package StatusNet
  24. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  25. * @link http://status.net/
  26. */
  27. if (!defined('STATUSNET') && !defined('LACONICA')) {
  28. exit(1);
  29. }
  30. /**
  31. * Add a new group
  32. *
  33. * This is the form for adding a new group
  34. *
  35. * @category Group
  36. * @package StatusNet
  37. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  38. * @link http://status.net/
  39. */
  40. class EditpeopletagAction extends Action
  41. {
  42. var $msg, $confirm, $confirm_args=array();
  43. function title()
  44. {
  45. if ($_SERVER['REQUEST_METHOD'] == 'POST' && $this->boolean('delete')) {
  46. // TRANS: Title for edit list page after deleting a tag.
  47. // TRANS: %s is a list.
  48. return sprintf(_('Delete %s list'), $this->peopletag->tag);
  49. }
  50. // TRANS: Title for edit list page.
  51. // TRANS: %s is a list.
  52. return sprintf(_('Edit list %s'), $this->peopletag->tag);
  53. }
  54. /**
  55. * Prepare to run
  56. */
  57. function prepare(array $args = array())
  58. {
  59. parent::prepare($args);
  60. if (!common_logged_in()) {
  61. // TRANS: Error message displayed when trying to perform an action that requires a logged in user.
  62. $this->clientError(_('Not logged in.'));
  63. }
  64. $id = $this->arg('id');
  65. if (common_config('singleuser', 'enabled')) {
  66. $tagger_arg = User::singleUserNickname();
  67. } else {
  68. $tagger_arg = $this->arg('tagger');
  69. }
  70. $tag_arg = $this->arg('tag');
  71. $tagger = common_canonical_nickname($tagger_arg);
  72. $tag = common_canonical_tag($tag_arg);
  73. $current = common_current_user();
  74. // Permanent redirect on non-canonical tag
  75. if ($tagger_arg != $tagger || $tag_arg != $tag) {
  76. $args = array('tagger' => $tagger, 'tag' => $tag);
  77. common_redirect(common_local_url('editpeopletag', $args), 301);
  78. }
  79. $user = null;
  80. if ($id) {
  81. $this->peopletag = Profile_list::getKV('id', $id);
  82. if (!empty($this->peopletag)) {
  83. $user = User::getKV('id', $this->peopletag->tagger);
  84. }
  85. } else {
  86. if (!$tagger) {
  87. // TRANS: Error message displayed when trying to perform an action that requires a tagging user or ID.
  88. $this->clientError(_('No tagger or ID.'), 404);
  89. }
  90. $user = User::getKV('nickname', $tagger);
  91. $this->peopletag = Profile_list::pkeyGet(array('tagger' => $user->id, 'tag' => $tag));
  92. }
  93. if (!$this->peopletag) {
  94. // TRANS: Client error displayed when referring to a non-existing list.
  95. $this->clientError(_('No such list.'), 404);
  96. }
  97. if (!$user) {
  98. // This should not be happening
  99. // TRANS: Client error displayed when referring to non-local user.
  100. $this->clientError(_('Not a local user.'), 404);
  101. }
  102. if ($current->id != $user->id) {
  103. // TRANS: Client error displayed when reting to edit a tag that was not self-created.
  104. $this->clientError(_('You must be the creator of the tag to edit it.'), 404);
  105. }
  106. $this->tagger = $user->getProfile();
  107. return true;
  108. }
  109. /**
  110. * Handle the request
  111. *
  112. * On GET, show the form. On POST, try to save the group.
  113. *
  114. * @param array $args unused
  115. *
  116. * @return void
  117. */
  118. function handle()
  119. {
  120. parent::handle();
  121. if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  122. $this->trySave();
  123. } else {
  124. $this->showForm();
  125. }
  126. }
  127. function showConfirm($msg=null, $fwd=null)
  128. {
  129. $this->confirm = $msg;
  130. $this->confirm_args = $fwd;
  131. $this->showPage();
  132. }
  133. function showConfirmForm()
  134. {
  135. $this->elementStart('form', array('id' => 'form_peopletag_edit_confirm',
  136. 'class' => 'form_settings',
  137. 'method' => 'post',
  138. 'action' => common_local_url('editpeopletag',
  139. array('tagger' => $this->tagger->nickname,
  140. 'tag' => $this->peopletag->tag))));
  141. $this->elementStart('fieldset');
  142. $this->hidden('token', common_session_token());
  143. $this->hidden('id', $this->arg('id'));
  144. foreach ($this->confirm_args as $key => $val) {
  145. $this->hidden($key, $val);
  146. }
  147. $this->submit('form_action-no',
  148. _m('BUTTON','No'),
  149. 'submit form_action-primary',
  150. 'cancel');
  151. $this->submit('form_action-yes',
  152. _m('BUTTON','Yes'),
  153. 'submit form_action-secondary',
  154. 'confirm');
  155. $this->elementEnd('fieldset');
  156. $this->elementEnd('form');
  157. }
  158. function showForm($msg=null)
  159. {
  160. $this->msg = $msg;
  161. $this->showPage();
  162. }
  163. function showObjectNav()
  164. {
  165. $nav = new PeopletagGroupNav($this, $this->peopletag);
  166. $nav->show();
  167. }
  168. function showContent()
  169. {
  170. if ($this->confirm) {
  171. $this->showConfirmForm();
  172. return;
  173. }
  174. $form = new PeopletagEditForm($this, $this->peopletag);
  175. $form->show();
  176. $form->showProfileList();
  177. }
  178. function showPageNotice()
  179. {
  180. if ($this->msg) {
  181. $this->element('p', 'error', $this->msg);
  182. } else if ($this->confirm) {
  183. $this->element('p', 'instructions', $this->confirm);
  184. } else {
  185. $this->element('p', 'instructions',
  186. // TRANS: Form instruction for edit list form.
  187. _('Use this form to edit the list.'));
  188. }
  189. }
  190. function showScripts()
  191. {
  192. parent::showScripts();
  193. $this->autofocus('tag');
  194. }
  195. function trySave()
  196. {
  197. $tag = common_canonical_tag($this->trimmed('tag'));
  198. $description = $this->trimmed('description');
  199. $private = $this->boolean('private');
  200. $delete = $this->arg('delete');
  201. $confirm = $this->arg('confirm');
  202. $cancel = $this->arg('cancel');
  203. if ($delete && $cancel) {
  204. // TRANS: Form validation error displayed if the form data for deleting a tag was incorrect.
  205. $this->showForm(_('Delete aborted.'));
  206. return;
  207. }
  208. $set_private = $private && $this->peopletag->private != $private;
  209. if ($delete && !$confirm) {
  210. // TRANS: Text in confirmation dialog for deleting a tag.
  211. $this->showConfirm(_('Deleting this tag will permanantly remove ' .
  212. 'all its subscription and membership records. ' .
  213. 'Do you still want to continue?'), array('delete' => 1));
  214. return;
  215. } else if (common_valid_tag($tag)) {
  216. // TRANS: Form validation error displayed if a given tag is invalid.
  217. $this->showForm(_('Invalid tag.'));
  218. return;
  219. } else if ($tag != $this->peopletag->tag && $this->tagExists($tag)) {
  220. // TRANS: Form validation error displayed if a given tag is already present.
  221. // TRANS: %s is the already present tag.
  222. $this->showForm(sprintf(_('You already have a tag named %s.'), $tag));
  223. return;
  224. } else if (Profile_list::descriptionTooLong($description)) {
  225. $this->showForm(sprintf(
  226. // TRANS: Client error shown when providing too long a description when editing a list.
  227. // TRANS: %d is the maximum number of allowed characters.
  228. _m('Description is too long (maximum %d character).',
  229. 'Description is too long (maximum %d characters).',
  230. Profile_list::maxDescription()),
  231. Profile_list::maxDescription()));
  232. return;
  233. } else if ($set_private && !$confirm && !$cancel) {
  234. $fwd = array('tag' => $tag,
  235. 'description' => $description,
  236. 'private' => (int) $private);
  237. // TRANS: Text in confirmation dialog for setting a tag from public to private.
  238. $this->showConfirm(_('Setting a public tag as private will ' .
  239. 'permanently remove all the existing ' .
  240. 'subscriptions to it. Do you still want to continue?'), $fwd);
  241. return;
  242. }
  243. // We tested $delete && !$confirm earlier so confirmation is required before getting here
  244. if ($delete) {
  245. // This might take quite a bit of time.
  246. $this->peopletag->delete();
  247. // send home.
  248. common_redirect(common_local_url('all', array('nickname' => $this->tagger->getNickname())), 303);
  249. }
  250. $this->peopletag->query('BEGIN');
  251. $orig = clone($this->peopletag);
  252. $this->peopletag->tag = $tag;
  253. $this->peopletag->description = $description;
  254. if (!$set_private || $confirm) {
  255. $this->peopletag->private = $private;
  256. }
  257. $result = $this->peopletag->update($orig);
  258. if (!$result) {
  259. common_log_db_error($this->group, 'UPDATE', __FILE__);
  260. // TRANS: Server error displayed when updating a list fails.
  261. $this->serverError(_('Could not update list.'));
  262. }
  263. $this->peopletag->query('COMMIT');
  264. if ($set_private && $confirm) {
  265. Profile_tag_subscription::cleanup($this->peopletag);
  266. }
  267. if ($tag != $orig->tag) {
  268. common_redirect(common_local_url('editpeopletag',
  269. array('tagger' => $this->tagger->nickname,
  270. 'tag' => $tag)),
  271. 303);
  272. } else {
  273. // TRANS: Edit list form success message.
  274. $this->showForm(_('Options saved.'));
  275. }
  276. }
  277. function tagExists($tag)
  278. {
  279. $args = array('tagger' => $this->tagger->id, 'tag' => $tag);
  280. $ptag = Profile_list::pkeyGet($args);
  281. return !empty($ptag);
  282. }
  283. }