I've got this custom button on Lead Editview that when clicked on generates (via AJAX) an invoice number and a PDF bearing the same number.
In the next step, the routine uses SOAP to loopback to Sugar and creates a Note (along with the 开发者_JAVA百科PDF as attachment).
My question is can I avoid this SOAP call and use some other internal mechanism / classes to do the same? Something along the lines of
$invoice = new Note();
$invoice->create(....);
...
Is this possible? I couldn't find any documentation anywhere... all roads seem to point to SOAP.
If your Ajax call is performing a db update/save operation, then you could look into using a after_save
logic hook.
EDIT: for eg: you could try out this code, have a look at the code in <sugar_root>/modules/Notes/Note.php
$note = new Note();
$note->modified_user_id = $current_user->id;
$note->created_by = $current_user->id;
$note->name = 'New';
$note->parent_type = "Accounts";
$note->parent_id = $bean->parent_id;
$note->description = $bean->description;
$note->save();
As far as attachment goes, it's a bit tricky. Sugar expects the attachment to be a upload_file object. Have a look at the code in <sugar_root>/modules/Notes/controller.php
the function action_save()
and <sugar_root>/include/upload_file.php
HACK: this is not the correct way but it works. With a slight modification to the code above and cunning use of the move
function , you could make the attachment work. Sugar stores the attachments in cache/upload
folder with the ID of the note created.
$note->filename = "Yourfilename.txt" //your file name goes here
$note->file_mime_type = "text/plain" // your file's mime type goes here
$new_note_id = $note->save();
move(your_file_location, cache/upload/$new_note_id)
//don't add a extension to cache/upload/$new_note_id
HTH
P.S: untested code
Do this on controller.php
foreach ( $_FILES as $file ) {
for ( $i = 0 ; $i < count( $file[ 'name' ] ) ; $i++ ) {
$fileData = file_get_contents( $file[ 'tmp_name' ][ $i ] );
$fileTmpLocation = $file[ 'tmp_name' ][ $i ];
$fileMimeType = mime_content_type( $file[$i] );
$fileInfo = array( 'name' => $file[ 'name' ][ $i ], 'data' => $fileData, 'tmpLocation' =>$fileTmpLocation, 'mimeType' => $fileMimeType );
array_push( $files, $fileInfo );
}
}
$this->guardarNotas($this->bean->id,$files);
}
And this is the function to save Notes with attachment:
private function guardarNotas($case_id,$files){
foreach($files as $file){
$noteBean = BeanFactory::newBean('Notes');
$noteBean->name = $file['name'];
$noteBean->parent_type = "Cases";
$noteBean->parent_id = $case_id;
$noteBean->filename = $file["name"];
$noteBean->file_mime_type = $file["mimeType"];
$noteBean->save();
move_uploaded_file($file["tmpLocation"], "upload/".$noteBean->id);
}
}
精彩评论