- Bug fix in MessageLib

- Now is possible to send a message to more recipients from FAS
- Can be used the variables substitution from FAS
This commit is contained in:
bison-paolo
2017-03-02 15:47:17 +01:00
parent d8bb1ae500
commit e7be37708d
5 changed files with 253 additions and 32 deletions
+3 -1
View File
@@ -2,8 +2,10 @@
if (! defined('BASEPATH')) exit('No direct script access allowed');
// ONLY FOR DEBUGGING - If you are unsure, don't change it. If the message should be sent immediately. Default false
$config['send_immediately'] = false;
$config['msg_delivery'] = true; // Default true
$config['send_immediately'] = false; // If the message should be sent immediately. Default false
$config['system_person_id'] = 1; // Dummy sender, used for sending messages from the system
$config['redirect_view_message_url'] = 'index.ci.php/Redirect/redirectByToken/'; //
$config['message_html_view_url'] = 'index.ci.php/ViewMessage/toHTML/';
+84 -18
View File
@@ -103,42 +103,80 @@ class Messages extends VileSci_Controller
redirect('/system/Messages/view/' . $msg->retval . '/' . $originMsg->retval[0]->person_id);
}
public function write($sender_id, $receiver_id)
public function write($sender_id)
{
$person = $this->PersonModel->load($receiver_id);
if ($person->error)
$prestudent_id = $this->input->post('prestudent_id');
$this->load->model('crm/Prestudent_model', 'PrestudentModel');
$prestudent = $this->MessageModel->getMsgVarsData($prestudent_id);
if ($prestudent->error)
{
show_error($person->retval);
show_error($prestudent->retval);
}
$this->load->model('system/Message_model', 'MessageModel');
if (!hasData($variables = $this->MessageModel->getMessageVars()))
{
unset($variables);
}
else
{
$variablesArray = array();
// Skip person_id and prestudent_id
for($i = 2; $i < count($variables->retval); $i++)
{
$variablesArray['{'.str_replace(" ", "_", strtolower($variables->retval[$i])).'}'] = $variables->retval[$i];
}
}
array_shift($variables->retval); // Remove person_id
array_shift($variables->retval); // Remove prestudent_id
$data = array (
'sender_id' => $sender_id,
'receiver_id' => $receiver_id,
'receiver' => $person->retval[0]
'receivers' => $prestudent->retval,
'variables' => $variablesArray
);
$v = $this->load->view('system/messageWrite', $data);
}
public function send($sender_id, $receiver_id)
public function send($sender_id)
{
$error = false;
$subject = $this->input->post('subject');
$body = $this->input->post('body');
$this->load->model('system/Message_model', 'MessageModel');
$originMsg = $this->MessageModel->load($msg_id);
if ($originMsg->error)
$prestudents = $this->input->post('prestudents');
$data = $this->MessageModel->getMsgVarsData($prestudents);
if (hasData($data))
{
show_error($originMsg->retval);
for ($i = 0; $i < count($data->retval); $i++)
{
$parsedText = "";
$dataArray = (array)$data->retval[$i];
foreach($dataArray as $key => $val)
{
$newKey = str_replace(" ", "_", strtolower($key));
$dataArray[$newKey] = $dataArray[$key];
}
$parsedText = $this->messagelib->parseMessageText($body, $dataArray);
$msg = $this->messagelib->sendMessage($sender_id, $dataArray['person_id'], $subject, $parsedText, PRIORITY_NORMAL);
if ($msg->error)
{
show_error($msg->retval);
$error = true;
break;
}
}
}
$msg = $this->messagelib->sendMessage($sender_id, $receiver_id, $subject, $body, PRIORITY_NORMAL);
if ($msg->error)
if (!$error)
{
show_error($msg->retval);
echo "Messages sent successfully";
}
redirect('/system/Messages/view/' . $msg->retval . '/' . $receiver_id);
}
private function getPersonId()
@@ -176,4 +214,32 @@ class Messages extends VileSci_Controller
->set_output(json_encode($result));
}
}
}
public function parseMessageText()
{
$prestudent_id = $this->input->get('prestudent_id');
$text = $this->input->get('text');
if (isset($prestudent_id))
{
$data = $this->MessageModel->getMsgVarsData($prestudent_id);
$parsedText = "";
if (hasData($data))
{
$dataArray = (array)$data->retval[0];
foreach($dataArray as $key => $val)
{
$newKey = str_replace(" ", "_", strtolower($key));
$dataArray[$newKey] = $dataArray[$key];
}
$parsedText = $this->messagelib->parseMessageText($text, $dataArray);
}
$this->output
->set_content_type('application/json')
->set_output(json_encode($parsedText));
}
}
}
+11 -3
View File
@@ -265,12 +265,12 @@ class MessageLib
}
else
{
if (!empty($subject))
if (empty($subject))
{
$result = $this->_error('', MSG_ERR_SUBJECT_EMPTY);
break;
}
else if (!empty($body))
else if (empty($body))
{
$result = $this->_error('', MSG_ERR_BODY_EMPTY);
break;
@@ -884,4 +884,12 @@ class MessageLib
{
return success($retval, $code, MessageLib::MSG_INDX_PREFIX);
}
}
/**
*
*/
public function parseMessageText($text, $data = array())
{
return $this->ci->parser->parse_string($text, $data, true);
}
}
@@ -66,4 +66,31 @@ class Message_model extends DB_Model
return $this->execQuery($sql, $parametersArray);
}
/**
*
*/
public function getMessageVars()
{
$result = $this->db->query('SELECT * FROM public.vw_msg_vars WHERE 0 = 1');
if ($result)
{
return success($result->list_fields());
}
else
{
return error($this->db->error(), FHC_DB_ERROR);
}
}
/**
*
*/
public function getMsgVarsData($prestudent_id)
{
$query = 'SELECT * FROM public.vw_msg_vars WHERE prestudent_id %s ?';
return $this->execQuery(sprintf($query, is_array($prestudent_id) ? 'IN' : '='), array($prestudent_id));
}
}
+128 -10
View File
@@ -3,14 +3,47 @@
<body>
<?php
$href = str_replace("/system/Messages/write", "/system/Messages/send", $_SERVER["REQUEST_URI"]);
$href = substr($href, 0, strrpos($href, '?'));
?>
<form id="sendForm" method="post" action="<?php echo $href; ?>">
<div class="row">
<div class="span4">
To: <?php echo $receiver->vorname . " " . $receiver->nachname; ?><br/>
Subject: <input type="text" value="" name="subject"><br/>
To:
<?php
for($i = 0; $i < count($receivers); $i++)
{
$receiver = $receivers[$i];
// Every 10 recipients a new line
if ($i > 1 && $i % 10 == 0)
{
echo '<br>';
}
echo $receiver->Vorname . " " . $receiver->Nachname . "; ";
}
?>
<br>
Subject: <input type="text" value="" name="subject" size="70"><br/>
<textarea id="bodyTextArea" name="body"></textarea>
<?php
if (isset($variables))
{
?>
Variables:<br>
<select id="variables" size="12" style="min-width:200px;">
<?php
foreach($variables as $key => $val)
{
?>
<option value="<?php echo $key; ?>"><?php echo $val; ?></option>
<?php
}
?>
</select>
<?php
}
?>
</div>
</div>
@@ -22,29 +55,114 @@
</div>
</div>
<?php
if (isset($receivers) && count($receivers) > 0)
{
?>
<div class="row">
<div class="span4">
Recipients:<br>
<select id="recipients">
<option value="-1">Select...</option>
<?php
foreach($receivers as $receiver)
{
?>
<option value="<?php echo $receiver->prestudent_id; ?>"><?php echo $receiver->Nachname . " " . $receiver->Vorname; ?></option>
<?php
}
?>
</select>
<a href="#" id="refresh">Refresh</a>
</div>
</div>
<div class="row">
<div class="span4">
<textarea id="tinymcePreview"></textarea>
</div>
</div>
<?php
}
?>
<?php
for($i = 0; $i < count($receivers); $i++)
{
$receiver = $receivers[$i];
echo '<input type="hidden" name="prestudents[]" value="' . $receiver->prestudent_id . '">' . "\n";
}
?>
</form>
</body>
<script>
tinymce.init({
selector: "#bodyTextArea"
selector: "#bodyTextArea"
});
<?php
$url = str_replace("/system/Messages/write", "/system/Messages/getVorlage", $_SERVER["REQUEST_URI"]);
?>
tinymce.init({
menubar: false,
toolbar: false,
readonly: 1,
selector: "#tinymcePreview",
statusbar: true
});
function getVorlageText(vorlage_kurzbz)
$(document).ready(function() {
if ($("#variables"))
{
$("#variables").dblclick(function() {
if ($("#bodyTextArea"))
{
tinyMCE.get("bodyTextArea").setContent(tinyMCE.get("bodyTextArea").getContent() + $(this).children(":selected").val());
}
});
}
if ($("#recipients"))
{
$("#recipients").change(tinymcePreviewSetContent);
}
if ($("#refresh"))
{
$("#refresh").click(tinymcePreviewSetContent);
}
});
function tinymcePreviewSetContent()
{
if ($("#tinymcePreview"))
{
if ($("#recipients").children(":selected").val() > -1)
{
parseMessageText($("#recipients").children(":selected").val(), tinyMCE.get("bodyTextArea").getContent());
}
else
{
tinyMCE.get("tinymcePreview").setContent("");
}
}
}
function parseMessageText(prestudent_id, text)
{
<?php
$url = str_replace("/system/Messages/write", "/system/Messages/parseMessageText", $_SERVER["REQUEST_URI"]);
$url = substr($url, 0, strrpos($url, '/'));
?>
$.ajax({
dataType: "json",
url: "<?php echo $url; ?>",
data: {"vorlage_kurzbz": vorlage_kurzbz},
data: {"prestudent_id": prestudent_id, "text" : text},
success: function(data, textStatus, jqXHR) {
tinyMCE.activeEditor.setContent(data.retval[0].text);
tinyMCE.get("tinymcePreview").setContent(data);
},
error: function(jqXHR, textStatus, errorThrown) {
alert(textStatus + " - " + errorThrown);
alert(textStatus + " - " + errorThrown + " - " + jqXHR.responseText);
}
});
}