Render a different view from the same action in Symfony2 - symfony

I am developing a small system using Symfony2, and I have reached a situation that I need to duplicate the same editAction, but only change the view it renders.
I use this action to edit basic information of all the registered budgets that is listed in a page. I have a special page listing all the inactive budgets, and I want a different editing page to change some statuses and add dates.
How can I use the same editAction to render different views depending on the URLs? The page that lists all the budgets is /budgets and the inactive ones is /budgets/inactive.

Use something like:
/**
* #Route("/budgets/{active}", defaults={"active" = "active"})
*/
public function editAction($active)
{
// ...
}
Then, when you go to /budgets $active will be "active" and when you go to /budgets/inactive $active will be "inactive". You can then use this variable to decide which template to show
More info here:
http://symfony.com/doc/current/book/routing.html#required-and-optional-placeholders

Nice answer by Carlos, thanks, but an edit action requires also validation, and after that the same page with errors or some kind of action on a valid form. So i would say that even a simple form is a bit complex. When you also make a "two views" action it gets 2 * 2 = 4 times more complex. besides of that the $active variable could have any value and not only "active" or as you want "inactive". It could be "joke", "stupid" and a lot more possibilities. My suggestion would be to add a second action and to use private functions for functionality that fits for both forms OR to use a custum class (or service) that handles the forms.

Related

In ASP MVC is there a way access a session state from a shared view such as _Layout.cshml

I'm trying to make it so the links that display on the nav bar from the _Layout view change depedning on whether you are logged in. This is using a SessionStateRepository which I use by initializing it in my controllers.
On the layout page I want to be able to check if
state.GetLoginState().email = "anon"
This is so I can change if the login button is there or not. I just can't find a way to use it on a shared view which goes on all pages.
Well, using session state with MVC is a topic of religious debate (for number of good reasons ) and you should definitely read up on that.
But to answer your question, with traditional session class, you can do this
#if (HttpContext.Current.Session["email"] == "anon")
{
<span>Value1</span>
}
else
{
<span>Value2</span>
}
you will have to devise specific implementation on the similar lines based on your repository

Show different menu links to user depending upon their Drupal 6 role

I want to display different navigation links and data to a user depending upon their role.
What module(s) could I use to achieve this?
Use hook_menu_alter()
/*
* Implementation of hook_menu_alter()
*/
function MYMODULE_menu_alter(&$items)
{
$items['your/path']['access callback'] = _custom_access_callback_for_this_page();
}
And inside the access callback function (here: _custom_access_callback_for_this_page), write whatever the validation you want.
Don't forget to clear the cache after any changes inside your `hook_menu_alter() implementation.
Hope this helps.
I really like this module for Drupal 6, you should check it out:
Menu per Role

How navigate to next page using AJAX in MVC4?

I don't have so much experience using AJAX in a MVC application, in fact is my first facing. Please check the below image and note the rectangles.
The image is just an example that I took from internet.
The biggest rectangle is a partial view in my application and I have to render it when the user press Continue or Continuar button. The application should replace the current view for another without refresh the page.
This is the code which I'm testing, note first that I'm passing the first element of a list, but when the user press the button, render the view with the next element index = 2.
public ActionResult DoTest()
{
if (!Request.IsAjaxRequest())
{ }
List<Worksheet> worksheets = new List<Worksheet>()
{
new Worksheet("Hoja 1", ...),
new Worksheet("Hoja 2", ...)
};
return View(worksheets[0]);
}
Can orient me a little bit to know how to implement this feature? I just know that I need to use Ajax.
Have a look through the tutorials and examples here. There's plenty of other material around on the web with information on this subject.
There are many different ways you can achieve this. One way would be to write a custom paging Helper (HtmlHelper) that accepts new content upon the post event. You can view all about Helpers here : Custom HTML Helpers
Another way could be to use partial page rendering to achieve the partial page update upon post event.
If I was you I would combine a partial view with a jquery function to update the content. You can view some help on that here: Change dive content with Jquery

Drupal 6 Views: Toggling filters on/off via checkbox?

I have a view I'm using as an "Advanced Search" tool - it simply provides a list of relevant nodes which are limited by a number of exposed filters I have in place. In particular, I have a "keyword" exposed filter that's acting as the main search box, which filters on the node title. Now, I've had a request for the ability to toggle (I assume with a checkbox or similar) this filter to search on both the node title and body at the same time, as an option to provide users with more search results.
I currently have a view set up with two displays: one that filters on just the node title, and one that uses Views Or to limit the results to nodes that have the keywords in either the node title OR the node body. By themselves, both of these displays are working the way I'd like.
My question is how I'd go about toggling between these two displays using a checkbox or something similar. I don't want to use Views Display Tabs because both displays look visually identical (the only difference is how the filters are configured to include more/less results) which would be confusing to the user - plus it uses AJAX which breaks some (mostly small) modifications I've made via jQuery to the behavior of the view.
What I'd like to accomplish, in essence, is a conditional check when the view is submitted - depending on whether a checkbox is selected, the arguments are sent to one display or the other. Any suggestions on how to accomplish this?
One way you might be able to go about this could involve adding a custom validate handler on the exposed filter form that would in theory allow you to check the value and forward the request to a different page display appropriately. Exposed filters are exposed in the URL as $_GET variables, so, forwarding a user to a specific page with filters predefined should be easy enough.
<?php
function mymodule_form_alter(&$form, $form_state, $form_id) {
switch ($form_id) {
case 'views_exposed_form':
// Deterimine the ID so you only do this
// to a specific exposed filter form
// drupal_set_message($form['#id']);
if ($form['#id'] == 'views-exposed-form-api-search-page-1') {
// You might also want to add the checkbox FAPI item in this area
$form['#validate'][] = 'mymodule_api_search_validate'; // custom validate handler function name
}
break;
}
}
function mymodule_api_search_validate($form, &$form_state) {
// Check if the FAPI item has the specified checkbox value
if ($form_state['values']['options'] == 'title') {
// The get variables to pass to the views exposed filters
// You can configure what this $_GET variable should be while editing the filter
$query = array(
'query' => 'the search query',
);
drupal_goto('api/search', $query);
}
}
?>

ASP.NET MVC cancel, not delete

I have two questions, both related to the same view: so there is view called ProductDetails which shows the details of a product.
Each product can have the status:
Available - in this case, two button are available "edit" and "remove"(which will change the status of the product to "Not available" but will not remove it from DB)
Not available - in this case, the page displays the product but no options to edit or remove are
visible.
The controller ProductsController has an action Details that shows that view.
The problem is that I don't know how to implement the two buttons (Edit and Remove) because:
Edit sends to another action method (Edit which display another view) <- this works
Remove should do (IMO) a post on the current page. In the post action, the status of the product is changed and the view is shown again.
I want both button to look like links. If I put a form for remove, then it will be displayed as a button. I would like to avoid making the button look like a link with css. Or... at least I want to use the same HTML element for both 'buttons'.
This is more an issue of displaying the elements so I have added the CSS tag to your question as some alternative answers may rely on this.
Personally I think trying to make a button look like a text link would be quite awkward, even once you turn off the border and background you have issues with lining up the text etc.
I'd say you have 2 "simple" options.
Firstly you could make the delete not post a delete request but link to a delete confirmation page (or bring up a JS modal window with your delete form and button).
Secondly you could make them both look like buttons, while you requested that it looks like a link I figured that the main point was consistency in UI than the link look specifically. You could use JQueryUI and invoke .button() on both elements, invoking JQueryUI for 1 feature is a bit overkill but it's a quick change, of course you could replicate the same idea of styling the link like the buttons but would have to spend time dealing with browser CSS issues.
the Remove link should post to the Remove action, which should in turn (after validation and DB update) redirect to the details action.
public ActionResult Details(int productId)
{
// Your current action method
return View(model);
}
public ActionResult Remove(int productId)
{
// Validate productId
// Update DB
return RedirectToAction("Details", new { productId = productId } );
}
You easily can solve your link vs button problem by using a GET instead of a POST. Don't be blinded by best practices.
Or you can use a Remove link that executes a one-liner Javascript function that posts the form:
Remove

Resources