You are here

gallery_assist_form.inc in Gallery Assist 7

GA node settings, upload, sorting and gallery items management forms.

File

gallery_assist_form.inc
View source
<?php

/**
 * @file
 * GA node settings, upload, sorting and gallery items management forms.
 */

/**
 * Form builder; GA settings form for a node gallerie.
 * 
 * @param $node
 *   Object containing the node object.
 * @param $allowed
 *   Associative array containing GA internally permisions.
 * 
 * @ingroup forms
 * @see gallery_assist_node_settings_form_submit()
 */
function gallery_assist_node_settings_form($node, $allowed = array()) {
  global $base_path;
  $form = array(
    //    '#tree' => TRUE,
    'supertest' => array(
      '#type' => 'vertical_tabs',
    ),
  );
  $form['order'] = array(
    '#type' => 'fieldset',
    '#title' => 'Order',
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#group' => 'supertest',
  );
  $form['order']['db_order_field'] = array(
    '#type' => 'select',
    '#title' => t('Order field'),
    '#options' => array(
      'p.weight' => t('weight'),
    ),
    '#default_value' => $node->ga_conf[$node->type]['db_order_field'],
  );
  $form['order']['db_order_direction'] = array(
    '#type' => 'select',
    '#title' => t('Order'),
    '#options' => array(
      'ASC' => t('ascending'),
      'DESC' => t('descending'),
    ),
    '#default_value' => $node->ga_conf[$node->type]['db_order_direction'],
  );
  $form['order']['gallery_assist_weight'] = array(
    '#type' => 'select',
    '#title' => t('Container weight'),
    '#options' => drupal_map_assoc(range(-50, 50, 1)),
    '#default_value' => $node->ga_conf[$node->type]['gallery_assist_weight'],
  );
  $form['align_float'] = array(
    '#type' => 'fieldset',
    '#title' => 'Align, Float',
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#group' => 'supertest',
  );
  $form['align_float']['teaser_container_align'] = array(
    '#type' => 'select',
    '#title' => 'Teaser align',
    '#default_value' => $node->ga_conf[$node->type]['teaser_container_align'],
    '#options' => array(
      'center' => 'center',
      'left' => 'left',
      'right' => 'right',
    ),
  );
  $form['align_float']['page_container_align'] = array(
    '#type' => 'select',
    '#title' => 'Page align',
    '#default_value' => $node->ga_conf[$node->type]['page_container_align'],
    '#options' => array(
      'center' => 'center',
      'left' => 'left',
      'right' => 'right',
    ),
  );
  $form['align_float']['preview_container_align'] = array(
    '#type' => 'select',
    '#title' => 'Preview align',
    '#default_value' => $node->ga_conf[$node->type]['preview_container_align'],
    '#options' => array(
      'center' => 'center',
      'left' => 'left',
      'right' => 'right',
    ),
  );
  $form['align_float']['teaser_container_float'] = array(
    '#type' => 'select',
    '#title' => 'Teaser float',
    '#default_value' => $node->ga_conf[$node->type]['teaser_container_float'],
    '#options' => array(
      'none' => 'none',
      'left' => 'left',
      'right' => 'right',
    ),
  );
  $form['align_float']['page_container_float'] = array(
    '#type' => 'select',
    '#title' => 'Page float',
    '#default_value' => $node->ga_conf[$node->type]['page_container_float'],
    '#options' => array(
      'none' => 'none',
      'left' => 'left',
      'right' => 'right',
    ),
  );
  $form['align_float']['preview_container_float'] = array(
    '#type' => 'select',
    '#title' => 'Preview float',
    '#default_value' => $node->ga_conf[$node->type]['preview_container_float'],
    '#options' => array(
      'none' => 'none',
      'left' => 'left',
      'right' => 'right',
    ),
  );
  $form['behavior'] = array(
    '#type' => 'fieldset',
    '#title' => 'Behavior',
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#group' => 'supertest',
  );
  $form['behavior']['ga_public_status'] = array(
    '#type' => 'select',
    '#title' => 'Public status',
    '#default_value' => $node->ga_public_status,
    '#options' => array(
      0 => 'public',
      1 => 'privat',
      2 => 'public for all',
    ),
  );
  $form['behavior']['thm_linked_to'] = array(
    '#type' => 'select',
    '#title' => 'Thumbnail links to...',
    '#default_value' => $node->ga_conf[$node->type]['thm_linked_to'],
    '#options' => array(
      'preview' => 'preview',
      'node' => 'page',
    ),
  );
  $form['behavior']['thm_link_type'] = array(
    '#type' => 'select',
    '#title' => 'Thumbnail link type',
    '#default_value' => $node->ga_conf[$node->type]['thm_link_type'],
    '#options' => array(
      'default' => 'default',
    ),
  );
  $form['behavior']['display_option'] = array(
    '#type' => 'select',
    '#title' => 'Display option',
    '#default_value' => $node->ga_conf[$node->type]['display_option'],
    '#options' => array(
      'grid' => 'grid',
      'list' => 'list',
    ),
  );
  $form['behavior']['ga_shadow'] = array(
    '#type' => 'checkbox',
    '#title' => 'Shadow',
    '#default_value' => $node->ga_conf[$node->type]['ga_shadow'],
  );
  $form['behavior']['display_download_link'] = array(
    '#type' => 'checkbox',
    '#title' => 'Download',
    '#default_value' => $node->ga_conf[$node->type]['display_download_link'],
  );
  $form['behavior']['in_profile'] = array(
    '#type' => 'checkbox',
    '#title' => 'List in profile',
    '#default_value' => $node->ga_conf[$node->type]['in_profile'],
  );
  $form['behavior']['show_title'] = array(
    '#type' => 'checkbox',
    '#title' => 'Show titles',
    '#default_value' => $node->ga_conf[$node->type]['show_title'],
  );
  $form['behavior']['show_in_homepage_block'] = array(
    '#type' => 'checkbox',
    '#title' => 'Show in homepage block',
    '#default_value' => $node->ga_conf[$node->type]['show_in_homepage_block'],
  );
  $form['#submit'][] = 'gallery_assist_node_settings_form_submit';
  return $form;
}

/**
 * Process the GA settings form submission for a node gallerie.
 */
function gallery_assist_node_settings_form_submit($form, &$form_state) {

  //  dsm($form_state);
  $allowed = variable_get("gallery_assist_{$form_state['values']['node_type']}_allowed", array());
  $conf = array();
  foreach ($form_state['values'] as $k => $v) {

    //    foreach ($form_state['values'][$k] as $key => $val) {
    //      $conf[$key] = $val;
    //    }
    $conf[$k] = $v;
  }
  db_update('gallery_assist')
    ->fields(array(
    'in_profile' => $conf['in_profile'],
    'ga_public_status' => $conf['ga_public_status'],
    'show_title' => $conf['show_title'],
    'show_in_homepage_block' => $conf['show_in_homepage_block'],
    'gallery_assist_weight' => $conf['gallery_assist_weight'],
    'data' => serialize($conf),
  ))
    ->condition('gref', $form_state['values']['node_gref'])
    ->execute();
  drupal_set_message(t("The configuration options for this gallery have been saved."));
}

/**
 * Form builder; GA items data form.
 * 
 * @param $node
 *   Object containing the node object.
 * 
 * @ingroup forms
 * @see gallery_assist_items_form_submit()
 * 
 * @todo: Theme as table to a better overview.
 */
function gallery_assist_items_form($node) {
  $form = array(
    '#theme' => 'gallery_assist_items_form',
  );
  $form['pager_top'] = array(
    '#markup' => theme('pager', array(
      'tags' => NULL,
    )),
    '#weight' => -50,
  );
  $form['gallery_items'] = array(
    '#tree' => TRUE,
  );
  foreach ($node->ga_items as $item) {
    $variables = array(
      'path' => $item->opath,
      'alt' => $item->palt,
      'title' => $item->ptitle,
      'getsize' => FALSE,
      'style_name' => 'gallery_default_icon_75',
      'attributes' => array(
        'id' => $node->nid . '-' . $item->weight,
      ),
    );
    if (isset($item->info['top'])) {
      $variables['attributes']['style'] = 'margin-top:' . $item->info['top'] . 'px;';
    }
    $image = theme('image_style', $variables);

    //    $image = gallery_assist_thumbnail_link($node, $item, $image);
    $form['gallery_items'][$item->pid]['fid'] = array(
      '#type' => 'hidden',
      '#value' => $item->fid,
    );
    $form['gallery_items'][$item->pid]['pid'] = array(
      '#type' => 'hidden',
      '#value' => $item->pid,
    );
    $form['gallery_items'][$item->pid]['delete'] = array(
      '#type' => 'checkbox',
      '#title' => 'delete',
    );
    $form['gallery_items'][$item->pid]['image'] = array(
      '#markup' => $image,
    );
    $form['gallery_items'][$item->pid]['ptitle'] = array(
      '#type' => 'textfield',
      '#title' => 'Imagetitle',
      '#default_value' => $item->ptitle,
    );
    $form['gallery_items'][$item->pid]['palt'] = array(
      '#type' => 'textfield',
      '#title' => 'Image alt info',
      '#default_value' => $item->palt,
    );
    $form['gallery_items'][$item->pid]['pdescription'] = array(
      '#type' => 'textarea',
      '#title' => 'Image description',
      '#default_value' => $item->pdescription,
    );
    $form['gallery_items'][$item->pid]['cover'] = array(
      '#type' => 'radio',
      '#name' => "coverimage[{$item->pid}]",
      '#attributes' => array(
        'class' => array(
          'coverimage',
        ),
      ),
      '#return_value' => $item->cover == 1 ? $item->pid : false,
      '#default_value' => $item->cover == 1 ? $item->pid : false,
    );
  }
  $form['gallcount'] = array(
    '#type' => 'hidden',
    '#value' => $node->ga_count,
  );

  //  dsm($node);
  $form['pager_bottom'] = array(
    '#markup' => theme('pager', array(
      'tags' => NULL,
    )),
    '#weight' => 50,
  );
  return $form;
}

/**
 * Process the GA items data form submission.
 * 
 * @todo: Investigate issues and solve functionality.
 */
function gallery_assist_items_form_submit($form, &$form_state) {
  $node = $form_state['build_info']['args'][0];
  $delete = array();
  if (isset($form_state['values']['gallery_items'])) {
    foreach ($form_state['values']['gallery_items'] as $item) {

      // Set gallery cover.
      if (isset($form_state['input']['coverimage'][$item['pid']]) && $form_state['input']['coverimage'][$item['pid']] == 1) {
        gallery_assist_set_cover(array(), $node, $item['pid']);
      }

      // Collect marked to delete items.
      if (isset($item['delete']) && $item['delete'] == 1) {
        if ($item['cover'] == $item['pid']) {
          drupal_set_message(t('The cover image has ben deleted.'), 'warning');
        }
        $delete[$item['pid']]['pid'] = $item['pid'];
        $delete[$item['pid']]['fid'] = $item['fid'];
        $delete[$item['pid']]['count'] = $form_state['values']['gallcount'];
        $delete[$item['pid']]['gref'] = $form_state['values']['node_gref'];
      }
      $x = db_update('gallery_assist_translated')
        ->fields(array(
        'ptitle' => $item['ptitle'],
        'palt' => $item['palt'],
        'pdescription' => $item['pdescription'],
      ))
        ->condition('pid', $item['pid'])
        ->execute();
    }
  }

  // Delete items
  if (count($delete) > 0) {
    gallery_assist_delete_items($delete);
  }
}
function gallery_assist_delete_items($items) {
  $count = count($items);

  // - Drupal
  db_query('DELETE FROM {file_managed} WHERE fid IN(:fids)', array(
    ':fids' => array_keys($items),
  ));
  db_query('DELETE FROM {file_usage} WHERE fid IN(:fids)', array(
    ':fids' => array_keys($items),
  ));

  // - GA
  db_query('DELETE FROM {gallery_assist_item} WHERE pid IN(:pids)', array(
    ':pids' => array_keys($items),
  ));
  db_query('DELETE FROM {gallery_assist_translated} WHERE pid IN(:pids)', array(
    ':pids' => array_keys($items),
  ));

  // Update GA counter
  $ga = array_shift($items);
  db_update('gallery_assist')
    ->fields(array(
    'count' => $ga['count'] * 1 - $count * 1,
  ))
    ->condition('gref', $ga['gref'])
    ->execute();
  drupal_set_message(t('@count items from this gallery were removed successfully', array(
    '@count' => $count,
  )));
}

/**
 * Form builder; GA items data form.
 * 
 * @param $node
 *   Object containing the node object.
 * 
 * @ingroup forms
 * @see gallery_assist_items_form_submit()
 */
function gallery_assist_items_upload($node, $allowed = array()) {
  if (isset($node->ga_items_unassigned) && count($node->ga_items_unassigned) > 0) {
    $files = '';
    if (count($node->ga_items_unassigned) > 0) {
      $rows = array();
      $fheader = array(
        t('#'),
        t('Archives'),
        #t('URL')
        '',
        '',
      );
      foreach ($node->ga_items_unassigned as $pid => $file) {
        $file = (object) $file;
        $row = array();
        $row[] = $file->fid;
        $row[] = array(
          'data' => $file->filename,
          'style' => 'width:100%;',
        );

        #$row[] = array('data' => $file->uri, 'style' => 'width:100%;');
        $row[] = l(t('add'), '#', array(
          'attributes' => array(
            'title' => 'Unpack and add images to the gallery',
          ),
        ));
        $row[] = l(t('discard'), '#', array(
          'attributes' => array(
            'title' => 'Remove this package permanently',
          ),
        ));
        $rows[] = $row;
      }
      $files = theme('table', array(
        'header' => $fheader,
        'rows' => $rows,
      ));
    }
  }
  $form = array();
  $form['upload'] = array(
    '#type' => 'fieldset',
    '#title' => 'Upload',
    '#collapsible' => TRUE,
    '#weight' => 1,
  );
  for ($i = 0; $i < 6; $i++) {
    $form['upload']["ga_file_{$i}"] = array(
      '#type' => 'file',
      '#title' => 'File',
    );
  }
  $form['upload']['allow_all_extensions'] = array(
    '#type' => 'hidden',
    '#value' => TRUE,
  );
  $form['upload']['extensions'] = array(
    '#type' => 'hidden',
    '#value' => 'jpg jpeg gif png zip ZIP',
  );
  if (!empty($files)) {
    $form['files_archives'] = array(
      '#type' => 'fieldset',
      '#title' => t('Files and archives'),
      '#collapsible' => TRUE,
      '#weight' => 3,
    );
    $form['files_archives']['table'] = array(
      '#markup' => !empty($packages) ? $packages . $files : $files,
    );
  }
  return $form;
}
function gallery_assist_build_dir_name($node, $other = FALSE) {
  return $node->ga_conf[$node->type]['ganame'];
}

/**
 * Prepare the GA items upload data submission.
 * 
 * @todo: Implementation: Recognize archives, unpack and assign items to a gallery.
 */
function gallery_assist_items_upload_submit($form, &$form_state) {

  // Process the upload and perform validation. Note: we're using the
  // form value for the $replace parameter.
  $node = $form_state['build_info']['args'][0];
  $ganame = gallery_assist_build_dir_name($node);
  $ga_dir = variable_get('gallery_assist_directory', FALSE);
  $ga['user_dir'] = $ga_dir . '/' . $node->uid;
  $ga['user_ga_dir'] = $ga['user_dir'] . '/' . $ganame;
  $ga['user_pack_dir'] = $ga['user_dir'] . '/' . 'packages';
  $ga['user_tmp_dir'] = $ga['user_dir'] . '/' . 'tmp';
  foreach ($ga as $key => $uri) {
    file_prepare_directory($uri, FILE_CREATE_DIRECTORY);
  }
  $destination = array(
    'files' => $ga['user_ga_dir'],
    'packages' => $ga['user_pack_dir'],
  );
  $validators['file_validate_extensions'] = array(
    $node->ga_conf[$node->type]['upload_extensions'],
  );
  $files = array();
  foreach ($_FILES['files']['name'] as $key => $value) {
    if ($_FILES['files']['type'][$key] == 'application/zip' || $_FILES['files']['type'][$key] == 'application/x-zip' || $_FILES['files']['type'][$key] == 'application/x-gzip') {
      $type = 'packages';
      $validators['file_validate_extensions'] = array(
        'zip ZIP',
      );
    }
    else {
      $type = 'files';
      $validators['file_validate_is_image'] = array();
      $validators['file_validate_extensions'] = array(
        $node->ga_conf[$node->type]['upload_extensions'] . ' ' . strtoupper($node->ga_conf[$node->type]['upload_extensions']),
      );
    }

    // Save
    $file = file_save_upload($key, $validators, $destination[$type]);
    if ($file) {
      if ($type == 'packages') {
        $_SESSION['gallery_assist']['packages'][$file->fid] = $file;
      }
      elseif ($type == 'files') {
        $_SESSION['gallery_assist']['files'][$file->fid] = $file;
      }
      $form_state['values'][$key] = $file;
      drupal_set_message(t('File @filepath was uploaded.', array(
        '@filepath' => $file->uri,
      )));
      drupal_set_message(t('File name is @filename.', array(
        '@filename' => $file->filename,
      )));
      drupal_set_message(t('File MIME type is @mimetype.', array(
        '@mimetype' => $file->filemime,
      )));
      $files[] = $file;
    }
    elseif ($file === FALSE) {
      drupal_set_message(t('File upload FAIL!'), 'error');
    }
  }
  if (count($files)) {
    gallery_assist_save_uploaded_files($node, $files);
  }
}

/**
 * Process the GA items upload.
 */
function gallery_assist_save_uploaded_files($node, $files) {
  if (count($files)) {
    $w = db_select('gallery_assist_item', 'gi')
      ->fields('gi', array(
      'weight',
    ))
      ->condition('gi.gref', $node->ga_gref)
      ->orderBy('gi.weight', 'DESC')
      ->range(0, 1)
      ->execute()
      ->fetchField();
    foreach ($files as $upfile) {
      $w = $w + 1;

      // Set the first uploaded image as cover.
      $hasCover = db_select('gallery_assist_item', 'gi')
        ->fields('gi', array(
        'pid',
      ))
        ->condition('gi.gref', $node->ga_gref)
        ->condition('gi.cover', 1)
        ->execute()
        ->fetchField();

      // Set cover
      $cover = empty($hasCover) ? 1 : 0;

      // Save items data.
      if (isset($upfile->fid) && $upfile->fid > 0) {

        // Get extension
        $ext = substr($upfile->filename, strrpos($upfile->filename, '.') + 1);
        $ext = strtolower($ext);
        if (in_array($ext, explode(' ', $node->ga_conf[$node->type]['upload_extensions']))) {

          // Set permanent status.
          $upfile->status = FILE_STATUS_PERMANENT;
          $upfile = file_save($upfile);

          // In case the uploaded file is an archive, save it unassigned.
          // This means that the archives will be listed on each
          // assignment node editing.
          if ($ext == 'zip') {
            $pid = db_insert('gallery_assist_item')
              ->fields(array(
              'uid' => $node->uid,
              'fid' => $upfile->fid,
              'filename' => $upfile->filename,
              'opath' => $upfile->uri,
              'weight' => $w,
              'created' => time(),
              'type' => 'archive',
              'type_value' => $ext,
            ))
              ->execute();
          }
          else {
            $pid = db_insert('gallery_assist_item')
              ->fields(array(
              'gid' => $node->ga_gid,
              'gref' => $node->ga_gref,
              'nid' => $node->nid,
              'ref' => $node->ga_ref,
              'uid' => $node->uid,
              'fid' => $upfile->fid,
              'filename' => $upfile->filename,
              'opath' => $upfile->uri,
              'weight' => $w,
              'copyright' => isset($node->copyright) ? $node->copyright : '',
              'cover' => $cover,
              'created' => time(),
            ))
              ->execute();

            // Prepare data for the translations table.
            $my_ptitle = preg_replace('/\\.[a-z0-9]+$/i', '', $upfile->filename);
            $my_ptitle = preg_replace('/\\_/', ' ', $my_ptitle);

            // Save data in the translations table.
            $did = db_insert('gallery_assist_translated')
              ->fields(array(
              'nid' => $node->nid,
              'gid' => $node->ga_gid,
              'gref' => $node->ga_gref,
              'pid' => $pid,
              'lang' => $node->language,
              'ptitle' => $my_ptitle,
              'palt' => $upfile->filename,
              'pdescription' => '',
            ))
              ->execute();

            // Update gallery counter. Necessary for other modules.
            $count = db_select('gallery_assist_item', 'i')
              ->fields('i')
              ->condition('i.gref', $node->ga_gref)
              ->execute()
              ->rowCount();
            db_update('gallery_assist')
              ->fields(array(
              'count' => $count,
            ))
              ->condition('gref', $node->ga_gref)
              ->execute();
          }
        }
      }
    }
  }
}

Functions

Namesort descending Description
gallery_assist_build_dir_name
gallery_assist_delete_items
gallery_assist_items_form Form builder; GA items data form.
gallery_assist_items_form_submit Process the GA items data form submission.
gallery_assist_items_upload Form builder; GA items data form.
gallery_assist_items_upload_submit Prepare the GA items upload data submission.
gallery_assist_node_settings_form Form builder; GA settings form for a node gallerie.
gallery_assist_node_settings_form_submit Process the GA settings form submission for a node gallerie.
gallery_assist_save_uploaded_files Process the GA items upload.