You are here

function oa_archive_trigger_flag in Open Atrium Archive 7.2

Helper function for flag and unflag hooks.

We need to archive/unarchive child content when archiving a Space or Section. We handle unarchiving parents who had child content that was already archived by storing a list of those nodes in a variable.

2 calls to oa_archive_trigger_flag()
oa_archive_flag_flag in ./oa_archive.module
Implements hook_flag_flag().
oa_archive_flag_unflag in ./oa_archive.module
Implements hook_flag_unflag().

File

./oa_archive.module, line 166

Code

function oa_archive_trigger_flag($action, $flag, $content_id, $account) {
  if ($flag->name == 'trash') {
    $node = node_load($content_id);

    // We only deal with Spaces or Sections.
    if (empty($node) || !in_array($node->type, array(
      OA_SPACE_TYPE,
      OA_SECTION_TYPE,
    ))) {
      return;
    }

    // Get all the child NIDs.
    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';

    // Since we are recursively flagging child content, we need to record which
    // child nodes were already archived, so that we can leave them in that
    // state when unarchiving the parent.
    if ($action == 'flag') {
      $already_archived = _oa_archive_find_archived_nodes($child_nids);
      variable_set($already_archived_variable, $already_archived);
    }
    else {

      // If we're unflagging, so we need to pull this list from storage.
      $already_archived = variable_get($already_archived_variable, array());
      variable_delete($already_archived_variable);
    }

    // We don't need to bother archiving content that's already archived, or
    // unarchiving content that should remain that way.
    $child_nids = array_diff($child_nids, $already_archived);

    // Fire off a batch operation to do the flagging/unflagging.
    $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);
  }
}