You are here

function ad_owners_add in Advertisement 7

Same name and namespace in other branches
  1. 5 ad.module \ad_owners_add()
  2. 6.3 owners/ad_owners.module \ad_owners_add()
  3. 6 owners/ad_owners.module \ad_owners_add()
  4. 6.2 owners/ad_owners.module \ad_owners_add()

Add an owner to an ad.

3 calls to ad_owners_add()
ad_owners_add_form_submit in owners/ad_owners.module
ad_owners_nodeapi in owners/ad_owners.module
Implementation of hook_nodeapi().
ad_owners_overview in owners/ad_owners.module
TODO: Make this themeable. TODO: Group permissions by module. TODO: Allow modules to define default value for permission.

File

owners/ad_owners.module, line 373
Enhances the ad module to support ad owners.

Code

function ad_owners_add($node, $owner, $permissions = array()) {
  $rc = 0;
  $uid = is_numeric($owner) ? $owner : $owner->uid;
  if (!db_query('SELECT oid FROM {ad_owners} WHERE aid = %d AND uid = %d', $node->nid, $uid)
    ->fetchField()) {
    db_query('INSERT INTO {ad_owners} (aid, uid) VALUES(%d, %d)', $node->nid, $uid);
    $rc = db_affected_rows() ? 1 : 0;
    if (empty($permissions)) {

      // build permissions array from defaults
      $perms = ad_owners_default_permissions();
      $owner = user_load($uid);
      if (is_array($owner->roles)) {
        foreach ($owner->roles as $rid => $role) {
          $default = variable_get("ad_default_permissions_{$rid}_" . $node->adtype, $perms['default']);
          $new = array();
          foreach ($default as $key => $value) {
            if ($value) {
              $new[] = $value;
            }
          }
          $permissions = $permissions + $new;
        }
      }
    }
    $oid = db_query("SELECT oid FROM {ad_owners} WHERE aid = %d and uid = %d", $node->nid, $uid)
      ->fetchField();
    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' => $node->nid,
    ));
  }
  return $rc;
}