/[drupal]/contributions/modules/mmedia/mmedia.module
ViewVC logotype

Contents of /contributions/modules/mmedia/mmedia.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.3 - (show annotations) (download) (as text)
Thu Mar 19 13:31:53 2009 UTC (8 months, 1 week ago) by rhys
Branch: MAIN
CVS Tags: HEAD
Changes since 1.2: +21 -20 lines
File MIME type: text/x-php
General fixups
1 <?php
2 // $Id: mmedia.module,v 1.1.2.4 2009/02/02 12:13:27 casey Exp $
3
4 /**
5 * @file
6 * Media Management Module that allows the creation and management of media as files.
7 */
8
9 // For specific media
10 define('MEDIA_PATH', trim(variable_get('mmedia_url', 'media'), '/'));
11 define('MEDIA_ARG', count(explode('/', MMEDIA_PATH)) - 1);
12
13 // For specific folders
14 define('FOLDER_PATH', trim(variable_get('mmedia_folder_url', 'folder'), '/'));
15 define('FOLDER_ARG', count(explode('/', FOLDER_PATH)) - 1);
16
17 // Install the API immediately
18 $path = drupal_get_path('module', 'mmedia');
19 require_once $path .'/API/hooks.inc';
20 require_once $path .'/API/media.inc';
21 require_once $path .'/API/file.inc';
22 require_once $path .'/API/folder.inc';
23 require_once $path .'/API/form.inc';
24
25 /**
26 * Implementation of hook_init().
27 */
28 function mmedia_init() {
29 $path = drupal_get_path('module', 'mmedia');
30
31 // Load module support additions
32 $files = drupal_system_listing('.*\.inc$', $path .'/modules', 'name', 0);
33 foreach ($files as $module => $file) {
34 if (module_exists($module)) {
35 require_once($file->filename);
36 }
37 }
38 }
39
40 /**
41 * Implementation of hook_perm().
42 */
43 function mmedia_perm() {
44 $perms = array(
45 'administer media',
46 'manage media',
47 'manage own media',
48 'access media',
49 'manage media folder',
50 'manage own media folder',
51 'access media folder',
52 'upload media',
53 );
54
55 return $perms;
56 }
57
58 /**
59 * Implementation of hook_theme().
60 */
61 function mmedia_theme() {
62 return array(
63 'media_view' => array(
64 'template' => 'media-view',
65 'arguments' => array('display' => '', 'filename' => NULL, 'media' => NULL),
66 ),
67 );
68 }
69
70 /**
71 * Implementation of hook_menu().
72 */
73 function mmedia_menu() {
74 include drupal_get_path('module', 'mmedia') .'/media.menu.inc';
75 return $items;
76 }
77
78 /**
79 * Implementation of hook_mapi_workspaces().
80 */
81 function mmedia_mapi_workspaces() {
82 // check that the files exist
83 $base = file_directory_path() .'/'. variable_get('mmedia_dir_public', 'media');
84 $private = variable_get('mmedia_dir_private', '../media');
85 $temp = file_directory_path() .'/'. variable_get('mmedia_dir_temp', 'temp');
86
87 // check that base directory exists
88 if (!is_dir($base) && !mapi_directory_check($base, array('create' => TRUE))) {
89 mapi_error('Unable to create directory %dir.', array('%dir' => $base));
90 }
91
92 // check that the temporary directory exists
93 if (!is_dir($temp) && !mapi_directory_check($temp, array('create' => TRUE))) {
94 mapi_error('Unable to create directory %dir.', array('%dir' => $temp));
95 }
96
97 $dirs = array('base' => $base, 'temp' => $temp);
98
99 // check the private directory
100 if (variable_get('mmedia_private', FALSE)) {
101 $private = variable_get('mmedia_dir_private', '../media');
102 if (!is_dir($private) && !is_writeable($private)) {
103 mapi_error('Unable to locate a writeable directory %dir.', array('%dir' => $private));
104 }
105 $dirs['private'] = $private;
106 }
107
108 return $dirs;
109 }
110
111 /**
112 * Implementation of hook_media().
113 */
114 function mmedia_media($op, &$media, $data) {
115 switch ($op) {
116 case 'move':
117 // in the case of movement, we move the generated derivatives
118 if (module_exists('mapi_derivatives')) {
119 mapi_generated_moveall($media->old_filename, media_filename($media), $media->path, 'mmedia');
120 }
121 break;
122 case 'delete':
123 // we make sure the generated derivatives are deleted for a media delete
124 if (module_exists('mapi_derivatives')) {
125 mapi_generated_flush(media_filename($media));
126 }
127 break;
128 }
129 }
130
131 /**
132 * Implementation of hook_media_metadata().
133 */
134 function mmedia_media_metadata($op, &$metadata, $data) {
135 include drupal_get_path('module', 'mmedia') .'/media.metadata.inc';
136 return isset($form) ? $form : array();
137 }
138
139 /**
140 * Implementation of hook_mapi_extensions()
141 */
142 function mmedia_mapi_extensions($op, $filename) {
143 static $cache = array();
144
145 switch ($op) {
146 case 'filename dimensions':
147 // cache the file dimensions request once
148 if (!array_key_exists($filename, $cache)) {
149 $ext = mapi_file_ext($filename);
150 $path = mapi_file_path($filename);
151 $name = mapi_file_name($filename);
152 $mid = db_result(db_query("SELECT mid FROM {media} WHERE path = '%s' AND name = '%s' AND ext = '%s'", $path, $name, $ext));
153 $cache[$filename] = $mid;
154 }
155 // only return those dimensions that actually exist
156 if ($cache[$filename]) {
157 $media = media_load($cache[$filename]);
158 return $media->dimensions;
159 }
160 break;
161 case 'filename metadata':
162 // cache the file dimensions request once
163 if (!array_key_exists($filename, $cache)) {
164 $ext = mapi_file_ext($filename);
165 $path = mapi_file_path($filename);
166 $name = mapi_file_name($filename);
167 $mid = db_result(db_query("SELECT mid FROM {media} WHERE path = '%s' AND name = '%s' AND ext = '%s'", $path, $name, $ext));
168 $cache[$filename] = $mid;
169 }
170 // only return those dimensions that actually exist
171 if ($cache[$filename]) {
172 $meta = media_metadata_load($cache[$filename]);
173 return $meta;
174 }
175 break;
176 }
177 }
178
179 /**
180 * Menu Helper function title
181 */
182 function _mmedia_title($media) {
183 return $media->title;
184 }
185
186 /**
187 * Menu Helper function folder title
188 */
189 function _mmedia_folder_title($folder) {
190 return $folder->name;
191 }
192
193 /**
194 * Set the current breadcrumb to represent where the media item is located.
195 */
196 function _mmedia_set_breadcrumb($parent) {
197 // load the entire sequence of folders for the parent
198 $list = array(
199 l(t('Home'), NULL),
200 l(t('Folders'), FOLDER_PATH),
201 );
202
203 $parents = array_filter(explode('/', $parent->lineage));
204 $paths = array_filter(explode('/', $parent->path));
205 while ($fid = array_shift($parents)) {
206 $name = array_shift($paths);
207 $list[] = l($name, FOLDER_PATH .'/'. $fid);
208 }
209 if ($parent->fid) {
210 $list[] = l($parent->name, FOLDER_PATH .'/'. $parent->fid);
211 }
212
213 return $list;
214 }
215
216 /**
217 * Determines access capability
218 */
219 function _mmedia_access($media, $type) {
220 if (user_access('administer media')) {
221 return TRUE;
222 }
223
224 global $user;
225
226 switch ($type) {
227 case 'access':
228 return user_access('access media');
229 break;
230 case 'manage':
231 if (user_access('manage own media') && $media->uid == $user->uid) {
232 return TRUE;
233 }
234 return user_access('manage media');
235 break;
236 case 'upload':
237 return user_access('upload media');
238 break;
239 }
240 }
241
242 /**
243 * Determines access capability
244 */
245 function _mmedia_folder_access($folder, $type) {
246 if (user_access('administer media')) {
247 return TRUE;
248 }
249
250 global $user;
251
252 switch ($type) {
253 case 'access':
254 return user_access('access media');
255 break;
256 case 'manage':
257 if (user_access('manage own media folder') && $folder->uid == $user->uid) {
258 return TRUE;
259 }
260 return user_access('manage media folder');
261 break;
262 }
263 }

  ViewVC Help
Powered by ViewVC 1.1.2