/[drupal]/contributions/sandbox/jjeff/favorites/favorites.module
ViewVC logotype

Contents of /contributions/sandbox/jjeff/favorites/favorites.module

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


Revision 1.5 - (show annotations) (download) (as text)
Mon Aug 8 16:31:24 2005 UTC (4 years, 3 months ago) by thehunmonkgroup
Branch: MAIN
CVS Tags: HEAD
Changes since 1.4: +1 -1 lines
File MIME type: text/x-php
fixed broken favorites edit page
1 <?php
2
3 function favorites_help($section) {
4 switch ($section) {
5 case 'admin/modules#description':
6 // This description is shown in the listing at admin/modules.
7 return t('User configurable block of favorite links.');
8 }
9 }
10
11 function favorites_menu($may_cache) {
12 if ($may_cache) {
13 $items[] = array('path'=>'admin/settings/favorites', 'title'=>t('favorites'), 'callback'=>'favorites_settings_page', 'access'=>user_access('manage favorites'));
14 } else {
15 $items[] = array('path' => 'user/'. arg(1) .'/favorites', 'title' => strtolower(variable_get('favorites_name', t('Favorites'))), 'callback' => 'favorites_user_form', 'callback arguments' => array(arg(1)), 'access' => user_access('choose favorites'), 'type' => MENU_LOCAL_TASK, 'weight' => 20);
16 }
17 return $items;
18 }
19
20 function favorites_perm() {
21 return array('choose favorites', 'manage favorites');
22 }
23
24 /**
25 * The settings form displayed to users.
26 *
27 */
28
29 function favorites_user_form($uid = NULL) {
30
31 $op = $_POST['op'];
32 $edit = $_POST['edit'];
33 if (!$uid) {
34 global $user;
35 $uid = $user->uid;
36 }
37 else {
38 $user = user_load(array('uid'=>$uid));
39 }
40 switch ($op) {
41
42 //if changes have been submitted, save them
43 case t('Submit'):
44 db_query('DELETE FROM {favorites_user} WHERE uid=%d', $user->uid);
45 if (is_array($edit['favorites'])) {
46 foreach ($edit['favorites'] as $key => $value) {
47 // we add 1 to the value so that we can tell the difference between 0 and unset
48 db_query('INSERT INTO {favorites_user} (uid, fid, status) VALUES (%d, %d, %d)', $user->uid, $key, $value+1);
49 }
50 }
51 drupal_set_message(variable_get('favorites_name', t('Favorites')).' '.t('settings updated'));
52 break;
53 }
54
55 $userfaves = favorites_get_user_faves($uid);
56 $favorites = favorites_get_favorites();
57 foreach ($favorites as $favorite) {
58 if ($favorite->def != 2) {
59 // if the default is 2, then it is set to persistent and the user shouldn't be able to change
60
61 // 1 has been added to the status values so that we know the difference between 0 and unset items...
62 $checked = $userfaves[$favorite->fid] == 2 ? TRUE : ($userfaves[$favorite->fid] == 0 ? $favorite->def : FALSE );
63
64 $form .= form_checkbox(str_replace('%name', $user->name, $favorite->name), 'favorites]['.$favorite->fid , 1, $checked, $favorite->description);
65 }
66 else {
67 $form .= form_item('', str_replace('%name', $user->name, $favorite->name).t(' (always on)'), $favorite->description);
68 }
69 }
70 $group = form_group(t('Select').' '.variable_get('favorites_name', t('Favorites')), $form, t('Select the links you would like to appear.'), $attributes = NULL);
71 $group .= form_submit(t('Submit'));
72 print theme('page', form($group));
73 }
74
75
76 /**
77 * Return an array where keys are favorite ids and values are user's setting
78 *
79 */
80
81 function favorites_get_user_faves($uid) {
82 if (!$uid) {
83 global $user;
84 $uid = $user->uid;
85 }
86 $userfaves = array();
87
88 $sql = db_query('SELECT fid, status FROM {favorites_user} WHERE uid=%d', $uid);
89 while ($row = db_fetch_object($sql)) {
90 $userfaves[$row->fid] = $row->status;
91 }
92 return $userfaves;
93 }
94
95 /**
96 * Return an array of objects with the following properties:
97 * fid, name, description, link, def, weight
98 *
99 */
100
101 function favorites_get_favorites() {
102 $favorites = array();
103 $sql = db_query('SELECT * FROM {favorites_locations} ORDER BY weight, name');
104 while ($row = db_fetch_object($sql)) {
105 $favorites[] = $row;
106 }
107 return $favorites;
108 }
109
110 function favorites_settings_page($op = NULL, $fid = NULL) {
111 if ($_POST['op'] == 'Submit') {
112 favorites_save_favorite($_POST['edit']);
113 }
114 switch ($op) {
115 case 'edit':
116 if (is_numeric($fid)) {
117 $output = favorites_edit_form($fid);
118 break;
119 }
120 case 'delete' :
121 if (is_numeric($fid)) {
122 $output = favorites_delete_favorite($fid);
123 break;
124 }
125 default:
126 $output = form_textfield(t('Favorites name'), 'favorites_name', variable_get('favorites_name', t('Favorites')), 30, 30, t('If desired, the name \'Favorites\' can be overridden with a custom name here'), $attributes = NULL, $required = FALSE);
127 $favorites = favorites_get_favorites();
128 $header = array(t('link name/description'), t('destination'), t('default'), t('operations'));
129 foreach ($favorites as $favorite) {
130 $tablerow[$favorite->fid]['name'] = $favorite->name.'<br/><span style="font-size:xx-small">'.$favorite->description.'</span>';
131 $tablerow[$favorite->fid]['link'] = $favorite->link;
132 switch ($favorite->def) {
133 case 0:
134 $def = t('hidden');
135 break;
136 case 1:
137 $def = t('shown');
138 break;
139 case 2:
140 $def = t('persistent');
141 break;
142 }
143 $tablerow[$favorite->fid]['default'] = $def;
144 $tablerow[$favorite->fid]['ops'] = l(t('edit'), 'admin/settings/favorites/edit/'.$favorite->fid).' '.l(t('delete'), 'admin/settings/favorites/delete/'.$favorite->fid);
145 }
146 $output .= theme('table', $header, $tablerow, array('style'=>'width:100%'));
147 $output .= "<br/><br/>";
148 $output .= form_group(t('Add another link'), favorites_edit_form());
149 }
150 $output = form($output);
151 print theme('page', $output);
152 }
153
154 function favorites_edit_form($fid = NULL) {
155 if (is_numeric($fid)) {
156 $edit = db_fetch_object(db_query('SELECT * FROM {favorites_locations} WHERE fid = %d', $fid));
157 if (is_numeric($edit->fid)) {
158 $form .= form_hidden('fid', $edit->fid);
159 }
160 }
161 $form .= form_textfield(t('Title'), 'name', $edit->name, 40, 100, t('Text displayed for the favorite link.<br/>Use %name for the username.<br/>Example: %name\'s profile'), NULL, TRUE);
162 $form .= form_textfield(t('Description'), 'description', $edit->description, 40, '', t('Optional: This text will be displayed by each checkbox in the user profile.<br/>It also appears as a tooltip when mousing over the link.'));
163 $form .= form_textfield(t('Link'), 'link', $edit->link, 40, 100, t('URL of the link.<br/>%uid is the user\'s id.<br/>Example: user/%uid<br/>Leave blank to create nonlinking text'), NULL, FALSE);
164 $radios = form_radio(t('Default Hidden'), 'def', 0, $edit->def==0, t('New users will not see this item, but they will have the option to turn it on.'));
165 $radios .= form_radio(t('Default Visible'), 'def', 1, $edit->def==1, t('New users will see this item in the favorites block. They will have the option to turn it off.'));
166 $radios .= form_radio(t('Persistent'), 'def', 2, $edit->def==2, t('This item will appear always for all users. Users do not have the option to turn it off.'));
167 $form .= form_group(t('Configuration'), $radios);
168 //$form .= form_checkbox(t('Default Setting'), 'def', 1, $edit->def, t("Before the user makes any customizations, should this item be visible in the favorites block?"));
169 $form .= form_weight(t('Weight'), 'weight', $edit->weight, 10, t('Lighter weighted items appear at the beginning of lists. Heavier items go to the end.'));
170 $form .= form_submit('Submit');
171
172 return $form;
173 }
174
175 function favorites_save_favorite($edit) {
176 $edit = array2object($edit);
177 variable_set('favorites_name', $edit->favorites_name ? $edit->favorites_name : t('Favorites'));
178 if ($edit->name) {
179 if (is_numeric($edit->fid)) {
180 db_query('DELETE FROM {favorites_locations} WHERE fid = %d', $edit->fid);
181 }
182 else {
183 $edit->fid = db_next_id('favorites_locations');
184 }
185 $result = db_query("INSERT INTO {favorites_locations} (fid, name, description, link, def, weight) VALUES (%d, '%s', '%s', '%s', %d, %d)", $edit->fid, $edit->name, $edit->description, $edit->link, $edit->def, $edit->weight);
186 if (!$result) {
187 drupal_set_message(t('There was a problem saving the favorite to the database.'));
188 drupal_goto('admin/settings/favorites');
189 return $result;
190 }
191 }
192 drupal_set_message(t('Favorite settings saved'));
193 drupal_goto('admin/settings/favorites');
194 return $result;
195 }
196
197 function favorites_delete_favorite($fid) {
198 if (!$_POST['edit']['confirm']) {
199 return theme_confirm(t('Are you sure you want to delete this favorite?'), 'admin/settings/favorites/delete/'.$fid);
200 }
201 else {
202 $result = db_query("DELETE FROM {favorites_locations} WHERE fid = %d", $fid);
203 if ($result) {
204 drupal_set_message(t('Favorite deleted.'));
205 }
206 else {
207 drupal_set_message(t('There was a problem deleting the favorite to the database.'));
208 }
209 unset($_POST);
210 drupal_goto('admin/settings/favorites');
211 return $result;
212 }
213 }
214
215
216 function favorites_block($op = 'list', $delta = 0) {
217 // The $op parameter determines what piece of information is being requested.
218 if ($op == 'list') {
219 // If $op is "list", we just need to return a list of block descriptions. This
220 // is used to provide a list of possible blocks to the administrator.
221 $blocks[0]['info'] = t('Favorite Links');
222 return $blocks;
223 }
224 else if ($op == 'view') {
225 // If $op is "view", then we need to generate the block for display purposes.
226 // The $delta parameter tells us which block is being requested.
227 switch ($delta) {
228 case 0:
229 // The subject is displayed at the top of the block. Note that it should
230 // be passed through t() for translation.
231 $block['subject'] = variable_get('favorites_name', t('Favorites'));
232 // The content of the block is typically generated by calling a custom
233 // function.
234 $block['content'] = favorites_contents();
235 break;
236 }
237 return $block;
238 }
239 }
240
241
242 /**
243 * A block content function.
244 */
245 function favorites_contents() {
246 global $user;
247 $userfaves = favorites_get_user_faves($user->uid);
248 $favorites = favorites_get_favorites();
249 foreach ($favorites as $favorite) {
250 $active = $userfaves[$favorite->fid] == 2 ? TRUE : ($userfaves[$favorite->fid] == 0 ? $favorite->def : FALSE );
251 if ($active) {
252 if ($favorite->link) {
253 $links[] = l(str_replace('%name', $user->name, $favorite->name), url(str_replace('%uid', $user->uid, $favorite->link)), array('title'=>$favorite->description), NULL, NULL, FALSE, TRUE);
254 }
255 else {
256 $links[] = str_replace('%name', $user->name, $favorite->name);
257 }
258 }
259 }
260 return theme('favorites_block', $links);
261 }
262
263 function theme_favorites_block($links) {
264 // How would you like the links to appear?
265 return $links ? "<div>".implode("</div>\n<div>", $links)."</div>" : '';
266 }
267
268
269 ?>

  ViewVC Help
Powered by ViewVC 1.1.2