<tal:comment replace="nothing">
    This macro displays the content of the application portlet.
</tal:comment>
<metal:portlet define-macro="portlet"
   tal:define="queryUrl           python: '%s/ui/query' % tool.absolute_url();
               toolUrl            tool/absolute_url;
               currentSearch      req/search|nothing;
               currentClass       req/className|nothing;
               contextObj         tool/getPublishedObject;
               rootClasses        tool/getRootClasses">

 <div class="portletContent" tal:condition="python: contextObj and contextObj.mayNavigate()">
  <div class="portletTitle" tal:define="parent contextObj/getParent">
    <span tal:replace="contextObj/Title"></span>
     <a tal:condition="python: parent" tal:attributes="href parent/absolute_url">
       <img tal:attributes="src string: $appUrl/ui/gotoSource.png"/>
     </a>
  </div>
  <metal:phases use-macro="here/ui/portlet/macros/phases"/>
 </div>

 <tal:comment replace="nothing">One section for every searchable root class.</tal:comment>
 <tal:section repeat="rootClass python: [rc for rc in rootClasses if tool.userMaySearch(rc)]">

  <tal:comment replace="nothing">A separator if required</tal:comment>
  <div class="portletSep" tal:define="nb repeat/rootClass/number"
                          tal:condition="python: (nb == 1 and contextObj) or (nb != 1)"></div>  

  <div class="portletContent">
   <tal:comment replace="nothing">Section title, with action icons</tal:comment>
   <a tal:attributes="href python: '%s?className=%s' % (queryUrl, rootClass);
                      class python:test(not currentSearch and (currentClass==rootClass), 'portletCurrent', '')"
      tal:content="structure python: _(rootClass + '_plural')">
   </a>
   <span tal:define="addPermission python: '%s: Add %s' % (appName, rootClass);
                     userMayAdd python: user.has_permission(addPermission, appFolder);
                     createMeans python: tool.getCreateMeans(rootClass)">
    <tal:comment replace="nothing">Create a new object from a web form</tal:comment>
    <a tal:condition="python: ('form' in createMeans) and userMayAdd"
       tal:attributes="href python: '%s/do?action=Create&className=%s' % (toolUrl, rootClass);
                       title python: _('query_create')">
     <img tal:attributes="src string: $appUrl/ui/plus.png"/>
    </a>
    <tal:comment replace="nothing">Create (a) new object(s) by importing data</tal:comment>
    <a tal:condition="python: ('import' in createMeans) and userMayAdd"
       tal:attributes="href python: '%s/ui/import?className=%s' % (toolUrl, rootClass);
                       title python: _('query_import')">
     <img tal:attributes="src string: $appUrl/ui/import.png"/>
    </a>
    <tal:comment replace="nothing">Search objects of this type</tal:comment>
    <a tal:define="showSearch python: tool.getAttr('enableAdvancedSearchFor%s' % rootClass)"
       tal:condition="showSearch"
       tal:attributes="href python: '%s/ui/search?className=%s' % (toolUrl, rootClass);
                       title python: _('search_objects')">
     <img tal:attributes="src string: $appUrl/ui/search.gif"/>
    </a>
   </span>
   <tal:comment replace="nothing">Searches for this content type.</tal:comment>
    <tal:searchOrGroup repeat="searchOrGroup python: tool.getSearches(rootClass)">
      <tal:group condition="searchOrGroup/isGroup">
      <tal:expanded define="group searchOrGroup;
                            expanded python: request.get(group['labelId'], 'collapsed') == 'expanded'">
        <tal:comment replace="nothing">Group name</tal:comment>
        <div class="portletGroup">
          <img style="cursor:pointer; margin-right: 3px"
               tal:attributes="id python: '%s_img' % group['labelId'];
                               src python:test(expanded, 'ui/collapse.gif', 'ui/expand.gif');
                               onClick python:'toggleCookie(\'%s\')' % group['labelId'];
                               align dleft"/>
          <span tal:replace="group/label"/>
        </div>
        <tal:comment replace="nothing">Group searches</tal:comment>
        <div tal:attributes="id group/labelId;
                              style python:test(expanded, 'display:block', 'display:none')">
          <div class="portletSearch" tal:repeat="search group/searches">
            <a tal:attributes="href python: '%s?className=%s&search=%s' % (queryUrl, rootClass, search['name']);
                               title search/descr;
                               class python: test(search['name'] == currentSearch, 'portletCurrent', '');"
               tal:content="structure search/label"></a>
          </div>
        </div>
      </tal:expanded>
      </tal:group>
      <dt tal:define="search searchOrGroup" tal:condition="not: searchOrGroup/isGroup"
          class="portletAppyItem portletSearch">

        <a tal:attributes="href python: '%s?className=%s&search=%s' % (queryUrl, rootClass, search['name']);
                     title search/descr;
                     class python: test(search['name'] == currentSearch, 'portletCurrent', '');"
           tal:content="structure search/label"></a>
      </dt>
   </tal:searchOrGroup>
  </tal:section>
</metal:portlet>

<tal:comment replace="nothing">
   This macro displays, within the portlet, the navigation tree for the
   currently shown object, made of phases and contained pages.
</tal:comment>
<table metal:define-macro="phases"
       tal:define="phases contextObj/getAppyPhases;
                   singlePhase python: len(phases) == 1;
                   page python: req.get('page', 'main')">
  <tal:phase repeat="phase phases">
    <tal:comment replace="nothing">The box containing phase-related information</tal:comment>
    <tr tal:define="singlePage python: len(phase['pages']) == 1">
      <td tal:define="label python:'%s_phase_%s' % (contextObj.meta_type, phase['name']);
                      status phase/phaseStatus;
                      phaseCss python: (status == 'Current') and ' phaseSelected' or '';"
          tal:attributes="class python: not singlePhase and 'phase%s' % phaseCss or ''">
       <tal:comment replace="nothing">The title of the phase</tal:comment>
       <div class="portletGroup" tal:condition="python: not singlePhase and not singlePage"
            tal:content="structure python: _(label)">
       </div>
       <tal:comment replace="nothing">The page(s) within the phase</tal:comment>
       <tal:page repeat="aPage phase/pages">
        <tal:comment replace="nothing">First line: page name and icons</tal:comment>
        <div tal:condition="python: not (singlePhase and singlePage)"
             tal:attributes="class python: test(aPage == page, 'portletCurrent portletPage', 'portletPage')">
          <a tal:attributes="href python: contextObj.getUrl(page=aPage)"
             tal:content="structure python: _('%s_page_%s' % (contextObj.meta_type, aPage))">
          </a>
          <a tal:condition="python: contextObj.allows('Modify portal content') and phase['pagesInfo'][aPage]['showOnEdit']"
             tal:attributes="href python: contextObj.getUrl(mode='edit', page=aPage)">
           <img title="Edit" tal:attributes="src string: $appUrl/ui/edit.gif"/>
          </a>
        </div>
        <tal:comment replace="nothing">Next lines: links</tal:comment>
        <tal:links define="links python: phase['pagesInfo'][aPage].get('links')" tal:condition="links">
         <div tal:repeat="link links">
           <a tal:content="link/title" tal:attributes="href link/url"></a>
         </div>
        </tal:links>
       </tal:page>
      </td>
    </tr>
    <tal:comment replace="nothing">The down arrow pointing to the next phase (if any)</tal:comment>
    <tr tal:condition="python: phase['name'] != phases[-1]['name']">
      <td>&nbsp;&nbsp;<img tal:attributes="src string: $appUrl/ui/nextPhase.png"/></td>
    </tr>
  </tal:phase>
</table>
