How to convert params file to i18n in yii2 - yii2

I try to convert params.php string to i18n in yii2, But, it's can't convert.
And another all string are converted sucessfully.
Here is my file.
params.php
<?php
return [
'bsVersion' => '4.x',
'bsDependencyEnabled' => false,
'adminEmail' => 'admin#example.com',
'unauthorized_error'=>'You are not access this action.',
'status_change_success'=>'Records status has been changed..',
'records_save_success'=> Yii::t('params', 'Records save has been success..'),
'status_change_error'=>'Records status not changed..',
'records_delete_success'=>'Records has been deleted..',
'records_delete_error'=>'Records not deleted..',
'records_not_found'=>'Data not found..',
'execution_error' => 'Oops!!! There was some problem while executing your request. Try again later.',
'reset_password_request' => 'Your password reset link has been sent you in your email , please check your email inbox or spam folder',
'password_change_success'=>'Passwod change successfully!!',
'password_wrong' => 'Your password is wrong!',
'inactive_user' => 'You have inactive user. Please contact to website admin',
'not_registered_user' => 'You have not registered. Please register first then login',
'enter_user_pass' => 'Enter username and password!',
'something_wrong' => 'Something went wrong!',
'server_error' => 'Server error',
'student_not_found' => 'Student not found.',
'teacher_not_found' => 'Teachers not found.',
'validation_error' => 'Validation Error.',
'comment_success' => 'Your Comment send successfully!.',
'attendance_save_success' => 'Attendance save successfully!.',
'homework_save_success' => 'Homework save successfully!.',
'register_successfully' => 'User registered successfully!',
'photo_gallery_path' =>'uploads/photo_gallery/',
'appointment_status_change' =>'Appointments status has been changed!',
'leave_application_status_change'=>'Leave application status changed..',
'timetable_import_success' => 'Your timetable import successfully',
'group_type' =>[
"7" => "Hostel Rector",
"8" => "Security"
],
'day' => [
'1' => Yii::t('params', 'Monday'),
'2' => Yii::t('params', 'Tuesday'),
'3' => Yii::t('params', 'Wednesday'),
'4' => Yii::t('params', 'Thursday'),
'5' => Yii::t('params', 'Friday'),
'6' => Yii::t('params', 'Saturday'),
// '7' => 'Sunday',
],
....
'syllabus_status' =>[
'1' => Yii::t('params', 'Running'),
'2' => Yii::t('params', 'Finish'),
],
'news_event' =>[
'1' => Yii::t('params', 'News'),
'2' => Yii::t('params', 'Event'),
],
'exam_type' => [
'1' => Yii::t('params', 'Weekly'),
'2' => Yii::t('params', 'Monthly'),
'3' => Yii::t('params', 'Yearly'),
],
'lang_list' => [
'1' => Yii::t('params', 'English'),
'2' => Yii::t('params', 'Gujarati'),
]
];

You cannot use Yii::t() for translating params directly in config file - this file is used before application is initialized, so at this point Yii is not able to detect current language and i18n is not yet initialized. The easiest way is to postpone translation - put untranslated strings in params:
'lang_list' => [
'1' => 'English',
'2' => 'Gujarati',
]
And translate when you need it:
echo Yii::t('param', Yii::$app->params['lang_list']['1']);
Alternatively you may use beforeRequest event to generate params after app initialization:
'on beforeRequest' => function ($event) {
Yii::$app->params += [
'syllabus_status' => [
'1' => Yii::t('params', 'Running'),
'2' => Yii::t('params', 'Finish'),
],
'news_event' => [
'1' => Yii::t('params', 'News'),
'2' => Yii::t('params', 'Event'),
],
'exam_type' => [
'1' => Yii::t('params', 'Weekly'),
'2' => Yii::t('params', 'Monthly'),
'3' => Yii::t('params', 'Yearly'),
],
'lang_list' => [
'1' => Yii::t('params', 'English'),
'2' => Yii::t('params', 'Gujarati'),
],
];
},

Related

Order my consulting by field of my contain

I want order by proyecto_id who is in my ProyectosCategoraisTareas and also order by my categoria id, this is my contain Tareas.Categorias.
I try this but not work.
$proyectostareas = $this->ProyectosCategoriasTareas
->find('all', [
'contain' => [
'Proyectos',
'Tareas',
'Tareas.Categorias' => [
'order' => [
'Tarea.Categoria.id' => 'DESC'
]
]
]
])
->where([
'activa' => '1'
])
->order([
'proyecto_id' => 'DESC'
]);
Try this:
$proyectostareas = $this->ProyectosCategoriasTareas
->find('all')
->where([
'activa' => '1'
])
->join([
'Proyectos',
'Tareas'])
->order([
'proyecto_id DESC',
'Tarea.Categoria.id DESC'
]);

Yii2 formatting boolean field in GridView

I remember to have done this before, but now it does not work and I can't get it out.
[
'label' => 'Sex',
'attribute' => 'gan_sex',
'filter' => [
'1' => 'Male',
'2' => 'Female'
]
],
The output is
1
2
2
1
instead of
Male
Female
Female
Male
What is the problem now? I'd swear I used it just the same way but ...
I do it like this
[
'label' => 'Sex',
'attribute' => 'gan_sex',
'filter' => [
'1' => 'Male',
'2' => 'Female'
],
// translate lookup value
'value' => function ($model) {
$gender = [
'1' => 'Male',
'2' => 'Female'
];
return $gender[$model->gan_sex];
}
]
Possible values for gan_sex must be restricted to 1 and 2.

validation error on patchEntity telling field is not unique

I update an entity (so same id) and I get a validation error on a field which has a 'unique' rule.
I verified in the db that there is no an existing identical field and so I don't understand why I have this error as I update the record so obviously the field exists already because it's itself!
Is it a problem to have a 'unique' rule on patchEntity()?
[
'site_name' => '...',
'datas' => object(App\Model\Entity\User) {
'id' => (int) 653,
'owner_id' => (int) 611,
'extraemails' => [
],
'owner' => object(App\Model\Entity\Owner) {
'id' => (int) 611,
'company' => 'This is the Company name',
'created' => object(Cake\I18n\Time) {
'time' => '2015-06-06T18:07:17+0000',
'timezone' => 'UTC',
'fixedNowTime' => false
},
'modified' => object(Cake\I18n\Time) {
'time' => '2015-06-17T02:44:36+0000',
'timezone' => 'UTC',
'fixedNowTime' => false
},
'[new]' => false,
'[accessible]' => [
'email' => true,
'phone' => true,
'phone2' => true,
'fax' => true,
'company' => true,
'tva_number' => true,
'address' => true,
'postcode' => true,
'city' => true,
'country_id' => true,
'users' => true,
'username' => true,
'origin' => true
],
'[dirty]' => [
'email' => true,
'phone' => true,
'postcode' => true,
'city' => true
],
'[original]' => [
'email' => '...',
'phone' => '...',
'postcode' => '...',
'city' => '...'
],
'[virtual]' => [],
'[errors]' => [
'company' => [
'unique' => 'This company name exists already.'
]
],
'[repository]' => 'Owners'
},
....
EDIT patchEntity() call added
$user = $this->Users->patchEntity($site->users[0], [
'email' => $this->siteDatas['userEmail'],
'phone' => $this->siteDatas['ownerPhone'],
'role' => 'owner',
'active' => 1,
'extraemails' => $this->siteDatas['extraemails'],
'owner' => [
'company' => $this->siteDatas['ownerName'],
'email' => $this->siteDatas['ownerEmail'],
'phone' => $this->siteDatas['ownerPhone'],
'address' => $this->siteDatas['ownerAddress'],
'postcode' => $this->siteDatas['ownerPostcode'],
'city' => $this->siteDatas['ownerCity'],
'country_id' => $this->siteDatas['countryId'],
'web' => $this->siteDatas['ownerWeb'],
'origin' => 'sitra',
],
],
[
'validate' => 'import',
'associated' => [
'Extraemails',
'Owners' => [
'validate' => 'import'
]
]
]);
if ($user->errors()) {
// Here is where I get the 'unique' error
}
So again I verified that no other record in the table have the same 'company' name.
and as you can see in the debug() at the begining of my post, I have a owner->id and it's really the one I want to modify.
Here is the validator
public function validationImport(Validator $validator) {
$validator
->add('id', 'valid', ['rule' => 'numeric'])
->allowEmpty('id', 'create')
->notEmpty('company')
->add('company', 'unique', ['rule' => 'validateUnique', 'provider' => 'table', 'message' => __("This company exists already.")])
...
])
;
return $validator;
}

Bareword "ident" not allowed while "strict subs" in use at cgi-bin/test.pl

I attempting to take two separate tables, in two separate databases, and compare the fields to each other. I wrote the below code to demonstrate the problem I am having, since I can not copy the code I am using. In the example below I am using one database. Below the code example, I will provide the dump of each table. In any case on with a better description.
reference_table holds the records listed in original_table. I have no control over the contents of original_table. After obtaining these records, the person/s who edit the information in original_table can update any field, or they can downright remove the entry.
My goal is, when going back through original_table, to update the field status to disabled where any of the fields in the original_table no longer match the record I had in reference_table. When looking at the dump you will notice a field called unique_id. This is the id of original_table.
The two main problems that I run into is using the ident of reference_table to update status in reference_table, and properly seeing the tables similarities.
#!/usr/bin/perl
use strict;
use DBI;
use Data::Dumper;
my $dbh = DBI->connect('dbi:mysql:test','user','password',
{
RaiseError => 1,
AutoCommit => 0
}
) || die "Database connection not made: $DBI::errstr";
my $sql = "SELECT * FROM original_table";
my $sth = $dbh->prepare( $sql ) or die $dbh->errstr;
$sth->execute() or die $sth->errstr;
my #original;
while (my $data = $sth->fetchrow_hashref()){
push(#original, $data);
}
my $sql = "SELECT * FROM reference_table";
my $sth = $dbh->prepare( $sql ) or die $dbh->errstr;
$sth->execute() or die $sth->errstr;
my #reference;
while (my $data = $sth->fetchrow_hashref()){
push(#reference, $data);
}
foreach (sort keys #reference){
unless (exists $original[$_]){
my $sql = "UPDATE reference_table SET status = 'd' WHERE ident $reference[ident]";
my $sth = $dbh->prepare( $sql ) or die $dbh->errstr;
$sth->execute() or die $sth->errstr;
print "$_: not found in Remote Group\n";
next;
}
if ($reference[$_] eq $original[$_]){
print "$_: values are equal\n"
}else{
print "$_: values are not equal\n";
}
}
$dbh->disconnect();
The code produces the following results:
0: values are not equal
1: values are not equal
10: values are not equal
11: not found in Remote Group
2: values are not equal
3: values are not equal
4: values are not equal
5: values are not equal
6: values are not equal
7: values are not equal
8: values are not equal
9: values are not equal
The dump of each table:
#original
$VAR1 = [
{
'timezone' => 'tz_001',
'status' => 'a',
'full_name' => 'name_001',
'ident' => '1'
},
{
'timezone' => 'tz_002',
'status' => 'a',
'full_name' => 'name_002',
'ident' => '2'
},
{
'timezone' => 'tz_003',
'status' => 'a',
'full_name' => 'name_003',
'ident' => '3'
},
{
'timezone' => 'tz_004',
'status' => 'a',
'full_name' => 'name_004',
'ident' => '4'
},
{
'timezone' => 'tz_005',
'status' => 'a',
'full_name' => 'name_005',
'ident' => '5'
},
{
'timezone' => 'tz_006',
'status' => 'a',
'full_name' => 'name_006',
'ident' => '6'
},
{
'timezone' => 'tz_007',
'status' => 'a',
'full_name' => 'name_007',
'ident' => '7'
},
{
'timezone' => 'tz_008',
'status' => 'a',
'full_name' => 'name_008',
'ident' => '8'
},
{
'timezone' => 'tz_009',
'status' => 'a',
'full_name' => 'name_009',
'ident' => '9'
},
{
'timezone' => 'tz_010',
'status' => 'a',
'full_name' => 'name_010',
'ident' => '10'
},
{
'timezone' => 'tz_011',
'status' => 'a',
'full_name' => 'name_011',
'ident' => '11'
}
];
#reference
$VAR1 = [
{
'timezone' => 'tz_001',
'status' => 'a',
'full_name' => 'name_001',
'ident' => '1',
'unique_id' => '1'
},
{
'timezone' => 'tz_002',
'status' => 'a',
'full_name' => 'name_002',
'ident' => '2',
'unique_id' => '2'
},
{
'timezone' => 'tz_003',
'status' => 'a',
'full_name' => 'name_003',
'ident' => '3',
'unique_id' => '3'
},
{
'timezone' => 'tz_004',
'status' => 'a',
'full_name' => 'name_004',
'ident' => '4',
'unique_id' => '4'
},
{
'timezone' => 'tz_005',
'status' => 'a',
'full_name' => 'name_122',
'ident' => '5',
'unique_id' => '5'
},
{
'timezone' => 'tz_006',
'status' => 'a',
'full_name' => 'name_006',
'ident' => '6',
'unique_id' => '6'
},
{
'timezone' => 'tz_007',
'status' => 'a',
'full_name' => 'name_007',
'ident' => '7',
'unique_id' => '7'
},
{
'timezone' => 'tz_008',
'status' => 'a',
'full_name' => 'name_008',
'ident' => '8',
'unique_id' => '8'
},
{
'timezone' => 'tz_009',
'status' => 'a',
'full_name' => 'name_009',
'ident' => '9',
'unique_id' => '9'
},
{
'timezone' => 'tz_010',
'status' => 'a',
'full_name' => 'name_010',
'ident' => '10',
'unique_id' => '10'
},
{
'timezone' => 'tz_011',
'status' => 'a',
'full_name' => 'name_011',
'ident' => '11',
'unique_id' => '11'
},
{
'timezone' => 'tz_012',
'status' => 'a',
'full_name' => 'name_012',
'ident' => '12',
'unique_id' => '12'
}
];
Please let me know if you have additional questions. Any help is greatly appreciated.
"UPDATE reference_table SET status = 'd' WHERE ident $reference[ident]"
The problem is this $reference[ident]. You are trying to access an array element indexed by the bareword ident, but that does not work.
To correct this, use an appropriate index like $_. The next problem is that #reference is an array of hashrefs. I assume you want to use the ident field in that hashref. Then: $reference[$_]{ident}.
I also suggest you use placeholders to avoid possible issues with escaping:
my $sql = "UPDATE reference_table SET status = 'd' WHERE ident ?";
my $sth = $dbh->prepare($sql) or die $dbh->errstr;
$sth->execute($reference[$_]{ident}) or die $sth->errstr;
There are some remaining style issues with your code, but this should get you going.
You are using the string comparison operator eq to try to compare to hash references.
$reference[$_] eq $original[$_]
The "stringification" of a hash reference is a string like HASH(0xdeadbeef), so this will always be false unless $reference[$_] and $original[$_] refer to the exact same memory.
Even comparing all keys and values will not give you what you want, because the $reference[$_] hash reference contains the key unique_id and $original[$_] does not.
A more customized solution would be something like:
if (equivalent($reference[$_],$original[$_]) {
print "$_: values are equal\n"
} else {
print "$_: values are not equal\n";
}
sub equivalent {
my ($hash1, $hash2) = #_;
return $hash1->{timezone} eq $hash2->{timezone} &&
$hash1->{status} eq $hash2->{status} &&
$hash1->{full_name} eq $hash2->{full_name} &&
$hash1->{ident} eq $hash2->{ident} &&
}

Zend_Db_Expr('NOW()') and datetime in Mysql

So I am having a strange problem on live website, and I am not sure what is causing the problem since in datetime field in mysql table sometimes time is mapped and more often it isn't. However date is always mapped.
Code goes like this:
$party_system_id = $this->blabla->addUser($username, 'None', 'None', '',
$email,$promo_code,$distributor_id);
if ($party_system_id!=0){
$loginToken = md5(uniqid(rand(), true));
$params = array(
'sponsorId' => $sponsor_id,
'refferalId' => $referral_id,
'accountType' => $type,
'email' => $email,
'username' => $username,
'password' => md5($password),
'firstName' => 'n/a',
'lastName' => 'n/a',
'expires' => $expiration_date,
'activated' => 1,
'gender' => $gender,
'languageId' => 38,
'onlineStatus'=> 0,
'status' => 5,
'dateAdded' => new Zend_Db_Expr('NOW()'),
'blaId' => $party_system_id,
'loginToken' => $token,
'termsAccepted' => 1
);
return $this->insert($params);
}
And registration is allways fine, but like I said for lot of users insted of:
2012-05-16 10:20:33 I would get: 2012-05-16 00:00:00 and of-course it is not 00:00:00 at the moment of registration :)
I am not sure what can be a problem, mysql, script structure, or Zend_Db_Expr. I also don't have an idea how to properly debug this, but all suggestions are more then welcome :)
make sure dateAdded field does have Y-m-d H:i:s as a format,
but you can also simply use date('Y-m-d H:i:s')
$params = array(
'sponsorId' => $sponsor_id,
'refferalId' => $referral_id,
'accountType' => $type,
'email' => $email,
'username' => $username,
'password' => md5($password),
'firstName' => 'n/a',
'lastName' => 'n/a',
'expires' => $expiration_date,
'activated' => 1,
'gender' => $gender,
'languageId' => 38,
'onlineStatus'=> 0,
'status' => 5,
'dateAdded' => date('Y-m-d H:i:s'),
'blaId' => $party_system_id,
'loginToken' => $token,
'termsAccepted' => 1
);