Guest User

Untitled

a guest
Sep 20th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.33 KB | None | 0 0
  1. <?php
  2. App::uses('AppModel', 'Model');
  3. /**
  4. * Veiculo Model
  5. *
  6. * @property Contrato $Contrato
  7. * @property Categoria $Categoria
  8. * @property Cor $Cor
  9. * @property Combustivel $Combustivel
  10. * @property Versao $Versao
  11. * @property Veiculo $Veiculo
  12. * @property Anuncio $Anuncio
  13. * @property Carro $Carro
  14. * @property Moto $Moto
  15. * @property Veiculo $Veiculo
  16. * @property Visita $Visita
  17. */
  18. class Veiculo extends AppModel {
  19. public $actsAs = array('Containable');
  20. public $displayField = 'placa';
  21. /**
  22. * Validation rules
  23. *
  24. * @var array
  25. */
  26. public $validate = array(
  27. 'categoria_id' => array(
  28. 'numeric' => array(
  29. 'rule' => array('numeric'),
  30. //'message' => 'Your custom message here',
  31. //'allowEmpty' => false,
  32. //'required' => false,
  33. //'last' => false, // Stop validation after this rule
  34. //'on' => 'create', // Limit validation to 'create' or 'update' operations
  35. ),
  36. ),
  37. 'cor_id' => array(
  38. 'numeric' => array(
  39. 'rule' => array('numeric'),
  40. //'message' => 'Your custom message here',
  41. //'allowEmpty' => false,
  42. //'required' => false,
  43. //'last' => false, // Stop validation after this rule
  44. //'on' => 'create', // Limit validation to 'create' or 'update' operations
  45. ),
  46. ),
  47. 'combustivel_id' => array(
  48. 'numeric' => array(
  49. 'rule' => array('numeric'),
  50. //'message' => 'Your custom message here',
  51. //'allowEmpty' => false,
  52. //'required' => false,
  53. //'last' => false, // Stop validation after this rule
  54. //'on' => 'create', // Limit validation to 'create' or 'update' operations
  55. ),
  56. ),
  57. 'versao_id' => array(
  58. 'numeric' => array(
  59. 'rule' => array('numeric'),
  60. //'message' => 'Your custom message here',
  61. //'allowEmpty' => false,
  62. //'required' => false,
  63. //'last' => false, // Stop validation after this rule
  64. //'on' => 'create', // Limit validation to 'create' or 'update' operations
  65. ),
  66. ),
  67. 'antigo' => array(
  68. 'boolean' => array(
  69. 'rule' => array('boolean'),
  70. //'message' => 'Your custom message here',
  71. //'allowEmpty' => false,
  72. //'required' => false,
  73. //'last' => false, // Stop validation after this rule
  74. //'on' => 'create', // Limit validation to 'create' or 'update' operations
  75. ),
  76. ),
  77. );
  78.  
  79. //The Associations below have been created with all possible keys, those that are not needed can be removed
  80.  
  81. /**
  82. * belongsTo associations
  83. *
  84. * @var array
  85. */
  86. public $belongsTo = array(
  87. 'Categoria' => array(
  88. 'className' => 'Categoria',
  89. 'foreignKey' => 'categoria_id',
  90. 'conditions' => '',
  91. 'fields' => '',
  92. 'order' => ''
  93. ),
  94. 'Cor' => array(
  95. 'className' => 'Cor',
  96. 'foreignKey' => 'cor_id',
  97. 'conditions' => '',
  98. 'fields' => '',
  99. 'order' => ''
  100. ),
  101. 'Combustivel' => array(
  102. 'className' => 'Combustivel',
  103. 'foreignKey' => 'combustivel_id',
  104. 'conditions' => '',
  105. 'fields' => '',
  106. 'order' => ''
  107. ),
  108. 'Versao' => array(
  109. 'className' => 'Versao',
  110. 'foreignKey' => 'versao_id',
  111. 'conditions' => '',
  112. 'fields' => '',
  113. 'order' => ''
  114. ),
  115. 'Parent' => array(
  116. 'className' => 'Veiculo',
  117. 'foreignKey' => 'veiculo_id',
  118. 'conditions' => '',
  119. 'fields' => '',
  120. 'order' => ''
  121. )
  122. );
  123.  
  124. /**
  125. * hasMany associations
  126. *
  127. * @var array
  128. */
  129. public $hasMany = array(
  130. 'Anuncio' => array(
  131. 'className' => 'Anuncio',
  132. 'foreignKey' => 'foreign_key',
  133. 'dependent' => false,
  134. 'conditions' => '',
  135. 'fields' => '',
  136. 'order' => '',
  137. 'limit' => '',
  138. 'offset' => '',
  139. 'exclusive' => '',
  140. 'finderQuery' => '',
  141. 'counterQuery' => ''
  142. ),
  143. 'Child' => array(
  144. 'className' => 'Veiculo',
  145. 'foreignKey' => 'veiculo_id',
  146. 'dependent' => false,
  147. 'conditions' => '',
  148. 'fields' => '',
  149. 'order' => '',
  150. 'limit' => '',
  151. 'offset' => '',
  152. 'exclusive' => '',
  153. 'finderQuery' => '',
  154. 'counterQuery' => ''
  155. ),
  156. 'Visita' => array(
  157. 'className' => 'Visita',
  158. 'foreignKey' => 'veiculo_id',
  159. 'dependent' => false,
  160. 'conditions' => '',
  161. 'fields' => '',
  162. 'order' => '',
  163. 'limit' => '',
  164. 'offset' => '',
  165. 'exclusive' => '',
  166. 'finderQuery' => '',
  167. 'counterQuery' => ''
  168. )
  169. );
  170.  
  171. public $hasOne = array(
  172.  
  173. 'Carro' => array(
  174. 'className' => 'Carro',
  175. 'foreignKey' => 'veiculo_id',
  176. 'dependent' => false,
  177. 'conditions' => '',
  178. 'fields' => '',
  179. 'order' => '',
  180. 'limit' => '',
  181. 'offset' => '',
  182. 'exclusive' => '',
  183. 'finderQuery' => '',
  184. 'counterQuery' => ''
  185. ),
  186. 'Moto' => array(
  187. 'className' => 'Moto',
  188. 'foreignKey' => 'veiculo_id',
  189. 'dependent' => true,
  190. 'conditions' => '',
  191. 'fields' => '',
  192. 'order' => '',
  193. 'limit' => '',
  194. 'offset' => '',
  195. 'exclusive' => '',
  196. 'finderQuery' => '',
  197. 'counterQuery' => ''
  198. ),
  199. 'Nautica' => array(
  200. 'className' => 'Nautica',
  201. 'foreignKey' => 'veiculo_id',
  202. 'dependent' => false,
  203. 'conditions' => '',
  204. 'fields' => '',
  205. 'order' => '',
  206. 'limit' => '',
  207. 'offset' => '',
  208. 'exclusive' => '',
  209. 'finderQuery' => '',
  210. 'counterQuery' => ''
  211. ),
  212. 'Tunado' => array(
  213. 'className' => 'Tunado',
  214. 'foreignKey' => 'veiculo_id',
  215. 'dependent' => false,
  216. 'conditions' => '',
  217. 'fields' => '',
  218. 'order' => '',
  219. 'limit' => '',
  220. 'offset' => '',
  221. 'exclusive' => '',
  222. 'finderQuery' => '',
  223. 'counterQuery' => ''
  224. ),
  225. 'Raridade' => array(
  226. 'className' => 'Raridade',
  227. 'foreignKey' => 'veiculo_id',
  228. 'dependent' => false,
  229. 'conditions' => '',
  230. 'fields' => '',
  231. 'order' => '',
  232. 'limit' => '',
  233. 'offset' => '',
  234. 'exclusive' => '',
  235. 'finderQuery' => '',
  236. 'counterQuery' => ''
  237. ),
  238. 'CaminhoesOnibus' => array(
  239. 'className' => 'CaminhoesOnibus',
  240. 'foreignKey' => 'veiculo_id',
  241. 'dependent' => false,
  242. 'conditions' => '',
  243. 'fields' => '',
  244. 'order' => '',
  245. 'limit' => '',
  246. 'offset' => '',
  247. 'exclusive' => '',
  248. 'finderQuery' => '',
  249. 'counterQuery' => ''
  250. )
  251. );
  252.  
  253. public $hasAndBelongsToMany = array(
  254. 'Opcional' => array(
  255. 'className' => 'Opcional',
  256. 'joinTable' => 'veiculos_opcionais',
  257. 'foreignKey' => 'veiculo_id',
  258. 'associationForeignKey' => 'opcional_id',
  259. 'unique' => true,
  260. 'conditions' => '',
  261. 'fields' => '',
  262. 'order' => '',
  263. 'limit' => '',
  264. 'offset' => '',
  265. 'finderQuery' => '',
  266. 'deleteQuery' => '',
  267. 'insertQuery' => ''
  268. ),
  269. /*'BuscaIndice' => array(
  270. 'className' => 'BuscaIndex',
  271. 'joinTable' => 'busca_indices_veiculos',
  272. 'foreignKey' => 'veiculo_id',
  273. 'associationForeignKey' => 'busca_indice_id',
  274. 'unique' => true,
  275. 'conditions' => '',
  276. 'fields' => '',
  277. 'order' => '',
  278. 'limit' => '',
  279. 'offset' => '',
  280. 'finderQuery' => '',
  281. 'deleteQuery' => '',
  282. 'insertQuery' => ''
  283. )*/
  284. );
  285.  
  286. public function findLimitesAnoModelo($conditions = null){
  287. $limitesAnoModelo = $this->find('first',array(
  288. 'fields' => array(
  289. 'MIN(Veiculo.ano_modelo) AS inferior',
  290. 'MAX(Veiculo.ano_modelo) AS superior',
  291. ),
  292. 'conditions' => $conditions
  293. ));
  294.  
  295. return $limitesAnoModelo[0];
  296. }
  297.  
  298. public function findLimitesPreco($conditions = null){
  299. $limitesPreco = $this->find('first',array(
  300. 'fields' => array(
  301. 'MIN(Veiculo.preco) AS inferior',
  302. 'MAX(Veiculo.preco) AS superior',
  303. ),
  304. 'conditions'
  305. ));
  306.  
  307. return $limitesPreco[0];
  308. }
  309.  
  310. public function getBuscaRapida($model = false){
  311. $padraoGeral = array(
  312. 'categorias' => $this->Categoria->find('list',array(
  313. 'fields'=>array('Categoria.id','Categoria.nome'),
  314. 'conditions' => array('Categoria.ativo' => true, 'OR'=>array('Categoria.model' =>array('carros','motos','nautica','caminhoes_onibus'))),
  315. 'order' => 'nome ASC'
  316. )),
  317. 'limitesAnoModelo' => $this->findLimitesAnoModelo(),
  318. 'combustiveis' => $this->Combustivel->find('list',array(
  319. 'fields' => array('Combustivel.id','Combustivel.nome'),
  320. 'conditions' => array('Combustivel.ativo'=>true)
  321. )),
  322. 'cores' => $this->Cor->find('list',array(
  323. 'fields' => array('Cor.id','Cor.nome'),
  324. 'conditions' => array('Cor.ativo' => true)
  325. )),
  326. 'limitesPreco' => $this->findLimitesPreco(),
  327. 'limitesAnoModelo' => $this->findLimitesAnoModelo()
  328. );
  329.  
  330. switch($model){
  331. case 'carros':
  332. $buscas = $this->Carro->getBuscaRapida();
  333. break;
  334. case 'caminhoes_onibus':
  335. $buscas = $this->CaminhoesOnibus->getBuscaRapida();
  336. break;
  337. case 'nautica':
  338. $buscas = $this->Nautica->getBuscaRapida();
  339. break;
  340. case 'motos':
  341. $buscas = $this->Moto->getBuscaRapida();
  342. break;
  343. case 'tunados':
  344. $buscas = $this->Tunado->getBuscaRapida();
  345. break;
  346. default:
  347. return $padraoGeral;
  348. }
  349.  
  350. $resultado = array_merge($padraoGeral,$buscas);
  351.  
  352. return $resultado;
  353. }
  354.  
  355. }
Add Comment
Please, Sign In to add comment