]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/tagprofile.php
Email notify-on-fave moved to Profile_prefs (run upgrade.php)
[quix0rs-gnu-social.git] / actions / tagprofile.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2008, 2009, StatusNet, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 if (!defined('GNUSOCIAL')) { exit(1); }
21
22 require_once INSTALLDIR . '/lib/settingsaction.php';
23 require_once INSTALLDIR . '/lib/peopletags.php';
24
25 class TagprofileAction extends FormAction
26 {
27     var $error = null;
28
29     protected $target = null;
30     protected $form = 'TagProfile';
31
32     protected function prepare(array $args=array())
33     {
34         parent::prepare($args);
35
36         $id = $this->trimmed('id');
37         if (!$id) {
38             $this->target = null;
39         } else {
40             $this->target = Profile::getKV('id', $id);
41
42             if (!$this->target instanceof Profile) {
43                 // TRANS: Client error displayed when referring to non-existing profile ID.
44                 $this->clientError(_('No profile with that ID.'));
45             }
46         }
47
48         if ($this->target instanceof Profile && !$this->scoped->canTag($this->target)) {
49             // TRANS: Client error displayed when trying to tag a user that cannot be tagged.
50             $this->clientError(_('You cannot tag this user.'));
51         }
52
53         return true;
54     }
55
56     protected function handle()
57     {
58         if (Event::handle('StartTagProfileAction', array($this, $this->target))) {
59             parent::handle();
60             Event::handle('EndTagProfileAction', array($this, $this->target));
61         }
62     }
63
64     function title()
65     {
66         if (!$this->target instanceof Profile) {
67             // TRANS: Title for list form when not on a profile page.
68             return _('List a profile');
69         }
70         // TRANS: Title for list form when on a profile page.
71         // TRANS: %s is a profile nickname.
72         return sprintf(_m('ADDTOLIST','List %s'), $this->target->getNickname());
73     }
74
75     function showContent()
76     {
77         $this->elementStart('div', 'entity_profile h-card');
78         // TRANS: Header in list form.
79         $this->element('h2', null, _('User profile'));
80
81         $avatarUrl = $this->target->avatarUrl(AVATAR_PROFILE_SIZE);
82         $this->element('img', array('src' => $avatarUrl,
83                                     'class' => 'u-photo avatar entity_depiction',
84                                     'width' => AVATAR_PROFILE_SIZE,
85                                     'height' => AVATAR_PROFILE_SIZE,
86                                     'alt' => $this->target->getBestName()));
87
88         $this->element('a', array('href' => $this->target->getUrl(),
89                                   'class' => 'entity_nickname p-nickname'),
90                        $this->target->getNickname());
91         if ($this->target->fullname) {
92             $this->element('div', 'p-name entity_fn', $this->target->fullname);
93         }
94
95         if ($this->target->location) {
96             $this->element('div', 'p-locality label entity_location', $this->target->location);
97         }
98
99         if ($this->target->homepage) {
100             $this->element('a', array('href' => $this->target->homepage,
101                                       'rel' => 'me',
102                                       'class' => 'u-url entity_url'),
103                            $this->target->homepage);
104         }
105
106         if ($this->target->bio) {
107             $this->element('div', 'p-note entity_note', $this->target->bio);
108         }
109
110         $this->elementEnd('div');
111
112         if (Event::handle('StartShowTagProfileForm', array($this, $this->target))) {
113             parent::showContent();
114             Event::handle('EndShowTagProfileForm', array($this, $this->target));
115         }
116     }
117
118     protected function getForm()
119     {
120         $class = $this->form.'Form';
121         $form = new $class($this, $this->target);
122         return $form;
123     }
124
125     protected function handlePost()
126     {
127         parent::handlePost();   // Does nothing for now
128
129         $tagstring = $this->trimmed('tags');
130         $token = $this->trimmed('token');
131
132         if (Event::handle('StartSavePeopletags', array($this, $tagstring))) {
133             $tags = array();
134             $tag_priv = array();
135
136             if (is_string($tagstring) && strlen($tagstring) > 0) {
137
138                 $tags = preg_split('/[\s,]+/', $tagstring);
139
140                 foreach ($tags as &$tag) {
141                     $private = @$tag[0] === '.';
142
143                     $tag = common_canonical_tag($tag);
144                     if (!common_valid_profile_tag($tag)) {
145                         // TRANS: Form validation error displayed if a given tag is invalid.
146                         // TRANS: %s is the invalid tag.
147                         $this->showForm(sprintf(_('Invalid tag: "%s".'), $tag));
148                         return;
149                     }
150
151                     $tag_priv[$tag] = $private;
152                 }
153             }
154
155             try {
156                 $result = Profile_tag::setTags($this->scoped->id, $this->target->id, $tags, $tag_priv);
157                 if (!$result) {
158                     throw new Exception('The tags could not be saved.');
159                 }
160             } catch (Exception $e) {
161                 $this->showForm($e->getMessage());
162                 return false;
163             }
164
165             if ($this->boolean('ajax')) {
166                 $this->startHTML('text/xml;charset=utf-8');
167                 $this->elementStart('head');
168                 $this->element('title', null, _m('TITLE','Tags'));
169                 $this->elementEnd('head');
170                 $this->elementStart('body');
171
172                 if ($this->scoped->id == $this->target->id) {
173                     $widget = new SelftagsWidget($this, $this->scoped, $this->target);
174                     $widget->show();
175                 } else {
176                     $widget = new PeopletagsWidget($this, $this->scoped, $this->target);
177                     $widget->show();
178                 }
179
180                 $this->elementEnd('body');
181                 $this->endHTML();
182             } else {
183                 // TRANS: Success message if lists are saved.
184                 $this->msg = _('Lists saved.');
185                 $this->showForm();
186             }
187
188             Event::handle('EndSavePeopletags', array($this, $tagstring));
189         }
190     }
191
192     function showPageNotice()
193     {
194         if ($this->error) {
195             $this->element('p', 'error', $this->error);
196         } else {
197             $this->elementStart('div', 'instructions');
198             $this->element('p', null,
199                            // TRANS: Page notice.
200                            _('Use this form to add your subscribers or subscriptions to lists.'));
201             $this->elementEnd('div');
202         }
203     }
204 }