Advertisement
Guest User

Shock

a guest
Aug 21st, 2009
1,418
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.83 KB | None | 0 0
  1. <?php
  2. function generateData ($size, $depth, $parent) {
  3.     $result = array();
  4.     for ($i = 0; $i < $size; $i++) {
  5.         $elem = new Elem;
  6.         $elem->setData ($parent ? $parent->getData() . '.' . ($i+1) : '#'.($i+1));
  7.         $elem->setChildren ($depth ? generateData($size, $depth-1, $elem) : array());
  8.         $elem->setParent ($parent);
  9.         $result[] = $elem;
  10.     }
  11.     return $result;
  12. }
  13.  
  14. function insertTreeRecursive ($tree) {
  15.     $qElem = "INSERT INTO `Elements` (`ID`, `Data`) VALUES (NULL, '%s')";
  16.     $qHier = "INSERT INTO `ElementsHierarchy`(`ElemID`,`ParentID`,`Level`) VALUES (%s)";
  17.     $qParents = "SELECT * FROM `ElementsHierarchy` WHERE `ElemID` = '%d'";
  18.     foreach ($tree as $elem) {
  19.         $query   = sprintf($qElem, Db::escape($elem->getData()));
  20.         $elem->setId(Db::execute($query));
  21.         $parents = array();
  22.         if ($elem->getParent()) {
  23.             $parentId = $elem->getParent()->getId();
  24.             $parentParents = Db::select(sprintf($qParents, $parentId));
  25.             foreach ($parentParents as $k => $v) {
  26.                 $parents[] = join (',', array ($elem->getId(), $v['ParentID'] ? $v['ParentID'] : 'NULL', $v['Level']+1));
  27.             }
  28.             $parents[] = join (',', array ($elem->getId(), $parentId, 1));
  29.             $parentsString = join ('), (', $parents);
  30.         } else {
  31.             $parentsString = join (',', array($elem->getId(), 'NULL', 1));
  32.         }
  33.         Db::execute(sprintf($qHier, $parentsString));
  34.         if ($elem->getChildren()) {
  35.             insertTreeRecursive($elem->getChildren());
  36.         }
  37.     }
  38. }
  39.  
  40. function getRowsByParent ($id) {
  41.     $id = (int) $id;
  42.     return Db::select("
  43. SELECT * FROM `Elements`
  44. INNER JOIN `ElementsHierarchy` as `Hier`
  45.     ON `Hier`.`ElemID` = `Elements`.`ID`
  46. WHERE
  47.     `Hier`.`ParentID` = '$id' OR
  48.     `Elements`.`Id` = '$id'
  49. GROUP BY `Elements`.`Id`
  50.     ");
  51. }
  52.  
  53. function getTreeByParent ($id) {
  54.     $where = !is_int($id) ? '' :
  55.         "WHERE `Hier`.`ParentID` = $id OR `Elements`.`Id` = $id";
  56.     $rows = Db::select("
  57. SELECT `Elements`.*, `Parent`.`ParentID` FROM `Elements`
  58. INNER JOIN `ElementsHierarchy` as `Hier`
  59.     ON `Hier`.`ElemID` = `Elements`.`ID`
  60. INNER JOIN `ElementsHierarchy` as `Parent`
  61.     ON (`Parent`.`ElemID` = `Elements`.`ID` AND `Parent`.`Level` = 1)
  62.     " . $where);
  63.     $tree = createElem($id);
  64.     foreach ($rows as $r) {
  65.         $parent = createElem($r['ParentID']);
  66.         $elem   = createElem($r['ID']);
  67.         $elem->setData($r['Data']);
  68.         $elem->setParent($parent);
  69.         $parent->addChild($elem);
  70.     }
  71.     return $tree;
  72. }
  73.  
  74. function createElem ($id) {
  75.     static $elements = array ();
  76.     if (empty($elements[$id])) {
  77.         $elem = new Elem;
  78.         $elem->setId($id);
  79.         $elements[$id] = $elem;
  80.     }
  81.     return $elements[$id];
  82. }
  83.  
  84.  
  85. function markTreeByParent ($id, $value) {
  86.     $value = (int) $value;
  87.     $where = !is_int($id) ? '' :
  88.         "WHERE `Hier`.`ParentID` = $id OR `Elements`.`Id` = $id";
  89.     Db::execute("
  90. UPDATE `Elements`
  91. INNER JOIN `ElementsHierarchy` as `Hier`
  92.     ON `Hier`.`ElemID` = `Elements`.`ID`
  93. SET `Elements`.`Marked` = '$value'
  94.     " . $where);
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement