Making a Tag List Widget

Posted by DavidCramer | February 15, 2012
 

In this Tutorial we’re going to build an interface that can be used as a tag list widget.

We’ll explore the use of Joins and Grouping in this project and should give you a better understanding of connecting data together.

This is going to be build live on this site, so you’ll be able to see the widget in this posts sidebar ->

The first step is to Create a New Applicaiton called “Widgets” give it the description “Widgets for my site”.

Create a New Interface in your widgets app and call “Tags” With the Description of “Tag view widget” Leave Navigation alone as its only a display interface for the front end.

Now on your new interface in the interface list, click on the edit link to edit it.

Now we’re going to setup the Table and Fields. So click on the “Fields Setup” tab.For the Table, select “wp_term_relationships” where wp_ is the prefix of your installation. You Should see the Fields of the selected table load below in the “Define Fieldtypes” section.

The Fields listed are:

  • object_id
  • term_taxonomy_id
  • term_order

What we are needing is the tag name, which is found in the wp_terms table. but there is no direct relationship value in wp_term_relationships. But there is a relationship value in wp_term_taxonomy. So what we need to do is create a join from our primary table “wp_term_relationships” to “wp_term_taxonomy” and then join the value retrieved from “wp_term_taxonomy” to “wp_terms” which then can give us the tag name. Complicated sounding I know, but simple when you start doing it.

Lets jump back to setting up our fields. We wont be using the “object_id” field so leave that as it is.

Set the field “term_taxonomy_id” to “Join Table” and set it up like the following:

join1[1]

You may also see a “Sort By” option. you can leave that as it is.

So. the first part is done. We are returning the term_id from wp_term_taxonomy. We now need to take this value and join it to the wp_terms table so that we can return the tag name.

Now lets make a Virtual Field. Click the Virtual Field button is just below the “Define Fieldtypes” title if you don’t see the button, Click the Save button at the bottom of the interface and re-edit it. Virtual Fields are only apply-able to interfaces that have a saved table.

Once saved and opend again, head back to the Fields Setup tab and click the Virtual Field button.

A new field will appear below the button. Drag the field below the “term_taxonomy_if field we just worked on. the new field will be indented when placed below the field.

In your Virtual Field, Set the “Master Field” to “term_taxonomy_id”  Set the field as visible by clicking the eye icon so it highlights green and then set its field type to Join Table and configure it like the following:

Screen-shot-2012-02-15-at-10.30.33-AM[1]

Be sure to click the advanced button to set the join type

So what we are now doing is taking the value from the first join and then joining that value to the wp_terms table to retrieve the actual tag name. What that does is in the list of the initial table wp_term_relationships entries, the the term_taxonomy_id is translated as a term_id which in tern is translated as a name. the name we’re going to show. You’ll aslo see that you have set the Join Type to Right Join. Let me try explain how the join types work

When you join a table the two tables are places side by side On to the left and One to the Right. The left table is the table you running your query on in this case its wp_term_taxonomy the table to the right is the joined table in this case wp_terms. Setting the join type is stating which table take priority.

Setting it to join left will be saying return all the results found in the left table. So if one of the values in the left doesnt have a value in the right you’ll get a blank entry because its returning the values from the left regardless of whether it gets a value fro the right or not.
Setting it to join right will be saying return all the results found in the right table. so if the left doesn’t have a value in the right, it wont return it as it will only return the rights value or found values

Setting it to just a join will give no preference to either so will return everything regardless.

We are setting it to right join because we only want tags . We set the term_taxonomy_id to join where taxonomy = post_tag so it wont find a connection for the wp_term_relationship on other terms meaning that there wont be a match from term_taxonomy_id to wp_terms so giving preference to the right, means we’ll only have tag names returned. Yikes that’s complicated!

Ok now We need to get the Tag Slug for the link. So Click on add Virtual Field and drag the field below the one you made above.

Click on the Little gear icon and change the title to Tag Slug.

Setup the field exactly like the one above, only where it says “Value Field” choose slug instead of name.

it should look like this.

Screen-shot-2012-02-15-at-12.32.20-PM[1]

OK so now Click the Save Button and then go view the interface by clicking the view link on the interface.

So now what you should see is essentially a list of all the tags in the system. But you’ll see duplicates of where tags have been used multiple times. so what we need to do is group them together so we only get 1 result for each tag.

So go edit the interface again and click on Fields Setup.

Add another Virtual Field and drag it below the first Virtual Field you created previously.

Click on the Little gear icon on the left corner to open the field properties and change the Title to “Tag Count”.
Then set its master as “term_taxonomy_id” an set the field to visible. Now set the fieldtype to Field Group.
In the fieldtype config panel that opens up, set the Grouping Action to Count and the Action Field to object_id. It should look like the image below.

Screen-shot-2012-02-15-at-11.12.41-AM[1]
What this is going to do is take the term_taxonomy_id field and where its repeated in the results, group them together and return them as a single entry. then for each of the items thats in the group its going to count the unique ones. we set the count to be object_id because this is a unique field so each one will be unique and therfore give us the exact number if entries per group. This virtual fields value will then be the number of items per tag or group.

So now Click Save and view your interface again. You’ll now no repeats with a column showing how many times the tag has been used. Awesome.

Now lets sort the tags by the number of times each is present. Edit the interface again and go to the Fields Setup. Scroll right down to Sort Field and select “Tag Count” or the title you gave your Field Group, as your sorting field and set it “Descending” and Click Save.

View your interface again and now you should see you most used Tags in DESCENDING ORDER

Great. We’re almost done. Now to give the interface a nice looking Template and we’re ready to add it to our side bar. Let get to styling.

Go edit the interface again.

Go to the General Setting tab.
Check the “Hide New Item Button” as we don’t want inserting ability.
In the “Items Per Page” you can set how many you want to show. If you want to show the Top 10 tags, enter 10. if you want to show all the tags enter 0. I’m going to enter 10 for this example.
scroll to Toolbar Setting and uncheck everything. Actually you can uncheck everything right down to the bottom.

Now go to the Permissions Tab. We want to have this interface visible to everyone, so set its permission to public. Its the very first one in the list.

Head on to the templates tab.

Click the Enable Checkbox to tell the interface to use the template. Set the Wrapper Element to div (just the word div not) and the Wrapper Class to “tagLinks”

Now in the Header Template box, we’ll define our css style. You can add this style to your template style.css to be cleaner if you want, but for this we’re adding it here.

Enter the following css code in your Header Template box. You can change it how you want your tags to look.

<style>
.tagLinks a {
background-color: #EFEFEF;
border: 1px solid #CCCCCC;
border-radius: 3px;
color: #333333 !important;
display: inline-block;
font-size: 11px;
margin: 3px;
padding: 2px 4px;
text-decoration: none;
}
</style>

Scroll down to the “Before” box and use the following Code for the widget title:

<h4 class=”widgetTitle”>Tags</h4>

You can call it what ever you want and use the style class that matches your them for better customisation.

Scroll down to the “Content” box. This is the loop area for each result. input the following code.

<a href=”/tag/{{__4f3b860ec42e1}}/”>{{__4f3b659c3be98}}</a>

Now depending on how you have set up your permalinks the href will change, set it how its appropriate to your config. I think this is most common.

The code {{_4f3b860ec42e1}} and {{__4f3b659c3be98}} are the generated ID of the Virtual Fields that joins to the tag name and tag slug.

The slug goes in the href and the name between the a tags.

It will be different for you. so you’ll need to click on the little blue ? icon to the top of the templates pane, you can see which code to use in that box. its the only one that doesn’t have a title. copy it and replace it in the example i gave above.I left of giving a title to the first virtual field so that you can see how they look untitled in the templates.

Screen-shot-2012-02-15-at-12.22.53-PM1[1]

When done it should look like this:

Screen-shot-2012-02-15-at-12.00.53-PM[1]

Right, with that done,

Click on save and view your interface. You’re done!

Now you can go to your widgets page, drop in the DB-Toolkit widget, select your app “widgets” save, then select your interface and save again.

Screen-shot-2012-02-15-at-11.57.58-AM[1]

And that’s it. You should have a tags list like the one to the right in my side bar.

One to Many Relationship Tutorial in DB-Toolkit

Posted by DavidCramer | February 14, 2012
 

In the last week alone I have had 8 requests to help with one to many relationships in DB-Toolkit. So I’ve decided to show how its possible to do this.

The initial idea was and still is (once its done) is to have the join fieldtype have multiselect options working properly, but until then this is my preferred method to do this kind or relationship.

In this tutorial I’m going to do feature based example. It will be a listing of camera’s and have a one to many relationship of features.

Before i start any application, I create my manager interfaces for the application. Manager interfaces are admin level screens for full management of data. something like the users page, where you can manage all the users details. By doing this, we get the chance to fully create the tables needed for the application and are able to build test data.

Lets start with the features interface: Co to the DB-Toolkit App Builder. If you have an app open, close it and create a new app called cameras.

Screen-shot-2012-02-14-at-8.24.44-AM[1]Once your new app is created, we’ll create the “Features” manager. Click on “New Interface” to start with the creation of our features manager for managing the main features. Screen-shot-2012-02-14-at-8.29.44-AM[1]

Interface Tab

Set the Interface Title to: “Manage Features” and the description “Admin feature management” (the description is purely to let you know what the interface is for).
In the Navigation section, leave the Group blank as we wont be using that since this is a small app. Set the Menu Label to “Features”. leave the other settings on the tab alone as they are not needed.

Fields Setup Tab

On the select table entry, click “Add New” and call your table “App Feature” (I like using a singular term so the auto id is singular) and click “Create”

Once this is done you’ll get your first field added “app_featureID” as in the image below.

Screen-shot-2012-02-14-at-8.38.10-AM[1]

The app_featureID field is our Auto-Increment Field to keep each entry unique. this field is always set to ignore in management interfaces.

Screen-shot-2012-02-14-at-8.41.27-AM[1]

Add a new Field and call it “Feature” This is going to be the actual title we see when we select it for a listing. Once you see it added to your fields list, add another field and call it Description. This is to be a small blurb describing the feature.

Once you have created your two fields, its time to set them to the fieldtypes they are going to be.

In this case the Feature field will be a “Single Text Field” and the Description will be a “Text Area”.

On the Feature field, click the Ignore button to open the fieldtype selector menu and select  “Single Text Input” from the “Text” fields.

Screen-shot-2012-02-14-at-8.46.52-AM[1]

Once you click on the Single Text Field, the menu will change to a config panel. you can leave the settings in here as the are and hide it by clicking the “Setup” button to the bottom right. You have now set this fields fieldtype to single text field.

Screen-shot-2012-02-14-at-8.51.54-AM[1]Screen-shot-2012-02-14-at-8.50.14-AM[1]

We want to have this field visible on the manager screen, so click on the little eye icon to set it as visible. It will highlight green when is visible.

For the Description Field, set it’s fieldtype to “Text Area” and also set it to visible. as we want to see it in the manager.

That’s it. You can now click “Save” all the other settings in other tabs are preset for manager screens, so we wont be changing anything else.

You should now have an interface in your application that looks similar to the image below.

Screen-shot-2012-02-14-at-8.54.47-AM[1]Click on the View link in the interface listing and take a look at what we have created.

Click on the “Add Entry” button to create the first usable feature. I’m making one called “Interchangeable lens” with the description “Lenses can be changed to provide different focal lengths.” and click save. You now have a feature listed in your feature manager. Go an add a few more of your own that relates to cameras.

Once you have added 6 features, or as many as you wanted. your manager will look similar to the image bellow.

Screen-shot-2012-02-14-at-9.05.25-AM[1]Now let’s go create the Camera Manager.

Go back to DB-Toolkit -> App Builder and click new Interface.

Call Your interface “Camera Manager” and description “Admin camera management”

Set the menu label to Cameras and go to the Field Setup tab.

Add a new table and call it “app camera” and click create.

Add 3 fields: Model, Make and Year. (just keeping it simple for now.)

Set the fieldtypes for all 3 fields to “single text field” and make all 3 visible. It should look like the image below.

Screen-shot-2012-02-14-at-10.55.13-AM1[1]Quickly go to the General Settings tab, scroll right down to the bottom and check the Disable Notifications checkbox.

click save.

Now you should have 2 interfaces in you app. click on view link in the Manage Cameras interface and add a camera.

Now you have features and a camera, but we want to be able to assign many features to a camera. The idea is that we need a table to keep all the features that the camera has. What we need is a camera feature interface. so let’s go make one.

Go to your app builder and click Create Interface. Call it “Camera features” and the description “Camera Feature Relation” there will be no menu label in this one.

In the Field Setup, add a new table called “app camera feature” and click create.

Add two fields “cameraid” and “featureid”

Set the cameraid fieldtype to “selected item filter”

Screen-shot-2012-02-14-at-9.22.37-AM[1]

The selected item filter allows the interface to capture and show only entries that match the id present we don’t need this field to be visible as it’s a control not a display field. I’ll show and explain it all a little later.

The featureid is going to be a Join fieldtype. so click on the ignore field button to change its fieldtype and select “Join Table” You’ll then get a config panel for the join that will look like the image bellow.

Screen-shot-2012-02-14-at-9.25.45-AM[1]So what we are going to do is have this show us a dropdown of all the features when adding a new feature to a listing. this way we can select the feature we want and add it to the list. So in the Table selection, select the features table we created earlier. is should be “wp_app_feature” where wp is your wordpress prefix. once selected you’ll get the next part of the setup load up.

Screen-shot-2012-02-14-at-9.28.59-AM[1]To try to explain what the settings are:

Reference Field: this is the field that is actually stored in the database. as a join we want to store the ID or at least a unique value. this is so we can reference a full entry from another table without getting the wrong entry. the ID will always be unique so go with that.
Value Field: this is the value we want to pull from the join table. Essentially the value we see to select. the little green + synbol adds another field if we want to add another display field. this is usefull if we join to a user table and want to show both the firstname and lastname in the selector or a list.
Join WHERE: this is a specific filter applied to the selector. it will only list items to select where the WHERE field is = the value you give it. an example of its use would be if the table you joining to had an enabled field, you can select the join WHERE to be that enabled field and set the = value to 1. then it will only allow you to select entries that have been enabled. Leaving this field blank lists all the entries. we’ll leave it blank.
Sort by: this is the field you want to sort the list by and in which direction.
Select Type: you have the options of:

  • Autocomplete – this allows you to start typing and then it will give you matchin options from the table to select. )Great for joining to a table of lots of entries like countries)
  • Dropdown – a simple dropdown list of the entries in the table.
  • Checkbox – meant to be the rudimentary one to many type. its faulty and need work! consider it not working.
  • RadioGroup – a radio selector allows you to select one option from the tables entries. use it only on small tables that have only a few entries.

Single Select checkbox: This sets the filter selection to be a single selector so you can only filter by a single entry at a time.

Leave the advanced button for now. well get to it later.

Right lets setup the join.

Reference Field: leave it as it is. app_featureID
Value Field: Set it to app_feature
Select Type: Dropdown

click setup button on the bottom right to hide this panel.

We want this field to be visible so set it visible by clicking the little eye icon so it’s highlighted in green.

General Settings Tab

We don’t want all the options available for the user on this interface so we’re going to remove a few options. well pretty much all of the.

go down to “New Item Title” and change it to “Add Feature” Set the “Items Per Page” to 0, we want to see all of them.

– toolbar settings section:

keep checked “show toolbar”  and uncheck everything down to list settings

scroll right to the bottom and check “disable notifications”

Form Layout Tab:

We want to have the form work in Ajax when adding features as its faster. So just check the Ajax Form checkbox. no need to build a layout as there is only 1 field.

And that’s it, you can now click save to create the Interface. You should now have 3 interfaces in your application:

Screen-shot-2012-02-14-at-9.50.24-AM[1]

Ok that was the easy part!

Now to great the actual relation part. The first thing we need is an edit interface. So in the “Camera Manager” interface, click the Duplicate link.

this will make a copy of the interface so that we can use its settings for the edit camera form. once duplicated you’ll have a new interface at the bottom of the list called “Camera Manager 4f3a12ecb2abd” the code will be different as its unique to everyone. Click on edit link for this interface.

Set the Interface Title to “Edit Camera” and the Description to “Edit Camera Form interface” remove the Menu Label as we don’t need that.

Go to the field setup tab.

What we are going to do is create a new “Virtual Field” and drag it below the “app cameraID” field. what we are doing is making a copy of the value in app_cameraID so we can give it a new fieldtype without effecting the actual value. In the Virtual Field Set the Master to app_cameraID it should already be set as it’s the first field. Then set the field to be visible by clicking the Eye.

Now set the Virtual Field FieldType to Include Interface. In the FieldType Config you’ll get a dropdown of all your interfaces in your system. This is very messy I know and I’ll be changing it soon to be a better easier to use selector. but for now please bear with me on it icon_smile[1] The interface you want to select is the “Camera Features” interface we created just before this one. We are going to pull in the interface when this interface is loaded. you’ll see.

Lets just name the Field quick. Click on the little gear icon to the top right of virtual field and in the panel that opens, change the title to “Features” click the icon again to hide the panel.

Screen-shot-2012-02-14-at-10.27.07-AM[1]Your settings should look like the image below

Screen-shot-2012-02-14-at-10.01.51-AM[1]Now let’s go to the General Settings Tab.

General Settings Tab

First thing is to put the interface into Form Mode but selecting Form mode radio button.

Now scroll down to Notification and Button. Change the Edit Form Title to “Add Features to Camera

Now let’s go to the form layout tab to set up the form.

Form Layout

Click on the Sync Fields. This fetches out fields and places them in the “Available Fields” box to use.
Now click the “Insert Row” button to add a row to our form layout.

Click on the green + icon to the right of the new row to split it into 2 columns.

Screen-shot-2012-02-14-at-10.05.30-AM[1]

Now that we have a canvas for our form, we can drag our fields onto the form area. Drag all the fields to the left column and them move the virtual field (the one that’s just a code, as the title “Features” you gave it won’t be active until its saved) to the right. You can also adjust the column widths by clicking the black spanner and screwdriver icon to the right. set the widths to 30% and 70% so that the first column is smaller than the second. you can play with this later to make it perfect. Now at the top, set the form width to 650px and check Ajax Form.

Your form layout page should now look like the image below.Screen-shot-2012-02-14-at-10.10.43-AM[1]

Now that our form layout is set, click on the Redirect tab to set redirecting.

Redirects Tab

On the Redirects Tab, select the interface subtab, expand your application entry by clicking the little gear icon to the right and select the interface “Camera Manager” This is so when we click save, it will take us back to the Camera Manager.

Screen-shot-2012-02-14-at-10.14.20-AM[1]I think we’re done. Click Save to finish of the interface.

You should now have 4 Interfaces in your list.

Screen-shot-2012-02-14-at-10.16.05-AM[1]Ok now that we have our edit interface we need to redirect out Camera Manager to it. So click Edit link in the Manage Cameras interface and go to the Redirects Tab.

Select the Interface SubTab, expand your Cameras Application and select the Edit Camera interface as your redirect to and Click Save.

Now the Final Step is to make sure that the Add Features to Camera Interface will accept the correct Passback Value. So click the Edit link on the “Camera features” interface and go to the Field Setup Tab. Click the Setup button on the bottom right corner of the “cameraid” field. and set the GET override to “app_cameraID” as this is the ID field of the camera listing. When you click on a Camera, a value is passed back to the next interface in this case it will be app_cameraID=x this will tell the next interface what you clicked on. The selected item filter looks for a specified value and uses that as a filter id. By default it will look for a value of its own name. so here it will be looking for cameraid= but it will be coming in as app_cameraID= so we set the override to look for the correct value.

Screen-shot-2012-02-14-at-10.21.51-AM[1]Click Save and we’re done!

Now go to the Manage Cameras interface by clicking the View link in its listing. The camera you made earlier will now be a link. Click it to go to the add Features screen. You’ll see the settings for the camera to the left and the add features interface to the right. Simply click “Add feature” button and select a feature to add. the list will then show the features you’ve added.

I know its a little complicated to do but its currently the best way to do a many to one connection. I am working on the checkbox and a multi-select join but until then this is the way to do it.

BUGS
There is a bug with the Delete action, in that when you delete a feature it will refresh and all features will be gone. just reload the page and it will be fine! the refresh doesn’t pass back the passback value so it returns nothing. This is due to be fixed in the next update.

UPDATE: BUG fixed – get update to 0.3.2.009