Created
October 21, 2015 01:32
-
-
Save possamai/73f9afd375f985b9fcf9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
App::uses('AppController', 'Controller'); | |
class FotosController extends AppController { | |
public $base_url = array('admin'=>true, 'controller' => 'fotos', 'action' => 'admin_index'); | |
var $component_name = 'Foto'; | |
public function beforeFilter() { | |
$this->set('title_for_layout', plural($this->component_name)); | |
$this->base_url['action'] = $this->request->params['action']; | |
$this->Foto->recursive = -1; | |
parent::beforeFilter(); | |
} | |
public function beforeRender() { | |
$this->set('base_url', $this->base_url ); | |
$this->Breadcrumb->addBreadcrumb(array('title' => plural($this->component_name), 'url' => array_merge($this->base_url, array('action'=>'index')) )); | |
parent::beforeRender(); | |
} | |
public function admin_index() { | |
$this->set('bodyClass', 'dynamic-gallery-page'); | |
$id_empresa = $this->verificaEmpresaLogada( array('admin'=>true, 'controller' => 'fotos', 'action' => 'index') ); | |
$this->Breadcrumb->addBreadcrumb(array('title' => $this->empresaLogada['Empresa']['nome_fantasia'])); | |
} | |
public function admin_lista() { | |
$arr_conditions = $this->Search->getCondition(); | |
$conditions = array(); | |
foreach($arr_conditions as $name=>$value) { | |
switch( $name ) { | |
case 'legenda': | |
case 'arquivo': | |
case 'filename': | |
$conditions[] = array('AND' => array("Foto.{$name} LIKE" => '%' . $value . '%') ); break; | |
default: | |
$conditions[] = array('AND' => array("Foto.{$name} =" => $value)); break; | |
} | |
} | |
$empresas = $this->Foto->Empresa->find('list'); | |
$this->set(compact('empresas')); | |
if (!isset($limit)) { $limit = 25; } | |
$this->paginate = array( 'conditions' => $conditions, 'limit' => $limit ); | |
$this->Foto->recursive = 0; | |
$this->set('fotos', $this->paginate()); | |
} | |
public function admin_cadastro($id = null) { | |
$id_empresa = $this->verificaEmpresaLogada( array('admin'=>true, 'controller' => 'fotos', 'action' => 'index') ); | |
$Foto = null; | |
if ($id<>null) { | |
if (!$this->Foto->exists($id)) { | |
throw new NotFoundException(__(singular($this->component_name).' inválido.')); | |
} else { | |
$Foto = $this->Foto->findById($id); | |
$this->Breadcrumb->addBreadcrumb(array('title' => $id.': '.$Foto['Foto'][$this->Foto->displayField], 'url' => array_merge($this->base_url,array($id)) )); | |
} | |
} | |
$this->Breadcrumb->addBreadcrumb(array('title' => 'Cadastro', 'url' => $this->base_url ) ); | |
$_old_arquivo = (($Foto<>null) ? $this->Foto->getPathFile('arquivo') . $Foto['Foto']['arquivo'] : ''); | |
if ($this->request->is(array('post', 'put'))) { | |
if ($this->Foto->save($this->request->data)) { | |
// Remove arquivos antigos | |
if ((is_file($_old_arquivo))&&(!empty($this->request->data['Foto']['arquivo']['name']))) { unlink($_old_arquivo); } | |
$this->Session->setFlash(__(singular($this->component_name).' gravado com sucesso.'), 'flash_ok'); | |
$this->redirectBeforeSaveForm(); // Direciona Form | |
} else { | |
$this->Session->setFlash(__(singular($this->component_name).' não pode ser gravado. Tente novamente.'), 'flash_error'); | |
} | |
} else { | |
$this->request->data = $Foto; | |
} | |
$empresas = $this->Foto->Empresa->find('list'); | |
$this->set(compact('empresas')); | |
} | |
public function admin_excluir($id = null) { | |
$this->Foto->id = $id; | |
if (!$this->Foto->exists()) { | |
throw new NotFoundException(__(singular($this->component_name).' inválido.')); | |
} | |
if ($this->Foto->delete()) { | |
$this->Session->setFlash(__(singular($this->component_name).' excluído.'), 'flash_ok'); | |
} else { | |
$this->Session->setFlash(__(singular($this->component_name).' não pode ser excluído.'), 'flash_error'); | |
} | |
$this->redirect(array('action' => 'index')); | |
} | |
public function admin_ajax() { | |
$this->autoRender = false; | |
$str_retorno = ''; | |
$data = $this->request->data; | |
$file = $this->params->form['arquivo']; | |
if ( (isset($data['tipo_acao'])) ) { | |
$boo = false; | |
$arr_erro = array(); | |
$arr_cadastro = array(); | |
switch( $data['tipo_acao'] ) { | |
case 'upload': | |
$empresa_id = $this->verificaEmpresaLogada( array('admin'=>true, 'controller' => 'fotos', 'action' => 'index') ); | |
$boo = $this->verifyAuth(0); | |
if (is_array($empresa_id) || !$boo) { // Erro | |
$boo = false; | |
$arr_erro['id'] = array('Sem permissão de acesso'); | |
$arr_retorno = array(); | |
} else { | |
$arr_cadastro['Foto']['empresa_id'] = $empresa_id; | |
$arr_cadastro['Foto']['arquivo'] = $file; | |
if ($this->Foto->save( $arr_cadastro )) { | |
$boo = true; | |
} else { | |
$arr_erro = $this->Foto->validationErrors; | |
} | |
} | |
$str_retorno = json_encode( utf8IsoConverter( array('status'=>$boo, | |
'retorno' => $arr_cadastro, | |
'error_input'=> ( $arr_erro )) ) | |
); | |
break; | |
case 'listar_fotos_empresa': | |
$empresa_id = $this->verificaEmpresaLogada( array('admin'=>true, 'controller' => 'fotos', 'action' => 'index') ); | |
if (is_array($empresa_id)) { // Erro | |
$arr_erro = $empresa_id; | |
$arr_retorno = array(); | |
} else { | |
$boo = true; | |
$conditions = array(); | |
$conditions[] = array('AND' => array("Foto.empresa_id =" => $empresa_id)); | |
$this->paginate = array( 'conditions' => $conditions ); | |
$this->Foto->recursive = -1; | |
$arr_retorno = $this->paginate(); | |
} | |
$str_retorno = json_encode( utf8IsoConverter( array('status'=>$boo, | |
'retorno' => $arr_retorno, | |
'error_input'=> ( $arr_erro )) ) | |
); | |
break; | |
case 'excluir_foto': | |
if (isset($data['id'])) { | |
$this->Foto->id = $data['id']; | |
if (!$this->Foto->exists()) { | |
$arr_erro['arquivo'] = array( __(singular($this->component_name).' inválido.') ); | |
} else { | |
$boo = $this->verifyAuth( $data['id'] ); | |
if ($boo) { | |
$Foto = $this->Foto->findById($data['id']); | |
if ($this->Foto->delete()) { | |
$boo = true; | |
@unlink( $this->Foto->getPathFile('arquivo') . $Foto['Foto']['arquivo'] ); | |
} else { | |
$arr_erro['arquivo'] = array( __(singular($this->component_name).' não foi excluído.') ); | |
} | |
} else { | |
$arr_erro['empresa_id'] = array('Sem permissão de acesso.'); | |
} | |
} | |
} else { | |
$arr_erro['id'] = array('Código não informado.'); | |
} | |
$str_retorno = json_encode( utf8IsoConverter( array('status'=>$boo, | |
'error_input'=> ( $arr_erro )) ) | |
); | |
break; | |
case 'editar_legenda': | |
if (isset($data['id'])) { | |
$this->Foto->id = $data['id']; | |
if (!$this->Foto->exists()) { | |
$arr_erro['arquivo'] = array( __(singular($this->component_name).' inválido.') ); | |
} else { | |
$arr_obj = unserializeCakeArrJquery('Foto', $data['formulario']); | |
$boo = $this->verifyAuth( $data['id'] ); | |
if ($boo) { | |
$Foto = $this->Foto->findById($data['id']); | |
if ($this->Foto->saveField('legenda', (isset($arr_obj['Foto']['legenda']) ? $arr_obj['Foto']['legenda'] : '') )) { | |
$boo = true; | |
} else { | |
$arr_erro = $this->Foto->validationErrors; | |
} | |
} else { | |
$arr_erro['empresa_id'] = array('Sem permissão de acesso.'); | |
} | |
} | |
} else { | |
$arr_erro['id'] = array('Código não informado.'); | |
} | |
$str_retorno = json_encode( utf8IsoConverter( array('status'=>$boo, | |
'retorno' => $arr_obj, | |
'error_input'=> ( $arr_erro )) ) | |
); | |
break; | |
case 'listar_fotos': | |
$fotos = array(); | |
if (isset($data['id'])) { | |
$boo = true; | |
$arr_conditions = array(); | |
$arr_conditions[] = array('AND' => array("Foto.empresa_id" => $data['id']) ); | |
$fotos = $this->Foto->find('all', array( | |
'conditions' => $arr_conditions, | |
'recursive' => -1, | |
'contain' => array('Usuario', 'Nivel') | |
)); | |
$fotos = $this->Foto->arrFoto($fotos, 'Foto', 'arquivo', null, 160, 120); | |
} else { | |
$arr_erro['id'] = array('Código da empresa não informado.'); | |
} | |
$str_retorno = json_encode( utf8IsoConverter( array('status'=>$boo, | |
'retorno' => $fotos, | |
'error_input'=> ( $arr_erro )) ) | |
); | |
break; | |
} | |
} else { | |
$str_retorno = 'Necessário informar tipo de busca.'; | |
} | |
echo utf8_encode( $str_retorno ); | |
die; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment