View source
<?php
define('TFT_ARCHIVE_PERM__ARCHIVE_TERMS', 'tft archive child terms');
function tft_archive_menu() {
$menu = array(
'tft/term/archive/%' => array(
'title' => "Archive a folder",
'access callback' => 'tft_term_access',
'access arguments' => array(
3,
NULL,
'archive',
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'tft_archive_term_form',
3,
),
'file' => 'includes/tft_archive.pages.inc',
'type' => MENU_CALLBACK,
),
'tft/file/archive/%' => array(
'title' => "Archive a file",
'access callback' => 'tft_archive_access',
'access arguments' => array(
3,
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'tft_archive_file_form',
3,
),
'file' => 'includes/tft_archive.pages.inc',
'type' => MENU_CALLBACK,
),
'tft/term/restore/%' => array(
'title' => "Restore a file",
'access callback' => 'tft_term_access',
'access arguments' => array(
3,
NULL,
'archive',
),
'page callback' => 'tft_archive_restore_element',
'page arguments' => array(
3,
'term',
),
'file' => 'includes/tft_archive.pages.inc',
'type' => MENU_CALLBACK,
),
'tft/file/restore/%' => array(
'title' => "Restore a file",
'access callback' => 'tft_archive_access',
'access arguments' => array(
3,
),
'page callback' => 'tft_archive_restore_element',
'page arguments' => array(
3,
'node',
),
'file' => 'includes/tft_archive.pages.inc',
'type' => MENU_CALLBACK,
),
);
return $menu;
}
function tft_archive_permission() {
return array(
TFT_ARCHIVE_PERM__ARCHIVE_TERMS => array(
'title' => t("Archive terms and files"),
),
);
}
function tft_archive_tft_term_access($tid, $account = NULL, $op = 'view') {
if (!isset($account)) {
global $user;
$account = $user;
}
if ($op == 'archive') {
return user_access(TFT_ARCHIVE_PERM__ARCHIVE_TERMS, $account);
}
}
function tft_archive_tft_item_operation_links_alter(&$links, $type, $id, $parent_tid) {
$query = isset($_SESSION['tft']['q']) ? $_SESSION['tft']['q'] : '';
if ($type == 'folder') {
if (tft_term_access($id, NULL, 'archive') && !tft_archive_is_archive_folder($id)) {
if (tft_archive_element_is_archived($id, 'term')) {
$links['restore'] = array(
'title' => t("restore"),
'href' => "tft/term/restore/{$id}",
'attributes' => array(
'class' => 'ops-link term-restore-link',
),
'query' => array(
'destination' => $query . (isset($parent_tid) ? "#tft/{$parent_tid}" : ''),
),
);
}
else {
$links['archive'] = array(
'title' => t("archive"),
'href' => "tft/term/archive/{$id}",
'attributes' => array(
'class' => 'ops-link term-archive-link',
),
'query' => array(
'destination' => $query . (isset($parent_tid) ? "#tft/{$parent_tid}" : ''),
),
);
}
}
}
else {
if (isset($links['edit']) && tft_term_access($parent_tid, NULL, 'archive')) {
if (tft_archive_element_is_archived($id, 'node')) {
$links['restore'] = array(
'title' => t("restore"),
'href' => "tft/file/restore/{$id}",
'attributes' => array(
'class' => 'ops-link term-restore-link',
),
'query' => array(
'destination' => $query . (isset($parent_tid) ? "#tft/{$parent_tid}" : ''),
),
);
}
else {
$links['archive'] = array(
'title' => t("archive"),
'href' => "tft/file/archive/{$id}",
'attributes' => array(
'class' => 'ops-link term-archive-link',
),
'query' => array(
'destination' => $query . (isset($parent_tid) ? "#tft/{$parent_tid}" : ''),
),
);
}
}
}
}
function tft_archive_access($file_nid, $account = NULL) {
$tid = db_query("SELECT DISTINCT(tn.tid) FROM {node_revision} v\n LEFT JOIN {node} n ON n.vid = v.vid\n LEFT JOIN {taxonomy_index} tn ON tn.nid = n.nid\n WHERE n.nid = :nid AND n.status = 1", array(
':nid' => $file_nid,
))
->fetchField();
return tft_term_access($tid, $account, 'archive');
}
function tft_archive_create_archive_folder($parent_tid = 0) {
$tid = tft_add_term(t("Archives"), $parent_tid);
db_insert('tft_archive_tid')
->fields(array(
'tid' => $tid,
'is_archive' => 1,
))
->execute();
return $tid;
}
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 {
$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;
}
}
$tid = $parent_tid;
}
}
return 0;
}
function tft_archive_log($id, $type, $previous_parent_tid) {
db_insert('tft_archive_restore')
->fields(array(
'id' => $id,
'type' => $type,
'previous_parent_tid' => $previous_parent_tid,
))
->execute();
}
function tft_archive_element_is_archived($id, $type) {
$result = db_select('tft_archive_restore', 't')
->fields('t', array(
'id',
))
->condition('id', $id)
->condition('type', $type)
->execute()
->fetchField();
return !empty($result);
}
function tft_archive_is_archive_folder($tid) {
$result = db_select('tft_archive_tid', 't')
->fields('t', array(
'is_archive',
))
->condition('tid', $tid)
->execute()
->fetchField();
return !empty($result);
}
function tft_archive_restore_archived_element($id, $type) {
$log = db_query("SELECT * FROM {tft_archive_restore} WHERE type = :type AND id = :id", array(
':type' => $type,
':id' => $id,
))
->fetchAssoc();
if (empty($log)) {
$log = array(
'id' => $id,
'previous_parent_tid' => 0,
);
}
drupal_alter('tft_archive_restore_element', $log, $id, $type);
if ($type == 'node') {
$node = node_load($log['id']);
$node->{"taxonomy_vocabulary_" . variable_get('tft_vocabulary_vid', 0)}[LANGUAGE_NONE][0]['tid'] = $log['previous_parent_tid'];
node_save($node);
}
else {
db_update('taxonomy_term_hierarchy')
->fields(array(
'parent' => $log['previous_parent_tid'],
))
->condition('tid', $log['id'])
->execute();
}
db_delete('tft_archive_restore')
->condition('id', $id)
->condition('type', $type)
->execute();
return $log['previous_parent_tid'];
}