You are here

function ad_image_adapi in Advertisement 6.3

Same name and namespace in other branches
  1. 5.2 image/ad_image.module \ad_image_adapi()
  2. 5 image/ad_image.module \ad_image_adapi()
  3. 6 image/ad_image.module \ad_image_adapi()
  4. 6.2 image/ad_image.module \ad_image_adapi()
  5. 7 image/ad_image.module \ad_image_adapi()

Implementation of hook_adapi().

1 call to ad_image_adapi()
ad_image_node_form in image/ad_image.module
Adapi helper function for displaying a node form.

File

image/ad_image.module, line 228
Enhances the ad module to support banner ads.

Code

function ad_image_adapi($op, $node) {
  $output = NULL;
  switch ($op) {
    case 'load':
      $return = db_fetch_array(db_query("SELECT a.*, f.filepath FROM {ad_image} a LEFT JOIN {files} f ON a.fid = f.fid WHERE aid = %d", $node['aid']));
      if (isset($return['remote_image']) && !empty($return['remote_image'])) {
        $path = $return['remote_image'];
      }
      else {
        $path = file_create_url($return['filepath']);
      }
      $return['ad'] = '<img src="' . $path . '" width="' . $return['width'] . '" height="' . $return['height'] . '" alt="' . check_plain($return['tooltip']) . '" />';
      return $return;
    case 'insert':
    case 'update':
      if (!property_exists($node, 'remote_image')) {
        $node->remote_image = NULL;
      }
      $fid = isset($node->files) ? (int) ad_image_active_file($node->files) : 0;
      $image = ad_image_load_image($node);

      // This is ugly, but as "a" comes before "u" we don't seem to be able
      // to modify the upload module's form.  Instead, we check after the fact
      // if someone is editing images when they're not allowed, and if so we
      // prevent the ad from being saved.
      if ($op == 'update' && !ad_permission($node->nid, 'manage active image')) {

        // See if fid is changing -- it's okay if new images are uploaded, it's
        // just not okay if the active fid is changed.
        if ($fid != $image->fid) {
          drupal_set_message(t('You do not have the necessary permissions to change the active advertisement.'), 'error');

          // This causes upload_save() to simply return without making any
          // changes to the files attached to this node.
          unset($node->files);
        }
      }
      else {

        // Check that all values are valid -- this is a kludge to work around
        // bug #146147 until the problem is better understood.
        $width = isset($image->width) ? $image->width : 0;
        $height = isset($image->height) ? $image->height : 0;
        $fid = isset($image->fid) ? $image->fid : 0;
        if ($image !== FALSE && $width != 0 && $height != 0 && ($fid != 0 || $node->remote_image)) {
          $node->fid = isset($image->fid) ? $image->fid : 0;
          $node->width = $image->width;
          $node->height = $image->height;
        }
        else {
          $image = FALSE;
        }
      }
      if ($op == 'insert') {
        db_query("INSERT INTO {ad_image} (aid, fid, url, tooltip, remote_image, width, height) VALUES (%d, %d, '%s', '%s', '%s', %d, %d)", $node->nid, $node->fid, $node->url, $node->tooltip, $node->remote_image, isset($node->width) ? $node->width : 0, isset($node->height) ? $node->height : 0);
      }
      else {
        db_query("UPDATE {ad_image} SET fid = %d, url = '%s', tooltip = '%s', remote_image = '%s', width = %d, height = %d WHERE aid = %d", $fid, $node->url, $node->tooltip, $node->remote_image, isset($node->width) ? $node->width : 0, isset($node->height) ? $node->height : 0, $node->nid);
      }

      // No valid image has been uploaded, don't allow ad to be 'active'.
      if ($node->remote_image && $image === FALSE || !$node->remote_image && ($image === FALSE || !ad_image_active_file($node->files))) {
        db_query("UPDATE {ads} SET adstatus = '%s' WHERE aid = %d AND adstatus = '%s'", t('pending'), $node->nid, t('active'));
        if (db_affected_rows()) {
          drupal_set_message(t('Image validation failed, unable to mark ad as %active.  Setting ad as %pending.', array(
            '%active' => t('active'),
            '%pending' => t('pending'),
          )), 'error');
        }
      }
      else {
        if (!$node->remote_image && !$fid) {
          db_query("UPDATE {ads} SET adstatus = '%s' WHERE aid = %d AND adstatus = '%s'", t('pending'), $node->nid, t('active'));
          if (db_affected_rows()) {
            drupal_set_message(t('Unable to mark ad as <em>active</em> until uploaded image is validated.  If you do not see any more errors, you should now be able to set your ad as <em>active</em>.'), 'error');
          }
        }
      }
      break;
    case 'validate':
      if (isset($node->remote_image) && !empty($node->remote_image)) {
        if (variable_get('ad_validate_url', 1) && !valid_url($node->url, TRUE)) {
          drupal_set_message(t('You must specify a valid path for your remote advertisement.'), 'error');
        }
      }
      else {
        if (!isset($node->files) || !ad_image_active_file($node->files)) {
          form_set_error('upload', t('It is required that you upload an image for your image advertisement.'));
        }
      }
      if ($node->url && variable_get('ad_validate_url', 1) && !valid_url($node->url, TRUE)) {
        form_set_error('url', t('You must specify a valid %field.', array(
          '%field' => t('Destination URL'),
        )));
      }
      break;
    case 'delete':
      db_query('DELETE FROM {ad_image} WHERE aid = %d', $node->nid);
      break;
    case 'form':
      return ad_image_node_form($node);
    case 'view':
      return ad_image_node_view($node);
    case 'redirect':
      return db_result(db_query('SELECT url FROM {ad_image} WHERE aid = %d', $node->nid));
    case 'type':
      return array(
        'image' => array(
          'name' => t('Image ad'),
          'module' => 'ad_image',
          'description' => t('An image or banner advertisement.'),
          'help' => t('An image or banner advertisement.'),
        ),
      );
    case 'permissions':
      if (!isset($node->adtype) || $node->adtype == 'image') {
        return array(
          'manage active image' => TRUE,
        );
      }
    case 'check_install':
      if (!module_exists('upload')) {
        drupal_set_message(t("The required <em>upload module</em> is not enabled, you will not be able to upload image ads.  Please %enable the upload module, or %disable the ad_image module.", array(
          '%enable' => l('enable', 'admin/modules'),
          '%disable' => l('disable', 'admin/modules'),
        )), 'error');
      }
      if (is_object($node) && !variable_get("upload_{$node->type}", TRUE)) {
        drupal_set_message(t('You will not be able to upload image ads until you !enable for the advertisement content type.', array(
          '!enable' => l(t('enable attachments'), 'admin/content/types/ad'),
        )), 'error');
      }
      if (empty($node)) {
        if (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PRIVATE) {
          drupal_set_message(t('Your website is configured to use Drupal\'s private !method.  You have to enable "!view" permissions in addition to the ad module\'s standard "!show" permissions for all roles that you wish to view image advertisements.', array(
            '!method' => l(t('download method'), 'admin/settings/file-system'),
            '!view' => l(t('view uploaded files'), 'admin/user/access'),
            '!show' => l(t('show advertisements'), 'admin/user/access'),
          )));
        }
      }
      break;
  }
  return $output;
}