comparison owners/ad_owners.module @ 0:d8a3998dac8e ad

ajout module ad
author pierre
date Fri, 20 Feb 2009 14:04:09 +0000
parents
children 948362c2a207
comparison
equal deleted inserted replaced
-1:000000000000 0:d8a3998dac8e
1 <?php
2 // $Id: ad_owners.module,v 1.1.2.8 2009/02/17 04:34:27 jeremy Exp $
3
4 /**
5 * @file
6 * Enhances the ad module to support ad owners.
7 *
8 * Copyright (c) 2009.
9 * Jeremy Andrews <jeremy@tag1consulting.com>.
10 */
11
12 /**
13 * Implementation of hook_theme().
14 */
15 function ad_owners_theme() {
16 return array(
17 'ad_owner_permissions_form' => array(
18 'arguments' => array(
19 'form' => NULL,
20 ),
21 ),
22 );
23 };
24
25 /**
26 * Implementation of hook_menu().
27 */
28 function ad_owners_menu() {
29 $items = array();
30
31 $items['node/%node/adowners'] = array(
32 'title' => 'Ad owners',
33 'page callback' => 'ad_owners_overview',
34 'page arguments' => array(1),
35 'access callback' => 'ad_owners_access',
36 'access arguments' => array(1),
37 'type' => MENU_LOCAL_TASK,
38 'weight' => 5,
39 );
40 $items['node/%node/adowners/list'] = array(
41 'title' => 'List',
42 'access callback' => 'ad_adaccess',
43 'access arguments' => array(1, 'manage owners'),
44 'type' => MENU_DEFAULT_LOCAL_TASK,
45 'weight' => 0,
46 );
47 $items['node/%node/adowners/%user/permissions'] = array(
48 'title callback' => 'owner_permissions_title',
49 'title arguments' => array('!owner' => 3),
50 'page callback' => 'drupal_get_form',
51 'page arguments' => array('ad_owner_permissions_form', 1, 3),
52 'access callback' => 'ad_adaccess',
53 'access arguments' => array(1, 'manage owners'),
54 'type' => MENU_LOCAL_TASK,
55 'weight' => 2,
56 );
57 $items['node/%node/adowners/%user/remove'] = array(
58 'title' => 'Remove owner',
59 'page callback' => 'drupal_get_form',
60 'page arguments' => array('ad_owner_remove_form', 1, 3),
61 'access callback' => 'ad_adaccess',
62 'access arguments' => array(1, 'manage owners'),
63 'type' => MENU_CALLBACK,
64 'weight' => 6,
65 );
66 $items['node/%node/adowners/add'] = array(
67 'title' => 'Add owner',
68 'page callback' => 'drupal_get_form',
69 'page arguments' => array('ad_owners_add_form', 1),
70 'access callback' => 'ad_adaccess',
71 'access arguments' => array(1, 'manage owners'),
72 'type' => MENU_LOCAL_TASK,
73 'weight' => 4,
74 );
75
76 return $items;
77 }
78
79 /**
80 * Menu item access callback.
81 */
82 function ad_owners_access($node) {
83 return ($node->type == 'ad') && ad_adaccess($node, 'manage owners');
84 }
85
86 /**
87 * Menu item title callback - use the user name
88 */
89 function owner_permissions_title($account) {
90 return t('!owner\'s permissions', array('!owner' => $account->name));
91 }
92
93 /**
94 * Implementation of hook_form_alter().
95 */
96 function ad_owners_form_alter(&$form, &$form_state, $form_id) {
97 if ($form_id == 'ad_'. arg(4) .'_global_settings' || $form_id == 'ad_no_global_settings') {
98 if (!isset($form['adtype'])) {
99 $form['adtype'] = array('#type' => 'value', '#value' => arg(4));
100 }
101 $permissions = module_invoke_all('adapi', 'permissions', NULL);
102 $form['permissions'] = array(
103 '#type' => 'fieldset',
104 '#title' => t('Permissions'),
105 '#collapsible' => TRUE,
106 '#description' => t('Select which permissions will be automatically granted to new owners of <em>!type</em> advertisements.', array('!type' => ad_get_types('name', arg(4)))),
107 );
108 $form['permissions']['default_permissions'] = array(
109 '#type' => 'checkboxes',
110 '#title' => t('Default permissions for <em>!type</em> owners', array('!type' => ad_get_types('name', arg(4)))),
111 '#options' => drupal_map_assoc($permissions),
112 '#default_value' => variable_get('ad_'. arg(4) .'_default_permissions', array('access statistics', 'access click history', 'manage status')),
113 );
114 if (isset($form['save'])) {
115 $form['save']['#weight'] = 10;
116 }
117 if (isset($form['#submit']) && is_array($form['#submit'])) {
118 $form['#submit'] = array('ad_global_settings_submit') + $form['#submit'];
119 }
120 else {
121 $form['#submit'] = array('ad_global_settings_submit');
122 }
123 }
124 }
125
126 /**
127 * Implementation of hook_nodeapi().
128 */
129 function ad_owners_nodeapi(&$node, $op, $teaser, $page) {
130 global $user;
131
132 switch ($op) {
133 case 'insert':
134 case 'update':
135 if (isset($node->adtype)) {
136 // Be sure ad owner has at least default ad permissions.
137 ad_owners_add($node, $node->uid);
138 ad_host_id_create($node->uid);
139 }
140 break;
141 case 'delete':
142 // Clean up ad_permissions and any other per-ad tables.
143 $result = db_query('SELECT oid, uid FROM {ad_owners} WHERE aid = %d', $node->nid);
144 while ($id = db_fetch_object($result)) {
145 db_query('DELETE FROM {ad_permissions} WHERE oid = %d', $id->oid);
146 $owner = user_load(array('uid' => $id->uid));
147 // Tell plug-in modules to clean up.
148 module_invoke_all('adowners', 'remove', $id->oid, $owner);
149 }
150 db_query('DELETE FROM {ad_owners} WHERE aid = %d', $node->nid);
151 break;
152 }
153 }
154
155 /**
156 * Implementation of hook_adapi().
157 */
158 function ad_owners_adapi($op, $node = NULL) {
159 switch ($op) {
160 case 'permissions':
161 return array('manage owners');
162 break;
163 }
164 }
165
166 /**
167 * Determine whether the ad owner has a given privilege.
168 *
169 * @param $ad
170 * Node object or aid of advertisement.
171 * @param $permission
172 * Special Ad owners permission which should be checked (such as 'manage owners')
173 * @param $account
174 * User object, which are accessing the ad or current user by default.
175 */
176 function ad_owners_adaccess($ad, $permission, $account = NULL) {
177 global $user;
178 static $permissions = array();
179
180 if (!isset($account)) {
181 $account = $user;
182 }
183
184 $aid = 0;
185 if (isset($ad)) {
186 if (is_numeric($ad)) {
187 $aid = $ad;
188 }
189 else if (is_object($ad) && isset($ad->nid)) {
190 $aid = $ad->nid;
191 }
192 }
193
194 if (!isset($permissions[$aid][$account->uid])) {
195 $oid = db_result(db_query("SELECT oid FROM {ad_owners} WHERE aid = %d and uid = %d", $aid, $account->uid));
196 $permissions[$aid][$account->uid] = explode('|,|', db_result(db_query("SELECT permissions FROM {ad_permissions} WHERE oid = %d", $oid)));
197 }
198 $access = '';
199 if (is_array($permission)) {
200 foreach ($permission as $perm) {
201 $access |= in_array($perm, $permissions[$aid][$account->uid]);
202 }
203 }
204 else {
205 $access = in_array($permission, $permissions[$aid][$account->uid]);
206 }
207
208 return $access;
209 }
210
211
212 /**
213 * TODO: Make this themeable.
214 * TODO: Group permissions by module.
215 * TODO: Allow modules to define default value for permission.
216 */
217 function ad_owners_overview($node) {
218 drupal_set_title(t('Ad owners'));
219
220 // Be sure the node owner is listed as an ad owner
221 if (!db_result(db_query('SELECT oid FROM {ad_owners} WHERE uid = %d AND aid = %d', $node->uid, $node->nid))) {
222 ad_owners_add($node, $node->uid);
223 }
224
225 $header = array(
226 array('data' => t('Username'), 'field' => 'uid'),
227 array('data' => t('Options')),
228 );
229
230 $sql = "SELECT a.uid, u.name FROM {ad_owners} a INNER JOIN {users} u ON a.uid = u.uid WHERE aid = %d";
231 $sql .= tablesort_sql($header);
232 $result = pager_query($sql, 25, 0, NULL, $node->nid);
233
234 $rows = array();
235 while ($owner = db_fetch_object($result)) {
236 $row = array();
237 $row[] = $owner->name;
238 $options = array();
239 // first option is 'permissions', plug-ins come afterwards
240 $options[] = l(t('permissions'), 'node/'. $node->nid .'/adowners/'. $owner->uid .'/permissions');
241 $options = array_merge($options, module_invoke_all('adowners', 'overview', $node->nid, $owner->uid));
242 // node owner has to remain an ad owner
243 if ($node->uid != $owner->uid) {
244 $options[] = l(t('remove'), 'node/'. $node->nid .'/adowners/'. $owner->uid .'/remove');
245 }
246 $options = implode(' | ', $options);
247 $row[] = $options;
248 $rows[] = $row;
249 }
250
251 $output = theme('table', $header, $rows);
252 $output .= theme('pager', NULL, 25, 0);
253
254 return $output;
255 }
256
257 /**
258 * A simple form for adding new users as owners of ads.
259 */
260 function ad_owners_add_form($form_state, $node) {
261 $form = array();
262 drupal_set_title(t('Add owner'));
263
264 $form['aid'] = array(
265 '#type' => 'value',
266 '#value' => $node->nid,
267 );
268 $form['username'] = array(
269 '#autocomplete_path' => 'user/autocomplete',
270 '#description' => t('Enter the username of the user who should have ownership permissions on this advertisement.'),
271 '#required' => TRUE,
272 '#type' => 'textfield',
273 '#title' => t('Username'),
274 );
275 $form['save'] = array(
276 '#type' => 'submit',
277 '#value' => t('Add owner'),
278 );
279
280 return $form;
281 }
282
283 function ad_owners_add_form_validate($form, &$form_state) {
284 $owner = user_load(array('name' => $form_state['values']['username']));
285 if (!is_object($owner)) {
286 form_set_error('username', t('The specified username %username does not exist.', array('%username' => $form_state['values']['username'])));
287 }
288 else if (db_result(db_query('SELECT oid FROM {ad_owners} WHERE uid = %d AND aid = %d', $owner->uid, $form_state['values']['aid']))) {
289 form_set_error('username', t('The specified user %username is already an owner of this ad.', array('%username' => $form_state['values']['username'])));
290 }
291 else if (!user_access('edit own advertisements', $owner) &&
292 !user_access('administer advertisements', $owner)) {
293 form_set_error('username', t('The specified user %username does not have <em>edit own advertisements</em> nor <em>administer advertisements</em> permissions. The user must be !assigned to a !role with these privileges before you can add them as an ad owner.', array('%username' => $form_state['values']['username'], '!assigned' => l(t('assigned'), "user/$owner->uid/edit"), '!role' => l(t('role'), 'admin/user/permissions'))));
294 }
295 module_invoke_all('adowners', 'validate', $owner, $form_state['values']['aid']);
296 }
297
298 function ad_owners_add_form_submit($form, &$form_state) {
299 $owner = user_load(array('name' => $form_state['values']['username']));
300 $node = node_load($form_state['values']['aid']);
301 if (!(ad_owners_add($node, $owner->uid))) {
302 form_set_error('username', t('The user is already an owner of the ad.'));
303 }
304 else {
305 drupal_set_message(t('The user %username has been added as an owner of this advertisement.', array('%username' => $form_state['values']['username'])));
306 drupal_goto('node/'. $form_state['values']['aid'] .'/adowners/'. $owner->uid .'/permissions');
307 }
308 }
309
310 function ad_is_owner($aid, $account = NULL) {
311 global $user;
312 if (!isset($account)) {
313 $account = $user;
314 }
315 if (db_result(db_query('SELECT oid FROM {ad_owners} WHERE uid = %d AND aid = %d', $account->uid, $aid))) {
316 return 1;
317 }
318 else {
319 return 0;
320 }
321 }
322
323 /**
324 * Add an owner to an ad.
325 */
326 function ad_owners_add($node, $owner, $permissions = array()) {
327 $rc = 0;
328 $uid = is_numeric($owner) ? $owner : $owner->uid;
329 if (!db_result(db_query('SELECT oid FROM {ad_owners} WHERE aid = %d AND uid = %d', $node->nid, $uid))) {
330 db_query('INSERT INTO {ad_owners} (aid, uid) VALUES(%d, %d)', $node->nid, $uid);
331 $rc = db_affected_rows() ? 1 : 0;
332
333 if (!$permissions) {
334 $permissions = variable_get('ad_'. $node->adtype .'_default_permissions', array('access statistics', 'access click history', 'manage status'));
335 }
336
337 $oid = db_result(db_query("SELECT oid FROM {ad_owners} WHERE aid = %d and uid = %d", $node->nid, $uid));
338 db_query('DELETE FROM {ad_permissions} WHERE oid = %d', $oid);
339 db_query("INSERT INTO {ad_permissions} VALUES(%d, '%s')", $oid, implode('|,|', $permissions));
340 module_invoke_all('adowners', 'add', $node, array('oid' => $oid, 'uid' => $uid, 'aid' => $node->nid));
341 }
342 return $rc;
343 }
344
345 /**
346 * Create a unique host id for each ad owner, used when displaying ads remotely.
347 */
348 function ad_host_id_create($uid) {
349 $hostid = db_result(db_query('SELECT hostid FROM {ad_hosts} WHERE uid = %d', $uid));
350 if (!$hostid) {
351 $hostid = md5($uid . time());
352 db_query("INSERT INTO {ad_hosts} (uid, hostid) VALUES (%d, '%s')", $uid, md5($uid . time()));
353 }
354
355 return $hostid;
356 }
357
358 /**
359 * Removes ad owner from an ad.
360 */
361 function ad_owner_remove_form($form_state, $node, $owner) {
362 $form['aid'] = array(
363 '#type' => 'value',
364 '#value' => $node->nid,
365 );
366 $form['uid'] = array(
367 '#type' => 'value',
368 '#value' => $owner->uid,
369 );
370
371 return confirm_form($form,
372 t('Are you sure you want to remove user %name as an owner of this advertisement?', array('%name' => $owner->name)),
373 "node/$aid/adowners",
374 t('This action cannot be undone.'),
375 t('Remove'),
376 t('Cancel')
377 );
378 }
379
380 /**
381 * Don't allow the removal of the primary owner of the advertisement.
382 */
383 function ad_owner_remove_form_validate($form, &$form_state) {
384 $node = node_load($form_state['values']['aid']);
385 if ($node->uid == $form_state['values']['uid']) {
386 $owner = user_load(array('uid' => $form_state['values']['uid']));
387 drupal_set_message(t('%name is the primary owner of this advertisement. You cannot remove the primary owner.', array('%name' => $owner->name)), 'error');
388
389 $form_state['redirect'] = 'node/'. $form_state['values']['aid'] .'/adowners';
390 }
391 }
392
393 /**
394 * Remove the ad owner, and all associated permissions.
395 */
396 function ad_owner_remove_form_submit($form, &$form_state) {
397 $oid = db_result(db_query('SELECT oid FROM {ad_owners} WHERE aid = %d AND uid = %d', $form_state['values']['aid'], $form_state['values']['uid']));
398 db_query('DELETE FROM {ad_owners} WHERE oid = %d', $oid);
399 db_query('DELETE FROM {ad_permissions} WHERE oid = %d', $oid);
400 $owner = user_load(array('uid' => $form_state['values']['uid']));
401 module_invoke_all('adowners', 'remove', $oid, $owner);
402 drupal_set_message(t('The ad owner %name has been removed.', array('%name' => $owner->name)));
403
404 $form_state['redirect'] = 'node/'. $form_state['values']['aid'] .'/adowners';
405 }
406
407
408 /**
409 * Display a form with all available permissions and their status for the
410 * selected ad and ad owner.
411 */
412 function ad_owner_permissions_form($form_state, $node, $user) {
413 drupal_set_title(t('Permissions'));
414
415 $oid = db_result(db_query("SELECT oid FROM {ad_owners} WHERE aid = %d and uid = %d", $node->nid, $user->uid));
416 $granted = explode('|,|', db_result(db_query("SELECT permissions FROM {ad_permissions} WHERE oid = %d", $oid)));
417
418 $form['header'] = array(
419 '#type' => 'value',
420 '#value' => array(t('permissions'), t('granted'))
421 );
422
423 $rows = array();
424
425 $permissions = module_invoke_all('adapi', 'permissions', $node);
426 foreach ($permissions as $permission) {
427 $form['permission'][$permission] = array(
428 '#value' => t($permission),
429 );
430 $form['grant'][str_replace(' ', '_', $permission)] = array(
431 '#type' => 'checkbox',
432 '#default_value' => in_array($permission, $granted) ? 1 : 0,
433 );
434 }
435
436 $form['oid'] = array(
437 '#type' => 'hidden',
438 '#value' => $oid,
439 );
440
441 $form['aid'] = array(
442 '#type' => 'hidden',
443 '#value' => $node->nid,
444 );
445
446 $form['uid'] = array(
447 '#type' => 'hidden',
448 '#value' => $user->uid,
449 );
450
451 $form['submit'] = array(
452 '#type' => 'submit',
453 '#value' => t('Save'),
454 );
455
456 return $form;
457 }
458
459 /**
460 * Display ad owner permissions in a simple table.
461 */
462 function theme_ad_owner_permissions_form($form) {
463 $output = drupal_render($form['options']);
464 foreach (element_children($form['permission']) as $key) {
465 $row = array();
466 $row[] = drupal_render($form['permission'][$key]);
467 $row[] = drupal_render($form['grant'][str_replace(' ', '_', $key)]);
468 $rows[] = $row;
469 }
470
471 $output = theme('table', $form['header']['#value'], $rows);
472 $output .= drupal_render($form);
473 return $output;
474 }
475
476 /**
477 * Store the ad owner's updated permissions in the ad_permissions table.
478 */
479 function ad_owner_permissions_form_submit($form, &$form_state) {
480 $permissions = module_invoke_all('adapi', 'permissions', array());
481 $perms = array();
482 foreach ($permissions as $permission) {
483 $perm = str_replace(' ', '_', $permission);
484 if (isset($form_state['values'][$perm]) && $form_state['values'][$perm] > 0) {
485 $perms[] = $permission;
486 }
487 }
488 db_query('DELETE FROM {ad_permissions} WHERE oid = %d', $form_state['values']['oid']);
489 db_query("INSERT INTO {ad_permissions} VALUES(%d, '%s')", $form_state['values']['oid'], implode('|,|', $perms));
490
491 drupal_set_message(t('The permissions have been saved.'));
492 $form_state['redirect'] = 'node/'. $form_state['values']['aid'] .'/adowners';
493 }