You are here

function node_gallery_api_sort_items_form_set_chronological_sorting in Node Gallery 7

Submit function to set image weights reflecting chronological order of their making

1 string reference to 'node_gallery_api_sort_items_form_set_chronological_sorting'
node_gallery_api_sort_items_form in ./node_gallery_api.pages.inc
Form function for the "Sort Items" tab.

File

./node_gallery_api.pages.inc, line 127
Node Gallery module.

Code

function node_gallery_api_sort_items_form_set_chronological_sorting($form, &$form_state) {
  $images = array();
  $ngid = $form_state['values']['ngid'];
  $gallery = node_load($ngid);
  $items = node_gallery_api_get_items($gallery, FALSE, FALSE);
  $no_exif_count = 0;
  $exif = NULL;

  // Get exif creation DateTime
  foreach ($items as $item) {
    $item->original_DateTime = node_gallery_api_original_datetime_from_exif($item);
    if (!isset($item->original_DateTime)) {
      if (user_access('access site reports')) {
        drupal_set_message(t("Unable to find creation date in exif data for: @title", array(
          '@title' => $item->title,
        )));
      }
      $no_exif_count++;
      if (!empty($item->node_gallery['item_file']['timestamp'])) {
        $item->original_DateTime = $item->node_gallery['item_file']['timestamp'];
      }
      else {
        $item->original_DateTime = $item->created;
      }
    }
  }
  if ($no_exif_count) {
    drupal_set_message(t("Unable to find creation date in exif data for: @count media(s)", array(
      '@count' => $no_exif_count,
    )));
    if ($no_exif_count == count($items)) {
      drupal_set_message(t("The order of the images was left unchanged."));
      return;
    }
  }
  usort($items, 'node_gallery_api_original_datetime_chronological_sort');
  $i = 0;
  foreach ($items as $item) {
    $images[] = array(
      'nid' => $item->nid,
      'ngid' => $ngid,
      'weight' => $i,
    );
    $i++;
  }
  $batch = array(
    'title' => t('Updating image order'),
    'operations' => array(
      array(
        'node_gallery_api_batch_sorting_callback',
        array(
          $images,
        ),
      ),
    ),
    'finished' => 'node_gallery_api_batch_sorting_finished',
    'file' => drupal_get_path('module', 'node_gallery_api') . '/node_gallery_api.inc',
    'init_message' => t('New image positions are being calculated.'),
    'progress_message' => t('Processing image sorting.'),
    'error_message' => t('Image sorting has encountered an error.'),
  );
  batch_set($batch);
}