public function getRecharge()
 {
     $accounts = Subscriber::where('is_admin', 0)->where(function ($query) {
         $query->where('plan_type', PREPAID_PLAN)->orWhere('plan_type', FREE_PLAN);
     })->orderby('uname')->lists('uname', 'id');
     return View::make('admin.refill_coupons.recharge')->with('accounts', $accounts);
 }
 public function doApplySubscriber()
 {
     $validator = Validator::make(array('email' => trim(Input::get('email'))), array('email' => 'required|email|min:6|max:32'));
     if ($validator->fails()) {
         return Response::json(array('status' => false));
     }
     $existed = Subscriber::where('email', trim(Input::get('email')))->first();
     if ($existed) {
         return Response::json(array('status' => false, 'exist' => true));
     }
     Subscriber::create(array('email' => trim(Input::get('email'))));
     return Response::json(array('status' => true));
 }
 public function postResetPassword()
 {
     $pword = Input::get('npword');
     $id = Input::get('id');
     $affectedRows = Subscriber::where('id', $id)->update(['pword' => Hash::make($pword), 'clear_pword' => $pword]);
     if ($affectedRows) {
         $this->notifySuccess("Password Changed.");
     } else {
         $this->notifyError("Failed to change password.");
     }
     return Redirect::back();
 }
 Route::post('dashboard/lists/remove-from-list/{id}', 'ListController@remove_subscribers_from_list');
 Route::get('dashboard/lists/export-list/{id}', 'ListController@export_list_csv');
 Route::get('dashboard/lists/compose-form', function () {
     return View::make('dashboard.ajax.compose-list-email', array('user' => Sentry::getUser()));
 });
 Route::post('dashboard/lists/fetch-subs/{num}', 'ListController@fetch_subs');
 Route::post('dashboard/lists/send-email/{num}', 'ListController@sub_send_email');
 /*
 |----------------------------------------------------------------------------
 |EMAILS
 |----------------------------------------------------------------------------
 */
 Route::get('dashboard/emails', 'EmailController@index');
 Route::post('dashboard/emails', 'EmailController@index');
 Route::get('dashboard/emails/compose-email', function () {
     $subscribers = Subscriber::where('active', '=', 1)->get();
     return View::make('dashboard.ajax.compose-emails-email', array('user' => Sentry::getUser(), 'subscribers' => $subscribers));
 });
 Route::post('dashboard/emails/move-to-drafts/{num}', 'EmailController@move_to_drafts');
 Route::post('dashboard/emails/drafts-destroy/{num}', 'EmailController@destroy_draft');
 Route::post('dashboard/emails/send-email/{num}', 'EmailController@send_email');
 Route::post('dashboard/emails/trash/{num}', 'EmailController@trash_email');
 Route::post('dashboard/emails/destroy/{num}', 'EmailController@destroy_email');
 Route::get('dashboard/emails/move-to-sent/{id}', 'EmailController@move_to_sent');
 /*
 |----------------------------------------------------------------------------
 |HELP
 |----------------------------------------------------------------------------
 */
 Route::get('dashboard/help', 'HelpController@index');
 Route::post('dashboard/help', 'HelpController@do_help');
 public function import_csv($num)
 {
     $input = Input::file('csvfile');
     $ext = pathinfo($input->getClientOriginalName(), PATHINFO_EXTENSION);
     $rules = array('csvfile' => 'required');
     $validator = Validator::make(Input::all(), $rules);
     if ($validator->fails()) {
         $error = array('files' => array(array('error' => $validator->messages()->first(), 'name' => 'none', 'size' => 'none', 'url' => 'none', 'thumbnail_url' => 'none', 'delete_url' => 'none', 'delete_type' => 'DELETE')));
         return Response::json($error);
     } elseif ($ext != 'csv') {
         $error = array('files' => array(array('error' => 'Only CSV files are allowed', 'name' => 'none', 'size' => 'none', 'url' => 'none', 'thumbnail_url' => 'none', 'delete_url' => 'none', 'delete_type' => 'DELETE')));
         return Response::json($error);
     } else {
         $destination = 'imports';
         $filename = date('Y_m_d_H_i') . '_subscribers.' . $ext;
         $filepath = asset('imports/' . $filename);
         if ($input->move($destination, $filename)) {
             $csvpath = public_path() . '/imports/' . $filename;
             $processed = CsvManipulation::csvToArray($csvpath);
             $thecsv = $processed['uniqueSubscribers'];
             $rules = array('first_name' => 'required|alpha_num|max:128', 'last_name' => 'required|alpha_num|max:128', 'email' => 'required|email|max:255|unique:subscribers');
             $length = count($thecsv);
             $failedValidation = 0;
             for ($i = 0; $i < $length; $i++) {
                 $validator = Validator::make($thecsv[$i], $rules);
                 if ($validator->fails()) {
                     unset($thecsv[$i]);
                     $failedValidation++;
                 }
             }
             $newcsvarray = array_values($thecsv);
             $newlength = count($newcsvarray);
             $timestamp = date("Y-m-d H:i:s");
             $defaults = ['active' => 1, 'created_at' => $timestamp, 'updated_at' => $timestamp];
             $arrayforinsert = [];
             foreach ($newcsvarray as $key => $value) {
                 $arrayforinsert[] = array_merge($value, $defaults);
             }
             if (count($arrayforinsert) > 0) {
                 Subscriber::insert($arrayforinsert);
                 $list = Addressbook::find(1);
                 $subscribers = Subscriber::get();
                 foreach ($subscribers as $key => $subscriber) {
                     $list->subscribers()->attach($subscriber->id);
                 }
                 $successful = Subscriber::where('created_at', '=', $timestamp)->count();
                 $successMsg = "Total no. of subscribers successfully imported: <b>" . $successful . "</b><br />" . "Total no. of rows in the CSV file: <b>" . $processed['allRows'] . "</b><br />" . "Total no. of rows with the required 3 fields: <b>" . $processed['rowsWithThreeFields'] . "</b><br />" . "Total no. of unique emails in the CSV file: <b>" . $processed['noOfUniqueEmails'] . "</b><br />" . "Total no. of failed validations: <b>" . $failedValidation . "</b>";
                 $success = array('files' => array(array('success' => $successMsg, 'name' => $filename, 'size' => 'none', 'url' => $filepath, 'thumbnail_url' => 'none', 'delete_url' => 'none', 'delete_type' => 'DELETE')));
                 return Response::json($success);
             } else {
                 $error = array('files' => array(array('error' => 'None of the subscribers has been imported. Kindly try again.', 'name' => 'none', 'size' => 'none', 'url' => 'none', 'thumbnail_url' => 'none', 'delete_url' => 'none', 'delete_type' => 'DELETE')));
                 return Response::json($error);
             }
         } else {
             return Response::json(array('error' => "An error was encountered. Kindly refresh and try again."));
         }
     }
 }
 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 index()
 {
     $minutes = 10;
     //30
     $user = Sentry::getUser();
     $sitename = Cache::remember('sitename', $minutes, function () {
         return Setting::first()->pluck('sitename');
     });
     $subscribers = Cache::remember('subscribers', $minutes, function () {
         return Subscriber::orderBy('id', 'desc')->get();
     });
     $active_subs = Cache::remember('active_subs', $minutes, function () {
         return Subscriber::where('active', '=', 1)->count();
     });
     $inactive_subs = Cache::remember('inactive_subs', $minutes, function () {
         return Subscriber::where('active', '=', 0)->count();
     });
     $percent_active = 0;
     $percent_inactive = 0;
     if ($subscribers->count() > 0) {
         $percent_active = round($active_subs / $subscribers->count() * 100);
         $percent_inactive = round($inactive_subs / $subscribers->count() * 100);
     }
     $emails_num = Cache::remember('emails_num', $minutes, function () {
         return Email::with('trackers')->count();
     });
     $email_impressions = Cache::remember('email_impressions', $minutes, function () {
         return Tracker::count();
     });
     $read_emails = Cache::remember('read_emails', $minutes, function () {
         return Tracker::where('read', '=', 1)->count();
     });
     $unread_emails = Cache::remember('unread_emails', $minutes, function () {
         return Tracker::where('read', '=', 0)->count();
     });
     $bounced_emails = Cache::remember('bounced_emails', $minutes, function () {
         return Tracker::where('bounced', '=', 1)->count();
     });
     $unsubscribed_emails = Cache::remember('unsubscribed_emails', $minutes, function () {
         return Tracker::where('unsubscribed', '=', 1)->count();
     });
     $percent_read = 0;
     $percent_unread = 0;
     $percent_bounced = 0;
     $percent_unsubscribed = 0;
     if ($email_impressions > 0) {
         $percent_read = round($read_emails / $email_impressions * 100);
         $percent_unread = round($unread_emails / $email_impressions * 100);
         $percent_bounced = round($bounced_emails / $email_impressions * 100);
         $percent_unsubscribed = round($unsubscribed_emails / $email_impressions * 100);
     }
     $browser_outof = Cache::remember('browser_outof', $minutes, function () {
         return DB::table('trackers')->select('browser', DB::raw('count(*) as total'))->groupBy('browser')->where('read', '=', 1)->orderBy('total', 'desc')->get();
     });
     $browsers_emails = Cache::remember('browsers_emails', $minutes, function () {
         return DB::table('trackers')->select('browser', DB::raw('count(*) as total'))->groupBy('browser')->where('read', '=', 1)->orderBy('total', 'desc')->take(5)->get();
     });
     $platform_outof = Cache::remember('platform_outof', $minutes, function () {
         return DB::table('trackers')->select('platform', DB::raw('count(*) as total'))->groupBy('platform')->where('read', '=', 1)->orderBy('total', 'desc')->get();
     });
     $platforms_emails = Cache::remember('platform_emails', $minutes, function () {
         return DB::table('trackers')->select('platform', DB::raw('count(*) as total'))->groupBy('platform')->where('read', '=', 1)->orderBy('total', 'desc')->take(5)->get();
     });
     $percent_browsers = [];
     $percent_platforms = [];
     if ($read_emails > 0) {
         foreach ($browsers_emails as $key => $browser) {
             $percent_browsers[] = round($browser->total / $read_emails * 100);
         }
         foreach ($platforms_emails as $key => $platform) {
             $percent_platforms[] = round($platform->total / $read_emails * 100);
         }
     }
     $subs_unsubscribed = Cache::remember('subs_unsubscribed', $minutes, function () {
         return Tracker::where('unsubscribed', '=', 1)->groupBy('subscriber_id')->get()->count();
     });
     $self_deactivated = 0;
     if ($inactive_subs > 0) {
         $self_deactivated = round($subs_unsubscribed / $inactive_subs * 100);
     }
     $admin_deactivated = 100 - $self_deactivated;
     return View::make('dashboard.index', array('user' => $user, 'sitename' => $sitename, 'emails_num' => $emails_num, 'impressions' => $email_impressions, 'read_emails' => $percent_read, 'unread_emails' => $percent_unread, 'bounced_emails' => $percent_bounced, 'unsubscribed_emails' => $unsubscribed_emails, 'browsers_emails' => $browsers_emails, 'browser_outof' => count($browser_outof), 'browsers_array' => $percent_browsers, 'platforms_emails' => $platforms_emails, 'platform_outof' => count($platform_outof), 'platforms_array' => $percent_platforms, 'subscribers' => $subscribers, 'active_subs' => $percent_active, 'inactive_subs' => $percent_inactive, 'inactive_outof' => $inactive_subs, 'self_deactivated' => $self_deactivated, 'admin_deactivated' => $admin_deactivated));
 }
 public function import()
 {
     $validation = Jamesy\MyValidations::validate(Input::all(), $this->fileRules);
     if ($validation != NULL) {
         return Redirect::back()->withErrors($validation);
     } 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');
         } else {
             $import = new Jamesy\Imports($input, $this->rules);
             $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];
                 if (count($passedArr)) {
                     Subscriber::insert($passedArr);
                     $list = Maillist::find(1);
                     $list->touch();
                     $subscribers = Subscriber::where('created_at', $timestamp)->where('updated_at', $timestamp)->get();
                     foreach ($subscribers as $key => $subscriber) {
                         $list->subscribers()->attach($subscriber->id);
                     }
                 }
                 $message = "<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::back()->withSuccess($message);
             } else {
                 return Redirect::back()->withIssues($importResult);
             }
         }
     }
 }
 public function getRecharge()
 {
     $accounts = Subscriber::where('is_admin', 0)->where('plan_type', PREPAID_PLAN)->lists('uname', 'id');
     return View::make('admin.vouchers.recharge')->with('plans', Plan::lists('name', 'id'))->with('accounts', $accounts);
 }