ZF2 Zend_Translate content to get search completion - php

i'm searching for a solution to solve the following problem in zf2:
i have a list of destinations, and i want a search-formular on my page with auto-completion.
also the list is used to translate parts of my web-page, so i will use zend_translate to do this.
it it possible to "reverse-search" the list of translations with zend_translate?
Example:
$translate->getKeysByExpression('*ger*');
result:
array (
array ( 'key' => '__germany__', 'name' => 'germany'),
array ( 'key' => '__trn_landwithger__', 'name' => 'landwithger'
)
it is not the biggest problem if i have to load the complete list of translations, it is not that much.
or is it better to use mongodb as backend for zend_translate, and use direct queries to find the completion-candidates?

Related

Adding Product Bundle and variation child to Order/Cart using add_bundle_to_order()

I'm trying to add a Product Bundle to an Order (or a Cart, add_bundle_to_cart uses the same args). The Bundle has several child products, all of which have variations. The args I need to be providing according to the documentation are:
$args = array(
81 => array(
'product_id' => 1543,
'quantity' => 3,
),
84 => array(
'product_id' => 1386,
'quantity' => 1,
'variation_id' => 3535,
'attributes' => array(
'attribute_pa_attribute-1' => 'value-1b',
'attribute_pa_attribute-2' => 'value-2b',
),
)
);
Where the second bundled product (84) is a variable product. My issue: I cannot verify whether the values I am providing for the 'attributes' array are correct.
My attribute is named 'Servings' and there are two options - '2 People' and '4 People'. I am not sure how to format the attribute name to replace 'attribute_pa_attribute-1'. I have tried 'servings', 'attribute_pa_servings', etc. If I return the variation information for the product it gives me: 'attribute_servings' as the name and '2 People' as the value. But that doesn't seem to work either.
I am not getting any errors returned in debug.log, and I cannot find any examples of someone using this in the land of Google.
If anyone has any experience with this, I would greatly appreciate a tip as to what I am doing wrong.
Many thanks.
I came to realise there are two different types of 'attributes' in Woo: global attributes (across all products), and local attributes (specific to one product). The quoted instructions from the documentation are referring to how to reference global attributes - I am using local ones. So that is why the terms weren't working.
General overview here.
Information on the syntax between the two here.

Set alias for index using PHP client library in elasticsearch?

I am creating search application using elasticsearch. I want to make indexing with zero downtime. I found one solution using aliases with client library in php. But I don't know how to implement it using client library in php.
In order to add an alias to an existing index, you need to call the updateAliases method:
$params['body'] = array(
'actions' => array(
array(
'add' => array(
'index' => 'your_index',
'alias' => 'your_alias'
)
)
)
);
$client->indices()->updateAliases($params);
If you are looking for a more complete solution, have a look at http://elasticorn.net - you can configure your index via yaml and it automatically creates them with aliases and the possibility to remap / reconfigure indices on the fly.

Mailchimp grouping doesn't seem to work

I am trying to implement an opt-in on a website to add a newly registered user to a mailing list on Mailchimp. This user can either be an individual or a business client and has to be in different groups.
Now what I've done is, I created 1 list with 1 group and in that group I created 2 subgroups(?)
Like there's a group title and there's groupings underneath. Now I try to use the API Wrapper provided over here: https://github.com/drewm/mailchimp-api/ and used this method for the API request:
$r = $mailchimp->call('lists/subscribe', array(
'id' => 'REMOVED_ID',
'email' => array(
'email' => 'REMOVED_EMAIL'
),
'merge_vars' => array(
'groupings' => array(
'id' => REMOVED_ID,
'groups' => array('REMOVED_NAME')
)
)
));
For obvious reasons I've replaced the actual values with placeholders.
What happens is: The user with the opt-in gets a email asking for their confirmation, and then the email adres gets added to the list but they're not in any groups.
Am I doing something wrong or am I being impatient?
I've found out what the problem is for this particulair problem. The JSON produced by the MailChimp class looks like this:
{"id":"ID_REMOVED","email":{"email":"EMAIL_REMOVED"},"merge_vars":{"FNAME":"Captain","LNAME":"Jack","groupings":{"id":ID_REMOVED,"groups":["GROUP_NAME_REMOVED"]}},"apikey":"API_KEY_REMOVED"}
But it should have [] around the groupings like so:
"groupings":[{"id":ID_REMOVED,"groups":["GROUP_NAME_REMOVED"]}]
Now I don't know how to achieve this, but for future reference, this is what caused my problem

ZF2 global config settings

My goal is to have configuration settings shared and editable within each module in a Zend Framework 2 project. After research, I came to this: http://akrabat.com/zend-framework-2/injecting-configuration-into-a-zf2-controller/ which worked great at first but after my application keeps expanding to more modules and more controllers, I don't appreciate this solution. Precisely, I don't appreciate having to "implement" interface, add SetConfig method and so on. Instead, I'd like to be able to just call on $GLOBALS or similar in my controller and get the parameter of interest.
Say I have a file config.local.php with the following format:
return array(
'group1' => array(
'key1' => 'value1',
'key2' => 'value2',
)
'group2' => array (
'subgroup' => array (
'keyA' => 'valueA',
'keyB' => 'valueB',
),
)
);
Right now, i'm following the tutorial above and after implemeting the ConfigAwareInterface in my controller, I get valueA using
$this->config['group2']['subgroup']['keyA']
I'm now hoping to just rather call $GLOBALS['group2']['subgroup']['keyA'] without the ConfigAwareInterface intermediate.
I laid the following quick pseudo code to help me achieve that:
for every this->config as key => value
if $key is not array
$GLOBALS[$key] => $value
else
for every $value as $k => $v
$GLOBALS[$key][$k] => $value
As ZF2 and php are new to me, I'm not completely sure I can have more than 1 index/nested keys for $GLOBALS but I can tailor my config file to avoid that.
There are probably other logical mistakes above. But just to get the point. Now, is this the best way to go about global configuration or is there a better approach?
Thanks in advance.
You can always easily access the configuration via:
$this->getServiceLocator()->get('config');
What Akrabat did was to provide a shortHand getConfig()-

Dynamic input generation with Zend_Form

Good morning,
I have a multilingual backend application which has to support n languages.
The languages are stored in a database and can be added at any moment.
Now for my backend functionality, I need to dynamically generate <input> fields based on the languages stored in my db, for example:
German: <input type="text" id="title_de">
English <input type="text" id="title_en">
Italian <input type="text" id="title_it">
...
I currently add them like this using Zend_Form:
foreach($languages as $language){
$title = new Zend_Form_Element_Text("title_" . $language->getPrefix());
$title->setLabel("Title " . $language->getPrefix())
->setRequired(true)
$this->addElements(array($title));
}
And this is how I save the fields to my DB using Doctrine:
//get language-dependent title and write to DB
foreach($languages as $language){
$title->setLanguage($language);
$title->setContent($form->getValue("title_" . $language->getPrefix()));
$touristArea->addTitle($title);
//persist and flush
$em->persist($touristArea);
$em->flush();
}
Anyway, as I have many language dependent fields (Titles, Captions, etc.), the code to process the form can get quite nasty and I always have to query the language table in my DB in order to know what inputs have been generated.
Is there a better way to do this? Like with an array of inputs or something like that? Thanks for any inputs :)
I suggest you to use Zend_Translate component
First of all create source file with translations ->
Creating Array source files
Array source files are plain arrays. But you have to define them manually since there is no tool to aid this. But because they are so simple, it's the fastest way to look up messages if your code works as expected. It's generally the best adapter to get started with translation business.
$english = array(
'message1' => 'message1',
'message2' => 'message2',
'message3' => 'message3');
$german = array(
'message1' => 'Nachricht1',
'message2' => 'Nachricht2',
'message3' => 'Nachricht3');
$translate = new Zend_Translate(
array(
'adapter' => 'array',
'content' => $english,
'locale' => 'en'
)
);
$translate->addTranslation(array('content' => $german, 'locale' => 'de'));
Since release 1.5 it is also supported to have arrays included within an external file. You just have to provide the filename and Zend_Translate will automatically include it and look for the array. See the following page for more info about source files
Then take a look for Internationalization of Zend_Form
In order to initialize I18n in forms, you will need either a Zend_Translate object or a Zend_Translate_Adapter object, as detailed in the Zend_Translate documentation. Once you have a translation object, you can attach a translator to a specific form instance or to specific elements using their setTranslator() methods:
// Tell *this* form instance to use a specific translate adapter; it
// will also be used to translate validation error messages for all
// elements:
$form->setTranslator($translate);
// Tell *this* element to use a specific translate adapter; it will
// also be used to translate validation error messages for this
// particular element:
$element->setTranslator($translate);
Detailed guide is here

Categories