first import
[projectpier.git] / application / models / project_files / ProjectFiles.class.php
blobca9b7306d0c2ecf140dd4509643b052a85be9940
1 <?php
3 /**
4 * ProjectFiles, generated on Tue, 04 Jul 2006 06:46:08 +0200 by
5 * DataObject generation tool
7 * @http://www.projectpier.org/
8 */
9 class ProjectFiles extends BaseProjectFiles {
11 const ORDER_BY_NAME = 'name';
12 const ORDER_BY_POSTTIME = 'created_on';
14 /**
15 * Array of types that will script treat as images (provide thumbnail, add
16 * it to insert image editor function etc)
18 * @var array
20 static public $image_types = array(
21 'image/jpg', 'image/jpeg', 'image/pjpeg',
22 'image/gif',
23 'image/png'
24 ); // array
26 /**
27 * Return paged project files
29 * @param Project $project
30 * @param ProjectFolder $folder
31 * @param boolean $hide_private Don't show private files
32 * @param string $order Order files by name or by posttime (desc)
33 * @param integer $page Current page
34 * @param integer $files_per_page Number of files that will be showed per single page
35 * @param boolean $group_by_order Group files by order field
36 * @return array
38 static function getProjectFiles(Project $project, $folder = null, $hide_private = false, $order = null, $page = null, $files_per_page = null, $group_by_order = false) {
39 if($order == self::ORDER_BY_POSTTIME) {
40 $order_by = '`created_on` DESC';
41 } else {
42 $order_by = '`filename`';
43 } // if
45 // #PAGE# is reserved as a placeholder
46 //if(!($page == '#PAGE#')) {
47 if((integer) $page < 1) $page = 1;
48 if((integer) $files_per_page < 1) $files_per_page = 10;
49 //} // if
51 $folder_ids = array();
52 if(($folder instanceof ProjectFolder) && ($folder->getProjectId() == $project->getId())) {
53 if($hide_private) {
54 $conditions = array('`project_id` = ? AND `folder_id` = ? AND `is_private` = ? AND `is_visible` = ?', $project->getId(), $folder->getId(), false, true);
55 } else {
56 $conditions = array('`project_id` = ? AND `folder_id` = ? AND `is_visible` = ?', $project->getId(), $folder->getId(), true);
57 } // if
58 } else {
59 if($hide_private) {
60 $conditions = array('`project_id` = ? AND `is_private` = ? AND `is_visible` = ?', $project->getId(), false, true);
61 } else {
62 $conditions = array('`project_id` = ? AND `is_visible` = ?', $project->getId(), true);
63 } // if
64 } // if
66 list($files, $pagination) = ProjectFiles::paginate(array(
67 'conditions' => $conditions,
68 'order' => $order_by
69 ), $files_per_page, $page);
71 if($group_by_order) {
72 $grouped_files = array();
73 if(is_array($files) && count($files)) {
74 $today = DateTimeValueLib::now();
75 foreach($files as $file) {
77 $group_by_str = '';
78 if($order == self::ORDER_BY_POSTTIME) {
79 $created_on = $file->getCreatedOn();
80 if($created_on->getYear() == $today->getYear()) {
81 $group_by_str = format_descriptive_date($created_on);
82 } else {
83 $group_by_str = format_date($created_on);
84 } // if
85 } else {
86 $group_by_str = strtoupper(substr_utf($file->getFilename(), 0, 1));
87 } // if
89 if(!isset($grouped_files[$group_by_str]) || !is_array($grouped_files[$group_by_str])) $grouped_files[$group_by_str] = array();
90 $grouped_files[$group_by_str][] = $file;
92 } // foreach
93 } // if
94 $files = is_array($grouped_files) ? $grouped_files : null;
95 } // if
97 return array($files, $pagination);
98 } // getProjectFiles
101 * Orphened files are files that are not part of any folder, but project itself
103 * @param Project $project
104 * @param boolean $show_private
105 * @return null
107 static function getOrphanedFilesByProject(Project $project, $show_private = false) {
108 if($show_private) {
109 $conditions = array('`project_id` =? AND `folder_id` = ?', $project->getId(), 0);
110 } else {
111 $conditions = array('`project_id` =? AND `folder_id` = ? AND `is_private` = ?', $project->getId(), 0, false);
112 } // if
114 return self::findAll(array(
115 'conditions' => $conditions,
116 'order' => '`filename`',
118 } // getOrphanedFilesByProject
121 * Reaturn all project files
123 * @param Project $project
124 * @return array
126 static function getAllFilesByProject(Project $project) {
127 return self::findAll(array(
128 'conditions' => array('`project_id` = ?', $project->getId())
129 )); // findAll
130 } // getAllFilesByProject
133 * Return files by URL. Files will be ordered by filename
135 * @param ProjectFolder $folder
136 * @param boolean $show_private
137 * @return array
139 static function getByFolder(ProjectFolder $folder, $show_private = false) {
140 $project = $folder->getProject();
141 if(!($project instanceof Project)) {
142 return null;
143 } // if
145 if($show_private) {
146 $conditions = array('`project_id` =? AND `folder_id` = ?', $project->getId(), $folder->getId());
147 } else {
148 $conditions = array('`project_id` =? AND `folder_id` = ? AND `is_private` = ?', $project->getId(), $this->getId(), false);
149 } // if
151 return self::findAll(array(
152 'conditions' => $conditions,
153 'order' => '`filename`',
155 } // getByFolder
158 * Return files index page
160 * @param string $order_by
161 * @param integer $page
162 * @return string
164 static function getIndexUrl($order_by = null, $page = null) {
165 if(($order_by <> ProjectFiles::ORDER_BY_NAME) && ($order_by <> ProjectFiles::ORDER_BY_POSTTIME)) {
166 $order_by = ProjectFiles::ORDER_BY_POSTTIME;
167 } // if
169 // #PAGE# is reserved as a placeholder
170 if($page <> '#PAGE#') {
171 $page = (integer) $page > 0 ? (integer) $page : 1;
172 } // if
174 return get_url('files', 'index', array(
175 'active_project' => active_project()->getId(),
176 'order' => $order_by,
177 'page' => $page
178 )); // array
179 } // getIndexUrl
182 * Return important project files
184 * @param Project $project
185 * @param boolean $include_private
186 * @return array
188 static function getImportantProjectFiles(Project $project, $include_private = false) {
189 if($include_private) {
190 $conditions = array('`project_id` = ? AND `is_important` = ?', $project->getId(), true);
191 } else {
192 $conditions = array('`project_id` = ? AND `is_important` = ? AND `is_private` = ?', $project->getId(), true, false);
193 } // if
195 return self::findAll(array(
196 'conditions' => $conditions,
197 'order' => '`created_on`',
199 } // getImportantProjectFiles
202 * Handle files uploaded using helper forms. This function will return array of uploaded
203 * files when finished
205 * @param Project $project
206 * @param string $files_var_prefix If value of this variable is set only elements in $_FILES
207 * with key starting with $files_var_prefix will be handled
208 * @return array
210 static function handleHelperUploads(Project $project, $files_var_prefix = null) {
211 if(!isset($_FILES) || !is_array($_FILES) || !count($_FILES)) {
212 return null; // no files to handle
213 } // if
215 $uploaded_files = array();
216 foreach($_FILES as $uploaded_file_name => $uploaded_file) {
217 if((trim($files_var_prefix) <> '') && !str_starts_with($uploaded_file_name, $files_var_prefix)) {
218 continue;
219 } // if
221 if(!isset($uploaded_file['name']) || !isset($uploaded_file['tmp_name']) || !is_file($uploaded_file['tmp_name'])) {
222 continue;
223 } // if
225 $uploaded_files[$uploaded_file_name] = $uploaded_file;
226 } // foreach
228 if(!count($uploaded_file)) {
229 return null; // no files to handle
230 } // if
232 $result = array(); // we'll put all files here
233 $expiration_time = DateTimeValueLib::now()->advance(1800, false);
235 foreach($uploaded_files as $uploaded_file) {
236 $file = new ProjectFile();
238 $file->setProjectId($project->getId());
239 $file->setFilename($uploaded_file['name']);
240 $file->setIsVisible(false);
241 $file->setExpirationTime($expiration_time);
242 $file->save();
244 $file->handleUploadedFile($uploaded_file); // initial version
246 $result[] = $file;
247 } // foreach
249 return count($result) ? $result : null;
250 } // handleHelperUploads
252 } // ProjectFiles