CakePHP adding image to echo $this->Html->link - image

Hi I have tried various things but not sure if I am trying to over complicate things. Quite simply I want to add a small image country flag for the word 'Spanish' and 'English' and have an 'Alt' tag with those words instead. The following works fine with just text and as is but it would be cool to have the image either to replace or sit side by side with the word. Any help available?
echo $this->Html->link('Spanish', array('controller'=>'settings', 'action'=>'lang', 'spa'),
array('rel'=>'nofollow'));
}
else {
echo $this->Html->link('English', array('controller'=>'settings', 'action'=>'lang', 'eng'),
array('rel'=>'nofollow'));
}

You can either output just a linked image using the image method with a url attribute:-
<?php
echo $this->Html->image(
'spain.jpg',
array(
'alt' => 'Spanish',
'url' => array('controller' => 'settings', 'action' => 'lang', 'spa')
)
);
Or you can include an image with a text link by combining the normal CakePHP link method and the image method:-
<?php
echo $this->Html->link(
h('Spanish') . $this->Html->image('spain.jpg', array('alt' => 'Spanish')),
array('controller' => 'settings', 'action' => 'lang', 'spa'),
array('escape' => false, 'rel' => 'nofollow')
);
With the link method you need to remember to prevent Cake from escaping the img tag using 'escape' => false. If you disable escaping like this it is work remembering to escape any user provided text by using the h() method to prevent any HTML injection (I've shown this in my example wrapping the word 'Spanish' but this is only necessary if this is coming from a variable).

if you are using cake2 see this link to the cookbook
echo $this->Html->image("spain.jpg", array(
"alt" => "Spanish language",
'url' => array('controller' => 'settings', 'action' => 'lang', 'spa')
));

Related

Codeigniter: creating helper with associative array

I have a number of social networking links (about 5) in the footer of my site and I need to generate data for each of the anchor link’s TITLE and URL - and ICON (image).
It’s not really worth creating a new table for only 5 rows, so I’m thinking a helper might be a the answer - some sort of associative array.
However, I’m a bit unsure how to construct the helper’s array - and completely clueless as to how to loop the results in the view file.
Any help, or any links to useful examples, is greatly appreciated!
In your helper:
function footer_link_data()
{
return array(
array(
'title' => 'Facebook',
'url' => 'http://facebook.com',
'icon' => 'http://placehold.it/64x64'
),
array(
'title' => 'Twitter',
'url' => 'http://twitter.com',
'icon' => 'http://placehold.it/64x64'
),
array(
'title' => 'Pinterest',
'url' => 'http://pinterest.com',
'icon' => 'http://placehold.it/64x64'
),
);
}
In your view:
<?php
$links = footer_link_data();
foreach($links as $link): ?>
<?php echo $link['icon'] ?>
<?php
endforeach; ?>
Alternatively, you can just create the HTML statically. For 5 links having it generated dynamically is kind of pointless.

How to translate form labels in Zend Framework 2?

I'm not getting it!.. Can please someone explain, how to translate form labels? A simple example would be great.
Thank you in advance!
class Search\Form\CourseSearchForm
...
class CourseSearchForm extends Form {
...
public function __construct(array $cities) {
parent::__construct('courseSearch');
...
$this->add(array(
'name' => 'city',
'type' => 'Zend\Form\Element\Select',
'options' => array(
'label' => 'Stadt',
'value_options' => $this->cities,
'id' => 'searchFormCity',
),
));
...
}
}
view script /module/Search/view/search/search/search-form.phtml
<?php echo $this->form()->openTag($form); ?>
<dl>
...
<dt><label><?php echo $form->get('city')->getLabel(); ?></label></dt>
<dd><?php echo $this->formRow($form->get('city'), null, false, false); ?></dd>
...
</dl>
<?php echo $this->form()->closeTag(); ?>
<!-- The formRow(...) is my MyNamespace\Form\View\Helper (extends Zend\Form\View\Helper\FormRow); the fourth argument of it disables the label. -->
The module/Application/config/module.config.php is configured:
return array(
'router' => ...
'service_manager' => array(
'factories' => array(
'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',
),
),
'translator' => array(
'locale' => 'de_DE',
'translation_file_patterns' => array(
array(
'type' => 'gettext',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.mo',
),
),
),
'controllers' => ...
'view_manager' => ...
);
I also edited my view and use the FormLabel view helper:
<dt><label><?php echo $this->formLabel($form->get('city')); ?></label></dt>
Furthermore I debugged the FormLabel at the place, where the tranlator is used (lines 116-120) -- seems to be OK.
But it's still not working.
EDIT
The (test) items for labels, I added to the de_DE.po file manually, are tranlated. The ZF2 side problem was actually, that I was using $form->get('city')->getLabel() instead of $this->formlabel($form->get('city')) in th view script.
The problem is now, that the labels are not added to the de_DE.po file. But it's not a ZF2 issue anymore, so I've accept Ruben's answer and open a new Poedit question.
Instead of using:
<?php echo $form->get('city')->getLabel(); ?>
You should use the formlabel view helper. This helper automatically uses your translator during rendering if you have inserted it in your ServiceManager. Most likely you will have it in your Application's module module.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',
),
),
),
Once you do use the formlabel view helper:
echo $this->formLabel($form->get('city'));
And of course make sure your translations are in your .po file.
i think your problem is that you label are not detected by poedit (or similar tool), so you have to add them manually to your poedit catalogs (.po)
to make your label strings detected by tools like poedit, your strings need to be used inside a translate() function or _() (other function can be added in Catalog/properties/sources keyword)
as the _() function is not user in ZF2 (today) so a tiny hack is to add a function like this in your index.php (no need to modify anything, this way, in poedit params):
// in index.php
function _($str)
{
return $str;
}
and in your code, just use it when your strings are outside of a translate function
//...
$this->add(array(
'name' => 'city',
'type' => 'Zend\Form\Element\Select',
'options' => array(
'label' => _('myLabel') , // <------ will be detected by poedit
'value_options' => $this->cities,
'id' => 'searchFormCity',
),
));
//...
or like this if you prefer
$myLabel = _('any label string'); // <--- added to poedit catalog
//...
'options' => array(
'label' => $myLabel ,
'value_options' => $this->cities,
'id' => 'searchFormCity',
),
#Ruben says right!
Me I use PoEdit to generate my *.mo files and to be sure to get all translations in the file, I create somewhere (in view for example) a file named _lan.phtml with all text to be translated :
<?php echo $this->translate("My label");
... ?>
Of course, Poedit has to be configured to find my keywords. check this to how to configure it
All solutions don't use the power of ZF2. You must configure your poedit correctly :
All things are here :
http://circlical.com/blog/2013/11/5/localizing-your-twig-using-zend-framework-2-applications

CakePHP 2.1 - Callback functions for JsHelper / AJAX

Using the JsHelper for CakePHP 2.x, for each callback function, is it possible to have more than one selector being subject to various effects. For example, I am using:
echo $this->Js->submit('thumbs-up-green.jpg', array(
'id' => 'thumbs-up-green',
'before' => $this->Js->get('#thumbs-down-red')->effect('fadeOut'),
'success' => $this->Js->get('#thumbs-down-gray')->effect('fadeIn')
));
Let's say I want to apply an effect on #thumbs-down-gray in the before callback function as well (in addition to the effect on #thumbs-down-red which I currently have), what is the syntax for that? I have been searching around but documentation is limited for the JsHelper.
Additionally a simpler question, the JsHelper submit button / form seems to perform a line-break even if CSS display:none; is active. How do I get rid of that line-break?
I haven't found a very neat JsHelper solution, I don't think I can chain on additional elements and actions.
So I think for more complex callback functions the solution is to execute a pre-loaded function like:
'before' => 'someFunction();',
But if you just want to for example fadeOut multiple elements in one go then you could write it like:
'before' => $this->Js->get('#thumbs-down-red, #other-element')->effect('fadeOut'),
You have to set the "before"-action in a variable before the actual submit-call. Like this:
$before = $this->Js->get('#skadetyp_form')->effect('fadeOut', array('buffer' => false));
$before .= '$(\'#notice\').append(\'<div class="notice">Sending..<br/>' . $this->Html->image('load_bar.gif', array('alt' => 'Loading..')) . '</div>\')';
$complete = $this->Js->get('#notice div')->effect('fadeOut', array('buffer' => false));
$complete .= '$(\'#notice\').append(\'<div class="success">Succssfully seny!</div>\')';
echo $this->Js->submit('Send!',
array(
'update'=>'#skadetyp_form',
'complete' => $complete,
'before' => $before,
'error' => $error,
'async' => true,
'method' => 'post',
'dataExpression'=>true,
'data'=> $this->Js->serializeForm(
array(
'isForm' => true,
'inline' => true
)
)
)
);

Data validation with custom route issue (default url when errors occur)

I'm building a user panel, and having some problems with data validation. As an example, the page where you change your password (custom validation rule comparing string from two fields (password, confirm password)):
Route:
Router::connect('/profile/password', array('controller' => 'users', 'action' => 'profile_password'));
Controller:
function profile_password()
{
$this->User->setValidation('password'); // using the Multivalidatable behaviour
$this->User->id = $this->Session->read('Auth.User.id');
if (empty($this->data))
{
$this->data = $this->User->read();
} else {
$this->data['User']['password'] = $this->Auth->password($this->data['User']['password_change']);
if ($this->User->save($this->data))
{
$this->Session->setFlash('Edytowano hasło.', 'default', array('class' => 'success'));
$this->redirect(array('action' => 'profile'));
}
}
}
The problem is, that when I get to http://website.com/profile/password and mistype in one of the fields, the script goes back to http://website.com/users/profile_password/5 (5 being current logged users' id). When I type it correctly then it works, but I don't really want the address to change.
It seems that routes aren't supported by validation... (?) I'm using Cake 1.3 by the way.
Any help would be appreciated,
Paul
EDIT 1:
Changing the view from:
echo $form->create(
'User',
array(
'url' => array('controller' => 'users', 'action' => 'profile_password'),
'inputDefaults' => array('autocomplete' => 'off')
)
);
to:
echo $form->create(
'User',
array(
'url' => '/profile/password',
'inputDefaults' => array('autocomplete' => 'off')
)
);
does seem to do the trick, but that's not ideal.
Check the URL of the form in the profile_password.ctp view file.
Try the following code:
echo $this->Form->create('User', array('url' => array('controller' => 'users', 'action' => 'profile_password')));
Also, I think your form might be a little vulnerable. Try using Firebug or something similar to POST a data[User][id] to your action. If I'm right, you should be setting:
$this->data['User']['id'] = $this->Auth->user('id');
instead of:
$this->User->id = $this->Session->read('Auth.User.id');
because your id field is set in $this->data.
HTH.

Simultaneous ajax requests

I am trying to run the remote function in the code below every 5 seconds. It runs only once if I remove the frequency option. I tried remoteTimer function, but when I use remoteTimer function, some code of the script goes outside the script tags and I see that in the webpage.
echo $ajax->form(array('type' => 'post', 'options' => array('model' => 'Thing',
'url' => array('controller' => 'things', 'action' => 'xyz'),'update' => 'dy4', 'indicator' => 'ldng', 'loading' => ( $ajax->
remoteFunction(array('url' => array('controller' => 'stories', 'action' =>
'keep'), 'update' => 'dy3', 'frequency' => 5))))));echo $form->input('a', array('type' => 'checkbox'));echo $form->input('b', array('type' => 'checkbox')); echo $form->end('RUN');
If in case, this cannot be done using CakePHP helpers, how can I do it with JavaScript?
Use remoteTimer in a very simple controller. See the page source and between <script> tags you'll get code that does what you want.You can use that code as value for "loading" option.

Resources