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

Contents of /contributions/modules/weblink/weblink.module

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


Revision 1.84 - (show annotations) (download) (as text)
Thu Jan 12 21:11:28 2006 UTC (3 years, 10 months ago) by weitzman
Branch: MAIN
CVS Tags: HEAD
Changes since 1.83: +9 -7 lines
File MIME type: text/x-php
update for node permissions. upgrade recommended.

added 'edit own weblinks' permission. adjust your site accordingly.
1 <?php
2 // $Id: weblink.module,v 1.83 2005/09/01 13:44:53 ber Exp $
3
4 /**
5 * General Hooks
6 */
7
8 function weblink_help($section = "admin/help#weblink") {
9 switch ($section) {
10 case "admin/help#weblink":
11 return t("<p>The weblinks module is used to create links to other resources - websites, pages, documents, etc. Each click on an outgoing link is logged and counted.</p>");
12 case "admin/modules#description":
13 return t("Allows articles with an associated weblink");
14 case 'node/add#weblink':
15 return t("Weblinks allow you to link to other websites and pages.");
16 break;
17 }
18 }
19
20 /**
21 * Implementation of hook_menu().
22 */
23 function weblink_menu() {
24 global $user;
25 //if ($may_cache) {
26 $items[] = array(
27 'path' => 'node/add/weblink',
28 'title' => t('weblink'),
29 'access' => user_access('create weblinks'));
30 $items[] = array(
31 'path' => 'weblink/goto',
32 'callback' => 'weblink_goto',
33 'type' => MENU_CALLBACK,
34 'callback arguments' => arg(2),
35 'access' => user_access('access content'));
36 //}
37 return $items;
38 }
39
40
41 function weblink_link($type, $node = 0, $main = 0) {
42 $links = array();
43 // Node links for a weblink
44 if ($type == 'node' && $node->type == 'weblink' && $node->url) {
45 $links[] = theme('weblink_link', $node);
46 }
47 return $links;
48 }
49
50 function weblink_perm() {
51 return array('create weblinks', 'edit own weblinks');
52 }
53
54 /**
55 * Filter handling code
56 */
57
58 function weblink_filter($op, $delta = 0, $format = -1, $text = "") {
59 switch ($op) {
60 case 'list':
61 return array(0 => t("Weblink filter"));
62 case 'description':
63 return t("Filter for weblink tokens, [weblink:node_id|text] or [weblink:http://weblink.example.com/]");
64 case 'process':
65 return _weblink_filter_process($text);
66 default:
67 return $text;
68 }
69 }
70
71 function _weblink_filter_process($text) {
72 $match = array();
73 if (preg_match_all("/\[weblink:(\d+)(\|([^\|\]]*))?\]/i", $text, $match)) { // look for a node ID
74 for ($i = 0; $i <= count($match[1]); $i++) {
75 $node = node_load(array("nid" => $match[1][$i]));
76 $match[2][$i] = preg_replace('/(^\|)/e', '', $match[2][$i]); // remove leading bar
77 if (!$node->nid) {
78 $text = str_replace($match[0][$i], $match[2][$i] . ' (' .sprintf(t('unknown link #%d'), $match[1][$i]) . ')', $text);
79 }
80 else {
81 if (!$match[2][$i]) {
82 $match[2][$i] = $node->title;
83 }
84
85 if ($node->url) {
86 $text = str_replace($match[0][$i], l($match[2][$i], "weblink/goto/$node->lid", array('class' => 'outgoing', 'title' => t('visit %link', array('%link' => $node->url)))), $text);
87 }
88 }
89 }
90 }
91
92 $match = array();
93 if (preg_match_all("/\[weblink:(\S+?)\]/i", $text, $match)) {
94 for ($i = 0; $i <= count($match[1]); $i++) {
95 $result = db_query(db_rewrite_sql("SELECT n.nid FROM {node} n INNER JOIN {weblinks_node} wn ON n.nid = wn.nid INNER JOIN {weblinks} w ON wn.lid = w.lid WHERE url_md5 = '%s'"), md5($match[1][$i]));
96 $weblink = db_fetch_object($result);
97 $node = node_load(array("nid" => $weblink->nid));
98 $text = str_replace($match[0][$i], l($node->title, "weblink/goto/$node->lid", array('class' => 'outgoing','title' => t('visit %link', array('%link' => $node->url)))), $text);
99 }
100 }
101 return $text;
102 }
103
104 function weblink_filter_tips($delta, $format, $long = false) {
105 if ($long) {
106 return t("You may create links to items stored in our weblink registry using a special syntax. The weblink codes will be replaced by a links to visit the real websites. Syntax: <code>[weblink:link_id]</code> or <code>[weblink:http://a.weblink.example.com/]</code>.");
107 }
108 else {
109 return t("You may link to webpages <a href=\"%long-tip\">through the weblinks registry</a>", array("%long-tip" => url("filter/tips", NULL, 'weblink')));
110 }
111 }
112
113 /**
114 * Node Hooks
115 */
116 function weblink_node_name($node) {
117 return t("weblink");
118 }
119
120 function weblink_access($op, $node) {
121 if ($op == 'update' || $op == 'delete') {
122 if (user_access('edit own weblinks') && ($user->uid == $node->uid)) {
123 return TRUE;
124 }
125 }
126
127 if ($op == 'create') {
128 return user_access("create weblinks");
129 }
130 }
131
132 function weblink_form(&$node, &$param) {
133 $output .= form_textfield(t("Link"), "url", $node->url, 60, 65535, t("Links should be submitted in a form similar to http://www.example.com") . ($error['url'] ? $error['url'] : ''), NULL, TRUE);
134
135 if (function_exists('taxonomy_node_form')) {
136 $output .= implode('', taxonomy_node_form('weblink', $node));
137 }
138
139 $output .= form_textarea(t("Body"), "body", $node->body, 60, 20, t("Textual description of the weblink") . ($error['body'] ? $error['body'] : ''));
140 $output .= filter_form('format', $node->format);
141 return $output;
142 }
143
144 function weblink_insert($node) {
145 $lid = db_next_id('weblinks_lid');
146 db_query("INSERT INTO {weblinks_node} (lid, nid) VALUES ('%d', '%d')", $lid, $node->nid);
147 db_query("INSERT INTO {weblinks} (lid, url, url_md5) VALUES ('%d', '%s', '%s')", $lid, $node->url, md5($node->url));
148 }
149
150 function weblink_update($node) {
151 $old_node = node_load(array(nid => $node->nid));
152 db_query("UPDATE {weblinks} SET url = '%s', url_md5 = '%s' WHERE lid = '%d'", $node->url, md5($node->url), $old_node->lid);
153 }
154
155 function weblink_delete(&$node) {
156 $result = db_query("SELECT lid FROM {weblinks_node} WHERE nid = %d", $node->nid);
157 db_query("DELETE FROM {weblinks_node} WHERE nid = %d", $node->nid);
158 while ($row = db_fetch_object($result)) {
159 db_query("DELETE FROM {weblinks} WHERE lid = '%s'", $row->lid);
160 }
161 }
162
163 function weblink_validate(&$node) {
164 if ($node->url) {
165 if(!valid_url($node->url, TRUE)) {
166 form_set_error('url', t('The weblink is not valid. Make sure you have included the http:// part. Other valid protocols are https and ftp'));
167 }
168 else {
169 $result = db_query("SELECT n.nid FROM {weblinks} w INNER JOIN {weblinks_node} wn ON w.lid = wn.lid INNER JOIN {node} n ON wn.nid = n.nid WHERE url_md5 = '%s' AND NOT(n.nid = %d)", md5($node->url), $node->nid);
170 if (db_num_rows($result) > 0) {
171 $weblink = db_fetch_object($result);
172 $othernode = node_load(array("nid" => $weblink->nid));
173 form_set_error('url', t('A weblink "%link-to-existing" using that link already exists', array("%link-to-existing" => l($othernode->title, 'node/' . $othernode->nid . '/edit'))));
174 }
175 }
176 }
177 elseif (isset($node->url) && (trim($node->url) == '')) {
178 form_set_error('url', t('You have to specify a weblink.'));
179 }
180 }
181
182 function weblink_load($node) {
183 return db_fetch_object(db_query("SELECT * FROM {weblinks_node} n INNER JOIN {weblinks} w ON n.lid = w.lid WHERE n.nid = '%d' LIMIT 1", $node->nid));
184 }
185
186 /**
187 * Block display functions
188 */
189 function weblink_block($op = "list", $delta = 0) {
190 if ($op == "list") {
191 return array(
192 0 => array('info' => t("Top weblinks")),
193 1 => array('info' => t("Latest weblinks")),
194 );
195 }
196 elseif ($op == 'view') {
197 switch ($delta) {
198 case 0:
199 return array(
200 'subject' => t("Top weblinks"),
201 'content' => weblink_block_list('top')
202 );
203 case 1:
204 return array(
205 'subject' => t("Latest weblinks"),
206 'content' => weblink_block_list('new')
207 );
208 }
209 }
210 }
211
212 function weblink_block_list($type = 'top') {
213 $orderby = ($type == 'new') ? 'n.created' : 'w.clicks';
214 return node_title_list(db_query_range(db_rewrite_sql("SELECT n.nid, n.title, w.url, w.clicks FROM {node} n INNER JOIN {weblinks_node} wn on n.nid = wn.nid INNER JOIN {weblinks} w ON wn.lid = w.lid WHERE n.type = 'weblink' AND n.status = 1 AND n.moderate = 0 ORDER by $orderby DESC"),0, 10));
215 }
216
217 /**
218 @addtogroup themeable
219 @{
220 **/
221 function theme_weblink_link($node) {
222 return l(t('visit %link', array('%link' => $node->title)), "weblink/goto/$node->lid", array('class' => 'outgoing', 'title' => t('visit %link', array('%link' => $node->url)))) . (user_access("access statistics") ? " ({$node->clicks})" : "");
223 }
224 /** @} End of addtogroup themeable */
225
226 function weblink_goto($lid = NULL, $url = NULL) {
227 if ($lid) {
228 $result = db_query("SELECT url FROM {weblinks} WHERE lid = '%d'", $lid);
229 }
230 elseif ($url) {
231 $result = db_query("SELECT url FROM {weblinks} WHERE url_md5 = '%s'", md5($url));
232 }
233 if ($weblink = db_fetch_object($result)) {
234 db_query("UPDATE {weblinks} SET clicks = clicks + 1 WHERE lid = '%d'", $lid);
235 header("Location: " . $weblink->url);
236 }
237 else {
238 drupal_not_found();
239 }
240 }
241
242 /**
243 * Add a "(visit)" link to the RSS excerpt if it is a weblink.
244 */
245 function weblink_nodeapi(&$node, $op, $teaser, $page) {
246 switch ($op) {
247 case 'rss item':
248 if ($node->type == 'weblink') {
249 $node->teaser .= l(t('visit %link', array('%link' => $node->title)), "weblink/goto/$node->lid", array('class' => 'outgoing', 'title' => t('visit %link', array('%link' => $node->url))), NULL, NULL, TRUE) . (user_access("access statistics") ? " ({$node->clicks})" : "");
250 }
251 break;
252 }
253 }
254 ?>

  ViewVC Help
Powered by ViewVC 1.1.2