Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP/MySQL - Allowing users to create custom form fields for CMS

I'm making a CMS with PHP just as practice and want to give a user the ability to create custom form fields that can be used on their website's signup/registration (similar to SocialEngine or PHPFox). However I really don't have a good idea as to how I am going to accomplish this. Would I have to make some sort of plugin ("hook") system? I originally had this in mind but I don't know if this is really a good way to go:

The admin of the CMS would be given a form with the options of the form field's name that they want to create, the type of field(string, boolean, date etc.), the category of the field and whether or not it is a required field.

The values would then be passed into a database table called "custom_form_fields" with the columns: id, field_name, field_type, category, required. Also to store the values of these fields; a column would be added to the 'users' table where all the user's data is stored.

ALTER TABLE users ADD the_custom_form_field_name datatype

But now I'm confused about how I would display the different form fields in PHP and how I would insert that data from the form fields into the user's table.

If you don't understand my question please look at: http://www.phpfox.com/features/custom-fields/ It shows what I am trying to achieve.

Can anyone help me out with this?

Thanks

like image 309
user1302430 Avatar asked Oct 07 '22 04:10

user1302430


1 Answers

You need to slow down a little; at first it seemed like your question was about the js/php code for generating HTML forms dynamically, but then you started talking about the database fields.

Your question is still not 100% clear to me, but what I'd do in your situation is start off with the database design and focus on nothing else.

I can see that you're thinking about altering the users table. I don't think that would be necessary, nor good design.

So long as the each user has a primary key i.e. users.id field, you could create a new table just to store form field data like this:

CREATE TABLE users_custom_form_data(
  id INT NOT NULL,
  FOREIGN KEY(id) REFERENCES (users.id) ON DELETE CASCADE,
  /* then store any data you need to store about a form field */
  field_type ENUM("text","number","phone","email","submit","etc"),
  field_default_value VARCHAR(100) /* or what you think is necessary */,
  field_name VARCHAR(100),
  field_id VARCHAR(50),
  field_class VARCHAR(50)
  /* and any others you need */
) ENGINE = InnoDB;

Since we're assigning each record to a USER ID, we can later track which users own what custom form fields.

I hope this is enough to get you started.


Since you clarified your question, I think what we are looking at now is a table with a field for the user ID that owns the record, a field for the custom attributes table id to reference their custom fields. It would look like this:

CREATE TABLE tbl_user_attributes(
    user_id INT NOT NULL,
    FOREIGN KEY(user_id) REFERENCES(tbl_users.id) ON DELETE CASCADE,
    attribute_id INT NOT NULL,
    FOREIGN KEY(attribute_id) REFERENCES(tbl_attributes.id) ON DELETE CASCADE
) ENGINE=InnoDB;

Then the attributes ID will tell you what kind of field the user added to their CMS. The attributes table will be pre-populated by you and defined by you (with your default fields). It could look something like this:

CREATE TABLE tbl_attributes(
    id INT NOT NULL AUTO_INCREMENT,
    text_label VARCHAR(100),
    type ENUM("TextArea","CheckGroup","DropDown"),
    data BLOB
) ENGINE=InnoDB;

So once you have populated that table with options, the user should be able to add any of those types of fields (on top of the default fields) and give it a name i.e. "Movies" would be stored in the field named 'text_label' and it would be of the type "TextArea". That tbl_attributes.id would then be inserted into the tbl_user_attributes with the user ID of the user that created it. Then your program would do the rest, by reading the database. If you want to store user data in that, store it in the 'data' field (if its ALWAYS text, you could use a TEXT type instead of a BLOB but using a BLOB isn't a problem as you can work out what you need to do with the data judging by the 'type' field i.e. a TextArea means text was stored in the BLOB.

Hope this helps.

like image 77
Ozzy Avatar answered Oct 13 '22 10:10

Ozzy