Advertisement
Guest User

Untitled

a guest
Aug 1st, 2015
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.93 KB | None | 0 0
  1. <?php
  2. App::uses('AppModel', 'Model');
  3. /**
  4. * Anuncio Model
  5. *
  6. * @property Contrato $Contrato
  7. * @property Veiculo $Veiculo
  8. * @property AnuncioFoto $AnuncioFoto
  9. */
  10. class Anuncio extends AppModel {
  11.  
  12. const AUTOMOVEIS = 3;
  13. const MOTOS = 4;
  14. const CAMINHOES_ONIBUS = 6;
  15. const NAUTICA = 5;
  16. const SERVICOS = 160;
  17. const PECAS_ACESSORIOS = 159;
  18. const RARIDADES = 7;
  19. const TUNADOS = 8;
  20.  
  21. public $actsAs = array('Containable');
  22. /**
  23. * Validation rules
  24. *
  25. * @var array
  26. */
  27. public $validate = array(
  28. 'contrato_id' => array(
  29. 'numeric' => array(
  30. 'rule' => array('numeric'),
  31. //'message' => 'Your custom message here',
  32. //'allowEmpty' => false,
  33. //'required' => false,
  34. //'last' => false, // Stop validation after this rule
  35. //'on' => 'create', // Limit validation to 'create' or 'update' operations
  36. ),
  37. ),
  38. 'foreign_key' => array(
  39. 'numeric' => array(
  40. 'rule' => array('numeric'),
  41. //'message' => 'Your custom message here',
  42. //'allowEmpty' => false,
  43. //'required' => false,
  44. //'last' => false, // Stop validation after this rule
  45. //'on' => 'create', // Limit validation to 'create' or 'update' operations
  46. ),
  47. ),
  48. 'ativo' => array(
  49. 'boolean' => array(
  50. 'rule' => array('boolean'),
  51. //'message' => 'Your custom message here',
  52. //'allowEmpty' => false,
  53. //'required' => false,
  54. //'last' => false, // Stop validation after this rule
  55. //'on' => 'create', // Limit validation to 'create' or 'update' operations
  56. ),
  57. ),
  58. );
  59.  
  60. //The Associations below have been created with all possible keys, those that are not needed can be removed
  61.  
  62. /**
  63. * belongsTo associations
  64. *
  65. * @var array
  66. */
  67. public $belongsTo = array(
  68. 'Contrato' => array(
  69. 'className' => 'Contrato',
  70. 'foreignKey' => 'contrato_id',
  71. 'conditions' => '',
  72. 'fields' => '',
  73. 'order' => '',
  74. 'counterCache' => true,
  75. 'counterScope' => array(
  76. 'Anuncio.ativo' => true,
  77. 'Veiculo.antigo' => false,
  78. 'Veiculo.ativo' => true
  79. )
  80. ),
  81. 'Veiculo' => array(
  82. 'className' => 'Veiculo',
  83. 'foreignKey' => 'foreign_key',
  84. 'conditions' => '',
  85. 'fields' => '',
  86. 'order' => ''
  87. ),
  88. 'PecaAcessorio' => array(
  89. 'className' => 'PecaAcessorio',
  90. 'foreignKey' => 'foreign_key',
  91. 'conditions' => '',
  92. 'fields' => '',
  93. 'order' => ''
  94. ),
  95. 'Servico' => array(
  96. 'className' => 'Servico',
  97. 'foreignKey' => 'foreign_key',
  98. 'conditions' => '',
  99. 'fields' => '',
  100. 'order' => ''
  101. )
  102. );
  103.  
  104. /**
  105. * hasMany associations
  106. *
  107. * @var array
  108. */
  109. public $hasMany = array(
  110. 'AnuncioFoto' => array(
  111. 'className' => 'AnuncioFoto',
  112. 'foreignKey' => 'anuncio_id',
  113. 'dependent' => false,
  114. 'conditions' => '',
  115. 'fields' => '',
  116. 'order' => '',
  117. 'limit' => '',
  118. 'offset' => '',
  119. 'exclusive' => '',
  120. 'finderQuery' => '',
  121. 'counterQuery' => ''
  122. )
  123. );
  124.  
  125.  
  126.  
  127. /*
  128. * action findMaisBuscados
  129. * criado por: Patrick Maciel - patrickmaciel.info@gmail.com - www.patrickmaciel.com
  130. * data: 19-03-2012
  131. */
  132. public function findMaisBuscados($quantidade,$params = array(), $order = null, $options = null){
  133.  
  134. $paramsThumbs = array(
  135. 'recursive' => -1,
  136. 'joins' => $this->getInformacoesCompletasJoins(),
  137. 'limit' => $quantidade,
  138. 'order' => array(
  139. 'Anuncio.buscado DESC',
  140. 'Anuncio.visualizacoes DESC'
  141. ),
  142. 'fields' => array(
  143. 'Anuncio.created',
  144. 'Anuncio.buscado',
  145. 'Anuncio.visualizacoes',
  146. 'Marca.nome',
  147. 'Versao.nome',
  148. 'Modelo.nome',
  149. 'Veiculo.id',
  150. 'Veiculo.placa',
  151. 'Veiculo.ano_modelo',
  152. 'Veiculo.preco',
  153. 'Combustivel.id',
  154. 'Cor.nome',
  155. 'Veiculo.categoria_id',
  156. 'Categoria.model'
  157. )
  158. );
  159.  
  160. $params = array_merge($paramsThumbs,$params);
  161.  
  162. return $this->findMaisBuscadosAtivos('all',$params,$options);
  163. }
  164.  
  165. public function findMaisBuscadosAtivos($tipo, $params = array(), $options = null){
  166. $condicoesAtivo = array(
  167. 'Veiculo.ativo' => true,
  168. 'Anuncio.ativo' => true,
  169. 'Contrato.ativo' => true,
  170. 'Veiculo.antigo' =>false,
  171. 'Contrato.vencimento >' => date('Y-m-d G:i:s'),
  172. //'Contrato.inicio <=' => date('Y-m-d G:i:s'),
  173. //'Contrato.plano_id' => 13,
  174. 'Contrato.plano_status_id' => 2
  175. );
  176.  
  177. if(isset($params['conditions']))
  178. $params['conditions'] = array_merge($params['conditions'],$condicoesAtivo);
  179. else
  180. $params['conditions'] = $condicoesAtivo;
  181.  
  182. $ativos = $this->find($tipo,$params);
  183.  
  184. return $ativos;
  185. }
  186.  
  187.  
  188.  
  189. /*
  190. * action findThumbsAtivos
  191. * criado por: Caio - caioaao@gmail.com
  192. */
  193. public function findThumbsAtivos($tipo,$params = array(),$options = null){
  194.  
  195. $paramsThumbs = array(
  196. 'recursive' => -1,
  197. 'joins' => $this->getInformacoesCompletasJoins(),
  198. 'limit' => 20,
  199. 'order' => 'RAND()',
  200. 'fields' => array(
  201. 'Anuncio.created',
  202. 'Marca.nome',
  203. 'Versao.nome',
  204. 'Modelo.nome',
  205. 'Veiculo.id',
  206. 'Veiculo.placa',
  207. 'Veiculo.ano_modelo',
  208. 'Veiculo.preco',
  209. 'Combustivel.id',
  210. 'Cor.nome',
  211. 'Veiculo.categoria_id',
  212. 'Categoria.model'
  213. )
  214. // 'order' => array(
  215. // 'Veiculo.ano_modelo' => 'DESC'
  216. // )
  217. );
  218.  
  219. $params = array_merge($paramsThumbs,$params);
  220.  
  221. return $this->findAtivos($tipo,$params,$options);
  222. }
  223.  
  224. public function findAtivos($tipo, $params = array(), $options = null){
  225. $condicoesAtivo = $this->getCondicoesAtivo($options);
  226.  
  227. if(isset($params['conditions']))
  228. $params['conditions'] = array_merge($params['conditions'],$condicoesAtivo);
  229. else
  230. $params['conditions'] = $condicoesAtivo;
  231.  
  232. $ativos = $this->find($tipo,$params);
  233.  
  234. return $ativos;
  235. }
  236.  
  237. public function getCondicoesAtivo($options = null){
  238. if(isset($options['pessoa_fisica'])) {
  239. if($options['pessoa_fisica']) {
  240. return array(
  241. 'Veiculo.ativo' => true,
  242. 'Anuncio.ativo' => true,
  243. 'Contrato.ativo' => true,
  244. 'Veiculo.antigo' =>false,
  245. 'Contrato.vencimento >' => date('Y-m-d G:i:s')
  246. //'Contrato.inicio <=' => date('Y-m-d G:i:s'),
  247. // 'Contrato.plano_id' => 13,
  248. // 'Contrato.plano_status_id' => 2
  249. );
  250. }
  251. }
  252.  
  253. return array(
  254. 'Veiculo.ativo' => true,
  255. 'Anuncio.ativo' => true,
  256. 'Contrato.ativo' => true,
  257. 'Veiculo.antigo' =>false,
  258. 'Contrato.vencimento >' => date('Y-m-d G:i:s'),
  259. //'Contrato.inicio <=' => date('Y-m-d G:i:s'),
  260. //'Contrato.plano_id' => 13,
  261. 'Contrato.plano_status_id' => 2
  262. );
  263. }
  264.  
  265. public function getInformacoesCompletasJoins(){
  266. return array(
  267. array(
  268. 'table' => 'contratos',
  269. 'alias' => 'Contrato',
  270. 'type' => 'INNER',
  271. 'conditions' => array(
  272. 'Contrato.id = Anuncio.contrato_id'
  273. )
  274. ),
  275. array(
  276. 'table' => 'veiculos',
  277. 'alias' => 'Veiculo',
  278. 'type' => 'INNER',
  279. 'conditions' => array(
  280. 'Veiculo.id = Anuncio.foreign_key'
  281. )
  282. ),
  283. array(
  284. 'table' => 'versoes',
  285. 'alias' => 'Versao',
  286. 'type' => 'INNER',
  287. 'conditions' => array(
  288. 'Versao.id = Veiculo.versao_id'
  289. )
  290. ),
  291. array(
  292. 'table' => 'modelos',
  293. 'alias' => 'Modelo',
  294. 'type' => 'INNER',
  295. 'conditions' => array(
  296. 'Modelo.id = Versao.modelo_id'
  297. )
  298. ),
  299. array(
  300. 'table' => 'marcas',
  301. 'alias' => 'Marca',
  302. 'type' => 'INNER',
  303. 'conditions' => array(
  304. 'Marca.id = Modelo.marca_id'
  305. )
  306. ),
  307. array(
  308. 'table' => 'combustiveis',
  309. 'alias' => 'Combustivel',
  310. 'type' => 'INNER',
  311. 'conditions' => array(
  312. 'Combustivel.id = Veiculo.combustivel_id'
  313. )
  314. ),
  315. array(
  316. 'table' => 'cores',
  317. 'alias' => 'Cor',
  318. 'type' => 'INNER',
  319. 'conditions' => array(
  320. 'Cor.id = Veiculo.cor_id'
  321. )
  322. ),
  323. array(
  324. 'table' => 'categorias',
  325. 'alias' => 'Categoria',
  326. 'type' => 'INNER',
  327. 'conditions' => array(
  328. 'Categoria.id = Veiculo.categoria_id'
  329. )
  330. )
  331. );
  332. }
  333.  
  334.  
  335.  
  336. /**
  337. * Actions relacionadas a listagem de Peças & Acessórios e Serviços
  338. * Criado: 07-02-2012
  339. * Patrick Maciel, Matheus Manoel
  340. */
  341. public function findModulo($tipo, $model, $params = array(),$options = null){
  342.  
  343. switch($model) {
  344. case 'servicos':
  345. $paramsThumbs = array(
  346. 'recursive' => -1,
  347. 'joins' => $this->getModuloInformacoesCompletasJoins($model),
  348. 'limit' => 20,
  349. 'order' => 'RAND()',
  350. 'fields' => array(
  351. 'Anuncio.created',
  352. 'Servico.titulo',
  353. 'Servico.valor',
  354. 'Servico.id',
  355. 'Servico.categoria_id',
  356. 'Servico.imagem_1',
  357. 'Categoria.id',
  358. 'Categoria.parent_id',
  359. 'Categoria.nome',
  360. 'Categoria.model',
  361. 'Cliente.id',
  362. 'Cliente.nome',
  363. 'Cliente.nome_fantasia'
  364. )
  365. );
  366. break;
  367. case 'pecas_acessorios':
  368. $paramsThumbs = array(
  369. 'recursive' => -1,
  370. 'joins' => $this->getModuloInformacoesCompletasJoins($model),
  371. 'limit' => 20,
  372. 'order' => 'RAND()',
  373. 'fields' => array(
  374. 'Anuncio.created',
  375. 'PecaAcessorio.id',
  376. 'PecaAcessorio.categoria_id',
  377. 'PecaAcessorio.nome',
  378. 'PecaAcessorio.imagem_1',
  379. 'PecaAcessorio.preco',
  380. 'Categoria.id',
  381. 'Categoria.parent_id',
  382. 'Categoria.nome',
  383. 'Categoria.model',
  384. 'Cliente.id',
  385. 'Cliente.nome',
  386. 'Cliente.nome_fantasia'
  387. )
  388. );
  389. break;
  390. default:
  391. $paramsThumbs = array(
  392. 'recursive' => -1,
  393. 'joins' => $this->getModuloInformacoesCompletasJoins(),
  394. 'limit' => 20,
  395. 'order' => 'RAND()',
  396. 'fields' => array(
  397. 'Anuncio.created',
  398. 'Cateoria.nome',
  399. 'Categoria.model'
  400. )
  401. );
  402. break;
  403. }
  404.  
  405. $params = array_merge($paramsThumbs,$params);
  406.  
  407. return $this->findModuloAtivos($tipo,$model, $params,$options);
  408. }
  409.  
  410. public function findModuloAtivos($tipo, $model, $params = array(), $options = null){
  411. $condicoesAtivo = $this->getModuloCondicoesAtivo($model, $options);
  412.  
  413. if(isset($params['conditions']))
  414. $params['conditions'] = array_merge($params['conditions'],$condicoesAtivo);
  415. else
  416. $params['conditions'] = $condicoesAtivo;
  417.  
  418. // debug($tipo);
  419. // debug($params);
  420.  
  421. $ativos = $this->find($tipo,$params);
  422.  
  423. return $ativos;
  424. }
  425.  
  426. public function getModuloCondicoesAtivo($model, $options = null){
  427.  
  428. switch($model) {
  429. case 'servicos':
  430. return array(
  431. 'Servico.ativo' => true,
  432. 'Anuncio.ativo' => true,
  433. 'Contrato.ativo' => true,
  434. 'Contrato.vencimento >' => date('Y-m-d G:i:s')
  435. //'Contrato.inicio <=' => date('Y-m-d G:i:s'),
  436. // 'Contrato.plano_id' => 13,
  437. // 'Contrato.plano_status_id' => 2
  438. );
  439. break;
  440. case 'pecas_acessorios':
  441. return array(
  442. 'PecaAcessorio.ativo' => true,
  443. 'Anuncio.ativo' => true,
  444. 'Contrato.ativo' => true,
  445. 'Contrato.vencimento >' => date('Y-m-d G:i:s')
  446. //'Contrato.inicio <=' => date('Y-m-d G:i:s'),
  447. // 'Contrato.plano_id' => 13,
  448. // 'Contrato.plano_status_id' => 2
  449. );
  450. break;
  451. }
  452.  
  453. }
  454.  
  455. public function getModuloInformacoesCompletasJoins($model = null){
  456. switch($model) {
  457. case 'pecas_acessorios':
  458. return array(
  459. array(
  460. 'table' => 'contratos',
  461. 'alias' => 'Contrato',
  462. 'type' => 'INNER',
  463. 'conditions' => array(
  464. 'Contrato.id = Anuncio.contrato_id'
  465. )
  466. ),
  467. array(
  468. 'table' => 'clientes',
  469. 'alias' => 'Cliente',
  470. 'type' => 'INNER',
  471. 'conditions' => array(
  472. 'Cliente.id = Contrato.cliente_id'
  473. )
  474. ),
  475. array(
  476. 'table' => 'pecas_acessorios',
  477. 'alias' => 'PecaAcessorio',
  478. 'type' => 'INNER',
  479. 'conditions' => array(
  480. 'PecaAcessorio.id = Anuncio.foreign_key'
  481. )
  482. ),
  483. array(
  484. 'table' => 'categorias',
  485. 'alias' => 'Categoria',
  486. 'type' => 'INNER',
  487. 'conditions' => array(
  488. 'Categoria.id = PecaAcessorio.categoria_id'
  489. )
  490. )
  491. );
  492. break;
  493. case 'servicos':
  494. return array(
  495. array(
  496. 'table' => 'contratos',
  497. 'alias' => 'Contrato',
  498. 'type' => 'INNER',
  499. 'conditions' => array(
  500. 'Contrato.id = Anuncio.contrato_id'
  501. )
  502. ),
  503. array(
  504. 'table' => 'clientes',
  505. 'alias' => 'Cliente',
  506. 'type' => 'INNER',
  507. 'conditions' => array(
  508. 'Cliente.id = Contrato.cliente_id'
  509. )
  510. ),
  511. array(
  512. 'table' => 'servicos',
  513. 'alias' => 'Servico',
  514. 'type' => 'INNER',
  515. 'conditions' => array(
  516. 'Servico.id = Anuncio.foreign_key'
  517. )
  518. ),
  519. array(
  520. 'table' => 'categorias',
  521. 'alias' => 'Categoria',
  522. 'type' => 'INNER',
  523. 'conditions' => array(
  524. 'Categoria.id = Servico.categoria_id'
  525. )
  526. )
  527. );
  528. break;
  529. }
  530. }
  531.  
  532.  
  533.  
  534.  
  535. public function getParamsBusca($params){
  536. $params = Sanitize::clean($params);
  537.  
  538. if(isset($params['model'])) {
  539. switch($params['model']) {
  540. case 'servicos':
  541. $model = $this->Servico->Categoria->getModel(Anuncio::SERVICOS);
  542.  
  543. // Pega as condições de ativo do model
  544. $conditions = $this->getModuloCondicoesAtivo('servicos');
  545.  
  546. // Pega os joins do model
  547. $joins = $this->getModuloInformacoesCompletasJoins('servicos');
  548.  
  549. if(isset($params['categoria']) && $params['categoria'] != '')
  550. $conditions['Servico.categoria_id'] = $params['categoria'];
  551.  
  552. if(isset($params['tipo']) && $params['tipo'] != '')
  553. $conditions['Servico.tipo_id'] = $params['tipo'];
  554. break;
  555. case 'pecas_acessorios':
  556. $model = $this->PecaAcessorio->Categoria->getModel(Anuncio::PECAS_ACESSORIOS);
  557.  
  558. // Pega as condições de ativo do model
  559. $conditions = $this->getModuloCondicoesAtivo('pecas_acessorios');
  560.  
  561. // Pega os joins do model
  562. $joins = $this->getModuloInformacoesCompletasJoins('pecas_acessorios');
  563.  
  564. if(isset($params['categoria']) && $params['categoria'] != '')
  565. $conditions['PecaAcessorio.categoria_id'] = $params['categoria'];
  566.  
  567. if(isset($params['tipo']) && $params['tipo'] != '')
  568. $conditions['PecaAcessorio.tipo_id'] = $params['tipo'];
  569.  
  570. break;
  571. default:
  572. break;
  573. }
  574. } else {
  575. $model = $this->Veiculo->Categoria->getModel($params['tipo']);
  576.  
  577. // Pega as condições de ativo do model
  578. $conditions = $this->getCondicoesAtivo();
  579.  
  580. // Pega os joins do model
  581. $joins = $this->getInformacoesCompletasJoins();
  582.  
  583. if(isset($params['modelo']) && $params['modelo'] != '')
  584. $conditions['Modelo_id'] = $params['modelo'];
  585.  
  586. if(isset($params['marca']) && $params['marca'] != '')
  587. $conditions['Marca.id'] = $params['marca'];
  588.  
  589. if(isset($params['ano_inferior']) && $params['ano_inferior'] != '')
  590. $conditions['Veiculo.ano_modelo >='] = $params['ano_inferior'];
  591.  
  592. if(isset($params['ano_superior']) && $params['ano_superior'] != '')
  593. $conditions['Veiculo.ano_modelo <='] = $params['ano_superior'];
  594.  
  595. if(isset($params['preco_inferior']) && $params['preco_inferior'] != '')
  596. $conditions['Veiculo.preco >='] = $params['preco_inferior'];
  597.  
  598. if(isset($params['preco_superior']) && $params['preco_superior'] != '')
  599. $conditions['Veiculo.preco <='] = $params['preco_superior'];
  600.  
  601. if(isset($params['combustivel']) && $params['combustivel'] != '')
  602. $conditions['Combustivel.id'] = $params['combustivel'];
  603.  
  604. if(isset($params['cor']) && $params['cor'] != '')
  605. $conditions['Cor.id'] = $params['cor'];
  606.  
  607. if(isset($params['opcionais']) && $params['opcionais'] != '' && $params['opcionais'] != null){
  608. $opcionaisConditions = array(
  609. 'VeiculoOpcional.veiculo_id'=>'Veiculo.id',
  610. // 'VeiculoOpcional.opcional_id' => array()
  611. );
  612. $conditions['VeiculoOpcional.opcional_id'] = array();
  613. foreach($params['opcionais'] as $opcionalId){
  614. array_push(&$conditions['VeiculoOpcional.opcional_id'],$opcionalId);
  615. #$conditions['VeiculoOpcional.opcional_id'] = $opcionalId;
  616. }
  617.  
  618. array_push(&$joins,array(
  619. 'table'=>'veiculos_opcionais',
  620. 'alias'=>'VeiculoOpcional',
  621. 'type'=>'INNER',
  622. 'conditions'=>array('VeiculoOpcional.veiculo_id = Veiculo.id')
  623. ));
  624.  
  625. }
  626. }
  627.  
  628. return array(
  629. 'conditions' => $conditions,
  630. 'joins' => $joins,
  631. 'recursive' => -1,
  632. );
  633. }
  634.  
  635. public function validarAtivos($anuncios){
  636. foreach($anuncios as $i => $anuncio){
  637. if($this->isAtivo($anuncio['Anuncio']['id']))
  638. $anuncios[$i]['ativo'] = true;
  639. else
  640. $anuncios[$i]['ativo'] = false;
  641. }
  642.  
  643. return $anuncios;
  644. }
  645.  
  646. public function isAtivo($id){
  647. $options['conditions'] = $this->getCondicoesAtivo();
  648. $options['conditions']['Anuncio.id'] = $id;
  649.  
  650. $anuncio = $this->find('first',$options);
  651. if(!empty($anuncio))
  652. return true;
  653.  
  654. return false;
  655. }
  656.  
  657.  
  658. // public function afterSave() {
  659. // debug($this->data);
  660. // exit;
  661. // }
  662. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement