Hi Vincent,

Hi Vincent,

just wanted to let you know that my app is now working :-)

But now the cleanup progress starts and some things are really messy coded.

So I could need some tipps, because I recognized that you are really fit with 
jQuery and OC.

Here you can see the current state: http://imgbox.com/ZFjqBuyh . It locks all 
other fileactions and
marks the line in a color. But i will add more options later.

If you have some ideas to shorten the code please feel free to answer.

Here is my filetree:
+apps
-+ajax
  - l10n.php (grab translation with ajax)
  - workin2gether.php (the core)
-+appinfo
 -app.php
 -info.xml
-+css
  -styles.css
-+img
  -pattern.png
  -w2g.svg
  -whoshare.png
-+js
  -workin2gether.js
-+l10n
  -<somelanguages>
-+lock (where the locks are stored)

Source: workin2gether.js

var text = "";
var lockedtext = "";
var lockstate = "";

$(document).ready(function(){

        text = translate("filelock");
        lockedtext = translate("File is locked");
        lockstate = translate("Status: locked");

    if (typeof FileActions !== 'undefined' && $('#dir').length>0) {


            
FileActions.register('file',text,OC.PERMISSION_READ,function(){return 
OC.imagePath('files_w2g','w2g.svg')},function(filename){
                getState(filename,"false");
            });

            
FileActions.register('dir',text,OC.PERMISSION_READ,function(){return 
OC.imagePath('files_w2g','w2g.svg')},function(filename){
                getState(filename,"false");
            });

                        $('#fileList').on('fileActionsReady',function(filename){
                                $('#fileList tr td.filename a.name 
span.nametext').each(function() {
                                        var $tr = $(this);
                                        getState($tr.html(),"true");
                                });
                        });
    }

});

function disable_control(filename)
{

        $('#fileList tr').each(function() {

                var $tr = $(this);

                var $_tr = $tr.html().replace(/^\s+|\s+$/g, '').replace('<span 
class="extension">','').replace('</span>','');

                    
if($_tr.indexOf(lockedtext)!=-1||$_tr.indexOf(lockstate)!=-1)
                    {
                                if($_tr.indexOf(filename)!=-1)
                                        
$tr.find('a.action[data-action!='+text+']').addClass('locked');
                                        
$tr.find('a.name').addClass('statelock');
                                        
$tr.find('td.filesize').addClass('statelock');
                                        
$tr.find('td.date').addClass('statelock');

                        }
        });
}

function enable_control(filename)
{
        $('#fileList tr').each(function() {

                var $tr = $(this);

                var $_tr = $tr.html().replace(/^\s+|\s+$/g, '').replace('<span 
class="extension">','').replace('</span>','');

                    if($_tr.indexOf(lockedtext)==-1 && 
$_tr.indexOf(lockstate)==-1)
                    {
                                if($_tr.indexOf(filename)!=-1)
                                        
$tr.find('a.action[data-action!='+text+']').removeClass('locked');
                                        
$tr.find('a.name').removeClass('statelock');
                                        
$tr.find('td.filesize').removeClass('statelock');
                                        
$tr.find('td.date').removeClass('statelock');
                        }
        });
}

function translate(text)
{
        $.ajax({
        url: OC.filePath('files_w2g','ajax','l10n.php'),
        type: "post",
        data: { rawtext: text},
                async: false,
        success: function(data){text = data;},
    });
        return text;
}

function getState(filename,_safe)
{
        filename = filename.replace(/^\s+|\s+$/g, '').replace('<span 
class="extension">','').replace('</span>','');

    oc_dir = $('#dir').val();
        filename = filename.replace(/ /g, "%20");
        oc_path = oc_dir +'/'+filename;


        $.ajax({
        url: OC.filePath('files_w2g','ajax','workin2gether.php'),
        type: "post",
        data: { path: oc_path, safe: _safe},
        success: function(data){postmode(filename,data)},
    });

}

function postmode(filename,data)
{
                filename = filename.replace(/%20/g,' ');

                var html = '<img class="svg" 
src="'+OC.imagePath('files_w2g','w2g.svg')+'"></img> '+data;
                
$('tr').filterAttr('data-file',filename).find('td.filename').find('a.name').find('span.fileactions').find("a.action").filterAttr('data-action',text).html(html);

                if(data==lockedtext||data==lockstate)
                        disable_control(filename);
                else
                        enable_control(filename);

}


Source: workin2gether.php
<?php

//Init translations
$l = \OCP\Util::getL10N('files_w2g');

//Requirements check
OCP\JSON::checkLoggedIn();
OCP\JSON::checkAppEnabled('files_w2g');


//Default path for lock files

$root = $_SERVER['DOCUMENT_ROOT'];

$apath = $root.OCP\Util::linkTo('files_w2g', 'lock')."/";

if(!@file_exists($root.OCP\Util::linkTo('files_w2g')."/lock"))
        @mkdir($root.OCP\Util::linkTo('files_w2g')."/lock", 0777, true);

$storage = "D:/";

//Vars
$path = stripslashes($_POST['path']) ;
@$safe = @$_POST['safe'];
$ppath = lockname($path);


if(cleanPath($path)=="/Shared"){ echo "Forbidden"; return 1; }

if(substr(cleanPath($path),0,7)=="/Shared")
{
        // /Shared/...
        $relpath = explode('/',substr(cleanPath($path),7,strlen($path)-7));
        $relative_path = '/'.$relpath[count($relpath)-1];

        $backcount = 0;
        $dbg = $relpath[count($relpath)-1-0];
        do
        {
                $query = OCP\DB::prepare("SELECT X.parent, X.id, X.uid_owner, 
Y.path FROM *PREFIX*share X INNER JOIN *PREFIX*filecache Y ON X.file_source = 
Y.fileid where X.share_with = ? and X.file_target = ? LIMIT 1");
                $result = 
$query->execute(array(OCP\USER::getUser(),'/'.$relpath[count($relpath)-1-$backcount]))->fetchAll();
                $backcount+=1;
        }while(count($result)<1 && $backcount<count($relpath));

        if ($backcount>0) $backcount-=1;




        $user = $result[0]['uid_owner'];
        $postpath = $result[0]['path'];
        //get the original share user name

        $old = ""; $new = "-";
        while ($new!="")
        {
                $old = $new;
                $query = OCP\DB::prepare("SELECT parent, uid_owner, id FROM 
*PREFIX*share where id = ? LIMIT 1");
                $result = 
$query->execute(array($result[0]['parent']))->fetchAll();
                $new = $result[0]['uid_owner'];
        }
        if($old!="-") $user = $old;


        $realpath = $storage.$user.'$'.$postpath;

        for($i=$backcount;$i>0;$i-=1)
        {
                $realpath .= '/'.$relpath[count($relpath)-$i];
        }



}
else $realpath = $storage.OCP\USER::getUser()."/files".cleanPath($path);

//lockfile name
$lock = $apath.lockname($realpath);

if (file_exists($lock))
{
        if(@$safe=="false")
        {
                @unlink($lock);
                echo $l->t("File not locked");
        }
        else echo $l->t("Status: locked");
}
else
{
        @date_default_timezone_set('Europe/Berlin');

        if(@$safe=="false")
        {
                $h = fopen($lock, "w");
                fwrite($h,OCP\User::getDisplayName()." ".date(DATE_RFC822)." 
".$path);
                fclose($h);
                echo $l->t("File is locked");
        }
        else echo $l->t("Status: not locked");
}



function lockname($path)
{
        //Replace all '/' with $ for a filename for the lock file
        $ppath = str_replace("/","$",cleanPath($path))."$";

        $ppath = str_replace("Shared","",$ppath);
        $ppath = str_replace(":","#",$ppath);

        //Remove double dollar char when exist
        if ($ppath[0] == "$" and $ppath[1] == "$") $ppath = substr($ppath,1);

        return $ppath;

}

function cleanPath($path) {

        $path = rtrim($path, "/");
        $path = urldecode($path);
                return preg_replace('{(/)\1+}', "/", $path);
}


Source: l10n.php
<?php
$l = \OCP\Util::getL10N('files_w2g');
if($l->t($_POST['rawtext'])!="") echo $l->t($_POST['rawtext']); else echo 
$_POST['rawtext'];


Source: styles.css
#workin2gether p {
        float: left;
        padding-top: 0.8em;
        padding-left: 1.2em;
}

#workin2gether img  {
        float: left;
        width: 48px;
        height: 48px;
        background: transparent url('apps/files_w2g/img/pattern.png');
        border: 1px solid #D8D8D8;
        padding: 5px;
}
a.action.locked{
display:none !important;
}

a.name.statelock a.action{
color:#eee;
opacity:1 !important;
}
a.name.statelock a.action:hover, a.name.statelock span.extension{
color:#fff;
}

.statelock, .statelock span.modified{
background-color:#008887;
color:#fff !important;
}

Source: sample language file (de.php)
<?php
$TRANSLATIONS = array(
"File not locked" => "Datei nicht gesperrt",
"File is locked" => "Datei ist gesperrt",
"Status: locked" => "Status: gesperrt",
"Status: not locked" => "Status: nicht gesperrt",
"filelock" => "Dateisperre"
);

Example for Lockname:
D#$Patrick$files$_locked_folder$

Cheers,

Patrick


BITO-Lagertechnik Bittmann GmbH

Obertor 29
55590 Meisenheim
Germany

Tel: +49 (0) 6753 122 0
Fax: +49 (0) 6753 122 399

E-Mail: i...@bito.de
http://www.bito.de

Gesch?ftsf?hrer: Werner Magin, Winfried Schmuck | HRB 2704 Bad Kreuznach | 
Gesellschafter: Fritz Bittmann Holding GmbH | USt-ID: DE 811 202 181


*************************************************************************************
Dieses E-Mail ist nur f?r den Empf?nger bestimmt, an den es gerichtet ist und 
kann vertrauliches bzw. unter das Berufsgeheimnis fallendes Material enthalten. 
Jegliche darin enthaltene Ansicht oder Meinungs?u?erung ist die des Autors und 
stellt nicht notwendigerweise die Ansicht oder Meinung von BITO-Lagertechnik 
Bittmann GmbH dar. Sind Sie nicht der Empf?nger und haben diese E-Mail 
irrt?mlich erhalten, sind jegliche Verwendung, Ver?ffentlichung, Weiterleitung, 
Abschrift oder jeglicher Druck dieser E-Mail strengstens untersagt. Jede 
ausgehende E-Mail wird von uns mit h?chster Sorgfalt auf Viren gepr?ft. Jedoch 
?bernehmen weder BITO-Lagertechnik Bittmann GmbH noch der Absender (Patrick 
Hoffmann) die Haftung f?r Viren; es obliegt Ihrer Verantwortung, die E-Mail und 
deren Anh?nge auf Viren zu pr?fen.

This email is exclusively meant for the addressee and may contain confidential 
information or information which can be classified as professional secret. Any 
view or opinion stated in this email is that of the author and does not 
necessarily represent the view or the opinion of BITO-Lagertechnik Bittmann 
GmbH. If you are not the addressee and if this email has been transmitted to 
you by mistake, you may not make use of, publish, transmit, reproduce or print 
the information contained therein for whatever purpose. We take every 
reasonable care to check all out-bound emails for viruses. However, neither 
BITO-Lagertechnik Bittmann GmbH nor the sender (Patrick Hoffmann) can be held 
liable for the occurrence of viruses and any consequential damages. It is 
therefore the addressee's sole responsibility to check incoming emails and 
attachments for viruses.
*************************************************************************************
Anh?nge:
Versand am 04.03.2014 13:31 von Patrick Hoffmann

_______________________________________________
Devel mailing list
Devel@owncloud.org
http://mailman.owncloud.org/mailman/listinfo/devel

_______________________________________________
Devel mailing list
Devel@owncloud.org
http://mailman.owncloud.org/mailman/listinfo/devel

Reply via email to