Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- App::uses('AppModel', 'Model');
- /**
- * Anuncio Model
- *
- * @property Contrato $Contrato
- * @property Veiculo $Veiculo
- * @property AnuncioFoto $AnuncioFoto
- */
- class Anuncio extends AppModel {
- const AUTOMOVEIS = 3;
- const MOTOS = 4;
- const CAMINHOES_ONIBUS = 6;
- const NAUTICA = 5;
- const SERVICOS = 160;
- const PECAS_ACESSORIOS = 159;
- const RARIDADES = 7;
- const TUNADOS = 8;
- public $actsAs = array('Containable');
- /**
- * Validation rules
- *
- * @var array
- */
- public $validate = array(
- 'contrato_id' => array(
- 'numeric' => array(
- 'rule' => array('numeric'),
- //'message' => 'Your custom message here',
- //'allowEmpty' => false,
- //'required' => false,
- //'last' => false, // Stop validation after this rule
- //'on' => 'create', // Limit validation to 'create' or 'update' operations
- ),
- ),
- 'foreign_key' => array(
- 'numeric' => array(
- 'rule' => array('numeric'),
- //'message' => 'Your custom message here',
- //'allowEmpty' => false,
- //'required' => false,
- //'last' => false, // Stop validation after this rule
- //'on' => 'create', // Limit validation to 'create' or 'update' operations
- ),
- ),
- 'ativo' => array(
- 'boolean' => array(
- 'rule' => array('boolean'),
- //'message' => 'Your custom message here',
- //'allowEmpty' => false,
- //'required' => false,
- //'last' => false, // Stop validation after this rule
- //'on' => 'create', // Limit validation to 'create' or 'update' operations
- ),
- ),
- );
- //The Associations below have been created with all possible keys, those that are not needed can be removed
- /**
- * belongsTo associations
- *
- * @var array
- */
- public $belongsTo = array(
- 'Contrato' => array(
- 'className' => 'Contrato',
- 'foreignKey' => 'contrato_id',
- 'conditions' => '',
- 'fields' => '',
- 'order' => '',
- 'counterCache' => true,
- 'counterScope' => array(
- 'Anuncio.ativo' => true,
- 'Veiculo.antigo' => false,
- 'Veiculo.ativo' => true
- )
- ),
- 'Veiculo' => array(
- 'className' => 'Veiculo',
- 'foreignKey' => 'foreign_key',
- 'conditions' => '',
- 'fields' => '',
- 'order' => ''
- ),
- 'PecaAcessorio' => array(
- 'className' => 'PecaAcessorio',
- 'foreignKey' => 'foreign_key',
- 'conditions' => '',
- 'fields' => '',
- 'order' => ''
- ),
- 'Servico' => array(
- 'className' => 'Servico',
- 'foreignKey' => 'foreign_key',
- 'conditions' => '',
- 'fields' => '',
- 'order' => ''
- )
- );
- /**
- * hasMany associations
- *
- * @var array
- */
- public $hasMany = array(
- 'AnuncioFoto' => array(
- 'className' => 'AnuncioFoto',
- 'foreignKey' => 'anuncio_id',
- 'dependent' => false,
- 'conditions' => '',
- 'fields' => '',
- 'order' => '',
- 'limit' => '',
- 'offset' => '',
- 'exclusive' => '',
- 'finderQuery' => '',
- 'counterQuery' => ''
- )
- );
- /*
- * action findMaisBuscados
- * criado por: Patrick Maciel - patrickmaciel.info@gmail.com - www.patrickmaciel.com
- * data: 19-03-2012
- */
- public function findMaisBuscados($quantidade,$params = array(), $order = null, $options = null){
- $paramsThumbs = array(
- 'recursive' => -1,
- 'joins' => $this->getInformacoesCompletasJoins(),
- 'limit' => $quantidade,
- 'order' => array(
- 'Anuncio.buscado DESC',
- 'Anuncio.visualizacoes DESC'
- ),
- 'fields' => array(
- 'Anuncio.created',
- 'Anuncio.buscado',
- 'Anuncio.visualizacoes',
- 'Marca.nome',
- 'Versao.nome',
- 'Modelo.nome',
- 'Veiculo.id',
- 'Veiculo.placa',
- 'Veiculo.ano_modelo',
- 'Veiculo.preco',
- 'Combustivel.id',
- 'Cor.nome',
- 'Veiculo.categoria_id',
- 'Categoria.model'
- )
- );
- $params = array_merge($paramsThumbs,$params);
- return $this->findMaisBuscadosAtivos('all',$params,$options);
- }
- public function findMaisBuscadosAtivos($tipo, $params = array(), $options = null){
- $condicoesAtivo = array(
- 'Veiculo.ativo' => true,
- 'Anuncio.ativo' => true,
- 'Contrato.ativo' => true,
- 'Veiculo.antigo' =>false,
- 'Contrato.vencimento >' => date('Y-m-d G:i:s'),
- //'Contrato.inicio <=' => date('Y-m-d G:i:s'),
- //'Contrato.plano_id' => 13,
- 'Contrato.plano_status_id' => 2
- );
- if(isset($params['conditions']))
- $params['conditions'] = array_merge($params['conditions'],$condicoesAtivo);
- else
- $params['conditions'] = $condicoesAtivo;
- $ativos = $this->find($tipo,$params);
- return $ativos;
- }
- /*
- * action findThumbsAtivos
- * criado por: Caio - caioaao@gmail.com
- */
- public function findThumbsAtivos($tipo,$params = array(),$options = null){
- $paramsThumbs = array(
- 'recursive' => -1,
- 'joins' => $this->getInformacoesCompletasJoins(),
- 'limit' => 20,
- 'order' => 'RAND()',
- 'fields' => array(
- 'Anuncio.created',
- 'Marca.nome',
- 'Versao.nome',
- 'Modelo.nome',
- 'Veiculo.id',
- 'Veiculo.placa',
- 'Veiculo.ano_modelo',
- 'Veiculo.preco',
- 'Combustivel.id',
- 'Cor.nome',
- 'Veiculo.categoria_id',
- 'Categoria.model'
- )
- // 'order' => array(
- // 'Veiculo.ano_modelo' => 'DESC'
- // )
- );
- $params = array_merge($paramsThumbs,$params);
- return $this->findAtivos($tipo,$params,$options);
- }
- public function findAtivos($tipo, $params = array(), $options = null){
- $condicoesAtivo = $this->getCondicoesAtivo($options);
- if(isset($params['conditions']))
- $params['conditions'] = array_merge($params['conditions'],$condicoesAtivo);
- else
- $params['conditions'] = $condicoesAtivo;
- $ativos = $this->find($tipo,$params);
- return $ativos;
- }
- public function getCondicoesAtivo($options = null){
- if(isset($options['pessoa_fisica'])) {
- if($options['pessoa_fisica']) {
- return array(
- 'Veiculo.ativo' => true,
- 'Anuncio.ativo' => true,
- 'Contrato.ativo' => true,
- 'Veiculo.antigo' =>false,
- 'Contrato.vencimento >' => date('Y-m-d G:i:s')
- //'Contrato.inicio <=' => date('Y-m-d G:i:s'),
- // 'Contrato.plano_id' => 13,
- // 'Contrato.plano_status_id' => 2
- );
- }
- }
- return array(
- 'Veiculo.ativo' => true,
- 'Anuncio.ativo' => true,
- 'Contrato.ativo' => true,
- 'Veiculo.antigo' =>false,
- 'Contrato.vencimento >' => date('Y-m-d G:i:s'),
- //'Contrato.inicio <=' => date('Y-m-d G:i:s'),
- //'Contrato.plano_id' => 13,
- 'Contrato.plano_status_id' => 2
- );
- }
- public function getInformacoesCompletasJoins(){
- return array(
- array(
- 'table' => 'contratos',
- 'alias' => 'Contrato',
- 'type' => 'INNER',
- 'conditions' => array(
- 'Contrato.id = Anuncio.contrato_id'
- )
- ),
- array(
- 'table' => 'veiculos',
- 'alias' => 'Veiculo',
- 'type' => 'INNER',
- 'conditions' => array(
- 'Veiculo.id = Anuncio.foreign_key'
- )
- ),
- array(
- 'table' => 'versoes',
- 'alias' => 'Versao',
- 'type' => 'INNER',
- 'conditions' => array(
- 'Versao.id = Veiculo.versao_id'
- )
- ),
- array(
- 'table' => 'modelos',
- 'alias' => 'Modelo',
- 'type' => 'INNER',
- 'conditions' => array(
- 'Modelo.id = Versao.modelo_id'
- )
- ),
- array(
- 'table' => 'marcas',
- 'alias' => 'Marca',
- 'type' => 'INNER',
- 'conditions' => array(
- 'Marca.id = Modelo.marca_id'
- )
- ),
- array(
- 'table' => 'combustiveis',
- 'alias' => 'Combustivel',
- 'type' => 'INNER',
- 'conditions' => array(
- 'Combustivel.id = Veiculo.combustivel_id'
- )
- ),
- array(
- 'table' => 'cores',
- 'alias' => 'Cor',
- 'type' => 'INNER',
- 'conditions' => array(
- 'Cor.id = Veiculo.cor_id'
- )
- ),
- array(
- 'table' => 'categorias',
- 'alias' => 'Categoria',
- 'type' => 'INNER',
- 'conditions' => array(
- 'Categoria.id = Veiculo.categoria_id'
- )
- )
- );
- }
- /**
- * Actions relacionadas a listagem de Peças & Acessórios e Serviços
- * Criado: 07-02-2012
- * Patrick Maciel, Matheus Manoel
- */
- public function findModulo($tipo, $model, $params = array(),$options = null){
- switch($model) {
- case 'servicos':
- $paramsThumbs = array(
- 'recursive' => -1,
- 'joins' => $this->getModuloInformacoesCompletasJoins($model),
- 'limit' => 20,
- 'order' => 'RAND()',
- 'fields' => array(
- 'Anuncio.created',
- 'Servico.titulo',
- 'Servico.valor',
- 'Servico.id',
- 'Servico.categoria_id',
- 'Servico.imagem_1',
- 'Categoria.id',
- 'Categoria.parent_id',
- 'Categoria.nome',
- 'Categoria.model',
- 'Cliente.id',
- 'Cliente.nome',
- 'Cliente.nome_fantasia'
- )
- );
- break;
- case 'pecas_acessorios':
- $paramsThumbs = array(
- 'recursive' => -1,
- 'joins' => $this->getModuloInformacoesCompletasJoins($model),
- 'limit' => 20,
- 'order' => 'RAND()',
- 'fields' => array(
- 'Anuncio.created',
- 'PecaAcessorio.id',
- 'PecaAcessorio.categoria_id',
- 'PecaAcessorio.nome',
- 'PecaAcessorio.imagem_1',
- 'PecaAcessorio.preco',
- 'Categoria.id',
- 'Categoria.parent_id',
- 'Categoria.nome',
- 'Categoria.model',
- 'Cliente.id',
- 'Cliente.nome',
- 'Cliente.nome_fantasia'
- )
- );
- break;
- default:
- $paramsThumbs = array(
- 'recursive' => -1,
- 'joins' => $this->getModuloInformacoesCompletasJoins(),
- 'limit' => 20,
- 'order' => 'RAND()',
- 'fields' => array(
- 'Anuncio.created',
- 'Cateoria.nome',
- 'Categoria.model'
- )
- );
- break;
- }
- $params = array_merge($paramsThumbs,$params);
- return $this->findModuloAtivos($tipo,$model, $params,$options);
- }
- public function findModuloAtivos($tipo, $model, $params = array(), $options = null){
- $condicoesAtivo = $this->getModuloCondicoesAtivo($model, $options);
- if(isset($params['conditions']))
- $params['conditions'] = array_merge($params['conditions'],$condicoesAtivo);
- else
- $params['conditions'] = $condicoesAtivo;
- // debug($tipo);
- // debug($params);
- $ativos = $this->find($tipo,$params);
- return $ativos;
- }
- public function getModuloCondicoesAtivo($model, $options = null){
- switch($model) {
- case 'servicos':
- return array(
- 'Servico.ativo' => true,
- 'Anuncio.ativo' => true,
- 'Contrato.ativo' => true,
- 'Contrato.vencimento >' => date('Y-m-d G:i:s')
- //'Contrato.inicio <=' => date('Y-m-d G:i:s'),
- // 'Contrato.plano_id' => 13,
- // 'Contrato.plano_status_id' => 2
- );
- break;
- case 'pecas_acessorios':
- return array(
- 'PecaAcessorio.ativo' => true,
- 'Anuncio.ativo' => true,
- 'Contrato.ativo' => true,
- 'Contrato.vencimento >' => date('Y-m-d G:i:s')
- //'Contrato.inicio <=' => date('Y-m-d G:i:s'),
- // 'Contrato.plano_id' => 13,
- // 'Contrato.plano_status_id' => 2
- );
- break;
- }
- }
- public function getModuloInformacoesCompletasJoins($model = null){
- switch($model) {
- case 'pecas_acessorios':
- return array(
- array(
- 'table' => 'contratos',
- 'alias' => 'Contrato',
- 'type' => 'INNER',
- 'conditions' => array(
- 'Contrato.id = Anuncio.contrato_id'
- )
- ),
- array(
- 'table' => 'clientes',
- 'alias' => 'Cliente',
- 'type' => 'INNER',
- 'conditions' => array(
- 'Cliente.id = Contrato.cliente_id'
- )
- ),
- array(
- 'table' => 'pecas_acessorios',
- 'alias' => 'PecaAcessorio',
- 'type' => 'INNER',
- 'conditions' => array(
- 'PecaAcessorio.id = Anuncio.foreign_key'
- )
- ),
- array(
- 'table' => 'categorias',
- 'alias' => 'Categoria',
- 'type' => 'INNER',
- 'conditions' => array(
- 'Categoria.id = PecaAcessorio.categoria_id'
- )
- )
- );
- break;
- case 'servicos':
- return array(
- array(
- 'table' => 'contratos',
- 'alias' => 'Contrato',
- 'type' => 'INNER',
- 'conditions' => array(
- 'Contrato.id = Anuncio.contrato_id'
- )
- ),
- array(
- 'table' => 'clientes',
- 'alias' => 'Cliente',
- 'type' => 'INNER',
- 'conditions' => array(
- 'Cliente.id = Contrato.cliente_id'
- )
- ),
- array(
- 'table' => 'servicos',
- 'alias' => 'Servico',
- 'type' => 'INNER',
- 'conditions' => array(
- 'Servico.id = Anuncio.foreign_key'
- )
- ),
- array(
- 'table' => 'categorias',
- 'alias' => 'Categoria',
- 'type' => 'INNER',
- 'conditions' => array(
- 'Categoria.id = Servico.categoria_id'
- )
- )
- );
- break;
- }
- }
- public function getParamsBusca($params){
- $params = Sanitize::clean($params);
- if(isset($params['model'])) {
- switch($params['model']) {
- case 'servicos':
- $model = $this->Servico->Categoria->getModel(Anuncio::SERVICOS);
- // Pega as condições de ativo do model
- $conditions = $this->getModuloCondicoesAtivo('servicos');
- // Pega os joins do model
- $joins = $this->getModuloInformacoesCompletasJoins('servicos');
- if(isset($params['categoria']) && $params['categoria'] != '')
- $conditions['Servico.categoria_id'] = $params['categoria'];
- if(isset($params['tipo']) && $params['tipo'] != '')
- $conditions['Servico.tipo_id'] = $params['tipo'];
- break;
- case 'pecas_acessorios':
- $model = $this->PecaAcessorio->Categoria->getModel(Anuncio::PECAS_ACESSORIOS);
- // Pega as condições de ativo do model
- $conditions = $this->getModuloCondicoesAtivo('pecas_acessorios');
- // Pega os joins do model
- $joins = $this->getModuloInformacoesCompletasJoins('pecas_acessorios');
- if(isset($params['categoria']) && $params['categoria'] != '')
- $conditions['PecaAcessorio.categoria_id'] = $params['categoria'];
- if(isset($params['tipo']) && $params['tipo'] != '')
- $conditions['PecaAcessorio.tipo_id'] = $params['tipo'];
- break;
- default:
- break;
- }
- } else {
- $model = $this->Veiculo->Categoria->getModel($params['tipo']);
- // Pega as condições de ativo do model
- $conditions = $this->getCondicoesAtivo();
- // Pega os joins do model
- $joins = $this->getInformacoesCompletasJoins();
- if(isset($params['modelo']) && $params['modelo'] != '')
- $conditions['Modelo_id'] = $params['modelo'];
- if(isset($params['marca']) && $params['marca'] != '')
- $conditions['Marca.id'] = $params['marca'];
- if(isset($params['ano_inferior']) && $params['ano_inferior'] != '')
- $conditions['Veiculo.ano_modelo >='] = $params['ano_inferior'];
- if(isset($params['ano_superior']) && $params['ano_superior'] != '')
- $conditions['Veiculo.ano_modelo <='] = $params['ano_superior'];
- if(isset($params['preco_inferior']) && $params['preco_inferior'] != '')
- $conditions['Veiculo.preco >='] = $params['preco_inferior'];
- if(isset($params['preco_superior']) && $params['preco_superior'] != '')
- $conditions['Veiculo.preco <='] = $params['preco_superior'];
- if(isset($params['combustivel']) && $params['combustivel'] != '')
- $conditions['Combustivel.id'] = $params['combustivel'];
- if(isset($params['cor']) && $params['cor'] != '')
- $conditions['Cor.id'] = $params['cor'];
- if(isset($params['opcionais']) && $params['opcionais'] != '' && $params['opcionais'] != null){
- $opcionaisConditions = array(
- 'VeiculoOpcional.veiculo_id'=>'Veiculo.id',
- // 'VeiculoOpcional.opcional_id' => array()
- );
- $conditions['VeiculoOpcional.opcional_id'] = array();
- foreach($params['opcionais'] as $opcionalId){
- array_push(&$conditions['VeiculoOpcional.opcional_id'],$opcionalId);
- #$conditions['VeiculoOpcional.opcional_id'] = $opcionalId;
- }
- array_push(&$joins,array(
- 'table'=>'veiculos_opcionais',
- 'alias'=>'VeiculoOpcional',
- 'type'=>'INNER',
- 'conditions'=>array('VeiculoOpcional.veiculo_id = Veiculo.id')
- ));
- }
- }
- return array(
- 'conditions' => $conditions,
- 'joins' => $joins,
- 'recursive' => -1,
- );
- }
- public function validarAtivos($anuncios){
- foreach($anuncios as $i => $anuncio){
- if($this->isAtivo($anuncio['Anuncio']['id']))
- $anuncios[$i]['ativo'] = true;
- else
- $anuncios[$i]['ativo'] = false;
- }
- return $anuncios;
- }
- public function isAtivo($id){
- $options['conditions'] = $this->getCondicoesAtivo();
- $options['conditions']['Anuncio.id'] = $id;
- $anuncio = $this->find('first',$options);
- if(!empty($anuncio))
- return true;
- return false;
- }
- // public function afterSave() {
- // debug($this->data);
- // exit;
- // }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement