You are here

function gallery_assist_save in Gallery Assist 6

Implementation of hook_save().

1 call to gallery_assist_save()
gallery_assist_form_submit in ./gallery_assist.module
Prepare some input keys for save.

File

./gallery_assist.module, line 2943
Drupal content type with gallery functionality.

Code

function gallery_assist_save(&$node, $u = '') {
  if ($node->gallery_assist_lockked) {
    return;
  }
  $cq = "DELETE FROM {cache_gallery_assist_data} WHERE cid = '%s'";
  db_query($cq, $node->nid);
  if (empty($u)) {
    global $user;
  }
  else {
    $user = user_load($u);
  }

  // Update or delete items data.
  if (count($node->gallery_items) > 0) {
    foreach ($node->gallery_items as $pid => $item) {
      $item = (object) $item;
      $pa_op = 'update';

      // Remove Item.
      if (!empty($item->remove)) {
        $pa_op = 'delete';

        // Get paths and fid from gallery_assist_item.
        $res = db_fetch_object(db_query("SELECT fid, uid, ref, filename, tpath, ppath, opath FROM {gallery_assist_item} WHERE pid=%d", $pid));

        // Delete fisicaly the files.
        $res->thmb_path = variable_get('gallery_assist_directory', file_directory_path() . '/gallery_assist') . '/' . $res->uid . '/gallery_assist' . $res->ref . '/img/' . $res->filename;
        gallery_assist_delete_items($res);

        // Delete tables etries, order: files, gallery_assist_translated and gallery_assist_item.
        db_query('DELETE FROM {files} WHERE fid = %d', $res->fid);
        db_query('DELETE FROM {gallery_assist_translated} WHERE pid = %d', $pid);
        db_query('DELETE FROM {gallery_assist_item} WHERE pid = %d', $pid);
        drupal_set_message(t('@title has been successful deleted.', array(
          '@title' => $item->ptitle,
        )), 'status');

        // Send information to other modules when a image will be deleted.
        $node_light = new \stdClass();
        $node_light->pid = $pid;
        $node_light->nid = $node->nid;
        $node_light->nid = $node->ref;
        foreach (module_implements('gallery_assist_api') as $name) {
          $function = $name . '_gallery_assist_api';
          $result = $function('item_delete', $pid, $node_light);
        }
      }
      if ($node->temp_copyright) {
        $item->copyright = $node->temp_copyright;
      }

      // Update items data.
      $update_item_data = array(
        'pid' => $pid,
        'copyright' => $item->copyright,
        'weight' => $item->weight,
      );
      drupal_write_record('gallery_assist_item', $update_item_data, 'pid');
      $update_translated_data = array(
        'pid' => $pid,
        'lang' => $node->language,
        'ptitle' => $item->ptitle,
        'pdescription' => empty($item->pdescription) ? '' : $item->pdescription,
      );
      drupal_write_record('gallery_assist_translated', $update_translated_data, array(
        'pid',
        'lang',
      ));
      if (function_exists('pathauto_menu')) {
        $item->pid = $pid;
        $item->old_ptitle = $node->gallitems[$pid]->ptitle;
        $item->nid = $node->nid;
        $item->language = $node->language;
        gallery_assist_rebuild_alias($item, $x = NULL);
      }
    }
  }
  if (count($node->newfile['name']) > 0) {
    $w = db_result(db_query("SELECT weight FROM {gallery_assist_item} WHERE gref = %d ORDER BY weight DESC LIMIT 1", $node->gref));
    for ($i = 0; $i < count($node->newfile['name']); ++$i) {

      // Increment the weight value.
      $w = $w + 1;

      // Set the first uploaded image as cover.
      $node->has_cover = db_result(db_query("SELECT pid FROM {gallery_assist_item} WHERE gref = %d AND cover = %d", $node->gref, 1));
      if (empty($node->has_cover)) {
        $cover = 1;
      }
      else {
        $cover = 0;
      }
      if (!empty($node->newfile['name']['pictures' . $i])) {
        $upload_dir = variable_get('gallery_assist_directory', file_directory_path() . '/gallery_assist') . '/' . $node->uid . '/' . $node->ganame;

        // Found supported packages will be sended to upport.
        $imp_ext = substr(strrchr($node->newfile['name']['pictures' . $i], '.'), 1);
        $imp_ext = strtolower($imp_ext);

        // Upport.
        if (module_exists('gallery_assist_upport') && user_access('allow upport zip upload')) {
          ++$zip_counter;

          //if (user_access('use zip upload on '. $node->type) && ($imp_ext == 'zip' || $imp_ext == 'rar' || $imp_ext == 'tar' || $imp_ext == 'tar.gz')) {
          if ($user->uid == 1 || $imp_ext == 'zip') {
            if ($imp_ext == 'tar' || $imp_ext == 'tar.gz' || $imp_ext == 'rar') {
              drupal_set_message('Currently are the formate tar, tar.gz and rar not supported.', 'warning');
              next;
            }
            ++$zip_counter;
            $arch = $node->newfile['name']['pictures' . $i];
            $import_dir = variable_get('gallery_assist_directory', file_directory_path() . '/gallery_assist') . '/' . $node->uid . '/import';
            $import_tmp = variable_get('gallery_assist_directory', file_directory_path() . '/gallery_assist') . '/' . $node->uid . '/import/tmp';
            file_check_directory($import_dir, 1);
            @chmod($import_dir, 0777);
            file_check_directory($import_tmp, 1);
            @chmod($import_tmp, 0777);
            $arch_validators = array(
              'file_validate_extensions' => array(
                'zip',
              ),
              'file_validate_size' => array(
                $node->gallconf[$node->type]['file_size'],
                $node->gallconf[$node->type]['user_size'],
              ),
            );
            $arch_file = file_save_upload('pictures' . $i, $arch_validators, $import_dir, $replace = FILE_EXISTS_REPLACE);
            if (!empty($arch_file->filepath)) {
              if (module_exists('gallery_assist_upport')) {
                $node->gall_imports['arch']['zip'][] = $arch_file->filepath;
                $import_message = gallery_assist_upport_archiv_save($node);
                drupal_set_message($import_message);
                db_query("DELETE FROM {files} WHERE fid = %d", $arch_file->fid);
              }
            }
            else {
              drupal_set_message(t('Archive @zip_name but the module <a href="@upport_url">Gallery Assist Upport</a> is not installed. The archiv would uploaded to the specific folder and is ready to import to a gallery by upport installing.', array(
                '@zip_name' => $node->newfile['name']['pictures' . $i],
                '@upport_url' => url('drupal.org/project/gallery_assist_upport'),
              )));
            }
            unset($node->newfile['name']['pictures' . $i]);
            next;
          }
        }

        // End of upport preprocess.
        if ((user_access('create gallery_assist content') || user_access('create gallery_assist_' . $node->type . ' content')) && !is_dir($upload_dir || !is_dir($upload_dir . '/img') || !is_dir($upload_dir . '/thm') || !is_dir($upload_dir . '/prev'))) {
          gallery_assist_makedirs($node);
        }

        // separe file name and file extension.
        $f_ext_string = substr(strrchr($node->newfile['name']['pictures' . $i], '.'), 0);
        $f_name_string = preg_replace('/\\.[a-z0-9]+$/i', '', $node->newfile['name']['pictures' . $i]);
        $my_ptitle = $f_name_string;
        if (file_create_path($upload_dir, 1)) {
          $validators = array(
            'file_validate_is_image' => array(),
          );
          if (!empty($node->gallconf[$node->type]['extensions'])) {
            $validators['file_validate_extensions'] = array(
              $node->gallconf[$node->type]['extensions'],
            );
          }
          if (!empty($node->gallconf[$node->type]['resolution'])) {
            $validators['file_validate_image_resolution'] = array(
              $node->gallconf[$node->type]['resolution'],
            );
          }
          if (!empty($node->gallconf[$node->type]['file_size']) || $node->gallconf[$node->type]['file_size'] > 0) {
            $validators['file_validate_size'] = array(
              $node->gallconf[$node->type]['file_size'],
              $node->gallconf[$node->type]['user_size'],
            );
          }
          if (is_dir($upload_dir) && $node->newfile['name']['pictures' . $i]) {
            $replace = variable_get('gallery_assist_upload_method', 1) == 1 ? TRUE : FALSE;

            // In case the file exists remove al derivatives.
            // So can the module write the new files.
            $replaced = FALSE;
            if ($replace == 1 && file_exists($upload_dir . '/' . $node->newfile['name']['pictures' . $i])) {

              // drupal_set_message(t('Files are be replaced. In case you see the old files, you see only from the cache of your browser. <br />By reload you can see the just uploaded.'));
              drupal_set_message(t('Replace file: @file.', array(
                '@file' => $node->newfile['name']['pictures' . $i],
              )));
              if (file_exists($upload_dir . '/' . $node->newfile['name']['pictures' . $i])) {
                file_delete($upload_dir . '/' . $node->newfile['name']['pictures' . $i]);
                $p_i_d = db_fetch_object(db_query("SELECT fid, pid FROM {gallery_assist_item} WHERE opath = '%s'", $upload_dir . '/' . $node->newfile['name']['pictures' . $i]));
                if (!empty($p_i_d)) {
                  db_query('DELETE FROM {files} WHERE fid = %d', $p_i_d->fid);
                  db_query('DELETE FROM {gallery_assist_translated} WHERE pid = %d', $p_i_d->pid);
                  db_query('DELETE FROM {gallery_assist_item} WHERE pid = %d', $p_i_d->pid);
                }
              }
              if (file_exists($upload_dir . '/img/' . $node->newfile['name']['pictures' . $i])) {
                file_delete($upload_dir . '/img/' . $node->newfile['name']['pictures' . $i]);
              }
              if (file_exists($upload_dir . '/thm/' . $node->newfile['name']['pictures' . $i])) {
                file_delete($upload_dir . '/thm/' . $node->newfile['name']['pictures' . $i]);
              }
              if (file_exists($upload_dir . '/prev/' . $node->newfile['name']['pictures' . $i])) {
                file_delete($upload_dir . '/prev/' . $node->newfile['name']['pictures' . $i]);
              }
              $replaced = TRUE;
            }
            $upl_file = file_save_upload('pictures' . $i, $validators, $upload_dir, $replace);
            $upl_files[] = $upl_file;
            if (isset($upl_file->filepath)) {
              $a = explode('/', $upl_file->filepath);
              $new_filename = end($a);
              $gallery_assist_thumbnail = $upload_dir . '/img/' . $new_filename;
              $upload_thm = $upload_dir . '/thm/' . $new_filename;
              $upload_prev = $upload_dir . '/prev/' . $new_filename;
              if (is_file($upl_file->filepath)) {
                @chmod($upl_file->filepath, 0664);
              }
              $imageinfo = image_get_info($upl_file->filepath);
              $my_dim_ref = $imageinfo['width'] > $imageinfo['height'] ? $imageinfo['width'] : $imageinfo['height'];
              $img_direction = $imageinfo['width'] > $imageinfo['height'] ? 'h' : 'v';
              if (80 < $my_dim_ref) {
                @image_scale($upl_file->filepath, $gallery_assist_thumbnail, 80, 80);
              }
              else {
                @copy($upl_file->filepath, $gallery_assist_thumbnail);
              }
              if (is_file($gallery_assist_thumbnail)) {
                @chmod($gallery_assist_thumbnail, 0664);
              }

              //              if ($conf['use_imagecache'] == 0) {
              //                $thmb_size = $conf['upload_thm'];
              //                $prev_size = $conf['upload_prev'] ? $conf['upload_prev'] : 550;
              //
              //                if ($thmb_size < $my_dim_ref) {
              //                  image_scale($upl_file->filepath, $upload_thm, $thmb_size, $thmb_size);
              //                }
              //                else {
              //                  copy($upl_file->filepath, $upload_thm);
              //                }
              //                @chmod($upload_thm, 0664);
              //
              //                if ($prev_size < $my_dim_ref) {
              //                  image_scale($upl_file->filepath, $upload_prev, $prev_size, $prev_size);
              //                }
              //                else {
              //                  copy($upl_file->filepath, $upload_prev);
              //                }
              //                @chmod($upload_prev, 0664);
              //
              //                // Clear the cached filesize and refresh the image information.
              //                clearstatcache();
              //              }
              // Set permanent status to the files table entries.
              file_set_status($upl_file, 1);
              $result = db_fetch_object(db_query("SELECT gid, gref FROM {gallery_assist} WHERE nid = %d", $node->nid));
              $my_gref = $result->gref ? $result->gref : $result->gid;
              $insert_item_data = array(
                'nid' => $node->nid,
                'ref' => $node->ref,
                'gid' => $result->gid,
                'gref' => $my_gref,
                'sid' => $node->sid ? $node->sid : $node->gallconf[$node->type]['show_download_link'],
                'uid' => $node->uid,
                'fid' => $upl_file->fid,
                'filename' => $new_filename,
                'tpath' => $upload_thm,
                'ppath' => $upload_prev,
                'opath' => $upl_file->filepath,
                'copyright' => $node->temp_copyright,
                'weight' => $w,
                'cover' => $cover,
              );
              drupal_write_record('gallery_assist_item', $insert_item_data);

              // Get the pid from recent updated file.
              $my_last_incerted = db_last_insert_id('gallery_assist_item', 'pid');
              if ($cover == 1) {
                $node->has_cover = $my_last_incerted;
              }

              // See how many language entries, and make the picture-entry for each language.
              $just_translated = db_query("SELECT nid, lang FROM {gallery_assist} WHERE ref=%d", $node->ref);
              while ($r = db_fetch_object($just_translated)) {
                $my_ptitle = preg_replace('/\\_/', ' ', $my_ptitle);
                $inset_translated_data = array(
                  'nid' => $r->nid,
                  'gid' => $result->gid,
                  'gref' => $my_gref,
                  'pid' => $my_last_incerted,
                  'lang' => $r->lang,
                  'ptitle' => $my_ptitle,
                  'palt' => $my_ptitle,
                  'pdescription' => '',
                );
                drupal_write_record('gallery_assist_translated', $inset_translated_data);
                if (function_exists('pathauto_menu')) {
                  $new_item = new \stdClass();
                  $new_item->pid = $my_last_incerted;
                  $new_item->aid = 0;
                  $new_item->ptitle = $my_ptitle;
                  $new_item->old_ptitle = '';
                  $new_item->nid = $r->nid;
                  gallery_assist_rebuild_alias($new_item, $x);
                }
              }
              if (module_exists('image_upload_gallery_assist') && $node->ga_multiupload == TRUE) {
                drupal_set_message('The image ' . $my_ptitle . ' was uploaded sucessfull.');
                return $upl_file;
              }
              else {
                if ($replaced == FALSE) {
                  drupal_set_message(t('Add file: @file.', array(
                    '@file' => $my_ptitle,
                  )), 'status');
                }
              }
            }
          }
        }
      }
    }
  }
}