Skip to content

Instantly share code, notes, and snippets.

@Sigmus
Created August 22, 2014 19:19
Show Gist options
  • Save Sigmus/ac506d21e87a0dfbf891 to your computer and use it in GitHub Desktop.
Save Sigmus/ac506d21e87a0dfbf891 to your computer and use it in GitHub Desktop.
<?php
class Destaque extends Aware {
const STATUS_PUBLISHED = 1;
const STATUS_UNPUBLISHED = 0;
public static $rules = array(
'link' => 'required|url'
);
public static $messages = array(
'required' => 'Campo obrigatório',
'url' => 'Digite uma URL válida'
);
public static $files = array('imagem');
public function upload()
{
return $this->queryUpload()->first();
}
public function queryUpload()
{
return UploadFoto::where('eloquent_name', '=', 'destaque')
->where('eloquent_id', '=', $this->id)
->order_by('created_at', 'desc');
}
public function defaultQuery()
{
return static::where_temp(0)->order_by('ordem', 'asc');
}
public function populate_form()
{
return array(
'eloquent' => $this,
);
}
public static function createTemp()
{
static::removeTemps();
$aware = new self;
$aware->force_save();
return $aware;
}
public static function removeTemps()
{
$destaque = static::where_temp(1)->first();
if ($destaque == null) {
return;
}
UploadFoto::where('eloquent_id', '=', $destaque->id)->where('eloquent_name', '=', 'destaque')->delete();
$destaque->delete();
}
public function onSave()
{
if ($this->exists)
{
$this->temp = false;
$this->ordem = $this->defaultQuery()->count();
}
return true;
}
public function publish()
{
$this->published = static::STATUS_PUBLISHED;
return $this->force_save();
}
public function unpublish()
{
$this->published = static::STATUS_UNPUBLISHED;
return $this->force_save();
}
public function mudarOrdem($ordem)
{
$this->ordem = $ordem;
$this->force_save();
foreach ($this->outras()->get() as $index => $destaque)
{
$novaOrdem = $index;
if ($index >= $ordem)
{
$novaOrdem = $index + 1;
}
$destaque->ordem = $novaOrdem;
$destaque->force_save();
}
}
public function resetarOrdem()
{
foreach ($this->outras()->get() as $index => $destaque)
{
$destaque->ordem = $index;
$destaque->force_save();
}
}
public function outras()
{
return $this->defaultQuery()->where('id', '<>', $this->id);
}
}
@layout('admin.index')
@section('pagina')
@include('admin.js-operation')
<a href="/admin/destaque/create" class="btn">Criar novo</a>
<div class="row-fluid">&nbsp;</div>
<table class="table table-striped table-bordered" data-script="destaque-list">
<thead>
<tr>
<th class="span4">Imagem</th>
<th class="span4">Link</th>
<th>
Publicada no site?
</th>
<th>&nbsp;</th>
</tr>
</thead>
<tbody>
@foreach ($destaques as $index => $destaque)
<tr>
<td>
<a href="/admin/destaque/{{ $destaque->id }}">
<img src="/uploads/{{ $destaque->upload()->arquivo }}" width="80" class="clearfix">
</a>
@if ($index > 0)
<a href="/admin/destaque/ordem/{{ $destaque->id }}/{{ $index - 1}}" class="btn btn-small">
<i class="icon icon-hand-up">&nbsp;</i>
</a>
@endif
@if ($index < count($destaques) - 1)
<a href="/admin/destaque/ordem/{{ $destaque->id }}/{{ $index + 1 }}" class="btn btn-small">
<i class="icon icon-hand-down">&nbsp;</i>
</a>
@endif
</td>
<td>
<a href="/admin/destaque/{{ $destaque->id }}">
{{ $destaque->link }}
</a>
</td>
<td>
@if ($destaque->published)
<label class="label label-success">Sim</label>
@else
<label class="label">Não</label>
@endif
</td>
<td>
<a class="remover btn btn-small btn-danger" data-publicada="{{ $destaque->published }}" href="/admin/destaque/remover/{{ $destaque->id }}">Remover</a>
</td>
</tr>
@endforeach
</tbody>
</table>
@endsection
<?php
Route::get('/admin/destaque/ordem/(:any)/(:any)', function($destaque_id, $ordem)
{
$destaque = Destaque::find($destaque_id);
$destaque->mudarOrdem($ordem);
return Redirect::to('/admin/destaque');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment