<?php

require_once '../commons/base.inc.php';
try {
    $Host = $FOGCore->getHostItem();
    // Try and get the task.
    $Task = $Host->get('task');
    // Work on the current Snapin Task.
    $SnapinTask = new SnapinTask($_REQUEST['taskid']);
    if (!$SnapinTask->isValid()) {
        throw new Exception('#!ns');
    }
    //Get the snapin to work off of.
    $Snapin = new Snapin($SnapinTask->get('snapinID'));
    // Find the Storage Group
    if ($Snapin && $Snapin->getStorageGroup() && $Snapin->isValid() && $Snapin->getStorageGroup()->isValid()) {
        $StorageGroup = $Snapin->getStorageGroup();
    }
    // Allow plugins to enact against this. (e.g. location)
    $HookManager->processEvent('SNAPIN_GROUP', array('Host' => &$Host, 'StorageGroup' => &$StorageGroup));
    // Assign the file for sending.
    if (!$StorageGroup || !$StorageGroup->isValid()) {
        if (file_exists(rtrim($FOGCore->getSetting('FOG_SNAPINDIR'), '/') . '/' . $Snapin->get('file'))) {
            $SnapinFile = rtrim($FOGCore->getSetting('FOG_SNAPINDIR'), '/') . '/' . $Snapin->get('file');
        } elseif (file_exists($Snapin->get('file'))) {
            $SnapinFile = $Snapin->get('file');
        }
    } else {
        $StorageNode = $StorageGroup->getMasterStorageNode();
        // Allow plugins to enact against this. (e.g. location)
        $HookManager->processEvent('SNAPIN_NODE', array('Host' => &$Host, 'StorageNode' => &$StorageNode));
 public function active_snapins_post()
 {
     if (isset($_REQUEST['rmid'])) {
         // Get the snapin task.
         $SnapinTask = new SnapinTask($_REQUEST['rmid']);
         // Get the job associated with the task.
         $SnapinJob = new SnapinJob($SnapinTask->get('jobID'));
         // Get the referenced host.
         $Host = new Host($SnapinJob->get('hostID'));
         // Get the active task.
         $Task = current($this->getClass('TaskManager')->find(array('hostID' => $Host->get('id'), 'stateID' => array(1, 2, 3))));
         // Check the Jobs to Snapin tasks to verify if this is the only one.
         $SnapinJobManager = $this->getClass('SnapinTaskManager')->find(array('jobID' => $SnapinJob->get('id')));
         // This task is the last task, destroy the job and the task
         if (count($SnapinJobManager) <= 1) {
             $SnapinJob->destroy();
             if ($Task) {
                 $Task->cancel();
             }
         }
         // Destroy the individual task.
         $SnapinTask->destroy();
         // Redirect to the current page.
         $this->FOGCore->redirect("?node=" . $this->node . "&sub=active-snapins");
     }
 }