public function getFields($business_id)
 {
     $fields = array();
     $res = Forms::getFieldsByBusinessId($business_id);
     foreach ($res as $count => $data) {
         $field_data = unserialize($data->field_data);
         $fields[$data->form_id] = array('field_type' => $data->field_type, 'label' => $field_data['label'], 'options' => array_key_exists('options', $field_data) ? unserialize($field_data['options']) : array(), 'value_a' => array_key_exists('value_a', $field_data) ? $field_data['value_a'] : '', 'value_b' => array_key_exists('value_b', $field_data) ? $field_data['value_b'] : '');
     }
     return $fields;
 }
 public function postSendtoBusiness()
 {
     if (Auth::check()) {
         $business_id = Input::get('business_id');
         $attachment = Input::get('contfile');
         $email = User::email(Auth::user()->user_id);
         $timestamp = time();
         $thread_key = $this->threadKeyGenerator($business_id, $email);
         $custom_fields_bool = Input::get('custom_fields_bool');
         // save if there are custom fields available
         $custom_fields_data = '';
         if ($custom_fields_bool) {
             $custom_fields = Input::get('custom_fields');
             $res = Forms::getFieldsByBusinessId($business_id);
             foreach ($res as $count => $data) {
                 $custom_fields_data .= '<strong>' . Forms::getLabelByFormId($data->form_id) . ':</strong> ' . $custom_fields[$data->form_id] . "\n";
             }
         }
         if (!Message::checkThreadByKey($thread_key)) {
             $phones[] = Input::get('contmobile');
             Message::createThread(array('contactname' => User::first_name(Auth::user()->user_id) . ' ' . User::last_name(Auth::user()->user_id), 'business_id' => $business_id, 'email' => $email, 'phone' => serialize($phones), 'thread_key' => $thread_key));
             $data = json_encode(array(array('timestamp' => $timestamp, 'contmessage' => Input::get('contmessage') . "\n\n" . $custom_fields_data, 'attachment' => $attachment, 'sender' => 'user')));
             file_put_contents(public_path() . '/json/messages/' . $thread_key . '.json', $data);
         } else {
             $data = json_decode(file_get_contents(public_path() . '/json/messages/' . $thread_key . '.json'));
             $data[] = array('timestamp' => $timestamp, 'contmessage' => Input::get('contmessage') . "\n\n" . $custom_fields_data, 'attachment' => $attachment, 'sender' => 'user');
             $data = json_encode($data);
             file_put_contents(public_path() . '/json/messages/' . $thread_key . '.json', $data);
         }
         /*
         Mail::send('emails.contact', array(
           'name' => $name,
           'email' => $email,
           'messageContent' => Input::get('contmessage') . "\n\nAttachment: " . $attachment . "\n\n" . $custom_fields_data,
         ), function($message, $email, $name)
         {
           $message->subject('Message from '. $name . ' ' . $email);
           $message->to('*****@*****.**');
         });
         */
         return json_encode(array('status' => 1));
     } else {
         return json_encode(array('messages' => 'You are not allowed to access this function.'));
     }
 }