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

Contents of /contributions/modules/sitenotes/sitenotes.module

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


Revision 1.11 - (show annotations) (download) (as text)
Wed May 7 03:53:34 2008 UTC (18 months, 3 weeks ago) by nancyw
Branch: MAIN
CVS Tags: DRUPAL-5--1-3, HEAD
Branch point for: DRUPAL-5
Changes since 1.10: +2 -18 lines
File MIME type: text/x-php
#255474 by NancyDru for okeedoak - use node_content_form.
1 <?php
2 // $Id: sitenotes.module,v 1.10 2008/02/14 17:05:08 nancyw Exp $
3
4 /**
5 * @file
6 * Creates site notes node type for admins and allowed roles.
7 */
8
9 /**
10 * Implementation of hook_node_info().
11 * Min_word count is set just to make sure something is there.
12 * The type is locked because we use it in queries in this module. (default any way)
13 */
14 function sitenotes_node_info() {
15 return array(
16 'sitenotes' => array(
17 'name' => t('Site Note'),
18 'module' => 'sitenotes',
19 'description' => t('Create site notes for admin user(s).'),
20 'has_title' => TRUE,
21 'has_body' => TRUE,
22 'min_word_count' => 5,
23 'locked' => TRUE,
24 )
25 );
26 }
27
28 /**
29 * Implementation of hook_perm().
30 */
31 function sitenotes_perm() {
32 return array('access site notes');
33 }
34
35 /**
36 * Implementation of hook_access().
37 */
38 function sitenotes_access($op, $node) {
39 global $user;
40
41 if ($op == 'create' || $op == 'update' || $op == 'delete' || $op == 'view') {
42 return user_access('access site notes');
43 }
44 }
45
46 /**
47 * Implementation of hook_form_alter().
48 * Disables the 'promote to front' option.
49 * Redirects the user to the view function.
50 * Remove sitenotes content from the advanced search options to prevent it being found.
51 * - Copied from: http://drupal.org/node/84955#comment-162473
52 */
53 function sitenotes_form_alter($form_id, &$form) {
54 switch ($form_id) {
55 case 'sitenotes_node_form':
56 $form['options']['promote']['#value'] = 0;
57 $form['#redirect'] = 'admin/build/sitenotes';
58 break;
59
60 // Check for "advanced search form".
61 // If we don't allow search or the user doesn't have access, then turn off content type in form.
62 case 'search_form':
63 if (variable_get('sitenotes_search', false) == false || user_access('access site notes') == false) {
64 unset($form['advanced']['type']['#options']['sitenotes']);
65 }
66 break;
67 }
68 }
69
70 /**
71 * Implementation of hook_help().
72 * The first case adds a little help text.
73 * The second case adds some submission guidelines on the create content page.
74 */
75 function sitenotes_help($section) {
76 switch ($section) {
77
78 case 'admin/help#sitenotes':
79 $output = '<p>'. t('The site designer can create site notes for later reference. Usually only super users can read them, unless other roles are given persmissions.') .'</p>';
80 return $output;
81
82 case 'node/add/sitenotes':
83 $output = t('<h3>Remember: you should insert <b>&lt;!--Break--&gt;</b> after the text you want to show in the teaser.</h3>');
84 return $output;
85 }
86 }
87
88 /**
89 * Implementation of hook_form().
90 * Creates the form for adding the sitnenotes content type.
91 */
92 function sitenotes_form(&$node) {
93 $type = node_get_types('type', $node);
94
95 $form = node_content_form($node);
96
97 $form['weight'] = array(
98 '#type' => 'weight',
99 '#title' => t('Weight'),
100 '#default_value' => $node->weight,
101 '#delta' => 20,
102 '#description' => t('Optional. The heavier items will sink and the lighter items will be positioned nearer the top.'),
103 );
104
105 return $form;
106 }
107
108 /**
109 * Implementation of hook_menu().
110 * Add a menu item to the Administer >> Site building menu for displaying the sitenotes.
111 */
112 function sitenotes_menu($may_cache) {
113 $items = array();
114
115 if ($may_cache) {
116 $items[] = array(
117 'path' => 'node/add/sitenotes',
118 'title' => t('Site notes'),
119 'access' => user_access('access site notes'),
120 );
121 $items[] = array(
122 'path' => 'admin/settings/sitenotes',
123 'title' => t('Site notes settings'),
124 'description' => t('Control the Site Notes module.'),
125 'callback' => 'drupal_get_form',
126 'callback arguments' => array('sitenotes_settings_form'),
127 'access' => user_access('access site notes'),
128 );
129 $items[] = array(
130 'path' => 'admin/build/sitenotes',
131 'title' => t('Site notes display'),
132 'description' => t('Show all site notes'),
133 'callback' => 'sitenotes_list',
134 'access' => user_access('access site notes'),
135 );
136 }
137 return $items;
138 }
139
140 /**
141 * Implementation of hook_form().
142 * Allows the admin to set whether or not to be able to search the sitenotes content.
143 */
144 function sitenotes_settings_form() {
145 $form['sitenotes_search'] = array(
146 '#type' => 'checkbox',
147 '#title' => t('Allow search for authorized users?'),
148 '#description' => t("Indexes Site Notes for searching. Only users with 'access site notes' permission will be able to see the results."),
149 '#default_value' => variable_get('sitenotes_search', FALSE),
150 );
151
152 $form['update']['attach'] = array(
153 '#type' => 'submit',
154 '#value' => t('Update'),
155 '#weight' => 3
156 );
157
158 return $form;
159 }
160
161 /**
162 * Implementation of hook_form_submit().
163 * Saves the sitenotes search option.
164 */
165 function sitenotes_settings_form_submit($form_id, $form_values) {
166 variable_set('sitenotes_search', $form_values['sitenotes_search']);
167 }
168
169 /**
170 * Implementation of hook_list().
171 * Called from the Administer >> Site building menu to retrieve the sitenotes teasers.
172 * Note the use of styling classes for individual customization.
173 */
174 function sitenotes_list() {
175 $sql = "SELECT n.nid FROM {node} n WHERE type='sitenotes' AND status=1 ORDER BY n.sticky DESC, n.created DESC";
176 $result = pager_query(db_rewrite_sql($sql), 10);
177 $output = '>> '. l(t('Add a new Site Note'), 'node/add/sitenotes') ."\n";
178 $output .= '<div class="sitenotes-list">'."\n";
179 while ($notes = db_fetch_object($result)) {
180 $node = node_load($notes->nid);
181 if ($node->sticky) {
182 $output .= '<div class="sticky">';
183 }
184 if (strlen($node->teaser) == strlen($node->body)) {
185 $read = "";
186 }
187 else {
188 $read = '<p class="sitenotes-readmore">'. l('read more...', 'node/'. $node->nid) .'</p>';
189 }
190 $output .= '<h2 class="sitesnotes-title">'. l($node->title, 'node/'. $node->nid) ."\n"
191 .'</h2><div class="sitenotes-teaser">'. $node->teaser ."</div>\n". $read;
192 if ($node->sticky) {
193 $output .= "</div>\n";
194 }
195 }
196 $output .= "</div>\n". theme('pager', NULL, 10);
197 return $output;
198 }
199
200 /**
201 * Implementation of hook_view().
202 * List all the sitenotes and provide breadcrumbs on the display.
203 */
204 function sitenotes_view($node, $teaser = FALSE, $page = FALSE) {
205 if (user_access('access site notes')) {
206 $node = node_prepare($node, $teaser);
207 $node->content['note'] = array(
208 '#value' => theme('sitenotes_note', $node),
209 '#weight' => 1,
210 );
211
212 if ($page) {
213 $breadcrumb = array();
214 $breadcrumb[] = array('path' => 'admin/build/sitenotes', 'title' => "Site Notes");
215 $breadcrumb[] = array('path' => 'node/'. $node->nid);
216 menu_set_location($breadcrumb);
217 }
218 return $node;
219 } /* end if user access */
220 else { return NULL; }
221 }
222
223 /**
224 * Implementation of hook_nodeapi
225 * On submit, encode the weight in the sticky field.
226 * On load, decode the weight from the sticky field.
227 */
228 function sitenotes_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
229 // Only do my content type.
230 if ($node->type != 'sitenotes') {
231 return;
232 }
233
234 switch ($op) {
235 case 'submit':
236 // non-weighted nodes have a weight of zero
237 if (is_null($node->weight)) {
238 $node->weight = 0;
239 }
240
241 // here we're 'encoding' weight into the sticky value for the database
242 // stickiness is the inverse of weight
243 // - stickiness is sorted DESC while weight is sorted ASC so we invert
244 // the weight before saving... if sticky box is checked, add 100 to
245 // weight unweighted sticky nodes will have a value of 100
246 if ($node->sticky) {
247 $node->sticky = 100 - $node->weight;
248 }
249 // unweighted non-sticky nodes will have a value of -100
250 else {
251 $node->sticky = -100 - $node->weight;
252 }
253
254 break;
255
256 case 'load':
257 $sticky = $node->sticky; /* save value */
258 $node->sticky = ($sticky > 0) ? 1 : 0;
259 $node->weight = ($sticky > 0) ? 100 - $sticky : -100 - $sticky;
260 break;
261 }
262 }
263
264 /**
265 * Implementation of hook_update_index.
266 * Remove sitenotes content from the search index to prevent it being found,
267 * if we are not allowing searching.
268 * Copied from: http://drupal.org/node/84955#comment-162473
269 */
270 function sitenotes_update_index() {
271 if (function_exists('search_wipe') && !variable_get('sitenotes_search', FALSE)) {
272 $result = db_query("SELECT nid FROM {node} WHERE type = 'sitenotes'");
273 while ($data = db_fetch_object($result)) {
274 search_wipe($data->nid, 'node');
275 }
276 }
277 }
278
279 /**
280 * Implementation of hook_block.
281 * Show recent sitenotes content.
282 */
283 function sitenotes_block($op = 'list', $delta = 0, $edit = array()) {
284 // Only authorized users can do this.
285 if (user_access('access site notes')) {
286 switch ($op) {
287 case 'list':
288 $blocks[0]['info'] = t('Recent Site Notes');
289 return $blocks;
290 break;
291
292 case 'configure':
293 switch ($delta) {
294 case 0:
295 // 1 day, 2 days, 3 days, 4 days, 5 days, 6 days, 1 week, 2 weeks, 3 weeks, 4 weeks,
296 // 6 weeks, 8 weeks, 12 weeks, 16 weeks, 26 weeks, 52 weeks
297 $how_recent = drupal_map_assoc(array(0, 86400, 172800, 259200, 345600, 432000, 518400, 604800, 1209600, 1814400, 2419200, 3628800, 4838400, 7257600, 9676800, 15724800, 31449600), 'format_interval');
298 $how_recent['0'] = t('All');
299
300 $form['sitenotes_block_recent_show_author'] = array('#type' => 'checkbox',
301 '#title' => t('Show author?'),
302 '#default_value' => variable_get('sitenotes_block_show_author', FALSE),
303 '#options' => $fields,
304 '#description' => t("Show the author's user id?"),
305 );
306
307 $form['sitenotes_block_recent_show_when'] = array('#type' => 'checkbox',
308 '#title' => t('Show when?'),
309 '#default_value' => variable_get('sitenotes_block_show_when', FALSE),
310 '#options' => $fields,
311 '#description' => t("Show the creation/update date?"),
312 );
313
314 $form['sitenotes_block_recent_range'] = array('#type' => 'select',
315 '#title' => t('How recent?'),
316 '#default_value' => variable_get('sitenotes_block_recent_range', 0),
317 '#options' => $how_recent,
318 '#description' => t("How long since it was created/updated?"),
319 );
320
321 $form['sitenotes_block_how_many'] = array('#type' => 'textfield',
322 '#title' => t('How many?'),
323 '#default_value' => variable_get('sitenotes_block_how_many', 5),
324 '#size' => 8,
325 '#description' => t("How many do you want shown?"),
326 );
327
328 return $form;
329 break;
330 } /* end switch configure delta */
331 break;
332
333 case 'save':
334 switch ($delta) {
335 case 0:
336 variable_set('sitenotes_block_0',
337 array('show_author' => $edit['sitenotes_block_recent_show_author'],
338 'show_when' => $edit['sitenotes_block_recent_show_when'],
339 'recent_range' => $edit['sitenotes_block_recent_range'],
340 'how_many' => $edit['sitenotes_block_how_many'])
341 );
342 break;
343 } /* end switch save delta */
344 break;
345
346 case 'view':
347 switch ($delta) {
348 case 0:
349 $block['subject'] = t('Recent Site Notes');
350 $default = array('show_author' => FALSE,
351 'show_when' => FALSE,
352 'recent_range' => 0, 'how_many' => 5
353 );
354
355 $sitenotes_block_0 = variable_get('sitenotes_block_0', $default);
356 $show_author = $sitenotes_block_0['show_author'];
357 $show_when = $sitenotes_block_0['show_when'];
358 $range = $sitenotes_block_0['recent_range'];
359 $how_many = $sitenotes_block_0['how_many'];
360
361 $sql = 'SELECT n.nid, n.title';
362 $sql .= $show_author ? ', n.uid' : NULL;
363 $sql .= $show_when || $range ? ', n.changed' : NULL;
364 $sql .= " FROM {node} n WHERE n.type='sitenotes' AND n.status=1";
365 $sql .= $range ? ' AND n.changed>='. (time() - $range) : NULL;
366 $sql .= ' ORDER BY n.sticky DESC, n.changed DESC';
367
368 $output = '<div class="sitenotes-recent"><ul>';
369 $results = db_query_range($sql, 0, $how_many);
370 while ($data = db_fetch_array($results)) {
371 $output .= '<li>'. l($data['title'], 'node/'. $data['nid'], array('title' => t('View content.')));
372 if ($show_author || $show_when) {
373 $output .= '<div class="sitenotes-recent-info">';
374 if ($show_author) {
375 $author = db_result(db_query_range('SELECT name FROM {users} WHERE uid=%d', $data['uid'], 0, 1));
376 $output .= l($author, 'user/'. $data['uid'], array('title' => t('View user profile.')));
377 }
378 $output .= $show_when ? ' '. format_date($data['changed'], 'custom', 'j M y') : NULL;
379 $output .= '</div>';
380 }
381 $output .= '</li>';
382 } /* end while */
383 $output .= '</ul><div class="sitenotes-recent-info">'. l(t('Add a new Site Note'), 'node/add/sitenotes') .'</div></div>';
384
385 $block['content'] = $output;
386 return $block;
387
388 } /* end switch view delta */
389 break;
390
391 } /* end switch $op */
392
393 } /* end access check */
394 }
395
396 //function sitenotes_search_item($item) {
397 // drupal_set_message('sitenotes_search_item entered');
398 // echo print_r($item);
399 //}
400
401 function theme_sitenotes_search_item($item, $type) {
402 $output = ' <dt class="title"><a href="'. check_url($item['link']) .'">'. check_plain($item['title']) .'</a></dt>';
403 $info = array();
404 if ($item['type']) {
405 $info[] = check_plain($item['type']);
406 }
407 if ($item['user']) {
408 $info[] = $item['user'];
409 }
410 if ($item['date']) {
411 $info[] = format_date($item['date'], 'small');
412 }
413 if (is_array($item['extra'])) {
414 $info = array_merge($info, $item['extra']);
415 }
416 $output .= ' <dd>'. ($item['snippet'] ? '<p>'. $item['snippet'] .'</p>' : '') .'<p class="search-info">'. implode(' - ', $info) .'</p></dd>';
417 return $output;
418 }

  ViewVC Help
Powered by ViewVC 1.1.2