<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:xi="http://www.w3.org/2001/XInclude"
      xmlns:py="http://genshi.edgewall.org/">
    <xi:include href="layout.html" />
    <xi:include href="macros.html" />
    <xi:include href="agilo_macros.html" />
    <head>
        <title>${_('Backlog')}: ${_(backlog) and _(backlog.name) or _('Not Found')}</title>
        <script type="text/javascript">
        //<![CDATA[
        // We need to use jQuery and not the 'dollar' sign inside here, cause
        // may interfere with Genshi templating, so to avoid any conflict please
        // remind to always use jQuery inside HTML files.
        var SPRINT = { name: "${sprint and sprint.name or ''}" };
        var IS_SPRINT_BACKLOG = ${(backlog and backlog.b_type == 1) and 'true' or 'false'};
        var CHILDTYPE_MAPPING = $child_type_mapping;
        var BACKLOG_INFO = $backlog_info;
        //]]>
        </script>
    </head>

<?python
from agilo.ticket.renderers import Renderer
from agilo.utils import Key

def get_valid_column(col, bi):
    """
    Function to check if a column with all alternate possible fields
    is a valid column for the given backlog item type. Returns the
    column to use.
    """
    res = None
    # Get the alternative column
    alt_name = col.get(Key.ALTERNATIVE)
    if bi.ticket.is_readable_field(col[Key.NAME]):
        res = col
    elif bi.ticket.is_readable_field(alt_name):
        res = bi.ticket.get_field(alt_name)
    if res is not None:
        # We need to associate a new renderer for each value
        res[Key.RENDERED] = Renderer(bi.ticket, res[Key.NAME])
    return res

def requires_user(backlog, i, username):
    """
    Function to see if this row or a row of a higher level is owned by this user
    or requires him/her as a resource. Gets passed the index of the current row
    and scans from there.
    """
    if backlog[i].ticket[Key.OWNER] == username or \
      (backlog[i].ticket[Key.RESOURCES] or '').find(username) > -1:
        return True

    level = backlog[i].level
    i+=1
    while backlog[i] and backlog[i].level > level:
        if backlog[i].ticket[Key.OWNER] == username or \
          (backlog[i].ticket[Key.RESOURCES] or '').find(username) > -1:
            return True
        i += 1
    return False

# We need this method to detect if a field contains a digit or a text value
from trac.util.text import to_unicode
?>

    <body>
    <div id="content" class="report">
        <h1>${_(backlog) and _(backlog.name)} <span py:if="backlog and backlog.scope and backlog.scope != 'global'" py:strip="">for ${backlog and backlog.scope}</span>
        <span py:if="backlog and backlog.count() != 0" class="numrows" py:with="item_counts = backlog and backlog.count()">
                (<span id="items_count">$item_counts</span> of <span id="total_items">$item_counts</span> items)
        </span>
        </h1>
        <div class="sprint_info" py:if="sprint">
            <strong py:if="backlog.b_type == 1 and defined('team') and team"><a href="${href.team(team.name)}">${team.name}</a></strong>
            <div class="description">${wiki_to_html(context, sprint.description)}</div>
            <div class="status">
                <b>${_('Start')}:</b> ${sprint.start}
                <b>${_('End')}:</b> ${sprint.end}
                <b>${_('Status')}:</b> <span class="sprint_status ${(sprint.is_closed and 'closed') or (sprint.is_currently_running and 'running') or 'not_started'}" ></span>
            </div>
            <!--! 
                req.query_string was added with Trac 0.11.2 (http://trac.edgewall.org/changeset/7493) so 
                for Trac 0.11.1 we can not use this property. Therefore I added a fallback (which is 
                actually the exact implementation of the property in trac 0.11.2)
            -->
            <py:with vars="query_string = req.query_string or req.environ.get('QUERY_STRING', '')">
                <py:choose>
                    <py:when test="may_edit_backlog">
                        <a href="${req.href.sprint(sprint.name, 'edit', {'redirect': req.path_info + '/?' + query_string})}">${_('Edit Sprint details')}.</a>
                    </py:when>
                    <py:otherwise> 
                        <a href="${req.href.sprint(sprint.name)}">${_('View Sprint details')}.</a>
                    </py:otherwise>
                </py:choose>
            </py:with>
        </div>

        <div class="charts" py:if="charts" py:attrs="charts_attrs">
            <py:for each="chart in charts">
                ${chart.display()}
            </py:for>
        </div>

        <!--! GvdM: Legenda -->
        <h3 class="legenda">${_('Legenda')}</h3>
        <ul class="backlog legenda" id="legenda">
            <li class="requirement">${_('Requirement')}</li>
            <li class="story">${_('Story')}</li>
            <li class="task">${_('Task')}</li>
            <li class="accepted">${_('Accepted')}</li>
            <li class="done">${_('Done')}</li>
            <li class="closed">${_('Closed')}</li>
            <li class="bug">${_('Bug')}</li>
        </ul>

        <p class="date" py:if="backlog">${wiki_to_html(context, backlog.description)}</p>
        <div class="buttons">
            <form id="display_form"> <!--! py:if="backlog.b_type != 0" > --> 
                <input type="button"
                       id="my-tickets"
                       value="${my_tickets and 'Show everybody\'s tickets' or 'Show my tickets'}"
                       name="my_tickets"
                       title="Show all tickets assigned or owned by the current user"
                       class="${my_tickets and 'down' or None}" />
                <input type="button"
                       id="closed-tickets"
                       value="${closed_tickets and 'Show closed tickets' or 'Hide closed tickets'}"
                       name="closed_tickets"
                       title="Hide or Show tickets that are closed or done"
                       class="${closed_tickets and 'down' or None}" />
            </form>
            <form id="filter_attribute_chooser" name="filter_attribute_chooser" class="no_print" 
	                py:if="(attribute_filter_options is not None) and (backlog is not None)">
	            <!--! b_type == 1 -> Sprint Backlog -->
	            <input type="hidden" name="bscope" value="${backlog and backlog.scope}" 
	                   py:if="backlog and backlog.scope and backlog.scope != 'global'" />
	            <input type="hidden" name="name" value="${backlog_name}" />
	            <p>
	                Show only items for:
	                <select name="filter_by">
	                    <option value="">${_('show all')}</option>
	                    <option py:for="option in attribute_filter_options"
	                            selected="${backlog and backlog.filter_by == option and 'selected' or None}">
	                            $option</option>
	                </select>
	                <input type="submit" id="choose_filter_submit" name="choose_filter_submit" value="Filter" />
	            </p>
	        </form>
        </div>

        <form method="POST" action="" id="backlog_form">
            <table id="backlog" class="listing tickets backlog">
                <thead>
                    <tr class="nodrag nodrop">
                        <!--! <th class="dragHandle"></th> -->
                        <th class="checkbox"></th>
                        <py:for each="header in headers">
                            <th>${header}</th>
                        </py:for>
                    </tr>
                </thead>
                <tbody>
                    <py:for each="i, bi in enumerate(backlog)">
                        <py:with vars="is_editable = can_edit_ticket(req, bi.ticket);
                                       has_checkbox = can_select_ticket_to_do_some_action(req, bi.ticket);
                                       hide_ticket_due_to_status = ((bi.ticket.status=='closed' and not closed_tickets) or (my_tickets and not requires_user(backlog, i, username)));
                                       hide_ticket_due_to_filter = not bi.is_visible;">
                        <!--! FIXME: this calls requires_user twice, which unnecessary.
                              For some reason, Genshi does not update a py:with directive
                              in the loop though, so for now this is it.
                        -->
                        <tr id="${bi.id}" style="${(hide_ticket_due_to_status or hide_ticket_due_to_filter) and 'display:none;' or None}"
                            class="${bi.style} level${bi.level} ${i % 2 and 'odd' or 'even'} ${bi.ticket.type} ${requires_user(backlog, i, username) and 'user' or 'notuser'} ${(bi.ticket.status == 'closed' and bi.ticket.remaining_time == '0' and 'done') or bi.ticket.status} ${is_editable and 'is-editable' or ''}">
                            <!--! <td class="dragHandle"></td> -->
                            <td class="checkbox">
                                <input type="hidden" 
                                       name="${bi.id}_ts" 
                                       value="${str(bi.ticket.time_changed)}" />
                                <input type="checkbox" 
                                    name="chk_${bi.id}" 
                                    id="chk-${bi.id}" 
                                    title="Select this ticket" 
                                    py:if="has_checkbox"/>
                            </td>
                            <py:for each="col in columns">
	                            <py:choose py:with="name = col.name">
	                                <py:when test="name in ('id', 'summary')">
	                                    <!--! ID Column requires the link -->
	                                    <td class="${name == 'summary' and 'summary' or None} ticket-field-${name}">
	                                        <a title="View Ticket" href="${href.ticket(bi.id)}"><py:if test="name == 'id'">#</py:if>${bi[name]}</a>
	                                    </td>
	                                </py:when>
	                                <py:otherwise>
	                                    <!--! Other columns -->
	                                    <td class="other ticket-field-${name}">
	                                    <py:with vars="column = get_valid_column(col, bi)"
	                                             py:if="get_valid_column(col, bi) is not None">
		                                    <py:choose>
		                                        <py:when test="column.editable and is_editable and bi.ticket['status'] != 'closed'">
		                                        <py:choose>
		                                            <select py:when="column.type in ('select', 'radio')" 
		                                                    id="col-${bi.id}-${column.name}" 
		                                                    name="col_${bi.id}_${column.name}"
		                                                    title="${column.name}">
		                                                <option py:if="col.optional"></option>
		                                                <option py:for="option in column.options"
		                                                        selected="${bi[column.name] == option or None}"
		                                                        py:content="option"></option>
		                                                <optgroup py:for="optgroup in column.optgroups"
		                                                          label="${optgroup.label}">
		                                                    <option py:for="option in optgroup.options"
		                                                            selected="${bi[column.name] == option or None}"
		                                                            py:content="option"></option>
		                                                </optgroup>
		                                            </select>
		                                            <textarea py:when="column.type == 'textarea'" 
		                                                      id="col-${bi.id}-${column.name}" 
		                                                      name="col_${bi.id}_${column.name}"
		                                                      cols="${column.width}"
		                                                      rows="${column.height}"
		                                                      py:content="bi[column.name]"></textarea>
		                                            <span py:when="column.type == 'checkbox'">
		                                                <input type="checkbox" id="col-${bi.id}-${column.name}" name="col_${bi.id}_${column.name}"
		                                                       checked="${bi[column.name] == '1' and 'checked' or None}" value="1" />
		                                                <input type="hidden" name="col_${bi.id}_${column.name}" value="1" />
		                                            </span>
		                                            <py:otherwise><!--! Text input fields -->
		                                                <py:choose>
		                                                <input py:when="to_unicode(bi[column.name]).isdigit()" type="text" id="col-${bi.id}-${column.name}"
		                                                       name="col_${bi.id}_${column.name}" value="${bi[column.name]}" title="${column.name}"/>
		                                                <input py:otherwise="" type="text" id="col-${bi.id}-${column.name}"
		                                                       name="col_${bi.id}_${column.name}" value="${bi[column.name]}" title="${column.name}"/>
		                                                </py:choose>
		                                            </py:otherwise>
		                                        </py:choose>
		                                    </py:when>
		                                    <py:otherwise>
		                                        <py:choose>
		                                            <py:when test="column.rendered">
		                                            ${column.rendered()}
		                                            </py:when>
		                                            <py:otherwise>
		                                            ${bi[column.name or column]}
		                                            </py:otherwise>
		                                        </py:choose>
		                                    </py:otherwise>
		                                    </py:choose>
	                                    </py:with>
	                                    </td>
	                                </py:otherwise>
	                            </py:choose>
                            </py:for>
                        </tr>
                        </py:with>
                    </py:for>
    
				    <!--! Now the Totals row -->
				    <tr class="backlog_totals nodrop nodrag" py:if="backlog_totals and len(backlog_totals) > 0">
				        <td class="checkbox"></td>
				        <td colspan="2">Totals:</td>
				        <py:for each="col in columns" py:with="name = col.name">
				            <py:choose py:if="name not in ['id','pos','summary']">
				                <td py:when="backlog_totals[name] > 0">${backlog_totals[name]}</td>
				                <td py:otherwise="">n.a.</td>
				            </py:choose>
				        </py:for>
				    </tr>
                </tbody>
            </table>

		    <input type="hidden" name="name" value="${backlog and backlog.name}" />
		    <input type="hidden" name="bscope" value="${backlog and backlog.scope}" 
		           py:if="backlog and backlog.scope and backlog.scope != 'global'" />
		    <div class="single group buttons" py:if="may_edit_backlog">
		        Backlog
		        <py:for each="button_name in sorted(global_actions.keys())" 
		                py:with='binfo = global_actions[button_name]; button = binfo.button'>
		        <input type="submit" 
		               disabled="${not binfo.show and 'disabled' or None}"
		               name="${button_name}"
		               value="${button.label}"
		               title="${button.title}" />
		        </py:for>
		    </div>
		    <div class="multi group buttons" py:if="may_edit_backlog">
		        Tickets
		        <py:for each="act in sorted(ticket_actions.keys())"
		                py:with='binfo = ticket_actions[act]; button = binfo.button'>
		        <input type="submit" 
		               disabled="${not binfo.show and 'disabled' or None}"
		               name="${act}" 
		               value="${button.label}"
		               title="${button.title}" />
		        </py:for>
		    </div>
		    <div class="multi group buttons" py:if="may_edit_backlog">
		        Planning
		        <py:for each="act in sorted(planning_actions.keys())"
		                py:with='binfo = planning_actions[act]; button = binfo.button'>
		        <input type="submit" 
		               disabled="${not binfo.show and 'disabled' or None}"
		               name="${act}" 
		               value="${button.label}"
		               title="${button.title}" />
		        </py:for>
		    </div>
        </form>
        <py:if test="defined('contingent_widget')">${contingent_widget.display()}</py:if>
    </div>
    <div id="help">
        <strong>Note:</strong> if you want to know more see 
        <a href="${req.href('agilo-help', 'user/backlog')}">${_('Backlogs')}</a> in the Agilo help. 
    </div>
    </body>
</html>