1. <?php
  2.  
  3. class BannersController extends BaseController {
  4.  
  5.     /**
  6.      * Banner Repository
  7.      *
  8.      * @var Banner
  9.      */
  10.     protected $banner;
  11.  
  12.     public function __construct(Banner $banner) {
  13.         $this->banner = $banner;
  14.     }
  15.  
  16.     /**
  17.      * Display a listing of the resource.
  18.      *
  19.      * @return Response
  20.      */
  21.     public function index() {
  22.         $banners = $this->banner->all();
  23.  
  24.         return View::make('banners.index', compact('banners'));
  25.     }
  26.  
  27.     /**
  28.      * Show the form for creating a new resource.
  29.      *
  30.      * @return Response
  31.      */
  32.     public function create() {
  33.         $sectionList = Section::getList();
  34.         $banner = new Banner;
  35.         return View::make('banners.create')->with(compact('sectionList', 'banner'));
  36.     }
  37.  
  38.     /**
  39.      * Store a newly created resource in storage.
  40.      *
  41.      * @return Response
  42.      */
  43.     public function store() {
  44.         $input = array_except(Input::all(), array('_method', 'q'));
  45.         $destinationPath = '/tmp';
  46.  
  47.         if (Input::hasFile('image')) {
  48.             $extension = Input::file('image')->getClientOriginalExtension();
  49.             $name = Input::file('image')->getClientOriginalName();
  50.             $fileName = $name; //.'.'.$extension;
  51.             Input::file('image')->move($destinationPath, $fileName);
  52.             $input['image'] = $destinationPath . DIRECTORY_SEPARATOR . $fileName;
  53.         } else {
  54.  
  55.         }
  56.  
  57.         if (empty($input['current'])) {
  58.             $input['current'] = 0;
  59.         }
  60.  
  61.         $validation = Validator::make($input, Banner::$rules);
  62.  
  63.         if ($validation->passes()) {
  64.             $this->banner->create($input);
  65.  
  66.             return Redirect::route('banners.index');
  67.         }
  68.  
  69.         return Redirect::route('banners.create')
  70.                         ->withInput()
  71.                         ->withErrors($validation)
  72.                         ->with('message', 'There were validation errors.');
  73.     }
  74.  
  75.     /**
  76.      * Display the specified resource.
  77.      *
  78.      * @param  int  $id
  79.      * @return Response
  80.      */
  81.     public function show($id) {
  82.         $banner = $this->banner->findOrFail($id);
  83.  
  84.         return View::make('banners.show', compact('banner'));
  85.     }
  86.  
  87.     /**
  88.      * Show the form for editing the specified resource.
  89.      *
  90.      * @param  int  $id
  91.      * @return Response
  92.      */
  93.     public function edit($id) {
  94.         $banner = $this->banner->find($id);
  95.  
  96.         if (is_null($banner)) {
  97.             return Redirect::route('banners.index');
  98.         }
  99.  
  100.         $sectionList = Section::getList();
  101.         return View::make('banners.edit', compact('banner', 'sectionList'));
  102.     }
  103.  
  104.     /**
  105.      * Update the specified resource in storage.
  106.      *
  107.      * @param  int  $id
  108.      * @return Response
  109.      */
  110.     public function update($id) {
  111.         $input = array_except(Input::all(), array('_method', 'q'));
  112.         $destinationPath = '/tmp';
  113.  
  114.         if (Input::hasFile('image')) {
  115.             $extension = Input::file('image')->getClientOriginalExtension();
  116.             $name = Input::file('image')->getClientOriginalName();
  117.             $fileName = $name; //.'.'.$extension;
  118.             Input::file('image')->move($destinationPath, $fileName);
  119.             $input['image'] = $destinationPath . DIRECTORY_SEPARATOR . $fileName;
  120.         } else {
  121.             unset($input['image']);
  122.         }
  123.  
  124.         if (empty($input['current'])) {
  125.             $input['current'] = 0;
  126.         }
  127.  
  128.         $validation = Validator::make($input, Banner::$rules);
  129.  
  130.         if ($validation->passes()) {
  131.             $banner = $this->banner->find($id);
  132.             $banner->update($input);
  133.  
  134.             return Redirect::route('banners.show', $id);
  135.         }
  136.  
  137.         return Redirect::route('banners.edit', $id)
  138.                         ->withInput()
  139.                         ->withErrors($validation)
  140.                         ->with('message', 'There were validation errors.');
  141.     }
  142.  
  143.     /**
  144.      * Remove the specified resource from storage.
  145.      *
  146.      * @param  int  $id
  147.      * @return Response
  148.      */
  149.     public function destroy($id) {
  150.         $this->banner->find($id)->delete();
  151.  
  152.         return Redirect::route('banners.index');
  153.     }
  154.  
  155.     // otros
  156.     /**
  157.      * Show the report for the specified resource.
  158.      *
  159.      * @param  int  $id
  160.      * @return Response
  161.      */
  162.     public function report($id, $month = null, $year = null) {
  163.         if (is_null($month)) {
  164.             $month = date('m');
  165.         }
  166.         if (is_null($year)) {
  167.             $year = date('Y');
  168.         }
  169.         $banner = $this->banner->find($id);
  170.  
  171.         if (is_null($banner)) {
  172.             return Redirect::route('banners.index');
  173.         }
  174.  
  175.         return View::make('banners.report', compact('banner', 'month', 'year'));
  176.     }
  177.  
  178.     /**
  179.      * Actualizar los banners según el día y la fecha que tengan
  180.      */
  181.     public function getUpdate() {
  182.         $banners = Banner::all();
  183.         foreach ($banners as $banner) {
  184.             $banner->current = $banner->isCurrent();
  185.             $banner->save();
  186.         }
  187.         $banners = Banner::all();
  188.  
  189.         Session::flash('message', 'Banners actualizados!');
  190.         return Redirect::route('banners.index');
  191.     }
  192.  
  193. }