You are here

function _image_import_create_subgalleries in Image 6

Same name and namespace in other branches
  1. 7 contrib/image_import/image_import.pages.inc \_image_import_create_subgalleries()

Creates subgalleries.

Parameters

$origname: The original name (with its path) of the image which subcategory has to be created.

$image_galleries_vid: The vocabulary id of corresponding to image galleries.

$subgalleries_parent_tid: The term id of the parent gallery, which will contain the subgallery.

$subgalleries: An array of the tids of each subgallery already created, indexed by subgallery path.

$context: The execution log.

Return value

The tid of the subgallery in which the image goes to.

1 call to _image_import_create_subgalleries()
_image_import_batch_op in contrib/image_import/image_import.pages.inc
Batch operation callback for image import.

File

contrib/image_import/image_import.pages.inc, line 313

Code

function _image_import_create_subgalleries($origname, $image_galleries_vid, $subgalleries_parent_tid, &$subgalleries, &$context) {

  // Get a term id from its parent (if any), its vocabulary, and its name.
  $tid_query = 'SELECT td.tid FROM {term_data} td, {term_hierarchy} th WHERE td.tid = th.tid AND vid = %d AND name = \'%s\' AND parent = %d';

  // Explode path to image.
  $requested_subgalleries = explode('/', $origname);

  // Remove the filename at the end.
  array_pop($requested_subgalleries);

  // Parent gallery.
  $parent_tid = (int) $subgalleries_parent_tid;

  // Current image's gallery.
  $subgallery_tid = (int) $subgalleries_parent_tid;

  // Create each gallery in the path, if necessary.
  foreach ($requested_subgalleries as $key => $subgallery_name) {

    // The current subgallery's relative path.
    $subgallery_path = implode('/', array_slice($requested_subgalleries, 0, $key + 1));

    // Was this subgallery already created by the current batch?
    $subgallery_tid = $subgalleries[$subgallery_path];
    if (!$subgallery_tid) {

      // First, look in DB.
      $subgallery_tid = db_result(db_query($tid_query, $image_galleries_vid, $subgallery_name, $parent_tid));

      // Didn't find? Create it.
      if (!$subgallery_tid) {
        $subgallery_term = array(
          'name' => $subgallery_name,
          'parent' => $parent_tid,
          'vid' => $image_galleries_vid,
        );
        $status = taxonomy_save_term($subgallery_term);
        if ($status) {

          // Get its tid back.
          $subgallery_tid = db_result(db_query($tid_query, $image_galleries_vid, $subgallery_name, $parent_tid));
          $term = taxonomy_get_term($subgallery_tid);
          $context['results']['good'][] = t('Created gallery !link [!edit-link].', array(
            '!link' => l($subgallery_name, taxonomy_term_path($term)),
            '!edit-link' => l(t('edit'), 'admin/content/taxonomy/edit/term/' . $subgallery_tid),
          ));
        }
        else {
          watchdog('image_import', 'There was an error that prevented gallery %gallery_path from being created.', array(
            '%gallery_path' => $subgallery_path,
          ), WATCHDOG_ERROR);
          $context['results']['bad'][] = t('Error creating gallery %gallery_path.', array(
            '%gallery_path' => $subgallery_path,
          ));

          // Return an error.
          return 0;
        }
      }
      $subgalleries[$subgallery_path] = $subgallery_tid;
    }

    // For the next subgallery to be created, last created will be its parent.
    $parent_tid = (int) $subgallery_tid;
  }
  return $subgallery_tid;
}