Editor upload error on conditional insert

Editor upload error on conditional insert

crush123crush123 Posts: 417Questions: 126Answers: 18

I have an editor instance with upload which is working ok.
I have changed the ajax source so that a row is inserted into a related table ONLY if a post value is not empty.
If I do this, the upload action throws an error.

Fatal error: Uncaught exception 'Exception' with message 'Unknown Editor action: upload' in D:\vhosts\newcfci\DataTables-1.10.5\extensions\Editor-PHP-1.4.2\php\Editor\Editor.php:105 Stack trace: #0 D:\vhosts\newcfci\ajax\companies.php(27): DataTables\Editor::action(Array) #1 {main} thrown in D:\vhosts\newcfci\DataTables-1.10.5\extensions\Editor-PHP-1.4.2\php\Editor\Editor.php on line 105

Original working code snippet

$editor = Editor::inst( $db, 'companies', 'CompanyID' )//table name and PKey(defaults to ID)

->field(
    Field::inst( 'companies.CompanyID' ),
    Field::inst( 'companies.BusinessTypeID' ),
    Field::inst( 'companies.CompanyDescription' ),
    Field::inst( 'companies.CompanyURL' ),
    Field::inst( 'companies.CompanyLogo' )
        ->upload(
        Upload::inst( function ( $file, $id ) {
        move_uploaded_file( $file['tmp_name'], $_SERVER['DOCUMENT_ROOT'].'/images/logos/'.$file['name'] );
        return $file['name'];
    } )
        ->allowedExtensions( array( 'png', 'jpg', 'bmp' ), "Please upload an image file (.pmg, .jpg, .bmp)" )
        ->validator( function ( $file ) {
              if ( $file['size'] >= 500000 ) {
                  return "Files must be smaller than 500Kb";
              }
              return null;
        } )
    ),
    Field::inst( 'companies.ListOrder' ),
    Field::inst( 'members.CompanyID' ),
    Field::inst( 'members.MembershipExpiresEnd' ),
    Field::inst( 'members.Complimentary' ),
    Field::inst( 'members.PremiumMember' ),
    Field::inst( 'refgrouptype.GroupTypeDescription' ),
    Field::inst( 'vw_contacts_count.CountOfContacts' )
        ->set( Field::SET_NONE )
)
->leftJoin('refgrouptype','refgrouptype.GroupTypeID', '=','companies.BusinessTypeID')
->leftJoin('members','members.CompanyID', '=','companies.CompanyID')
->leftJoin( 'vw_contacts_count', 'vw_contacts_count.CompanyID', '=', 'companies.CompanyID' );

 $data = $editor
->process($_POST)
->data();

New code snippet

$editor = Editor::inst( $db, 'companies', 'CompanyID' );//table name and PKey(defaults to ID)

  $memberscompanyidfield = Field::inst( 'members.CompanyID' )
  ->set( Field::SET_NONE );
  $memberscomplimentaryfield = Field::inst( 'members.Complimentary' )
  ->set( Field::SET_NONE );
  $memberspremiumfield = Field::inst( 'members.PremiumMember' )
  ->set( Field::SET_NONE );
  $membersexpiresfield = Field::inst( 'members.MembershipExpiresEnd' )
  ->set( Field::SET_NONE );

  if ( (Editor::action( $_POST ) === Editor::ACTION_CREATE) || (Editor::action( $_POST ) === Editor::ACTION_EDIT)) {
     $membershipexpiresvalue = $_POST['data']['members']['MembershipExpiresEnd'];
     if ($membershipexpiresvalue != '') {
            $memberscompanyidfield
                ->set( Field::SET_BOTH );
            $memberscomplimentaryfield
                ->set( Field::SET_BOTH );
            $memberspremiumfield
                ->set( Field::SET_BOTH );
            $membersexpiresfield
                ->set( Field::SET_BOTH );
     }
  }

$editor->field(
    Field::inst( 'companies.CompanyID' ),
    Field::inst( 'companies.BusinessTypeID' ),
    Field::inst( 'companies.CompanyDescription' ),
    Field::inst( 'companies.CompanyURL' ),
    Field::inst( 'companies.CompanyLogo' )
        ->upload(
        Upload::inst( function ( $file, $id ) {
        move_uploaded_file( $file['tmp_name'], $_SERVER['DOCUMENT_ROOT'].'/images/logos/'.$file['name'] );
        return $file['name'];
    } )
        ->allowedExtensions( array( 'png', 'jpg', 'bmp' ), "Please upload an image file (.pmg, .jpg, .bmp)" )
        ->validator( function ( $file ) {
              if ( $file['size'] >= 500000 ) {
                  return "Files must be smaller than 500Kb";
              }
              return null;
        } )
    ),
    Field::inst( 'companies.ListOrder' ),
    $memberscompanyidfield,
    $membersexpiresfield,
    $memberscomplimentaryfield,
    $memberspremiumfield,
    Field::inst( 'refgrouptype.GroupTypeDescription' ),
    Field::inst( 'vw_contacts_count.CountOfContacts' )
        ->set( Field::SET_NONE )
)
->leftJoin('refgrouptype','refgrouptype.GroupTypeID', '=','companies.BusinessTypeID')
->leftJoin('members','members.CompanyID', '=','companies.CompanyID')
->leftJoin( 'vw_contacts_count', 'vw_contacts_count.CompanyID', '=', 'companies.CompanyID' );

 $data = $editor
->process($_POST)
->data();

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    Hi,

    I'm afraid that this is a bug in the Editor.php file - specifically in the action() method:

    In the method action you will find a case statement which should have:

                case 'upload':
                    return self::ACTION_UPLOAD;
    

    added to it. Just after the remove statement would be fine.

    Also at the top of the class this should be added:

        /** Request type - upload */
        const ACTION_UPLOAD = 'upload';
    

    Apologies for the error!

    Regards,
    Allan

  • crush123crush123 Posts: 417Questions: 126Answers: 18
    edited May 2015

    Thanks Allan,

    Will check it out and feed back.

    Have a good weekend

    UPDATE

    The patch to Editor.php fixes the problem.

    Thx

This discussion has been closed.