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

Contents of /contributions/modules/title_rewrite/title_rewrite.module

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


Revision 1.1 - (show annotations) (download) (as text)
Mon Apr 7 15:37:55 2008 UTC (19 months, 3 weeks ago) by shannonlucas
Branch: MAIN
CVS Tags: DRUPAL-5--1-0, DRUPAL-5--1-2, HEAD
Branch point for: DRUPAL-5
File MIME type: text/x-php
initial checkin
1 <?php
2 // $Id$
3
4 /**
5 * @file
6 * Provides a system for creating custom content for the title element of
7 * HTML pages.
8 */
9
10 /** The permission for administering custom titles. */
11 define('TITLE_REWRITE_PERM_ADMIN', 'administer custom titles');
12
13 require_once(drupal_get_path('module', 'title_rewrite') . '/title_rewrite_forms.inc');
14
15
16 /**
17 * Implementation of hook_help().
18 *
19 * TODO - Add documentation for 'admin/help#custom_title' and
20 * 'admin/help/custom_title'
21 */
22 function title_rewrite_help($section) {
23 switch($section) {
24 case 'admin/settings/custom_title':
25 $help = '<p>' . t('Title rewrite rules allow the contents of the XHTML &lt;title&gt; element to be overridden using text specified by an administrator. The override text may include dynamic content using the tokens specified in the !ref_link page.',
26 array('!ref_link' => l(t('Token Reference Page'), 'admin/settings/custom_title/tokens'))
27 ) . '</p>';
28 $help .= '<p>' . t('Rules are evaluated by their weight. Lighter rules are evaluated before heavier ones. Rule evaluation stops on the first rule encountered that matches any given path. For example, if the lightest weight rule matches a path, that rule is evaluated and no further rules are evaluated against that path.') . '</p>';
29 return $help;
30 case 'admin/settings/custom_title/new':
31 $help = '<p>' . t('When using tokens in the Rewrite Text field, keep in mind that node tokens are only available when a page is displaying a single node. Global tokens are available on all pages.') . '</p>';
32 return $help;
33 }
34 }
35
36
37 /**
38 * Implementation of hook_menu().
39 */
40 function title_rewrite_menu($may_cache) {
41 $items = array();
42
43 if (!$may_cache) {
44 $items[] = array(
45 'path' => 'admin/settings/custom_title',
46 'title' => t('Title Rewrite Rules'),
47 'callback' => 'drupal_get_form',
48 'callback arguments' => array('title_rewrite_admin_overview'),
49 'type' => MENU_NORMAL_ITEM,
50 'access' => user_access(TITLE_REWRITE_PERM_ADMIN),
51 );
52
53 $items[] = array(
54 'path' => 'admin/settings/custom_title/list',
55 'title' => t('List'),
56 'callback' => 'title_rewrite_admin_overview',
57 'type' => MENU_DEFAULT_LOCAL_TASK,
58 'access' => user_access(TITLE_REWRITE_PERM_ADMIN),
59 );
60
61 $items[] = array(
62 'path' => 'admin/settings/custom_title/new',
63 'title' => t('New Rewrite Rule'),
64 'callback' => 'drupal_get_form',
65 'callback arguments' => array('title_rewrite_create_form'),
66 'type' => MENU_LOCAL_TASK,
67 'weight' => 1,
68 'access' => user_access(TITLE_REWRITE_PERM_ADMIN),
69 );
70
71 $items[] = array(
72 'path' => 'admin/settings/custom_title/tokens',
73 'title' => t('Token Reference'),
74 'callback' => 'drupal_get_form',
75 'callback arguments' => array('title_rewrite_tokenref_form'),
76 'type' => MENU_LOCAL_TASK,
77 'weight' => 2,
78 'access' => user_access(TITLE_REWRITE_PERM_ADMIN),
79 );
80
81 $items[] = array(
82 'path' => 'admin/settings/custom_title/edit/' . arg(4),
83 'callback' => 'drupal_get_form',
84 'callback arguments' => array('title_rewrite_create_form', arg(4)),
85 'type' => MENU_CALLBACK,
86 'access' => user_access(TITLE_REWRITE_PERM_ADMIN),
87 );
88
89 $items[] = array(
90 'path' => 'admin/settings/custom_title/delete/' . arg(4),
91 'callback' => 'drupal_get_form',
92 'callback arguments' => array('title_rewrite_delete_form', arg(4)),
93 'type' => MENU_CALLBACK,
94 'access' => user_access(TITLE_REWRITE_PERM_ADMIN),
95 );
96 }
97
98 return $items;
99 }
100
101
102 /**
103 * Implementation of hook_perm().
104 */
105 function title_rewrite_perm() {
106 return array(TITLE_REWRITE_PERM_ADMIN);
107 }
108
109
110 /**
111 * Load a custom title by its ID.
112 *
113 * @param $id
114 * The unique ID of the custom title to load.
115 *
116 * @return
117 * An array containing the custom title information, or NULL if no title
118 * with the given ID was found.
119 */
120 function _title_rewrite_load_instance($id) {
121 $title = NULL;
122 $result = db_query('SELECT id, name, type, path_expr, token_title, weight
123 FROM {title_rewrite} WHERE id = %d', $id);
124
125 if (db_num_rows($result) > 0) {
126 $title = db_fetch_array($result);
127 }
128
129 return $title;
130 }
131
132
133 /**
134 * Retrieve all the custom title patterns.
135 *
136 * @return
137 * An array of associative arrays with they keys 'path_expr' and
138 * 'toke_title'.
139 */
140 function _title_rewrite_get_all() {
141 $paths = array();
142 $result = db_query('SELECT path_expr, token_title, type FROM {title_rewrite} ORDER BY weight ASC');
143
144 while ($row = db_fetch_array($result)) {
145 $paths[] = $row;
146 }
147
148 return $paths;
149 }
150
151
152 /**
153 * Retrieve the title rewrite rule for the given path.
154 *
155 * @param $path
156 * The Drupal path to replace the title for.
157 *
158 * @return
159 * The title rewrite rule.
160 */
161 function _title_rewrite_get_rule($path) {
162 $rules = _title_rewrite_get_all();
163
164 foreach($rules as $rule) {
165 if ($rule['type'] == 1) {
166 $regexp = '/^('.
167 preg_replace(array('/(\r\n?|\n)/', '/\\\\\*/', '/(^|\|)\\\\<front\\\\>($|\|)/'),
168 array('|', '.*', '\1'. preg_quote(variable_get('site_frontpage', 'node'), '/') .'\2'),
169 preg_quote($rule['path_expr'], '/')) .
170 ')$/';
171
172 // Compare with the Drupal path and the query.
173 $page_match = preg_match($regexp, $path);
174 if ($path != $_GET['q']) {
175 $page_match = $page_match || preg_match($regexp, $_GET['q']);
176 }
177 }
178 elseif ($rule['type'] == 2) {
179 $page_match = drupal_eval($rule['path_expr']);
180 }
181
182 if ($page_match > 0) {
183 return $rule;
184 }
185 }
186
187 return NULL;
188 }
189
190
191 /**
192 * Evaluate the title rewrite rules agains the current page and generate a
193 * re-written title if a rule is available.
194 *
195 * @param $current_title
196 * The title that would be displayed on the page if no re-write rule was
197 * present. If no rule matches this page's path, this value will be
198 * returned.
199 * @param &$node
200 * If this page is a node page, template.php should pass the node. This
201 * will enable the re-write rule to make use of node tokens.
202 *
203 * @return
204 * The rewritten title for the current page. If no rule was available
205 * for this page, the value passed to $current_title will be returned.
206 */
207 function title_rewrite_page_get_title($current_title = NULL, &$node = NULL) {
208 $path = drupal_get_path_alias($_GET['q']);
209 $rule = _title_rewrite_get_rule($path);
210 $result = $current_title;
211
212 $scope = ($node != NULL) ? 'node' : 'global';
213
214 if ($rule !== NULL) {
215 $result = token_replace($rule['token_title'], $scope, $node);
216 }
217
218 return $result;
219 }

  ViewVC Help
Powered by ViewVC 1.1.2