changed git call from https to git readonly
[atutor.git] / mods / social / groups / edit.php
1 <?php
2 /****************************************************************/
3 /* ATutor                                                                                                               */
4 /****************************************************************/
5 /* Copyright (c) 2002-2009                                                                              */
6 /* Adaptive Technology Resource Centre / University of Toronto  */
7 /* http://atutor.ca                                                                                             */
8 /*                                                              */
9 /* This program is free software. You can redistribute it and/or*/
10 /* modify it under the terms of the GNU General Public License  */
11 /* as published by the Free Software Foundation.                                */
12 /****************************************************************/
13 // $Id$
14 $_user_location = 'public';
15
16 define('AT_INCLUDE_PATH', '../../../include/');
17 require(AT_INCLUDE_PATH.'vitals.inc.php');
18 require(AT_SOCIAL_INCLUDE.'constants.inc.php');
19 require(AT_SOCIAL_INCLUDE.'friends.inc.php');
20 require(AT_SOCIAL_INCLUDE.'classes/SocialGroups/SocialGroup.class.php');
21 require(AT_SOCIAL_INCLUDE.'classes/SocialGroups/SocialGroups.class.php');
22 $_custom_css = $_base_path . AT_SOCIAL_BASENAME . 'module.css'; // use a custom stylesheet
23
24 // Get social group class
25 $social_groups = new SocialGroups();
26
27 // Get this group
28 $id = intval($_REQUEST['id']);  //make sure $_GET and $_POST don't overlap the use of 'id'
29 $group = new SocialGroup($id);
30
31 //validate if this user is the administrator of the group
32 if ($group->getUser() != $_SESSION['member_id']){
33         $msg->addError('CANT_EDIT_GROUP');
34         header('Location: index.php');
35         exit;
36 }
37
38 //TODO
39 //validate the group_admin is indeed a group member
40
41 function resize_image($src, $dest, $src_h, $src_w, $dest_h, $dest_w, $type) {
42         $thumbnail_img = imagecreatetruecolor($dest_w, $dest_h);
43
44         if ($type == 'gif') {
45                 $source = imagecreatefromgif($src);
46         } else if ($type == 'jpg') {
47                 $source = imagecreatefromjpeg($src);
48         } else {
49                 $source = imagecreatefrompng($src);
50         }
51         
52         imagecopyresampled($thumbnail_img, $source, 0, 0, 0, 0, $dest_w, $dest_h, $src_w, $src_h);
53
54         if ($type == 'gif') {
55                 imagegif($thumbnail_img, $dest);
56         } else if ($type == 'jpg') {
57                 imagejpeg($thumbnail_img, $dest, 75);
58         } else {
59                 imagepng($thumbnail_img, $dest, 7);
60         }
61 }
62
63 // check if GD is installed
64 if (!extension_loaded('gd')) {
65         require(AT_INCLUDE_PATH.'header.inc.php');
66         $msg->printInfos('FEATURE_NOT_AVAILABLE');
67         require(AT_INCLUDE_PATH.'footer.inc.php');
68         exit;
69 }
70
71
72 // Update group
73 if (isset($_POST['save'])){
74         //handles group logo
75         if ($_FILES['logo']['name']!=''){
76                 $gd_info = gd_info();
77                 $supported_images = array();
78                 if ($gd_info['GIF Create Support']) {
79                         $supported_images[] = 'gif';
80                 }
81                 if ($gd_info['JPG Support']) {
82                         $supported_images[] = 'jpg';
83                 }
84                 if ($gd_info['PNG Support']) {
85                         $supported_images[] = 'png';
86                 }
87
88                 if (!$supported_images) {
89                         require(AT_INCLUDE_PATH.'header.inc.php');
90                         $msg->printInfos('FEATURE_NOT_AVAILABLE');
91                         require(AT_INCLUDE_PATH.'footer.inc.php');
92                         exit;
93                 }
94
95                 // check if this is a supported file type
96                 $filename   = $stripslashes($_FILES['logo']['name']);
97                 $path_parts = pathinfo($filename);
98                 $extension  = strtolower($path_parts['extension']);
99                 $image_attributes = getimagesize($_FILES['logo']['tmp_name']);
100
101                 if ($extension == 'jpeg') {
102                         $extension = 'jpg';
103                 }
104
105                 if (!in_array($extension, $supported_images)) {
106                         $msg->addError(array('FILE_ILLEGAL', $extension));
107                         header('Location: '.$_SERVER['PHP_SELF'].'?id='.$id);
108                         exit;
109                 } else if ($image_attributes[2] > IMAGETYPE_PNG) {
110                         $msg->addError(array('FILE_ILLEGAL', $extension));
111                         header('Location: '.$_SERVER['PHP_SELF'].'?id='.$id);
112                         exit;
113                 }
114
115                 // make sure under max file size
116                 if ($_FILES['logo']['size'] > $_config['prof_pic_max_file_size']) {
117                         $msg->addError('FILE_MAX_SIZE');
118                         header('Location: '.$_SERVER['PHP_SELF'].'?id='.$id);
119                         exit;
120                 }
121
122                 // delete the old images (if any)
123                 foreach ($supported_images as $ext) {
124                         if (file_exists(AT_CONTENT_DIR.'social/'. $id.'.'.$ext)) {
125                                 unlink(AT_CONTENT_DIR.'social/'. $id.'.'.$ext);
126                         }
127                 }
128
129                 $new_filename = $id . '.' . $extension;
130                 $original_img = AT_CONTENT_DIR.'social/temp_'. $new_filename;
131                 $thumbnail_img= AT_CONTENT_DIR.'social/'. $new_filename;
132
133                 // only want the resized logo. (for now)
134                 if (!move_uploaded_file($_FILES['logo']['tmp_name'], $original_img)) {
135                         $msg->addError('CANNOT_OVERWRITE_FILE');
136                         header('Location: '.$_SERVER['PHP_SELF'].'?id='.$id);
137                         exit;
138                 }
139
140                 // resize the original and save it at $thumbnail_file
141                 $width  = $image_attributes[0];
142                 $height = $image_attributes[1];
143
144                 if ($width > $height && $width>100) {
145                         $thumbnail_height = intval(100 * $height / $width);
146                         $thumbnail_width  = 100;
147
148                         resize_image($original_img, $thumbnail_img, $height, $width, $thumbnail_height, $thumbnail_width, $extension);
149                 } else if ($width <= $height && $height > 100) {
150                         $thumbnail_height= 100;
151                         $thumbnail_width = intval(100 * $width / $height);
152                         resize_image($original_img, $thumbnail_img, $height, $width, $thumbnail_height, $thumbnail_width, $extension);
153                 } else {
154                         // no resizing, just copy the image.
155                         // it's too small to resize.
156                         copy($original_img, $thumbnail_img);
157                 }
158                 // clean the original
159                 unlink($original_img);
160         } 
161
162
163         //check if fields are empty
164         if ($_POST['group_name']==''){
165                 $missing_fields[] = _AT('group_name');
166         } elseif (intval($_POST['group_type'])<=0){
167                 $missing_fields[] = _('group_type');
168         }
169         if ($missing_fields) {
170                 $missing_fields = implode(', ', $missing_fields);
171                 $msg->addError(array('EMPTY_FIELDS', $missing_fields));
172         } else {
173                 $isSucceded = $social_groups->updateGroup($id, $_POST['group_admin'], $_POST['group_type'], $_POST['group_name'], $_POST['description'], $new_filename, $_POST['group_privacy']);
174
175                 if($isSucceded){
176                         $msg->addFeedback('SOCIAL_GROUP_UPDATED');
177                         header('Location: '.url_rewrite(AT_SOCIAL_BASENAME.'groups/index.php', AT_PRETTY_URL_HEADER));
178                         exit;
179                 } else {
180                         //Something went bad in the backend, contact admin?
181                         $msg->addError('GROUP_EDIT_FAILED');
182                 }
183         }
184 } elseif (isset($_POST['cancel'])){
185         $msg->addFeedback('CANCELLED');
186         header('Location: '.url_rewrite(AT_SOCIAL_BASENAME.'groups/index.php', AT_PRETTY_URL_HEADER));
187         exit;
188 }
189
190 //Display
191 include(AT_INCLUDE_PATH.'header.inc.php');
192 $savant->display('pubmenu.tmpl.php');
193 $savant->assign('group_obj', $group);
194 $savant->assign('group_types', $social_groups->getAllGroupType());
195 $savant->display('sgroup_edit.tmpl.php');
196 include(AT_INCLUDE_PATH.'footer.inc.php');
197 ?>