Skip to content

Commit 3220893

Browse files
committed
Working on ioDoctrineMenuItem::generateNestedSortableArray(). No real changes needed, but moved it into another class and added a unit test.
1 parent a0ff887 commit 3220893

File tree

3 files changed

+155
-49
lines changed

3 files changed

+155
-49
lines changed

lib/model/doctrine/PluginioDoctrineMenuItem.class.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,4 +376,52 @@ public function postSave($event)
376376
->clearCache($this);
377377
}
378378
}
379+
380+
/**
381+
* Return a JSON-encoded nested set array of this menu
382+
*
383+
* This assists in the json response expected by jQuery Nested Sortable.
384+
*
385+
* @return array|false A nested array ready to be converted to json
386+
* @author Brent Shaffer
387+
*/
388+
public function generateNestedSortableArray()
389+
{
390+
if (!$this->getNode()->isRoot())
391+
{
392+
throw new sfException('ioDoctrineMenuItem::findAllNestedsetJson() can only be called on root nodes.');
393+
}
394+
395+
$children = $this->getNode()->getDescendants();
396+
397+
// Generate a JSON-encoded Nested Set Array
398+
if ($children->count() > 0)
399+
{
400+
$childrenArr = $children->toArray();
401+
402+
$itemArray = array();
403+
$itemArray['requestFirstIndex'] = 0;
404+
$itemArray['firstIndex'] = 0;
405+
$itemArray['count'] = count($childrenArr);
406+
$itemArray['columns'] = array('“'.$this->name.'”');
407+
408+
$items = array();
409+
foreach ($childrenArr as $childArr)
410+
{
411+
$jsonItem = array(
412+
'id' => $childArr['id'],
413+
'level' => $childArr['level'],
414+
'info' => array('<strong>'.$childArr['name'].'</strong>')
415+
);
416+
$items[] = $jsonItem;
417+
}
418+
419+
// Set Nest Level
420+
$itemArray['items'] = array_values(ioDoctrineMenuToolkit::nestify($items, 1));
421+
422+
return $itemArray;
423+
}
424+
425+
return false;
426+
}
379427
}

lib/model/doctrine/PluginioDoctrineMenuItemTable.class.php

Lines changed: 3 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -112,51 +112,6 @@ public function fetchRootByName($name)
112112
->fetchOne();
113113
}
114114

115-
116-
/**
117-
* Return a JSON-encoded nested set array of available menus
118-
*
119-
* @param string $orgId
120-
* @param Doctrine_Collection $useritem
121-
* @return array|false
122-
*/
123-
public function findAllNestedsetJson($name)
124-
{
125-
$root = $this->fetchRootByName($name);
126-
127-
$children = $root->getNode()->getDescendants();
128-
129-
// Generate a JSON-encoded Nested Set Array
130-
if (0 < $children->count())
131-
{
132-
$itemResult = $children->toArray();
133-
134-
$itemArray = array();
135-
$itemArray['requestFirstIndex'] = 0;
136-
$itemArray['firstIndex'] = 0;
137-
$itemArray['count'] = count($itemResult);
138-
$itemArray['columns'] = array("&ldquo;$name&rdquo;");
139-
140-
$items = array();
141-
foreach ($itemResult as $item)
142-
{
143-
$jsonItem = array(
144-
'id' => $item['id'],
145-
'level' => $item['level'],
146-
'info' => array('<strong>'.$item['name'].'</strong>'));
147-
148-
$items[] = $jsonItem;
149-
}
150-
151-
// Set Nest Level
152-
$itemArray['items'] = array_values(ioDoctrineMenuToolkit::nestify($items, 1));
153-
154-
return $itemArray;
155-
}
156-
157-
return false;
158-
}
159-
160115
/**
161116
* Clear a tree based on a root id. Leave the root node intact
162117
*
@@ -167,9 +122,9 @@ public function findAllNestedsetJson($name)
167122
public function clearTree($root)
168123
{
169124
$nodes = $this->createQuery()
170-
->where('root_id = ?', $root['id'])
171-
->andWhere('level != ?', 0)
172-
->execute();
125+
->where('root_id = ?', $root['id'])
126+
->andWhere('level != ?', 0)
127+
->execute();
173128

174129
foreach ($nodes as $node)
175130
{

test/unit/model/doctrine/PluginioDoctrineMenuItemTest.php

Lines changed: 104 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
require_once $_SERVER['SYMFONY'].'/vendor/lime/lime.php';
55
require_once sfConfig::get('sf_lib_dir').'/test/unitHelper.php';
66

7-
$t = new lime_test(182);
7+
$t = new lime_test(183);
88

99
$t->info('1 - Test getChildrenIndexedByName().');
1010
extract(create_doctrine_test_tree($t)); // create the tree and make its vars accessible
@@ -251,3 +251,106 @@
251251
// just doing in stages so its more obvious when something fails
252252
$t->is($menu->toArray(false), $matchingMenu->toArray(false), 'The menus match non-recursively.');
253253
$t->is($menu->toArray(), $matchingMenu->toArray(), 'The full menus match recursively.');
254+
255+
$t->info('4 - Test generateNestedSortableArray()');
256+
Doctrine_Query::create()->from('ioDoctrineMenuItem')->delete()->execute();
257+
$arr = create_doctrine_test_tree($t);
258+
$rt = $arr['rt'];
259+
260+
$expected = array (
261+
'requestFirstIndex' => 0,
262+
'firstIndex' => 0,
263+
'count' => 7,
264+
'columns' =>
265+
array (
266+
'&ldquo;Root li&rdquo;',
267+
),
268+
'items' =>
269+
array (
270+
array (
271+
'id' => $arr['pt1']->id,
272+
'level' => '1',
273+
'info' =>
274+
array (
275+
'<strong>Parent 1</strong>',
276+
),
277+
'children' =>
278+
array (
279+
array (
280+
'id' => $arr['ch1']->id,
281+
'level' => '2',
282+
'info' =>
283+
array (
284+
'<strong>Child 1</strong>',
285+
),
286+
),
287+
array (
288+
'id' => $arr['ch2']->id,
289+
'level' => '2',
290+
'info' =>
291+
array (
292+
'<strong>Child 2</strong>',
293+
),
294+
),
295+
array (
296+
'id' => $arr['ch3']->id,
297+
'level' => '2',
298+
'info' =>
299+
array (
300+
'<strong>Child 3</strong>',
301+
),
302+
),
303+
),
304+
),
305+
array (
306+
'id' => $arr['pt2']->id,
307+
'level' => '1',
308+
'info' =>
309+
array (
310+
'<strong>Parent 2</strong>',
311+
),
312+
'children' =>
313+
array (
314+
array (
315+
'id' => $arr['ch4']->id,
316+
'level' => '2',
317+
'info' =>
318+
array (
319+
'<strong>Child 4</strong>',
320+
),
321+
'children' =>
322+
array (
323+
array (
324+
'id' => $arr['gc1']->id,
325+
'level' => '3',
326+
'info' =>
327+
array (
328+
'<strong>Grandchild 1</strong>',
329+
),
330+
),
331+
),
332+
),
333+
),
334+
),
335+
),
336+
);
337+
338+
$result = $rt->generateNestedSortableArray();
339+
340+
/**
341+
* If the final test doesn't pass, these could be helpful for debugging why:
342+
$t->is($result['count'], 7, '$result[count] = 7 for the 7 children under root.');
343+
$t->is($result['columns'], array('&ldquo;Root li&rdquo;'), '$result[columns] = array(Root li) .');
344+
345+
$t->is(count($result['items']), 2, '$result[items] count is 2 (pt1, pt2).');
346+
$t->is(count($result['items'][0]['children']), 3, '$result[items][0][items] count is 3 (ch1, ch2, ch3).');
347+
$t->is(count($result['items'][1]['children']), 1, '$result[items][1][items] count is 1 (ch4).');
348+
349+
$t->is($result['items'][0]['id'], $arr['pt1']->id, '$result[items][0][id] is pt1\'s id');
350+
$t->is($result['items'][0]['level'], 1, '$result[items][0][level] is 1');
351+
$t->is($result['items'][0]['info'], array('<strong>Parent 1</strong>'), '$result[items][0][info] is array(Parent 1)');
352+
353+
$t->is($result['items'][0]['children'][0]['level'], 2, '$result[items][0][children][0][level] is 2');
354+
*/
355+
356+
$t->is($result['items'][0], $expected['items'][0], '->generateNestedSortableArray() returns the correctly formatted array.');

0 commit comments

Comments
 (0)