<?php
$otags  = get_records_sql('SELECT * from '.$CFG->prefix.'tags WHERE type=\'official\' ORDER by text ASC');
$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');

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

<table>
    <tr>
        <td width="50%" align="left"><label for="otags"><?php print_string('otags','blog');?></label></td>
        <td width="50%" align="left"><label for="ptags"><?php print_string('ptags','blog');?></label></td>
    </tr>

    <tr>
        <td>
            <form action="tags.php" method="post">
            <input type="hidden" name="sesskey" value="<?php echo sesskey(); ?>" />
            <?php
            if (has_capability('moodle/blog:manageofficialtags', $sitecontext)) {
            ?>
            <select name="tags[]" id="otags" multiple="multiple" size="8">
            <?php
            if ($otags) {
                foreach ($otags as $otag) {
                    echo '<option value="'.$otag->id.'">'.$otag->text.'</option>';
                }
            }
            ?>
            </select>
            <input type="hidden" name="action" value="delete" />
            <br />
            <input type="submit" value="<?php print_string('delete');?>" />
            <?php }
                else {
                    if ($otags) {
                        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" id="ptags" size="8">
            <?php

                $ptags = array();
                
                // Not sure if this sowring is needed anymore
                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="action" value="delete" />
            <br />
            <input type="submit" value="<?php print_string('delete');?>" />
            </form>
        </td>
    </tr>

    <tr>
        <td>
            <?php if (has_capability('moodle/blog:manageofficialtags', $sitecontext)) { ?>
            <form action="tags.php" method="post">
            <input type="hidden" name="sesskey" value="<?php echo sesskey(); ?>" />
            <label for="newotag"><?php print_string('addotags','blog');?></label>:<br/>
            <input type="text" name="otag" id="newotag" />
            <input type="submit" value="<?php print_string('add');?>" />
            <input type="hidden" name="action" value="addofficial" />
            </form>
            <?php }?>
        </td>

        <td>
        </td>
    </tr>

</table>

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