UploadMany - assessing data with an If statement

UploadMany - assessing data with an If statement

hamlet1964hamlet1964 Posts: 43Questions: 8Answers: 1

I'm trying to assess individual files returned from uploadMany to render either a PDF icon or an image, as appropriate. This works with a simple upload type. Any insight on how I craft this to work with an array?

from the HTML Editor:

{
                label: "files:",
                name: "files[].id",
                type: "uploadMany",
                display: function ( fileId, counter ) {
                    if (fileId !== null) {
                    var ext = editor.file('files', fileId).filetype.toLowerCase();
                    if (ext == "pdf") {
                        return '<a href ="' + editor.file('files', fileId).web_path + '" target=_blank><i class ="fa fa-file-pdf-o fa-3x"></i>' + '</center></a>';
                        }
                    
                    if ((ext == "png") || (ext == "jpg") || (ext == "gif")) {
                    
                    return fileId ?
                        '<img src="'+editor.file( 'files', fileId ).web_path+'" class="img-circle" width="60" height="60"/>' :
                        null;
                }
                }},
                clearText: "Clear",
                noFileText: 'No images'
            }

And from the PHP:


Editor::inst( $db, 'stuff' ) ->fields( Field::inst( 'stuff.date' ), Field::inst( 'stuff.notes' ), Field::inst( 'stuff.category' ) ->options( Options::inst() ->table( 'categories' ) ->value( 'id' ) ->label( 'category' ) ), Field::inst( 'categories.category'), Field::inst( 'stuff.person' ) ->options( Options::inst() ->table( 'persons' ) ->value( 'id' ) ->label( 'person' ) ), Field::inst( 'persons.person' ) ) ->join( Mjoin::inst( 'files' ) ->link( 'stuff.id', 'stuff_files.stuff_id' ) ->link( 'files.id', 'stuff_files.file_id' ) ->fields( Field::inst( 'id' ) ->upload( Upload::inst( $_SERVER['DOCUMENT_ROOT'].'/upload_test/__ID__.__EXTN__' ) ->db( 'files', 'id', array( 'filename' => Upload::DB_FILE_NAME, 'filesize' => Upload::DB_FILE_SIZE, 'web_path' => Upload::DB_WEB_PATH, 'system_path' => Upload::DB_SYSTEM_PATH ) ) ->validator( Validate::fileSize( 500000, 'Files must be smaller that 500K' ) ) ->validator( Validate::fileExtensions( array( 'png', 'jpg', 'jpeg', 'gif' ), "Please upload an image" ) ) ) ) ) ->leftJoin( 'categories', 'categories.id', '=', 'stuff.category' ) ->leftJoin( 'persons', 'persons.id', '=', 'stuff.person' ) ->process( $_POST ) ->json();

This question has accepted answers - jump to:

Answers

  • hamlet1964hamlet1964 Posts: 43Questions: 8Answers: 1
    Answer ✓

    Figured it out. No If Statement necessary. That, plus I had some silly errors in my PHP. Now fixed and appended for everyone's benefit.

    HTML Editor remains unchanged. From the PHP:

    Editor::inst( $db, 'stuff' )
        ->fields(
            Field::inst( 'stuff.date' ),
            Field::inst( 'stuff.notes' ),
            Field::inst( 'stuff.category' )
            ->options( Options::inst()
                    ->table( 'categories' )
                    ->value( 'id' )
                    ->label( 'category' )
                ),
            Field::inst( 'categories.category'),
            Field::inst( 'stuff.person' )
                ->options( Options::inst()
                    ->table( 'persons' )
                    ->value( 'id' )
                    ->label( 'person' )
                ),
            Field::inst( 'persons.person' )
        )
        ->join(
            Mjoin::inst( 'files' )
                ->link( 'stuff.id', 'stuff_files.stuff_id' )
                ->link( 'files.id', 'stuff_files.file_id' )
                ->fields(
                    Field::inst( 'id' )
                        ->setFormatter( 'Format::ifEmpty', null )
                        ->upload( Upload::inst( $_SERVER['DOCUMENT_ROOT'].'/upload_test/__ID__.__EXTN__' )
                            ->db( 'files', 'id', array(
                                'filename'    => Upload::DB_FILE_NAME,
                                'filesize'    => Upload::DB_FILE_SIZE,
                                'web_path'    => Upload::DB_WEB_PATH,
                                'system_path' => Upload::DB_SYSTEM_PATH,
                                'filetype'    => Upload::DB_EXTN
                            ) )
                            ->validator( Validate::fileSize( 2000000, 'Files must be smaller that 2M' ) )
                            ->validator( Validate::fileExtensions( array( 'png', 'jpg', 'jpeg', 'gif', 'pdf' ), "Please upload an image" ) )
                        )
                )
        )
        ->leftJoin( 'categories', 'categories.id', '=', 'stuff.category' )
        ->leftJoin( 'persons', 'persons.id', '=', 'stuff.person' )
        ->process( $_POST )
        ->json();
    
    

    Apologies for a premature posting.

  • allanallan Posts: 63,158Questions: 1Answers: 10,405 Site admin
    Answer ✓

    No worries! Thanks for posting back and good to hear you've got it working now.

    Allan

This discussion has been closed.