You are here

function tft_archive_get_closest_parent_archive_tid in Taxonomy File Tree 7.2

Helper function to get the closest archive folder.

Recursively walks up the file tree and returns the first parent that is an archive folder. If none are found, returns 0.

Parameters

int $tid:

Return value

int

2 calls to tft_archive_get_closest_parent_archive_tid()
tft_archive_file_form_submit in modules/tft_archive/includes/tft_archive.pages.inc
Submission callback for tft_archive_term_form().
tft_archive_term_form_submit in modules/tft_archive/includes/tft_archive.pages.inc
Submission callback for tft_archive_term_form().

File

modules/tft_archive/tft_archive.module, line 192
Hook implementation and logic.

Code

function tft_archive_get_closest_parent_archive_tid($tid) {
  while ($parent_tid = tft_get_parent_tid($tid)) {
    if (tft_archive_is_archive_folder($parent_tid)) {
      return $parent_tid;
    }
    else {

      // Are any of the siblings an archive folder ?
      $result = db_query("SELECT td.tid FROM {taxonomy_term_data} td\n                        LEFT JOIN {taxonomy_term_hierarchy} th ON th.tid = td.tid\n                      WHERE th.parent = :ptid AND td.vid = :vid ORDER BY td.name", array(
        ':ptid' => $parent_tid,
        ':vid' => variable_get('tft_vocabulary_vid', 0),
      ));
      foreach ($result as $term) {
        if (tft_archive_is_archive_folder($term->tid)) {
          return $term->tid;
        }
      }

      // No archive. Go up.
      $tid = $parent_tid;
    }
  }
  return 0;
}