View source
<?php
include_once 'oa_archive.features.inc';
function oa_archive_node_access_records($node) {
if (($flag = flag_load('trash')) && $flag
->is_flagged($node->nid)) {
$group = oa_core_get_group_from_node($node);
$gid = isset($group) ? $group + 1 : $node->status;
return array(
array(
'realm' => 'trash_flag',
'gid' => $gid,
'grant_view' => $node->status,
'grant_update' => 0,
'grant_delete' => 0,
'priority' => 10000,
),
);
}
}
function oa_archive_node_grants($account, $op) {
$grants = array();
if ($space_id = oa_core_get_space_context(TRUE)) {
if (og_user_access('node', $space_id, "view trash content", $account)) {
$grants['trash_flag'] = array(
$space_id + 1,
);
}
}
return $grants;
}
function oa_archive_node_access($node, $op, $account) {
if ($op == 'delete' && variable_get('oa_archive_disable_delete', FALSE) && module_exists('trash_flag')) {
return NODE_ACCESS_DENY;
}
}
function oa_archive_form_views_exposed_form_alter(&$form, &$form_state) {
foreach (_oa_archive_find_archived_exposed_filters($form_state['view']) as $filter_name) {
if (isset($form[$filter_name])) {
_oa_archive_alter_archived_exposed_filter($form[$filter_name]);
}
}
}
function oa_archive_form_views_content_views_panes_content_type_edit_form_alter(&$form, &$form_state, $form_id) {
foreach (_oa_archive_find_archived_exposed_filters($form_state['view']) as $filter_name) {
if (isset($form['exposed']['filter-' . $filter_name][$filter_name])) {
_oa_archive_alter_archived_exposed_filter($form['exposed']['filter-' . $filter_name][$filter_name]);
}
}
}
function _oa_archive_find_archived_exposed_filters($view) {
$result = array();
foreach ($view->filter as $filter_name => $filter) {
if (is_a($filter, 'flag_handler_filter_flagged') && !empty($filter->options['exposed'])) {
if ($relationship = $view->relationship[$filter->options['relationship']]) {
if ($relationship->options['flag'] == 'trash') {
$result[] = $filter_name;
}
}
}
}
return $result;
}
function _oa_archive_alter_archived_exposed_filter(&$element) {
$element['#options'] = array(
'0' => t('No'),
'All' => t('Yes'),
'1' => t('Only archived content'),
);
if (empty($element['#default_value'])) {
$element['#default_value'] = 0;
}
}
function oa_archive_views_query_alter(&$view, &$query) {
if ($flag = flag_get_flag('trash')) {
if (($space_nid = oa_core_get_space_context()) && $flag
->is_flagged($space_nid)) {
$archive_filters = _oa_archive_find_archived_exposed_filters($view);
foreach ($archive_filters as $filter_name) {
$field_name = $view->filter[$filter_name]->relationship . '.uid';
foreach ($query->where as $where_group => $where) {
$new_where = array();
foreach ($where['conditions'] as $condition) {
if ($condition['field'] != $field_name) {
$new_where[] = $condition;
}
}
$query->where[$where_group] = $new_where;
}
}
}
}
}
function oa_archive_flag_flag($flag, $content_id, $account, $flagging) {
oa_archive_trigger_flag('flag', $flag, $content_id, $account);
}
function oa_archive_flag_unflag($flag, $content_id, $account, $flagging) {
oa_archive_trigger_flag('unflag', $flag, $content_id, $account);
}
function oa_archive_trigger_flag($action, $flag, $content_id, $account) {
if ($flag->name == 'trash') {
$node = node_load($content_id);
if (empty($node) || !in_array($node->type, array(
OA_SPACE_TYPE,
OA_SECTION_TYPE,
))) {
return;
}
if ($node->type == OA_SPACE_TYPE) {
$child_nids = array_merge(_oa_archive_get_subspaces($node->nid), _oa_archive_get_sections($node->nid));
}
elseif ($node->type == OA_SECTION_TYPE) {
$child_nids = _oa_archive_get_section_content($node->nid);
}
$already_archived_variable = 'oa_archive_node_' . $node->nid . '_already_archived';
if ($action == 'flag') {
$already_archived = _oa_archive_find_archived_nodes($child_nids);
variable_set($already_archived_variable, $already_archived);
}
else {
$already_archived = variable_get($already_archived_variable, array());
variable_delete($already_archived_variable);
}
$child_nids = array_diff($child_nids, $already_archived);
$batch = array(
'title' => $action == 'flag' ? t('Archiving content') : t('Unarchiving content'),
'operations' => array(),
);
foreach ($child_nids as $nid) {
$batch['operations'][] = array(
'_oa_archive_batch_operation',
array(
$action,
$nid,
),
);
}
batch_set($batch);
}
}
function _oa_archive_get_section_content($nid) {
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', 'node')
->propertyCondition('status', 1)
->fieldCondition('oa_section_ref', 'target_id', $nid);
$result = $query
->execute();
if (isset($result['node'])) {
return array_keys($result['node']);
}
return array();
}
function _oa_archive_get_subspaces($nid) {
return oa_core_get_groups_by_parent($nid, OA_SPACE_TYPE, NULL, FALSE, NULL, TRUE);
}
function _oa_archive_get_sections($nid) {
return array_keys(oa_core_space_sections($nid, NODE_PUBLISHED, NULL, array(), TRUE));
}
function _oa_archive_find_archived_nodes($nids) {
$result = array();
if ($flag = flag_get_flag('trash')) {
foreach ($nids as $nid) {
if ($flag
->is_flagged($nid)) {
$result[] = $nid;
}
}
}
return $result;
}
function _oa_archive_batch_operation($action, $nid, &$context) {
if ($flag = flag_get_flag('trash')) {
$flag
->flag($action, $nid);
}
}