<?php
if (!defined('ARTICLEMS_ADMIN')) {
	header('HTTP/1.0 403 Forbidden');
	die;
}
/**
 * Admin Blocks Class
 *
 **/
class blocks extends admin{
	
	function execute()	{
		
		switch ($this->get['s']){
			
			case 'create':
				$output = $this->create_block();
				break;
				
			case 'export':
				$output = $this->export_block();
				break;
				
			case 'delete':
				$output = $this->delete_block();
				break;
				
			case 'edit':
				$output = $this->edit_block();
				break;
				
			default:
			
	        	if ($this->post['submitskin']){
	        		
	        		$this->adminsets['admin_skin'] = $this->post['selectskin'];
	        		$this->saveAdminSettings();
	        		$this->adminsets['admin_skin'] = $this->post['selectskin'];
	        		
	        	}
       
			
	            $result = $this->db->query("SELECT * FROM {$this->pre}blocks WHERE block_type = 0 AND block_skin = '{$this->adminsets['admin_skin']}'");
	
	            $text = '';
	            while ($row = $this->db->nqfetch($result)){
	                $text .= '<tr>
	                <td width="30%" style="border:1px solid;border-color:#CBD1DB">    &nbsp;&raquo;
	                <b>'.$row['block_displayname'].'</b></td>
	                <td width=10% style="border:1px solid;border-color:#CBD1DB" nowrap><a href="?a=blocks&amp;s=edit&amp;block='.$row['block_name'].'&amp;parentset='.$row['block_parentset'].'&amp;parentname='.$row['block_parentname'].'">Edit Settings</a></td>
	                <td width=10% style="border:1px solid;border-color:#CBD1DB" nowrap><a href="?a=templates&amp;block='.$row['block_name'].'&amp;parentset='.$row['block_parentset'].'&amp;parentname='.$row['block_parentname'].'">Edit Templates</a></td>
	                <td width=5% style="border:1px solid;border-color:#CBD1DB" nowrap>'.($row['block_active']? 'Yes': 'No').'</td>                 
	                 <td width=5% style="border:1px solid;border-color:#CBD1DB" nowrap><a href="?a=blocks&amp;block='.$row['block_name'].'&amp;s=delete">Delete</a></td>
	                 <td width=45% style="border:1px solid;border-color:#CBD1DB">'.htmlentities($row['block_description']).'</td>'.
	                '</tr>';
	            }
	            $output = '
	                <h1>Blocks</h1>
					<table width="40%" cellpadding=4 cellspacing=0>
		            <tr>
		            <td width=10% style="border:1px solid;border-color:#DFD299" nowrap>Blocks are part of the template to hold specific information, eg a menu.<br>
					You can use them by inserting {$Blocks->blockname} in the template.</td>
					</tr></table>
					<br>
					<b>Skin:</b><br>
					<form method="post" action="'.$this->url.'">
					 <select name=selectskin style="font-size:9pt">'.$this->getSkinList($this->adminsets['admin_skin']).'</select> 
					<input type="submit" name="submitskin" value="Select" style="font-size:9pt">
					</form>		
					<br>
	                <a href="?a=blocks&amp;s=create"><b>Create New Block</b></a><br>
	                <br>
	                <table width="100%" cellpadding=4 cellspacing=0>
	                <tr>
	                <td width=20% style="border:1px solid;border-color:#CBD1DB" nowrap><b>Block</b></td>
	                <td width=10% style="border:1px solid;border-color:#CBD1DB" nowrap><b>Settings</b></td>
	                <td width=10% style="border:1px solid;border-color:#CBD1DB" nowrap><b>Templates</b></td>
	                <td width=5% style="border:1px solid;border-color:#CBD1DB" nowrap><b>Active</b></td>
	                <td width=5% style="border:1px solid;border-color:#CBD1DB" nowrap><b>Delete</b></td>
	                <td width=45% style="border:1px solid;border-color:#CBD1DB" nowrap><b>Description</b></td>
	                </tr>
	            '.$text.'
	               ';		
		}		

        return $this->adminheader.$output.$this->adminfooter;
	}
	
	function edit_block(){

        if (isset($this->post['submit'])){
            $parent = $this->post['blockparent'];
            $parent = explode(':',$parent);
            $parentset = trim($parent[0]);
            $parentname = trim($parent[1]);
            $result = $this->db->query("UPDATE {$this->pre}blocks SET
                        block_displayname = '{$this->post['blockdisplayname']}',
                        block_description = '{$this->post['blockdescription']}',
                        block_active = {$this->post['blockactive']},
                        block_parentset = '{$parentset}',
                        block_parentname = '{$parentname}'
                        WHERE block_name = '{$this->get['block']}' AND block_skin = '{$this->adminsets['admin_skin']}' AND block_parentset = '{$this->get['parentset']}' AND block_parentname = '{$this->get['parentname']}'");
            $output = '
            <h1>Edit Block</h1>
            Block Updated';
        }
        else{

            $result = $this->db->fetch("SELECT block_name, block_displayname, block_description, block_active, block_parentset, block_parentname
                    FROM {$this->pre}blocks
                        WHERE block_name = '{$this->get['block']}' AND block_skin = '{$this->adminsets['admin_skin']}' AND block_parentset = '{$this->get['parentset']}' AND block_parentname = '{$this->get['parentname']}'");

            $output = '
                <h1>Edit Block</h1>
                <form action="" method="post">
                <table width="90%" cellpadding=4 cellspacing=0>
                <tr>
                <td width="50%"   style="border:1px solid;border-color:#CBD1DB" nowrap>
                <label><b>Block Name:</b></label>
                </td>
                <td style="border:1px solid;border-color:#CBD1DB">
                <b>'.$result['block_name'].'</b> ($Blocks->'.$result['block_name'].')
                </td>
                </tr>
                <tr>
                <td width="50%"   style="border:1px solid;border-color:#CBD1DB" nowrap>
                <label><b>Display Name:</b></label>
                </td>
                <td style="border:1px solid;border-color:#CBD1DB">
                <input type=text name=blockdisplayname value=\''.$result['block_displayname'].'\'>
                </td>
                </tr>
                <tr>
                <td width="50%"   style="border:1px solid;border-color:#CBD1DB" nowrap>
                <label><b>Block Active:</b></label>
                </td>
                <td style="border:1px solid;border-color:#CBD1DB">
                <select name="blockactive" style="font-size:9pt">
                  <option value=0>No
                  <option value=1 '.(($result['block_active'])? 'selected' :'').'>Yes
                </select>
                </td>
                </tr>
                <tr>
                <td width="50%"   style="border:1px solid;border-color:#CBD1DB" nowrap>
                <label><b>Block Parent:</b></label>
                </td>
                <td style="border:1px solid;border-color:#CBD1DB">
                <select name="blockparent" style="font-size:9pt">
                    '.$this->getBlockParentList($result['block_parentset'], $result['block_parentname']).'
                </select>
                </td>
                </tr>
                <tr>
                <td width="50%"   style="border:1px solid;border-color:#CBD1DB" nowrap>
                <label><b>Block Description:</b></label>
                </td>
                <td style="border:1px solid;border-color:#CBD1DB">
                <textarea name="blockdescription" style="width:100%" rows=2>'.htmlentities($result['block_description']).'</textarea>
                </td>
                </tr>
                </table>
                </br>
                <input type=submit value="Update" name=submit>
                </form>
                ';
            }
            return $output;
	}		
	
	function delete_block(){

        if ($this->get['confirm'] == '1'){
                $result = $this->db->query("DELETE FROM {$this->pre}blocks
                            WHERE block_name = '{$this->get['block']}' AND block_skin = '{$this->adminsets['admin_skin']}'");

            $output = '<h1>Delete Block</h1>
            Block Deleted.';

        }
        else{
            $output = '
                <h1>Delete Block</h1>
                Really delete block?<br>
                <a href="?a=blocks&amp;block='.$this->get['block'].'&amp;s=delete&amp;confirm=1">Delete Block</a>
                ';


        }
        return $output;
	}		
	
	function export_block(){

        if (isset($this->get['block'])){
            $tables = array();
            $tables['blocks'] = "block_name = '{$this->get['block']}'";
            $output = '<h1>Export Block</h1>
            Block Successfully exported:<br><br>
            
            <textarea style="width:100%" cols=20 rows=20>'.htmlentities($this->dump_database($tables, array('block_skin','__BLOCK_SKIN__'))).'</textarea>'; 
        }
        else{
            $result = $this->db->query("SELECT block_name, block_displayname, block_active, block_description FROM {$this->pre}blocks WHERE block_skin = '{$this->adminsets['admin_skin']}'");

            $text = '';
            while ($row = $this->db->nqfetch($result)){
                $text .= '<a href="?a=blocks&amp;block='.$row['block_name'].'&amp;s=export">'.$row['block_displayname'].'</a><br>';
            }
            $output = '
                <h1>Export Block</h1>
                Select the block you wish to export:<br><br>
            '.$text.'
               ';            
       }
       return $output;
	}
            		
	
	function create_block(){

        if (isset($this->post['submit'])){
            if ($this->NameIsValid($this->post['blockname'])){
                $parent = $this->post['blockparent'];
                $parent = explode(':',$parent);
                $parentset = trim($parent[0]);
                $parentname = trim($parent[1]);

                $result = $this->db->query("INSERT INTO {$this->pre}blocks SET
                            block_name = '{$this->post['blockname']}',
                            block_displayname = '{$this->post['blockname']}',
                            block_parentset = '$parentset',
                            block_parentname = '$parentname',
                            block_active = 1,
							block_skin = '{$this->adminsets['admin_skin']}'");

                if ($result){
                    $output = 'Block Created';
                }
                else{
                    $output = mysql_error();
                }
            }
            else{
                $output = 'Invalid Name';
            }
        }
        else{
            $output = '
                <h1>Create Block</h1>
                <form action="?a=blocks&amp;s=create" method="post">
                <b>Block Name:</b><br>
                <input type=text name=blockname><br>
                <br>
                <b>Block Parent</b><br>
                <select name=blockparent>
                '.$this->getBlockParentList().'
                </select>
                <br>
                <br>
                <input type=submit name=submit value="Submit">
                </form>
            ';
        }
        
        return $output;        
	}


		
	
}
?>
