Библиотека для cis, online, cms1
This commit is contained in:
commit
3c2e614d87
269 changed files with 39854 additions and 0 deletions
69
core/tabletree.php
Normal file
69
core/tabletree.php
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Ïðåîáðàçîâàíèå äåðåâà èç ìîäåëè Plain â ìàññèâ ìàññèâîâ (Adjacency List)
|
||||
*/
|
||||
|
||||
require_once 'core/functions.php';
|
||||
|
||||
define (SORT_DESC, 0); // descending
|
||||
define (SORT_ASC, 1); // ascending
|
||||
|
||||
/**
|
||||
* Âûáèðàåò âñå ñðîêè èç òàáëèöû ñ óíèêàëüíûìè çíà÷åíèÿìè êëþ÷à
|
||||
* @param $name Èìÿ êëþ÷à
|
||||
* @param $table Äâóõìåðíûé ìàññèâ
|
||||
* @example
|
||||
* key_unique_values ('name', array (array ('name' => 1), array ('name' => 2), array ('name' => 1)))
|
||||
=> array (1, 2)
|
||||
* @end example
|
||||
*/
|
||||
function key_unique_values ($name, $table) {
|
||||
// Èùåì óíèêàëüíûå çíà÷åíèÿ äëÿ çàäàííîãî êëþ÷à
|
||||
$keys = array ();
|
||||
foreach ($table as $row) {
|
||||
if (!in_array ($row[$name], $keys))
|
||||
$keys[] = $row[$name];
|
||||
}
|
||||
return $keys;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ñîðòèðîâêà äâóìåðíîãî ìàññèâà ïî çàäàííîìó êëþ÷ó
|
||||
* @param $array Ìàññèâ
|
||||
* @param $key Èìÿ êëþ÷à ïî çíà÷åíèþ êîòîðîãî áóäåò èäòè ñðàâíåíèå
|
||||
* @return Îòñîðòèðîâàííûé ìàññèâ
|
||||
*/
|
||||
function sortOn($array, $key, $fn = '__cmp') {
|
||||
usort ($array, rcurry($fn, $key));
|
||||
//usort ($array, create_function ('$x,$y', 'return __cmp ($x, $y, "'.$key.'");'));
|
||||
return $array;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Îáõîäèò òàáëèöó êàê äåðåâî
|
||||
* @param $level Array Óðîâíè âëîæåííîñòè
|
||||
* @param $table Òàáëèöà
|
||||
* @param $fn Ôóíêöèÿ êîòîðàÿ ïðèìåíÿåòñÿ ê êàæäîé âåòêå äåðåâà
|
||||
* $fn ($name, $index, $rows, $cc)
|
||||
* @param $name Êëþ÷ óðîâíÿ
|
||||
* @param $index Çíà÷åíèå êëþ÷à óðîâíÿ
|
||||
* @param $rows Âñå ñòîëáöû òåêóùãî óðîâíÿ
|
||||
* @parma $cc Ñòîëáöû áîëåå íèçêîãî óðîâíÿ
|
||||
*/
|
||||
function tableTreeWalk($level, $table, $fn) {
|
||||
if (empty ($level)) return $table;
|
||||
$name = array_shift ($level);
|
||||
|
||||
$keys = key_unique_values($name, $table);
|
||||
$data = array ();
|
||||
foreach ($keys as $index) {
|
||||
list($rows, $table) = partition (lcurry('__index', $index, $name), $table);
|
||||
// $rows = array_filter ($table, lcurry('__index', intval($index), $name));
|
||||
//$rows = array_filter ($table, create_function ('$x', 'return __index ('.intval($index).', \''.$name.'\', $x);'));
|
||||
$data[$index] = call_user_func ($fn, $name, $index, $rows, tableTreeWalk ($level, $rows, $fn));
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue