Created
January 7, 2012 14:48
-
-
Save Marax/1574929 to your computer and use it in GitHub Desktop.
Traverzování nad stromem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class AddCategory extends AppForm { | |
public function __construct($parent, $name) { | |
parent::__construct($parent, $name); | |
$pres = $this->getPresenter()->getApplication()->getService('ModelLoader'); | |
//$this->addProtection('Login timeout. Please, try it again.'); | |
$kategorie = $pres->getModel('ShopCategory')->findAll($order = 'lft ASC', $where = NULL, NULL, NULL); | |
$kate = array(); | |
foreach ($kategorie as $kat) { | |
$nazev = str_repeat("-", $kat->level) . " " . $kat->nazev; | |
$kate[$kat->id] = $nazev; | |
} | |
$this->addText('nazev', 'Nazev Kategorie:') | |
->addRule(Form::FILLED, 'Vyplňte název kategorie!'); | |
$this->addTextArea('popis', 'Popis Kategorie:'); | |
//TODO vyber obrazku | |
$this->addSelect('idRodice', 'Nadřazená kategorie:', $kate, 6); //TODO filled | |
$this->addSubmit('send', 'Uložit!'); | |
$this->onSubmit[] = array($this, 'formSubmited'); | |
} | |
public function formSubmited($form) { | |
$values = $form->getValues(); | |
$pres = $this->getPresenter(); | |
$category = $pres->getApplication()->getService('ModelLoader')->getModel('ShopCategory'); | |
$category->add($values); | |
$pres->flashMessage('Přidána nová kategorie.', 'add'); | |
$pres->redirect('Shop:manageCategories'); | |
} | |
public function formEdit(SubmitButton $button) { | |
$form = $button->form; | |
if ($form['send']->isSubmittedBy()) { | |
$values = $form->getValues(); | |
$pres = $this->getPresenter(); | |
$category = $pres->getApplication()->getService('ModelLoader')->getModel('ShopCategory'); | |
$category->edit($values); | |
$this->getPresenter()->flashMessage('Článek upraven.', 'info'); | |
} | |
$this->getPresenter()->redirect('Shop:manageCategories'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Adminer 3.3.3 MySQL dump | |
SET NAMES utf8; | |
SET foreign_key_checks = 0; | |
SET time_zone = 'SYSTEM'; | |
SET sql_mode = 'NO_AUTO_VALUE_ON_ZERO'; | |
DROP TABLE IF EXISTS `shopcategory`; | |
CREATE TABLE `shopcategory` ( | |
`id` int(4) NOT NULL AUTO_INCREMENT, | |
`idRodice` int(4) NOT NULL, | |
`lft` int(4) NOT NULL COMMENT '{LFT}', | |
`rgt` int(4) NOT NULL COMMENT '{RGT}', | |
`level` tinyint(4) NOT NULL COMMENT '{DEPTH}', | |
`nazev` varchar(50) NOT NULL, | |
`popis` text, | |
`idObrazku` int(5) DEFAULT NULL, | |
PRIMARY KEY (`id`) | |
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8; | |
INSERT INTO `shopcategory` (`id`, `idRodice`, `lft`, `rgt`, `level`, `nazev`, `popis`, `idObrazku`) VALUES | |
(1, 0, 1, 18, 0, 'Autopneu', 'Základní Kategorie.', NULL), | |
(2, 1, 2, 7, 1, 'Ráfky', 'Ráfky atd...', NULL), | |
(3, 2, 5, 6, 2, 'Ocelové Ráfky', 'Náš obchod obsahuje širokou nabídku ocelových ráfků všech možných velikostí, šířek a barev. Pokud by jste měli zájem, můžeme dle vašich specifikací objednat požadovaný ráfek. V sekci oprava ráfků dále nabízíme opravy/renovace ocelových ráfků.', NULL), | |
(4, 2, 3, 4, 2, 'Hliníkové ráfky', 'Hliníkové ráfky jsou v poslední dobou hodně oblíbené. U nás najdete jen kvalitní, prověřené kusy všech velikostí a rozměrů. Pro více informací proč jsou ráfky hliníkové lepší než ocelové klikněte zde. (TODO: Co takhle o tom stranku?)', NULL), | |
(5, 1, 8, 11, 1, 'Bazar', 'Bazarové věci', NULL), | |
(6, 5, 9, 10, 2, 'Bazarové motocykly', 'Testovací kategorie s motorkama.', NULL), | |
(9, 1, 12, 13, 1, 'Motocykly', 'Motocykly dovezené ze zahraničí.', NULL), | |
(11, 1, 14, 15, 1, 'Pneumatiky', 'Pneumatiky všech druhů.', NULL), | |
(12, 1, 16, 17, 1, 'Automobily', '', NULL); | |
-- 2012-01-07 15:49:20 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{block content} | |
<h2>{block title}Seznam kategorií{/block}</h2> | |
<ul class="kategorie"> | |
{? $temp = 0; } | |
{foreach $kategorie as $node} | |
{if $node->level > $temp} | |
<ul> | |
{? $temp = $node->level;} | |
{elseif ($node->level == $temp) && (!$iterator->isFirst())} | |
</li> | |
{elseif $node->level < $temp} | |
{? $a=str_repeat("</li></ul></li>",$temp-$node->level)} | |
{!$a} | |
{? $temp = $node->level;} | |
{/if} | |
<li><a n:href=":Front:Shop:kategorie $node->id">{$node->nazev}</a> <a href="{link Shop:editCategory $node->id}"><img src="{$basePath}/images/icons/pencil.png" alt="Editovat" /></a> | |
<a href="{link deleteCategory! $node->id}"><img src="{$basePath}/images/icons/cross.png" alt="Smazat" /></a> | |
{/foreach} | |
{? $a=str_repeat("</li></ul></li>",$temp)} | |
{!$a} | |
</ul> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class ShopCategory extends Base { | |
public function deleteCat($kategorie) { | |
$this->connection->query("UPDATE shopCategory SET lft = lft - 2 WHERE lft > " . $kategorie['rgt']); | |
$this->connection->query("UPDATE shopCategory SET rgt = rgt - 2 WHERE rgt >=" . $kategorie['rgt']); | |
$kategorie->delete(); | |
//$this->connection->table('shopCategory')->where('id',$kategorie[])delete(); | |
} | |
public function add($category) { | |
$this->connection->query("START TRANSACTION"); | |
$row = $this->connection->query("SELECT * FROM shopCategory WHERE id = " . $category["idRodice"] . " FOR UPDATE")->fetch(); | |
Debug::barDump($row['rgt']); | |
$this->connection->query("UPDATE shopCategory SET lft = lft + 2 WHERE lft > " . $row['rgt']); | |
$this->connection->query("UPDATE shopCategory SET rgt = rgt + 2 WHERE rgt >=" . $row['rgt']); | |
$category['lft'] = $row['rgt']; | |
$category['rgt'] = $row['rgt'] + 1; | |
$category['level'] = $row['level'] + 1; | |
$this->connection->query('INSERT INTO ' . $this->prefix . 'shopCategory', $category); | |
$this->connection->query("COMMIT"); | |
} | |
public function edit($category) { | |
$oldCategory = $this->connection->table("shopCategory")->get($category['actionid']); //exec("Select idRodice FROM shopCategory WHERE id = ".$category['id']); | |
$oldCategory['popis'] = $category['popis']; | |
$oldCategory['nazev'] = $category['nazev']; | |
//TODO idObrazku $oldCategory['popis'] = $category['popis']; | |
$oldCategory->update(); | |
if ($oldCategory['idRodice'] != $category['idRodice']) | |
$this->moveTree($oldCategory['id'], $category['idRodice']); | |
} | |
public function moveTree($id_uzel, $id_uzel_to) { | |
$row = $this->connection->query("SELECT * FROM shopCategory WHERE id = " . $id_uzel . " FOR UPDATE")->fetch(); | |
$row_to = $this->connection->query("SELECT * FROM shopCategory WHERE id = " . $id_uzel_to . " FOR UPDATE")->fetch(); | |
$rozdil = $row['rgt'] - $row['lft'] + 1; | |
$lft = $row_to["rgt"]; | |
$hloubka = $row_to["level"] + 1; | |
if ($lft > $row["lft"]) { | |
$lft -= $rozdil; | |
} | |
if ($lft != $row["lft"]) { | |
$min_lft = min($lft, $row["lft"]); | |
$max_rgt = max($lft + $rozdil - 1, $row["rgt"]); | |
$posun = $lft - $row["lft"]; | |
if ($lft > $row["lft"]) { | |
$rozdil = -$rozdil; | |
} | |
$this->connection->query(" | |
UPDATE shopCategory | |
SET level = level + IF(@podstrom := lft >= $row[lft] AND rgt <= $row[rgt], " . ($hloubka - $row['level']) . ", 0), | |
lft = lft + IF(@podstrom, $posun, IF(lft >= $min_lft, $rozdil, 0)), | |
rgt = rgt + IF(@podstrom, $posun, IF(rgt <= $max_rgt, $rozdil, 0)) | |
WHERE rgt >= $min_lft AND lft <= $max_rgt | |
"); | |
$this->connection->query("Update shopCategory SET idRodice = " . $row_to['id'] . " WHERE id = " . $row['id']); | |
} | |
} | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Admin_ShopPresenter extends Admin_BasePresenter { | |
public function actionManageCategories($id) { | |
$kategorie = $this->getModel('ShopCategory')->findAll($order = 'lft ASC', $where = NULL, NULL, NULL); | |
$this->template->kategorie = $kategorie; | |
} | |
public function actionManageItems($id) { | |
$items = $this->getModel('ShopItem')->findAll(); | |
$this->template->items = $items; | |
} | |
public function renderRafky() { | |
$kategorie = $this->getModel('ShopCategory')->findAll('nazev ASC', array('idRodice' => 2), NULL, NULL); | |
$this->template->kategorie = $kategorie; | |
} | |
public function actionAddCategory() { | |
} | |
public function handleDeleteItem($id) { | |
$item = $this->getModel('ShopItem')->findById($id); | |
if (!$item) { | |
$this->flashMessage('Zboží neexistuje!'); | |
$this->redirect('this'); | |
} | |
$item->delete(); | |
$this->flashMessage('Zboží ' . $item['name'] . ' smazáno', 'delete'); | |
$this->redirect('this'); | |
} | |
public function handleDeleteCategory($id) { | |
$kategorie = $this->getModel('ShopCategory')->findById($id); | |
if (!$kategorie) { | |
$this->flashMessage('Kategorie neexistuje!'); | |
$this->redirect('this'); | |
} | |
/* Smazaní a úprava stromu */ | |
$this->getModel('ShopCategory')->deleteCat($kategorie); | |
/* Přesuneme itemy z této kategorie do jiné */ | |
if ($presunuto = $this->getModel('ShopItem')->move($kategorie['id'], 0)) { //TODO Spešl kategorie na odpad? nyní přesouvá do autopalme kategorie | |
$this->flashMessage($presunuto . ' itemů přesunuto do hlavní kategorie.'); | |
} | |
$this->flashMessage('Kategorie ' . $kategorie['nazev'] . ' smazána', 'delete'); | |
$this->redirect('this'); | |
} | |
public function actionEditCategory($id) { | |
$category = $this->getModel('ShopCategory')->findById($id); | |
$this->setView('addCategory'); | |
$form = $this->getComponent('addCategory'); | |
if ($category == FALSE) { | |
$this->flashMessage("Neexistující kategorie.", "error"); | |
$this->redirect("Shop:manageCategories"); | |
} | |
$form->addHidden("actionid")->setValue($id); | |
$form->setDefaults(array( | |
'nazev' => $category['nazev'], | |
'popis' => $category['popis'], | |
'idRodice' => $category['idRodice'])); | |
$form['send']->caption = "Upravit"; | |
$form['send']->onClick = array(array($form, 'formEdit')); | |
// $this->template->clanekEdit = $clanek; | |
} | |
public function actionEditItem($id) { | |
$item = $this->getModel('ShopItem')->findById($id); | |
$this->setView('addItem'); | |
$form = $this->getComponent('addItem'); | |
if ($item == FALSE) { | |
$this->flashMessage("Neexistující položka.", "error"); | |
$this->redirect("Shop:manageItems"); | |
} | |
$form->addHidden("actionid")->setValue($id); | |
$form->setDefaults(array( | |
'name' => $item['name'], | |
'description' => $item['description'], | |
'price' => $item['price'], | |
//TODO vy,brat aktualni'galerie_id' => $category['galerie_id'], | |
'dostupnost' => $item['dostupnost'], | |
'idRodice' => $item['idRodice'])); | |
$form['send']->caption = "Upravit"; | |
$form['send']->onClick = array(array($form, 'formEdit')); | |
$this->template->item = $item; | |
$this->template->fotos = $this->getModel('Fotografie')->getItemFotos($id); //TODO | |
} | |
public function handleMakeMain($fotoId) { //TODO ajax | |
$item = $this->getModel('ShopItem')->findById($this->getParam('id')); | |
$item['foto_id'] = $fotoId; | |
$item->update(); | |
$this->redirect('this'); | |
} | |
public function createComponentAddCategory($name) { | |
$form = new AddCategory($this, $name); | |
} | |
public function createComponentAddItem($name) { | |
$form = new AddItem($this, $name); | |
} | |
//TODO Delete items and categories | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment