How to call the filebrowser in a plugin?

Third Party Plugins to CMSimple - how to install, use and create plugins

Moderator: Tata

svasti
Posts: 1651
Joined: Wed Dec 17, 2008 5:08 pm

How to call the filebrowser in a plugin?

Post by svasti » Wed Apr 01, 2015 1:15 pm

XH-WIKI wrote:File Browsers
To be written …
:(
I'd like to make the filebrowser callable from the quoteoftheday admin backend to put images into quotes.
... but how?

cmb
Posts: 14225
Joined: Tue Jun 21, 2011 11:04 am
Location: Bingen, RLP, DE
Contact:

Re: How to call the filebrowser in a plugin?

Post by cmb » Wed Apr 01, 2015 2:11 pm

svasti wrote:I'd like to make the filebrowser callable from the quoteoftheday admin backend to put images into quotes.
... but how?
Basically, that is simple: (a) display the filebrowser, (b) do something with the returned value (a file path). In practise, however, it's rather complex, so for now let's concentrate on the default filebrowser.

The first thing is to have some JS that opens the filebrowser (usually in a new window) with the right parameters. See the relevant code of Codeeditor_XH, for an example. For some quick tries, just enter the URL into the browser's address line directly to find out what the parameters are doing (particularly, the `type` parameter). In your case, you can probably do with a mostly fix URL (only the prefix and the base URL have to be adjusted for second languages; use CMSIMPLE_BASE or $sn): http://example.com/plugins/filebrowser/ ... type=image.

Note, that the base URL of the default filebrowser will change with XH 1.7. Instead of $pth['folder']['plugins'].'filebrowser/editorbrowser.php' it'll probably be $pth['folder']['base'].'?filebrowser=editorbrowser'.

The final thing is to register a callback function that will be called by the filebrowser, when a file is selected. You have to put a respective script into plugins/filebrowser/editorhooks/quoteoftheday/script.php (this script is automatically included by the filebrowser, where the `editor` parameter gives the respective folder name). This script is supposed to define the function setLink(url), where url is the URL of the file the user selected. For a first proof of concept just copy the editorhook of codeeditor/, and do

Code: Select all

function setLink(url) {
    alert(url);
}
For external filebrowsers it works different; I'll try to explain later.
Christoph M. Becker – Plugins for CMSimple_XH

svasti
Posts: 1651
Joined: Wed Dec 17, 2008 5:08 pm

Re: How to call the filebrowser in a plugin?

Post by svasti » Wed Apr 01, 2015 6:59 pm

Uff, uff, uff, aber hat schließlich geklappt. Danke.

svasti
Posts: 1651
Joined: Wed Dec 17, 2008 5:08 pm

Re: How to call the filebrowser in a plugin?

Post by svasti » Sat Jul 04, 2015 10:18 pm

Klappt aber nicht in 1.7
cmb wrote:Note, that the base URL of the default filebrowser will change with XH 1.7. Instead of $pth['folder']['plugins'].'filebrowser/editorbrowser.php' it'll probably be $pth['folder']['base'].'?filebrowser=editorbrowser'.
Das gibt auch keinen Sinn, weil dann ? zweimal vorkommen würde.
Also wie ruft mans auf?
$pth['folder']['base'].'?filebrowser=editorbrowser??????[??was kommt hierhin?]???????quoteoftheday&prefix='.....etc

Holger
Site Admin
Posts: 3470
Joined: Mon May 19, 2008 7:10 pm
Location: Hessen, Germany

Re: How to call the filebrowser in a plugin?

Post by Holger » Sat Jul 04, 2015 11:08 pm

svasti wrote:$pth['folder']['base'].'?filebrowser=editorbrowser??????[??was kommt hierhin?]???????quoteoftheday&prefix='.....etc
Es sollte IMO so sein (ungetestet):

Code: Select all

$pth['folder']['base'].'?filebrowser=editorbrowser&editor=quoteoftheday&prefix=./&base=./&type=image
Die Idee dahinter ist, dass alle Requests direkt an index.php gehen, wo dann automatisch alle Pfade, Variablen usw. verfügbar sind. Bisher gehen die Requests an editorbrowser.php und alle nöigen Variablen müssen per URL bzw. Session dort erst bekannt gemacht werden.

cmb
Posts: 14225
Joined: Tue Jun 21, 2011 11:04 am
Location: Bingen, RLP, DE
Contact:

Re: How to call the filebrowser in a plugin?

Post by cmb » Sun Jul 05, 2015 11:31 am

Holger wrote:
svasti wrote:$pth['folder']['base'].'?filebrowser=editorbrowser??????[??was kommt hierhin?]???????quoteoftheday&prefix='.....etc
Es sollte IMO so sein (ungetestet):

Code: Select all

$pth['folder']['base'].'?filebrowser=editorbrowser&editor=quoteoftheday&prefix=./&base=./&type=image
Ja, so sollte es passen. Im Zweifel einfach mal vergleichen, wie die URLs aussehen, die der Editor beim Bildeinfügen aufruft. (Ich weiß übrigens nicht, ob der base Parameter überhaupt gebraucht wird.)
Holger wrote:Die Idee dahinter ist, dass alle Requests direkt an index.php gehen, wo dann automatisch alle Pfade, Variablen usw. verfügbar sind. Bisher gehen die Requests an editorbrowser.php und alle nöigen Variablen müssen per URL bzw. Session dort erst bekannt gemacht werden.
Genau. Und daher sollte sich das Script nun an die "Regeln" halten, die auch sonst für Plugins gelten (Bezeichner mit Prefix, usw.), um Kollisionen mit anderen Plugins und dem Core zu vermeiden. Z.B. konnte man für 1.6 im Script noch eine Funktion namens a() definieren, was unter 1.7 nicht mehr möglich ist, da diese bereits vom Core definiert wird.
Christoph M. Becker – Plugins for CMSimple_XH

svasti
Posts: 1651
Joined: Wed Dec 17, 2008 5:08 pm

Re: How to call the filebrowser in a plugin?

Post by svasti » Sun Jul 05, 2015 3:28 pm

Ja, danke, der Filebrowser wird jetzt aufgerufen. :)

Code: Select all

$editorbrowser = version_compare(CMSIMPLE_XH_VERSION, 1.7, '<') && version_compare(CMSIMPLE_XH_VERSION, 1.5, '>')
    ? $pth['folder']['plugins'].'filebrowser/editorbrowser.php?editor'
    : $pth['folder']['base'].'?filebrowser=editorbrowser&editor';
...
function filebrowser (type) {
    window.open("'.$editorbrowser
    . '=quoteoftheday&prefix='.$pth['folder']['base'].'&base=./&type=" + type, "",
    "toolbar=no,location=no,status=no,menubar=no," +
    "scrollbars=yes,resizable=yes,width=640,height=480");
}';
Allerdings passiert nichts mehr, wenn man auf eine Bildadresse klickt. :(
Bei 1.6.x wurde die URL dann übergeben... Es wird zwar der Händchencursor gezeigt, wenn die Maus über der Bildadresse ist, aber ein Klick bewirkt nichts.... normalerweise müsste dann die URL übergeben werden und der Filebrowser geschlossen werden.

cmb
Posts: 14225
Joined: Tue Jun 21, 2011 11:04 am
Location: Bingen, RLP, DE
Contact:

Re: How to call the filebrowser in a plugin?

Post by cmb » Sun Jul 05, 2015 4:21 pm

svasti wrote:Allerdings passiert nichts mehr, wenn man auf eine Bildadresse klickt. :(
Bei 1.6.x wurde die URL dann übergeben... Es wird zwar der Händchencursor gezeigt, wenn die Maus über der Bildadresse ist, aber ein Klick bewirkt nichts.... normalerweise müsste dann die URL übergeben werden und der Filebrowser geschlossen werden.
Ich vermute, dass da ein JavaScript-Fehler auftritt. Schau mal in die Konsole des Filebrowser-Fensters.
Christoph M. Becker – Plugins for CMSimple_XH

svasti
Posts: 1651
Joined: Wed Dec 17, 2008 5:08 pm

Re: How to call the filebrowser in a plugin?

Post by svasti » Sun Jul 05, 2015 6:25 pm

Konsole wrote:TypeError: window.setLink is not a function ..... filebrowser.js:256:0
Wie ich gerade durch Dateivergleich feststelle, ist die filebrowser.js für 1.7 ja völlig umgekrempelt worden und doppelt so groß geworden. :cry:

cmb
Posts: 14225
Joined: Tue Jun 21, 2011 11:04 am
Location: Bingen, RLP, DE
Contact:

Re: How to call the filebrowser in a plugin?

Post by cmb » Sun Jul 05, 2015 8:41 pm

svasti wrote:Wie ich gerade durch Dateivergleich feststelle, ist die filebrowser.js für 1.7 ja völlig umgekrempelt worden und doppelt so groß geworden. :cry:
Ja, plugins/filebrowser/js/filebrowser.js wurde ordentlich umgekrempelt (r1535). Aber ist das wirklich ein Problem? Die Fehlermeldung zeigt an, dass die Funktion setLink() nicht definiert ist – befindet sich denn in plugins/filebrowser/editorhooks auch der quoteoftheday/ Ordner mit der Datei script.php?
Christoph M. Becker – Plugins for CMSimple_XH

Post Reply