#
# Wiki
#
# {{action:SE::w}}
# {{wpId:SC0}}
# {{wpIdPrevious:S0}}
# {{sectionId:S0}}
# {{sectionIdEnd:S0}}
# {{history:CE}}
# {{print:SE}}

dbIndex={{indexWiki:Y}}
form={{form:SE}}

# Add variables from S- to R-store
{
  sql = SELECT '{{wpId:SC0:alnumx}}' AS _wpId
              , '{{history:CE:alnumx}}' AS _history
              , '{{print:SE}}' AS _print
}

# If no id is in the R-store, the id of the root page will be used
{
  sql = SELECT id AS _wpId
  		  FROM WikiPage
        WHERE id = '{{wpId:R0}}'

  altsql = SELECT id AS _wpId
  		     FROM WikiPage
           WHERE pageSlug = '{{pageSlug:T}}'
             AND ISNULL(wpIdCurrent)
             AND ISNULL(wpIdParent)
           ORDER BY modified DESC
           LIMIT 1
}

# Executes the function checkWikiAccess() from the PHP-script
# Checks the privileges of the user
{
  sql = SELECT 'F:typo3conf/ext/qfq/wikiScript.php|call:checkWikiAccess|arg:wpId={{wpId:R0}}' AS _script
}

# Function returns the user privileges in V-Store and render mode gets set accordingly
{
  sql = SELECT CASE
                WHEN '{{wikiAccess:VE}}' = 'ro' THEN '5'
                WHEN '{{wikiAccess:VE}}' = 'rw' THEN '0'
				        WHEN '{{wikiAccess:VE}}' = 'off' THEN '5'
              END AS _renderMode
              , IF('{{wikiAccess:VE}}' = 'rw', '', '|r:3') AS _readonlyFlag
}

# Grant access to developers
{
  sql = SELECT IF('{{beUser:T}}' != '', '0', '{{renderMode:R0}}') AS _renderMode
               , IF('{{beUser:T}}' != '', '', '{{readonlyFlag:RE}}') AS _readonlyFlag
}

# Prepare PDF Export
{
sql = SELECT CONCAT('p:{{pageSlug:T}}&wpId={{wpId:R0}}&print=true'
                    , '&--disable-external-links'
                    , '&--margin-top=20'
                   	, '&--margin-bottom=20'
                    , '&--margin-left=20'
                    , '&--margin-right=20'
                    , '&--header-left={{pageTitle:TE}}'
                   	, '&--header-font-name=Open Sans'
                    , '&--header-spacing=5'
                    , '&--footer-left=[date]'
                    , '&--footer-right=[page]/[topage]'
                    , '&--footer-font-name=Open Sans'
                    , '&--footer-spacing=5'
                    , '&--footer-font-size=10'
                    , '&type=2'
                    , '&_sip=1'
                    , '|g:_blank|b:0|t:<span class="glyphicon glyphicon-print"></span> Print|d:Print wiki') AS '_pdf|pdfDownload|_hide'
}

# Dropdown for "Settings", "History", "Print" and "Add new page"
{
  sql = SELECT CONCAT('z'
                      , '||p:{{pageSlug:T}}&form=wikiPage&r=', id, '|s|b:0|G:glyphicon glyphicon-cog|t:Settings|r:{{renderMode:RE}}'
                      , '||p:{{pageSlug:T}}&wpId={{wpId:R0}}&history=true|b:0|G:glyphicon glyphicon-backward|t:History'
                      , '||{{pdfDownload:RE}}'
                      , '||p:{{pageSlug:T}}&form=wikiPage&r=0'
                        ,'&wpIdParent=', id
                        , '&roUser=', roUser
                        , '&roGroup=', roGroup
                        , '&roPublic=', roPublic
                        , '&rwUser=', rwUser
                        , '&rwGroup=', rwGroup
                        , '&rwPublic=', rwPublic
                        , '&pageLock=', pageLock
                        , '|s|b:0|G:glyphicon glyphicon-plus|t:Add new page|r:{{renderMode:RE}}'
              ) AS _link
        FROM WikiPage
        WHERE id = '{{wpId:R0}}'
          AND '{{history:RE}}' != 'true'
          AND '{{print:RE}}' != 'true'
          AND ('{{wikiAccess:VE}}' != 'off'
            OR '{{beUser:T}}' != '')

  # If no wiki page is found, a new one can be created
  altsql = SELECT '<div style="float: right;">', 'p:{{pageSlug:T}}&form=wikiPage&r=0|s|b|N' AS _link, '</div>'
           FROM DUAL
           WHERE '{{history:RE}}' != 'true'
             AND '{{beUser:T}}' != ''
             AND '{{wpId:R0}}' = 0

  rbeg = <div style="float: right;">
  rend = </div>
}

# Executes function renderWiki() from the PHP-script
# Parses the content of the wiki page and returns it with edit links, replaced macros, etc.
{
  sql = SELECT 'F:typo3conf/ext/qfq/wikiScript.php|call:renderWiki|arg:wpId={{wpId:R0}}&renderMode={{renderMode:RE}}&printMode={{print:RE}}' AS _script
        FROM DUAL
        WHERE '{{history:RE}}' != 'true'
          AND ('{{wikiAccess:VE}}' != 'off'
            OR '{{beUser:T}}' != '')

  # User does not have the privileges to view the wiki page
  altsql = SELECT '<p>Access denied. You don\'t have permissions to view this page.</p>'
           FROM DUAL
           WHERE '{{wpId:R0}}' != 0
             AND '{{history:RE}}' != 'true'
}

# Style for container containing the files
{
  sql = SELECT IF('{{print:RE}}' = 'true', 'show', 'none') AS _styleContainer
}

# Continue only when not in "History"
{
  sql = SELECT ''
        FROM DUAL
        WHERE '{{history:RE}}' != 'true'
          AND '{{wpId:R0}}' != 0

  # Prepare link and container to toggle files
  {
    sql = SELECT CONCAT('</br>'
                        , '<a class="wiki-files-link" style="cursor:pointer;"'
                        , 'onclick="$(\'#wiki-files-chevron-{{wpId:R0}}\').toggleClass(\'glyphicon-chevron-down glyphicon-chevron-right\');'
                        , '$(\'#wiki-files-container-{{wpId:R0}}\').fadeToggle(\'fast\');">'
                        , '<span class="wiki-files-chevron glyphicon glyphicon-chevron-right" id="wiki-files-chevron-{{wpId:R0}}"></span>'
                        , 'Files ('
                        , COUNT(id)
                        , ')'
                        , '</a>'
         			          , '<div class="wiki-files-container" style="display:{{styleContainer:RE}};" id="wiki-files-container-{{wpId:R0}}">'
                )
		FROM WikiAttachment
    WHERE wpId = '{{wpId:R0}}'
      AND pathFileName != ''

    stail = </div>

    # Files
    {
      sql = SELECT CONCAT('p:{{pageSlug:T}}&form=wikiUpload&r=', id, '|s|b|E') AS _link
                  , CONCAT('F:', pathFileName
                          , '|t:', QLEFT(SUBSTRING_INDEX(pathFileName, '/', -1), 30)
                          , '|d:', SUBSTRING_INDEX(pathFileName, '/', -1)
                          , '|o:', SUBSTRING_INDEX(pathFileName, '/', -1)
                          , '|s|b|g:_blank|G:glyphicon glyphicon-paperclip|M:file'
                  ) AS _link
                  , QIFEMPTY(feUser, 'anonymous')
                  , modified
                  , CONCAT('U:table=WikiAttachment&r=', id, '|q:Do you want to delete <i>', SUBSTRING_INDEX(pathFileName, '/', -1), '</i> ?') AS _paged
      FROM WikiAttachment
      WHERE wpId = '{{wpId:R0}}'
        AND pathFileName != ''
        AND '{{history:RE}}' != 'true'
      ORDER BY modified ASC

      shead = <table class="table qfq-table-100">
                <thead>
                  <tr>
                    <th>{{'p:{{pageSlug:T}}&form=wikiUpload&r=0&wpId={{wpId:R0}}|s|b|N{{readonlyFlag:RE}}' AS _link}}</th>
                    <th>File</th>
                    <th>User</th>
                    <th>Date</th>
                    <th></th>
                  </tr>
                </thead>
                <tbody>
      stail =   </tbody>
              </table>
      fbeg = <td>
      fend = </td>
      rbeg = <tr>
      rend = </tr>
    }
  }
}

# History of the wiki page
{
  sql = SELECT created, author
        FROM WikiPage
        WHERE IFNULL(wpIdCurrent, id) = '{{wpId:R0}}'
          AND pageSlug = '{{pageSlug:T}}'
          AND '{{history:RE}}' = 'true'
          ORDER BY created DESC

  head = {{'p:{{pageSlug:T}}&wpId={{wpId:R0}}|b|t:Back|G:glyphicon glyphicon-arrow-left' AS _link}}
          <table class="table qfq-table-50"><thead><tr><th>Updated</th><th>Author</th></thead><tbody>
  tail = </tbody></table>
  fbeg = <td>
  fend = </td>
  rbeg = <tr>
  rend = </tr>
}