You are here

function ad_image_adapi in Advertisement 7

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.3 image/ad_image.module \ad_image_adapi()
  4. 6 image/ad_image.module \ad_image_adapi()
  5. 6.2 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 224
Enhances the ad module to support banner ads.

Code

function ad_image_adapi($op, $node) {
  $output = NULL;
  switch ($op) {
    case 'load':
      $return = db_query("SELECT a.*, f.uri as filepath FROM {ad_image} a LEFT JOIN {file_managed} f ON a.fid = f.fid WHERE aid = :aid", array(
        ':aid' => $node->aid,
      ))
        ->fetchAssoc();
      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['filepath'] = $node->field_ad_image[$node->language][0]['uri'];
      return $return;
    case 'insert':
    case 'update':

      /*
            $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('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);
              }
            }
      */
      if ($op == 'insert') {
        db_query("INSERT INTO {ad_image} (aid, fid, url, tooltip, remote_image, width, height) VALUES(:aid, :fid, :url, :tooltip, :remote_image, :width, :height)", array(
          ':aid' => $node->nid,
          ':fid' => 0,
          //$node->fid, TODO
          ':url' => $node->url,
          ':tooltip' => $node->tooltip,
          ':remote_image' => isset($node->remote_image) ? $node->remote_image : '',
          ':width' => isset($node->width) ? $node->width : 0,
          ':height' => isset($node->height) ? $node->height : 0,
        ));
      }
      else {
        db_query("UPDATE {ad_image} SET fid = :fid, url = :url, tooltip = :tooltip, remote_image = :remote_image, width = :width, height = :height WHERE aid = :aid", array(
          ':fid' => 0,
          // $fid, TODO
          ':url' => $node->url,
          ':tooltip' => $node->tooltip,
          ':remote_image' => isset($node->remote_image) ? $node->remote_image : '',
          ':width' => isset($node->width) ? $node->width : 0,
          ':height' => isset($node->height) ? $node->height : 0,
          ':aid' => $node->nid,
        ));
      }

      // No valid image has been uploaded, don't allow ad to be 'active'.
      if (isset($node->remote_image) && $image === FALSE || !isset($node->remote_image) && ($image === FALSE || !ad_image_active_file($node->files))) {
        $result = db_query("UPDATE {ads} SET adstatus = :newstatus WHERE aid = :aid AND adstatus = :oldstatus", array(
          ':oldstatus' => t('pending'),
          ':aid' => $node->nid,
          ':newstatus' => t('active'),
        ));
        if ($result
          ->rowCount() < 1) {
          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 (!isset($node->remote_image) && !$fid) {
          $result = db_query("UPDATE {ads} SET adstatus = :newstatus WHERE aid = :aid AND adstatus = :oldstatus", array(
            ':oldstatus' => t('pending'),
            ':aid' => $node->nid,
            ':newstatus' => t('active'),
          ));
          if ($result
            ->rowCount() < 1) {
            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('You must specify a valid path for your remote advertisement.', 'error');
        }
      }
      else {
        if (!isset($node->field_ad_image)) {
          form_set_error('field_ad_image', 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 = :aid', array(
        ':aid' => $node->nid,
      ));
      break;
    case 'form':
      return _ad_image_node_form($node);
    case 'view':
      return _ad_image_node_view($node);
    case 'redirect':
      return db_query('SELECT url FROM {ad_image} WHERE aid = :aid', array(
        ':aid' => $node->nid,
      ))
        ->fetchField();
    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('image')) {
        drupal_set_message(t("The required <em>image module</em> is not enabled, you will not be able to upload image ads.  Please %enable the image 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;
}