]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/GNUsocialPhotos/actions/editphoto.php
2ebcd97aff8f141acb3f1d31a0e35d139598edf9
[quix0rs-gnu-social.git] / plugins / GNUsocialPhotos / actions / editphoto.php
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
31 if (!defined('STATUSNET')) {
32     exit(1);
33 }
34
35 class EditphotoAction extends Action
36 {
37     var $user = null;
38
39     function prepare($args)
40     {
41         parent::prepare($args);
42         $args = $this->returnToArgs();
43         $this->user = common_current_user();
44         $this->photoid = $args[1]['photoid'];
45         $this->photo = GNUsocialPhoto::staticGet('id', $this->photoid);
46         return true;
47     }
48
49     function handle($args)
50     {
51         parent::handle($args);
52         if($_SERVER['REQUEST_METHOD'] == 'POST') {
53             $this->handlePost();
54         }
55         $this->showPage();
56     }
57
58     function title()
59     {
60         if ($this->photo->title)
61             return _m('Edit photo - ' . $this->photo->title);
62         else
63             return _m('Edit photo');
64     }
65
66     function showContent()
67     {
68
69         if ($this->photo->album_id == 0) {
70             $this->element('p', array(), _('This photo does not exist or was deleted.'));
71             return;
72         }
73
74         if ($this->user->profile_id != $this->photo->profile_id) {
75             $this->element('p', array(), _('You are not authorized to edit this photo.'));
76             return;
77         } 
78
79         $this->element('img', array('src' => $this->photo->uri));
80         $this->elementStart('form', array('method' => 'post',
81                                           'action' => '/editphoto/' . $this->photo->id));
82         $this->elementStart('ul', 'form_data');
83         $this->elementStart('li');
84         $this->input('phototitle', _("Title"), $this->photo->title, _("The title of the photo. (Optional)"));
85         $this->elementEnd('li');
86         $this->elementStart('li');
87         $this->textarea('photo_description', _("Description"), $this->photo->photo_description, _("A description of the photo. (Optional)"));
88         $this->elementEnd('li');
89         $this->elementStart('li');
90         $this->dropdown('album', _("Album"), $this->albumList(), _("The album in which to place this photo"), false, $this->photo->album_id);
91         $this->elementEnd('li');
92         $this->elementEnd('ul');
93         $this->submit('update', _('Update'));
94         $this->elementEnd('form');
95         $this->element('br');            
96         $this->elementStart('form', array('method' => 'post',
97                                           'action' => '/editphoto/' . $this->photo->id));
98         $this->element('input', array('type' => 'submit',
99                                       'name' => 'delete',
100                                       'value' => _('Delete Photo'),
101                                       'id' => 'delete',
102                                       'class' => 'submit',
103                                       'onclick' => 'return confirm(\'Are you sure you would like to delete this photo?\')'));
104         $this->elementEnd('form');
105
106     }
107
108     function handlePost()
109     {
110
111         common_log(LOG_INFO, 'handlPost()!');
112
113         if ($this->photo->album_id == 0) {
114             $this->element('p', array(), _('This photo does not exist or was deleted.'));
115             return;
116         }
117
118         if ($this->user->profile_id != $this->photo->profile_id) {
119             $this->element('p', array(), _('You are not authorized to edit this photo.'));
120             return;
121         } 
122
123         if ($this->arg('update')) {
124             $this->updatePhoto();
125         }
126         if ($this->arg('delete')) {
127             $this->deletePhoto();
128         }
129     }
130
131     function showForm($msg, $success=false)
132     {
133         $this->msg = $msg;
134         $this->success = $success;
135
136 //        $this->showPage();
137     }
138
139     function albumList() 
140     {
141         $cur = common_current_user();
142         $album = new GNUsocialPhotoAlbum();
143         $album->user_id = $cur->id;
144
145         $albumlist = array();
146         if (!$album->find()) {
147             GNUsocialPhotoAlbum::newAlbum($cur->id, 'Default');
148         }
149         while ($album->fetch()) {
150             $albumlist[$album->album_id] = $album->album_name;
151         }
152         return $albumlist;
153     }
154
155     function updatePhoto()
156     {
157         $cur = common_current_user();
158         $this->photo->title = $this->trimmed('phototitle');
159         $this->photo->photo_description = $this->trimmed('photo_description');
160
161         $profile_id = $cur->id;
162        
163         $album = GNUsocialPhotoAlbum::staticGet('album_id', $this->trimmed('album'));
164         if ($album->profile_id != $profile_id) {
165             $this->showForm(_('Error: This is not your album!'));
166             return;
167         }
168         $this->photo->album_id = $album->album_id;
169         if ($this->photo->validate())
170             $this->photo->update();
171         else {
172             $this->showForm(_('Error: The photo data is not valid.'));
173             return;
174         }
175         $this->showForm(_('Success!'));
176
177     }
178
179     function deletePhoto()
180     {
181         $this->photo->title = "";
182         $this->photo->photo_description = "";
183         $this->photo->profile_id = 0;
184         $this->photo->album_id = 0;
185         $this->photo->uri = "";
186         $this->photo->thumb_uri = "";
187
188         if ($this->photo->validate())
189             $this->photo->update();
190         else {
191             $this->showForm(_('Error: The photo data is not valid.'));
192             return;
193         }
194         $this->showForm(_('Success!'));
195         return;
196     }
197
198 }