356907dd04db081ea28d1fd8caee0bc28f0abaf8
[atutor.git] / mods / photos / albums.php
1 <?php
2 /***********************************************************************/
3 /* ATutor                                                                                                                          */
4 /***********************************************************************/
5 /* Copyright (c) 2002-2009                                                                                         */
6 /* Adaptive Technology Resource Centre / Inclusive Design Institution  */
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 define('AT_INCLUDE_PATH', '../../include/');
16 require (AT_INCLUDE_PATH.'vitals.inc.php');
17 include (AT_PA_INCLUDE.'classes/PhotoAlbum.class.php');
18 include (AT_PA_INCLUDE.'classes/SimpleImage.class.php');
19 include (AT_PA_INCLUDE.'lib.inc.php');
20 include (AT_PA_INCLUDE.'classes/AjaxMessage.class.php');
21 $_custom_css = $_base_path . AT_PA_BASENAME . 'module.css'; // use a custom stylesheet
22 $_custom_head .= '<script src="'.$_base_path . AT_PA_BASENAME . 'include/ajaxupload.js" type="text/javascript"></script>';
23
24 $id = intval($_REQUEST['id']);
25 $pa = new PhotoAlbum($id);
26 $info = $pa->getAlbumInfo();
27 $action_permission = $pa->checkAlbumPriv($_SESSION['member_id']);
28
29 //TODO: Validate users, using permission and course album control.
30 if ($info['member_id'] != $_SESSION['member_id']){
31         $visible_albums = $pa->getAlbums($_SESSION['member_id'], $info['type_id']);
32         if(!isset($visible_albums[$id]) && $info['permission']==AT_PA_PRIVATE_ALBUM){
33                 //TODO msg;
34                 $msg->addError("ACCESS_DENIED");
35                 header('location: index.php');
36                 exit;
37         }
38 }
39
40 //Set pages/submenu
41 $_pages[AT_PA_BASENAME.'index.php']['children'] = array(AT_PA_BASENAME.'albums.php');
42
43 $_pages[AT_PA_BASENAME.'albums.php']['title']    = _AT('pa_albums') .' - '.$info['name'];
44 if ($info['type_id']==AT_PA_TYPE_MY_ALBUM){
45         $_pages[AT_PA_BASENAME.'albums.php']['parent']    = AT_PA_BASENAME.'index.php';
46 } elseif ($info['type_id']==AT_PA_TYPE_COURSE_ALBUM){
47         $_pages[AT_PA_BASENAME.'albums.php']['parent']    = AT_PA_BASENAME.'course_albums.php';
48
49 //if this member has the permission to edit the album, show the edit/organize menu
50 if ($action_permission){
51         $_pages[AT_PA_BASENAME.'albums.php']['children']  = array(
52                                                                                                                         AT_PA_BASENAME.'edit_photos.php?aid='.$id,
53                                                                                                                         AT_PA_BASENAME.'edit_photos.php?aid='.$id.SEP.'org=1',
54                                                                                                                 );
55         $_pages[AT_PA_BASENAME.'edit_photos.php?aid='.$id]['title_var'] = 'pa_edit_photos';
56         $_pages[AT_PA_BASENAME.'edit_photos.php?aid='.$id]['parent'] = AT_PA_BASENAME.'albums.php';
57         $_pages[AT_PA_BASENAME.'edit_photos.php?aid='.$id.SEP.'org=1']['title_var'] = 'pa_organize_photos';
58         $_pages[AT_PA_BASENAME.'edit_photos.php?aid='.$id.SEP.'org=1']['parent'] = AT_PA_BASENAME.'albums.php';
59 }
60
61 //TODO: handle add_photo
62 if(isset($_POST['upload'])){
63         //check file size, filename, and extension
64         $_FILES['photo'] = checkPhoto($_FILES['photo']);
65         if ($_FILES['photo']===false || (!$action_permission && $info['type_id']!=AT_PA_TYPE_COURSE_ALBUM)){
66                 //owner and course members can upload pictures.  Not edit though. 
67                 echo json_encode(array(
68                                                 'aid'=>$id,
69                                                 'pid'=>-1,
70                                                 'msg'=>htmlentities($msg->printErrors()),
71                                                 'error'=>true));
72                 exit;
73         }
74
75         //computer album folder name and photo filename, if exist, shift bits
76         //goal: generate a random yet computable file structure to disallow
77         //              users to browse through others' photos through URLs.    
78         $album_file_path = getAlbumFilePath($id, $info['created_date']);
79         $album_file_path_tn = $album_file_path.'_tn'.DIRECTORY_SEPARATOR;
80         $album_file_path .= DIRECTORY_SEPARATOR;
81
82         if (!is_dir(AT_PA_CONTENT_DIR.$album_file_path)){
83                 mkdir(AT_PA_CONTENT_DIR.$album_file_path);              
84         }
85         if (!is_dir(AT_PA_CONTENT_DIR.$album_file_path_tn)){
86                 mkdir(AT_PA_CONTENT_DIR.$album_file_path_tn);           
87         }
88
89         //add the photo
90         $result = $pa->addPhoto($_FILES['photo']['name'], $_POST['photo_comment'], $_SESSION['member_id']);
91         if ($result===FALSE){
92                 $msg->addError('PA_ADD_PHOTO_FAILED');
93         }
94
95         if (!$msg->containsErrors()){
96                 //get photo filepath
97                 $added_photo_id = mysql_insert_id();
98                 $photo_info = $pa->getPhotoInfo($added_photo_id);
99                 $photo_file_path = getPhotoFilePath($added_photo_id, $_FILES['photo']['name'], $photo_info['created_date']);
100
101                 //resize images to a specific size, and its thumbnail
102                 $si = new SimpleImage();
103                 $si->load($_FILES['photo']['tmp_name']);
104                 $image_w = $si->getWidth();
105                 $image_h = $si->getHeight();
106
107                 //picture is horizontal 
108                 if($image_w > $image_h){
109                         //don't stretch images
110                         if ($image_w > AT_PA_IMAGE){
111                                 $si->resizeToWidth(AT_PA_IMAGE);
112                                 $si->save(AT_PA_CONTENT_DIR.$album_file_path.$photo_file_path);
113                         } else {
114                                 move_uploaded_file($_FILES['photo']['tmp_name'], AT_PA_CONTENT_DIR.$album_file_path.$photo_file_path);
115                         }
116                         $si->resizeToWidth(AT_PA_IMAGE_THUMB);
117                         $si->save(AT_PA_CONTENT_DIR.$album_file_path_tn.$photo_file_path);
118                 } else {
119                         if ($image_h > AT_PA_IMAGE){
120                                 $si->resizeToHeight(AT_PA_IMAGE);
121                                 $si->save(AT_PA_CONTENT_DIR.$album_file_path.$photo_file_path);
122                         } else {
123                                 move_uploaded_file($_FILES['photo']['tmp_name'], AT_PA_CONTENT_DIR.$album_file_path.$photo_file_path);
124                         }
125                         $si->resizeToHeight(AT_PA_IMAGE_THUMB);
126                         $si->save(AT_PA_CONTENT_DIR.$album_file_path_tn.$photo_file_path);
127                 }
128                 if ($_POST['upload'] == 'ajax'){
129                         $photo_file_hash = getPhotoFilePath($added_photo_id, '', $photo_info['created_date']);
130                         //return JSON, relying on jQuery to convert entries to html entities.
131                         echo json_encode(array(
132                                                 'aid'=>$id,
133                                                 'pid'=>$added_photo_id,
134                                                 'ph'=>$photo_file_hash,
135                                                 'size'=>number_format(filesize(AT_PA_CONTENT_DIR.$album_file_path.$photo_file_path)/1024, 2),
136                                                 'title'=>$photo_info['title'],
137                                                 'alt'=>$photo_info['alt']));
138                         $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
139                         exit;
140                 }
141         } //if msg contain error
142         header('location: albums.php?id='.$id);
143         exit;
144 }
145
146 //paginator settings
147 $page = intval($_GET['p']);
148 $photos_count = sizeof($pa->getAlbumPhotos());
149 $last_page = ceil($photos_count/AT_PA_PHOTOS_PER_PAGE);
150
151 if (!$page || $page < 0) {
152         $page = 1;
153 } elseif ($page > $last_page){
154         $page = $last_page;
155 }
156
157 $count  = (($page-1) * AT_PA_PHOTOS_PER_PAGE) + 1;
158 $offset = ($page-1) * AT_PA_PHOTOS_PER_PAGE;
159
160 //get details
161 $photos = $pa->getAlbumPhotos($offset);
162 $comments = $pa->getComments($id, false);
163 //TODO: Can improve performance by adding this to a session variable
164 $memory_usage = memoryUsage($_SESSION['member_id']);    
165
166 include (AT_INCLUDE_PATH.'header.inc.php');
167 $savant->assign('album_info', $info);
168 $savant->assign('photos', $photos);
169 $savant->assign('comments', $comments);
170 $savant->assign('page', $page);
171 $savant->assign('num_rows', $photos_count);
172 $savant->assign('memory_usage', $memory_usage/(1024*1024));     //mb
173 $savant->assign('allowable_memory_usage', $_config['pa_max_memory_per_member']);        //mb
174 $savant->assign('action_permission', $action_permission);
175 $savant->display('pa_albums.tmpl.php');
176 include (AT_INCLUDE_PATH.'footer.inc.php'); 
177 ?>