How to include CJuiAutoComplete in a CGridView filter? - jquery-ui

I have a grid view that lists contents of a table, table has column author_id.
Now I'm displaying usernames using relation name column syntax author.username.
Is it possible to allow user to type in a username in column filter, with support of CJuiAutoComplete, some examples hints?
My code sample:
<?php $this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$model->with('author')->search(),
'filter'=>$model,
'columns'=>array(
// ...
array(
'name'=>'author.username',
'filter'=> // ?
),
// ...
),
));

The widget has a 3rd parameter that can be set to true which means that will return a string and will not render the widget CJuiAutoComplete.
widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$model->with('author')->search(),
'filter'=>$model,
'columns'=>array(
// ...
array(
'name'=>'author.username',
'filter'=> $this->widget('zii.widgets.jui.CJuiAutoComplete', $array_params, true),
),
// ...
),
));
and $array_params can be replaced with similar as following ex :
array(
'name'=>'author_username',
//'model'=>$model,
'attribute'=>'city_eve',
'sourceUrl'=>"/controller/action/",
'options'=>array(
'minLength'=>'2',
),
'htmlOptions'=>array(
'size'=>'36'
),
)
and also you have to put in your model search method some checks :
if($request->getQuery("author_username")){
$criteria->addCondition(author.username=:author_username");
$criteria->params[':author_username'] = $request->getQuery("author_username");
}

Related

How to retrieve value from array and present in the field in admin orded data?

I have a question. I have plugin Furgonetka installed on woocommerce. Package numbers are collected from Furgonetka service in meta_key tracking_info like:
Array
(
[520000016902796077744528] => Array
(
[courierService] => inpostkurier
)
)
In data base it is presented like: a:1:{s:24:"520000016902796077744528";a:1:{s:14:"courierService";s:12:"inpostkurier";}}
I have added the function in functions.php to show field tracking_info in admin order data:
add_action( 'woocommerce_admin_order_data_after_order_details', 'add_admin_order_field' );
function add_admin_order_field( $order ){
woocommerce_wp_text_input(
array(
'id' => 'tracking_info',
'label' => __( 'Tracking number', 'your-text-domain' ),
'value' => get_post_meta( $order->get_id() , 'tracking_info', true ),
'wrapper_class' => 'form-field-wide',
)
);
}
But field presents only value "Array".
Do you know how to retrieve package number (which is for example 520000016902796077744528) and present in the field tracking_info?
I want to show package number in the field tracking_info.

ZF2 translator is not working in controller?

I am trying to translate in the controller by ServiceLocator, but this is not translating and I have tried many sulotions in stackoverflow but with out success. My system uses multiple languages and my goal is to use transtor in view, controller, form and filter. Tranlator in my view is working. Any sugestion and help will be appreciated.
Not working in controller:
$this->getServiceLocator()->get('translator')->translate('my text',$myLocale);
My Application mudole.config.php:
'service_manager' => array(
'abstract_factories' => array(
'Zend\Cache\Service\StorageCacheAbstractServiceFactory',
'Zend\Log\LoggerAbstractServiceFactory',
),
'factories' => array(
'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',
),
),
'translator' => array(
'locale' => 'en_US',// 'locale' => 'dk_DK',
'translation_file_patterns' => array(
array(
'type' => 'gettext',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.mo',
),
),
),
I changed the local in mudole.config.php to another language but still not translating.
View Helper/Forms
ZF2 ships with the view helper Zend\I18n\View\Helper\Translate; this is why you can already use the method $this->translate($text) in the view.
However all view helper classes that extend from Zend\I18n\View\Helper\AbstractTranslatorHelper (which includes all form view helpers) are also 'translation capable'.
You would need to pass in the translator using $viewHelper->setTranslator($translator) and enabling translation via $viewHelper->setTranslatorEnabled(true).
Controller Plugin
Unfortunately there is no default plugin (that I could find) to handle translators in controllers; I guess you could argue that text content shouldn't be in the controller anyway.
You could easily create one such as the example below. The key is to pass your new translator service as a dependency via a factory.
namespace MyModule\Controller\Plugin;
use Zend\Mvc\Controller\AbstractPlugin;
use Zend\I18n\Translator\Translator as TranslatorService;
class Translator extends AbstractPlugin
{
protected $translatorService;
public function __construct(TranslatorService $translatorService)
{
$this->translatorService = $translatorService;
}
public function invoke($text = null, array $options = [])
{
if (null == $text) {
return $this;
}
return $this->translate($text, $options);
}
public function translate($text, array $options = [])
{
return $this->translatorService->translate($text);
}
}
And create the factory class.
namespace MyModule\Controller\Plugin;
use MyModule\Controller\Plugin\Translator;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\ServiceManager\FactoryInterface;
class TranslatorFactory implements FactoryInterface
{
public function createService(ServiceLocatorInterface $controllerPluginManager)
{
$serviceManager = $controllerPluginManager->getServiceLocator();
return new Translator($serviceManager->get('translator'));
}
}
Register the service in module.config.php.
return [
'controller_plugins' => [
'factories' => [
'translate' => 'MyModule\\Controller\\Plugin\\TranslateFactory',
]
],
];
Then you can just call it within a controller class.
// Directly
$this->translate($text, $options);
// Or fetch the plugin first
$this->translate()->translate($text, $options);
It seems that the locale is sets not directly in the translating text, but by $this->getServiceLocator()->get('translator')->setLocale($locale), and now it is translating my text.
My Application mudole.config.php:
'service_manager' => array(
'factories' => array(
'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',
),
),
'translator' => array(
'locale' => 'en_US',
'translation_file_patterns' => array(
array(
'type' => 'gettext',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.mo',
),
),
),
And in the controller:
$this->getServiceLocator()->get('translator')->setLocale($locale);
echo $c=$this->getServiceLocator()->get('translator')->translate('Book'); // Print(Danish): Bog
You can use my ZfTranslate controller plugin.
installation
composer require mikica/zf2-translate-plugin
You need to register new module. Add in file config/application.config.php:
'modules' => array(
'...',
'ZfTranslate'
),
Usage in controller
<?php
$this->translate('translate word');
$this->translate('translate word', 'locale');
AlexP answer is the best way to do it.
But remains a question, why your way doesn't work?
It should work. But it doesn't because you are in different namespaces, so you are using distinct domains among the files. You doing something like that:
namespace MyModule\Controller;
class MyController {
public function someAction() {
$this->getServiceLocator()->get('translator')->translate('my text',__NAMESPACE__,$myLocale);
}
}
While at your `module.config.php', you probably using this namespace:
namespace MyModule;
return array(
//...
'translator' => array(
//...
),
);
Note that in the example of the controller __NAMESPACE__ is equals MyModule\Controller. While at the config file the __NAMESPACE__ is equals MyModule. You need to fix it, passing the same value in both cases.
In other words, there are several approachs to solve this, like AlexP's, for instance. But, any one of them need to have the domain of translator (the value of the 'text_domain' key) when you configure it equals the domain parameter (second paramater) of the translate method when you call it.
The faster solution is changing the $domain parameter to string at the controller file:
$this->getServiceLocator()->get('translator')->translate('my text','MyModule',$myLocale);
Another solution should be creating a constant and using it at the files (controllers, views and config).

Zend Navigation Helper not communicating with ZfcRbac

I have a problem implemented the ZfcRbac. At the moment the Rbac works and i enter a page on a certain url i will receive a 403. The code i used for this is the following:
'zfc_rbac' => array(
'guards' => array(
'ZfcRbac\Guard\RouteGuard' => array(
'relation*' => array('relation'),
),
),
'role_provider' => array(
'ZfcRbac\Role\InMemoryRoleProvider' => array(
'relation' => array(
'permissions' => array('view', 'edit', 'delete'),
),
),
),
),
The problem occurs when i try to let it talk with the Zend Navigation helper. Due some odd reason the navigation keeps telling me it can access the page. When i click it it seems you can't. Here is some code to check it
public function onBootstrap(MvcEvent $e)
{
$application = $e->getApplication();
$serviceManager = $application->getServiceManager();
$sharedEvents = $application->getEventManager()->getSharedManager();
$authorization = $serviceManager->get('ZfcRbac\Service\AuthorizationService');
$sharedEvents->attach(
'Zend\View\Helper\Navigation\AbstractHelper', 'isAllowed', function (EventInterface $e) use ($authorization)
{
$page = $e->getParam('page');
$permission = $page->getPermission();
return $authorization->isGranted($permission);
}
);
}
And the view helper will be called like:
<?=$this->navigation('navigation')->menu()->setPartial(array('sidebar.phtml', 'css'=>'nav navbar-nav'))->render();?>
Hope someone knows the answer on this one.
Thanks in advance.

How to add a class to all labels in a ZF2 form

I'm using a jQuery plugin that takes the text from labels associated with form elements and puts them as default text for the fields themselves. (You can find the plugin here.)
Here's the catch: it can only do this if the label has the class "inline". Now, I know I can use the following code to do this:
$this->add(array (
'name' -> 'name',
....
'options' => array (
'label' => 'Name',
'label_attributes' => array (
'class' => 'inline'
)
)
));
This will work fine, and if it has to be done item by item, then so be it. But I was wondering if there's some way I can add the class to ALL labels associated with text and text area form elements without using JavaScript. I'm thinking this would either done by a plugin, or by looping through all the elements in the form, but I don't know how to do either.
You could extend the FormRow view helper.
Here is a little example:
use Zend\Form\View\Helper\AbstractHelper;
use Zend\Form\View\Helper\FormRow;
class CustomFormRow extends FormRow
{
public function render(ElementInterface $element) {
...
$label = $element->getLabel();
if (isset($label) && '' !== $label) {
// Translate the label
if (null !== ($translator = $this->getTranslator())) {
$label = $translator->translate(
$label, $this->getTranslatorTextDomain()
);
}
$label->setAttribute('class', 'inline');
}
...
if ($this->partial) {
$vars = array(
'element' => $element,
'label' => $label,
'labelAttributes' => $this->labelAttributes,
'labelPosition' => $this->labelPosition,
'renderErrors' => $this->renderErrors,
);
return $this->view->render($this->partial, $vars);
}
...
}
You could probably leave the rest as it is and you should be good to go once you add some configuration in your Module.php for your view helper.
public function getViewHelperConfig() {
return array(
'factories' => array(
'CustomFormRow' => function($sm) {
return new \Application\View\Helper\CustomFormRow;
},
)
);
}
In your template files you now have to use your viewHelper instead.
<?php echo $this->CustomFormRow($form->get('yourelement')); ?>

yii CButton Column

How do i modify the links inside the CGridview?
This is from my view page:
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$new,
'columns'=>array(
'book.title',
'book.author',
'book.edition',
'date_borrowed',
'borrowed_status',
'date_returned',
'returned_status',
array(
'class'=>'Viewonly',
),
)
));
then from my Components:
class ViewOnly extends CButtonColumn {
public $template = '{view}';
}
what i want to happen "FOR EXAMPLE" if i click the view button in my CGridview, it will redirect me to http://www.google.com?action=someaction. how can i do this?
You don't need a separate button class. Do something like this:
array(
'class'=>'CButtonColumn',
'template'=>'{view} {google}',
'viewButtonUrl=>'Yii::app()->createUrl("http://google.com/",array("q"=>$data->name))',
'google'=>array(
... Init code for this button here
),
)
There is a document in here, you can check it.
By the way, document says, you can use url parameter in Array:
array(
'url' => '', //url comes here
),
Edit: Array must be out of columns.

Resources