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