<?php
class shellcommand extends Module
{
	
    public function __construct(database $db, user $user, $mediaid = '', $layoutid = '', $regionid = '', $lkid = '')
    {
        // Must set the type of the class
        $this->type = 'shellcommand';
        $this->displayType = 'Shell Command';

        // Must call the parent class
        parent::__construct($db, $user, $mediaid, $layoutid, $regionid, $lkid);
    }
	
    /**
     * Return the Add Form as HTML
     * @return
     */
    public function AddForm()
    {
        $db =& $this->db;
        $user =& $this->user;
        $layoutid = $this->layoutid;
        $regionid = $this->regionid;
        
        $msgWindows = __('Windows Command');
        $msgLinux = __('Linux Command');

        $form = <<<FORM
        <form id="ModuleForm" class="VsignForm" method="post" action="index.php?p=module&mod=$this->type&q=Exec&method=AddMedia">
            <input type="hidden" name="layoutid" value="$layoutid">
            <input type="hidden" id="iRegionId" name="regionid" value="$regionid">
            <input type="hidden" name="showRegionOptions" value="$this->showRegionOptions" />
            <table>
                <tr>
                    <td><label for="windowsCommand" title="">$msgWindows</label></td>
                    <td><input id="windowsCommand" name="windowsCommand" type="text"></td>
                </tr>
                <tr>
                    <td><label for="linuxCommand" title="">$msgLinux</label></td>
                    <td><input id="linuxCommand" name="linuxCommand" type="text"></td>
                </tr>
            </table>
        </form>
FORM;

        $this->response->html = $form;
        if ($this->showRegionOptions)
        {
            $this->response->AddButton(__('Cancel'), 'VsignSwapDialog("index.php?p=layout&layoutid=' . $layoutid . '&regionid=' . $regionid . '&q=RegionOptions")');
        }
        else
        {
            $this->response->AddButton(__('Cancel'), 'VsignDialogClose()');
        }
        $this->response->AddButton(__('Save'), '$("#ModuleForm").submit()');
        $this->response->dialogTitle = __('Add Shell Command');
        $this->response->dialogSize 	= true;
        $this->response->dialogWidth 	= '450px';
        $this->response->dialogHeight 	= '250px';

        return $this->response;
    }
	
    /**
     * Return the Edit Form as HTML
     * @return
     */
    public function EditForm()
    {
        $db =& $this->db;
        $layoutid = $this->layoutid;
        $regionid = $this->regionid;
        $mediaid = $this->mediaid;

        // Permissions
        if (!$this->auth->edit)
        {
            $this->response->SetError('You do not have permission to edit this assignment.');
            $this->response->keepOpen = true;
            return $this->response;
        }
		
        $windowsCommand = $this->GetOption('windowsCommand');
        $linuxCommand = $this->GetOption('linuxCommand');

        $msgWindows = __('Windows Command');
        $msgLinux = __('Linux Command');

        $form = <<<FORM
        <form id="ModuleForm" class="VsignForm" method="post" action="index.php?p=module&mod=$this->type&q=Exec&method=EditMedia">
            <input type="hidden" name="layoutid" value="$layoutid">
            <input type="hidden" id="iRegionId" name="regionid" value="$regionid">
            <input type="hidden" name="mediaid" value="$mediaid">
            <input type="hidden" name="showRegionOptions" value="$this->showRegionOptions" />
            <table>
                <tr>
                    <td><label for="windowsCommand" title="">$msgWindows</label></td>
                    <td><input id="windowsCommand" name="windowsCommand" type="text" value="$windowsCommand"></td>
                </tr>
                <tr>
                    <td><label for="linuxCommand" title="">$msgLinux</label></td>
                    <td><input id="linuxCommand" name="linuxCommand" type="text" value="$linuxCommand"></td>
                </tr>
            </table>
        </form>
FORM;

        $this->response->html = $form;

        if ($this->showRegionOptions)
        {
            $this->response->AddButton(__('Cancel'), 'VsignSwapDialog("index.php?p=layout&layoutid=' . $layoutid . '&regionid=' . $regionid . '&q=RegionOptions")');
        }
        else
        {
            $this->response->AddButton(__('Cancel'), 'VsignDialogClose()');
        }

        $this->response->AddButton(__('Save'), '$("#ModuleForm").submit()');
        $this->response->dialogTitle = __('Edit Shell Command');
        $this->response->dialogSize 	= true;
        $this->response->dialogWidth 	= '450px';
        $this->response->dialogHeight 	= '250px';

        return $this->response;
    }
	
    /**
     * Add Media to the Database
     * @return
     */
    public function AddMedia()
    {
        $db =& $this->db;

        $layoutid = $this->layoutid;
        $regionid = $this->regionid;
        $mediaid = $this->mediaid;

        // Required Attributes
        $this->mediaid = md5(uniqid());
        $this->duration = 1;
        
        $windowsCommand = Kit::GetParam('windowsCommand', _POST, _STRING);
        $linuxCommand = Kit::GetParam('linuxCommand', _POST, _STRING);

        if ($windowsCommand == '' && $linuxCommand == '')
        {
            $this->response->SetError('You must enter a command');
            $this->response->keepOpen = true;
            return $this->response;
        }

        // Any Options
        $this->SetOption('windowsCommand', $windowsCommand);
        $this->SetOption('linuxCommand', $linuxCommand);

        // Should have built the media object entirely by this time
        // This saves the Media Object to the Region
        $this->UpdateRegion();

        // Set this as the session information
        setSession('content', 'type', 'shellcommand');

        if ($this->showRegionOptions)
        {
            // We want to load a new form
            $this->response->loadForm = true;
            $this->response->loadFormUri = "index.php?p=layout&layoutid=$layoutid&regionid=$regionid&q=RegionOptions";
        }

        return $this->response;
    }

    /**
     * Edit Media in the Database
     * @return
     */
    public function EditMedia()
    {
        $db =& $this->db;

        $layoutid = $this->layoutid;
        $regionid = $this->regionid;
        $mediaid = $this->mediaid;

        if (!$this->auth->edit)
        {
            $this->response->SetError('You do not have permission to edit this assignment.');
            $this->response->keepOpen = false;
            return $this->response;
        }

        $windowsCommand = Kit::GetParam('windowsCommand', _POST, _STRING);
        $linuxCommand = Kit::GetParam('linuxCommand', _POST, _STRING);

        if ($windowsCommand == '' && $linuxCommand == '')
        {
            $this->response->SetError('You must enter a command');
            $this->response->keepOpen = true;
            return $this->response;
        }

        // Any Options
        $this->duration = 1;
        $this->SetOption('windowsCommand', $windowsCommand);
        $this->SetOption('linuxCommand', $linuxCommand);

        // Should have built the media object entirely by this time
        // This saves the Media Object to the Region
        $this->UpdateRegion();

        // Set this as the session information
        setSession('content', 'type', 'shellcommand');

        if ($this->showRegionOptions)
        {
            // We want to load a new form
            $this->response->loadForm = true;
            $this->response->loadFormUri = "index.php?p=layout&layoutid=$layoutid&regionid=$regionid&q=RegionOptions";
        }

        return $this->response;
    }

    public function Preview($width, $height)
    {
        $msgWindows = __('Windows Command');
        $msgLinux = __('Linux Command');

        $preview = '';
        $preview .= '<p>' . $msgWindows . ': ' . $this->GetOption('windowsCommand') . '</p>';
        $preview .= '<p>' . $msgLinux . ': ' . $this->GetOption('linuxCommand') . '</p>';

        return $preview;
    }

    public function HoverPreview()
    {
        return $this->Preview(0,0);
    }
}
?>