Search code examples
concrete5concrete5-5.7

Concrete5 5.7: Using file objects in a Single Page Controller


I try to attach a file object to a mail object.


I have included the following in the form of my view:

$f = new Concrete\Core\Application\Service\FileManager();
//...
echo $f->file('file', 'test', 'pls choose');

Then I submit my form back to the controller. There (BTW all other form fields arrive in the controller as expected) I do:

$files = $this->post('test');
$file = \File::getByID($files);

which should return a File object. When I do

$file = \File::getRelativePathFromID($files);

it gives me the correct path to the chosen file.

So far so good. BUT when I try to send a mail with exactly that file object attached:

$mail = Loader::helper('mail');
        $mail->setTesting(false);
        $mail->setSubject('test-subject');
        $mail->to($this->post('uEmail'));
//...
$attach = $mail->addAttachment($file);
        $attach->filename = 'tttt';
        $mail->sendMail();

the following error occurs:

call_user_func_array() expects parameter 1 to be a valid callback, class 'Concrete\Core\File\Version' does not have a method 'getPath'

which apparently comes from this class method (API):

namespace Concrete\Core\Mail;
//...
class Service {
//...
    /**
      * Add attachment to send with an email.
      *
      * Sample Code:
      * $attachment = $mailHelper->addAttachment($fileObject);
      * $attachment->filename = "CustomFilename";
      * $mailHelper->send();
      *
      * @param File $fob File to attach
      * @return StdClass Pointer to the attachment
      */
     public function addAttachment(\Concrete\Core\File\File $fob)
     {
         // @TODO make this work with the File Storage Locations

         $fv = $fob->getVersion();
         $path = $fob->getPath();
         $name = $fv->getFileName();
         //...
      }
//...
}

which apparently wants a file object as param, which I think I passed, weren't I? Why my file object becomes a FileVersion object, which, as I see by myself, hasn't got a method getPath().

My other trials so far:

$fv = $f->getApprovedVersion();
        $fv->setFile($f);
        $fv->getFile();

$fv = $f->getRecentVersion();
        $fv->setFile($f);
        $fv->getFile();

How do I get the correct file object, which I have to, maybe (??) , take out of the last/approved Version of this file?


Solution

  • This was a bug that has been fixed in the upstream, you'll have to either patch this yourself or wait until version 7.4 lands.