Radicore Forum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » RADICORE » How To » Email attachments
Re: Email attachments [message #3038 is a reply to message #3037] Tue, 17 July 2012 08:10 Go to previous messageGo to previous message
AJM is currently offline  AJM
Messages: 2361
Registered: April 2006
Location: Surrey, UK
Senior Member
The button is made to appear in the screen with the following:
$this->fieldspec['add_attachment'] = array('type' => 'string',
                                           'control' => 'button',
                                           'task_id' => 'py_email_attachment(fileupload)');

The fileupload task has thge following code in the _cm_post_fileUpload() method:
$array['filepath'] = $filename;
$array['filename'] = $this->filename;
$array['filesize'] = $filesize;

This causes those values to be passed back to the calling form where they appear in the _cm_popupReturn() method where I have the following code:
if ($return_from == 'py_email_attachment(fileupload)') {
    if (!empty($select_array['add_attachment'])) {
	$filename = $select_array['add_attachment']['filename'];
	$filepath = $select_array['add_attachment']['filepath'];
	$filesize = $select_array['add_attachment']['filesize'];
        $fieldarray['add_attachment'] = null;
        // save these details for the _cm_postInsertRecord() method
        $this->attachments[$filename] = $filepath .' [size='.$filesize.']';
        // rebuild the list of attachments
        $list = '';
        foreach ($this->attachments as $key => $value) {
            $list .= $key .', ';
	} // foreach
	$fieldarray['attachments'] = rtrim($list, ', ');
    } // if
} // if

These values are processed in the _cm_postInsertRecord() method with the following code:
if (!empty($this->attachments) AND is_array($this->attachments)) {
    $attachments = $this->attachments;

} elseif (!empty($rowdata['attachments']) AND is_array($rowdata['attachments'])) {
    $attachments = $rowdata['attachments'];
} // if

if (!empty($attachments)) {
    // $attachments has 2 possible formats:
    // 1) associative - 'filename = filepath'
    // 2) indexed - [idx]['filename'] = '....'
    //              [idx]['filesize'] = '....'
    //              [idx]['filebody'] = '....' or [idx]['filepath'] = '....'
    $array1      = $attachments;
    $attachments = array();
    foreach ($array1 as $key => $value) {
        if (is_array($value)) {
            $attachments[] = $value;
        } else {
            // convert from associative to indexed
            $filename = $key;
            $filepath = $value;
            if (preg_match('/(?<=\[size=).*(?=\])/', $filepath, $regs)) {
                $filesize = $regs[0];  // extract file size
                // remove '[size=...]' from the value
                $filepath = preg_replace('/\[size=.*\]/', '', $filepath);
            } else {
                $filesize = null;
            } // if
            $attachments[] = array('filename' => $filename,
                                   'filepath' => $filepath,
                                   'filesize' => $filesize);
        } // if
    } // foreach
} // if

if (!empty($attachments)) {
    // add each attachment to a separate table
    $dbobject =& RDCsingleton::getInstance('email_attachment');
    $dbobject->audit_logging = $this->audit_logging;
    foreach ($attachments as $idx => $filedata) {
        $insert['email_id'] = $rowdata['email_id'];
        $insert['filename'] = $filedata['filename'];
        $insert['filesize'] = $filedata['filesize'];
        if (!empty($filedata['filebody'])) {
            $insert['filebody'] = $filedata['filebody'];
        } else {
            $insert['filepath'] = $filedata['filepath'];
        } // if
        $insert = $dbobject->insertRecord($insert);
        if ($dbobject->errors) {
            $this->errors = array_merge($this->errors, $dbobject->errors);
            return $rowdata;
        } // if
    } // foreach
    unset($dbobject);
} // if

I hope this helps.


 
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Use SQL REPLACE function in UPDATE SET
Next Topic: Error reporting on UPD3 screen
Goto Forum:
  


Current Time: Fri Aug 30 12:09:52 EDT 2024

Total time taken to generate the page: 0.00799 seconds