How do I avoid code dupilcation in my controllers using Codeigniter? - rest

this is my first post and my English isn’t that good. So I hope you will understand my question/problem.
I'm using the Codeigniter framework for most of my projects and I always have the same problem. Let's have an example:
1.) A logged in user can change his profile with some basic information’s (e.g. displayname, firstname, lastname). The user submits the form and the controller will handle everything and load another view.
2.) In the administration an administrator wants to change the profile of a user. No problem, he clicks "edit" in his userlist and change the last name and submits the form. I have a second controller for that which does almost the same as the profileController but loads a different view.
That's a lot of code duplication and if I want to add a new attribute (e.g. birthday) I have to update many files.
I think a RESTful approach could be a solution for that problem but I am not very familiar with that. Is there another solution?
Do you get the problem?
Thank you for your patience and help!

Related

How to show confirm box before submit form in moodle on course edit page?

I am using moodle 2.8
I wants to confirm user before edit course.
Basically I have a category name ex. 'Live' category
So when user move course into 'Live' category then want to show confirm box and if he click on yes then course will be update otherwise redirect to course edit page.
This will require changes to the Moodle core code (not usually a great idea, for ongoing maintenance) and would probably be a bit fiddly to implement - you would need to store all the submitted details somewhere in the confirmation form, then re-send them along with the confirmation.
It might be easier to make a small core code change to prevent the user from ever moving the course directly into the 'live' category (adjust the 'validation' function in the form submission), then have a separate admin page (within a local plugin, or maybe a block), that listed all the non-live courses and gave the option of moving them into the 'live' category (with the appropriate warnings / confirm action).

Using ninja forms, how can I persist field values across pages?

I have a form on the home page of quotedjobs.com that I would like to persist the field values of across pages.
For example: A user enters some values in fields, such as job title (textbox), job type (list) and job description (text field). Underneath that I ask users to register on the site, but they have the option to click a link to allow them to login if they are already members.
What I would like to be able to do, is to redirect users to another form that is a copy, but allows them to login instead of register, but keep hold of the values that they entered in the title, type and description form of the previous page so they aren't losing their work.
I have seen the ninja_forms_processing variable in the docs, but I'm not clear on how to use that.
TL;DR - You can't.
As it was slim pickings here, I sent an email over to support. Got this response:
Hello,
At this time this use case is not possible in Ninja Forms. The plugin is currently unable to transfer data from field to field or between forms.
I’m sorry that we do not have a better solution for you at this time.
Thank you,

Rails 3 break users edit into categories

I have a Rails3 app (I'm new to rails) that allows users to sign up by filling name, email, password, password_conf.
Once signedup, I d like users to be able to edit more attributes through different forms, (Attributes such as an avatar image, address, etc..), in order to have an edit page which would not include too much information.
What is the best way to create different forms for the user to edit attributes separately, for example:
Edit General Info (name, email), Edit Password, Edit address, Edit picture
Was thinking of adding new routes
resources :users do
member do
get :general_info, :password, :address, :picture
end
end
Should I use nested attributes in order to keep a unique edit action in users controller, or add new actions?
Any advice for best practices would be extremely appreciated!
Thx
In my opinion, it should be a step by step process, where first when the user signs up, it is shown the first from (eg. General Info), when he clicks on skip or submit, he is shown the next form and so on.
In this case, the best way to do this is, to have a separate action for each form. There will be one view file for each form. This will make your code more readable and easy to understand. This implies, that you need separate routes for each file, as you suggested.
But if you are using some JS Framwork(eg. AngularJS) to manage your interactions, you can also put all the forms in the single file with a single edit option.

Does anybody know if there's a way to modify the action attribute of the wp-login.php?action=register form?

I'm trying to create a reference registration system in Wordpress. The idea is that the user will click a link which will contain wp-login.php?action=register&ref=12345
I'm hooking into the 'register_form' action hook and adding a reference number hidden input and on registration use that reference number to do whatever i need to do with it.
The issue appears when the user gets something wrong in the registration form. The problem is that he will be taken to wherever the action attribute of the register form states which is "wp-login.php?action=register" without the ref parameter. I'm looking for a way to modify that action attribute. I've looked in the wp-login.php file and it seems that there's no filter there.
I know there's always javascript, but is there another wordpressy way to do this?
I think you'll get tons of clues from this affiliates plugin, you can download it and checkout the code from http://wordpress.org/extend/plugins/affiliates/
Good luck,
Virgil!

MVC2 Routing and Security: How to securely pass parameters?

I'm a relative MVC noob coming from WebForms. I think I have a pretty good grasp of MVC with a couple exceptions, and I think I may have broken the pattern. I'm gonna try to keep this short, so I'm assuming that most of what I am asking is relatively obvious.
Let's say I have a news site with articles. In that case, a URL in the form of mynewssite.com/Articles/123 works just great because I don't care who views which article. The user can change the ArticleID in the URL to whatever they want and pull up that article. In my case, however, I only want the user to be able to view/edit data entities (articles, or whatever) that belong to them. To achieve this, I am using their UserID (GUID) as a foreign key in the database, and displaying a list of their data for them to choose from. Here comes the problem... when they click on the link that is created by Url.Action("Edit", New With {.id = item.id}) (I'm not using ActionLink because I need to add HTML content inside the link), the id shows up as a querystring parameter. I could add a route for it, but the id would still show up in the URL for them to tamper with. The obvious implication is that by tampering with the URL, they could view/edit any entity that they want.
What am I missing?
Is there a good way to pass the parameters without adding them on the URL? I know that I could put them in a form on the page and submit the form, but that seems cumbersome for my example, and I'm using jQuery.ajax in places that seems to conflict with this idea.
I could also check their UserID against the data in the Edit method, but that also seems cumbersome, too.
Is this question too broad? Please let me know what specifics you need. Thanks.
Even in Winforms, you would have to add special logic on each request to filter only the articles that the user owns. I don't see why MVC should be any different. Sure, you can use web.config to deny access to given url's, but not when you use a single page that takes a parameter of what data to show.
Your best bet is probably to filter this at the database level. By adding a where clause that includes the user id, then the app will return a "no records found" sort of error, and you can do whatever you want with it.
You could use forms authentication. This way when the user authenticates an encrypted cookie will be emitted which will contain his username which cannot be tampered with. Then you could verify whether the currently connected user has authorizations to edit this article.