diff --git a/application/controllers/api/v1/system/Message.php b/application/controllers/api/v1/system/Message.php index d8c6e0a0e..972f046b4 100644 --- a/application/controllers/api/v1/system/Message.php +++ b/application/controllers/api/v1/system/Message.php @@ -22,7 +22,7 @@ class Message extends APIv1_Controller public function __construct() { parent::__construct(); - // Load model MessageModel + // Load library MessageLib $this->load->library('MessageLib', array('uid' => $this->_getUID())); } @@ -71,7 +71,7 @@ class Message extends APIv1_Controller */ public function postMessage() { - $validation = $this->_validate($this->post()); + $validation = $this->_validatePostMessage($this->post()); if (is_object($validation) && $validation->error == EXIT_SUCCESS) { @@ -93,7 +93,33 @@ class Message extends APIv1_Controller } } - private function _validate($message = null) + /** + * @return void + */ + public function postMessageVorlage() + { + $validation = $this->_validatePostMessageVorlage($this->post()); + + if (is_object($validation) && $validation->error == EXIT_SUCCESS) + { + $result = $this->messagelib->sendMessageVorlage( + $this->post()['sender_id'], + $this->post()['receiver_id'], + $this->post()['vorlage_kurzbz'], + $this->post()['oe_kurzbz'], + $this->post()['data'], + $this->post()['orgform_kurzbz'] + ); + + $this->response($result, REST_Controller::HTTP_OK); + } + else + { + $this->response($validation, REST_Controller::HTTP_OK); + } + } + + private function _validatePostMessage($message = null) { if (!isset($message)) { @@ -118,4 +144,34 @@ class Message extends APIv1_Controller return $this->_success('Input data are valid'); } + + private function _validatePostMessageVorlage($message = null) + { + if (!isset($message)) + { + return $this->_error('Parameter is null'); + } + if (!isset($message['sender_id'])) + { + return $this->_error('person_id of sender is not set'); + } + if (!isset($message['receiver_id'])) + { + return $this->_error('person_id of receiver is not set'); + } + if (!isset($message['vorlage_kurzbz'])) + { + return $this->_error('vorlage_kurzbz is not set'); + } + if( !isset($message['oe_kurzbz'])) + { + return $this->_error('oe_kurzbz is not set'); + } + if (!isset($message['data'])) + { + return $this->_error('data is not set'); + } + + return $this->_success('Input data are valid'); + } } \ No newline at end of file diff --git a/application/helpers/message_helper.php b/application/helpers/message_helper.php index 0a70a1015..1e05b0d48 100644 --- a/application/helpers/message_helper.php +++ b/application/helpers/message_helper.php @@ -5,7 +5,7 @@ * * @return array */ -function success($retval, $message = FHC_SUCCESS) +function success($retval, $message = EXIT_SUCCESS) { $return = new stdClass(); $return->error = EXIT_SUCCESS; @@ -20,10 +20,10 @@ function success($retval, $message = FHC_SUCCESS) * * @return array */ -function error($retval = '', $message = FHC_MODEL_ERROR) +function error($retval = '', $message = EXIT_ERROR) { $return = new stdClass(); - $return->error = EXIT_MODEL; + $return->error = EXIT_ERROR; $return->fhcCode = $message; $return->msg = lang('fhc_' . $message); $return->retval = $retval; diff --git a/application/libraries/MessageLib.php b/application/libraries/MessageLib.php index 24ffbf723..038935b17 100644 --- a/application/libraries/MessageLib.php +++ b/application/libraries/MessageLib.php @@ -17,16 +17,26 @@ class MessageLib $this->ci->config->load('message'); $this->ci->load->model('system/Message_model', 'MessageModel'); - if (is_array($params) && isset($params['uid'])) - { - $this->ci->MessageModel->setUID($params['uid']); - } - $this->ci->load->model('system/MsgStatus_model', 'MsgStatusModel'); $this->ci->load->model('system/Recipient_model', 'RecipientModel'); $this->ci->load->model('system/Attachment_model', 'AttachmentModel'); + + if (is_array($params) && isset($params['uid'])) + { + $this->ci->load->library('VorlageLib', array('uid' => $params['uid'])); + $this->ci->MessageModel->setUID($params['uid']); + $this->ci->MsgStatusModel->setUID($params['uid']); + $this->ci->RecipientModel->setUID($params['uid']); + $this->ci->AttachmentModel->setUID($params['uid']); + } + else + { + $this->ci->load->library('VorlageLib'); + } + //$this->ci->load->helper('language'); $this->ci->lang->load('message'); + } // ------------------------------------------------------------------------ @@ -212,6 +222,86 @@ class MessageLib else return $this->_success($msg_id); + } + + /** + * sendMessage() - sends new internal message. This function will create a new thread + * + * @param integer $sender_id REQUIRED + * @param mixed $recipients REQUIRED - a single integer or an array of integers, representing user_ids + * @param string $subject + * @param string $body + * @param integer $priority + * @return array + */ + function sendMessageVorlage($sender_id, $receiver_id, $vorlage_kurzbz, $oe_kurzbz, $data, $orgform_kurzbz = null) + { + var_dump($data); + + exit; + + if (!is_numeric($sender_id) || !is_numeric($receiver_id)) + return $this->_invalid_id(MSG_ERR_INVALID_MSG_ID); + + $result = $this->ci->vorlagelib->getVorlage($vorlage_kurzbz); + if (is_object($result) && $result->error == EXIT_SUCCESS) + { + if (is_array($result->retval) && count($result->retval) > 0) + { + $parsedText = $this->ci->vorlagelib->parseVorlagetext($result->retval[0]->text, $data); + + error_log($parsedText); + + $this->ci->db->trans_start(false); + //save Message + $msgData = array( + 'person_id' => $sender_id, + //'subject' => $subject, + 'body' => $parsedText, + 'priority' => PRIORITY_NORMAL, + //'relationmessage_id' => $relationmessage_id, + 'oe_kurzbz' => $oe_kurzbz + ); + + $result = $this->ci->MessageModel->insert($msgData); + if (is_object($result) && $result->error == EXIT_SUCCESS) + { + $msg_id = $result->retval; + $recipientData = array( + 'person_id' => $receiver_id, + 'message_id' => $msg_id + ); + $result = $this->ci->RecipientModel->insert($recipientData); + /*if (is_object($result) && $result->error == EXIT_SUCCESS) + { + + }*/ + } + + $this->ci->db->trans_complete(); + + if ($this->ci->db->trans_status() === FALSE) + { + $this->db->trans_rollback(); + return $this->_error($result->msg, EXIT_ERROR); + } + else + { + $this->db->trans_commit(); + return $this->_success($msg_id); + } + } + else + { + $result = $this->_error('Vorlage not found', EXIT_ERROR); + } + } + else + { + $result = $this->_error($result->msg, EXIT_ERROR); + } + + return $result; } // ------------------------------------------------------------------------ @@ -345,4 +435,4 @@ class MessageLib 'msg' => lang('message_'.$error) ); } -} +} \ No newline at end of file diff --git a/application/libraries/VorlageLib.php b/application/libraries/VorlageLib.php index d7a315be4..797b9c5a3 100644 --- a/application/libraries/VorlageLib.php +++ b/application/libraries/VorlageLib.php @@ -11,7 +11,7 @@ class VorlageLib { private $recipients = array(); - public function __construct() + public function __construct($params = null) { require_once APPPATH.'config/message.php'; @@ -19,6 +19,13 @@ class VorlageLib $this->ci->load->library('parser'); $this->ci->load->model('system/Vorlage_model', 'VorlageModel'); $this->ci->load->model('system/Vorlagestudiengang_model', 'VorlageStudiengangModel'); + + if (is_array($params) && isset($params['uid'])) + { + $this->ci->VorlageModel->setUID($params['uid']); + $this->ci->VorlageStudiengangModel->setUID($params['uid']); + } + $this->ci->load->helper('language'); //$this->ci->lang->load('fhcomplete'); } @@ -149,4 +156,35 @@ class VorlageLib $text = $this->ci->parser->parse_string($text, $data, TRUE); return $text; } -} + + /** --------------------------------------------------------------- + * Success + * + * @param mixed $retval + * @return array + */ + protected function _success($retval, $message = EXIT_SUCCESS) + { + $return = new stdClass(); + $return->error = EXIT_SUCCESS; + $return->Code = $message; + $return->msg = lang('message_' . $message); + $return->retval = $retval; + return $return; + } + + /** --------------------------------------------------------------- + * General Error + * + * @return array + */ + protected function _error($retval = '', $message = EXIT_ERROR) + { + $return = new stdClass(); + $return->error = EXIT_ERROR; + $return->Code = $message; + $return->msg = lang('message_' . $message); + $return->retval = $retval; + return $return; + } +} \ No newline at end of file