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

Contents of /contributions/modules/user_profile_theme/user_profile_theme.module

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


Revision 1.4 - (show annotations) (download) (as text)
Sun Aug 16 07:10:47 2009 UTC (3 months, 1 week ago) by davyvandenbremt
Branch: MAIN
CVS Tags: HEAD
Changes since 1.3: +47 -47 lines
File MIME type: text/x-php
#211212 fixing 2 typos
1 <?php
2
3 /**
4 * @file
5 * User Profile Theme module, enables custom user profile themes.
6 *
7 * Provides each user the possibility to apply a different CSS file to
8 * his/her profile page.
9 *
10 * @todo Add feature: User can upload images for backgrounds etc. A variable is
11 * then provide to represent the images directory.
12 *
13 * @todo Add feature: Admin can specify where CSS files and images are put.
14 *
15 * @todo Add .po files for Dutch, French, ...
16 *
17 * @todo Write readme.txt.
18 *
19 * @todo Add license file.
20 *
21 * @todo Write install.txt explaining you need to add class="profile" to you body tag.
22 *
23 * @todo Implement hook_help().
24 *
25 * @todo Write handbook page http://www.drupal.org/handbook/config/contribmodules
26 *
27 * @todo Convert to drupal 6 (and 4 - but don't add more features to 4 in the future)
28 *
29 * @todo For help... refer to http://drupal.org/node/32077
30 */
31
32 /**
33 * Implementation of hook_help().
34 */
35 /*function user_profile_theme_help($section) {
36 switch ($section) {
37 case 'admin/modules#description':
38 return t('User profile theme');
39 }
40 }*/
41
42 /**
43 * Implementation of hook_perm().
44 */
45 function user_profile_theme_perm() {
46 return array(
47 'administer user profile theme',
48 'have theme',
49 'edit other users\' theme',
50 'edit own theme',
51 'view theme'
52 );
53 }
54
55 /**
56 * Implementation of hook_requirements().
57 */
58 function user_profile_theme_requirements($phase) {
59 $requirements = array();
60
61 $t = get_t();
62
63 if ($phase == 'runtime') {
64
65 // Test if files directory is writable.
66 if (!is_writable(file_create_path())) {
67 $requirements['files_writable'] = array(
68 'title' => $t('The directory files is not writable'),
69 'value' => $t('File directory is not writable. User Profile Theme CSS files will not be written.'),
70 'severity' => REQUIREMENT_ERROR,
71 'description' => $t('User Profile Theme requires your files directory to be written. You can find out where your files directory resides in the <a href="' . base_path() . 'admin/settings/file-system">file_system_settings</a>'),
72 );
73 }
74 }
75
76 return $requirements;
77 }
78
79 /**
80 * Implementation of hook_menu().
81 */
82 function user_profile_theme_menu($may_cache) {
83 $items = array();
84
85 if ($may_cache) {
86 $items[] = array(
87 'path' => 'admin/settings/user_profile_theme',
88 'title' => t('User Profile Theme'),
89 'description' => t('Administer user profile theme settings.'),
90 'callback' => 'drupal_get_form',
91 'callback arguments' => array('user_profile_theme_admin_settings'),
92 'access' => user_access('administer user profile theme'),
93 'type' => MENU_NORMAL_ITEM,
94 );
95 }
96 return $items;
97 }
98
99 /*
100 * Module settings form.
101 */
102 function user_profile_theme_admin_settings() {
103 $form = array();
104
105 $form['user_profile_theme_enabled'] = array(
106 '#type' => 'checkbox',
107 '#title' => t('Enabled'),
108 '#default_value' => variable_get('user_profile_theme_enabled', 0),
109 '#description' => t('Should a user be able to specify a custom CSS file for his user profile page?'),
110 );
111
112 return system_settings_form($form);
113 }
114
115 /**
116 * Implementation of hook_user().
117 *
118 * Provides signature customization for the user's comments.
119 */
120 function user_profile_theme_user($type, $edit, &$user, $category = NULL) {
121
122 if (variable_get('user_profile_theme_enabled', 0) == 1 ) {
123
124 /*
125 * User profile edit form.
126 */
127 if ($type == 'form' && $category == 'account') {
128
129 if (user_access('edit other users\' theme') || (user_access('have theme') && user_access('edit own theme'))) {
130
131 $filename = _user_profile_theme_get_filename($user->uid);
132
133 $contents = _user_profile_theme_read_file($filename);
134
135 $form = array();
136
137 $form['user_profile_theme'] = array(
138 '#type' => 'fieldset',
139 '#title' => t('User profile theme'),
140 '#collapsible' => TRUE,
141 '#collapsed' => TRUE,
142 '#weight' => 10
143 );
144
145 $form['user_profile_theme']['user_profile_theme_css'] = array(
146 '#type' => 'textarea',
147 '#title' => t('Theme'),
148 '#default_value' => $contents,
149 '#description' => t('Paste the CSS code of your theme here.')
150 );
151
152 return $form;
153
154 }
155
156 }
157
158 /*
159 * View user profile.
160 */
161 elseif ($type == 'view') {
162
163 // We add the CSS file to the header.
164 if(user_access('view theme') && user_access('have theme', $user)) {
165 drupal_add_css(_user_profile_theme_get_filename($user->uid), 'module', 'screen');
166 }
167
168 }
169
170 /*
171 * Submit user profile edit form.
172 */
173 elseif ($type == 'submit') {
174
175 if (user_access('edit other users\' theme') || (user_access('have theme') && user_access('edit own theme'))) {
176
177 if(!empty($edit['user_profile_theme_css'])) {
178 $content = $edit['user_profile_theme_css'];
179 file_save_data($content,_user_profile_theme_get_filename($user->uid), FILE_EXISTS_REPLACE);
180 }
181 else {
182 $filename = _user_profile_theme_get_filename($user->uid);
183 file_delete($filename);
184 }
185
186 }
187
188 }
189
190 }
191
192 }
193
194 /**
195 * Get file name of the user profile theme CSS file.
196 *
197 * If the file does not exist it is created.
198 *
199 * @param $uid
200 * An integer representing the user id for who the CSS file is requested.
201 * @return
202 * Path to user profile theme CSS file.
203 */
204 function _user_profile_theme_get_filename($uid) {
205 $dir = file_create_path('user-profile-themes');
206 file_check_directory($dir, FILE_CREATE_DIRECTORY);
207 return $dir . '/profile-' . $uid . '.css';
208 }
209
210 /**
211 * Read the content of a file into a string.
212 *
213 * @param $filename
214 * A string representing the filename of the file to be read.
215 * @return
216 * String representing the content of the file.
217 */
218 function _user_profile_theme_read_file($filename)
219 {
220 $content = '';
221 if (file_exists($filename)) {
222 if ($handle = fopen($filename, "r")) {
223 if (filesize($filename) > 0) {
224 $content = fread($handle, filesize($filename));
225 }
226 fclose($handle);
227 } else {
228 drupal_set_message(t('An error occured while trying to load the user profile theme file !filename', array('!filename', $filename)), 'error');
229 }
230 }
231 return $content;
232 }
233
234 ?>

  ViewVC Help
Powered by ViewVC 1.1.2