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