<?php
print_heading(get_string('tagmanagement'));
?>

<table>
    <tr>
        <td width="50%"><?php print_string('otags','blog');?></td>
        <td width="50%"><?php print_string('ptags','blog');?></td>
    </tr>
    
    <tr>
        <td>
            <form action="tags.php" method="POST">
            <input type="hidden" name="sesskey" value="<?php echo sesskey(); ?>" />
            <?php if (isadmin()){ ?>
            <select name="tags[]" multiple="multiple" size="8">
            <?php
                $otags = get_records_sql('SELECT * from '.$CFG->prefix.'tags WHERE type=\'official\' ORDER by text ASC');
                foreach ($otags as $otag) {
                    echo '<option value="'.$otag->id.'">'.$otag->text.'</option>';
                }
            ?>
            </select>
            <input type="hidden" name="mode" value="delete" />
            <br />
            <input type="submit" value="<?php print_string('delete');?>" />
            <?php }
                else {
                    if ($otags = get_records_sql('SELECT * from '.$CFG->prefix.'tags WHERE type=\'official\' ORDER by text ASC')) {
                        foreach ($otags as $otag) {
                            echo '<br />'.$otag->text;
                        }
                    }
                }
            ?>
            </form>
        </td>
        
        <td>
            <form action="tags.php" method="POST">
            <input type="hidden" name="sesskey" value="<?php echo sesskey(); ?>" />
            <select name="tags[]" multiple="multiple" size="8">
            <?php
                $ptags1 = get_records_sql('SELECT * from '.$CFG->prefix.'tags WHERE type=\'personal\' AND userid = '.$USER->id.' ORDER by text ASC');
                $ptags2 = get_records_sql('SELECT * from '.$CFG->prefix.'tags WHERE type=\'personal\' AND userid <> '.$USER->id.' ORDER by text ASC');
                
                $ptags = array();
                
                if (!empty($ptags1)) { // user's own tag exists
                    if (!empty($ptags2)) { // user's own tags, and other users tags exist, we merge
                        $ptags = array_merge($ptags1, $ptags2);
                    } else { // user's own tags exist, no other personal tags, just use that
                        $ptags = $ptags1;
                    }
                } else if (!empty($ptags2)) {
                    $ptags = $ptags2;
                } 

                if (!empty($ptags)) {
                    foreach ($ptags as $ptag) {
                        echo '<option value="'.$ptag->id.'">'.$ptag->text.'</option>';
                    }
                }
            ?>
            </select>
            <input type="hidden" name="mode" value="delete" />
            <br />
            <input type="submit" value="<?php print_string('delete');?>" />
            </form>
        </td>
    </tr>
    
    <tr>
        <td>
            <?php if (isadmin()) { ?>
            <form action="tags.php" method="POST">
            <input type="hidden" name="sesskey" value="<?php echo sesskey(); ?>" />
            <?php print_string('addotags','blog');?>:<br/>
            <input type="text" name="otag" />
            <input type="submit" value="<?php print_string('add');?>" />
            <input type="hidden" name="mode" value="addofficial" />
            </form>
            <?php }?>
        </td>
        
        <td>
            <form action="tags.php" method="POST">
            <input type="hidden" name="sesskey" value="<?php echo sesskey(); ?>" />
            <?php print_string('addptags','blog');?><br/>
            <input type="text" name="ptag" />
            <input type="submit" value="<?php print_string('add');?>" />
            <input type="hidden" name="mode" value="addpersonal" />
            </form>
        </td>
    </tr>
    
</table>

<p align="center">
    <a href="#" onclick="window.close()"><?php print_string('closewindow');?></a>
</p>
