You are here

function ad_permission_owners_add in Advertisement 5.2

Add an owner to an ad.

4 calls to ad_permission_owners_add()
ad_operations_callback in ./ad.module
Callback function for admin mass approving ads. TODO: Update activated and expired when appropriate. TODO: Publish/unpublish nodes when appropriate.
ad_permission_nodeapi in permission/ad_permission.module
Drupal _nodeapi hook.
ad_permission_owners_overview in permission/ad_permission.module
TODO: Make this themeable. TODO: Group permissions by module. TODO: Allow modules to define default value for permission.
ad_permission_owner_add_form_submit in permission/ad_permission.module

File

permission/ad_permission.module, line 257
Provide granular permissions for advertisements.

Code

function ad_permission_owners_add($aid, $uid, $permissions = array()) {
  $node = node_load($aid);
  if ($GLOBALS['db_type'] == 'pgsql') {
    db_query('START TRANSACTION;');
  }
  else {

    // MySQL, MySQLi
    db_query('LOCK TABLES {ad_owners} WRITE');
  }
  if (!db_result(db_query('SELECT oid FROM {ad_owners} WHERE uid = %d AND aid = %d', $uid, $aid))) {
    db_query('INSERT INTO {ad_owners} (aid, uid) VALUES(%d, %d)', $aid, $uid);
    $rc = db_affected_rows() ? 1 : 0;
    if (!$permissions) {

      // use default permissions
      $perms = array();
      $permissions = module_invoke_all('adapi', 'permissions', $node);
      foreach ($permissions as $permission => $default) {
        if ($default) {
          $perms[] = $permission;
        }
      }
      $permissions = variable_get('ad_' . $node->adtype . '_default_permissions', $perms);
    }
    $oid = db_result(db_query("SELECT oid FROM {ad_owners} WHERE aid = %d and uid = %d", $aid, $uid));
    if ($GLOBALS['db_type'] == 'pgsql') {
      db_query('START TRANSACTION;');
    }
    else {

      // MySQL, MySQLi
      db_query('LOCK TABLES {ad_permissions} WRITE');
    }
    db_query('DELETE FROM {ad_permissions} WHERE oid = %d', $oid);
    db_query("INSERT INTO {ad_permissions} VALUES(%d, '%s')", $oid, implode('|,|', $permissions));
    module_invoke_all('adowners', 'add', $node, array(
      'oid' => $oid,
      'uid' => $uid,
      'aid' => $aid,
    ));
  }
  if ($GLOBALS['db_type'] == 'pgsql') {
    db_query('COMMIT;');
  }
  else {

    // MySQL, MySQLi
    db_query('UNLOCK TABLES');
  }
  return $rc;
}