public function countTotal()
 {
     $ads = Ad_Web::all();
     $iterator = 0;
     foreach ($ads as $ad) {
         $iterator += count(glob(base_path('myFiles/' . $ad->id . '/*.txt'), GLOB_BRACE));
     }
     return $iterator;
 }
 public function ReportsAjaxAd(Request $request, $id, $idweb)
 {
     if ($idweb == 0) {
         if ($id == 'all') {
             if ($request->ajax()) {
                 $ads = Web::all();
                 $reports = Report::ReportHome();
                 return response()->json(["web" => $reports, 'ads' => $ads]);
             }
         } else {
             if ($request->ajax()) {
                 $ad = Ad::find($id);
                 $ad_web = Ad_Web::find($ad->id);
                 $ads = Report::AdAjaxReport($ad_web->id);
                 $reports = Report::ReportAd($ad->id);
                 return response()->json(["web" => $reports, "ads" => $ads]);
             }
         }
     } else {
         if ($id == 'all') {
             if ($request->ajax()) {
                 $ads = Web::all();
                 $reports = Report::ReportHome();
                 return response()->json(["web" => $reports, 'ads' => $ads]);
             }
         } else {
             if ($request->ajax()) {
                 $ad = Ad::find($id);
                 $ad_web = Ad_Web::find($ad->id);
                 $web = Web::find($idweb);
                 $ads = Report::WebAjaxReport($ad_web->id);
                 $reports = Report::ReportAdWeb($ad->id, $web->id);
                 return response()->json(["web" => $reports, "ads" => $ads]);
             }
         }
     }
 }
 public function ReportsDate(Request $request)
 {
     if ($request->ajax()) {
         $date = date('Y-m-d', strtotime($request->input('date')));
         $todate = date('Y-m-d', strtotime($request->input('todate')));
         $idad = $request->input('ad');
         $idweb = $request->input('web');
         if ($idad == 0) {
             if ($idweb == 'all') {
                 if ($request->ajax()) {
                     $reports = Report::TimeReportClient($date, $todate);
                     return response()->json(["web" => $reports]);
                 }
             } else {
                 if ($request->ajax()) {
                     if ($idad == 'all') {
                         if ($request->ajax()) {
                             $reports = Report::TimeReportClient($date, $todate);
                             return response()->json(["web" => $reports]);
                         }
                     } else {
                         $ad = Web::find($idweb);
                         $reports = Report::ReportTimeClient($date, $todate, $ad->id);
                         return response()->json(["web" => $reports]);
                     }
                 }
             }
         } else {
             if ($idad == 'all' || $idweb == 'all') {
                 if ($request->ajax()) {
                     $reports = Report::TimeReportClient($date, $todate);
                     return response()->json(["web" => $reports]);
                 }
             } else {
                 if ($request->ajax()) {
                     $ad = Ad_Web::find($idad);
                     $web = Web::find($idweb);
                     $reports = Report::TimeReportCompleteClient($date, $todate, $ad->id, $web->id);
                     return response()->json(["web" => $reports]);
                 }
             }
         }
     }
 }
 function borrar()
 {
     $ads = Ad_Web::all();
     foreach ($ads as $ad) {
         $iterator = count(glob(base_path('myFiles/' . $ad->id . '/*.txt'), GLOB_BRACE));
         $ad = Ad_Web::find($ad->id);
         //$fecha = date('Y-m-d',strtotime(Carbon::today()));
         $reporte = new Report();
         $reporte->ad_web_id = $ad->id;
         $reporte->impresiones = $iterator;
         $reporte->fecha = Carbon::today()->subDay(1)->addMinutes(1);
         $reporte->save();
         $ad->created_at = Carbon::tomorrow();
         $ad->save();
         $files = glob(base_path('myFiles/' . $ad->id . '/*.txt'));
         // get all file names
         foreach ($files as $file) {
             // iterate files
             if (is_file($file)) {
                 unlink($file);
             }
             // delete file
         }
     }
 }
 /**
  * Remove the specified resource from storage.
  *
  * @param  int  $id
  * @return Response
  */
 public function destroy($id)
 {
     $placement = Ad_Web::find($id);
     $delete = $placement->delete();
     if ($delete) {
         notify()->flash('Placement destroy correctly', 'success', ['timer' => 3000, 'text' => '']);
     } else {
         notify()->flash('There was a problem deleting the placement', 'error', ['timer' => 3000, 'text' => '']);
     }
     return redirect()->to('admin/placement');
 }
 public function code(Request $request)
 {
     $ad = Ad_Web::find($request->get('id_ad'));
     return response()->json($ad);
 }
 /**
  * Display a listing of the resource.
  *
  * @return Response
  */
 public function index()
 {
     $startDate = new \DateTime(Carbon::parse(Carbon::today()->subDay()));
     $endDate = new \DateTime(Carbon::parse(Carbon::today()->subMinutes(1)));
     $metrics = 'ga:pageviews,ga:totalEvents';
     //$analytics = \LaravelAnalytics::performQuery($startDate, $endDate, $metrics, $others = array( 'dimensions' => 'ga:country'));
     //$analytics = \LaravelAnalytics::getMostVisitedPages($numberOfDays = 1, $maxResults = 20);
     $analytics = \LaravelAnalytics::performQuery($startDate, $endDate, $metrics, $others = array("dimensions" => "ga:pagePath", "sort" => "-ga:pageviews"));
     $ads = Ad_Web::all();
     $pila = array();
     $ids = array();
     $a = 0;
     for ($x = 0; $x < count($analytics["rows"]); $x++) {
         $comma_separated = implode("/", $analytics['rows'][$x]);
         $pieces = explode("/", $comma_separated);
         //primer error si no existe el imp osea esta / o advertiserw
         foreach ($ads as $ad) {
             if ($a == 0) {
                 array_push($ids, $ad->id);
             }
             if (empty($pieces[1])) {
             } else {
                 if ($pieces[2] == $ad->id) {
                     array_push($pila, $ad->id);
                     // echo "id = $pieces[2]"." Impresiones = $pieces[3]<br/ >";
                     $reporte = new Report();
                     $reporte->ad_web_id = $ad->id;
                     if (empty($pieces[3])) {
                         $reporte->impresiones = 0;
                         $reporte->ctr = 0;
                         $reporte->click = 0;
                     } else {
                         $reporte->impresiones = $pieces[3];
                         $reporte->ctr = $pieces[4] / $pieces[3];
                         $reporte->click = $pieces[4];
                     }
                     $reporte->fecha = Carbon::today()->subDay(1)->addMinutes(1);
                     $reporte->save();
                     $ad->created_at = Carbon::tomorrow();
                     $ad->save();
                 }
             }
         }
         $a++;
     }
     $result = array_diff($ids, $pila);
     foreach ($result as $r) {
         $ad = Ad_Web::find($r);
         $reporte = new Report();
         $reporte->ad_web_id = $ad->id;
         $reporte->impresiones = 0;
         $reporte->click = 0;
         $reporte->ctr = 0;
         $reporte->fecha = Carbon::today()->subDay(1)->addMinutes(1);
         $reporte->save();
         $ad->created_at = Carbon::tomorrow();
         $ad->save();
     }
     /*
             $pieces = array();
             foreach ($analytics['rows'] as $row){
                 $pieces = array_merge(explode("/imp/",$row[0]),$pieces);
             }
             for($x = 0 ; $x < count($pieces); $x++){
                 foreach($ads as $ad){
                     if($ad->id == $pieces[$x]){
                         echo $pieces[$x]."<br/>";
                     }
                 }
             }*/
 }