From e7be37708d753ea886d9d013f8aa86bee363abbf Mon Sep 17 00:00:00 2001 From: bison-paolo Date: Thu, 2 Mar 2017 15:47:17 +0100 Subject: [PATCH] - Bug fix in MessageLib - Now is possible to send a message to more recipients from FAS - Can be used the variables substitution from FAS --- application/config/message.php | 4 +- application/controllers/system/Messages.php | 102 ++++++++++++--- application/libraries/MessageLib.php | 14 +- application/models/system/Message_model.php | 27 ++++ application/views/system/messageWrite.php | 138 ++++++++++++++++++-- 5 files changed, 253 insertions(+), 32 deletions(-) diff --git a/application/config/message.php b/application/config/message.php index 18e4e997e..687922f70 100644 --- a/application/config/message.php +++ b/application/config/message.php @@ -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/'; diff --git a/application/controllers/system/Messages.php b/application/controllers/system/Messages.php index 0fdd6b8ec..bdfa946ac 100755 --- a/application/controllers/system/Messages.php +++ b/application/controllers/system/Messages.php @@ -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)); + } + } +} \ No newline at end of file diff --git a/application/libraries/MessageLib.php b/application/libraries/MessageLib.php index 1e683f857..fa302abc1 100644 --- a/application/libraries/MessageLib.php +++ b/application/libraries/MessageLib.php @@ -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); + } +} \ No newline at end of file diff --git a/application/models/system/Message_model.php b/application/models/system/Message_model.php index 4748f9448..bc59bb24a 100644 --- a/application/models/system/Message_model.php +++ b/application/models/system/Message_model.php @@ -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)); + } } \ No newline at end of file diff --git a/application/views/system/messageWrite.php b/application/views/system/messageWrite.php index f0d22070a..69b37589c 100644 --- a/application/views/system/messageWrite.php +++ b/application/views/system/messageWrite.php @@ -3,14 +3,47 @@
- To: vorname . " " . $receiver->nachname; ?>
- Subject:
+ To: + 1 && $i % 10 == 0) + { + echo '
'; + } + echo $receiver->Vorname . " " . $receiver->Nachname . "; "; + } + ?> +
+ Subject:
+ + Variables:
+ + +
@@ -22,29 +55,114 @@ + 0) + { + ?> +
+
+ Recipients:
+ + Refresh +
+
+ +
+
+ +
+
+ + + prestudent_id . '">' . "\n"; + } + ?> +