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

Contents of /contributions/modules/referral_links/referral_links.module

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


Revision 1.2 - (show annotations) (download) (as text)
Wed Apr 11 22:12:46 2007 UTC (2 years, 7 months ago) by stevemckenzie
Branch: MAIN
CVS Tags: DRUPAL-5--1-0, HEAD
Branch point for: DRUPAL-5
Changes since 1.1: +103 -17 lines
File MIME type: text/x-php
added support for attaching referral sites to users giving them permissions to view their referral sites stats
1 <?php
2 // $Id: referral_links.module,v 1.1 2007/04/11 20:37:37 stevemckenzie Exp $
3
4 /**
5 * Implementation of hook_perm().
6 */
7 function referral_links_perm() {
8 return array('administer referral links', 'access referral links stats', 'access own referral links stats');
9 }
10
11 /**
12 * Implementation of hook_menu().
13 */
14 function referral_links_menu($may_cache) {
15 global $user;
16 $items = array();
17
18 $items[] = array(
19 'path' => 'admin/referral_links',
20 'title' => t('Referrals links'),
21 'description' => t('View referral stats.'),
22 'callback' => 'referral_links_admin',
23 'access' => user_access('access referral links stats'),
24 'type' => MENU_NORMAL_ITEM
25 );
26 $items[] = array(
27 'path' => 'admin/referral_links/delete/site',
28 'title' => t('Referral links'),
29 'callback' => 'drupal_get_form',
30 'callback arguments' => 'referral_links_site_delete_confirm',
31 'access' => user_access('administer referral links'),
32 'type' => MENU_CALLBACK
33 );
34 $items[] = array(
35 'path' => 'admin/settings/referral_links',
36 'title' => t('Referral links'),
37 'description' => t('Manage referral links.'),
38 'callback' => 'drupal_get_form',
39 'callback arguments' => 'referral_links_settings_form',
40 'access' => user_access('administer referral links'),
41 'type' => MENU_NORMAL_ITEM
42 );
43
44 // Display personal user tabs for referral links sites.
45 if (arg(0) == 'user' && is_numeric(arg(1))) {
46 $uid = arg(1);
47 $sites = db_result(db_query("SELECT COUNT(rid) FROM {referral_links_sites} WHERE uid = %d", $uid));
48 if ($sites > 0 && ($user->uid == $uid || user_access('administer referral links'))) {
49 $items[] = array(
50 'path' => 'user/'. $uid .'/referral_links',
51 'title' => t('Referral Sites'),
52 'callback' => 'referral_links_user_stats',
53 'callback arguments' => array($uid),
54 'access' => user_access('access own referral links stats'),
55 'type' => MENU_LOCAL_TASK
56 );
57 }
58 }
59
60 return $items;
61 }
62
63
64 /**
65 * Implementation of hook_user().
66 */
67 function referral_links_user($op, &$edit, &$account, $category = NULL) {
68 switch ($op) {
69 case 'insert':
70 _referral_links_add_signup($account);
71 break;
72 case 'load':
73 $referrals = array();
74 $result = db_query("SELECT * FROM {referral_links_sites} WHERE uid = %d", $account->uid);
75 while ($row = db_fetch_array($result)) {
76 $referrals[] = $row;
77 }
78 $user->referral_links = $referrals;
79 break;
80 }
81 }
82
83 /**
84 * Implementation of hook_init().
85 */
86 function referral_links_init() {
87 global $user;
88
89 // Do not look at registered users.
90 if ($user->uid > 0) {
91 return false;
92 }
93
94 // Check if a referral_id is available and save it as a cookie.
95 if ($_GET['referral_id']) {
96 // Save the referral id in a cookie and sessions for more compatibility.
97 if (db_result(db_query("SELECT rid FROM {referral_links_sites} WHERE rid = %d", $_GET['referral_id']))) {
98 setcookie('referral_id', $_GET['referral_id']);
99 $_SESSION['referral_id'] = $_GET['referral_id'];
100 drupal_goto($_GET['q']);
101 }
102 }
103 }
104
105 /**
106 * Module settings form.
107 */
108 function referral_links_settings_form() {
109 drupal_set_message(t('Soon to come.'));
110
111 return system_settings_form($form);
112 }
113
114 /**
115 * Add / update a referral site form.
116 */
117 function referral_links_site_form($site = null) {
118 // Permissions check.
119 if (!user_access('administer referral links')) {
120 return false;
121 }
122 // Labels.
123 if ($site['rid']) {
124 $op = t('Save');
125 } else {
126 $op = t('Add');
127 }
128
129 if ($site['uid']) {
130 $account = user_load(array('uid' => $site['uid']));
131 $account_name = $account->name;
132 }
133
134 $form['add'] = array('#type' => 'fieldset', '#title' => t('!op a referral site', array('!op' => $op)), '#collapsible' => true, '#collapsed' => $site['rid'] ? false : true);
135 if ($site['rid']) {
136 $form['add']['rid'] = array('#type' => 'hidden', '#value' => $site['rid']);
137 }
138 $form['add']['domain'] = array(
139 '#type' => 'textfield',
140 '#title' => t('Site Domain'),
141 '#required' => true,
142 '#default_value' => $site['domain'],
143 '#description' => t('Provide only the domain and not a folder inside a site.')
144 );
145 $form['add']['uid'] = array(
146 '#type' => 'textfield',
147 '#autocomplete_path' => 'user/autocomplete',
148 '#title' => t('User'),
149 '#default_value' => $account_name,
150 '#description' => t('Associate a referral site to a user which grants the user rights to view their site(s) referral data.')
151 );
152 $form['add']['submit'] = array('#type' => 'submit', '#value' => $op);
153
154 return $form;
155 }
156
157 /**
158 * Validate the referral site information.
159 */
160 function referral_links_site_form_validate($form_id, $form_values) {
161 // Validate domain syntax.
162 if (!strchr($form_values['domain'], '.')) {
163 form_set_error('domain', t('You must provide a valid domain. (Example: site.com)'));
164 }
165 if (strchr($form_values['domain'], '/')) {
166 form_set_error('domain', t('You must provide only the domain. (Example: site.com)'));
167 }
168 // Validate existance in db.
169 if (arg(2) != 'edit' && db_result(db_query("SELECT rid FROM {referral_links_sites} WHERE domain = '%s'", $form_values['domain']))) {
170 form_set_error('domain', t('!site already exists in the referral sites list.', array('!site' => theme('placeholder', $form_values['domain']))));
171 }
172 }
173
174 /**
175 * Save the referral site information.
176 */
177 function referral_links_site_form_submit($form_id, $form_values) {
178 // Handle the autocomplete user name to switch with a uid value.
179 if ($form_values['uid']) {
180 $account = user_load(array('name' => $form_values['uid']));
181 $uid = $account->uid;
182 } else {
183 $uid = 0;
184 }
185 // Update.
186 if (is_numeric($form_values['rid'])) {
187 $rid = $form_values['rid'];
188 db_query("UPDATE {referral_links_sites} SET domain = '%s', uid = %d WHERE rid = %d", $form_values['domain'], $uid, $rid);
189 $message = 'The site !site was updated.';
190 }
191 // Insert.
192 else {
193 $rid = db_next_id('{referral_links_sites}_rid');
194 db_query("INSERT INTO {referral_links_sites} (rid, domain, uid, added) VALUES (%d, '%s', %d, %d)", $rid, $form_values['domain'], $uid, time());
195 $message = 'The site !site was added as a referral site.';
196 }
197 drupal_set_message(t($message, array('!site' => theme('placeholder', $form_values['domain']))));
198 _referral_links_link($rid);
199
200 return 'admin/referral_links';
201 }
202
203 /**
204 * Confirm site delete.
205 */
206 function referral_links_site_delete_confirm($site) {
207 return confirm_form(
208 array('rid' => array('#type' => 'hidden', '#value' => $site['rid'])),
209 t('Are you sure you want to delete the site !site?', array('!site' => theme('placeholder', $site['domain']))),
210 'admin/referral_links',
211 t('This will remove the site from the referral list.'),
212 t('Delete'),
213 t('Cancel'));
214 }
215
216 /**
217 * Delete site.
218 */
219 function referral_links_site_delete_confirm_submit($form_id, $form_values) {
220 // Delete site.
221 db_query("DELETE FROM {referral_links_sites} WHERE rid = %d", $form_values['rid']);
222 // Delete records of sign ups.
223 db_query("DELETE FROM {referral_links_signups} WHERE rid = %d", $form_values['rid']);
224
225 return 'admin/referral_links';
226 }
227
228 /**
229 * Check if the $_SERVER['HTTP_REFERER'] is a referral.
230 */
231 function referral_links_site() {
232 $result = db_query("SELECT domain, rid FROM {referral_links_sites}");
233 while ($row = db_fetch_object($result)) {
234 $referrer = strtolower($_SERVER['HTTP_REFERRER']);
235 if (strchr($referrer, $row->domain)) {
236 return $row->rid;
237 }
238 }
239
240 return false;
241 }
242
243 /**
244 * Signs ups table view.
245 */
246 function referral_links_signups($limit = 25, $uid = null) {
247 $headers = array(
248 array('data' => t('Referral Site'), 'field' => 'u.rid', 'sort' => 'desc'),
249 array('data' => t('User')),
250 array('data' => t('Added'), 'field' => 'u.added')
251 );
252 $fields = 's.domain, u.sid, u.rid, u.uid, u.added';
253 $sql = "SELECT $fields FROM {referral_links_signups} u INNER JOIN {referral_links_sites} s ON s.rid = u.rid";
254 if ($uid > 0) {
255 $sql .= " WHERE s.uid = %d";
256 $args[] = $uid;
257 }
258 $tablesort = tablesort_sql($headers);
259 $result = pager_query($sql . $tablesort, $limit, 0, str_replace($fields, 'COUNT(u.sid)', $sql), $args);
260 $rows = array();
261 while ($row = db_fetch_object($result)) {
262 $rows[] = array(
263 l($row->domain,
264 'admin/referral_links/view/site/'. $row->domain),
265 theme('username', user_load(array('uid' => $row->uid))),
266 format_date($row->added)
267 );
268 }
269 if (!empty($rows)) {
270 $output .= '<h3>'. t('Referral Sign Ups') .'</h3>';
271 $output .= theme('table', $headers, $rows);
272 $output .= theme('pager', NULL, $limit, 0);
273 }
274
275 return $output;
276 }
277
278 /**
279 * Referral sites table view.
280 */
281 function referral_links_sites($limit = 25, $uid = null) {
282 $admin = user_access('administer referral links');
283 $headers = array(
284 array('data' => t('Referral ID'), 'field' => 's.rid', 'sort' => 'desc'),
285 array('data' => t('Site'), 'field' => 's.domain'),
286 array('data' => t('Site Owner'), 'field' => 's.uid'),
287 array('data' => t('Total Referrals'), 'align' => 'center', 'field' => 's.total'),
288 array('data' => t('Added'), 'field' => 's.added')
289 );
290
291 // Admin options.
292 if ($admin) {
293 $headers[] = array('data' => t('Operations'), 'align' => 'center');
294 }
295
296 $sql = "SELECT s.rid, s.domain, s.uid, s.added, s.total FROM {referral_links_sites} s";
297 if ($uid > 0) {
298 $sql .= " WHERE s.uid = %d";
299 $args[] = $uid;
300 }
301 $tablesort = tablesort_sql($headers);
302 $result = pager_query($sql . $tablesort, $limit, 1, str_replace('s.*', 'COUNT(s.rid)', $sql), $args);
303 $rows = array();
304
305 while ($row = db_fetch_object($result)) {
306 $edit = l(t('edit'), 'admin/referral_links/edit/site/'. $row->rid);
307 $delete = l(t('delete'), 'admin/referral_links/delete/site/'. $row->rid);
308
309 $rows[$row->rid] = array(
310 $row->rid,
311 $row->domain,
312 ($row->uid > 0) ? theme('username', user_load(array('uid' => $row->uid))) : t('n/a'),
313 array('data' => $row->total, 'align' => 'center'),
314 format_date($row->added)
315 );
316
317 // Admin options.
318 if ($admin) {
319 $rows[$row->rid][] = array('data' => t('!edit | !delete', array('!edit' => $edit, '!delete' => $delete)), 'align' => 'center');
320 }
321 }
322
323 if (!empty($rows)) {
324 $output .= '<h3>'. t('Referral Sites') .'</h3>';
325 $output .= theme('table', $headers, $rows, array('style' => 'width:100%'));
326 $output .= theme('pager', NULL, $limit, 1);
327 } else {
328 $output .= '<p>'. t('There are currently no sites.') .'</p>';
329 }
330
331 return $output;
332 }
333
334 /**
335 * Referral links stats page for individual users.
336 */
337 function referral_links_user_stats($uid) {
338 if ($uid > 0) {
339 $output .= referral_links_sites(25, $uid);
340 $output .= referral_links_signups(25, $uid);
341 }
342
343 print theme('page', $output);
344 }
345
346 /**
347 * Referral links stats page.
348 */
349 function referral_links_admin($form_id = null, $form = null) {
350 $op = arg(2);
351 $type = arg(3);
352 $id = arg(4);
353
354 switch ($op) {
355 case 'edit':
356 $site = db_fetch_array(db_query("SELECT * FROM {referral_links_sites} WHERE rid = %d", $id));
357 _referral_links_link($site['rid']);
358 break;
359 case 'delete':
360 if ($op == 'site') {
361 $site = db_fetch_array(db_query("SELECT * FROM {referral_links_sites} WHERE rid = %d", $id));
362 if ($site['rid']) {
363 $output .= drupal_get_form('referral_links_site_delete_confirm', $site);
364 }
365 }
366 break;
367 }
368
369 $output .= drupal_get_form('referral_links_site_form', $site);
370 $output .= referral_links_sites();
371 $output .= referral_links_signups();
372
373 print theme('page', $output);
374 }
375
376 /**
377 * Add a signup to the database.
378 */
379 function _referral_links_add_signup($account, $rid = NULL) {
380 if (!$rid) {
381 if ($_SESSION['referral_id']) {
382 $rid = $_SESSION['referral_id'];
383 } else if ($_COOKIE['referral_id']) {
384 $rid = $_COOKIE['referral_id'];
385 }
386 }
387
388 // Handle giving the referral credit.
389 if (is_numeric($rid)) {
390 $sid = db_next_id('{referral_links_signups}_sid');
391 db_query("INSERT INTO {referral_links_signups} (sid, rid, uid, added) VALUES (%d, %d, %d, %d)", $sid, $rid, $account->uid, time());
392 db_query("UPDATE {referral_links_sites} s SET s.total = (s.total + 1) WHERE s.rid = %d", $rid);
393 }
394 }
395
396 /**
397 * Display the link to use for referrals.
398 */
399 function _referral_links_link($rid) {
400 static $include;
401
402 if (!$include) {
403 drupal_set_message(t('To use this site as a referral, use the URL !url?!rid or simply add the !rid to a URL on this site. Example: !url/user/register?!rid', array('!url' => theme('placeholder', 'http://'. $_SERVER['HTTP_HOST']), '!rid' => theme('placeholder', 'referral_id='. $rid))));
404 $include = true;
405 }
406 }

  ViewVC Help
Powered by ViewVC 1.1.2