Hi,
After being sick and busy with other things I checked again and found out
that the xredirect does not work so the NameManager is not executed.
Even the following in the application sheet does not do any redirect when
pressing Save and View button of an application document:
```
<div class="hidden">
<input type="hidden" name="xredirect"
value="$doc.space" />
</div>
```
The value of the xredirect field must be an URL, see my first reply. And
you need XWiki 8.4+
Hope this helps,
Marius
Could you please help me how proceed further?
Thanks, Dennis
2017-03-01 15:51 GMT+01:00 D R <rir.ceg(a)gmail.com>om>:
Hi Marius,
thanks, I'm one step further now, at least the NameManager now does
renaming in general (it renamed itself until I added an #if clause to not
do so).
I wanted to implement it step by step so I didn't add the redirect yet.
But the sheet is not executed when editing application entries.
Meanwhile I have the following.
Test App.Code.Test AppSheet:
```
[...]
#set( $nameManRef = $services.model.createDocumentReference('', ['Test
App', 'Code'], 'Test App NameManager') )
#set( $nameMan = $xwiki.getDocument($nameManRef) )
<div class="hidden">
<input type="hidden" name="xredirect"
value="$doc.getURL('view',
"sheet=${escapetool.url($nameMan)}")" />
</div>
{{/html}}
{{/velocity}}
```
Test App.Code.Test App NameManager:
```
{{velocity}}
#if ( $doc.name != 'Test App NameManager' )
#set ($renameRequest = $services.refactoring.createRenameRequest($doc.
documentReference,
'Now RENAMED'))
#set ($discard = $renameRequest.setAutoRedirect(false))
$services.refactoring.rename($renameRequest)).join()
#end
{{/velocity}}
```
Thanks again,
Dennis
2017-03-01 10:30 GMT+01:00 Marius Dumitru Florea <
mariusdumitru.florea(a)xwiki.com>gt;:
> On Tue, Feb 28, 2017 at 3:03 PM, D R <rir.ceg(a)gmail.com> wrote:
>
> > Hi Marius,
> >
> > the refactoring approach sounds great but there seems to be something
I
> do
> > wrong because the renaming doesn't work. Maybe I misunderstood the
term
> > "current document" but I'm
not sure.
> >
> > I created "Test App.Code.Test App NameManager" with the following
> content:
> >
> > ```
> > {{velocity}}
> > #set ($renameRequest =
> >
>
>
> > $services.refactoring.createRenameRequest($doc.fullName, 'Now
> RENAMED'))
> >
>
> As per
>
http://extensions.xwiki.org/xwiki/bin/view/Extension/Refacto
> ring+Module#HScriptService
> the first parameter passed to createRenameRequest must be an
> EntityReference. In your case you can pass:
>
> * either the reference of the current document, i.e.
> $doc.documentReference
> , if the document is terminal (its name != 'WebHome')
> * or the reference of the current space, i.e.
> $doc.documentReference.parent
> , if the document is not terminal (its name is 'WebHome')
>
>
> > #set ($discard = $renameRequest.setAutoRedirect(false))
> > $services.refactoring.rename($renameRequest)).join()
> > {{/velocity}}
> >
> > ```
> >
> > Then I put your DIV at the end of my app sheet:
> >
> > ```
> > {{velocity}}
> > {{html wiki="true"}}
> > #set ($discard = $doc.use('Test App.Code.Test AppClass'))
> > (% class="xform" %)
> > (((
> > [FORM CONTENT]
> > )))
> >
> > <div class="hidden">
> > <input type="hidden" name="xredirect"
value="$doc.getURL('view',
> > "sheet=${escapetool.url('Test App.Code.Test App
NameManager')}")" />
> > </div>
> >
> > {{/html}}
> > {{/velocity}}
> >
> > ```
> >
> > Then I opened up one of the application entries, edited it and hit
"Save
> > and view" but the name didn't
change.
> >
>
> I don't see any redirect in "Test App NameManager" so it's normal
you
stay
> on the same page, but do you have the sheet
parameter in the query
string?
> i.e. is the "Test App NameManager"
sheet executed?
>
>
> >
> > Could you please tell me what I did wrong.
> >
> > Thanks in advance,
> > Dennis
> >
> > 2017-02-28 11:38 GMT+01:00 Marius Dumitru Florea <
> > mariusdumitru.florea(a)xwiki.com>gt;:
> >
> > > Hi Dennis,
> > >
> > > A relatively simple solution is the following:
> > >
> > > (1) modify the application sheet to include the following HTML at
the
> > end:
> > >
> > > <div class="hidden">
> > > <input type="hidden" name="xredirect"
value="$doc.getURL('view',
> > > 'sheet=Path.To.NameManager')" />
> > > </div>
> > >
> > > The result is that whenever you create a new application entry or
when
> > you
> > > edit an existing application entry, after you click "Save and
view"
> you
> > > will be taken to the view mode with ?sheet=Path.To.NameManager in
the
> URL
> > > query string, which means the Path.To.NameManager sheet will be
> applied
> > to
> > > the saved document.
> > >
> > > (2) Create the Path.To.NameManager page that determines the right
name
> > for
> > > the current document (based on its fields) and renames the current
> > document
> > > if needed ( using the refactoring API
> > >
http://extensions.xwiki.org/xwiki/bin/view/Extension/Refacto
> ring+Module
> > ),
> > > finally redirecting to the view mode of the renamed document.
> > >
> > > The problem with this solution is that it doesn't work if you use
> Save &
> > > Continue and then Cancel (I think, to be checked).
> > >
> > > Another option, more complex, is to write an event listener that
> catches
> > > the document save event and renames the document if needed based on
> its
> > > fields. The bad part with this is that the user is still not
> redirected
> > > when using Save & Continue.
> > >
> > > Hope this helps,
> > > Marius
> > >
> > > On Mon, Feb 27, 2017 at 3:52 PM, D R <rir.ceg(a)gmail.com> wrote:
> > >
> > > > Hi,
> > > >
> > > > In an XWiki Application I created I want to make sure documents
> created
> > > > within it get named in a specific pattern from the fields filled
> inside
> > > > them.
> > > >
> > > > Currently I use a custom button to redirect the user to a new
> document
> > > with
> > > > a dummy name so he doesn't need to fill the name (that should get
> > > > overwritten anyway on saving) but only the relevant fields.
> > > >
> > > > Something like
> > > > #set ( $createDocLink = $xwiki.getURL($document, 'inline',
> > > > "template=${escapetool.url($urlTemplate)}&parent=${
> > > >
escapetool.url($urlParent)}&title=${escapetool.url($urlTitle)}")
> > > > )
> > > >
> > > > This leads to a new document as defined in the application sheet
> with
> > the
> > > > name 'New document'.
> > > >
> > > > Now I want the user to fill 3 fields ('freetext1',
'freetext2',
> > > > 'dropdown1'), click 'Save & View' or a custom
button. Then the
> document
> > > > should be saved as '<freetext1> - <freetext2>
(<dropdown1>)' and
the
> > user
> > > > should be redirected to that page in view mode.
> > > >
> > > > I've tried different approaches including a temporary creation
page
with
> > the 3 fields as HTML input fields (can't use the applications
dropdown),
> > event listeners (the redirect after saving doesn't work) ...
> >
> > Additionally the same must be done when a page is edited (one of
the 3
> > fields is changed).
> >
> > Please help me find a sane approach before I bite the table.
> >
> > Regs, Dennis
> >
>