Editor upload with custom value

Editor upload with custom value

rpconsultantsrpconsultants Posts: 6Questions: 2Answers: 0

This server side script was working correctly until I updated to editor v2.

<?php

include( "DataTables/Editor-PHP-2.2.2/lib/DataTables.php" );
$retcmpid = 0;

use
    DataTables\Editor,
    DataTables\Editor\Field,
    DataTables\Editor\Format,
    DataTables\Editor\Join,
    DataTables\Editor\Upload,
    DataTables\Editor\Validate,
    DataTables\Editor\ValidateOptions;

Editor::inst( $db, 'ffam_competition', 'cmpid' )
    ->fields(
        Field::inst( 'ffam_competition.cmpid' ),
        Field::inst( 'ffam_competition.typeconcoursid' )
          ->options( 'core_typeconcours', 'typeconcoursid', 'typeconcourslabelid' ),
        Field::inst( 'core_typeconcours.typeconcourslabelid' ),
        Field::inst( 'core_typeconcours.typeconcoursdesc' ),
        Field::inst( 'ffam_competition.typeid' )
          ->options( 'core_type', 'typeid', 'typecode' ),
        Field::inst( 'core_type.typecode' ),
        Field::inst( 'ffam_competition.catid' )
          ->options( 'core_categorie', 'id', 'libelle' ),
        Field::inst( 'core_categorie.libelle' ),
        Field::inst( 'ffam_competition.cmplibelle' ),
        Field::inst( 'ffam_competition.color' ),
        Field::inst( 'ffam_competition.cmpdate' ),
        Field::inst( 'ffam_competition.cmpenddate' ),
        Field::inst( 'ffam_competition.cmplieu' ),
        Field::inst( 'ffam_competition.cmporganisateur' ),
        Field::inst( 'ffam_competition.cmpvolphase1' ),
        Field::inst( 'ffam_competition.cmpvolphase2' ),
        Field::inst( 'ffam_competition.cmpvolphase3' ),
        Field::inst( 'ffam_competition.cmpflagnautomatic' ),
        Field::inst( 'ffam_competition.cmpistbl' ),
        Field::inst( 'ffam_competition.cmpisoldnorm' ),
        Field::inst( 'ffam_competition.cmppromotion' ),
        //Field::inst( 'ffam_competition.cmppromprog' ),
        Field::inst( 'ffam_competition.cmppromprog' )
          ->validator(Validate::notEmpty(ValidateOptions::inst()
            ->message('Field required')
          )),
        Field::inst( 'ffam_competition.cmpisadmin' ),
        Field::inst( 'ffam_competition.cmpdisplayresultspublic' ),
        Field::inst( 'ffam_competition.cmplogoid' )
          ->setFormatter( 'Format::NullEmpty' )
          ->upload(
            Upload::inst( $_SERVER['DOCUMENT_ROOT'].'/img/logos/cmp/__NAME__' )
              ->db( 'ffam_file', 'fileid', array(
                'filename'    => Upload::DB_FILE_NAME,
                'fileext'     => Upload::DB_EXTN,
                'filesize'    => Upload::DB_FILE_SIZE,
                'web_path'    => Upload::DB_WEB_PATH,
                'system_path' => Upload::DB_SYSTEM_PATH,
                'filetype'    => 'cmp'
              ) )
              ->validator( Validate::fileSize( 5000000, 'Files must be smaller that 5 Mo' ) )
              ->validator( Validate::fileExtensions( array( 'png', 'jpg', 'jpeg', 'gif' ), "Please upload an image" ) )
          ),
        Field::inst( 'ffam_file.web_path' )
    )
    ->leftJoin( 'ffam_file', 'ffam_file.fileid', '=', 'ffam_competition.cmplogoid' )
    ->leftJoin( 'core_categorie', 'core_categorie.id', '=', 'ffam_competition.catid' )
    ->leftJoin( 'core_type', 'core_type.typeid', '=', 'ffam_competition.typeid' )
    ->leftJoin( 'core_typeconcours', 'core_typeconcours.typeconcoursid', '=', 'ffam_competition.typeconcoursid' )
    ->process( $_POST )
    ->json();
?>

The 'filetype' field which should be set to 'cmp' is set to the WEB_PATH

Answers

  • allanallan Posts: 61,744Questions: 1Answers: 10,111 Site admin

    That should work - what is happening instead? Are you getting an error, or is it just not setting or what?

    This is the piece of code in question. It sets a temporary string value in the db, and then on lines 690+ it will write in the actual value to that row that was generated.

    Allan

  • allanallan Posts: 61,744Questions: 1Answers: 10,111 Site admin

    Perhaps you could add ->debug(true) just before the ->process() call, and show me the JSON return from the server when you perform an upload action.

    Allan

  • rpconsultantsrpconsultants Posts: 6Questions: 2Answers: 0

    Here it is

    {"data":[],"files":{"ffam_file":{"20":{"fileid":"20","filename":"logo_Cadophies.jpg","fileext":"jpg","filesize":"31317","web_path":"\/img\/logos\/cmp\/logo_Cadophies.jpg","system_path":"\/Users\/rp\/github\/NotauScore.v4\/html\/img\/logos\/cmp\/logo_Cadophies.jpg","filetype":"\/Users\/rp\/github\/NotauScore.v4\/html\/img\/logos\/cmp\/logo_Cadophies.jpg"}}},"upload":{"id":"20"},"debug":["Editor PHP libraries - version 2.2.2",{"query":"INSERT INTO  `ffam_file`  ( `filename`, `fileext`, `filesize`, `web_path`, `system_path`, `filetype` ) VALUES (  :filename,  :fileext,  :filesize,  :web_path,  :system_path,  :filetype )","bindings":[{"name":":filename","value":"logo_Cadophies.jpg","type":null},{"name":":fileext","value":"jpg","type":null},{"name":":filesize","value":31317,"type":null},{"name":":web_path","value":"-","type":null},{"name":":system_path","value":"-","type":null},{"name":":filetype","value":"-","type":null}]},{"query":"UPDATE  `ffam_file` SET  `web_path` = :web_path, `system_path` = :system_path, `filetype` = :filetype WHERE `fileid` = :where_0 ","bindings":[{"name":":where_0","value":"20","type":null},{"name":":web_path","value":"\/img\/logos\/cmp\/logo_Cadophies.jpg","type":null},{"name":":system_path","value":"\/Users\/rp\/github\/NotauScore.v4\/html\/img\/logos\/cmp\/logo_Cadophies.jpg","type":null},{"name":":filetype","value":"\/Users\/rp\/github\/NotauScore.v4\/html\/img\/logos\/cmp\/logo_Cadophies.jpg","type":null}]},{"query":"SELECT  `fileid` as 'fileid', `filename` as 'filename', `fileext` as 'fileext', `filesize` as 'filesize', `web_path` as 'web_path', `system_path` as 'system_path', `filetype` as 'filetype' FROM  `ffam_file` WHERE `fileid` IN (:wherein1) ","bindings":[{"name":":wherein1","value":"20","type":null}]}]}
    
  • rpconsultantsrpconsultants Posts: 6Questions: 2Answers: 0

    The ffam_file.filetype field is set to 'system_path' instead of the constant 'cmp'

  • allanallan Posts: 61,744Questions: 1Answers: 10,111 Site admin

    Thanks.

    It appears to be writing the path name to the Web path:

                    {
                        "name": ":filetype",
                        "value": "\/Users\/rp\/github\/NotauScore.v4\/html\/img\/logos\/cmp\/logo_Cadophies.jpg",
                        "type": null
                    }
    

    I hadn't clicked that this is exactly what you said in your initial post. Apologies.

    That is indeed a bug and I've just committed a fix. It will be in Editor 2.3.1, or you can just patch your own copy of Upload.php with that fix directly, to get it immediately.

    Allan

  • rpconsultantsrpconsultants Posts: 6Questions: 2Answers: 0

    Thanks a lot Allan. Will do

Sign In or Register to comment.