comparison owners/ad_owners.module @ 1:948362c2a207 ad

update advertisement
author pierre
date Thu, 02 Apr 2009 15:28:21 +0000
parents d8a3998dac8e
children e5584a19768b
comparison
equal deleted inserted replaced
0:d8a3998dac8e 1:948362c2a207
1 <?php 1 <?php
2 // $Id: ad_owners.module,v 1.1.2.8 2009/02/17 04:34:27 jeremy Exp $ 2 // $Id: ad_owners.module,v 1.1.2.8.2.6 2009/03/31 04:41:03 jeremy Exp $
3 3
4 /** 4 /**
5 * @file 5 * @file
6 * Enhances the ad module to support ad owners. 6 * Enhances the ad module to support ad owners.
7 * 7 *
8 * Copyright (c) 2009. 8 * Copyright (c) 2008-2009.
9 * Jeremy Andrews <jeremy@tag1consulting.com>. 9 * Jeremy Andrews <jeremy@tag1consulting.com>.
10 */ 10 */
11 11
12 /** 12 /**
13 * Implementation of hook_theme(). 13 * Implementation of hook_theme().
37 'type' => MENU_LOCAL_TASK, 37 'type' => MENU_LOCAL_TASK,
38 'weight' => 5, 38 'weight' => 5,
39 ); 39 );
40 $items['node/%node/adowners/list'] = array( 40 $items['node/%node/adowners/list'] = array(
41 'title' => 'List', 41 'title' => 'List',
42 'access callback' => 'ad_adaccess', 42 'access callback' => 'ad_permission',
43 'access arguments' => array(1, 'manage owners'), 43 'access arguments' => array(1, 'manage owners'),
44 'type' => MENU_DEFAULT_LOCAL_TASK, 44 'type' => MENU_DEFAULT_LOCAL_TASK,
45 'weight' => 0, 45 'weight' => 0,
46 ); 46 );
47 $items['node/%node/adowners/%user/permissions'] = array( 47 $items['node/%node/adowners/%user/permissions'] = array(
48 'title callback' => 'owner_permissions_title', 48 'title callback' => 'owner_permissions_title',
49 'title arguments' => array('!owner' => 3), 49 'title arguments' => array('!owner' => 3),
50 'page callback' => 'drupal_get_form', 50 'page callback' => 'drupal_get_form',
51 'page arguments' => array('ad_owner_permissions_form', 1, 3), 51 'page arguments' => array('ad_owner_permissions_form', 1, 3),
52 'access callback' => 'ad_adaccess', 52 'access callback' => 'ad_permission',
53 'access arguments' => array(1, 'manage owners'), 53 'access arguments' => array(1, 'manage owners'),
54 'type' => MENU_LOCAL_TASK, 54 'type' => MENU_LOCAL_TASK,
55 'weight' => 2, 55 'weight' => 2,
56 ); 56 );
57 $items['node/%node/adowners/%user/remove'] = array( 57 $items['node/%node/adowners/%user/remove'] = array(
58 'title' => 'Remove owner', 58 'title' => 'Remove owner',
59 'page callback' => 'drupal_get_form', 59 'page callback' => 'drupal_get_form',
60 'page arguments' => array('ad_owner_remove_form', 1, 3), 60 'page arguments' => array('ad_owner_remove_form', 1, 3),
61 'access callback' => 'ad_adaccess', 61 'access callback' => 'ad_permission',
62 'access arguments' => array(1, 'manage owners'), 62 'access arguments' => array(1, 'manage owners'),
63 'type' => MENU_CALLBACK, 63 'type' => MENU_CALLBACK,
64 'weight' => 6, 64 'weight' => 6,
65 ); 65 );
66 $items['node/%node/adowners/add'] = array( 66 $items['node/%node/adowners/add'] = array(
67 'title' => 'Add owner', 67 'title' => 'Add owner',
68 'page callback' => 'drupal_get_form', 68 'page callback' => 'drupal_get_form',
69 'page arguments' => array('ad_owners_add_form', 1), 69 'page arguments' => array('ad_owners_add_form', 1),
70 'access callback' => 'ad_adaccess', 70 'access callback' => 'ad_permission',
71 'access arguments' => array(1, 'manage owners'), 71 'access arguments' => array(1, 'manage owners'),
72 'type' => MENU_LOCAL_TASK, 72 'type' => MENU_LOCAL_TASK,
73 'weight' => 4, 73 'weight' => 4,
74 ); 74 );
75 75
76 return $items; 76 return $items;
77 } 77 }
78 78
79 /** 79 /**
80 * Implementation of hook_perm().
81 */
82 function ad_owners_perm() {
83 return array('grant default per ad type permissions');
84 }
85
86 /**
80 * Menu item access callback. 87 * Menu item access callback.
81 */ 88 */
82 function ad_owners_access($node) { 89 function ad_owners_access($node) {
83 return ($node->type == 'ad') && ad_adaccess($node, 'manage owners'); 90 return ($node->type == 'ad') && ad_permission($node->nid, 'manage owners');
84 } 91 }
85 92
86 /** 93 /**
87 * Menu item title callback - use the user name 94 * Menu item title callback - use the user name
88 */ 95 */
89 function owner_permissions_title($account) { 96 function owner_permissions_title($account) {
90 return t('!owner\'s permissions', array('!owner' => $account->name)); 97 return t('!owner\'s permissions', array('!owner' => $account->name));
98 }
99
100 /**
101 * Return array default permissions.
102 */
103 function ad_owners_default_permissions() {
104 $permissions = module_invoke_all('adapi', 'permissions', NULL);
105 $all = array();
106 $perms = array();
107 foreach ($permissions as $permission => $default) {
108 if ($default) {
109 $perms[] = $permission;
110 }
111 $all[] = $permission;
112 }
113 return array('default' => $perms, 'all' => $all);
91 } 114 }
92 115
93 /** 116 /**
94 * Implementation of hook_form_alter(). 117 * Implementation of hook_form_alter().
95 */ 118 */
96 function ad_owners_form_alter(&$form, &$form_state, $form_id) { 119 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') { 120 if ($form_id == 'ad_'. arg(4) .'_global_settings' || $form_id == 'ad_no_global_settings') {
98 if (!isset($form['adtype'])) { 121 if (!isset($form['adtype'])) {
99 $form['adtype'] = array('#type' => 'value', '#value' => arg(4)); 122 $form['adtype'] = array('#type' => 'value', '#value' => arg(4));
100 } 123 }
101 $permissions = module_invoke_all('adapi', 'permissions', NULL); 124
125 $perms = ad_owners_default_permissions();
102 $form['permissions'] = array( 126 $form['permissions'] = array(
103 '#type' => 'fieldset', 127 '#type' => 'fieldset',
104 '#title' => t('Permissions'), 128 '#title' => t('Permissions'),
105 '#collapsible' => TRUE, 129 '#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)))), 130 '#description' => t('Select which permissions will be automatically granted to new owners of <em>!type</em> advertisements, per role. If a user is a member of multiple roles, he will get all default permissions defined for each role he is a member of.', array('!type' => ad_get_types('name', arg(4)))),
107 ); 131 );
108 $form['permissions']['default_permissions'] = array( 132
109 '#type' => 'checkboxes', 133 $roles = user_roles(TRUE);
110 '#title' => t('Default permissions for <em>!type</em> owners', array('!type' => ad_get_types('name', arg(4)))), 134 foreach ($roles as $rid => $role) {
111 '#options' => drupal_map_assoc($permissions), 135 $defaults = variable_get("ad_default_permissions_{$rid}_". $form['adtype']['#value'], $perms['default']);
112 '#default_value' => variable_get('ad_'. arg(4) .'_default_permissions', array('access statistics', 'access click history', 'manage status')), 136 $form['permissions']["role-$rid"] = array(
113 ); 137 '#type' => 'fieldset',
138 '#title' => $role,
139 '#collapsible' => TRUE,
140 '#collapsed' => TRUE,
141 );
142 $form['permissions']["role-$rid"]["default_permissions_$rid"] = array(
143 '#type' => 'checkboxes',
144 '#title' => t('Default permissions for users in the <em>!role</em> role', array('!role' => $role)),
145 '#options' => drupal_map_assoc($perms['all']),
146 '#default_value' => $defaults,
147 );
148 }
114 if (isset($form['save'])) { 149 if (isset($form['save'])) {
115 $form['save']['#weight'] = 10; 150 $form['save']['#weight'] = 10;
116 } 151 }
117 if (isset($form['#submit']) && is_array($form['#submit'])) { 152 if (isset($form['#submit']) && is_array($form['#submit'])) {
118 $form['#submit'] = array('ad_global_settings_submit') + $form['#submit']; 153 $form['#submit'] = array_merge(array('ad_owners_settings_submit'), $form['#submit']);
119 } 154 }
120 else { 155 else {
121 $form['#submit'] = array('ad_global_settings_submit'); 156 $form['#submit'] = array('ad_owners_settings_submit');
122 } 157 }
123 } 158 }
159 }
160
161 /**
162 * Submit handler for global settings of all ad types.
163 * @see ad_form_alter()
164 */
165 function ad_owners_settings_submit($form, $form_state) {
166 $roles = user_roles(TRUE);
167 foreach ($roles as $rid => $role) {
168 variable_set("ad_default_permissions_{$rid}_". $form_state['values']['adtype'], $form_state['values']["default_permissions_$rid"]);
169 unset($form_state['values']["default_permissions_$rid"]);
170 }
171 unset($form_state['values']['adtype']);
124 } 172 }
125 173
126 /** 174 /**
127 * Implementation of hook_nodeapi(). 175 * Implementation of hook_nodeapi().
128 */ 176 */
133 case 'insert': 181 case 'insert':
134 case 'update': 182 case 'update':
135 if (isset($node->adtype)) { 183 if (isset($node->adtype)) {
136 // Be sure ad owner has at least default ad permissions. 184 // Be sure ad owner has at least default ad permissions.
137 ad_owners_add($node, $node->uid); 185 ad_owners_add($node, $node->uid);
138 ad_host_id_create($node->uid); 186 ad_owners_create_hostid($node->uid);
139 } 187 }
140 break; 188 break;
141 case 'delete': 189 case 'delete':
142 // Clean up ad_permissions and any other per-ad tables. 190 // 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); 191 $result = db_query('SELECT oid, uid FROM {ad_owners} WHERE aid = %d', $node->nid);
156 * Implementation of hook_adapi(). 204 * Implementation of hook_adapi().
157 */ 205 */
158 function ad_owners_adapi($op, $node = NULL) { 206 function ad_owners_adapi($op, $node = NULL) {
159 switch ($op) { 207 switch ($op) {
160 case 'permissions': 208 case 'permissions':
161 return array('manage owners'); 209 return array('manage owners' => FALSE);
162 break; 210 break;
163 } 211 }
164 } 212 }
165 213
166 /** 214 /**
305 drupal_set_message(t('The user %username has been added as an owner of this advertisement.', array('%username' => $form_state['values']['username']))); 353 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'); 354 drupal_goto('node/'. $form_state['values']['aid'] .'/adowners/'. $owner->uid .'/permissions');
307 } 355 }
308 } 356 }
309 357
310 function ad_is_owner($aid, $account = NULL) { 358 function is_ad_owner($aid, $account = NULL) {
311 global $user; 359 global $user;
312 if (!isset($account)) { 360 if (!isset($account)) {
313 $account = $user; 361 $account = $user;
314 } 362 }
315 if (db_result(db_query('SELECT oid FROM {ad_owners} WHERE uid = %d AND aid = %d', $account->uid, $aid))) { 363 if (db_result(db_query('SELECT oid FROM {ad_owners} WHERE uid = %d AND aid = %d', $account->uid, $aid))) {
328 $uid = is_numeric($owner) ? $owner : $owner->uid; 376 $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))) { 377 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); 378 db_query('INSERT INTO {ad_owners} (aid, uid) VALUES(%d, %d)', $node->nid, $uid);
331 $rc = db_affected_rows() ? 1 : 0; 379 $rc = db_affected_rows() ? 1 : 0;
332 380
333 if (!$permissions) { 381 if (empty($permissions)) {
334 $permissions = variable_get('ad_'. $node->adtype .'_default_permissions', array('access statistics', 'access click history', 'manage status')); 382 // build permissions array from defaults
383 $perms = ad_owners_default_permissions();
384 $owner = user_load($uid);
385 if (is_array($owner->roles)) {
386 foreach ($owner->roles as $rid => $role) {
387 $default = variable_get("ad_default_permissions_{$rid}_". $node->adtype, $perms['default']);
388 $new = array();
389 foreach ($default as $key => $value) {
390 if ($value) {
391 $new[] = $value;
392 }
393 }
394 $permissions = $permissions + $new;
395 }
396 }
335 } 397 }
336 398
337 $oid = db_result(db_query("SELECT oid FROM {ad_owners} WHERE aid = %d and uid = %d", $node->nid, $uid)); 399 $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); 400 db_query('DELETE FROM {ad_permissions} WHERE oid = %d', $oid);
339 db_query("INSERT INTO {ad_permissions} VALUES(%d, '%s')", $oid, implode('|,|', $permissions)); 401 db_query("INSERT INTO {ad_permissions} VALUES(%d, '%s')", $oid, implode('|,|', $permissions));
343 } 405 }
344 406
345 /** 407 /**
346 * Create a unique host id for each ad owner, used when displaying ads remotely. 408 * Create a unique host id for each ad owner, used when displaying ads remotely.
347 */ 409 */
348 function ad_host_id_create($uid) { 410 function ad_owners_create_hostid($uid) {
349 $hostid = db_result(db_query('SELECT hostid FROM {ad_hosts} WHERE uid = %d', $uid)); 411 $hostid = db_result(db_query('SELECT hostid FROM {ad_hosts} WHERE uid = %d', $uid));
350 if (!$hostid) { 412 if (!$hostid) {
351 $hostid = md5($uid . time()); 413 $hostid = md5($uid . time());
352 db_query("INSERT INTO {ad_hosts} (uid, hostid) VALUES (%d, '%s')", $uid, md5($uid . time())); 414 db_query("INSERT INTO {ad_hosts} (uid, hostid) VALUES (%d, '%s')", $uid, md5($uid . time()));
353 } 415 }
368 '#value' => $owner->uid, 430 '#value' => $owner->uid,
369 ); 431 );
370 432
371 return confirm_form($form, 433 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)), 434 t('Are you sure you want to remove user %name as an owner of this advertisement?', array('%name' => $owner->name)),
373 "node/$aid/adowners", 435 "node/$node->nid/adowners",
374 t('This action cannot be undone.'), 436 t('This action cannot be undone.'),
375 t('Remove'), 437 t('Remove'),
376 t('Cancel') 438 t('Cancel')
377 ); 439 );
378 } 440 }
415 $oid = db_result(db_query("SELECT oid FROM {ad_owners} WHERE aid = %d and uid = %d", $node->nid, $user->uid)); 477 $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))); 478 $granted = explode('|,|', db_result(db_query("SELECT permissions FROM {ad_permissions} WHERE oid = %d", $oid)));
417 479
418 $form['header'] = array( 480 $form['header'] = array(
419 '#type' => 'value', 481 '#type' => 'value',
420 '#value' => array(t('permissions'), t('granted')) 482 '#value' => array(t('permission'), t('granted'))
421 ); 483 );
422 484
423 $rows = array(); 485 $rows = array();
424 486
425 $permissions = module_invoke_all('adapi', 'permissions', $node); 487 $permissions = module_invoke_all('adapi', 'permissions', $node);
426 foreach ($permissions as $permission) { 488 foreach ($permissions as $permission => $default) {
427 $form['permission'][$permission] = array( 489 $form['permission'][$permission] = array(
428 '#value' => t($permission), 490 '#value' => t($permission),
429 ); 491 );
430 $form['grant'][str_replace(' ', '_', $permission)] = array( 492 $form['grant'][str_replace(' ', '_', $permission)] = array(
431 '#type' => 'checkbox', 493 '#type' => 'checkbox',
477 * Store the ad owner's updated permissions in the ad_permissions table. 539 * Store the ad owner's updated permissions in the ad_permissions table.
478 */ 540 */
479 function ad_owner_permissions_form_submit($form, &$form_state) { 541 function ad_owner_permissions_form_submit($form, &$form_state) {
480 $permissions = module_invoke_all('adapi', 'permissions', array()); 542 $permissions = module_invoke_all('adapi', 'permissions', array());
481 $perms = array(); 543 $perms = array();
482 foreach ($permissions as $permission) { 544 foreach ($permissions as $permission => $default) {
483 $perm = str_replace(' ', '_', $permission); 545 $perm = str_replace(' ', '_', $permission);
484 if (isset($form_state['values'][$perm]) && $form_state['values'][$perm] > 0) { 546 if (isset($form_state['values'][$perm]) && $form_state['values'][$perm] > 0) {
485 $perms[] = $permission; 547 $perms[] = $permission;
486 } 548 }
487 } 549 }
489 db_query("INSERT INTO {ad_permissions} VALUES(%d, '%s')", $form_state['values']['oid'], implode('|,|', $perms)); 551 db_query("INSERT INTO {ad_permissions} VALUES(%d, '%s')", $form_state['values']['oid'], implode('|,|', $perms));
490 552
491 drupal_set_message(t('The permissions have been saved.')); 553 drupal_set_message(t('The permissions have been saved.'));
492 $form_state['redirect'] = 'node/'. $form_state['values']['aid'] .'/adowners'; 554 $form_state['redirect'] = 'node/'. $form_state['values']['aid'] .'/adowners';
493 } 555 }
556
557 /**
558 * Determine whether the user has a given privilege.
559 */
560 function ad_owners_permission($aid, $string, $account) {
561 static $permissions = array();
562
563 if (!isset($permissions[$aid][$account->uid])) {
564 $oid = db_result(db_query("SELECT oid FROM {ad_owners} WHERE aid = %d and uid = %d", $aid, $account->uid));
565 if ($oid) {
566 $permissions[$aid][$account->uid] = explode('|,|', db_result(db_query("SELECT permissions FROM {ad_permissions} WHERE oid = %d", $oid)));
567 }
568 else {
569 $account = user_load($account->uid);
570 if (user_access('grant default per ad type permissions')) {
571 $perms = ad_owners_default_permissions();
572 $permissions[$aid][$account->uid] = $perms['default'];
573 }
574 }
575 }
576
577 return (in_array("$string", $permissions[$aid][$account->uid]));
578 }