PageRenderTime 62ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/app/classes/AppClass/App/Arvore.php

https://gitlab.com/jalon/doadoronline
PHP | 432 lines | 202 code | 78 blank | 152 comment | 53 complexity | ce164bd5ba74c11df90a5e9620903b00 MD5 | raw file
  1. <?php
  2. namespace AppClass\App;
  3. /**
  4. * Gerenciar as Arvores
  5. *
  6. * @package \AppClass\App\Arvore
  7. * @author Jalon Vitor Cerqueira Silva
  8. * @version 1.0.1
  9. */
  10. class Arvore {
  11. /**
  12. * Array de itens da árvore (Itens e Pastas)
  13. * @var array
  14. */
  15. private $itens;
  16. /**
  17. * Array
  18. * @var array
  19. */
  20. private $_array;
  21. /**
  22. * Indicador se vai exibir ou não a pasta raiz
  23. * @var Boolean
  24. */
  25. private $exibeRaiz;
  26. /**
  27. * Código da pasta raiz, quando exibida
  28. * @var string
  29. */
  30. const _codPastaRaiz = "_pastaRaiz";
  31. /**
  32. * nome da pasta raiz, quando exibida
  33. * @var string
  34. */
  35. const _nomePastaRaiz = "";
  36. /**
  37. * Construtor
  38. *
  39. * @param string $tipo
  40. * @return void
  41. */
  42. public function __construct() {
  43. /** Inicializa os arrays **/
  44. $this->itens = array();
  45. $this->_array = array();
  46. /** Define valor padrão de configurações **/
  47. $this->exibirRaiz(false);
  48. }
  49. /**
  50. * Adiciona uma pasta
  51. * @param integer $codigo
  52. * @param string $nome
  53. */
  54. public function adicionaPasta($codigo,$nome,$pastaMae) {
  55. global $system,$tr;
  56. /**
  57. * Define o código
  58. */
  59. if ($codigo == $this::_codPastaRaiz) {
  60. $id = $codigo;
  61. }else{
  62. $id = "P".$codigo;
  63. }
  64. if ($this->exibeRaiz == true) {
  65. if ($codigo == $this::_codPastaRaiz) {
  66. $mae = null;
  67. }else{
  68. $mae = ($pastaMae == null) ? $this::_codPastaRaiz : "P".$pastaMae;
  69. }
  70. }else{
  71. $mae = ($pastaMae == null) ? null : "P".$pastaMae;
  72. }
  73. /**
  74. * Verifica se o código já foi utilizado
  75. */
  76. if ($this->existeItem($id) == true) {
  77. $system->criaAviso(\AppClass\App\Aviso\Tipo::ERRO,$tr->trans("Código de pasta já existe na árvore (%s)",array("%s" => $codigo)));
  78. die('Código já existente ('.$codigo.')');
  79. }
  80. /**
  81. * Verifica se a pasta mãe existe
  82. */
  83. if (($pastaMae !== null) && ($this->existeItem($mae) == false) ) {
  84. $system->criaAviso(\AppClass\App\Aviso\Tipo::ERRO,$tr->trans("Pasta mãe não existe na árvore (%s)",array("%s" => $pastaMae)));
  85. die('Pasta mãe não existe ('.$pastaMae.')');
  86. }
  87. /**
  88. * Cria a pasta
  89. */
  90. $this->itens[$id] = new \AppClass\App\Arvore\Pasta();
  91. $this->itens[$id]->setId($id);
  92. $this->itens[$id]->setNome($nome);
  93. $this->itens[$id]->setPastaMae($pastaMae);
  94. $this->itens[$id]->setIdMae($mae);
  95. if ($codigo == $this::_codPastaRaiz) {
  96. $this->itens[$id]->setCodigo(null);
  97. }else{
  98. $this->itens[$id]->setCodigo($codigo);
  99. }
  100. return ($id);
  101. }
  102. /**
  103. * Adiciona um item
  104. * @param integer $codigo
  105. * @param string $nome
  106. */
  107. public function adicionaItem($codigo,$nome,$pastaMae) {
  108. global $system,$tr;
  109. /**
  110. * Define o ID do Objeto
  111. */
  112. $id = "I".$codigo;
  113. if ($this->exibeRaiz == true) {
  114. $mae = ($pastaMae == null) ? $this::_codPastaRaiz : "P".$pastaMae;
  115. }else{
  116. $mae = ($pastaMae == null) ? null : "P".$pastaMae;
  117. }
  118. //$mae = ($pastaMae == null) ? null : "P".$pastaMae;
  119. /**
  120. * Verifica se o código já foi utilizado
  121. */
  122. if ($this->existeItem($id) == true) {
  123. $system->criaAviso(\AppClass\App\Aviso\Tipo::ERRO,$tr->trans("Código de item já existe na árvore (%s)",array("%s" => $codigo)));
  124. die('Código já existente ('.$codigo.')');
  125. }
  126. /**
  127. * Verifica se a pasta mãe existe
  128. */
  129. if (($pastaMae !== null) && ($this->existeItem($mae) == false) ) {
  130. $system->criaAviso(\AppClass\App\Aviso\Tipo::ERRO,$tr->trans("Pasta mãe não existe na árvore (%s)",array("%s" => $pastaMae)));
  131. die('Pasta mãe não existe ('.$pastaMae.')');
  132. }
  133. /**
  134. * Cria o Item
  135. */
  136. $this->itens[$id] = new \AppClass\App\Arvore\Item();
  137. $this->itens[$id]->setId($id);
  138. $this->itens[$id]->setCodigo($codigo);
  139. $this->itens[$id]->setNome($nome);
  140. $this->itens[$id]->setPastaMae($pastaMae);
  141. $this->itens[$id]->setIdMae($mae);
  142. return ($id);
  143. }
  144. /**
  145. * Verifica se existe o item
  146. * @param integer $codigo
  147. * @return boolean
  148. */
  149. protected function existeItem($codigo) {
  150. if (!$this->itens) return false;
  151. if (array_key_exists($codigo, $this->itens)) {
  152. return true;
  153. }else{
  154. return false;
  155. }
  156. }
  157. /**
  158. * Gera o array na ordem correta de nível e ordem
  159. */
  160. public function geraArray() {
  161. global $nivel,$nivelMax;
  162. /**
  163. * Define os contadores para não deixar acontecer uma recursividade
  164. */
  165. $nivel = 0;
  166. $nivelMax = 500;
  167. /**
  168. * Primeiro percorre o nível 0, os itens que não tem pai
  169. **/
  170. foreach ($this->itens as $codigo => $obj) {
  171. if ($obj->getPastaMae() == null) {
  172. //$this->_array[$codigo] = array();
  173. //$this->_array[$codigo]['additionalParameters']['children'] = $obj->getArray();
  174. $this->_array[$codigo] = $obj->getArray();
  175. }
  176. }
  177. if ($this->_array) {
  178. /**
  179. * Encontrar os filhos
  180. **/
  181. foreach ($this->_array as $codigo => $array) {
  182. $this->descobreItensFilhos($this->_array[$codigo]['additionalParameters']['children'], $codigo);
  183. }
  184. }
  185. return ($this->_array);
  186. }
  187. /**
  188. * Gera o código JSON na ordem correta de nível e ordem
  189. */
  190. public function getJsonCode() {
  191. global $nivel,$nivelMax;
  192. /**
  193. * Define os contadores para não deixar acontecer uma recursividade
  194. */
  195. $nivel = 0;
  196. $nivelMax = 500;
  197. $json = "[ ";
  198. if ($this->exibeRaiz == true) {
  199. $_itens = $this->itens;
  200. $this->itens = null;
  201. $this->adicionaPasta($this::_codPastaRaiz,$this::_nomePastaRaiz,null);
  202. $this->itens = array_merge($this->itens, $_itens);
  203. }
  204. /**
  205. * Percorre os itens para gerar o código JSON deles
  206. **/
  207. foreach ($this->itens as $codigo => $obj) {
  208. $json .= " ".$obj->getJsonCode().",";
  209. }
  210. $json .= " ]";
  211. return ($json);
  212. }
  213. /**
  214. * Descobre os filhos do $item no $this->_array e coloca em $array
  215. * @param array $array
  216. * @param string $item
  217. */
  218. protected function descobreItensFilhos(&$array,$item) {
  219. global $nivel,$nivelMax;
  220. $nivel++;
  221. foreach ($this->itens as $codigo => $obj) {
  222. if ($obj->getPastaMae() == $item) {
  223. //$array[$codigo] = array();
  224. $array[$codigo] = $obj->getArray();
  225. //$array[$codigo]['additionalParameters']['children'] = $obj->getArray();
  226. $this->descobreItensFilhos($array[$codigo]['additionalParameters']['children'], $codigo);
  227. }
  228. if ($nivel > $nivelMax) die('Recursividade encontrada em :'.__FUNCTION__);
  229. }
  230. }
  231. /**
  232. * Definir o nível dos itens da árvore
  233. * @param array $array
  234. * @param Integer $nivel
  235. */
  236. protected function defineNivel(&$array,$nivel) {
  237. //return;
  238. foreach ($array as $cod => $arr) {
  239. $this->itens[$cod]->setNivel($nivel);
  240. if (!empty($arr)) {
  241. $this->defineNivel($array[$cod], $nivel+1);
  242. }
  243. }
  244. }
  245. /**
  246. * Filtrar os itens da árvore
  247. * @param unknown $string
  248. * @return multitype:NULL
  249. */
  250. public function filtrar($string) {
  251. global $log;
  252. if ($this->exibeRaiz == true) {
  253. $_itens = $this->itens;
  254. $this->itens = null;
  255. $this->adicionaPasta($this::_codPastaRaiz,$this::_nomePastaRaiz,null);
  256. $this->itens = array_merge($this->itens, $_itens);
  257. }
  258. $array = array();
  259. if ($this->itens) {
  260. foreach ($this->itens as $codigo => $obj) {
  261. if ($obj->getTipo() == 'item') {
  262. if (stripos(strtolower($obj->getNome()),strtolower($string)) !== false) {
  263. $array[] = $codigo;
  264. //$log->debug("Achei o Tipo: ".$codigo);
  265. }
  266. }
  267. }
  268. }
  269. /**
  270. * Deixar as pastas mãe e os tipos encontrados no filtro.
  271. */
  272. if ($this->itens && sizeof($array) > 0) {
  273. foreach ($array as $item) {
  274. $this->descobrePastasMae($array,$item);
  275. }
  276. }
  277. //$log->debug("Itens: ".serialize($this->itens));
  278. $array = array_reverse($array);
  279. $itens = array();
  280. foreach ($array as $item) {
  281. $itens[$item] = $this->itens[$item];
  282. }
  283. $this->itens = $itens;
  284. }
  285. /**
  286. * Decobrir as pastas mãe de um item
  287. * @param array $array
  288. * @param int $item
  289. */
  290. protected function descobrePastasMae(&$array,$item) {
  291. if ($this->itens) {
  292. foreach ($this->itens as $codigo => $obj) {
  293. if ($codigo == $item) {
  294. if ($obj->getIdMae() != null) {
  295. $array[] = $obj->getIdMae();
  296. $this->descobrePastasMae($array, $obj->getIdMae());
  297. }
  298. }
  299. }
  300. }
  301. }
  302. /**
  303. * Exibir a pasta raíz
  304. * @param boolean $val
  305. */
  306. public function exibirRaiz($val) {
  307. $this->exibeRaiz = $val;
  308. }
  309. /**
  310. * Adicionar um atributo customizado a um item
  311. * @param number $codigo
  312. * @param string $atributo
  313. * @param string $valor
  314. */
  315. public function adicionaAtributoItem($codigo,$atributo,$valor) {
  316. /**
  317. * Definindo o ID do objeto
  318. */
  319. $id = "I".$codigo;
  320. if (!$this->existeItem($id)) {
  321. die('Item não existe na árvore ('.$codigo.')');
  322. }
  323. $this->itens[$id]->adicionaAtributo($atributo,$valor);
  324. }
  325. /**
  326. * Adicionar um atributo customizado a uma pasta
  327. * @param number $codigo
  328. * @param string $atributo
  329. * @param string $valor
  330. */
  331. public function adicionaAtributoPasta($codigo,$atributo,$valor) {
  332. /**
  333. * Definindo o ID do objeto
  334. */
  335. $id = "P".$codigo;
  336. if (!$this->existeItem($id)) {
  337. die('Item não existe na árvore ('.$codigo.')');
  338. }
  339. $this->itens[$id]->adicionaAtributo($atributo,$valor);
  340. }
  341. /**
  342. * Desabilita um item
  343. * @param string $id
  344. */
  345. public function desabilitaItem($id) {
  346. if ($this->existeItem($id)) {
  347. if ($this->itens[$id]->getTipo() == "item") {
  348. $this->itens[$id]->setIndAtivo(false);
  349. }else{
  350. $this->itens[$id]->setIndAtiva(false);
  351. }
  352. }
  353. }
  354. }