alastairwalker
Member
This post may be considered as a sequel to the post http://www.fabrikar.com/forums/inde...k-to-work-with-nested-sets.25741/#post-136803 the result of which was inconclusive.
Out of necessity I have had to recently implement a nested sets model in Fabrik.
This post is a description of the 'recipe' that I have followed in achieving this goal. The recipe is as follows:
Steps to setting up the model:
0. Assumptions made regarding the Fabrik setup:
0.1 Absolutely essential reading before you get started:
http://docs.joomla.org/Using_nested_sets
This article provides a reference for the class and methods description used.
http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/
This article provides a useful example of data to test if the nested sets model you have set up works correctly.
0.2 Plugins that need to be installed:
1. Fabrik List - PHP events
2. Fabrik Element - Calculation
0.3 Class and methods file
Copy the file 'nestedsets.php' into the Fabrik folder:
<root>/administrator/components/com_fabrik/tables/
0.4 Assumptions made regarding your experience of Fabrik.
I assume that you are comfortable in creating Lists, Forms, Groups and Elements. If this
is not the case, you should make use of the extensive tutorials listed on the Fabrik website.
So an 'intermediate' level of experience is assumed.
0.5 The files attached to this post
The files attached to this post all have the .txt extension. To use the files (i.e. as php executable files)
replace the .txt extension with the .php extension.
0.6 The supplied images
There are three supplied images:
a) Image1 showing the list with data entered from http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/
b) Image2 showing the form under New (i.e. when Add is used)
c) Image3 showing the form when Edit is used (i.e. when the form is edited).
1. Create the table in the database, using the mysql script
-- Table structure for table `jm32_nestedsets`
CREATE TABLE `jm32_nestedsets` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`parent_id` int(11) DEFAULT NULL,
`lft` int(11) NOT NULL DEFAULT '0',
`rgt` int(11) NOT NULL DEFAULT '0',
`level` int(10) unsigned NOT NULL DEFAULT '0',
`title` varchar(255) NOT NULL,
`alias` varchar(255) NOT NULL DEFAULT '',
`access` tinyint(3) unsigned NOT NULL DEFAULT '0',
`path` varchar(255) NOT NULL DEFAULT '',
`hierarchy_display` text,
`relationship` text,
`list_toc_index` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `idx_left_right` (`lft`,`rgt`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
Use PHPADMIN for this task. Paste this script into the SQL tab, and execute. The table will be built.
Note: Replace the 'jm32_' with the actual table prefix.
2. In FB, in List, build the List/Form/Group objects using New.
Create the list, under Label enter a name like 'A Nested Set'.
Under List->Data->database table, select the nestedsets table name, as created in 1. (say jm32_nestedsets).
Save the result.
Under List->Data-Group by, select Order by column as 'lft', order Ascending.
3. In FB, in Elements, select the Form filter of 'A Nested Set', and Group filter of 'A Nested Set'.
The elements will be shown.
3.1 Select the element 'parent_id',
Choose the 'databasejoin' plugin; Group: A Nested Set;
Elements->Data, select table: jm32_nestedsets, Value: id; Label: hierarchy_display
Elements->Data-where, Joins where and/or order by statement (sql): order by lft asc
3.2 Select the element 'hierarchy_display'
Choose the 'calc' plugin: Group: A Nested Set;
Elements->More, Calculation: Add the following PHP script:
$result = JTable::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_fabrik/tables');
$table = JTable::getInstance( 'nestedsets', 'Table' );
$row_id = (int) '{rowid}';
return $table->hierarchy_display($row_id);
3.3 Select the element 'relationship'
Choose the 'radiobutton' plugin: Group: A Nested Set;
Elements->Options, set up the following options:
Value: before; Label: Before; Default (no)
Value: after; Label: After; Default (no)
Value: first-child; Label: First Child; Default (Yes)
Value: last-child; Label: Last Child; Default (no)
3.4 Forms setup
There are three php scripts to be set up, using the following files:
Nestedsets-Form-Create-initial-record.php
Nestedsets-Form-New-records.php
Nestedsets-Form-Edit-records.php
These forms must be located in the path: <root>/plugins/fabrik_form/php/scripts/
Settings for each file:
3.4.1 Nestedsets-Form-Create-initial-record.php
Published: Yes
-do-: PHP
In: Both
On: New
Process Script: Before the form data is loaded (onBeforeLoad)
3.4.2 Nestedsets-Form-New-records.php
Published: Yes
-do-: PHP
In: Both
On: New
Process Script: After any images have been uploaded (onBeforeStore)
3.4.3 Nestedsets-Form-Edit-records.php
Published: Yes
-do-: PHP
In: Both
On: Edit
Process Script: After any images have been uploaded (onBeforeStore)
3.4 Groups setup
In the New mode, the following fields must appear in the form:
- parent_id (i.e. to select the Parent Category); and
- title (to describe the item)
The other fields can be added as needed, including alias, access, and path.
In the Edit mode, the following fields must appear:
- parent_id (i.e. to select the Parent Category); and
- title (to describe the item)
- reference (select the type of move action (one of: after, before, first-child, last-child).
Note: In the New action, the default action is 'first-child'.
We will need to hide the reference field in the form on New, but show it on Edit.
To do this, we need to create a special group to hold the reference element. We will call this group 'Nestedsets-
reference'.
We will use Javascript to hide the new group.
But first we need to create the new group, and assign the element 'reference' to it.
3.4.1 Create new group
Use New to create the new group.
Under Details, assign the name of 'Nestedsets-reference' to the new group.
Label: Leave this blank
Form: Select 'nestedsets'
Save and Close.
3.4.2 Assign the element 'reference' to the new group
Select Elements.
Under Select Form: Select 'Nestedsets'
Under Select Group: Select 'Nestedsets'
Click on the 'relationship' element.
Click on Group. You will see two groups listed: 'Nestedsets', and 'Nestedsets-reference'
Select 'Nestedsets-reference'
Click Save and Close. This will save the element to the new group.
Now click on Select Group: and Select 'Nestedsets'
You will find a sing field there 'reference'
3.4.3 Set up to hide the new group on New.
In Elements, select 'id'.
Under Javascript, click 'Add'.
Set up as follows as a predefined action:
Event: 'on load'
If this element: '=='
Value: leave blank
Action: Hide
Element: nestedsets-reference
Note: This simply has the effect of hiding the group when the form is loaded.
We need to make is visible on Edit.
3.4.3 Set up to show the new group on Edit
To show the group, we need to choose an element that will have a changed value on Edit, compared to New.
A good element to choose for this purpose is 'rgt'. When a new record is created the assigned value will be '1'.
(See the default field settings in Section 1.)
In Elements, select 'rgt'.
Under Javascript, click 'Add'.
Set up as follows as a predefined action:
Event: 'on load'
If this element: '<='
Value: 1
Action: Hide
Element: nestedsets-reference
Note: This simply has the effect of hiding the group if the rgt value is still the default value. Once the rgt field has been modified, the group will be shown.
Out of necessity I have had to recently implement a nested sets model in Fabrik.
This post is a description of the 'recipe' that I have followed in achieving this goal. The recipe is as follows:
Steps to setting up the model:
0. Assumptions made regarding the Fabrik setup:
0.1 Absolutely essential reading before you get started:
http://docs.joomla.org/Using_nested_sets
This article provides a reference for the class and methods description used.
http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/
This article provides a useful example of data to test if the nested sets model you have set up works correctly.
0.2 Plugins that need to be installed:
1. Fabrik List - PHP events
2. Fabrik Element - Calculation
0.3 Class and methods file
Copy the file 'nestedsets.php' into the Fabrik folder:
<root>/administrator/components/com_fabrik/tables/
0.4 Assumptions made regarding your experience of Fabrik.
I assume that you are comfortable in creating Lists, Forms, Groups and Elements. If this
is not the case, you should make use of the extensive tutorials listed on the Fabrik website.
So an 'intermediate' level of experience is assumed.
0.5 The files attached to this post
The files attached to this post all have the .txt extension. To use the files (i.e. as php executable files)
replace the .txt extension with the .php extension.
0.6 The supplied images
There are three supplied images:
a) Image1 showing the list with data entered from http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/
b) Image2 showing the form under New (i.e. when Add is used)
c) Image3 showing the form when Edit is used (i.e. when the form is edited).
1. Create the table in the database, using the mysql script
-- Table structure for table `jm32_nestedsets`
CREATE TABLE `jm32_nestedsets` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`parent_id` int(11) DEFAULT NULL,
`lft` int(11) NOT NULL DEFAULT '0',
`rgt` int(11) NOT NULL DEFAULT '0',
`level` int(10) unsigned NOT NULL DEFAULT '0',
`title` varchar(255) NOT NULL,
`alias` varchar(255) NOT NULL DEFAULT '',
`access` tinyint(3) unsigned NOT NULL DEFAULT '0',
`path` varchar(255) NOT NULL DEFAULT '',
`hierarchy_display` text,
`relationship` text,
`list_toc_index` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `idx_left_right` (`lft`,`rgt`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
Use PHPADMIN for this task. Paste this script into the SQL tab, and execute. The table will be built.
Note: Replace the 'jm32_' with the actual table prefix.
2. In FB, in List, build the List/Form/Group objects using New.
Create the list, under Label enter a name like 'A Nested Set'.
Under List->Data->database table, select the nestedsets table name, as created in 1. (say jm32_nestedsets).
Save the result.
Under List->Data-Group by, select Order by column as 'lft', order Ascending.
3. In FB, in Elements, select the Form filter of 'A Nested Set', and Group filter of 'A Nested Set'.
The elements will be shown.
3.1 Select the element 'parent_id',
Choose the 'databasejoin' plugin; Group: A Nested Set;
Elements->Data, select table: jm32_nestedsets, Value: id; Label: hierarchy_display
Elements->Data-where, Joins where and/or order by statement (sql): order by lft asc
3.2 Select the element 'hierarchy_display'
Choose the 'calc' plugin: Group: A Nested Set;
Elements->More, Calculation: Add the following PHP script:
$result = JTable::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_fabrik/tables');
$table = JTable::getInstance( 'nestedsets', 'Table' );
$row_id = (int) '{rowid}';
return $table->hierarchy_display($row_id);
3.3 Select the element 'relationship'
Choose the 'radiobutton' plugin: Group: A Nested Set;
Elements->Options, set up the following options:
Value: before; Label: Before; Default (no)
Value: after; Label: After; Default (no)
Value: first-child; Label: First Child; Default (Yes)
Value: last-child; Label: Last Child; Default (no)
3.4 Forms setup
There are three php scripts to be set up, using the following files:
Nestedsets-Form-Create-initial-record.php
Nestedsets-Form-New-records.php
Nestedsets-Form-Edit-records.php
These forms must be located in the path: <root>/plugins/fabrik_form/php/scripts/
Settings for each file:
3.4.1 Nestedsets-Form-Create-initial-record.php
Published: Yes
-do-: PHP
In: Both
On: New
Process Script: Before the form data is loaded (onBeforeLoad)
3.4.2 Nestedsets-Form-New-records.php
Published: Yes
-do-: PHP
In: Both
On: New
Process Script: After any images have been uploaded (onBeforeStore)
3.4.3 Nestedsets-Form-Edit-records.php
Published: Yes
-do-: PHP
In: Both
On: Edit
Process Script: After any images have been uploaded (onBeforeStore)
3.4 Groups setup
In the New mode, the following fields must appear in the form:
- parent_id (i.e. to select the Parent Category); and
- title (to describe the item)
The other fields can be added as needed, including alias, access, and path.
In the Edit mode, the following fields must appear:
- parent_id (i.e. to select the Parent Category); and
- title (to describe the item)
- reference (select the type of move action (one of: after, before, first-child, last-child).
Note: In the New action, the default action is 'first-child'.
We will need to hide the reference field in the form on New, but show it on Edit.
To do this, we need to create a special group to hold the reference element. We will call this group 'Nestedsets-
reference'.
We will use Javascript to hide the new group.
But first we need to create the new group, and assign the element 'reference' to it.
3.4.1 Create new group
Use New to create the new group.
Under Details, assign the name of 'Nestedsets-reference' to the new group.
Label: Leave this blank
Form: Select 'nestedsets'
Save and Close.
3.4.2 Assign the element 'reference' to the new group
Select Elements.
Under Select Form: Select 'Nestedsets'
Under Select Group: Select 'Nestedsets'
Click on the 'relationship' element.
Click on Group. You will see two groups listed: 'Nestedsets', and 'Nestedsets-reference'
Select 'Nestedsets-reference'
Click Save and Close. This will save the element to the new group.
Now click on Select Group: and Select 'Nestedsets'
You will find a sing field there 'reference'
3.4.3 Set up to hide the new group on New.
In Elements, select 'id'.
Under Javascript, click 'Add'.
Set up as follows as a predefined action:
Event: 'on load'
If this element: '=='
Value: leave blank
Action: Hide
Element: nestedsets-reference
Note: This simply has the effect of hiding the group when the form is loaded.
We need to make is visible on Edit.
3.4.3 Set up to show the new group on Edit
To show the group, we need to choose an element that will have a changed value on Edit, compared to New.
A good element to choose for this purpose is 'rgt'. When a new record is created the assigned value will be '1'.
(See the default field settings in Section 1.)
In Elements, select 'rgt'.
Under Javascript, click 'Add'.
Set up as follows as a predefined action:
Event: 'on load'
If this element: '<='
Value: 1
Action: Hide
Element: nestedsets-reference
Note: This simply has the effect of hiding the group if the rgt value is still the default value. Once the rgt field has been modified, the group will be shown.
Attachments
-
Image1.png37 KB · Views: 1,163
-
Image2.png19.7 KB · Views: 951
-
Image3.png20.4 KB · Views: 1,051
-
Nestedsets-Form-Create-initial-record-php.txt679 bytes · Views: 481
-
Nestedsets-Form-Edit-records-php.txt1.3 KB · Views: 466
-
Nestedsets-Form-New-records-php.txt1.2 KB · Views: 453
-
nestedsets-php.txt4.8 KB · Views: 537