function navigateToCategory(ProdutoCategoria $pC) { $array['navegador'] = $pC->getNavegador(); if ($pC->getSubCategorias()->getTotal() > 0) { $array['filhos'] = new ArrayObject(); while ($sPC = $pC->getSubCategorias()->listar('ASC', ListaProdutoCategorias::NOME)) { $array['filhos']->append(navigateToCategory($sPC)); } } else { $array['produtos'] = new ArrayObject(); while ($p = $pC->getProdutos()->listar("ASC", ListaProdutos::NOME)) { $array['produtos']->append($p); } } return $array; }
public static function todosNiveisCategorias(ProdutoCategoria $cate) { $lC = new ListaProdutoCategorias(); if (!($cat = $lC->condicoes('', $cate->getId(), ListaProdutoCategorias::ID)->listar())) { $cat = new ProdutoCategoria(); } if ($cat->disponivel) { if (!empty(self::$condicao)) { $cat->getProdutos()->condicoes('', '', '', '', "SELECT * FROM " . Sistema::$BDPrefixo . "relacionamento_produtos_categorias rpc INNER JOIN " . Sistema::$BDPrefixo . "produtos p ON p.id = rpc.produto WHERE rpc.categoria = '" . $cat->getId() . "' " . self::$condicao)->setGroup(ListaProdutos::ID); } while ($prod = $cat->getProdutos()->listar()) { $adicionar = true; if (!empty($a)) { foreach ($a as $p) { if ($p->getId() == $prod->getId()) { $adicionar = false; break; } } } if ($adicionar) { $a[count($a)] = $prod; } } while ($cats = $cat->getSubCategorias()->listar("ASC")) { $b = self::todosNiveisCategorias($cats); if (!empty($a) && !empty($b)) { foreach ($b as $prod) { $adicionar = true; foreach ($a as $p) { if ($prod->getId() == $p->getId()) { $adicionar = false; break; } } if ($adicionar) { $a[count($a)] = $prod; } } } elseif (!empty($b)) { $a = $b; } } } if (empty($a)) { $a = array(); } return $a; }
$iLPC->createRepeticao("repetir->ProdutoCategorias"); if (!empty($_GET['busca'])) { $lPC->condicoes('', "%" . $_GET['busca'] . "%", 'empresa', 'LIKE'); } $iLPC->condicao("condicaoBusca", !empty($_SESSION['nivel'])); if (!empty($_GET['categoria'])) { $lPC->condicoes('', $_GET['categoria'], ListaProdutoCategorias::ID); if ($lPC->getTotal() > 0) { $cP = $lPC->listar(); } else { $cP = new ProdutoCategoria(); } } else { $cP = new ProdutoCategoria(); } $lPC = $cP->getSubCategorias(); if (isset($_GET['json'])) { $cond = array(); if (!empty($_GET['categoria'])) { $rs['id'] = $cP->getIdCategoriaPai(); $rs['nome'] = 'Voltar'; $rs['filhos'] = ''; $cond[] = $rs; } while ($pC = $lPC->listar()) { $rs['id'] = $pC->getId(); $rs['nome'] = str_replace('"', "'", $pC->getNavegador()); $rs['filhos'] = $pC->getSubCategorias()->getTotal(); $cond[] = $rs; } echo JSON::_Encode($cond);