Multiple languages in cakephp issue - can't get login to work - authentication

I followed this tutorial to create multilingual site.
http://nuts-and-bolts-of-cakephp.com/2008/11/28/cakephp-url-based-language-switching-for-i18n-and-l10n-internationalization-and-localization/
However although I can get to the login page at example.com/en/users/login when i try to login, the login wouldn't go through. I get thrown at example.com/users/login without the /en/ and I don't know if this matters, but the password does not contain 4 letters/stars/dots like the password i tried, but about 40.
Also, I noticed that the example.com/en/users/logout function does work to log me out, but takes me to example.com/users/login instead of example.com/en/users/logout
I managed to get to the fact that the function that breaks it is the:
class AppHelper extends Helper {
function url($url = null, $full = false) {
if(!isset($url['language']) && isset($this->params['language'])) {
$url['language'] = $this->params['language'];
}
return parent::url($url, $full);
}
}
However, without it, no urls work at all in terms of the /en/ addition in the URL.
Can anyone help? I will provide any code needed.

This was quite a journey to make it work. Some of the things I still don't get, but at least I know they work. If you make an improvement on this let me know, I'd like to be aware of it.
This tutorial in the question only gets you half way.
In addition, here are a few things that need to be moded to get this working:
Routes.php:
/**
* LANGUAGES
*/
Router::connect( '/:language/:plugin/:controller/:action/*', array(), array('language' => '[a-z]{3}')); // PLUGIN FIX
Router::connect('/:language/:controller/:action/*', array('plugin' => null), array('language' => '[a-z]{3}'));
Router::connect('/:language', array('controller'=>'static', 'plugin'=>null), array('language' => '[a-z]{3}'));
App_controller.php:
function beforeFilter(){
$this->_setLanguage();
$this->Auth->logoutRedirect = array( 'controller' => 'static', 'action' => 'index', 'language'=>$this->Session->read('Config.language'));
$this->Auth->loginRedirect = array( 'controller' => 'static', 'action' => 'dashboard', 'language'=>$this->Session->read('Config.language'));
$this->Auth->loginAction = array( 'controller'=>'users', 'action'=>'login', 'language'=>$this->Session->read('Config.language'));
}
function _setLanguage() {
if ($this->Cookie->read('lang') && !$this->Session->check('Config.language')) {
// If ... don't really get this.
$this->Session->write('Config.language', $this->Cookie->read('lang'));
}
else if (isset($this->params['language']) && ($this->params['language']
!= $this->Session->read('Config.language'))) {
// Had a language set, but different from the one in the URL, set to URL
$this->Session->write('Config.language', $this->params['language']);
$this->Cookie->write('lang',$this->params['language'], false, '360 days');
}else{
// First time comer
$this->Session->write('Config.language', Configure::read('Config.language' ));
$this->Cookie->write('lang', Configure::read('Config.language' ), false, '360 days');
}
}
function redirect( $url, $status = NULL, $exit = true ) {
if (!isset($url['language']) && $this->Session->check('Config.language')) {
$url['language'] = $this->Session->read('Config.language');
}
parent::redirect($url,$status,$exit);
}
app_helper.php:
function url($url = null, $full = false) {
if(!isset($url['language']) && isset($this->params['language'])) {
$url['language'] = $this->params['language'];
}else if (!isset($url['language']) && !isset($this->params['language'])){
if($_COOKIE['lang']){
$url['language'] = $_COOKIE['lang'];
}else{
$url['language'] = Configure::read('Config.language');
}
}
return parent::url($url, $full);
}
And this should be it. Hope that helps all of you lost souls with multilingual cakephp stuff
pp

I used the same resource to make cakephp multilingual, but today got stuck with that problem that by default login action loses language. When I found this post, the only thing that I need in addition to the original manual is the following code in controller_app.php beforeFilter:
$this->Auth->logoutRedirect = array( 'language'=>$this->Session->read('Config.language'));
$this->Auth->loginRedirect = array( 'language'=>$this->Session->read('Config.language'));
$this->Auth->loginAction = array( 'controller'=>'users', 'action'=>'login', 'language'=>$this->Session->read('Config.language'));

I saw your other question about multilingual setting. I was going to say that just make the default language to be 'en'. But I'm uncertain how that would affect SEO, so I didn't say anything. But yeah, the way you are doing right now might interfere with many Cake automagic, like the Auth problem you are having.
Here what I'd suggest: In app_controller:
function beforeFilter(){
$this->Auth->loginAction = array(
'controller' => 'users',
'action' => 'login' // insert the language option that you use here
);
$this->Auth->logoutRedirect = array(
'controller' => 'users',
'action' => 'index'
);// you can set where to redirect after logout here
}
I would advise not to redirect to /users/logout, unless you want to do something there. And you would need to change the logout function in users_controller too.

One thing that helped me, (not perfect but it's a start)
is i created exceptions for the 'loginPage' (the login action for the auto component)
so the language of the url doesn't get populated when logging in.
this helped me as i wasn't able to login until i did this
i added this to the app_helper.php
> //exceptions to allow to login
> if (isset($url['action'])) {
> if ($url['action'] == 'loginPage') $url['language'] = false;
> }

Related

backdrop cms module-created blocks deltas not working

(tagging with drupal7 because i cannot create a tag for backdrop)
i have a fresh backdrop install, with a new theme duped from bartik, and a new layout duped from moscone_flipped. no code changes to those yet.
i have a module that creates 2 simple blocks, mostly just some html. i have implemented hook_block_info() and hook_block_view(). i can place blocks into regions in the layout using the admin ui. i can see each block on the front end page when i place either one of them. but not both. when i have both blocks placed in the layout, for some reason both regions display the output from the same block. and i have verified that it is always the first block defined in the array returned from hook_block_info(). i have cleared caches, checked code, etc.
has anyone seen this before?
btw, i just applied the recent security upgrade, and the behavior is the same both before and after the upgrade.
i will paste in the module code below, in case i have missed something.
thanks for any help anyone can provide.
// implements hook_block_info()
function mbr_block_info()
{
$info = array();
$info['rate-tables'] = array(
'info' => 'Rate Tables (Buttons)',
'description' => 'The displays the rate table links for the sidebar',
);
$info['mbr-footer'] = array(
'info' => 'MBR Footer',
'description' => 'Displays footer links, disclaimer, copyright',
);
return($info);
}
// implements hook_block_view()
function mbr_block_view($delta = '', $settings = array(), $contexts = array())
{
$block = array();
switch($delta)
{
case 'mbr-footer':
$subject = null;
$mbrFooter = getMBRFooterBlock();
$block = array('subject' => $subject, 'content' => $mbrFooter);
case 'rate-tables':
$subject = null;
$rateTables = getRateTablesBlock();
$block = array('subject' => $subject, 'content' => $rateTables);
}
return($block);
}

Drupal module's list of permissions are stuck

From what I understand, my problem is that I am (rather, my client is) running an older version of Drupal, specifically Core 6.26 If you're curious about any additional specs, I'll be happy to divulge.
With that out of the way, I made a new module with the following menu hook.
function checkin_menu(){
$items = array();
$items['checkin'] = array(
'title' => 'Checkin'
,'type' => MENU_CALLBACK
,'access arguments' => array('checkin')
,'page callback' => 'checkin'
);
}
The permissions listed out exactly what I expected. There was a section called "Checkin" the same as the module's name as specified in the .info file, and one item to give permissions to "checkin"
Later on I expanded the module to have two different paths. The second one is supposed to be for admins only.
function checkin_menu(){
$items = array();
$items['checkin'] = array(
'title' => 'Checkin'
,'type' => MENU_CALLBACK
,'access arguments' => array('create a checkin')
,'page callback' => 'checkin'
);
$items['checkin_admin'] = array(
'title' => 'Checkin Admin'
,'type' => MENU_CALLBACK
,'access arguments' => array('view all checkins')
,'page callback' => 'device_checkin_page'
);
return $items;
}
Much to my surprise neither "create a checkin" or "view all checkins" is showing up. I still have the original "checkins" showing on the permissions page. I've been hunting for answers for a couple days now. Help a guy out?
Permissions are defined by a different hook, which is hook_perm.
So you should be doing something like this:
/**
* Implementation of the hook_perm()
*/
function checkin_perm() {
return array (
'create a checkin',
'view all checkins',
);
}

Accessing magento's checkout/cart using REST API

I'm trying to integrate a cart-synchronisation-solution for my rest-clients.
The goal should be that I can have the same cart wherever I access my store from.
So I'll have to first of all deliver the existing items out to the client using the authenticated api-user.
But I get stuck at the very beginning:
protected function _retrieveCollection()
{
$cart = Mage::getSingleton('checkout/cart')->getQuote();
$cart->setCustomerId($this->getApiUser()->getUserId());
$cart->setStoreId(4);
$cart->load();
return $cart->getAllItems();
}
returns an empty array even though I have products in my cart.
Anyone any hints? Have that feeling I'm totally on the wrong side...
Found a solution. Getting the quote by Customer which is the other way around worked pretty well:
Mage::app()->setCurrentStore(4);
$cart = Mage::getModel('sales/quote')->loadByCustomer($this->getApiUser()->getUserId());
$items = array();
foreach ($cart->getAllVisibleItems() as $key => $item) {
$items[] = array(
'name' => $item->getName(),
'entity_id' => $item->getProductId(),
'description' => $item->getDescription(),
'final_price_with_tax' => $item->getBasePriceInclTax(),
'qty' => $item->getQty()
);
}

limit number of results using magento API V2

Hi I have searched the site for my question but haven't found an easy solution and I think the issue is so basic.
I'm using Api V2 so maybe there's a solution now. Here I go, this is my code:
$api_soap_url = 'http://localhost/magento/api/v2_soap?wsdl=1';
$client = new SoapClient($api_soap_url);
$session_id = $client->__soapCall('login',array($user, $pw));
$data = array($session_id);
$result = $client->__soapCall('customerCustomerList', $data);
This returns all results, I need to limit number of result so I have tried using filters and other solutions found here but no luck.
The only one I haven't tried is this one:
Control the number of results from a Magento API call
But filtering by date doesn't solve my problem and rewriting classes is a ver complex solution for such a simple need.
Thanks in advance
I'm not sure the filter can limit number of result but you can try this:
$complexFilter = array(
'complex_filter' => array(
array(
'key' => 'created_at',
'value' => array('key' => 'gt', 'value' => '2012-05-13 06:11:00')
// where created_at is greater than 2012-05-13 06:11:00
// For example: eq (equals), neq (not equals), gt (greater than), lt (less than), etc.
)
)
);
$result = $client->customerCustomerList($session, $complexFilter);
I ended up overriding app/code/core/Mage/Sales/Model/Order/Api.php, adding a "special magic" field called "collection.limit". Your mileage may vary; I have tight controls on both the Magento installation and the programs (in this case, a set of C# programs) accessing the Magento installation.
My caller simply uses the "magic field" as a key/ value pair, something like this (please test, again, I was calling from C#, so this php should be considered suspect):
$collectionLimitClause = array (
'key' => 'collection.limit',
'value' => array('key' => 'eq', 'value' => '10')
);
In my Magento installation (this part is tested, live and running), I created a Sales/Model/Order/Api.php in my local namespace and over-rode the items function. Around the 32nd or so line of that function, you'll see this:
$apiHelper = Mage::helper('api');
$filters = $apiHelper->parseFilters($filters, $this->_attributesMap['order']);
try {
foreach ($filters as $field => $value) {
$orderCollection->addFieldToFilter($field, $value);
}
} catch (Mage_Core_Exception $e) {
$this->_fault('filters_invalid', $e->getMessage());
}
Instead, I "catch" my own magic limiter with the strncmp here, with an if-else inside the foreach:
$apiHelper = Mage::helper('api');
$filters = $apiHelper->parseFilters($filters, $this->_attributesMap['order']);
try {
foreach ($filters as $field => $value) {
if( !strncmp($field,"collection.limit",16) ) {
$orderCollection->getSelect()->limit($value['eq']);
}
else {
$orderCollection->addFieldToFilter($field, $value);
}
}
} catch (Mage_Core_Exception $e) {
$this->_fault('filters_invalid', $e->getMessage());
}
I'm not overly excited by this, but, I think it's pretty safe and it works.

Symfony file upload - "Array" stored in database instead of the actual filename

I'm using Symfony 1.4.4 and Doctrine and I need to upload an image on the server.
I've done that hundreds of times without any problem but this time something weird happens : instead of the filename being stored in the database, I find the string "Array".
Here's what I'm doing:
In my Form:
$this->useFields(array('filename'));
$this->embedI18n(sfConfig::get('app_cultures'));
$this->widgetSchema['filename'] = new sfWidgetFormInputFileEditable(array(
'file_src' => '/uploads/flash/'.$this->getObject()->getFilename(),
'is_image' => true,
'edit_mode' => !$this->isNew(),
'template' => '<div id="">%file%</div><div id=""><h3 class="">change picture</h3>%input%</div>',
));
$this->setValidator['filename'] = new sfValidatorFile(array(
'mime_types' => 'web_images',
'path' => sfConfig::get('sf_upload_dir').'/flash',
));
In my action:
public function executeIndex( sfWebRequest $request )
{
$this->flashContents = $this->page->getFlashContents();
$flash = new FlashContent();
$this->flashForm = new FlashContentForm($flash);
$this->processFlashContentForm($request, $this->flashForm);
}
protected function processFlashContentForm($request, $form)
{
if ( $form->isSubmitted( $request ) ) {
$form->bind( $request->getParameter( $form->getName() ), $request->getFiles( $form->getName() ) );
if ( $form->isValid() ) {
$form->save();
$this->getUser()->setFlash( 'notice', $form->isNew() ? 'Added.' : 'Updated.' );
$this->redirect( '#home' );
}
}
}
Before binding my parameters, everything's fine, $request->getFiles($form->getName()) returns my files.
But afterwards, $form->getValue('filename') returns the string "Array".
Did it happen to any of you guys or do you see anything wrong with my code?
Edit: I added the fact that I'm embedding another form, which may be the problem (see Form code above).
Alright, I got it. I wasn't properly declaring my validator.
What i should've done is:
$this->setValidator('filename', new sfValidatorFile(array(
'mime_types' => 'web_images',
'path' => sfConfig::get('sf_upload_dir').'/flash',
)));
Silly mistake, I hope that will help those who have the same problem.
Alternatively you can use;
$this->validatorSchema['filename']
in place of;
$this->setValidator['filename']