<?xml version="1.0" encoding="UTF-8"?>
<ajxp_plugin enabled="false" label="CONF_MESSAGE[Power FS]" description="CONF_MESSAGE[This set of extensions assume that you have an administration access to your server: ability to tweak the webserver and the php configuration, ability to access the command line, etc.]" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:noNamespaceSchemaLocation="file:../core.ajaxplorer/ajxp_registry.xsd">
    <class_definition classname="PowerFSController" filename="plugins/action.powerfs/class.PowerFSController.php"/>
    <client_settings>
        <resources>
            <i18n namespace="powerfs" path="plugins/action.powerfs/i18n"/>
        </resources>
    </client_settings>
    <server_settings>
        <global_param name="ZIP_PATH" type="string" label="CONF_MESSAGE[Zip path]"
                      description="CONF_MESSAGE[Path of the zip utilitary on the command line]" default="zip"/>
    </server_settings>
    <registry_contributions>
        <actions>
            <!-- Override access.fs compression -->
            <action name="compress">
                <processing>
                    <serverCallback methodName="switchAction" restParams="/file+" checkParams="false" sdkMethodName="compress" developerComment="Compress one or many files into a Zip archive. May be disabled for remote-access workspaces (like FTP).">
                        <input_param description="User selection" name="nodes" type="AJXP_NODE[]" mandatory="true"/>
                        <input_param description="Force new archive name. If not set, the parent folder name is used" name="archive_name" type="string"/>
                    </serverCallback>
                </processing>
            </action>
            <action name="precompress">
                <rightsContext noUser="true" userLogged="only" read="true" write="false" adminOnly=""/>
                <processing>
                    <serverCallback methodName="switchAction"/>
                </processing>
            </action>
            <action name="postcompress_download">
                <rightsContext noUser="true" userLogged="only" read="true" write="false" adminOnly=""/>
                <processing>
                    <serverCallback methodName="switchAction"/>
                </processing>
            </action>
            <action name="monitor_compression">
                <processing>
                    <serverCallback methodName="switchAction"/>
                </processing>
            </action>
            <!-- Override download client callback -->
            <action name="download">
                <processing>
                    <clientCallback prepareModal="true"><![CDATA[
                        var userSelection = ajaxplorer.getUserSelection();
                        if((userSelection.isUnique() && !userSelection.hasDir()) || multipleFilesDownloadEnabled)
                        {
                            if(window.gaTrackEvent){
                                var fileNames = userSelection.getFileNames();
                                for(var i=0; i<fileNames.length;i++){
                                    window.gaTrackEvent("Data", "Download", fileNames[i]);
                                }
                            }
                            var agent = navigator.userAgent;
                            if(agent && (agent.indexOf('iPhone')!=-1||agent.indexOf('iPod')!=-1||agent.indexOf('iPad')!=-1||agent.indexOf('iOs')!=-1)){
                                var downloadUrl = ajxpServerAccessPath+'&get_action=download';
                                downloadUrl = userSelection.updateFormOrUrl(null,downloadUrl);
                                document.location.href=downloadUrl;
                            }else{
                                if( !userSelection.isUnique() || userSelection.hasDir() ){

                                    var zipName = getBaseName(userSelection.getContextNode().getPath());
                                    if(zipName == "") zipName = "Archive";
                                    var index=1;
                                    var buff = zipName;
                                    while(userSelection.fileNameExists(zipName + ".zip")){
                                        zipName = buff + "-" + index; index ++ ;
                                    }

                                    var conn = new Connexion();
                                    conn.addParameter("get_action", "precompress");
                                    conn.addParameter("archive_name", zipName+".zip");
                                    conn.addParameter("on_end", "postcompress_download");
                                    var selected = userSelection.getFileNames();
                                    var dir = userSelection.getContextNode().getPath();
                                    for(var i=0;i<selected.length;i++){
                                        conn.addParameter("file_"+i, selected[i]);
                                        dir = getRepName(selected[i]);
                                    }
                                    conn.addParameter("dir", dir);
                                    conn.onComplete = function(transport){
                                        this.parseXmlMessage(transport.responseXML);
                                    }.bind(pydio.getController()) ;
                                    conn.sendAsync();

                                }else{

                                    PydioApi.getClient().downloadSelection(userSelection, $('download_form'));

                                }
                            }
                        }
                        else
                        {
                            var loadFunc = function(oForm){
                                var dObject = oForm.getElementsBySelector('div[id="multiple_download_container"]')[0];
                                var downloader = new MultiDownloader(dObject, ajxpServerAccessPath+'&action=download&file=');
                                downloader.triggerEnd = function(){hideLightBox()};
                                var fileNames = userSelection.getFileNames();
                                for(var i=0; i<fileNames.length;i++)
                                {
                                    downloader.addListRow(fileNames[i]);
                                }
                            };
                            var closeFunc = function(){
                                hideLightBox();
                                return false;
                            };
                            modal.showDialogForm('Download Multiple', 'multi_download_form', loadFunc, closeFunc, null, true);
                        }
                        ]]>
                    </clientCallback>
                </processing>
            </action>
        </actions>
    </registry_contributions>
    <dependencies>
       <activePlugin pluginName="access.fs"/>
    </dependencies>
</ajxp_plugin>
