public function sendMessage($society, $type)
 {
     $soc = Society::find($society);
     $input = Input::all();
     $alldat = array();
     if ($input['leaderoptions'] == "everyone") {
         $allhouseholds = Household::with('individual')->where('society_id', '=', $society)->get();
         foreach ($allhouseholds as $hhh) {
             foreach ($hhh->individual as $iii) {
                 if ($iii->id == $hhh->householdcell) {
                     $dum = self::PopulateRecipient($iii);
                     if ($dum['cellphone'] != "" and $iii->memberstatus == "member") {
                         $alldat[$iii->id] = $dum;
                     }
                 }
             }
         }
     } elseif (isset($input['groups'])) {
         $groups = $input['groups'];
         if ($input['leaderoptions'] == "allmembers") {
             foreach ($groups as $thisgroup) {
                 $group = Group::with('individual')->find($thisgroup);
                 foreach ($group->individual as $indiv) {
                     $dum = self::PopulateRecipient($indiv);
                     $alldat[$indiv->id] = $dum;
                 }
             }
         } elseif ($input['leaderoptions'] == "leadersonly" or $input['leaderoptions'] == "leadersandlist") {
             foreach ($groups as $thisgroup) {
                 $group = Group::with('individual')->find($thisgroup);
                 foreach ($group->individual as $indiv) {
                     if ($group->contact == $indiv->id) {
                         $dum = self::PopulateRecipient($indiv);
                         if (!isset($alldat[$indiv->id])) {
                             $alldat[$indiv->id] = $dum;
                         }
                         if ($input['leaderoptions'] == "leadersandlist") {
                             if (isset($alldat[$indiv->id]['grouplist'])) {
                                 $alldat[$indiv->id]['grouplist'] = $alldat[$indiv->id]['grouplist'] . $group->id . ",";
                             } else {
                                 $alldat[$indiv->id]['grouplist'] = $group->id . ",";
                             }
                         }
                     }
                 }
             }
         }
     }
     if (isset($input['indivs'])) {
         foreach ($input['indivs'] as $individ) {
             $indiv = Individual::find($individ);
             $dum = self::PopulateRecipient($indiv);
             if (!isset($alldat[$individ])) {
                 $alldat[$individ] = $dum;
             }
         }
     }
     if ($type == "email") {
         if (isset($input['htmlbody'])) {
             $message = nl2br($input['message']) . file_get_contents($input['htmlbody']);
         } else {
             $message = nl2br($input['message']);
         }
         $message = HTML::decode($message);
         $subject = $input['subject'];
         $sender = Input::get('sender');
         Mail::queue('messages.message', array('msg' => $message), function ($message) use($subject, $soc, $alldat, $sender) {
             if (Input::file('attachment')) {
                 $message->attach(Input::file('attachment'), array('as' => Input::file('attachment')->getClientOriginalName(), 'mime' => Input::file('attachment')->getMimeType()));
             }
             foreach ($alldat as $thisrec) {
                 $name = $thisrec['name'];
                 $email = $thisrec['email'];
                 $household = $thisrec['household'];
                 if (isset($thisrec['grouplist'])) {
                     $grouplist = $thisrec['grouplist'];
                 } else {
                     $grouplist = "No group";
                 }
                 $message->from('*****@*****.**', $soc->society . " Methodist Church");
                 $message->to($email, $name)->replyTo($sender);
                 if ($subject == "") {
                     $subject = "Email from " . $soc->society . " Methodist Church";
                 }
                 if ($grouplist != "No group") {
                     $ggl = explode(",", substr($grouplist, 0, -1));
                     foreach ($ggl as $gl) {
                         $repdata = app('App\\Http\\Controllers\\ReportsController')->show($gl, true);
                         $tg = Group::find($gl);
                         $message->attachData($repdata, $tg->groupname . ".pdf");
                     }
                 }
                 $message->subject($subject);
             }
         });
         $results = array();
         foreach ($alldat as $tdat) {
             $result['name'] = $tdat['name'];
             $result['address'] = $tdat['email'];
             $result['household'] = $tdat['household'];
             $results[] = $result;
         }
     } else {
         $final = array();
         foreach ($alldat as $thisdat) {
             $ndx = intval($thisdat['household']);
             if (Input::get('economy') == "household") {
                 if (array_key_exists($ndx, $final)) {
                     $keyhouse = Household::find($ndx)->householdcell;
                     $keycell = Individual::find($keyhouse);
                     if (SMSfunctions::checkcell($keycell->cellphone)) {
                         $final[$ndx]['cellphone'] = $keycell->cellphone;
                         $final[$ndx]['name'] = $keycell->firstname . " " . $keycell->surname;
                         $final[$ndx]['household'] = $ndx;
                     }
                 } else {
                     $final[$ndx]['cellphone'] = $thisdat['cellphone'];
                     $final[$ndx]['name'] = $thisdat['name'];
                     $final[$ndx]['household'] = $ndx;
                 }
             } else {
                 $dummy['cellphone'] = $thisdat['cellphone'];
                 $dummy['name'] = $thisdat['name'];
                 $dummy['household'] = $ndx;
                 $final[] = $dummy;
             }
         }
         if ($soc->sms_provider == "bulksms") {
             if (count($final) > SMSfunctions::BS_get_credits($soc->sms_username, $soc->sms_password)) {
                 return Redirect::back()->withInput()->withErrors("Insufficient Bulk SMS credits to send SMS");
             }
             $url = 'http://community.bulksms.com/eapi/submission/send_sms/2/2.0';
             $port = 80;
         } elseif ($soc->sms_provider == "smsfactory") {
             if (count($final) > SMSfunctions::SF_checkCredits($soc->sms_username, $soc->sms_password)) {
                 return Redirect::back()->withInput()->withErrors("Insufficient SMS Factory credits to send SMS");
             }
         }
         $seven_bit_msg = Input::get('message') . " (From " . substr($soc->society, 0, 1) . "MC)";
         foreach ($final as $sms) {
             if ($soc->sms_provider == "bulksms") {
                 $transient_errors = array(40 => 1);
                 $msisdn = "+27" . substr($sms['cellphone'], 1);
                 $post_body = SMSfunctions::BS_seven_bit_sms($soc->sms_username, $soc->sms_password, $seven_bit_msg, $msisdn);
             }
             $dum2['name'] = $sms['name'];
             $dum2['household'] = $sms['household'];
             if (SMSfunctions::checkcell($sms['cellphone'])) {
                 if ($soc->sms_provider == "bulksms") {
                     $smsresult = SMSfunctions::BS_send_message($post_body, $url, $port);
                 } elseif ($soc->sms_provider == "smsfactory") {
                     $smsresult = SMSfunctions::SF_sendSms($soc->sms_username, $soc->sms_password, $sms['cellphone'], $seven_bit_msg);
                 }
                 $dum2['address'] = $sms['cellphone'];
             } else {
                 if ($sms['cellphone'] == "") {
                     $dum2['address'] = "No cell number provided.";
                 } else {
                     $dum2['address'] = "Invalid cell number: " . $sms['cellphone'] . ".";
                 }
             }
             $results[] = $dum2;
         }
         $data['results'] = $results;
     }
     $data['type'] = $type;
     $data['results'] = $results;
     return View::make('messages.results', $data);
 }
 public function sms($society, $id, $send)
 {
     if (Helpers::perm('admin', $society) or Helpers::perm('edit', $society)) {
         $soc = Society::find($society);
         $extra = array();
         $data['society'] = $society;
         $extra = Input::get('extrainfo');
         $data['extrainfo'] = $extra;
         $daysofweek = array('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday');
         $data['roster'] = Roster::with(array('group'))->find($id);
         $data['rosterday'] = $daysofweek[$data['roster']->dayofweek - 1];
         $dday = date("Y-m-d", strtotime('next ' . $data['rosterday']));
         $rosterdetails = DB::table('group_individual_roster')->join('groups', 'group_id', '=', 'groups.id')->join('individuals', 'individual_id', '=', 'individuals.id')->join('rosters', 'roster_id', '=', 'rosters.id')->select('surname', 'firstname', 'cellphone', 'groupname', 'message', 'dayofweek', 'group_id', 'household_id')->where('rosterdate', '=', $dday)->where('roster_id', '=', $id)->orderby('groupname')->get();
         foreach ($rosterdetails as $detail) {
             $dum['cellphone'] = $detail->cellphone;
             $dum['message'] = $detail->message . " (" . $detail->groupname . ")";
             $dum['household'] = $detail->household_id;
             if ($extra and array_key_exists($detail->group_id, $extra)) {
                 $dum['message'] = $dum['message'] . " (" . $extra[$detail->group_id] . ")";
             }
             if (strpos($dum['message'], "[dayofweek]")) {
                 $dum['message'] = str_replace("[dayofweek]", $data['rosterday'], $dum['message']);
             }
             if (strpos($dum['message'], "[groupname]")) {
                 $dum['message'] = str_replace("[groupname]", $detail->groupname, $dum['message']);
             }
             if (strpos($dum['message'], "[firstname]")) {
                 $dum['message'] = str_replace("[firstname]", $detail->firstname, $dum['message']);
             }
             $dum['recipient'] = $detail->firstname . " " . $detail->surname;
             $data['rosterdetails'][] = $dum;
         }
         $data['rosterdate'] = $dday;
         if ($send == "preview") {
             return View::make('rosters.sms', $data);
         } else {
             if ($soc->sms_provider == "bulksms") {
                 if (count($data['rosterdetails']) > SMSfunctions::BS_get_credits($soc->sms_username, $soc->sms_password)) {
                     return Redirect::back()->withInput()->withErrors("Insufficient Bulk SMS credits to send SMS");
                 }
                 $url = 'http://community.bulksms.com/eapi/submission/send_sms/2/2.0';
                 $port = 80;
             } elseif ($soc->sms_provider == "smsfactory") {
                 if (count($data['rosterdetails']) > SMSfunctions::SF_checkCredits($soc->sms_username, $soc->sms_password)) {
                     return Redirect::back()->withInput()->withErrors("Insufficient SMS Factory credits to send SMS");
                 }
             }
             foreach ($data['rosterdetails'] as $sms) {
                 $seven_bit_msg = $sms['message'] . " (From " . substr($soc->society, 0, 1) . "MC)";
                 if ($soc->sms_provider == "bulksms") {
                     $transient_errors = array(40 => 1);
                     $msisdn = "+27" . substr($sms['cellphone'], 1);
                     $post_body = SMSfunctions::BS_seven_bit_sms($soc->sms_username, $soc->sms_password, $seven_bit_msg, $msisdn);
                 }
                 $dum2['name'] = $sms['recipient'];
                 $dum2['household'] = $sms['household'];
                 if (SMSfunctions::checkcell($sms['cellphone'])) {
                     if ($soc->sms_provider == "bulksms") {
                         $smsresult = SMSfunctions::BS_send_message($post_body, $url, $port);
                     } elseif ($soc->sms_provider == "smsfactory") {
                         $smsresult = SMSfunctions::SF_sendSms($soc->sms_username, $soc->sms_password, $sms['cellphone'], $seven_bit_msg);
                     }
                     $dum2['address'] = $sms['cellphone'];
                 } else {
                     if ($sms['cellphone'] == "") {
                         $dum2['address'] = "No cell number provided.";
                     } else {
                         $dum2['address'] = "Invalid cell number: " . $sms['cellphone'] . ".";
                     }
                 }
                 $results[] = $dum2;
             }
             $data['results'] = $results;
             $data['type'] = "SMS";
         }
         return View::make('messages.results', $data);
     } else {
         return view('shared.unauthorised');
     }
 }