<?php

/**
 * Contao Repository :: Template to display list of installed extensions
 * @copyright  Peter Koch 2008-2010
 * @author     Peter Koch, IBK Software AG
 * @license    See accompaning file LICENSE.txt
 */

$rep = &$this->rep;
$theme = &$rep->theme;
$text = &$GLOBALS['TL_LANG']['tl_repository'];
$statext = &$GLOBALS['TL_LANG']['tl_repository_statext'];
$state_options = &$GLOBALS['TL_LANG']['tl_repository_state_options'];

?>

<div id="tl_buttons" class="buttonwrapper">
  <?= $theme->createMainButton('dbcheck16', $rep->updateLink, $text['updatedatabase']) ?>
  <?= $theme->createMainButton('install16', $rep->installLink, $text['installextension']) ?>
</div>

<div class="mod_repository">
  <form action="<?= $rep->f_link ?>" id="repository_upgdform" method="post" >
    <div class="extension_container">
      <input type="hidden" name="repository_action" value="<?= $rep->f_action ?>">
      <input type="hidden" name="REQUEST_TOKEN" value="<?= REQUEST_TOKEN ?>">
      <?php if (count($rep->extensions) < 1): ?>
        <p><?= $text['noextensionsfound'] ?></p>
      <?php else: ?>
        <table class="installs">
          <tr class="title">
            <th class="col_selectcheckbox"><input type="checkbox" id="selectall" onclick="selectUnselectAll()"></th>
            <th class="col_extension"><?= $text['extension'][0] ?></th>
            <th class="col_version"><?= $text['version'][0] ?></th>
            <th class="col_build"><?= $text['build'] ?></th>
            <th class="col_status"><?= $text['status'] ?></th>
            <th class="col_functions">&nbsp;</th>
          </tr>
          <?php foreach ($rep->extensions as $ext):
            $icons = array($theme->createListButton('edit', $ext->editLink, $text['editextension']));
            if (property_exists($ext, 'uninstallLink'))
              $icons[] = $theme->createListButton('uninstall', $ext->uninstallLink, $text['uninstallextension']);
            if (property_exists($ext, 'manualLink'))
              $icons[] = $theme->createListButton('manual16', $ext->manualLink, $text['manual'], '', true);
            if (property_exists($ext, 'forumLink'))
              $icons[] = $theme->createListButton('forum16', $ext->forumLink, $text['forum'], '', true);
            if (property_exists($ext, 'updateLink'))
              $icons[] = $theme->createListButton('ok16', $ext->updateLink, $text['updateextension']);
            ?>
            <tr class="datarow">
              <td class="col_selectcheckbox"><?php if (property_exists($ext, 'updateLink')): ?><input type="checkbox" name="selectedids[]" value="<?= $ext->id ?>" onclick="enableSelectFunctions()"><?php else: ?>&nbsp;<?php endif; ?></td>
              <td class="col_extension"><?= property_exists($ext, 'catalogLink') ? '<a href="'.$ext->catalogLink.'" title="'.$text['showdetails'].'">'.$ext->extension.'</a>' : $ext->extension ?></td>
              <td class="col_version"><?= Repository::formatVersion($ext->version) ?></td>
              <td class="col_build"><?= $ext->build ?></td>
              <td class="col_status"><?php foreach ($ext->status as $sta) echo '<div class="color_'.$sta->color.'">'.sprintf($statext[$sta->text], $sta->par1, $sta->par2).'</div>'; ?></td>
              <td class="col_functions"><?= implode(' ',$icons) ?></td>
            </tr>
          <?php endforeach; ?>
        </table>
      <?php endif; ?>
    </div>
    <div class="tl_formbody_submit">
      <div class="tl_submit_container">
        <input type="submit" name="repository_upgradebutton" id="repository_upgradebutton" class="tl_submit" value="<?= $text['updateextensions'] ?>">
      </div>
    </div>
  </form>
</div>

<script>
  function enableSelectFunctions() {
    var sel = false;
    var cbs = document.getElementsByName('selectedids[]');
    for (var i = 0; i < cbs.length; ++i) {
      if (cbs[i].checked) {
        sel = true;
        break;
      }
    }
    document.getElementById('repository_upgradebutton').disabled = !sel;
  }
  function selectUnselectAll() {
    var chk = document.getElementById('selectall').checked;
    var cbs = document.getElementsByName('selectedids[]');
    for (var i = 0; i < cbs.length; ++i) cbs[i].checked = chk;
    enableSelectFunctions();
  }
  enableSelectFunctions();
</script>
