/**
  * Display a listing of the resource.
  *
  * @return \Illuminate\Http\Response
  */
 public function index()
 {
     // 1. Create new calendar
     $vCalendar = new \Eluceo\iCal\Component\Calendar(config('app.url'));
     $vCalendar->setName(config('mobilizator.name'));
     // returns actions from the last 60 days
     $actions = \App\Action::with('group')->where('start', '>=', Carbon::now()->subDays(60))->get();
     foreach ($actions as $action) {
         // 2. Create an event
         $vEvent = new \Eluceo\iCal\Component\Event();
         $vEvent->setDtStart($action->start);
         $vEvent->setDtEnd($action->stop);
         $vEvent->setSummary($action->name);
         $vEvent->setDescription(summary($action->body), 1000);
         $vEvent->setLocation($action->location);
         $vEvent->setUrl(action('ActionController@show', [$action->group->id, $action->id]));
         $vEvent->setUseUtc(false);
         //TODO fixme
         $vCalendar->addComponent($vEvent);
     }
     return response($vCalendar->render())->header('Content-Type', 'text/calendar; charset=utf-8')->header('Content-Disposition', 'attachment; filename="cal.ics"');
 }
 public function report($id)
 {
     $campaign = Campaign::find($id);
     $summary = [];
     $actions = Action::with('contact')->forCampaign($campaign)->orderBy('contact_id')->get();
     $summary['total_sent'] = $campaign->emails->count();
     $summary['opened'] = $actions->filter(function ($action) {
         return $action->action === 'opened';
     })->count();
     $summary['more info'] = $actions->filter(function ($action) {
         return $action->action === 'more info';
     })->count();
     $summary['website_visits'] = $actions->filter(function ($action) {
         return $action->action === 'website';
     })->count();
     $summary['skipped_video'] = $actions->filter(function ($action) {
         return $action->action === 'skipped';
     })->count();
     $summary['emailed_bill'] = $actions->filter(function ($action) {
         return $action->action === 'email';
     })->count();
     $summary['youtube_channel'] = $actions->filter(function ($action) {
         return $action->action === 'youtube';
     })->count();
     $summary['unsubscribed'] = Contact::where('unsubscribe', '=', 1)->count();
     \Excel::create('Report', function ($excel) use($actions, $summary, $campaign) {
         $excel->setTitle("eMail Report for {$campaign->name}")->setCreator('Exhibit Partners Mailer Service')->setCompany('Exhibit Partners')->setDescription("A detailed report of email recipients for the {$campaign->name} email campaign");
         $excel->sheet('Summary', function ($sheet) use($summary) {
             $sheet->row(1, ['Total', 'Opened', 'Went To Page 2', 'Website Visits', 'Skipped The Video', 'Emailed Bill', 'YouTube Channel', 'Unsubscribed']);
             $sheet->row(2, [$summary['total_sent'], $summary['opened'], $summary['more info'], $summary['website_visits'], $summary['skipped_video'], $summary['emailed_bill'], $summary['youtube_channel'], $summary['unsubscribed']]);
         });
         $excel->sheet('Recipient Actions', function ($sheet) use($actions) {
             $sheet->row(1, ['Contact', 'Email', 'Action']);
             foreach ($actions as $key => $action) {
                 $sheet->row($key + 2, [$action->contact->name, $action->contact->email, $action->action]);
             }
         });
     })->download('xlsx');
 }
 public function touchActions($id)
 {
     $touch = Touch::find($id);
     $actions = Action::with('contact')->where('touch_id', '=', $touch->id)->orderBy('action')->orderBy('contact_id', 'DESC')->get(['action', 'contact_id']);
     $actions = array_map(function ($action) {
         foreach ($action['contact'] as $key => $value) {
             $action[$key] = $value;
         }
         unset($action['contact_id'], $action['id'], $action['contact'], $action['unsubscribe'], $action['client_id'], $action['updated_at'], $action['created_at'], $action['bounced']);
         return $action;
     }, $actions->toArray());
     $metrics = Excel::create("{$touch->title} Metrics", function ($excel) use($touch, $actions) {
         $excel->setTitle("Email Metrics for {$touch->title}");
         $excel->setCreator("EP-Productions");
         $excel->setCompany('Exhibit Partners');
         $excel->setDescription("Email metrics from an email campaign for {$touch->campaign}->client->name by Exhibit Partners");
         $excel->sheet('Metrics', function ($sheet) use($actions) {
             $sheet->fromArray($actions);
         });
         $excel->sheet('Bounces', function ($sheet) use($touch) {
             $bounces = $touch->bounces->toArray();
             $bounces = array_map(function ($contact) {
                 unset($contact['id'], $contact['client_id'], $contact['bounced'], $contact['unsubscribe'], $contact['updated_at'], $contact['created_at']);
                 return $contact;
             }, $bounces);
             $sheet->fromArray($bounces);
         });
         $excel->sheet('Unsubscribes', function ($sheet) use($touch) {
             $unsubscribes = $touch->unsubscribes->toArray();
             $unsubscribes = array_map(function ($contact) {
                 unset($contact['id'], $contact['client_id'], $contact['bounced'], $contact['unsubscribe'], $contact['updated_at'], $contact['created_at']);
                 return $contact;
             }, $unsubscribes);
             $sheet->fromArray($unsubscribes);
         });
     })->download('xlsx');
     return $metrics;
 }
 public function agendaJson(Request $request)
 {
     // load of actions between start and stop provided by calendar js
     if ($request->has('start') && $request->has('end')) {
         $actions = \App\Action::with('group')->where('start', '>', Carbon::parse($request->get('start')))->where('stop', '<', Carbon::parse($request->get('end')))->orderBy('start', 'asc')->get();
     } else {
         $actions = \App\Action::with('group')->orderBy('start', 'asc')->get();
     }
     $event = '';
     $events = '';
     foreach ($actions as $action) {
         $event['id'] = $action->id;
         $event['title'] = $action->name;
         $event['description'] = $action->body . ' <br/> ' . $action->location;
         $event['body'] = filter($action->body);
         $event['summary'] = summary($action->body);
         $event['location'] = $action->location;
         $event['start'] = $action->start->toIso8601String();
         $event['end'] = $action->stop->toIso8601String();
         $event['url'] = action('ActionController@show', [$action->group->id, $action->id]);
         $event['group_url'] = action('ActionController@index', [$action->group->id]);
         $event['group_name'] = $action->group->name;
         $event['color'] = $action->group->color();
         $events[] = $event;
     }
     return $events;
 }