Build and edit table

jmahun 18 Jul, 2015
I thought this would be easy, but it's got me scratching my head.
I've worked with Chronoforms enough to feel comfortable designing a form to capture data from a user and/or write it to a table. Now what I want to do is pull records out of a table for viewing, editing, or deleting.

Basically, I want a table that is built and maintained by users:
Part (1) A registered user completes a form whose data is saved to the table; a registered user submit more than one form having multiple records in the table.
Part (2) Any user can retrieve a specific record from the table for viewing
Part (3) A registered user can retrieve one of his/her records and edit or delete it.

A very simple example would be a database of users and their cars. The input form would contain these fields:
name; text
year, numeric
model; text

One user may have multiple cars so there would be multiple records.

I created a table, cars_db, which includes the user_id field, unique for each registered user. I renamed the id field to cf_id.

Part (1) is pretty straightforward: I created a form, cfInputCar, which saves the data to the cars_db table using a DB Save action. The form is accessible from a menu that is only visible for a logged in registered user.
This form works fine and the records are saved correctly

Part (2) I created a ChronoConnectivity connection, ccListCars. On the Settings tab in Front List, I used cf_id in "View linkable". In Front List | Actions | View, I set the Form event to a new form, cfShowCar.
I assigned the connection to a menu item that any user can access. When they check that menu item, a list of all car models in the table appear. The cf_id field is a link which clicked opens the cfShowCar form.

I created the cfShowCar form by starting with a copy of cfInputCar. I removed the Submit button and DB Save action, and added the DB Read action to load the record for cf_id selected by user in in the ccListCars connection. The form loads with the table record selected.

Up to here, everything works, but this is where I get stuck: I'm not sure how to get Part (3) to work. I suspect I have to create a new ChronoConnectivity connection (ccUserCars) and assign it as a menu item which which only appears for a logged in registered user. When selected it has to list only that user's records, if any, in the table - not sure how to do that. The connection would be linked to another Form event so when the user clicks a record it opens another form, cfEditCar. I've not yet built a form for editing/deleting a table record, and am a php neophyte, so am not sure how to do it.


With my approach, I need three forms (cfInputCar, cfShowCar, cfEditCar) and two connections (ccListCars, ccUserCars). Am I on the right track or making this more complicated than it needs to be?
GreyHead 18 Jul, 2015
Hi jmahun,

This sounds more complicated than it needs to be. You should be able to do this from one CC listing using permissions to select records and show Edit links. You can get the current users ID (provided that they are logged in) and use that to filter the list shown.

You can use the same for for all three uses with a bit of coding to hide/show different parts but you are probably better off with separate forms - though Input and Edit could be merged as long as there aren't too many 'non-editable' entries.

ChronoConnectivity lets you add both Edit and Delete links to your listing that will work in a very straightforward way.

There is more information in the FAQs here.

Bob
jmahun 19 Jul, 2015
Thanks. I'll check into the links and FAQs and see if I can work my way thru.
jmahun 21 Jul, 2015
Bob:
I'm trying to filter the list in the connection by user_id so that only the logged in user's table records are shown. In Front List on the Settings tab I have user_id entered in the Filters field. I'm not sure, however, how or where to determine what the user's user_id is.

The "How can I get information about the user" FAQ shows php code to get the user_id (am not a php programmer). The code is:
<?php
$user=JFactory::getUser();
?>
I'm using a Table display so tried entering the code in the Header code box but that didn't work.

I'm also using a Model Title (jobs). How do I incorporate that into the php code?
GreyHead 22 Jul, 2015
Hi jmahun,

If you are trying to filter the listing then the code needs to be in the Connections box, something like this
<?php
$user = \JFactory::getUser();
if ( !$user->id > 0  ) {
  return array('user_id' => $user->id);
} else {
  return array('1' => '2');
}
?>
The second return should make sure that no records are shown if the user id isn't found.

Bob
jmahun 23 Jul, 2015
Actually, I sort of stumbled onto this which seems to work.
In the Conditions box for my model I entered
<?php
$user = JFactory::getUser();
return array("user_id" => $user->get("id"));
?>

The connection is paced in a menu and only appears when a registered user logs in. That way I don't have to worry about checking for a non-existent user_id.
When registered user clicks on the menu item, it shows a list of records associated with their user_id. If they have no records ion the table, they just get an empty list.

I've tried it with a number of different users and it seems to work pretty consistently.

Jerry
jmahun 25 Jul, 2015
I'm pretty confused on checkbox groups Handle arrays, explode, etc.

I've a form to save data to a table. The form uses a few radio boxes and checkbox groups. Before the DBSave action, I have a Handle Arrays action in which I left Fields List blank so all array fields are processed. Array fields have [] at the end of their field name (ex: pos_type[]). When the form is submitted, the array field values are saved as comma delimited strings.

So far, so good.

I'm using a ChronoConnectivity connection to list records in the table for editing. I've included _EDIT_ in the Front List Columns list so each record is shown with an Edit link. For Form event in Action/Edit I have the name of a form for editing. To edit a record, the user clicks on the record's ID in the list. The edit form loads, but the checkboxes are blank. I know the checkbox values were correctly saved because I can see them with a database manager extension I have.

In the edit form I have a Connection Action in On submit so when the user completes editing and clicks Submit, control is returned to the connection where the save takes place (as I understand it).

But when the edit form is submitted, only the last item in checkbox groups are saved to the table. There is no DB Read action or write action in the edit form.

I've gone thru the forum and FAQS and the one recurring comment is to include custom code with an explode command in it. Most of those, however, are described in conjunction with a DB Read action, which I don't have on my edit form.

Where do I put the custom code with the explode command? I tried putting it in a Custom Code action in the edit form's On load, but that didn't work. Neither did adding it in the Code window of Front List | Actions | edit in the connection.
GreyHead 25 Jul, 2015
Hi jmahun,

I suspect that this is a bug in the linkage between CC and CF - somewhere there needs to be a way to identify and handle array fields. I don't know CC well enough to be sure that this is the problem though.

Please contact Max, the developer, using the Contact Us menu above and linking to this thread. Hopefully he will have a more useful answer.

Bob
jmahun 25 Jul, 2015
Will do.
Thanks.
Jerry
This topic is locked and no more replies can be posted.