Restructure compose save as draft functionality and implement it as an ajax function

This commit is contained in:
Hadi Nategh 2014-09-22 12:53:27 +00:00
parent b02c771f4a
commit 6cd5e61979
3 changed files with 173 additions and 88 deletions

View File

@ -386,74 +386,7 @@ class mail_compose
egw_framework::message(lang('Message send failed: %1',$message),'error');// maybe error is more appropriate
}
}
if ($_content['button']['saveAsDraft']||$_content['button']['saveAsDraftAndPrint'])
{
$buttonClicked = $suppressSigOnTop = true;
$savedOK = true;
try
{
$_content['isDraft'] = 1;
$previouslyDrafted = $_content['lastDrafted'];
// save as draft
$folder = $this->mail_bo->getDraftFolder();
$this->mail_bo->reopen($folder);
$status = $this->mail_bo->getFolderStatus($folder);
//error_log(__METHOD__.__LINE__.array2string(array('Folder'=>$folder,'Status'=>$status)));
$uidNext = $status['uidnext']; // we may need that, if the server does not return messageUIDs of saved/appended messages
$_content['body'] = ($_content['body'] ? $_content['body'] : $_content['mail_'.($_content['mimeType'] == 'html'?'html':'plain').'text']);
$messageUid = $this->saveAsDraft($_content,$folder); // folder may change
if (!$messageUid) {
//try to reopen the mail from session data
throw new egw_exception_wrong_userinput(lang("Error: Could not save Message as Draft")." ".lang("Trying to recover from session data"));
}
// saving as draft, does not mean closing the message
$messageUid = ($messageUid===true ? $uidNext : $messageUid);
//error_log(__METHOD__.__LINE__.' (re)open drafted message with new UID: '.$messageUid.'/'.gettype($messageUid).' in folder:'.$folder);
if ($this->mail_bo->getMessageHeader($messageUid, '',false, false, $folder))
{
$draft_id = mail_ui::createRowID($folder, $messageUid);
//error_log(__METHOD__.__LINE__.' (re)open drafted message with new UID: '.$draft_id.'/'.$previouslyDrafted.' in folder:'.$folder);
if (isset($previouslyDrafted) && $previouslyDrafted!=$draft_id)
{
$dhA = mail_ui::splitRowID($previouslyDrafted);
$duid = $dhA['msgUID'];
$dmailbox = $dhA['folder'];
try
{
//error_log(__METHOD__.__LINE__."->".print_r($duid,true).' folder:'.$dmailbox.' Method:'.'remove_immediately');
$this->mail_bo->deleteMessages($duid,$dmailbox,'remove_immediately');
}
catch (egw_exception $e)
{
$error = str_replace('"',"'",$e->getMessage());
error_log(__METHOD__.__LINE__.$error);
}
}
$_content['lastDrafted'] = $draft_id;
//$draftContent = $this->bocompose->getDraftData($this->mail_bo->icServer, $folder, $messageUid);
//$this->compose($draftContent,null,'to',true);
//return true;
}
}
catch (egw_exception_wrong_userinput $e)
{
$error = str_replace('"',"'",$e->getMessage());
error_log(__METHOD__.__LINE__.$error);
$savedOK = false;
}
//error_log(__METHOD__.__LINE__.' :'.$draft_id.'->'.$savedOK);
if ($savedOK)
{
egw_framework::message(lang('Message saved successfully.'),'mail');
$response = egw_json_response::get();
if (isset($previouslyDrafted) && $previouslyDrafted!=$draft_id) $response->call('opener.egw_refresh',lang('Message saved successfully.'),'mail',$previouslyDrafted,'delete');
$response->call('opener.egw_refresh',lang('Message saved successfully.'),'mail',$draft_id,'add');
if ($_content['button']['saveAsDraftAndPrint'])
{
$response->call('app.mail.mail_compose_print',"mail::" .$draft_id);
}
}
}
if ($activeProfile != $composeProfile) $this->changeProfile($activeProfile);
$insertSigOnTop = false;
$content = (is_array($_content)?$_content:array());
@ -1322,7 +1255,7 @@ class mail_compose
}
//error_log(__METHOD__.__LINE__.array2string($content));
$etpl->exec('mail.mail_compose.compose',$content,$sel_options,$readonlys,$preserv,2);
$etpl->exec('mail.mail_compose.compose',$content,$sel_options,array(),$preserv,2);
}
/**
@ -2426,6 +2359,88 @@ class mail_compose
$mail_bo->closeConnection();
}
/**
* Save compose mail as draft
*
* @param array $content content sent from client-side
*/
public function ajax_saveAsDraft ($content)
{
$response = egw_json_response::get();
$success = true;
// check if default account is changed then we need to change profile
if (!empty($content['serverID']) && $content['serverID'] != $this->mail_bo->profileID)
{
$this->changeProfile($content['serverID']);
}
$formData = array_merge($content, array(
'isDrafted' => 1,
'body' => $content['mail_'.($content['mimeType']?'htmltext':'plaintext')],
'mimeType' => $content['mimeType']?'html':'plain' // checkbox has only true|false value
));
//Saving draft procedure
try
{
$folder = $this->mail_bo->getDraftFolder();
$this->mail_bo->reopen($folder);
$status = $this->mail_bo->getFolderStatus($folder);
if (($messageUid = $this->saveAsDraft($formData,$folder)))
{
// saving as draft, does not mean closing the message
$messageUid = ($messageUid===true ? $status['uidnext'] : $messageUid);
if (is_array($this->mail_bo->getMessageHeader($messageUid, '',false, false, $folder)))
{
$draft_id = mail_ui::createRowID($folder, $messageUid);
if ($content['lastDrafted'] != $draft_id && isset($content['lastDrafted']))
{
$dhA = mail_ui::splitRowID($content['lastDrafted']);
$duid = $dhA['msgUID'];
$dmailbox = $dhA['folder'];
try
{
$this->mail_bo->deleteMessages($duid,$dmailbox,'remove_immediately');
}
catch (egw_exception $e)
{
$msg = str_replace('"',"'",$e->getMessage());
$success = false;
error_log(__METHOD__.__LINE__.$msg);
}
}
}
}
else
{
throw new egw_exception_wrong_userinput(lang("Error: Could not save Message as Draft"));
}
}
catch (egw_exception_wrong_userinput $e)
{
$msg = str_replace('"',"'",$e->getMessage());
error_log(__METHOD__.__LINE__.$msg);
$success = false;
}
if ($success) $msg = lang('Message saved successfully.');
// Include new information to json respose, because we need them in client-side callback
$response->data(array(
'draftedId' => $draft_id,
'message' => $msg,
'success' => $success
));
}
/**
* Save message as draft to specific folder
*
* @param type $_formData content
* @param type $savingDestination destination folder
* @return boolean return messageUID| false due to an error
*/
function saveAsDraft($_formData, &$savingDestination='')
{
$mail_bo = $this->mail_bo;

View File

@ -3139,26 +3139,96 @@ app.classes.mail = AppJS.extend(
/**
* Save as Draft (VFS)
* -handel both actions save as draft and save as draft and print
*
* @param {egw object} _egw
* @param {widget object} _widget
* @param {window object} _window
* @param {string} _action autosaving trigger action
*/
saveAsDraft: function(_egw, _widget, _window)
saveAsDraft: function(_egw, _widget, _action)
{
this.et2_obj.submit();
//this.et2_obj.submit();
var content = this.et2.getArrayMgr('content').data;
if (_widget )
{
var action = _action == 'autosaving'?_action: _widget.id;
}
var widgets = ['from','to','cc','bcc','subject','folder','replyto','mailaccount', 'mail_htmltext', 'mail_plaintext', 'lastDrafted'];
var widget = {};
for (var index in widgets)
{
widget = this.et2.getWidgetById(widgets[index]);
if (widget)
{
content[widgets[index]] = widget.get_value();
}
}
var self = this;
if (content)
{
this.egw.json('mail.mail_compose.ajax_saveAsDraft',[content],function(_data){
self.savingDraft_response(_data,action);
}).sendRequest(true);
}
},
/**
* Save as Draf and pring (VFS)
* Set content of drafted message with new information sent back from server
* This function would be used as callback of send request to ajax_saveAsDraft.
*
* @param {egw object} _egw
* @param {widget object} _widget
* @param {window object} _window
* @param {object} _responseData response data sent back from server by ajax_saveAsDraft function.
* the object conatins below items:
* -draftedId: new drafted id created by server
* -message: resault message
* -success: true if saving was successful otherwise false
*
* @param {string} _action action is the element which caused saving draft, it could be as such:
* -button[saveAsDraft]
* -button[saveAsDraftAndPrint]
* -autosaving
*/
saveAsDraftAndPrint: function(_egw, _widget, _window)
savingDraft_response: function(_responseData, _action)
{
this.et2_obj.submit();
//Make sure there's a response from server otherwise shoot an error message
if (jQuery.isEmptyObject(_responseData))
{
this.egw.message('Could not saved the message. Besause, the response from server failed.', 'error');
return false;
}
if (_responseData.success)
{
var content = this.et2.getArrayMgr('content');
var lastDrafted = this.et2.getWidgetById('lastDrafted');
if (content)
{
var prevDraftedId = content.data.lastDrafted;
content.data.lastDrafted = _responseData.draftedId;
this.et2.setArrayMgr('content', content);
lastDrafted.set_value(_responseData.draftedId);
if (prevDraftedId)
{
opener.egw_refresh(_responseData.message,'mail', prevDraftedId, 'delete');
}
this.egw.refresh(_responseData.message,'mail',_responseData.draftedId,'add');
switch (_action)
{
case 'button[saveAsDraftAndPrint]':
this.mail_compose_print('mail::'+_responseData.draftedId);
this.egw.message(_responseData.message, 'add');
break;
case 'autosaving':
//Any sort of thing if it's an autosaving action
default:
this.egw.message(_responseData.message, 'add');
}
}
}
else
{
this.egw.message(_responseData.message, 'error');
}
},
/**

View File

@ -10,7 +10,7 @@
</description>
<description class="email-button-group save">
<buttononly class="et2_button ui-button" label="Save as Draft" value="saveAsDraft" id="button[saveAsDraft]" image="fileexport" onclick="app.mail.saveAsDraft"/>
<buttononly class="et2_button ui-button" label="Save as Draft and Print" value="saveAsDraftAndPrint" id="button[saveAsDraftAndPrint]" image="print" onclick="app.mail.saveAsDraftAndPrint"/>
<buttononly class="et2_button ui-button" label="Save as Draft and Print" value="saveAsDraftAndPrint" id="button[saveAsDraftAndPrint]" image="print" onclick="app.mail.saveAsDraft"/>
</description>
<description class="email-button-group file">
<vfs-select class="$cont[vfsNotAvailable] compose_egw_icons" id="selectFromVFSForCompose" onchange="app.mail.vfsUploadForCompose" button_caption=""/>
@ -56,7 +56,7 @@
<textbox class="mail-index_quotaDisplayNone" readonly="true" id="serverID"/>
<textbox class="mail-index_quotaDisplayNone" readonly="true" id="processedmail_id"/>
<textbox class="mail-index_quotaDisplayNone" readonly="true" id="mode"/>
<textbox class="mail-index_quotaDisplayNone" readonly="true" id="lastdrafted"/>
<textbox class="mail-index_quotaDisplayNone" id="lastDrafted"/>
<textbox class="mail-index_quotaDisplayNone" readonly="true" id="references"/>
<textbox class="mail-index_quotaDisplayNone" readonly="true" id="in-reply-to"/>
<textbox class="mail-index_quotaDisplayNone" readonly="true" id="thread-topic"/>