<% title _("Permissions report") %>

<%= form_tag(report_roles_path, :id => 'permissions_form') do %>
  <%= hidden_field_tag 'permissions[0]', '', :id => nil %>
  <table class="table table-bordered table-striped">
    <thead>
      <tr>
        <th><%= _("Permissions") %></th>
        <% @roles.each do |role| %>
          <th>
            <%= content_tag(role.builtin? ? 'em' : 'span', h(role.name)) %>
            <%= link_to_function(image_tag('toggle_check.png'), "toggleCheckboxesBySelector('input.role-#{role.id}')",
                                 :title => _("Check all/Uncheck all"))
            %>
          </th>
        <% end %>
      </tr>
    </thead>
    <tbody>
      <% perms_by_block = @permissions.group_by {|p| p.security_block.to_s} %>
      <% perms_by_block.keys.sort.each do |block| %>
        <% unless block.blank? %>
          <tr class="group open">
            <td colspan="<%= @roles.size + 1 %>">
              <span class="expander" onclick="toggleRowGroup(this); return false;">&nbsp;</span>
              <%= block.humanize %>
            </td>
          </tr>
        <% end %>
        <% perms_by_block[block].each do |permission| %>
          <tr class="<%= cycle('odd', 'even') %> permission-<%= permission.name %>">
            <td>
              <%= link_to_function(image_tag('toggle_check.png'), "toggleCheckboxesBySelector('.permission-#{permission.name} input')",
                                   :title => _("Check all/Uncheck all"))
              %>
              <%= permission.name.to_s.humanize %>
            </td>
            <% @roles.each do |role| %>
              <td align="center">
                <% if role.setable_permissions.include? permission %>
                  <%= check_box_tag "permissions[#{role.id}][]", permission.name, (role.permissions.include? permission.name),
                    :id => nil, :class => "role-#{role.id}" %>
                <% end %>
              </td>
            <% end %>
          </tr>
        <% end %>
      <% end %>
    </tbody>
  </table>
  <p><%= submit_tag _("Save") %></p>
<% end %>
