public function update($id)
 {
     $addressbook = Addressbook::find($id);
     $inputs = [];
     foreach (Input::all() as $key => $input) {
         $inputs[$key] = Jamesy\Sanitiser::trimInput($input);
     }
     if ($inputs['name'] == $addressbook->name) {
         $validation = NULL;
     } else {
         $validation = Jamesy\MyValidations::validate($inputs, $this->rules);
     }
     if ($validation != NULL) {
         return Redirect::back()->withErrors($validation)->withInput();
     } else {
         $addressbook->name = $inputs['name'];
         $addressbook->active = $inputs['active'];
         $addressbook->save();
         if ($addressbook->active == 0) {
             $subscribers = Subscriber::with(['addressbooks' => function ($query) use($id) {
                 $query->where('addressbook_id', $id);
             }])->get();
             foreach ($subscribers as $subscriber) {
                 $subscriber->active = 0;
                 $subscriber->save();
             }
         }
         return Redirect::to('dashboard/lists')->withSuccess($addressbook->active == 0 ? 'List updated. NOTE: All subscribers in the list are now inactive.' : 'List updated.');
     }
 }
 public function getProfile($id)
 {
     try {
         $profile = Subscriber::with('Recharge')->findOrFail($id);
         $sess_history = $profile->sessionHistory()->orderby('acctstarttime', 'DESC')->paginate(5);
         return View::make('admin.accounts.profile')->with('profile', $profile)->with('sess_history', $sess_history);
     } catch (Illuminate\Database\Eloquent\ModelNotFoundException $e) {
         App::abort(404);
     }
 }
 public function remove_subscribers_from_list($id)
 {
     $subs_array = Input::get('subsarray');
     $list = Addressbook::find($id);
     $count = 0;
     foreach ($subs_array as $key => $value) {
         $list->subscribers()->detach($value);
         if ($list->active == 0) {
             $subscriber = Subscriber::with('lists')->find($value);
             $check = 1;
             foreach ($subscriber->lists as $alist) {
                 if ($alist->active == 0) {
                     $check = 0;
                 }
             }
             if ($check == 1) {
                 $subscriber->active = 1;
                 $subscriber->save();
             }
         }
         $count++;
     }
     return Response::json(array('success' => $count . ' subscribers successfully removed from ' . $list->name));
 }
 public function update($id)
 {
     $maillist = Maillist::getMaillistWithSubs($id);
     $name = Jamesy\Sanitiser::trimInput(Input::get('name'));
     if ($name == $maillist->name) {
         $validation = NULL;
     } else {
         $validation = Jamesy\MyValidations::validate(['name' => $name], $this->rules);
     }
     if ($validation != NULL) {
         return Redirect::back()->withErrors($validation)->withInput();
     } else {
         $maillist->name = $name;
         $maillist->active = Input::get('active');
         $maillist->save();
         if ($maillist->active == 0) {
             $subscribers = $maillist->subscribers;
             foreach ($subscribers as $subscriber) {
                 $subscriber->active = 0;
                 $subscriber->save();
             }
         }
         if (Input::file('file')) {
             $fileValidation = Jamesy\MyValidations::validate(['file' => Input::file('file')], $this->fileSizeRules);
             if ($fileValidation != NULL) {
                 return Redirect::back()->withErrors($fileValidation)->withInput();
             } else {
                 $input = Input::file('file');
                 $ext = pathinfo($input->getClientOriginalName(), PATHINFO_EXTENSION);
                 if ($ext != 'xlt' && $ext != 'xls' && $ext != 'csv') {
                     return Redirect::back()->withIssues('You attempted the import with an invalid file. File must be Excel or CSV')->withInput();
                 } else {
                     $import = new Jamesy\Imports($input, $this->subscriberRules);
                     $importResult = $import->getInsertArray();
                     if (is_array($importResult)) {
                         $totalNum = $importResult[0];
                         $duplicatesNum = $importResult[1];
                         $passedArr = $importResult[2];
                         $passedNum = count($passedArr);
                         $failedNum = (int) $totalNum - (int) $duplicatesNum - (int) $passedNum;
                         $timestamp = $importResult[3];
                         $existingEmails = $importResult[4];
                         $newPassedArr = [];
                         if ($maillist->active == 0) {
                             foreach ($passedArr as $subscriber) {
                                 $subscriber['active'] = 0;
                                 $newPassedArr[] = $subscriber;
                             }
                         } else {
                             $newPassedArr = $passedArr;
                         }
                         if (count($newPassedArr)) {
                             Subscriber::insert($newPassedArr);
                             $generalList = Maillist::find(1);
                             $generalList->touch();
                             $maillist->touch();
                             $subscribers = Subscriber::where('created_at', $timestamp)->where('updated_at', $timestamp)->get();
                             foreach ($subscribers as $key => $subscriber) {
                                 $generalList->subscribers()->attach($subscriber->id);
                                 $maillist->subscribers()->attach($subscriber->id);
                             }
                         }
                         if (count($existingEmails)) {
                             $alreadySubs = Subscriber::with('maillists')->whereIn('email', $existingEmails)->get();
                             if (count($alreadySubs)) {
                                 foreach ($alreadySubs as $subscriber) {
                                     if (!$subscriber->maillists->contains($maillist->id)) {
                                         $maillist->subscribers()->attach($subscriber->id);
                                     }
                                     if ($maillist->active == 0) {
                                         $subscriber->active = 0;
                                         $subscriber->save();
                                     }
                                 }
                             }
                         }
                         $message = "List updated.";
                         $message .= "<br /><b>{$totalNum}</b> " . str_plural('row', $totalNum) . " found in excel file.";
                         if ($duplicatesNum) {
                             $message .= "<br /><b>{$duplicatesNum}</b> had duplicate email addresses.";
                         }
                         if ($passedNum == 1) {
                             $message .= "<br /><b>1</b> out of the <b>" . ($totalNum - $duplicatesNum) . "</b> with unique emails passed validation and was stored.";
                         } else {
                             $message .= "<br /><b>{$passedNum}</b> out of the <b>" . ($totalNum - $duplicatesNum) . "</b> with unique emails passed validation and were stored.";
                         }
                         if ($failedNum == 1) {
                             $message .= "<br /><b>1</b> out of the <b>" . ($totalNum - $duplicatesNum) . "</b> with unique emails failed validation (no first name, last name or bad email) and was NOT stored.";
                         } elseif ($failedNum > 1) {
                             $message .= "<br /><b>{$failedNum}</b> out of the <b>" . ($totalNum - $duplicatesNum) . "</b> with unique emails failed validation (no first name, last name or bad email) and were NOT stored.";
                         }
                         return Redirect::to('dashboard/lists')->withSuccess($message);
                     } else {
                         return Redirect::back()->withIssues($importResult);
                     }
                 }
             }
         } else {
             return Redirect::to('dashboard/lists')->withSuccess($maillist->active == 0 ? 'List updated. NOTE: All subscribers in the list are now inactive.' : 'List updated.');
         }
     }
 }
 public function update($id)
 {
     $subscriber = Subscriber::find($id);
     $inputs = [];
     foreach (Input::all() as $key => $input) {
         $inputs[$key] = Jamesy\Sanitiser::trimInput($input);
     }
     if ($inputs['email'] == $subscriber->email) {
         $validation = Jamesy\MyValidations::validate($inputs, $this->editRules);
     } else {
         $validation = Jamesy\MyValidations::validate($inputs, $this->rules);
     }
     if ($validation != NULL) {
         return Redirect::back()->withErrors($validation)->withInput();
     } else {
         $maillists = Input::has('lists') ? Input::get('lists') : [Maillist::first()->id];
         $subscriber->first_name = $inputs['first_name'];
         $subscriber->last_name = $inputs['last_name'];
         $subscriber->email = Str::lower($inputs['email']);
         $subscriber->active = $inputs['active'];
         $subscriber->save();
         $subscriber->maillists()->sync($maillists);
         $newMaillists = Subscriber::with('maillists')->find($id)->maillists;
         $deactivated = false;
         foreach ($newMaillists as $maillist) {
             if ($maillist->active == 0) {
                 $subscriber->active = 0;
                 $subscriber->save();
                 $deactivated = true;
                 break;
             }
         }
         return Redirect::to('dashboard/subscribers')->withSuccess($deactivated && $inputs['active'] ? 'Subscriber updated. NOTE: Subscriber belongs to an inactive list.' : 'Subscriber updated.');
     }
 }