/ieducar/intranet/include/pmieducar/clsPmieducarCategoriaNivel.inc.php

https://github.com/gtinfo/ieducar · PHP · 526 lines · 337 code · 53 blank · 136 comment · 57 complexity · f31d061609518e1fff2d131adaa30499 MD5 · raw file

  1. <?php
  2. /**
  3. * @author Prefeitura Municipal de Itajaí
  4. *
  5. * Criado em 25/05/2007 11:08 pelo gerador automatico de classes
  6. */
  7. require_once( "include/pmieducar/geral.inc.php" );
  8. class clsPmieducarCategoriaNivel
  9. {
  10. var $cod_categoria_nivel;
  11. var $ref_usuario_exc;
  12. var $ref_usuario_cad;
  13. var $nm_categoria_nivel;
  14. var $data_cadastro;
  15. var $data_exclusao;
  16. var $ativo;
  17. // propriedades padrao
  18. /**
  19. * Armazena o total de resultados obtidos na ultima chamada ao metodo lista
  20. *
  21. * @var int
  22. */
  23. var $_total;
  24. /**
  25. * Nome do schema
  26. *
  27. * @var string
  28. */
  29. var $_schema;
  30. /**
  31. * Nome da tabela
  32. *
  33. * @var string
  34. */
  35. var $_tabela;
  36. /**
  37. * Lista separada por virgula, com os campos que devem ser selecionados na proxima chamado ao metodo lista
  38. *
  39. * @var string
  40. */
  41. var $_campos_lista;
  42. /**
  43. * Lista com todos os campos da tabela separados por virgula, padrao para selecao no metodo lista
  44. *
  45. * @var string
  46. */
  47. var $_todos_campos;
  48. /**
  49. * Valor que define a quantidade de registros a ser retornada pelo metodo lista
  50. *
  51. * @var int
  52. */
  53. var $_limite_quantidade;
  54. /**
  55. * Define o valor de offset no retorno dos registros no metodo lista
  56. *
  57. * @var int
  58. */
  59. var $_limite_offset;
  60. /**
  61. * Define o campo padrao para ser usado como padrao de ordenacao no metodo lista
  62. *
  63. * @var string
  64. */
  65. var $_campo_order_by;
  66. /**
  67. * Construtor (PHP 4)
  68. *
  69. * @param integer cod_categoria_nivel
  70. * @param integer ref_usuario_exc
  71. * @param integer ref_usuario_cad
  72. * @param string nm_categoria_nivel
  73. * @param string data_cadastro
  74. * @param string data_exclusao
  75. * @param bool ativo
  76. *
  77. * @return object
  78. */
  79. function clsPmieducarCategoriaNivel( $cod_categoria_nivel = null, $ref_usuario_exc = null, $ref_usuario_cad = null, $nm_categoria_nivel = null, $data_cadastro = null, $data_exclusao = null, $ativo = null )
  80. {
  81. $db = new clsBanco();
  82. $this->_schema = "pmieducar.";
  83. $this->_tabela = "{$this->_schema}categoria_nivel";
  84. $this->_campos_lista = $this->_todos_campos = "cod_categoria_nivel, ref_usuario_exc, ref_usuario_cad, nm_categoria_nivel, data_cadastro, data_exclusao, ativo";
  85. if( is_numeric( $ref_usuario_cad ) )
  86. {
  87. if( class_exists( "clsPmieducarUsuario" ) )
  88. {
  89. $tmp_obj = new clsPmieducarUsuario( $ref_usuario_cad );
  90. if( method_exists( $tmp_obj, "existe") )
  91. {
  92. if( $tmp_obj->existe() )
  93. {
  94. $this->ref_usuario_cad = $ref_usuario_cad;
  95. }
  96. }
  97. else if( method_exists( $tmp_obj, "detalhe") )
  98. {
  99. if( $tmp_obj->detalhe() )
  100. {
  101. $this->ref_usuario_cad = $ref_usuario_cad;
  102. }
  103. }
  104. }
  105. else
  106. {
  107. if( $db->CampoUnico( "SELECT 1 FROM pmieducar.usuario WHERE cod_usuario = '{$ref_usuario_cad}'" ) )
  108. {
  109. $this->ref_usuario_cad = $ref_usuario_cad;
  110. }
  111. }
  112. }
  113. if( is_numeric( $ref_usuario_exc ) )
  114. {
  115. if( class_exists( "clsPmieducarUsuario" ) )
  116. {
  117. $tmp_obj = new clsPmieducarUsuario( $ref_usuario_exc );
  118. if( method_exists( $tmp_obj, "existe") )
  119. {
  120. if( $tmp_obj->existe() )
  121. {
  122. $this->ref_usuario_exc = $ref_usuario_exc;
  123. }
  124. }
  125. else if( method_exists( $tmp_obj, "detalhe") )
  126. {
  127. if( $tmp_obj->detalhe() )
  128. {
  129. $this->ref_usuario_exc = $ref_usuario_exc;
  130. }
  131. }
  132. }
  133. else
  134. {
  135. if( $db->CampoUnico( "SELECT 1 FROM pmieducar.usuario WHERE cod_usuario = '{$ref_usuario_exc}'" ) )
  136. {
  137. $this->ref_usuario_exc = $ref_usuario_exc;
  138. }
  139. }
  140. }
  141. if( is_numeric( $cod_categoria_nivel ) )
  142. {
  143. $this->cod_categoria_nivel = $cod_categoria_nivel;
  144. }
  145. if( is_string( $nm_categoria_nivel ) )
  146. {
  147. $this->nm_categoria_nivel = $nm_categoria_nivel;
  148. }
  149. if( is_string( $data_cadastro ) )
  150. {
  151. $this->data_cadastro = $data_cadastro;
  152. }
  153. if( is_string( $data_exclusao ) )
  154. {
  155. $this->data_exclusao = $data_exclusao;
  156. }
  157. if( ! is_null( $ativo ) )
  158. {
  159. $this->ativo = $ativo;
  160. }
  161. }
  162. /**
  163. * Cria um novo registro
  164. *
  165. * @return bool
  166. */
  167. function cadastra()
  168. {
  169. if( is_numeric( $this->ref_usuario_cad ) && is_string( $this->nm_categoria_nivel ) )
  170. {
  171. $db = new clsBanco();
  172. $campos = "";
  173. $valores = "";
  174. $gruda = "";
  175. if( is_numeric( $this->ref_usuario_cad ) )
  176. {
  177. $campos .= "{$gruda}ref_usuario_cad";
  178. $valores .= "{$gruda}'{$this->ref_usuario_cad}'";
  179. $gruda = ", ";
  180. }
  181. if( is_string( $this->nm_categoria_nivel ) )
  182. {
  183. $campos .= "{$gruda}nm_categoria_nivel";
  184. $valores .= "{$gruda}'{$this->nm_categoria_nivel}'";
  185. $gruda = ", ";
  186. }
  187. $campos .= "{$gruda}data_cadastro";
  188. $valores .= "{$gruda}NOW()";
  189. $gruda = ", ";
  190. $campos .= "{$gruda}ativo";
  191. $valores .= "{$gruda}'1'";
  192. $gruda = ", ";
  193. $db->Consulta( "INSERT INTO {$this->_tabela} ( $campos ) VALUES( $valores )" );
  194. return $db->InsertId( "{$this->_tabela}_cod_categoria_nivel_seq");
  195. }
  196. return false;
  197. }
  198. /**
  199. * Edita os dados de um registro
  200. *
  201. * @return bool
  202. */
  203. function edita()
  204. {
  205. if( is_numeric( $this->cod_categoria_nivel ) && is_numeric( $this->ref_usuario_exc ) )
  206. {
  207. $db = new clsBanco();
  208. $set = "";
  209. if( is_numeric( $this->ref_usuario_exc ) )
  210. {
  211. $set .= "{$gruda}ref_usuario_exc = '{$this->ref_usuario_exc}'";
  212. $gruda = ", ";
  213. }
  214. if( is_numeric( $this->ref_usuario_cad ) )
  215. {
  216. $set .= "{$gruda}ref_usuario_cad = '{$this->ref_usuario_cad}'";
  217. $gruda = ", ";
  218. }
  219. if( is_string( $this->nm_categoria_nivel ) )
  220. {
  221. $set .= "{$gruda}nm_categoria_nivel = '{$this->nm_categoria_nivel}'";
  222. $gruda = ", ";
  223. }
  224. if( is_string( $this->data_cadastro ) )
  225. {
  226. $set .= "{$gruda}data_cadastro = '{$this->data_cadastro}'";
  227. $gruda = ", ";
  228. }
  229. $set .= "{$gruda}data_exclusao = NOW()";
  230. $gruda = ", ";
  231. if( ! is_null( $this->ativo ) )
  232. {
  233. $val = dbBool( $this->ativo ) ? "TRUE": "FALSE";
  234. $set .= "{$gruda}ativo = {$val}";
  235. $gruda = ", ";
  236. }
  237. if( $set )
  238. {
  239. $db->Consulta( "UPDATE {$this->_tabela} SET $set WHERE cod_categoria_nivel = '{$this->cod_categoria_nivel}'" );
  240. return true;
  241. }
  242. }
  243. return false;
  244. }
  245. /**
  246. * Retorna uma lista filtrados de acordo com os parametros
  247. *
  248. * @param integer int_ref_usuario_exc
  249. * @param integer int_ref_usuario_cad
  250. * @param string str_nm_categoria_nivel
  251. * @param string date_data_cadastro_ini
  252. * @param string date_data_cadastro_fim
  253. * @param string date_data_exclusao_ini
  254. * @param string date_data_exclusao_fim
  255. * @param bool bool_ativo
  256. *
  257. * @return array
  258. */
  259. function lista( $int_cod_categoria_nivel = null, $int_ref_usuario_exc = null, $int_ref_usuario_cad = null, $str_nm_categoria_nivel = null, $date_data_cadastro_ini = null, $date_data_cadastro_fim = null, $date_data_exclusao_ini = null, $date_data_exclusao_fim = null, $bool_ativo = null )
  260. {
  261. $sql = "SELECT {$this->_campos_lista} FROM {$this->_tabela}";
  262. $filtros = "";
  263. $whereAnd = " WHERE ";
  264. if( is_numeric( $int_cod_categoria_nivel ) )
  265. {
  266. $filtros .= "{$whereAnd} cod_categoria_nivel = '{$int_cod_categoria_nivel}'";
  267. $whereAnd = " AND ";
  268. }
  269. if( is_numeric( $int_ref_usuario_exc ) )
  270. {
  271. $filtros .= "{$whereAnd} ref_usuario_exc = '{$int_ref_usuario_exc}'";
  272. $whereAnd = " AND ";
  273. }
  274. if( is_numeric( $int_ref_usuario_cad ) )
  275. {
  276. $filtros .= "{$whereAnd} ref_usuario_cad = '{$int_ref_usuario_cad}'";
  277. $whereAnd = " AND ";
  278. }
  279. if( is_string( $str_nm_categoria_nivel ) )
  280. {
  281. $filtros .= "{$whereAnd} nm_categoria_nivel LIKE '%{$str_nm_categoria_nivel}%'";
  282. $whereAnd = " AND ";
  283. }
  284. if( is_string( $date_data_cadastro_ini ) )
  285. {
  286. $filtros .= "{$whereAnd} data_cadastro >= '{$date_data_cadastro_ini}'";
  287. $whereAnd = " AND ";
  288. }
  289. if( is_string( $date_data_cadastro_fim ) )
  290. {
  291. $filtros .= "{$whereAnd} data_cadastro <= '{$date_data_cadastro_fim}'";
  292. $whereAnd = " AND ";
  293. }
  294. if( is_string( $date_data_exclusao_ini ) )
  295. {
  296. $filtros .= "{$whereAnd} data_exclusao >= '{$date_data_exclusao_ini}'";
  297. $whereAnd = " AND ";
  298. }
  299. if( is_string( $date_data_exclusao_fim ) )
  300. {
  301. $filtros .= "{$whereAnd} data_exclusao <= '{$date_data_exclusao_fim}'";
  302. $whereAnd = " AND ";
  303. }
  304. if( ! is_null( $bool_ativo ) )
  305. {
  306. if( dbBool( $bool_ativo ) )
  307. {
  308. $filtros .= "{$whereAnd} ativo = TRUE";
  309. }
  310. else
  311. {
  312. $filtros .= "{$whereAnd} ativo = FALSE";
  313. }
  314. $whereAnd = " AND ";
  315. }
  316. $db = new clsBanco();
  317. $countCampos = count( explode( ",", $this->_campos_lista ) );
  318. $resultado = array();
  319. $sql .= $filtros . $this->getOrderby() . $this->getLimite();
  320. $this->_total = $db->CampoUnico( "SELECT COUNT(0) FROM {$this->_tabela} {$filtros}" );
  321. $db->Consulta( $sql );
  322. if( $countCampos > 1 )
  323. {
  324. while ( $db->ProximoRegistro() )
  325. {
  326. $tupla = $db->Tupla();
  327. $tupla["_total"] = $this->_total;
  328. $resultado[] = $tupla;
  329. }
  330. }
  331. else
  332. {
  333. while ( $db->ProximoRegistro() )
  334. {
  335. $tupla = $db->Tupla();
  336. $resultado[] = $tupla[$this->_campos_lista];
  337. }
  338. }
  339. if( count( $resultado ) )
  340. {
  341. return $resultado;
  342. }
  343. return false;
  344. }
  345. /**
  346. * Retorna um array com os dados de um registro
  347. *
  348. * @return array
  349. */
  350. function detalhe()
  351. {
  352. if( is_numeric( $this->cod_categoria_nivel ) )
  353. {
  354. $db = new clsBanco();
  355. $db->Consulta( "SELECT {$this->_todos_campos} FROM {$this->_tabela} WHERE cod_categoria_nivel = '{$this->cod_categoria_nivel}'" );
  356. $db->ProximoRegistro();
  357. return $db->Tupla();
  358. }
  359. return false;
  360. }
  361. /**
  362. * Retorna true se o registro existir. Caso contrário retorna false.
  363. *
  364. * @return bool
  365. */
  366. function existe()
  367. {
  368. if( is_numeric( $this->cod_categoria_nivel ) )
  369. {
  370. $db = new clsBanco();
  371. $db->Consulta( "SELECT 1 FROM {$this->_tabela} WHERE cod_categoria_nivel = '{$this->cod_categoria_nivel}'" );
  372. if( $db->ProximoRegistro() )
  373. {
  374. return true;
  375. }
  376. }
  377. return false;
  378. }
  379. /**
  380. * Exclui um registro
  381. *
  382. * @return bool
  383. */
  384. function excluir()
  385. {
  386. if( is_numeric( $this->cod_categoria_nivel ) && is_numeric( $this->ref_usuario_exc ) )
  387. {
  388. /*
  389. delete
  390. $db = new clsBanco();
  391. $db->Consulta( "DELETE FROM {$this->_tabela} WHERE cod_categoria_nivel = '{$this->cod_categoria_nivel}'" );
  392. return true;
  393. */
  394. $this->ativo = 0;
  395. return $this->edita();
  396. }
  397. return false;
  398. }
  399. /**
  400. * Define quais campos da tabela serao selecionados na invocacao do metodo lista
  401. *
  402. * @return null
  403. */
  404. function setCamposLista( $str_campos )
  405. {
  406. $this->_campos_lista = $str_campos;
  407. }
  408. /**
  409. * Define que o metodo Lista devera retornoar todos os campos da tabela
  410. *
  411. * @return null
  412. */
  413. function resetCamposLista()
  414. {
  415. $this->_campos_lista = $this->_todos_campos;
  416. }
  417. /**
  418. * Define limites de retorno para o metodo lista
  419. *
  420. * @return null
  421. */
  422. function setLimite( $intLimiteQtd, $intLimiteOffset = null )
  423. {
  424. $this->_limite_quantidade = $intLimiteQtd;
  425. $this->_limite_offset = $intLimiteOffset;
  426. }
  427. /**
  428. * Retorna a string com o trecho da query resposavel pelo Limite de registros
  429. *
  430. * @return string
  431. */
  432. function getLimite()
  433. {
  434. if( is_numeric( $this->_limite_quantidade ) )
  435. {
  436. $retorno = " LIMIT {$this->_limite_quantidade}";
  437. if( is_numeric( $this->_limite_offset ) )
  438. {
  439. $retorno .= " OFFSET {$this->_limite_offset} ";
  440. }
  441. return $retorno;
  442. }
  443. return "";
  444. }
  445. /**
  446. * Define campo para ser utilizado como ordenacao no metolo lista
  447. *
  448. * @return null
  449. */
  450. function setOrderby( $strNomeCampo )
  451. {
  452. // limpa a string de possiveis erros (delete, insert, etc)
  453. //$strNomeCampo = eregi_replace();
  454. if( is_string( $strNomeCampo ) && $strNomeCampo )
  455. {
  456. $this->_campo_order_by = $strNomeCampo;
  457. }
  458. }
  459. /**
  460. * Retorna a string com o trecho da query resposavel pela Ordenacao dos registros
  461. *
  462. * @return string
  463. */
  464. function getOrderby()
  465. {
  466. if( is_string( $this->_campo_order_by ) )
  467. {
  468. return " ORDER BY {$this->_campo_order_by} ";
  469. }
  470. return "";
  471. }
  472. }
  473. ?>