You are here

function gallery_assist_save_uploaded_files in Gallery Assist 7

Same name in this branch
  1. 7 gallery_assist_form_BUP.inc \gallery_assist_save_uploaded_files()
  2. 7 gallery_assist_form.inc \gallery_assist_save_uploaded_files()

Process the GA items upload.

2 calls to gallery_assist_save_uploaded_files()
gallery_assist_items_upload_submit in ./gallery_assist_form_BUP.inc
Prepare the GA items upload data submission.
gallery_assist_items_upload_submit in ./gallery_assist_form.inc
Prepare the GA items upload data submission.

File

./gallery_assist_form.inc, line 518
GA node settings, upload, sorting and gallery items management forms.

Code

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();
          }
        }
      }
    }
  }
}