public function composeAction()
 {
     $auth = new FrontEndAuth();
     $getparams = $this->getRequest()->getQuery();
     isset($getparams['user_id']) ? $user_id = $getparams['user_id'] : ($user_id = '');
     if (!$auth->hasIdentity(3)) {
         return $this->redirect()->toUrl('/login');
     }
     $session = new Container('frontend');
     $model = new Practitioners();
     $common = new Common();
     $api = new Api();
     $api_url = $this->getServiceLocator()->get('config')['api_url']['value'];
     $subscriptionDetails = $common->getSubscriptiondetails($api_url, '', true);
     if (!isset($subscriptionDetails['features']) || !is_array($subscriptionDetails['features']) || !in_array(5, $subscriptionDetails['features'])) {
         $this->flashMessenger()->addErrorMessage("Either you have not subscribed any subscription or your subscription don't have permission to access this section..!!");
         return $this->redirect()->toRoute('practitioner', array('action' => 'dashboard'));
     }
     $model_consumer = new Consumers();
     $model_common = $this->getServiceLocator()->get('Application\\Model\\Common');
     $notifications = $model->getNotifications($api_url);
     $sp_details = $model->getSPDetails($api_url, $session->userid);
     $contacted_list = $model->getCPlist($api_url, false);
     //echo '<pre>';  print_r($contacted_list); exit;
     $form = new SPcomposemessageFrom($contacted_list);
     $form->get('to')->setValue($user_id);
     if ($this->getRequest()->isXmlHttpRequest()) {
         $request = $this->getRequest();
         $ids = $request->getPost('to');
         $subject = $request->getPost('subject');
         $message = $request->getPost('message');
         /* all these fields will be set to zero while inserting */
         $replyId = '0';
         $topLevel_id = '0';
         $readFlag = '0';
         $deleteFlag = '0';
         if (is_array($ids) && count($ids) > 0) {
             foreach ($ids as $id) {
                 $url = $api_url . "/api/messages/";
                 $data = array('subject' => $subject, 'message' => $message, 'to_user_id' => $id, 'from_user_id' => $session->userid, 'replyId' => $replyId, 'topLevel_id' => $topLevel_id, 'readFlag' => $readFlag, 'deleteFlag' => $deleteFlag, 'from_name' => $session->user_name);
                 $res = $api->curl($url, $data, "POST");
                 if ($res->getStatusCode() == 201) {
                     $result = json_decode($res->getBody(), true);
                     if ($result['id'] != '') {
                         /* update the message record 'reply id' and 'topL' after insert */
                         $last_insert_id = $result['id'];
                         // get last insert id for user
                         $replyId = $last_insert_id;
                         $topLevel_id = $last_insert_id;
                         $url = $api_url . "/api/messages/" . $last_insert_id . "/";
                         $data = array('subject' => $subject, 'message' => $message, 'to_user_id' => $id, 'from_user_id' => $session->userid, 'replyId' => $replyId, 'topLevel_id' => $topLevel_id);
                         $res = $api->curl($url, $data, "PUT");
                         if ($res->getStatusCode() == 200) {
                             // send email and sms notification as well
                             $consumer_data = $model_consumer->getConsumerdetails($api_url, $id);
                             if ($consumer_data) {
                                 $subscriptionDetails = $model_common->getSubscriptiondetails($api_url, $session->userid, true);
                                 $userFeatures = $model_common->getFeatures($api_url, $consumer_data['id']);
                                 if (count($consumer_data['contact']) > 0) {
                                     $contact_data = json_decode($consumer_data['contact'][0], true);
                                 }
                                 $pattern = array('/{{reciever}}/i', '/{{sender}}/i');
                                 $replace = array('<strong>' . $consumer_data['first_name'] . ' ' . $consumer_data['last_name'] . '</strong>', '<strong>' . $session->first_name . ' ' . $session->last_name . '</strong>');
                                 if (isset($consumer_data['email'])) {
                                     if (isset($subscriptionDetails['features']) && is_array($subscriptionDetails['features']) && in_array(11, $subscriptionDetails['features']) && $userFeatures['email'] == 1) {
                                         $model_common->sendMail($api_url, $consumer_data['email'], '', 16, '', $pattern, $replace, '');
                                     }
                                 }
                                 if (count($contact_data) > 0 && isset($contact_data['cell_phone'])) {
                                     $replace = array($consumer_data['first_name'] . ' ' . $consumer_data['last_name'], $session->first_name . ' ' . $session->last_name);
                                     if (isset($subscriptionDetails['features']) && is_array($subscriptionDetails['features']) && in_array(12, $subscriptionDetails['features']) && $userFeatures['sms'] == 1) {
                                         $model_common->sendMsg($contact_data['cell_phone'], 6, '', $pattern, $replace);
                                     }
                                 }
                             }
                             $msg = "Message sent sucessfully";
                         } else {
                             $error = true;
                             $msg = "Message sent with Errors";
                         }
                     } else {
                         $error = true;
                         $msg = "Message sent with errors";
                     }
                 } else {
                     $error = true;
                     $msg = "Error!! Message not sent";
                 }
             }
         } else {
             $error = true;
             $msg = "ERROR!! cannot send message";
         }
         echo json_encode(array('error' => $error, 'msg' => $msg));
         exit;
     }
     // getting banner for this page
     $banners = $common->getBanner($api_url, 4);
     return new ViewModel(array('form' => $form, 'notifications' => $notifications, 'avtar_url' => $sp_details['avtar_url'], 'first_name' => $sp_details['first_name'], 'last_name' => $sp_details['last_name'], 'banners' => $banners));
 }