<?php

class counter extends Module
{

    public function __construct(database $db, user $user, $mediaid = '', $layoutid = '', $regionid = '', $lkid = '')
    {
        // Must set the type of the class
        $this->type = 'counter';
        $this->displayType = 'Counter';
        $this->name = 'Counter';

        // 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;

        // Would like to get the regions width / height
        $layoutid	= $this->layoutid;
        $regionid	= $this->regionid;
        $rWidth		= Kit::GetParam('rWidth', _REQUEST, _STRING);
        $rHeight	= Kit::GetParam('rHeight', _REQUEST, _STRING);

        $form = <<<FORM
            <form id="ModuleForm" class="VsignTextForm" 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 colspan="2"><center>Python Client Only</center></td>
                    </tr>
                    <tr>
                        <td><label for="duration" title="The duration in seconds this counter should be displayed">Duration<span class="required">*</span></label></td>
                        <td><input id="duration" name="duration" type="text"></td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <input id="popupNotification" name="popupNotification" type="checkbox">
                            <label for="popupNotification" title="Popup a notification when the counter changes">Popup Notification?</label>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="4">
                            <textarea id="ta_text" name="ta_text">[Counter]</textarea>
                        </td>
                    </tr>
                </table>
            </form>
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->html = $form;
        $this->response->callBack = 'text_callback';
        $this->response->dialogTitle = __('Add Counter');
        $this->response->AddButton(__('Save'), '$("#ModuleForm").submit()');

        return $this->response;
    }

    /**
     * Return the Edit Form as HTML
     * @return
     */
    public function EditForm()
    {
        $db =& $this->db;
        $user =& $this->user;
        $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;
        }

        // Other properties
        $popupNotification = $this->GetOption('popupNotification');

        // Get the text out of RAW
        $rawXml = new DOMDocument();
        $rawXml->loadXML($this->GetRaw());

        Debug::LogEntry($db, 'audit', 'Raw XML returned: ' . $this->GetRaw());

        // Get the Text Node out of this
        $textNodes = $rawXml->getElementsByTagName('template');
        $textNode = $textNodes->item(0);
        $text = $textNode->nodeValue;

        $durationFieldEnabled = ($this->auth->modifyPermissions) ? '' : ' readonly';
        $popupNotificationChecked = ($popupNotification) ? 'checked' : '';

        // Output the form
        $form = <<<FORM
            <form id="ModuleForm" class="VsignTextForm" 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 colspan="2"><center>Python Client Only</center></td>
                    </tr>
                    <tr>
                        <td><label for="duration" title="The duration in seconds this counter should be displayed">Duration<span class="required">*</span></label></td>
                        <td><input id="duration" name="duration" value="$this->duration" type="text" $durationFieldEnabled></td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <input id="popupNotification" name="popupNotification" type="checkbox" $popupNotificationChecked>
                            <label for="popupNotification" title="Popup a notification when the counter changes">Popup Notification?</label>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="4">
                            <textarea id="ta_text" name="ta_text">$text</textarea>
                        </td>
                    </tr>
                </table>
            </form>
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->html = $form;
        $this->response->callBack = 'text_callback';
        $this->response->dialogTitle = __('Edit Counter');
        $this->response->AddButton(__('Save'), '$("#ModuleForm").submit()');

        return $this->response;
    }

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

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

        //Other properties
        $duration = Kit::GetParam('duration', _POST, _INT, 0);
        $text = Kit::GetParam('ta_text', _POST, _HTMLSTRING);
        $popupNotification = Kit::GetParam('popupNotification', _POST, _CHECKBOX);

        $url = "index.php?p=layout&layoutid=$layoutid&regionid=$regionid&q=RegionOptions";

        //validation
        if ($text == '')
        {
            $this->response->SetError('Please enter a template');
            $this->response->keepOpen = true;
            return $this->response;
        }

        if ($duration == 0)
        {
            $this->response->SetError('You must enter a duration.');
            $this->response->keepOpen = true;
            return $this->response;
        }

        // Required Attributes
        $this->mediaid	= md5(uniqid());
        $this->duration = $duration;

        // Any Options
        $this->SetOption('popupNotification', $popupNotification);
        $this->SetRaw('<template><![CDATA[' . $text . ']]></template>');

        // 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', 'counter');

        if ($this->showRegionOptions)
        {
            // We want to load a new form
            $this->response->loadForm = true;
            $this->response->loadFormUri = $url;
        }
        
        return $this->response;
    }

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

        $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;
        }

        //Other properties
        $text = Kit::GetParam('ta_text', _POST, _HTMLSTRING);
        $popupNotification = Kit::GetParam('popupNotification', _POST, _CHECKBOX);

        Debug::LogEntry($db, 'audit', 'Popup notification:' . $popupNotification);

        // If we have permission to change it, then get the value from the form
        if ($this->auth->modifyPermissions)
            $this->duration = Kit::GetParam('duration', _POST, _INT, 0);

        Debug::LogEntry($db, 'audit', 'Text received: ' . $text);

        $url = "index.php?p=layout&layoutid=$layoutid&regionid=$regionid&q=RegionOptions";

        // Validation
        if ($text == '')
        {
            $this->response->SetError('Please enter a template');
            $this->response->keepOpen = true;
            return $this->response;
        }

        if ($this->duration == 0)
        {
            $this->response->SetError('You must enter a duration.');
            $this->response->keepOpen = true;
            return $this->response;
        }

        // Any Options
        $this->SetOption('popupNotification', $popupNotification);
        $this->SetRaw('<template><![CDATA[' . $text . ']]></template>');

        // 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', 'counter');

        if ($this->showRegionOptions)
        {
            // We want to load a new form
            $this->response->loadForm = true;
            $this->response->loadFormUri = $url;
        }

        return $this->response;
    }

    public function Preview($width, $height)
    {
        $regionid   = $this->regionid;

        // Get the text out of RAW
        $rawXml = new DOMDocument();
        $rawXml->loadXML($this->GetRaw());

        // Get the Text Node out of this
        $textNodes 	= $rawXml->getElementsByTagName('template');
        $textNode 	= $textNodes->item(0);
        $text 	= $textNode->nodeValue;

        $textId 	= $regionid.'_text';
        $innerId 	= $regionid.'_innerText';
        $timerId	= $regionid.'_timer';
        $widthPx	= $width.'px';
        $heightPx	= $height.'px';

        //Show the contents of text accordingly
        $return = <<<END
        <div id="$textId" style="position:relative; overflow:hidden ;width:$widthPx; height:$heightPx; font-size: 1em;">
            <div id="$innerId" style="position:absolute; left: 0px; top: 0px;">
                <div class="article">
                        $text
                </div>
            </div>
        </div>
END;
        return $return;
    }
}
?>
