Skip to content

Instantly share code, notes, and snippets.

@patrickmaciel
Created January 11, 2012 11:47
Show Gist options
  • Save patrickmaciel/1594319 to your computer and use it in GitHub Desktop.
Save patrickmaciel/1594319 to your computer and use it in GitHub Desktop.
Não redireciona, fica na mesma página e aparece a mensagem de 'salvo com sucesso'
<?php
class IncentivesController extends AppController {
var $name = 'Incentives';
var $uses = array('Incentive');
var $components = array('Imagem');
var $helpers = array('Time', 'Text');
function index() {
$this->Incentive->recursive = 0;
$this->set('incentives', $this->paginate());
}
function view($id = null) {
if (!$id) {
$this->Session->setFlash(__('Invalid incentive', true));
$this->redirect(array('action' => 'index'));
}
$this->set('incentive', $this->Incentive->read(null, $id));
}
function admin_index() {
$this->Incentive->recursive = 0;
$this->set('incentives', $this->paginate());
}
function admin_view($id = null) {
if (!$id) {
$this->Session->setFlash(__('Invalid incentive', true));
$this->redirect(array('action' => 'index'));
}
$this->set('incentive', $this->Incentive->read(null, $id));
}
function admin_add() {
if (!empty($this->data)) {
$this->__upar('imagem_principal');
for($i = 1; $i <= 10; $i++) {
$this->__upar('imagem_' . $i);
}
$this->Incentive->create();
if ($this->Incentive->save($this->data)) {
$this->Session->setFlash(__('The incentive has been saved', true));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The incentive could not be saved. Please, try again.', true));
}
}
}
function admin_edit($id = null) {
if (!$id && empty($this->data)) {
$this->Session->setFlash(__('Invalid incentive', true));
$this->redirect(array('action' => 'index'));
}
if (!empty($this->data)) {
$this->__upar('imagem_principal');
for($i = 1; $i <= 10; $i++) {
$this->__upar('imagem_' . $i);
}
if ($this->Incentive->save($this->data)) {
$this->Session->setFlash(__('The incentive has been saved', true));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The incentive could not be saved. Please, try again.', true));
}
}
if (empty($this->data)) {
$this->data = $this->Incentive->read(null, $id);
}
}
function admin_delete($id = null) {
if (!$id) {
$this->Session->setFlash(__('Invalid id for incentive', true));
$this->redirect(array('action'=>'index'));
}
$this->Incentive->id = $id;
$incentive = $this->Incentive->read(null, $id);
if($this->Incentive->delete($id)) {
$this->Imagem->delete_image('resize_' . $incentive['Incentive']['imagem_principal'], 'img/uploads');
$this->Imagem->delete_image('crop_' . $incentive['Incentive']['imagem_principal'], 'img/uploads');
for($i = 1; $i <= 5; $i++) {
$this->Imagem->delete_image('resize_' . $incentive['Incentive']['imagem_' . $i], 'img/uploads');
$this->Imagem->delete_image('crop_' . $incentive['Incentive']['imagem_' . $i], 'img/uploads');
}
$this->Session->setFlash('Incentivo deletado com sucesso.', 'default', array('class' => 'message success'));
$this->redirect(array('action' => 'index'));
}
$this->Session->setFlash(__('incentive was not deleted', true));
$this->redirect(array('action' => 'index'));
}
function admin_search() {
$words = isset($this->data['Incentive ']['searchword']) ? $this->data['Incentive ']['searchword'] : null;
if (!$words) {
$this->Session->setFlash(__('Invalid parameters.', true));
// $this->Incentive->recursive = 0;
// $this->set('Incentive s', $this->paginate());
$this->set('incentives', array());
// $this->redirect(array('action' => 'index'));
} else
{
$this->set('incentives', $this->Incentive->find('all', array('conditions'=>array(
'or'=>array(
array('Incentive .name LIKE'=>"%$words%")
)
))));
}
}
function admin_active($value = null, $id = null) {
if (($value == null) || ($id == null)) {
$this->Session->setFlash(__('Invalid value', true));
$this->redirect(array('action' => 'index', 'admin'=>true));
}
else
{
if($value==0)
{
$this->Incentive->updateAll(array((Inflector::singularize($this->name)) . ".active" => 1), array((Inflector::singularize($this->name)) . ".id" => $id));
// $this->redirect(array('action' => 'index', 'admin'=>true));
$this->redirect($this->referer());
}
elseif($value==1)
{
$this->Incentive->updateAll(array((Inflector::singularize($this->name)) . ".active" => 0), array((Inflector::singularize($this->name)) . ".id" => $id));
// $this->redirect(array('action' => 'index', 'admin'=>true));
$this->redirect($this->referer());
}
else
{
$this->Session->setFlash(__('Invalid value 2', true));
// $this->redirect(array('action' => 'index', 'admin'=>true));
$this->redirect($this->referer());
}
}
}
function __upar($imagem_nome) {
$img = array();
$img['imagem'] = $this->data['Incentive']["". $imagem_nome .""];
if($imagem_nome == 'imagem_principal') {
$img['resize_width'] = 768;
$img['resize_height'] = 512;
$img['crop_width'] = 105;
$img['crop_height'] = 61;
$img['fit'] = 'outside';
} else {
$img['resize_width'] = 768;
$img['resize_height'] = 512;
$img['crop_width'] = 125;
$img['crop_height'] = 82;
$img['fit'] = 'inside';
}
if( ($this->data['Incentive']["". $imagem_nome .""]['name'] != null) &&
($this->data['Incentive']["". $imagem_nome .""]['name'] != "")) {
// Executando o upload
$this->data['Incentive']["". $imagem_nome .""] = $this->Imagem->upload($img);
if( ($this->data['Incentive']["". $imagem_nome .""] == null) || ($this->data['Incentive']["". $imagem_nome .""] == "") ) {
unset($this->data['Incentive']["". $imagem_nome .""]);
} else {
if(isset($this->data['Incentive']["" . $imagem_nome . "_old"])) {
// $this->Imagem->delete_image('resize_' . $this->data['Incentive']["" . $imagem_nome . "_old"], 'img/uploads');
// $this->Imagem->delete_image('crop_' . $this->data['Incentive']["" . $imagem_nome . "_old"], 'img/uploads');
}
}
// return $this->Imagem->upload($img);
} else {
unset($this->data['Incentive']["". $imagem_nome .""]);
// return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment