Yii: localization in Navbar with images - php

It would be nice to have a Dropdown menu with several flags.Is there a way to show a image like a Flag in the navigation when I use twitter-bootstrap?
If I select one flag, the language and the flag should change.
I can show the bootstrap supported Icons but they contain no flags.
I hope some can help me. below is my navigation.
$this->widget('bootstrap.widgets.TbNavbar', array(
'type' => 'null', // null or 'inverse'
'brand' => '<img src="http://localhost/images/logo.gif">',
'brandUrl' => 'localhost',
'collapse' => true, // requires bootstrap-responsive.css
'items' => array(
array(
'class' => 'bootstrap.widgets.TbMenu',
'type' => 'pills', // '', 'tabs', 'pills' (or 'list')
'items' => array(
array('label' => 'Home', 'url' => array('/site/index')),
array('label' => 'user create', 'url' => array('/account/user/create')),
array('label' => 'Contact', 'url' => array('/site/contact')),
array('label' => 'User', 'url' => array('/account/user/index')),
),
),
array(
'class' => 'bootstrap.widgets.TbMenu',
'type' => 'pills', // '', 'tabs', 'pills' (or 'list')
'htmlOptions' => array('class' => 'pull-right'),
'items' => array(
/**Added to show, that here should appear the flag*/
array('label' => '','htmlOptions'=> array('src'=>'http://localhost/images/german.png'), 'url' => ''),
array('label' => 'Sign in', 'url' => array('/account/signup/login'), 'visible' => Yii::app()->user->isGuest),
array('label' => 'Sign up', 'url' => array('/account/signup/registration'), 'visible' => Yii::app()->user->isGuest),
array('label' => Yii::app()->user->name , 'url' => '#', 'items' => array(
array('label' => 'Action', 'url' => '#'),
array('label' => 'Another action', 'url' => '#'),
array('label' => 'Something else here', 'url' => '#'),
'---',
array('label' => 'Logout', 'url' => array('/account/signup/logout')),
),'visible' => !Yii::app()->user->isGuest),
),
),
),
));

As you noticed, you'll need flag icons to start with. You could, for example, use these. You may or may not need to modify the returned CSS a little to suit your needs.
Next you need to create the switch-URLs. This essentially boils down to having the current URL AND the language selection as get parameter. Create a canonical link for the site you are currently on and attach ?lang=<target>. Another approach would be to simply attach the switch part to the current URL and use that instead. I outlined something like that here.
Next you need to make sure your application can actually handle localization. I have no idea how far along that road you are yet, so you may want to read this. To actually have the languages switch on request you can do the following in protected/components/Controller.php
public function init() {
parent:init();
if (null === Yii::app()->user->getState('lang') || null !== Yii::app()->request->getQuery('lang', null)) {
Yii::app()->user->setState('lang', Yii::app()->request->getQuery('lang','defaultLanguage'));
}
Yii::app()->setLanguage(Yii::app()->user->getState('lang'));
}
This way, wherever you are, the language gets set based on the get parameter and persists as long as the session exists. You may even want to replace 'defaultLanguage' with a call to a function that determines a default value e.g. based on the user agent string of the browser or the visitor's IP.
To generate the language files you need/want take a look at this.
To create the necessary links inside the menu you can either create a function returning the appropriate array or switch the values such as label and url with a ternary operator, e.g. 'label' => ('en' == Yii::app()->language) ? 'Switch to Spanish' : 'Switch to English'. However, this only properly works with binary decisions. If you want to offer more languages you should consider a different approach, such as the function I mentioned.

Related

Adding a dropdown menu in Prestashop 1.7 module

I'm so beginner in Prestashop 1.7, I wanted to add a dropdown select section in my banner module to select the way to open the banner link.
but the selected value is never passed to the HTML, the code below IS passed but the one under isn't, can you please assist me?
[enter image description here][1]
array(
'type' => 'text',
'lang' => true,
'label' => $this->trans('Banner description', array(), 'Modules.Banner.Admin'),
'name' => 'BANNER_DESC',
'desc' => $this->trans('Please enter a short but meaningful description for the banner.', array(), 'Modules.Banner.Admin')
)
array(
'type' => 'select', //select
'lang' => true,
'label' => $this->trans('Banner tab', array(), 'Modules.Banner.Admin'),
'name' => 'BANNER_TAB',
'required'=>'true',
'options' => array(
'query' => array(
array('key' => '_blank', 'name' => 'New tab'),
array('key' => '_self', 'name' => 'Same tab'),
),
'id' => 'key',
'name' => 'name'
),
'desc' => $this->trans('Please select the way to open the link.', array(), 'Modules.Banner.Admin')
)
This is how it looks in the Backoffice:
Here
You not only need to add a new field to your form but also handle saving the data from it.
Take a look at a few examples:
https://github.com/PrestaShop/ps_featuredproducts/blob/dev/ps_featuredproducts.php#L122
Notice how the module author managed to save each configuration field from the form. This is what you need to do.
If you want to have access to data in your view, you have to pass it:
https://github.com/PrestaShop/ps_featuredproducts/blob/dev/ps_featuredproducts.php#L244
Maybe after you added a new field, you forgot to handle the saving + passing to the view?

Access properties and methods of a class while configuring EntityType in Formbuilder::add()

I`ve got a dropdown list in my Symfony2 form like this:
$builder->add('categories','entity', array(
'class' => 'MyBundle:Myentity',
'property' => 'name',
'label' => 'Mylabel',
'expanded' => false,
'multiple' => false,
'label_attr' => array ( 'class' => 'control-label' ),
'attr' => array ( 'class' => 'form-control',
'placeholder' => 'Placeholder',
'title' => "Mytitle",
'data-toggle' => 'tooltip',
'data-myidfromDB' => '????',
'data-mynamefromDB'=>'????' etc. )));
So I am getting a list of MyBundle:Myentity objects and when I choose one I want to show all its properties (like ID, name, etc.) which are stored in my DB and described in Entity class, in different html data-* fields. If I select another one from the list I want to see all information related to my newly selected option in HTML (to change dynamically). Any ideas how to do that?
Since Symfony 2.7 you can set the option choice_attr to ChoiceType and set it a callable receiving the choice as an argument.
EntityType inherits this option and the choice in that case is the instantiated entity, so you can write something like :
$builder->add('categories','entity', array(
'class' => 'MyBundle:MyEntity',
'property' => 'name',
'label' => 'Mylabel',
'attr' => array('class' => 'form-control'),
'label_attr' => array('class' => 'control-label'),
'choice_attr' => function (\AppBundle\Entity\MyEntity $myEntity) {
return array(
'data-private-property' => $entity->getPrivateProperty(),
'data-some-value' => $entity->someMethod(),
);
},
);
You can't do that in easy way.
But you can put more information in select label.
Look on
http://symfony.com/doc/current/reference/forms/types/entity.html#choice-label
Yout can put here more field details and get it from your javascript.

Cake PHP Form getting data from input and passing it to the url

I have my view controller that grabs a code number from the url
users/view/10024 or users/view/10004 etc
in my user index.ctp
echo $this->Form->create(null, array(
'url' => array('controller' => 'users', 'action' => 'view2'),
'type' => 'get',
'inputDefaults' => array(
'label' => false,
'div' => false
)
));
echo $this->Form->input(
'Enter Code',
array('label' => 'Code')
);
$options = array(
'label' => 'Submit',
'div' => array(
'class' => 'glass-pill',
)
);
I get this
/users/view?Enter+Code=1001
I want
/users/view/1001
So i want to grab the input data and put it in the url
This cannot be done using PHP.
Instead you need to use the rewriting engine of Apache: MOD_REWRITE http://httpd.apache.org/docs/2.0/misc/rewriteguide.html
Create a simple rewrite rule that transforms the URL in the way you want to achieve.
You can see an example on rewriting URL's here: http://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/

Zend Form Validate URL

I am currently validating a URL using the Regex Pattern and it appears to be working correctly. However, if I leave the URL field blank, it should not check the Regex Validation or perhaps just return a message like "No URL given".
Here is an example of my current code I'm working with:
array(
'name' => 'programurl1',
'attributes' => array(
'type' => 'text',
'error_msg' => 'Enter Valid Program URL 1',
'label_msg' => 'Program URL 1 *'
),
'validation' => array(
'required' => true,
'filters' => array(
array(
'name' => 'StripTags'
),
array(
'name' => 'StringTrim'
)
),
'validators' => array(
array(
'name' => 'Regex',
'options' => array(
'pattern' => '/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/'
)
)
)
)
)
I'm not certain how to accomplish what I am looking for when the URL field is blank.
Instead of a type text, you can use the url type. That is specifically meant to enter url values:
$this->add(array(
'name' => 'programurl',
'type' => 'Zend\Form\Element\Url',
'options' => array(
'label' => 'Program URL 1'
),
'attributes' => array(
'required' => 'required'
)
));
The url element is a special HTML5 element, see also the docs.
Zend\Form\Element\Url is meant to be paired with the Zend\Form\View\Helper\FormUrl for HTML5 inputs with type url. This element adds filters and a Zend\Validator\Uri validator to it’s input filter specification for validating HTML5 URL input values on the server.
Afaik if the browser cannot render the url input element, it just shows the text input as a fallback.

Defining multiple streams and adding fields to each of them in PyroCMS

I am new to PyroCMS and am willing to build a Job Site wherein there'll be 2 main users namely, Employers and Job Seekers. In order to allow them to register on the site, I'm using the Streams API from PyroCMS to build the forms. These users will be part of 2 different modules namely the Employer module and the Job Seeker module.
In the details.php file, under the install() function, I want to create multiple streams(database tables). The following code helps us to add a stream:
$this->streams->streams->add_stream();
The following code then helps us to define the fields to be added to the stream:
$this->streams->fields->add_fields($fields);
My concern is how do I add multiple streams like the above ones and add fields to each of them? In other words, how would the syntax
$this->streams->fields->add_fields($fields);
know which stream to add the fields to?
Have a look at the Fields Driver documentation for the Streams API. Fields and streams are separate entities, with no required association between the two. When adding a field you can assign it to a stream like this:
$field = array(
'name' => 'Question',
'slug' => 'question',
'namespace' => 'streams_sample',
'type' => 'text',
'extra' => array('max_length' => 200),
'assign' => 'STREAM_SLUG_GOES_HERE',
'title_column' => true,
'required' => true,
'unique' => true
);
$this->streams->fields->add_field($field);
Or you can create the streams and fields separately, and then assign each field to a stream like this:
$this->streams->fields->assign_field('streams_sample', 'STREAM_SLUG_GOES_HERE', 'question', array('required' => true));
All this talk of fields and streams makes me want to go outside...
You can add multiple streams like this example.
// Add banners streams
if ( ! $this->streams->streams->add_stream(lang('banner:banners'), 'banners', 'banner', 'banner_', null)) return false;
// Add groups streams
if ( ! $this->streams->streams->add_stream(lang('banner:groups'), 'groups', 'banner', 'banner_', null)) return false;
// Add some fields
$fields = array(
// BANNERS
array(
'name' => 'Banner Title',
'slug' => 'banner_title',
'namespace' => 'banner',
'assign' => 'banners',
'type' => 'text',
'extra' => array('max_length' => 200),
'title_column' => true,
'required' => true,
'unique' => true
),
// GROUPS
array(
'name' => 'Group Title',
'slug' => 'group_title',
'namespace' => 'banner',
'assign' => 'groups',
'type' => 'text',
'extra' => array('max_length' => 200),
'title_column' => true,
'required' => true,
'unique' => true
)
);
$this->streams->fields->add_fields($fields);

Categories