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

Contents of /contributions/modules/nodeteaser/nodeteaser.module

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


Revision 1.2 - (show annotations) (download) (as text)
Wed Feb 14 00:22:36 2007 UTC (2 years, 9 months ago) by ideaoforder
Branch: MAIN
CVS Tags: DRUPAL-5--1-0, DRUPAL-5--1-2, HEAD
Branch point for: DRUPAL-5
Changes since 1.1: +285 -233 lines
File MIME type: text/x-php
Updated module for 5.0, added several new features
1 <?php
2 /**
3 * Valid permissions for this module
4 * @return An array of valid permissions for the nodeteaser module
5 */
6 function nodeteaser_perm() {
7
8 return array('administer nodeteaser');
9
10 } // function nodeteaser_perm()
11
12 /**
13 * Implementation of hook_menu().
14 */
15 function nodeteaser_menu($may_cache) {
16 $items = array();
17
18 if ($may_cache) {
19 $items[] = array(
20 'path' => 'admin/settings/nodeteaser',
21 'title' => t('Node Teaser'),
22 'description' => t('Set a variety of options for node teaser input and display.'),
23 'callback' => 'drupal_get_form',
24 'callback arguments' => array('nodeteaser_admin_settings'),
25 'access' => user_access('administer site configuration'),
26 'type' => MENU_NORMAL_ITEM, // optional
27 );
28 }
29 return $items;
30 }
31
32
33 /**
34 * Implementation of hook_nodeapi().
35 */
36 function nodeteaser_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
37 switch ($op) {
38 case 'delete':
39 _nodeteaser_delete($node);
40 break;
41 case 'insert':
42 _nodeteaser_insert($node);
43 break;
44 case 'update':
45 _nodeteaser_update($node);
46 break;
47 case 'prepare':
48 $node = _nodeteaser_load($node);
49 $node = node_prepare($node, $teaser);
50 break;
51 case 'view':
52 $node = _nodeteaser_load($node);
53 $node = node_prepare($node, $teaser);
54 break;
55 }
56 }
57
58
59 /**
60 * Implementation of hook_form_alter().
61 */
62 function nodeteaser_form_alter($form_id, &$form) {
63 if (_nodeteaser_page_match()) { // Make sure that teaser should be displayed on this page
64 if (isset($form['type']) && $form_id == $form['type']['#value'] . '_node_form') {
65 $type = 'node';
66 $id = $form['nid']['#value'];
67 } else if ($form_id == 'taxonomy_form_vocabulary') {
68 $type = 'vocabulary';
69 $id = $form['vid']['#value'];
70 $form['submit']['#weight'] = 5;
71 $form['delete']['#weight'] = 5;
72 } else if ($form_id == 'taxonomy_form_term') {
73 $type = 'term';
74 $id = $form['tid']['#value'];
75 $form['submit']['#weight'] = 5;
76 $form['delete']['#weight'] = 5;
77 }
78
79 if (isset($type)) {
80 if (isset($id) && is_numeric($id)) {
81 $tags = _nodeteaser_load($id);
82 } else {
83 $tags = array();
84 }
85
86 $form['nt'] = array(
87 '#type' => 'fieldset',
88 '#title' => t('Teaser or Summary'),
89 '#collapsible' => TRUE,
90 '#collapsed' => variable_get('nodeteaser_collapsed', FALSE),
91 );
92
93 $form['nt']['nodeteaser'] = array(
94 '#type' => 'textarea',
95 '#default_value' => $tags->teaser,
96 '#cols' => 65,
97 '#rows' => 12,
98 '#description' => t('Enter a teaser, summary, or description for this node. If you would like the whole body to display, DO NOT enter a teaser!'),
99 );
100 }
101 }
102 }
103
104 /**
105 * Implementation of hook_load().
106 */
107 function _nodeteaser_load($node) {
108 if (!is_object($node)) {
109 $id = $node;
110 $node = (object) $node;
111 }
112 else {
113 $id = $node->nid;
114 }
115 if (isset($node->nodeteaser)) {
116 $nt->teaser = $node->nodeteaser;
117 } else {
118 $nt = db_fetch_object(db_query('SELECT teaser FROM {nodeteaser} WHERE nid = %d', $id));
119 if(!$nt) {
120 $node->nodeteaser = FALSE;
121 }
122 }
123
124 if ($nt->teaser != '') {
125 $node->teaser = $nt->teaser;
126 $node->nodeteaser = TRUE;
127 }else {
128 switch (variable_get("nodeteaser_empty", 'drupal_default')) {
129 case 'drupal_default':
130 break;
131 case 'nothing':
132 $node->teaser = '&nbsp;';
133 break;
134 case 'node_title':
135 $node->teaser = $node->title;
136 break;
137 case 'full_post':
138 $node->teaser = $node->body;
139 break;
140 }
141 $node->nodeteaser = FALSE;
142 }
143
144 return $node;
145 }
146
147 /* This function formats the teaser for display */
148 /*function _nodeteaser_format($node) {
149 $output = $node->teaser;
150
151 $path = $node->path;
152 if (!isset($path)) {
153 $path = "node/".$node->nid;
154 }
155
156 if ($node->nodeteaser) {
157 if (variable_get('nodeteaser_readmore', FALSE)) {
158 $node->teaser .= "<br /><br /><a href=\"{$path}\">continue reading \"".$node->title."\"</a>";
159 }
160 } elseif (variable_get('nodeteaser_readmore', FALSE)) {
161 // Get the unformatted teaser and body for comparison
162 $nt = db_fetch_object(db_query('SELECT body FROM {node_revisions} WHERE nid = %d', $node->nid));
163 $body = $nt->body;
164 $teaser = $node->teaser;
165 $blength = strlen(htmlentities($body));
166 $tlength = strlen(htmlentities($teaser));
167 if ($blength > $tlength) {
168 $node->teaser .= "<br /><br /><a href=\"{$path}\">continue reading \"".$node->title."\"</a>";
169 }
170 }
171 return $node;
172 }*/
173
174 /* Delete teaser from table */
175 function _nodeteaser_delete($node) {
176 return db_query("DELETE FROM {nodeteaser} WHERE nid = %d", $node->nid);
177 }
178 /* Insert teaser into table */
179 function _nodeteaser_insert($node) {
180 return db_query("INSERT INTO {nodeteaser} (nid, teaser) VALUES (%d, '%s')", $node->nid, $node->nodeteaser);
181 }
182
183 /**
184 * Update teaser for new and existing content
185 */
186 function _nodeteaser_update($node) {
187
188 $nt = db_fetch_object(db_query('SELECT teaser FROM {nodeteaser} WHERE nid = %d', $node->nid));
189
190 if ($nt)
191 return db_query("UPDATE {nodeteaser} SET teaser = '%s' WHERE nid = %d", $node->nodeteaser, $node->nid);
192 else
193 return _nodeteaser_insert($node);
194 }
195
196 /**
197 * Module configuration settings
198 * @return settings HTML or deny access
199 */
200 function nodeteaser_admin_settings() {
201 /*$form['nodeteaser_readmore'] = array('#type' => 'checkbox',
202 '#title' => t('"continue reading" link'),
203 '#default_value' => variable_get("nodeteaser_readmore", FALSE),
204 '#description' => t('Display a "continue reading" link below teasers'),
205 '#required' => FALSE,
206 '#weight' => 0,
207 );*/
208
209 $form['nodeteaser_empty'] = array('#type' => 'radios',
210 '#title' => t('If node teaser is empty'),
211 '#default_value' => variable_get("nodeteaser_empty", 'drupal_default'),
212 '#description' => t('Default display for empty node teasers'),
213 '#options' => array('drupal_default' => 'Drupal default', 'nothing' => 'display nothing', 'node_title' => 'node title', 'full_post' => 'full post'),
214 '#required' => FALSE,
215 '#weight' => 0,
216 );
217 $form['nodeteaser_collapsed'] = array('#type' => 'checkbox',
218 '#title' => t('Collapse node teaser'),
219 '#default_value' => variable_get("nodeteaser_collapsed", FALSE),
220 '#description' => t('Collapse node teaser by default'),
221 '#required' => FALSE,
222 '#weight' => 0,
223 );
224
225 // Check if user has permission to 'use PHP for block visibility'
226 // in Administer > access control > block
227 //$access = user_access('use PHP for block visibility');
228
229 $options = array(t('Show on every page except the listed pages.'), t('Show on only the listed pages.'));
230 $description = t("Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths are '%blog' for the blog page and %blog-wildcard for every personal blog. %front is the front page.", array('%blog' => theme('placeholder', 'blog'), '%blog-wildcard' => theme('placeholder', 'blog/*'), '%front' => theme('placeholder', '<front>')));
231
232 // If user does not have access to PHP code for visibility in Administer > access control > block,
233 // radio button option for 'PHP for visibility' and description won't appear
234 //if ($access) {
235 $options[] = t('Show if the following PHP code returns <code>TRUE</code> (PHP-mode, experts only).');
236 $description .= t(' If the PHP-mode is chosen, enter PHP code between %php. Note that executing incorrect PHP-code can break your Drupal site.', array('%php' => theme('placeholder', '<?php ?>')));
237 //}
238
239 $form['nodeteaser_access'] = array(
240 '#type' => 'radios',
241 '#title' => t('Show Teaser on specific pages'),
242 '#default_value' => variable_get('nodeteaser_access', 1),
243 '#options' => $options,
244 );
245 $form['nodeteaser_access_pages'] = array(
246 '#type' => 'textarea',
247 '#title' => t('Pages'),
248 '#default_value' => variable_get('nodeteaser_access_pages', ''),
249 '#description' => $description,
250 );
251
252 return system_settings_form($form);
253 }
254
255 /**
256 * Determine if nodeteaser has permission to be used on the current page.
257 *
258 * @return
259 * TRUE if can render, FALSE if not allowed.
260 */
261 function _nodeteaser_page_match() {
262 $page_match = "FALSE";
263
264 // If one of the options to control nodeteaser visibility was selected
265 if (variable_get('nodeteaser_access_pages', '')) {
266 // If the PHP option wasn't selected
267 if (variable_get('nodeteaser_access', 1)< 2) {
268 $path = drupal_get_path_alias($_GET['q']);
269 $regexp = '/^('. preg_replace(array('/(\r\n?|\n)/', '/\\\\\*/', '/(^|\|)\\\\<front\\\\>($|\|)/'), array('|', '.*', '\1'. preg_quote(variable_get('site_frontpage', 'node'), '/') .'\2'), preg_quote(variable_get('nodeteaser_access_pages', ''), '/')) .')$/';
270 $page_match = !(variable_get('nodeteaser_access', 1) xor preg_match($regexp, $path));
271 }
272 // If the PHP option was selected
273 else {
274 $page_match = drupal_eval(variable_get('nodeteaser_access_pages', ''));
275 }
276 }
277 // If no options to control nodeteaser visibility are selected,
278 // visibility defaults to show nodeteaser on every page
279 else {
280 $page_match = "TRUE";
281 }
282 return $page_match;
283 }
284
285 ?>

  ViewVC Help
Powered by ViewVC 1.1.2