Symfony 1.4 linkToDeleteMenu Confirm not making JavaScript - php

I had to make an entity called ProfileSchema with some fields, which one is Project_id. I needed to make a List, Edit, New, etc., for which I used the admin generator. The problem was as follows.
I have a list of projects, which they link to the list of the ProfileSchemas that have the same project_id, so I needed a route like: /backend/project/:project_id/ListProfileSchema
I couldn't find a way to do this with the admin generator (getting the project_id into the route), so I coded by hand all the routes (around 12, the new, edit, delete, batch actions, etc.), and change all the code generated by the admin generator to use the project_id passed as a parameter and the code generated by the adming generator as a guide.
==Questions==
Is there a way more simple to do something like this?
Now I need to add the confirmation JavaScript on the delete action on the actions of the list, which is generated by the method linkToDeleteMenu
public function linkToDeleteMenu($object, $params){
$url = url_for('project_delete_profile_schema', array('project_id' => $this->project_id, 'profile_schema_id' => $object->getId() ));
// $url = '/backend/project/1/DeleteProfileSchema/16'
return link_to(__($params['label'], array(), 'sf_admin'), $url, $object, array('confirm' => !empty($params['confirm']) ? __($params['confirm'], array(), 'sf_admin') : $params['confirm'], 'project_id' => $this->project_id, 'profile_schema_id' => $object->getId()));
}
The above code doesn't get the JavaScript. This code below generates the link well and it works, but I can't make the confirmation for the JavaScript appear.
return link_to(__($params['label'], array(), 'sf_admin'), $url, $object);
And the confirmation data is set, as replacing $url with $this->getUrlForAction('delete') does the trick but with the incorrect URL (the one generated by the admin generator).
By the way, I searched a lot trying to find something similar. The only similar question was this:
Routing exception in symfony ("The "/league/:id_league/members/new" route has some missing mandatory parameters (:id_league)")
But the answer didn't help me (as the default value is not dynamic and can't get to override it)

I forgot to close this question. After some time i found that the solution was this:
return link_to('Delete', 'project_delete_profile_schema', array('project_id' => $object->getProjectId(), 'profile_schema_id' => $object->getId()), array( 'confirm' => !empty($params['confirm']) ? __($params['confirm'], array(), 'sf_admin') : $params['confirm']));
You pass the name displayed for the link as the 1st argument , the route name as the 2nd arguments, and the needed variables in the array in the 3rd parameter to generate the route. Finally, you pass the array to generate the confirmation code. My route is
project_delete_profile_schema:
url: /project/:project_id/DeleteProfileSchema/:profile_schema_id

Related

How to make referenced column sortable in drupal 7 view

I have one view in Drupal 7, which displays user information like (Name, address, Status, etc..). I have one column in this (Table)view as "Published event". Basically events are created by users do I want to make this column sortable. I have attached image for more reference.
I tried with applying relationship but no success.
table settings
my handler code is like below :
$handler->display->display_options['sorts']['event_count_published'] ['id'] = 'event_count_published';
$handler->display->display_options['sorts']['event_count_published'] ['table'] = 'search_api_index_user_search_index';
$handler->display->display_options['sorts']['event_count_published'] ['field'] = 'event_count_published';
$handler->display->display_options['sorts']['event_count_published'] ['order'] = 'DESC';
'mail' => array(
'sortable' => 1,
'default_sort_order' => 'asc',
'align' => '',
'separator' => '',
'empty_column' => 0,
),
'event_count_published' => array(
'align' => '',
'separator' => '',
'empty_column' => 0,
'sortable' => 1,
),
above code is in "tcd_reporting.views_default.inc" file, if I put 'sortable => 1', it still does not provide sorting
field is created by below code:
$properties['event_count_published'] = array(
'label' => t('Published Events'),
'description' => t('Number of published events authored by user.'),
'type' => 'integer',
'getter callback' => 'tcd_event_content_type_count_published_get',
'computed' => TRUE,
'entity views field' => TRUE,
);
[Introduction] Which function is responsible for 'click sort' in views?
Click sort -this checkbox from your second screen- in views table settings is function which is enabled only for fields which have properly defined handlers. As you may know each field in views have few handlers (for displaying, filtering, sorting). And for click sort to be possible on specified column its field handler must have two functions defined: click_sortable and click_sort. First one just need to return true, while second need to properly implements sorting on view. For example see handler: [views_module_path]/handlers/views_handler_field.inc.
Your case:
It seems that your column "Published event" have defined handler which does not have click_sortable and click_sort functions (or click_sortable simply returns false).
Possible fix:
Find place where you defined your view source (it depends on how you informed views about it, if I understand its something like "User info" - maybe in hook_entity_info function or hook_views_data function), check what handler is assigned to your "Published event" field and change it.
It's hard to tell where you need to look as it depends on your implementation.
I suggest you to try create hook_views_data_alter function and dpm() it for start. Later you can alter it like that:
mymodule_views_data_alter(&$data) {
$data['some_view_info']['published_event']['field']['handler'] = 'views_handler_field_numeric';
}
Edit 1
First could you tell where this code is? Is it inside handler class, or maybe some views hook? Views gives you a lot of flexibility but this make them hard to understand, and I'm not sure what exactly you achieve and how.
Assuming your field works properly you can try to simply enable click sort.
Example: I created hook_views_data_alter function to see content of views data
function mymodule_views_data_alter(&$data) {
dpm($data,'d');
}
You might need to clear cache to see dpm of *_alter hooks.
Inside dpm'ed array I found "users" for generic example, and its field name looks like this:
I suggest you to try alter your field with click_sortable = TRUE and see what happens. If this wont help please provide more information about your field, how you created it, how it looks in hook_views_data_alter and which handlers it has defined.
Edit 2
Ok, so you have your views exported to code into views_default file. But this only allows you to export view you created from database to code, so it is basically a reflection of what you done in views web editor (eg. page yourwebsite.com/admin/structure/views/view/your_view_name/edit). What you need to do is to change behavior of one of your fields so it became sortable (add click_sortable and click_sort functions in handler class) or change handler of this field to one with sorting option (change field handler to other one like views_handler_field_numeric). If you don't have experience in creating handlers and this is one of generic handlers i suggest you to go back to my Edit 1, examine your dpm, and try to alter $data array to find solution.
Edit 3
Little explanation to prevent confusion. When creating new view you select collection on which this particular view base on (simpliest example - it may be MySQL table, and view will use SQL queries to retrieve data from it). By digging down we have:
Collection - eg. User which is database table user, it is what you select as source when creating new view.
Field - eg. mail which is database column mail, this fields you add to your view.
Field handler - eg. views_handler_field_numeric, this is class name of handler to use by specified field
Now, if you don't created your own handler then your field "Published event" have one of generic views handler. You shouldn't ever change code of contributed modules - especially so widely used as views handlers. That's why my suggestion to add functions click_sortable and click_sort is incorrect. Instead you should change handler responsible for field "Published event".
Best way is to define proper handler in place where you define your field "Published event". If it's somehow impossible the only way I can think of is hook_views_data_alter see docs for more info and examples. I suppose you should try to redefine handler of your field to generic numeric handler views_handler_field_numeric as it should have full sorting functionallity, or try to add click_sortable property to field array as you can see in first image of my post, but I can't provide you fully tested example.

Laravel - First segment to be optional

I'm fairly new to Laravel and I want my website to be multilingual. And I store the chosen language slug in Session. But how can I make my urls display which language was chosen:
Route::get('/{lang?}/signup', array(
'as' => 'signup',
'uses' => 'UsersController#getSignup'
));
http://example.com/en/signup - if I get the first segment with URL::segment(1) and pass it to the Session everything is OK. With the Route I posted above I get 4oh4 if I open the second link.
So basically I can't think of a way that will make both http://example.com/en/signup and http://example.com/singup to work.
The first link should change the user language and the second should use the value stored in Session to fetch the results for this language.
Edit:
I'm trying to make something like a CMS, where I have set some keys in a table in the database - languages:
id - (int) AI, name - (varchar) - Language's name, prefix - (varchar) - Language's prefix
And another table that stores the translations of certain parts in the website:
id - (int) AI, key (eg. signup_form_name), value (eg. My sign up form)
And I have this function in my helpers.php:
function _t($key, $language = NULL)
{
if(is_null($language))
$language = Session::get('currLang');
$string = DB::table('translations')
->where('key', $key)
->pluck('value');
return (!is_null($string)) ? $string : $key;
}
How could it differentiate
en
from
signup
?
Those are just two strings. The only way could be to hit
http://example.com//signup
Instead of
http://example.com/signup
Because this way you are explicitly telling Laravel that you are not passing the first route parameter. But that will not work, because Laravel will not understand it too, there's an issue about this in Github.
So this is one of your options:
Route::get('/signup/{lang?}' ...);
So you can hit it as
http://example.com/signup
or
http://example.com/signup/en
Route::get('/{lang?}/signup', array(
'as' => 'signup',
'uses' => 'UsersController#getSignup'
))->where('lang', '(en|br)');
The key problem here is that you need to hard code the langs into your controller, but it should work...

How do I pass a parameter from URL to an admin generated form?

I have two models namely 'Client' and 'Pet' which have admin generated modules for each. There is a one-to-many relationship involved so I want to be able to create a new Pet using a cid (client id) specified in the URL (i.e. /pet/new/cid/6)
I already disabled the cid field in the Client form so that the parameter from the URL will be passed as the default value.
I tried overriding the actions.class.php of the admin generated module with this code:
public function executeCreate(sfWebRequest $request)
{
$this->form = $this->configuration->getForm();
$params = $request->getParameter($this->form->getName());
$params["cid"] = $request->getParameter('cid');
$request->setParameter($this->form->getName(), $params);
parent::executeCreate($request);
}
It works if i replaced
$request->getParameter('cid');
with a hard-coded int value so I think the problem is that I'm not able to get the parameter from the URL.
I think I might be missing on some routing configurations but I'm fairly new to this framework so I'm not really sure what to do.
You can simply pass the parameter cid in the url like this :
/pet/new?cid=6
and get the result in your action with :
$request->getParameter('cid');
The first part of the routing (/pet/new) is a specific doctrineRouteCollection

Passing parameters through form to Controller (CakePHP)

Okay, so I'm fairly new to CakePHP. This is the setup:
I've got a Model (Reservation), and a controller, (ReservationController).
I'm trying to provide a simple add() functionality.
The request url is: www.example.com/reservations/add/3
Where 3 is the ID of the event this reservation is for.
So far, so good. The problem is, the form is constructed like this:
<h2>Add reservation</h2>
<?php echo $form->create('Reservation');
echo $form->input('first_name');
echo $form->input('last_name');
echo $form->input('email');
echo $form->input('tickets_student'); echo $form->input('tickets_nstudent');
echo $form->end('Add');?>
When I hit the send button, the request URL becomes simply www.example.com/reservations/add/, and the event id is lost.
I've solved it now by grabbing the id in the controller, and make it available to the form:
// make it available for the form
$this->set('event_id', $eventid);
And then, in the form:
$form->create('Reservation',array('url' => array($event_id)));
It works, but it strikes me as a bit ugly. Isn't there an easier way to make sure the form POST action gets made to the current url, instead of the url without the id?
Nik's answer will fail if the website isn't in the server public_html root.
This answer is more solid:
<?php echo $form->create('Reservation',array('url'=>'/reservation/add/'.$data['id']));?>
Following the strictest convention for just a moment, reading a URL like /reservations/add/3 would be, well, confusing. You're calling on the ReservationsController to act on the Reservation model, but passing it an event ID. Calling /reservations/edit/3 is far less confusing, but just as wrong for your situation since the id value, "3", would be assumed to be a reservation identifier.
Essentially, you're making an ambiguous request at best. This is a simple form to create a reservation and that reservation has to be associated with an event. In a "traditional" scenario, the form would allow the user to select an event from some kind of list. After all, the foreign key, probably event_id in this case, is really just another property of a reservation. The list would have to be populated in the controller; this is usually done via a find( 'list' ) call.
If you already know the event that you want to create the reservation against (which you apparently do), then I'd probably select the analogous method of using a hidden field in the form. You still have to set the value in the controller just as you're doing, but the end result is a bit more Cake-y. The hidden field would be named data[Reservation][event_id] (again, I'm assuming the name of your foreign key field).
$form->create('Reservation',array('action' => 'add',$eventId);
and in the controller:
function add($eventId = null)
{
if($eventId == null)
{
// error state
throw new NotFoundException('Invalid id');
}
...
}
I do it all the time.
You can do following:
$form->create('Reservation',array('url' => $this->Html->url()));
this way all your variables from the url will be added in the form action :)
As Rob Wilkerson suggests, the issue is your URL route doesn't accurately describe the operation being performed. It becomes further confusing when you want to edit the reservation: /reservations/edit/6. Now the number in the URL means something different.
The URL convention I use for situations like these (adapted to your particular case) is /events/3/reservations/add. It takes a bit more up-front to configure your routes, but I've found it's superior for clarity down the road.
Sample routes.php:
Router::connect(
'/events/:event_id/reservations/:action',
array('controller'=>'reservations','action'=>'index'),
array(
'pass' => array('event_id'), // pass the event_id as a param to the action
'event_id' => '[0-9]+',
'actions'=>'add|index' // only reverse-routes with 'action'=>'add' or
// 'action'=>'index' will match this route
)
)
// Usage: array('controller'=>'reservations','action'=>'add','event_id'=>3)
Router::connect(
'/events/:event_id/reservations/:id/:action',
array('controller'=>'reservations'),
array(
'pass' => array('id'), // pass the reservation id as a param to the action
'id' => '[0-9]+',
'event_id' => '[0-9]+',
'actions'=>'edit|delete' // only reverse-routes with 'action'=>'edit' or
// 'action'=>'delete' will match this route
)
)
// Usage: array('controller'=>'reservations','action'=>'edit','event_id'=>3,'id'=>6)
In your FormHelper::create call, you'd have to specify most of the reverse-route you want to follow, but again, the up-front cost will pay dividends in the future. It's usually better to be explicit with Cake than to hope its automagic always works correctly, especially as the complexity of your application increases.

How does the backend look for AjaxLink() call in Zend Framework

I'm trying to make some ajax-functionality in my web application, but I cannot get all puzzle pieces to fit:
I want to add a link that, when clicked upon, will open a new input (text) field that can be filled by the user. In the back-end, I want to do some administration that the link is clicked.
I want to do according to the Zend Framework principles, with using the ajaxLink() method. Can anyone have an example for me? I've read the official documentation (ZendX_JQuery) but it doesn't fully help me.
My front-end (view) code looks like this;
<?= $this->ajaxLink("Subscribe", $this->url(array('controller' => 'mycontroller', 'action' => 'action1', 'id' => $event['id'])),
array("beforeSend" => "hide",
"update" => "#pb_" . $event['id'],
'noscript' => false,
'method' => 'POST')); ?>
My back-end code looks like this.
public function action1Action()
{
if( !$this->loggedIn || ! $this->athlete) {
$this->_redirect('index');
}
if(! $this->_request->isXmlHttpRequest())
{
//The request was NOT made with JS XmlHttpRequest
die;
}
// Do some administration
// (removed to make this easier in this example)
$pb = new Zend_Form_Element_Text('PB');
$pb->setLabel('PB:')
->addValidator('StringLength', false, array(0,20))
->setRequired(false);
$renderText = $pb->render();
return $renderText;
}
I keep getting errors back that the given method wants to look-up a action1.phtml view script. I'm also not sure if what I try to do with the generation of the form input element works in this way.
I found some of the answer in this question, but it's not that elegant (requires an extra parameter in the link and you need another controller) which I don't like.
You need to turn off the ViewRenderer for this particular action. ZF by default enables an Action Helper called ViewRenderer which assigns a conventionally named view script (in your case, action1.phtml) to a particular action method. Since you're only trying to return a small snippet of text, rather than a full site view, full view rendering isn't necessary. Fortunately, this is easy.
public function action1Action(){
$this->_helper->viewRenderer->setNoRender();
// the rest of your code
}
The full docs are here:
http://framework.zend.com/manual/en/zend.controller.actionhelpers.html#zend.controller.actionhelpers.viewrenderer
There's nothing to it. All you have to do is point it to where your content is coming from. In your view:
<?= $this->ajaxLink("Example 1","/controller/action1",
array('update' => '#content',
'noscript' => false,
'method' => 'POST')); ?>
In your controller:
echo 'Some Content';
Read this:
http://www.mikaelkael.fr/IMG/pdf/ZendX_Framework_1.7.x_EN.pdf

Categories